implement jumps in option menus

Enables shortcuts for option menus. Press a key to immediately jump to the next
option menu entry which starts with this key. Hold Alt to jump backwards.
Compatible with localized menus (checked on Russian).
This commit is contained in:
Alexander Kromm 2021-06-30 22:26:17 +07:00 committed by Christoph Oelckers
parent eb15d97fe3
commit 7eb12fa9fb

View file

@ -201,6 +201,24 @@ class OptionMenu : Menu
}
return true;
}
else if (ev.type == UIEvent.Type_Char)
{
String key = String.Format("%c", ev.keyChar).MakeLower();
int itemsNumber = mDesc.mItems.Size();
int direction = ev.IsAlt ? -1 : 1;
for (int i = 0; i < itemsNumber; ++i)
{
int index = (mDesc.mSelectedItem + direction * (i + 1) + itemsNumber) % itemsNumber;
if (!mDesc.mItems[index].Selectable()) continue;
String label = StringTable.Localize(mDesc.mItems[index].mLabel);
String firstLabelCharacter = String.Format("%c", label.GetNextCodePoint(0)).MakeLower();
if (firstLabelCharacter == key)
{
mDesc.mSelectedItem = index;
break;
}
}
}
return Super.OnUIEvent(ev);
}