Skip to content

Commit 9c859bd

Browse files
mpong: handle swipes
1 parent 6f65e6c commit 9c859bd

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

  • c_mpos/mpong
  • internal_filesystem/apps/com.micropythonos.mpong/assets

c_mpos/mpong/mpong.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static MP_DEFINE_CONST_FUN_OBJ_0(render_obj, render);
219219
// move_paddle(delta): move the paddle horizontally by delta.
220220
static mp_obj_t move_paddle(mp_obj_t delta_obj) {
221221
int delta = mp_obj_get_int(delta_obj);
222-
mp_printf(&mp_plat_print, "delta: %d\n", delta);
222+
//mp_printf(&mp_plat_print, "delta: %d\n", delta);
223223
if (g_framebuffer_width > 0) {
224224
g_paddle_x = clamp_int(g_paddle_x + delta, 0, (int)g_framebuffer_width - g_paddle_width);
225225
}

internal_filesystem/apps/com.micropythonos.mpong/assets/mpong.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class MPong(Activity):
1919
paddle_move_step = None
2020
layer = None
2121
buffer = None
22+
touch_active = False
23+
touch_last_x = None
2224

2325
# Widgets:
2426
screen = None
@@ -76,12 +78,28 @@ def run_mpong(self, timer=None):
7678
self.canvas.invalidate() # force redraw
7779

7880
def touch_cb(self, event):
79-
# TODO: track lv.EVENT.PRESSED, lv.EVENT.PRESSING and lv.EVENT.RELEASED events to detect swipes left and right to move_paddle
80-
event_code=event.get_code()
81-
import mpos.ui
82-
mpos.ui.print_event(event)
83-
if event_code not in [19,23,25,26,27,28,29,30,49]:
84-
if event_code == lv.EVENT.PRESSING: # this is probably enough
81+
event_code = event.get_code()
82+
if event_code == lv.EVENT.PRESSED:
83+
x, y = InputManager.pointer_xy()
84+
self.touch_active = True
85+
self.touch_last_x = x
86+
return
87+
88+
if event_code == lv.EVENT.PRESSING:
89+
if not self.touch_active:
8590
x, y = InputManager.pointer_xy()
86-
mpong.move_paddle(-10) # TODO: set actual detected swipe left or right
91+
self.touch_active = True
92+
self.touch_last_x = x
8793
return
94+
x, y = InputManager.pointer_xy()
95+
if self.touch_last_x is not None:
96+
delta = x - self.touch_last_x
97+
if delta:
98+
mpong.move_paddle(delta)
99+
self.touch_last_x = x
100+
return
101+
102+
if event_code == lv.EVENT.RELEASED:
103+
self.touch_active = False
104+
self.touch_last_x = None
105+
return

0 commit comments

Comments
 (0)