Skip to content

Commit

Permalink
Fix for default-selected item not working right in tooltipized menus
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicFrog committed Jul 15, 2023
1 parent dd3602b commit 50c592b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions libtooltipmenu/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.2.4

- Fix:
- `SelectedItem` and `DefaultSelection` were not handled properly, resulting in a default selection further down the menu than intended for some menus.

# 0.2.3

- Fix:
Expand Down
2 changes: 1 addition & 1 deletion libtooltipmenu/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NAME=libtooltipmenu
VERSION=0.2.3
VERSION=0.2.4
LUMPS=zscript.txt textures
LUMPS+=*.md
ZSCRIPT=ca.ancilla.libtooltipmenu/*.zsc
Expand Down
21 changes: 21 additions & 0 deletions libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipListMenu.zsc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ class TF_TooltipListMenu : ListMenu {
return;
}

// Save the selected item so we can restore it later even after all the
// item indexes have changed.
MenuItemBase selected = null;
if (desc.mSelectedItem >= 0 && desc.mSelectedItem < desc.mItems.size()) {
int i = desc.mSelectedItem;
// ListMenu uses SelectedItem after the default selection to mark it; if
// the MENUDEF places it after a tooltip, the default selection will be
// the tooltip, not the item it's attached to. So scan backwards until
// we find the real item.
while (i >= 0 && !desc.mItems[i].Selectable()) { --i; }
if (i >= 0)
selected = desc.mItems[i];
}

// Steal the descriptor's list of menu items, then rebuild it containing
// only the items we want to display.
array<ListMenuItem> items;
Expand Down Expand Up @@ -67,6 +81,13 @@ class TF_TooltipListMenu : ListMenu {
// Store our tooltips inside the menu descriptor so we can recover them when
// the menu is redisplayed.
desc.mItems.push(ListMenuItemTooltipHolder(new("ListMenuItemTooltipHolder").Init(tooltips)));

// Restore the originally selected item at its new index.
if (selected) {
for (uint i = 0; i < desc.mItems.size(); ++i) {
if (desc.mItems[i] == selected) { desc.mSelectedItem = i; break; }
}
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipOptionMenu.zsc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class TF_TooltipOptionMenu : OptionMenu {
return;
}

// Save the selected item so we can restore it later even after all the
// item indexes have changed.
MenuItemBase selected = null;
if (desc.mSelectedItem >= 0 && desc.mSelectedItem < desc.mItems.size()) {
selected = desc.mItems[desc.mSelectedItem];
}

// Steal the descriptor's list of menu items, then rebuild it containing
// only the items we want to display.
array<OptionMenuItem> items;
Expand Down Expand Up @@ -67,6 +74,13 @@ class TF_TooltipOptionMenu : OptionMenu {
// Store our tooltips inside the menu descriptor so we can recover them when
// the menu is redisplayed.
desc.mItems.push(OptionMenuItemTooltipHolder(new("OptionMenuItemTooltipHolder").Init(tooltips)));

// Restore the originally selected item at its new index.
if (selected) {
for (uint i = 0; i < desc.mItems.size(); ++i) {
if (desc.mItems[i] == selected) { desc.mSelectedItem = i; break; }
}
}
}
}

Expand Down

0 comments on commit 50c592b

Please sign in to comment.