From 3c6acf47a5f76f3bafffff20baf1d86edb4bcc33 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Tue, 23 Jun 2026 12:16:33 -0400 Subject: [PATCH] Add touch support to Snake game by dividing the screen into Up/Down/Left/Right zones --- Apps/HelloWorld/main/Source/main.c | 2 +- Apps/Snake/main/Source/SnakeUi.c | 36 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Apps/HelloWorld/main/Source/main.c b/Apps/HelloWorld/main/Source/main.c index c1a5742..28b4c75 100644 --- a/Apps/HelloWorld/main/Source/main.c +++ b/Apps/HelloWorld/main/Source/main.c @@ -10,7 +10,7 @@ static void onShowApp(AppHandle app, void* data, lv_obj_t* parent) { lv_obj_align(toolbar, LV_ALIGN_TOP_MID, 0, 0); lv_obj_t* label = lv_label_create(parent); - lv_label_set_text(label, "Hello, world!"); + lv_label_set_text(label, "Hello, world!\n(Loaded via LAN!)"); lv_obj_align(label, LV_ALIGN_CENTER, 0, 0); } diff --git a/Apps/Snake/main/Source/SnakeUi.c b/Apps/Snake/main/Source/SnakeUi.c index e828460..a1a77aa 100644 --- a/Apps/Snake/main/Source/SnakeUi.c +++ b/Apps/Snake/main/Source/SnakeUi.c @@ -245,6 +245,41 @@ static void game_play_event(lv_event_t* e) { default: break; } + } else if (code == LV_EVENT_CLICKED) { + lv_indev_t* indev = lv_indev_active(); + if (indev) { + lv_point_t point; + lv_indev_get_point(indev, &point); + + lv_area_t coords; + lv_obj_get_coords(game->container, &coords); + + lv_coord_t w = coords.x2 - coords.x1 + 1; + lv_coord_t h = coords.y2 - coords.y1 + 1; + lv_coord_t cx = coords.x1 + w / 2; + lv_coord_t cy = coords.y1 + h / 2; + + lv_coord_t rx = point.x - cx; + lv_coord_t ry = point.y - cy; + + if (rx != 0 || ry != 0) { + if (abs(rx) * h > abs(ry) * w) { + // Horizontal touch + if (rx > 0) { + snake_set_direction(game, SNAKE_DIR_RIGHT); + } else { + snake_set_direction(game, SNAKE_DIR_LEFT); + } + } else { + // Vertical touch + if (ry > 0) { + snake_set_direction(game, SNAKE_DIR_DOWN); + } else { + snake_set_direction(game, SNAKE_DIR_UP); + } + } + } + } } else if (code == LV_EVENT_KEY) { uint32_t key = lv_event_get_key(e); // Arrow keys, WASD, and punctuation keys for cardputer @@ -394,6 +429,7 @@ lv_obj_t* snake_create(lv_obj_t* parent, uint16_t cell_size, bool wall_collision // Add event callbacks for touch gestures and keyboard lv_obj_add_event_cb(game->container, game_play_event, LV_EVENT_GESTURE, obj); lv_obj_add_event_cb(game->container, game_play_event, LV_EVENT_KEY, obj); + lv_obj_add_event_cb(game->container, game_play_event, LV_EVENT_CLICKED, obj); lv_obj_add_event_cb(obj, delete_event, LV_EVENT_DELETE, NULL); // Set up keyboard focus if available