From 50c592b6c20cc4cbad3ed471dec028a7654c8809 Mon Sep 17 00:00:00 2001 From: Rebecca Kelly Date: Sat, 15 Jul 2023 13:11:11 -0400 Subject: [PATCH] Fix for default-selected item not working right in tooltipized menus --- libtooltipmenu/CHANGELOG.md | 5 +++++ libtooltipmenu/Makefile | 2 +- .../TooltipListMenu.zsc | 21 +++++++++++++++++++ .../TooltipOptionMenu.zsc | 14 +++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) diff --git a/libtooltipmenu/CHANGELOG.md b/libtooltipmenu/CHANGELOG.md index 4983d54..b8ff6b3 100644 --- a/libtooltipmenu/CHANGELOG.md +++ b/libtooltipmenu/CHANGELOG.md @@ -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: diff --git a/libtooltipmenu/Makefile b/libtooltipmenu/Makefile index 8f8237b..90299c1 100644 --- a/libtooltipmenu/Makefile +++ b/libtooltipmenu/Makefile @@ -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 diff --git a/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipListMenu.zsc b/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipListMenu.zsc index 0b3f83e..fb324fb 100644 --- a/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipListMenu.zsc +++ b/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipListMenu.zsc @@ -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 items; @@ -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; } + } + } } } diff --git a/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipOptionMenu.zsc b/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipOptionMenu.zsc index d3110de..5368c82 100644 --- a/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipOptionMenu.zsc +++ b/libtooltipmenu/ca.ancilla.libtooltipmenu/TooltipOptionMenu.zsc @@ -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 items; @@ -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; } + } + } } }