Skip to content

Commit

Permalink
- Fixed Rosalina menu handling for hidden items
Browse files Browse the repository at this point in the history
- Added a Rosalina menu reset selected item (needed for dynamic menus)
  • Loading branch information
valx76 committed Mar 6, 2022
1 parent 4f4e4b0 commit c307dc6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions sysmodules/rosalina/include/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ void menuEnter(void);
void menuLeave(void);
void menuThreadMain(void);
void menuShow(Menu *root);
void menuResetSelectedItem(void);
45 changes: 39 additions & 6 deletions sysmodules/rosalina/source/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
u32 menuCombo = 0;
bool isHidInitialized = false;
u32 mcuFwVersion = 0;
bool needsResetSelectedItem = false;

// libctru redefinition:

Expand Down Expand Up @@ -215,6 +216,24 @@ u32 menuCountItems(const Menu *menu)
return n;
}

static inline u32 menuAdvanceCursorWithVisibilityHandling(const Menu* menu, u32 pos, u32 numItems, s32 displ)
{
u32 menuIter = 0;
u32 selectedItem = pos;

do
{
// If everything is hidden in the menu, we return index 0 to avoid an infinite loop
if (menuIter >= numItems)
return 0;

selectedItem = menuAdvanceCursor(selectedItem, numItems, displ);
menuIter++;
} while (menuItemIsHidden(&menu->items[selectedItem]));

return selectedItem;
}

MyThread *menuCreateThread(void)
{
if(R_FAILED(MyThread_Create(&menuThread, menuThreadMain, menuThreadStack, 0x1000, 52, CORE_SYSTEM)))
Expand Down Expand Up @@ -371,7 +390,7 @@ void menuShow(Menu *root)

u32 numItems = menuCountItems(currentMenu);
if (menuItemIsHidden(&currentMenu->items[selectedItem]))
selectedItem = menuAdvanceCursor(selectedItem, numItems, 1);
selectedItem = menuAdvanceCursorWithVisibilityHandling(currentMenu, selectedItem, numItems, 1);

Draw_Lock();
Draw_ClearFramebuffer();
Expand Down Expand Up @@ -412,7 +431,10 @@ void menuShow(Menu *root)
previousSelectedItems[nbPreviousMenus] = selectedItem;
previousMenus[nbPreviousMenus++] = currentMenu;
currentMenu = currentMenu->items[selectedItem].menu;

selectedItem = 0;
if (menuItemIsHidden(&currentMenu->items[selectedItem]))
selectedItem = menuAdvanceCursorWithVisibilityHandling(currentMenu, selectedItem, numItems, 1);
break;
default:
__builtin_trap(); // oops
Expand Down Expand Up @@ -441,15 +463,21 @@ void menuShow(Menu *root)
}
else if(pressed & KEY_DOWN)
{
selectedItem = menuAdvanceCursor(selectedItem, numItems, 1);
if (menuItemIsHidden(&currentMenu->items[selectedItem]))
selectedItem = menuAdvanceCursor(selectedItem, numItems, 1);
selectedItem = menuAdvanceCursorWithVisibilityHandling(currentMenu, selectedItem, numItems, 1);
}
else if(pressed & KEY_UP)
{
selectedItem = menuAdvanceCursor(selectedItem, numItems, -1);
selectedItem = menuAdvanceCursorWithVisibilityHandling(currentMenu, selectedItem, numItems, -1);
}

if (needsResetSelectedItem)
{
selectedItem = 0;

if (menuItemIsHidden(&currentMenu->items[selectedItem]))
selectedItem = menuAdvanceCursor(selectedItem, numItems, -1);
selectedItem = menuAdvanceCursorWithVisibilityHandling(currentMenu, selectedItem, numItems, 1);

needsResetSelectedItem = false;
}

Draw_Lock();
Expand All @@ -458,3 +486,8 @@ void menuShow(Menu *root)
}
while(!menuShouldExit);
}

void menuResetSelectedItem(void)
{
needsResetSelectedItem = true;
}

0 comments on commit c307dc6

Please sign in to comment.