Fix menu bug that allowed the selection of invalid states when wrap=true

This commit is contained in:
TuxSH 2023-09-17 18:20:47 +02:00 committed by Vicki Pfau
parent 4ee633925c
commit 369e6a03f4
1 changed files with 10 additions and 7 deletions

View File

@ -18,30 +18,33 @@ DEFINE_VECTOR(GUIMenuItemList, struct GUIMenuItem);
DEFINE_VECTOR(GUIMenuSavedList, struct GUIMenuSavedState);
void _itemNext(struct GUIMenuItem* item, bool wrap) {
if (item->state < item->nStates - 1) {
if (wrap || item->state < item->nStates - 1) {
unsigned oldState = item->state;
do {
++item->state;
if (item->state >= item->nStates) {
item->state -= item->nStates;
}
} while (!item->validStates[item->state] && item->state < item->nStates - 1);
if (!item->validStates[item->state]) {
item->state = oldState;
}
} else if (wrap) {
item->state = 0;
}
}
void _itemPrev(struct GUIMenuItem* item, bool wrap) {
if (item->state > 0) {
if (wrap || item->state > 0) {
unsigned oldState = item->state;
do {
if (item->state > 0) {
--item->state;
} else {
item->state = item->nStates - 1;
}
} while (!item->validStates[item->state] && item->state > 0);
if (!item->validStates[item->state]) {
item->state = oldState;
}
} else if (wrap) {
item->state = item->nStates - 1;
}
}