Added touch controls to the main menu.

This commit is contained in:
Steven Mattera 2018-05-29 20:07:08 -04:00
parent 4539b8e78d
commit afbf85a65f
6 changed files with 109 additions and 6 deletions

View File

@ -30,6 +30,7 @@ static void menuAddEntry(menuEntry_s* me) {
m->firstEntry = me; m->firstEntry = me;
m->lastEntry = me; m->lastEntry = me;
} }
m->xPos = 0;
m->nEntries ++; m->nEntries ++;
} }

View File

@ -403,17 +403,16 @@ void menuLoop() {
} }
else else
{ {
static int x = 0;
static int v = 0; static int v = 0;
if (menu->nEntries > 7) { if (menu->nEntries > 7) {
int wanted_x = clamp(-menu->curEntry * (140 + 30), -(menu->nEntries - 7) * (140 + 30), 0); int wanted_x = clamp(-menu->curEntry * (140 + 30), -(menu->nEntries - 7) * (140 + 30), 0);
x += v; menu->xPos += v;
v += (wanted_x - x) / 3; v += (wanted_x - menu->xPos) / 3;
v /= 2; v /= 2;
} }
else { else {
x = v = 0; menu->xPos = v = 0;
} }
menuEntry_s *active_entry = NULL; menuEntry_s *active_entry = NULL;
@ -423,7 +422,7 @@ void menuLoop() {
int entry_start_x = 29 + i * (140 + 30); int entry_start_x = 29 + i * (140 + 30);
int screen_width = 1280; int screen_width = 1280;
if (entry_start_x >= (screen_width - x)) if (entry_start_x >= (screen_width - menu->xPos))
break; break;
int is_active = i==menu->curEntry; int is_active = i==menu->curEntry;
@ -431,7 +430,7 @@ void menuLoop() {
if (is_active) if (is_active)
active_entry = me; active_entry = me;
drawEntry(me, entry_start_x + x, is_active); drawEntry(me, entry_start_x + menu->xPos, is_active);
} }
if(active_entry != NULL) { if(active_entry != NULL) {

View File

@ -29,6 +29,7 @@ struct menu_s_tag
menuEntry_s *firstEntry, *lastEntry; menuEntry_s *firstEntry, *lastEntry;
int nEntries; int nEntries;
int curEntry; int curEntry;
int xPos;
char dirname[PATH_MAX+1]; char dirname[PATH_MAX+1];
}; };

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include "../common/common.h" #include "../common/common.h"
#include "nx_touch.h"
uint8_t* g_framebuf; uint8_t* g_framebuf;
u32 g_framebuf_width; u32 g_framebuf_width;
@ -110,6 +111,7 @@ bool menuUpdate(void) {
bool exitflag = 0; bool exitflag = 0;
menu_s* menu = menuGetCurrent(); menu_s* menu = menuGetCurrent();
u32 down = hidKeysDown(CONTROLLER_P1_AUTO); u32 down = hidKeysDown(CONTROLLER_P1_AUTO);
handleTouch(menu);
if (down & KEY_Y) if (down & KEY_Y)
{ {

89
nx_main/nx_touch.c Normal file
View File

@ -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(&currentTouch, 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(&currentTouch, 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;
}
}

11
nx_main/nx_touch.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include <switch.h>
struct touchInfo_s {
touchPosition* firstTouch;
touchPosition* prevTouch;
bool isTap;
int initMenuXPos;
int initMenuIndex;
};