From afbf85a65f13e11756fbedb0e03a68f043808226 Mon Sep 17 00:00:00 2001 From: Steven Mattera Date: Tue, 29 May 2018 20:07:08 -0400 Subject: [PATCH] Added touch controls to the main menu. --- common/menu-list.c | 1 + common/menu.c | 11 +++--- common/menu.h | 1 + nx_main/main.c | 2 ++ nx_main/nx_touch.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++ nx_main/nx_touch.h | 11 ++++++ 6 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 nx_main/nx_touch.c create mode 100644 nx_main/nx_touch.h diff --git a/common/menu-list.c b/common/menu-list.c index ba7aacd..ff74afc 100644 --- a/common/menu-list.c +++ b/common/menu-list.c @@ -30,6 +30,7 @@ static void menuAddEntry(menuEntry_s* me) { m->firstEntry = me; m->lastEntry = me; } + m->xPos = 0; m->nEntries ++; } diff --git a/common/menu.c b/common/menu.c index ba2a80c..c8d44b6 100644 --- a/common/menu.c +++ b/common/menu.c @@ -403,17 +403,16 @@ void menuLoop() { } else { - static int x = 0; static int v = 0; if (menu->nEntries > 7) { int wanted_x = clamp(-menu->curEntry * (140 + 30), -(menu->nEntries - 7) * (140 + 30), 0); - x += v; - v += (wanted_x - x) / 3; + menu->xPos += v; + v += (wanted_x - menu->xPos) / 3; v /= 2; } else { - x = v = 0; + menu->xPos = v = 0; } menuEntry_s *active_entry = NULL; @@ -423,7 +422,7 @@ void menuLoop() { int entry_start_x = 29 + i * (140 + 30); int screen_width = 1280; - if (entry_start_x >= (screen_width - x)) + if (entry_start_x >= (screen_width - menu->xPos)) break; int is_active = i==menu->curEntry; @@ -431,7 +430,7 @@ void menuLoop() { if (is_active) active_entry = me; - drawEntry(me, entry_start_x + x, is_active); + drawEntry(me, entry_start_x + menu->xPos, is_active); } if(active_entry != NULL) { diff --git a/common/menu.h b/common/menu.h index 9b4a17e..1d94ff0 100644 --- a/common/menu.h +++ b/common/menu.h @@ -29,6 +29,7 @@ struct menu_s_tag menuEntry_s *firstEntry, *lastEntry; int nEntries; int curEntry; + int xPos; char dirname[PATH_MAX+1]; }; diff --git a/nx_main/main.c b/nx_main/main.c index cf6c2bf..c519f3e 100644 --- a/nx_main/main.c +++ b/nx_main/main.c @@ -3,6 +3,7 @@ #include #include "../common/common.h" +#include "nx_touch.h" uint8_t* g_framebuf; u32 g_framebuf_width; @@ -110,6 +111,7 @@ bool menuUpdate(void) { bool exitflag = 0; menu_s* menu = menuGetCurrent(); u32 down = hidKeysDown(CONTROLLER_P1_AUTO); + handleTouch(menu); if (down & KEY_Y) { diff --git a/nx_main/nx_touch.c b/nx_main/nx_touch.c new file mode 100644 index 0000000..271131e --- /dev/null +++ b/nx_main/nx_touch.c @@ -0,0 +1,89 @@ +#include "nx_touch.h" +#include "../common/common.h" + +#define TAP_MOVEMENT_GAP 20 +#define LISTING_START_Y 475 +#define LISTING_END_Y 647 + +struct touchInfo_s touchInfo; + +void touchInit() { + touchInfo.firstTouch = NULL; + touchInfo.prevTouch = NULL; + touchInfo.isTap = true; + touchInfo.initMenuXPos = 0; + touchInfo.initMenuIndex = 0; +} + +void handleTap(menu_s* menu, int px) { + int i = 0; + menuEntry_s *me = NULL; + + for (me = menu->firstEntry, i = 0; me; me = me->next, i ++) { + int entry_start_x = 29 + i * (140 + 30); + + int screen_width = 1280; + if (entry_start_x >= (screen_width - menu->xPos)) + break; + + if (px >= (entry_start_x + menu->xPos) && px <= (entry_start_x + menu->xPos) + 140 ) { + launchMenuEntryTask(me); + break; + } + } +} + +void handleTouch(menu_s* menu) { + touchPosition currentTouch; + u32 touches = hidTouchCount(); + + // On touch start. + if (touches == 1 && touchInfo.firstTouch == NULL) { + hidTouchRead(¤tTouch, 0); + + touchInfo.firstTouch = malloc(sizeof(touchPosition)); + touchInfo.prevTouch = malloc(sizeof(touchPosition)); + touchInfo.firstTouch->px = currentTouch.px; + touchInfo.firstTouch->py = currentTouch.py; + touchInfo.isTap = true; + touchInfo.initMenuXPos = menu->xPos; + touchInfo.initMenuIndex = menu->curEntry; + } + // On touch moving. + else if (touches >= 1 && touchInfo.firstTouch != NULL) { + hidTouchRead(¤tTouch, 0); + + touchInfo.prevTouch->px = currentTouch.px; + touchInfo.prevTouch->py = currentTouch.py; + + if (touchInfo.isTap && (abs(touchInfo.firstTouch->px - currentTouch.px) > TAP_MOVEMENT_GAP || abs(touchInfo.firstTouch->py - currentTouch.py) > TAP_MOVEMENT_GAP)) { + touchInfo.isTap = false; + } + if (!menuIsMsgBoxOpen() && touchInfo.firstTouch->py > LISTING_START_Y && touchInfo.firstTouch->py < LISTING_END_Y && !touchInfo.isTap && menu->nEntries > 7) { + menu->xPos = touchInfo.initMenuXPos + (currentTouch.px - touchInfo.firstTouch->px); + menu->curEntry = touchInfo.initMenuIndex + ((int) (touchInfo.firstTouch->px - currentTouch.px) / 170); + + if (menu->curEntry < 0) + menu->curEntry = 0; + + if (menu->curEntry >= menu->nEntries - 6) + menu->curEntry = menu->nEntries - 7; + } + } + // On touch end. + else if (touchInfo.firstTouch != NULL) { + if (menuIsMsgBoxOpen()) { + // Handle tapping ok. + } else { + if (touchInfo.firstTouch->py > LISTING_START_Y && touchInfo.firstTouch->py < LISTING_END_Y && touchInfo.isTap) { + handleTap(menu, touchInfo.prevTouch->px); + } + } + + free(touchInfo.firstTouch); + touchInfo.firstTouch = NULL; + + free(touchInfo.prevTouch); + touchInfo.prevTouch = NULL; + } +} \ No newline at end of file diff --git a/nx_main/nx_touch.h b/nx_main/nx_touch.h new file mode 100644 index 0000000..910d88b --- /dev/null +++ b/nx_main/nx_touch.h @@ -0,0 +1,11 @@ +#pragma once + +#include + +struct touchInfo_s { + touchPosition* firstTouch; + touchPosition* prevTouch; + bool isTap; + int initMenuXPos; + int initMenuIndex; +}; \ No newline at end of file