refactor(client/radial): refresh to last available menu

When re-registering or removing an option linking to the current
submenu, restore the last available sub menu.
This commit is contained in:
Linden
2023-02-24 03:48:16 +11:00
parent 7ffa12af4f
commit 21739ac6cf

View File

@@ -26,9 +26,11 @@ local currentRadial = nil
---Open a registered radial submenu with the given id. ---Open a registered radial submenu with the given id.
---@param id string ---@param id string
local function showRadial(id) local function showRadial(id)
local radial = menus[id] local radial = id and menus[id]
if not radial then return error('No radial menu with such id found.') end if id and not radial then
return error('No radial menu with such id found.')
end
currentRadial = radial currentRadial = radial
@@ -46,60 +48,40 @@ local function showRadial(id)
SendNUIMessage({ SendNUIMessage({
action = 'openRadialMenu', action = 'openRadialMenu',
data = { data = {
items = radial.items, items = radial?.items or menuItems,
sub = true sub = radial and true or nil
} }
}) })
end end
---Refresh the global menu items or current submenu. ---Refresh the global menu items or return from a submenu to its parent.
local function refreshRadial() local function refreshRadial(menuId)
if not isOpen then return end if not isOpen then return end
if currentRadial then if currentRadial and menuId then
return showRadial(currentRadial.id) if menuId == currentRadial.id then
end currentRadial = nil
else
for i = 1, #menuHistory do
local subMenuId = menuHistory[i]
table.wipe(menuHistory) if subMenuId == menuId then
currentRadial = menus[subMenuId]
-- Hide current menu and allow for transition for j = #menuHistory, i, -1 do
SendNUIMessage({ menuHistory[j] = nil
action = 'openRadialMenu', end
data = false
})
Wait(100) return showRadial(currentRadial.id)
end
end
-- If menu was closed during transition, don't open the submenu return false
if not isOpen then return end
SendNUIMessage({
action = 'openRadialMenu',
data = {
items = menuItems
}
})
end
---Check if the menuId is currently open or part of the menu history.
---@param menuId string
---@return boolean
local function isMenuOrChildOpen(menuId)
if not currentRadial then return false end
if menuId == currentRadial.id then
return true
end
for i = 1, #menuHistory do
local subMenuId = menuHistory[i]
if subMenuId == menuId then
return true
end end
end end
return false table.wipe(menuHistory)
showRadial()
end end
---Registers a radial sub menu with predefined options. ---Registers a radial sub menu with predefined options.
@@ -108,10 +90,7 @@ function lib.registerRadial(radial)
menus[radial.id] = radial menus[radial.id] = radial
if currentRadial then if currentRadial then
if not isMenuOrChildOpen(radial.id) then return end refreshRadial(radial.id)
currentRadial = nil
refreshRadial()
end end
end end
@@ -172,13 +151,7 @@ function lib.removeRadialItem(id)
end end
if isOpen then if isOpen then
if currentRadial then refreshRadial(menuItem.menu)
if not isMenuOrChildOpen(menuItem.menu) then return end
currentRadial = nil
end
refreshRadial()
end end
end end