added momentum to touch drags

This commit is contained in:
Chris Bradel 2020-06-24 05:20:26 -04:00
parent 16958ac4ba
commit 5b3377d364
5 changed files with 48 additions and 13 deletions

View File

@ -35,6 +35,7 @@ static void _menuAddEntry(menu_s *m, menuEntry_s* me) {
m->lastEntry = me;
}
m->xPos = 0;
m->slideSpeed = 0;
m->nEntries ++;
}
@ -59,6 +60,7 @@ static void menuAddEntryToFront(menuEntry_s* me) {
m->lastEntry = me;
}
m->xPos = 0;
m->slideSpeed = 0;
m->nEntries ++;
}

View File

@ -800,16 +800,42 @@ void menuLoop(void) {
int entries_count = layoutobj->posEnd[0];
layoutobj = &themeCurrent.layoutObjects[ThemeLayoutId_MenuList];
if (menu->nEntries > entries_count) {
int wanted_x = clamp(-menu->curEntry * layoutobj->posEnd[0], -(menu->nEntries - entries_count) * layoutobj->posEnd[0], 0);
menu->xPos += v;
v += (wanted_x - menu->xPos) / 3;
v /= 2;
// Gentle Realign only when not manually moving
if (menu->slideSpeed == 0) {
if (menu->nEntries > entries_count) {
int wanted_x = clamp(-menu->curEntry * layoutobj->posEnd[0], -(menu->nEntries - entries_count) * layoutobj->posEnd[0], 0);
menu->xPos += v;
v += (wanted_x - menu->xPos) / 3;
v /= 2;
}
else {
menu->xPos = v = 0;
}
}
else {
menu->xPos = v = 0;
menu->xPos += menu->slideSpeed;
if (abs(menu->slideSpeed) > 2) {
// Slow down way faster when outside the normal bounds
if (menu->xPos > 0 || menu->xPos < -(menu->nEntries) * layoutobj->posEnd[0]) {
menu->slideSpeed *= .5;
}
else {
menu->slideSpeed *= .9;
}
}
else {
menu->slideSpeed = 0;
}
menu->curEntry = clamp(round(-((double) menu->xPos / layoutobj->posEnd[0])), 0, menu->nEntries);
}
// Debug Text for Slide Speed
//char array[20];
//sprintf(array, "Slide Speed: %i", menu->slideSpeed);
//DrawText(layoutobj->font, 640, 60, themeCurrent.textColor, array);
menuEntry_s *active_entry = NULL;
// Draw menu entries

View File

@ -33,6 +33,7 @@ struct menu_s_tag
int nEntries;
int curEntry;
int xPos;
int slideSpeed;
char dirname[PATH_MAX+1];
};

View File

@ -15,6 +15,7 @@ void touchInit() {
touchInfo.isTap = true;
touchInfo.initMenuXPos = 0;
touchInfo.initMenuIndex = 0;
touchInfo.lastSlideSpeed = 0;
}
void handleTappingOnApp(menu_s* menu, int px) {
@ -72,25 +73,25 @@ void handleTouch(menu_s* menu) {
touchInfo.isTap = true;
touchInfo.initMenuXPos = menu->xPos;
touchInfo.initMenuIndex = menu->curEntry;
touchInfo.lastSlideSpeed = 0;
menu->slideSpeed = 0;
}
// On touch moving.
else if (touches >= 1 && touchInfo.gestureInProgress) {
hidTouchRead(&currentTouch, 0);
touchInfo.lastSlideSpeed = ((int)(currentTouch.px - touchInfo.prevTouch.px));
touchInfo.prevTouch = currentTouch;
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 > layoutobj->posStart[1] && touchInfo.firstTouch.py < layoutobj->posStart[1]+layoutobj->size[1] && !touchInfo.isTap && menu->nEntries > entries_count) {
menu->xPos = touchInfo.initMenuXPos + (currentTouch.px - touchInfo.firstTouch.px);
menu->curEntry = touchInfo.initMenuIndex + ((int) (touchInfo.firstTouch.px - currentTouch.px) / layoutobj->posEnd[0]);
if (menu->curEntry < 0)
menu->curEntry = 0;
if (menu->curEntry >= menu->nEntries - entries_count - 1)
menu->curEntry = menu->nEntries - entries_count;
if (!touchInfo.isTap) {
menu->slideSpeed = touchInfo.lastSlideSpeed;
}
}
}
// On touch end.
@ -100,6 +101,10 @@ void handleTouch(menu_s* menu) {
int x2 = touchInfo.prevTouch.px;
int y2 = touchInfo.prevTouch.py;
if (!touchInfo.isTap) {
menu->slideSpeed = touchInfo.lastSlideSpeed;
}
bool netloader_active = menuIsNetloaderActive();
if (menuIsMsgBoxOpen() && !netloader_active) {

View File

@ -10,6 +10,7 @@ struct touchInfo_s {
bool isTap;
int initMenuXPos;
int initMenuIndex;
int lastSlideSpeed;
};
void handleTouch(menu_s* menu);