2022-07-31 12:54:40 +02:00
|
|
|
local registeredMenus = {}
|
|
|
|
|
local openMenu = nil
|
2022-08-08 18:07:30 +10:00
|
|
|
local keepInput = IsNuiFocusKeepingInput()
|
2022-07-31 12:54:40 +02:00
|
|
|
|
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)
|
2022-08-08 18:07:30 +10:00
|
|
|
local menu = registeredMenus[id]
|
2022-08-08 17:17:29 +10:00
|
|
|
|
2022-08-08 18:07:30 +10:00
|
|
|
if not menu then
|
2022-08-08 17:17:29 +10:00
|
|
|
return error(('No menu with id %s was found'):format(id))
|
|
|
|
|
end
|
|
|
|
|
|
2022-08-08 18:07:30 +10:00
|
|
|
if not openMenu then
|
|
|
|
|
CreateThread(function()
|
|
|
|
|
while openMenu do
|
|
|
|
|
if not openMenu.disableInput then
|
|
|
|
|
DisablePlayerFiring(cache.playerId, true)
|
|
|
|
|
HudWeaponWheelIgnoreSelection()
|
|
|
|
|
DisableControlAction(0, 140, true)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Wait(0)
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
openMenu = menu
|
|
|
|
|
keepInput = IsNuiFocusKeepingInput()
|
|
|
|
|
|
|
|
|
|
if not menu.disableInput then
|
|
|
|
|
SetNuiFocusKeepInput(true)
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-31 13:03:09 +02:00
|
|
|
SetNuiFocus(true, false)
|
2022-07-31 12:54:40 +02:00
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'setMenu',
|
|
|
|
|
data = {
|
2022-08-08 18:07:30 +10:00
|
|
|
position = menu.position,
|
2022-09-08 10:26:35 +02:00
|
|
|
canClose = menu.canClose,
|
2022-08-08 18:07:30 +10:00
|
|
|
title = menu.title,
|
|
|
|
|
items = menu.options
|
2022-07-31 12:54:40 +02:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
|
2022-08-08 18:07:30 +10:00
|
|
|
local function resetFocus()
|
|
|
|
|
SetNuiFocus(false, false)
|
|
|
|
|
SetNuiFocusKeepInput(keepInput)
|
|
|
|
|
end
|
|
|
|
|
|
2022-08-04 13:25:47 +02:00
|
|
|
function lib.hideMenu(onExit)
|
2022-08-23 19:12:50 +10:00
|
|
|
local menu = openMenu
|
|
|
|
|
openMenu = nil
|
|
|
|
|
|
|
|
|
|
if onExit and menu.onClose then
|
|
|
|
|
menu.onClose()
|
2022-08-05 00:58:19 +10:00
|
|
|
end
|
|
|
|
|
|
2022-08-08 18:07:30 +10:00
|
|
|
resetFocus()
|
2022-08-04 13:25:47 +02:00
|
|
|
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
|
2022-08-30 21:26:23 +02:00
|
|
|
if not options[1] then return error('Invalid override format used, expected table of options.') end
|
2022-08-03 12:07:01 +02:00
|
|
|
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-05 00:58:19 +10:00
|
|
|
data[1] += 1 -- selected
|
|
|
|
|
|
|
|
|
|
if data[2] then
|
|
|
|
|
data[2] += 1 -- scrollIndex
|
|
|
|
|
end
|
|
|
|
|
|
2022-08-23 19:12:50 +10:00
|
|
|
local menu = openMenu
|
2022-09-08 16:47:05 +05:30
|
|
|
|
2022-08-23 19:12:50 +10:00
|
|
|
if menu.options[data[1]].close ~= false then
|
2022-08-08 18:07:30 +10:00
|
|
|
resetFocus()
|
2022-09-08 16:47:05 +05:30
|
|
|
openMenu = nil
|
2022-08-05 00:58:19 +10:00
|
|
|
end
|
|
|
|
|
|
2022-08-23 19:12:50 +10:00
|
|
|
if menu.cb then
|
|
|
|
|
menu.cb(data[1], data[2], menu.options[data[1]].args)
|
2022-08-08 17:17:29 +10:00
|
|
|
end
|
|
|
|
|
|
2022-08-04 13:12:58 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('changeIndex', function(data, cb)
|
|
|
|
|
cb(1)
|
2022-08-08 17:17:29 +10:00
|
|
|
if not openMenu.onSideScroll then return end
|
2022-08-05 00:58:19 +10:00
|
|
|
|
|
|
|
|
data[1] += 1 -- selected
|
|
|
|
|
|
|
|
|
|
if data[2] then
|
|
|
|
|
data[2] += 1 -- scrollIndex
|
|
|
|
|
end
|
|
|
|
|
|
2022-08-08 17:17:29 +10:00
|
|
|
openMenu.onSideScroll(data[1], data[2], openMenu.options[data[1]].args)
|
2022-07-31 12:54:40 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('changeSelected', function(data, cb)
|
|
|
|
|
cb(1)
|
2022-08-08 17:17:29 +10:00
|
|
|
if not openMenu.onSelected then return end
|
2022-08-05 00:58:19 +10:00
|
|
|
|
|
|
|
|
data[1] += 1 -- selected
|
|
|
|
|
|
|
|
|
|
if data[2] then
|
|
|
|
|
data[2] += 1 -- scrollIndex
|
|
|
|
|
end
|
|
|
|
|
|
2022-08-08 17:17:29 +10:00
|
|
|
openMenu.onSelected(data[1], data[2], openMenu.options[data[1]].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)
|
2022-08-08 18:07:30 +10:00
|
|
|
resetFocus()
|
2022-08-08 17:17:29 +10:00
|
|
|
|
2022-08-23 19:12:50 +10:00
|
|
|
local menu = openMenu
|
2022-08-08 17:17:29 +10:00
|
|
|
openMenu = nil
|
2022-08-23 19:12:50 +10:00
|
|
|
|
|
|
|
|
if menu.onClose then
|
|
|
|
|
menu.onClose()
|
|
|
|
|
end
|
2022-08-01 11:13:26 +02:00
|
|
|
end)
|