Skip to content

Commit

Permalink
fix: hide menu when there's no visible option #6
Browse files Browse the repository at this point in the history
  • Loading branch information
swkeep committed Nov 18, 2024
1 parent a6849d0 commit cb68e9a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion interactionMenu/lua/client/interact.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions interactionMenu/lua/client/menuContainer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb68e9a

Please sign in to comment.