From cb68e9ac318b22b39784fcec3b3fe2d1fe1c7dfe Mon Sep 17 00:00:00 2001 From: swkeep Date: Mon, 18 Nov 2024 12:07:32 +0330 Subject: [PATCH] fix: hide menu when there's no visible option #6 --- interactionMenu/lua/client/interact.lua | 3 ++- interactionMenu/lua/client/menuContainer.lua | 24 ++++++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/interactionMenu/lua/client/interact.lua b/interactionMenu/lua/client/interact.lua index 1a442ff..00c8f26 100644 --- a/interactionMenu/lua/client/interact.lua +++ b/interactionMenu/lua/client/interact.lua @@ -340,8 +340,9 @@ exports("refresh", refresh) function Render.generic(data, metadata, callbacks) if not data then return end - StateManager.set('active', true) + if not IsMenuVisible(data) then return end + StateManager.set('active', true) -- onEnter callback if callbacks.onEnter then if not callbacks.onEnter(data, metadata) then return end diff --git a/interactionMenu/lua/client/menuContainer.lua b/interactionMenu/lua/client/menuContainer.lua index 565511c..67e0b2a 100644 --- a/interactionMenu/lua/client/menuContainer.lua +++ b/interactionMenu/lua/client/menuContainer.lua @@ -839,7 +839,6 @@ local function navigateMenu(wheelDirection, menus, selected) local validOptionCount = #validOptions -- no valid options (if this happens something is wrong!) if validOptionCount == 0 then return 1, 0 end - -- find the `current selected index` and its `position` local currentIndex = nil for i, option in ipairs(validOptions) do @@ -871,13 +870,10 @@ local function updateSelectedItem(menus, selected, nextSelectedIndex) end function Container.changeMenuItem(scaleform, menuData, wheelDirection) - if not hasValidMenuOption(menuData) then - return - end + if not hasValidMenuOption(menuData) then return end local selected = menuData.selected local menus = menuData.menus - -- Ignore if list is empty, selected is not present, or menus are absent if not selected or not menus or not next(menus) then return @@ -1247,6 +1243,24 @@ function Container.syncData(scaleform, menuData, refreshUI) end end +function IsMenuVisible(menu_data) + local is_visible = false + for _, menu in pairs(menu_data.menus) do + local menu_id = menu.id + local original_menu_data = Container.get(menu_id) + + if original_menu_data and not original_menu_data.flags.deleted then + for _, option in ipairs(menu.options) do + if option.flags.hide == false then + is_visible = true + end + end + end + end + + return is_visible +end + -- Validate and synchronize the selected option. If it's the first encounter with the menu, -- choose a valid option; otherwise, restore the state on top of revalidating it ---@param scaleform table