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)
|
2022-08-02 12:24:45 +02:00
|
|
|
if not data.id then return error('No menu id was provided.') end
|
|
|
|
|
if not data.title then return error('No menu title was provided.') end
|
|
|
|
|
if not data.options then return error('No menu options were provided.') end
|
2022-07-31 13:03:09 +02:00
|
|
|
data.cb = cb
|
2022-08-02 12:20:08 +02:00
|
|
|
registeredMenus[data.id] = data
|
2022-07-31 12:54:40 +02:00
|
|
|
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
|
|
|
|
|
|
2022-08-04 13:25:47 +02:00
|
|
|
function lib.hideMenu(onExit)
|
|
|
|
|
if onExit and registeredMenus[openMenu].onClose then registeredMenus[openMenu].onClose() end
|
|
|
|
|
SetNuiFocus(false, false)
|
|
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'closeMenu'
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
|
2022-08-03 12:07:01 +02:00
|
|
|
function lib.setMenuOptions(id, options, index)
|
|
|
|
|
if index then
|
|
|
|
|
registeredMenus[id].options[index] = options
|
|
|
|
|
else
|
|
|
|
|
registeredMenus[id].options = options
|
|
|
|
|
end
|
2022-08-03 11:42:12 +02:00
|
|
|
end
|
|
|
|
|
|
2022-07-31 12:54:40 +02:00
|
|
|
function lib.getOpenMenu() return openMenu end
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('confirmSelected', function(data, cb)
|
|
|
|
|
cb(1)
|
2022-08-04 13:12:58 +02:00
|
|
|
local selected = {data[1] + 1, data[2] and data[2] + 1} -- data = [selected, scrollIndex]
|
2022-08-03 12:32:50 +02:00
|
|
|
local menu = registeredMenus[openMenu]
|
2022-08-04 13:12:58 +02:00
|
|
|
if menu.options[selected[1]].close ~= false then SetNuiFocus(false, false) end
|
|
|
|
|
local args = menu.options[selected[1]].args
|
|
|
|
|
registeredMenus[openMenu].cb(selected[1], selected[2], args)
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('changeIndex', function(data, cb)
|
|
|
|
|
cb(1)
|
|
|
|
|
if not registeredMenus[openMenu].onSideScroll then return end
|
|
|
|
|
local selected = {data[1] + 1, data[2] and data[2] + 1} -- data = [selected, scrollIndex]
|
|
|
|
|
local args = registeredMenus[openMenu].options[selected[1]].args
|
|
|
|
|
registeredMenus[openMenu].onSideScroll(selected[1], selected[2], args)
|
2022-07-31 12:54:40 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('changeSelected', function(data, cb)
|
|
|
|
|
cb(1)
|
2022-08-04 13:12:58 +02:00
|
|
|
if not registeredMenus[openMenu].onSelected then return end
|
|
|
|
|
local selected = {data[1] + 1, data[2] and data[2] + 1} -- data = [selected, scrollIndex]
|
|
|
|
|
local args = registeredMenus[openMenu].options[selected[1]].args
|
|
|
|
|
registeredMenus[openMenu].onSelected(selected[1], selected[2], args)
|
2022-07-31 12:54:40 +02:00
|
|
|
end)
|
|
|
|
|
|
2022-08-01 11:13:26 +02:00
|
|
|
RegisterNUICallback('closeMenu', function(data, cb)
|
|
|
|
|
cb(1)
|
|
|
|
|
SetNuiFocus(false, false)
|
|
|
|
|
if registeredMenus[openMenu].onClose then return registeredMenus[openMenu].onClose() end
|
|
|
|
|
end)
|