added momentum to touch drags (#124)

* added momentum to touch drags
This commit is contained in:
Chris Bradel 2020-06-26 11:35:11 -04:00 committed by GitHub
parent 16958ac4ba
commit 45efcfcb98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 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,14 +800,35 @@ 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 *= .5f;
}
else {
menu->slideSpeed *= .9f;
}
}
else {
menu->slideSpeed = 0;
}
menu->curEntry = clamp(roundf(-((float) menu->xPos / layoutobj->posEnd[0])), 0, menu->nEntries);
}
menuEntry_s *active_entry = NULL;

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);