#!/usr/bin/env python3 """ Test swipe detection in Pygame HAL. This will show you how to perform swipes and what gestures are detected. """ import asyncio import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) from dreader.hal_pygame import PygameDisplayHAL from dreader.gesture import GestureType from PIL import Image, ImageDraw, ImageFont async def test_swipes(): """Test swipe detection with visual feedback.""" print("=" * 70) print("Swipe Detection Test") print("=" * 70) print("\nInstructions:") print(" - Click and drag to create swipes") print(" - Drag at least 30 pixels for swipe detection") print(" - Short movements are detected as taps") print(" - Press Q or ESC to quit") print("\nSwipe directions:") print(" - Drag LEFT → Next page (SWIPE_LEFT)") print(" - Drag RIGHT → Previous page (SWIPE_RIGHT)") print(" - Drag UP → Scroll up (SWIPE_UP)") print(" - Drag DOWN → Scroll down (SWIPE_DOWN)") print("\nOR use keyboard shortcuts:") print(" - Left/Right Arrow or Space/PageUp/PageDown") print("\n" + "=" * 70) # Create HAL hal = PygameDisplayHAL(width=800, height=600, title="Swipe Detection Test") await hal.initialize() # Create instruction image img = Image.new('RGB', (800, 600), color=(240, 240, 240)) draw = ImageDraw.Draw(img) try: font_large = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 32) font_small = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20) except: font_large = ImageFont.load_default() font_small = ImageFont.load_default() # Draw instructions y = 50 draw.text((400, y), "Swipe Detection Test", fill=(0, 0, 0), font=font_large, anchor="mt") y += 80 instructions = [ "Click and DRAG to create swipes:", "", "← Drag LEFT = Next Page", "→ Drag RIGHT = Previous Page", "↑ Drag UP = Scroll Up", "↓ Drag DOWN = Scroll Down", "", "Minimum drag distance: 30 pixels", "", "Or use keyboard:", "Space/Right Arrow = Next", "Left Arrow = Previous", "", "Press Q or ESC to quit" ] for line in instructions: draw.text((400, y), line, fill=(0, 0, 0), font=font_small, anchor="mt") y += 30 await hal.show_image(img) # Event loop hal.running = True gesture_count = 0 last_gesture = None print("\nWaiting for gestures... (window is now open)") while hal.running: event = await hal.get_touch_event() if event: gesture_count += 1 last_gesture = event.gesture print(f"\n[{gesture_count}] Detected: {event.gesture.value}") print(f" Position: ({event.x}, {event.y})") # Visual feedback feedback_img = img.copy() feedback_draw = ImageDraw.Draw(feedback_img) # Draw gesture type gesture_text = f"Gesture #{gesture_count}: {event.gesture.value.upper()}" feedback_draw.rectangle([(0, 550), (800, 600)], fill=(50, 150, 50)) feedback_draw.text((400, 575), gesture_text, fill=(255, 255, 255), font=font_large, anchor="mm") await hal.show_image(feedback_img) # Brief pause to show feedback await asyncio.sleep(0.5) await hal.show_image(img) await asyncio.sleep(0.01) await hal.cleanup() print("\n" + "=" * 70) print(f"Test complete! Detected {gesture_count} gestures.") if last_gesture: print(f"Last gesture: {last_gesture.value}") print("=" * 70) if __name__ == "__main__": asyncio.run(test_swipes())