mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
refactor(interface/menu): set openMenu to reference menu data, not id
No need to index registeredMenus[openMenu] repeatedly.
This commit is contained in:
@@ -10,25 +10,29 @@ function lib.registerMenu(data, cb)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function lib.showMenu(id)
|
function lib.showMenu(id)
|
||||||
if not registeredMenus[id] then return error('No menu of such id found.') end
|
openMenu = registeredMenus[id]
|
||||||
local data = registeredMenus[id]
|
|
||||||
openMenu = id
|
if not openMenu then
|
||||||
|
return error(('No menu with id %s was found'):format(id))
|
||||||
|
end
|
||||||
|
|
||||||
SetNuiFocus(true, false)
|
SetNuiFocus(true, false)
|
||||||
SendNUIMessage({
|
SendNUIMessage({
|
||||||
action = 'setMenu',
|
action = 'setMenu',
|
||||||
data = {
|
data = {
|
||||||
position = data.position,
|
position = openMenu.position,
|
||||||
title = data.title,
|
title = openMenu.title,
|
||||||
items = data.options
|
items = openMenu.options
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
function lib.hideMenu(onExit)
|
function lib.hideMenu(onExit)
|
||||||
if onExit and registeredMenus[openMenu].onClose then
|
if onExit and openMenu.onClose then
|
||||||
registeredMenus[openMenu].onClose()
|
openMenu.onClose()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
openMenu = nil
|
||||||
SetNuiFocus(false, false)
|
SetNuiFocus(false, false)
|
||||||
SendNUIMessage({
|
SendNUIMessage({
|
||||||
action = 'closeMenu'
|
action = 'closeMenu'
|
||||||
@@ -53,19 +57,20 @@ RegisterNUICallback('confirmSelected', function(data, cb)
|
|||||||
data[2] += 1 -- scrollIndex
|
data[2] += 1 -- scrollIndex
|
||||||
end
|
end
|
||||||
|
|
||||||
local menu = registeredMenus[openMenu]
|
if openMenu.options[data[1]].close ~= false then
|
||||||
|
|
||||||
if menu.options[data[1]].close ~= false then
|
|
||||||
SetNuiFocus(false, false)
|
SetNuiFocus(false, false)
|
||||||
end
|
end
|
||||||
|
|
||||||
return menu.cb and menu.cb(data[1], data[2], menu.options[data[1]].args)
|
if openMenu.cb then
|
||||||
|
openMenu.cb(data[1], data[2], openMenu.options[data[1]].args)
|
||||||
|
end
|
||||||
|
|
||||||
|
openMenu = nil
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNUICallback('changeIndex', function(data, cb)
|
RegisterNUICallback('changeIndex', function(data, cb)
|
||||||
cb(1)
|
cb(1)
|
||||||
local menu = registeredMenus[openMenu]
|
if not openMenu.onSideScroll then return end
|
||||||
if not menu.onSideScroll then return end
|
|
||||||
|
|
||||||
data[1] += 1 -- selected
|
data[1] += 1 -- selected
|
||||||
|
|
||||||
@@ -73,13 +78,12 @@ RegisterNUICallback('changeIndex', function(data, cb)
|
|||||||
data[2] += 1 -- scrollIndex
|
data[2] += 1 -- scrollIndex
|
||||||
end
|
end
|
||||||
|
|
||||||
menu.onSideScroll(data[1], data[2], menu.options[data[1]].args)
|
openMenu.onSideScroll(data[1], data[2], openMenu.options[data[1]].args)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNUICallback('changeSelected', function(data, cb)
|
RegisterNUICallback('changeSelected', function(data, cb)
|
||||||
cb(1)
|
cb(1)
|
||||||
local menu = registeredMenus[openMenu]
|
if not openMenu.onSelected then return end
|
||||||
if not menu.onSelected then return end
|
|
||||||
|
|
||||||
data[1] += 1 -- selected
|
data[1] += 1 -- selected
|
||||||
|
|
||||||
@@ -87,11 +91,16 @@ RegisterNUICallback('changeSelected', function(data, cb)
|
|||||||
data[2] += 1 -- scrollIndex
|
data[2] += 1 -- scrollIndex
|
||||||
end
|
end
|
||||||
|
|
||||||
menu.onSelected(data[1], data[2], menu.options[data[1]].args)
|
openMenu.onSelected(data[1], data[2], openMenu.options[data[1]].args)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNUICallback('closeMenu', function(data, cb)
|
RegisterNUICallback('closeMenu', function(data, cb)
|
||||||
cb(1)
|
cb(1)
|
||||||
SetNuiFocus(false, false)
|
SetNuiFocus(false, false)
|
||||||
if registeredMenus[openMenu].onClose then return registeredMenus[openMenu].onClose() end
|
|
||||||
|
if openMenu.onClose then
|
||||||
|
openMenu.onClose()
|
||||||
|
end
|
||||||
|
|
||||||
|
openMenu = nil
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user