2022-07-31 12:54:40 +02:00
|
|
|
local registeredMenus = {}
|
|
|
|
|
local openMenu = nil
|
|
|
|
|
|
2022-07-31 13:03:09 +02:00
|
|
|
function lib.registerMenu(data, cb)
|
|
|
|
|
data.cb = cb
|
2022-07-31 12:54:40 +02:00
|
|
|
if not registeredMenus[data.id] then registeredMenus[data.id] = data end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function lib.showMenu(id)
|
|
|
|
|
if not registeredMenus[id] then return error('No menu of such id found.') end
|
|
|
|
|
local data = registeredMenus[id]
|
|
|
|
|
openMenu = id
|
2022-07-31 13:03:09 +02:00
|
|
|
SetNuiFocus(true, false)
|
2022-07-31 12:54:40 +02:00
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'setMenu',
|
|
|
|
|
data = {
|
|
|
|
|
position = data.position,
|
|
|
|
|
title = data.title,
|
|
|
|
|
items = data.options
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function lib.getOpenMenu() return openMenu end
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('confirmSelected', function(data, cb)
|
|
|
|
|
cb(1)
|
2022-07-31 13:03:09 +02:00
|
|
|
SetNuiFocus(false, false)
|
|
|
|
|
local selected = data
|
|
|
|
|
if type(selected) == 'number' then
|
|
|
|
|
registeredMenus[openMenu].cb(selected)
|
|
|
|
|
else
|
|
|
|
|
registeredMenus[openMenu].cb(selected[1], selected[2])
|
|
|
|
|
end
|
2022-07-31 12:54:40 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('changeSelected', function(data, cb)
|
|
|
|
|
cb(1)
|
|
|
|
|
if registeredMenus[openMenu].onChange then
|
|
|
|
|
local selected = data
|
|
|
|
|
if type(selected) == 'number' then
|
|
|
|
|
registeredMenus[openMenu].onChange(selected)
|
|
|
|
|
else
|
|
|
|
|
registeredMenus[openMenu].onChange(selected[1], selected[2])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterCommand('testMenu', function()
|
|
|
|
|
lib.registerMenu({
|
|
|
|
|
id = 'epic_menu',
|
|
|
|
|
title = 'Nice menu',
|
|
|
|
|
position = 'top-right',
|
|
|
|
|
onChange = function(selected, scrollIndex)
|
|
|
|
|
print(selected, scrollIndex)
|
|
|
|
|
end,
|
|
|
|
|
options = {
|
|
|
|
|
{label = 'Nice option'},
|
|
|
|
|
{label = 'Nice header', values = {'Option1', 'option2', 'option3'}}
|
|
|
|
|
}
|
2022-07-31 13:03:09 +02:00
|
|
|
}, function(selected, scrollIndex)
|
|
|
|
|
print(selected, scrollIndex)
|
|
|
|
|
end)
|
2022-07-31 12:54:40 +02:00
|
|
|
lib.showMenu('epic_menu')
|
|
|
|
|
end)
|