Added touch controls to the main menu.
This commit is contained in:
parent
4539b8e78d
commit
afbf85a65f
@ -30,6 +30,7 @@ static void menuAddEntry(menuEntry_s* me) {
|
||||
m->firstEntry = me;
|
||||
m->lastEntry = me;
|
||||
}
|
||||
m->xPos = 0;
|
||||
m->nEntries ++;
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -29,6 +29,7 @@ struct menu_s_tag
|
||||
menuEntry_s *firstEntry, *lastEntry;
|
||||
int nEntries;
|
||||
int curEntry;
|
||||
int xPos;
|
||||
|
||||
char dirname[PATH_MAX+1];
|
||||
};
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#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)
|
||||
{
|
||||
|
89
nx_main/nx_touch.c
Normal file
89
nx_main/nx_touch.c
Normal 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(¤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;
|
||||
}
|
||||
}
|
11
nx_main/nx_touch.h
Normal file
11
nx_main/nx_touch.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <switch.h>
|
||||
|
||||
struct touchInfo_s {
|
||||
touchPosition* firstTouch;
|
||||
touchPosition* prevTouch;
|
||||
bool isTap;
|
||||
int initMenuXPos;
|
||||
int initMenuIndex;
|
||||
};
|
Loading…
Reference in New Issue
Block a user