From 9c859bdfe9f31a222ef85cb089b715b2e66951e7 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Mon, 9 Mar 2026 19:09:34 +0100 Subject: [PATCH] mpong: handle swipes --- c_mpos/mpong/mpong.c | 2 +- .../com.micropythonos.mpong/assets/mpong.py | 32 +++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/c_mpos/mpong/mpong.c b/c_mpos/mpong/mpong.c index 5fcc0a15..0fe96996 100644 --- a/c_mpos/mpong/mpong.c +++ b/c_mpos/mpong/mpong.c @@ -219,7 +219,7 @@ static MP_DEFINE_CONST_FUN_OBJ_0(render_obj, render); // move_paddle(delta): move the paddle horizontally by delta. static mp_obj_t move_paddle(mp_obj_t delta_obj) { int delta = mp_obj_get_int(delta_obj); - mp_printf(&mp_plat_print, "delta: %d\n", delta); + //mp_printf(&mp_plat_print, "delta: %d\n", delta); if (g_framebuffer_width > 0) { g_paddle_x = clamp_int(g_paddle_x + delta, 0, (int)g_framebuffer_width - g_paddle_width); } diff --git a/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py b/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py index f589134c..547e3be0 100644 --- a/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py +++ b/internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py @@ -19,6 +19,8 @@ class MPong(Activity): paddle_move_step = None layer = None buffer = None + touch_active = False + touch_last_x = None # Widgets: screen = None @@ -76,12 +78,28 @@ class MPong(Activity): self.canvas.invalidate() # force redraw def touch_cb(self, event): - # TODO: track lv.EVENT.PRESSED, lv.EVENT.PRESSING and lv.EVENT.RELEASED events to detect swipes left and right to move_paddle - event_code=event.get_code() - import mpos.ui - mpos.ui.print_event(event) - if event_code not in [19,23,25,26,27,28,29,30,49]: - if event_code == lv.EVENT.PRESSING: # this is probably enough + event_code = event.get_code() + if event_code == lv.EVENT.PRESSED: + x, y = InputManager.pointer_xy() + self.touch_active = True + self.touch_last_x = x + return + + if event_code == lv.EVENT.PRESSING: + if not self.touch_active: x, y = InputManager.pointer_xy() - mpong.move_paddle(-10) # TODO: set actual detected swipe left or right + self.touch_active = True + self.touch_last_x = x return + x, y = InputManager.pointer_xy() + if self.touch_last_x is not None: + delta = x - self.touch_last_x + if delta: + mpong.move_paddle(delta) + self.touch_last_x = x + return + + if event_code == lv.EVENT.RELEASED: + self.touch_active = False + self.touch_last_x = None + return