2023-01-31 14:07:52 +01:00
|
|
|
local isOpen = false
|
2023-02-04 22:13:12 +01:00
|
|
|
local menus = {}
|
2023-01-31 14:07:52 +01:00
|
|
|
local menuItems = {}
|
2023-02-04 22:13:12 +01:00
|
|
|
local currentRadial = nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function lib.registerRadial(radial)
|
|
|
|
|
menus[radial.id] = radial
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function lib.showRadial(id)
|
|
|
|
|
local radial = menus[id]
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-02-04 22:13:12 +01:00
|
|
|
if not radial then return error('No radial menu with such id found.') end
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-02-04 22:13:12 +01:00
|
|
|
currentRadial = radial
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-02-04 22:13:12 +01:00
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'openRadialMenu',
|
|
|
|
|
data = {
|
|
|
|
|
items = radial.items,
|
|
|
|
|
sub = true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
end
|
2023-01-31 14:07:52 +01:00
|
|
|
|
2023-02-07 13:46:13 +01:00
|
|
|
function lib.hideRadial()
|
|
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'openRadialMenu',
|
|
|
|
|
data = false
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-31 14:07:52 +01:00
|
|
|
function lib.addRadialItem(items)
|
2023-02-08 00:19:51 +11:00
|
|
|
local menuSize = #menuItems
|
|
|
|
|
|
2023-01-31 14:07:52 +01:00
|
|
|
if table.type(items) == 'array' then
|
|
|
|
|
for i = 1, #items do
|
2023-02-02 11:28:59 +01:00
|
|
|
local item = items[i]
|
2023-02-08 00:19:51 +11:00
|
|
|
menuSize += 1
|
|
|
|
|
menuItems[menuSize] = item
|
2023-01-31 14:07:52 +01:00
|
|
|
end
|
|
|
|
|
else
|
2023-02-08 00:19:51 +11:00
|
|
|
menuItems[menuSize + 1] = items
|
2023-01-31 14:07:52 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-02-07 13:51:58 +01:00
|
|
|
function lib.removeRadialItem(id)
|
2023-01-31 14:07:52 +01:00
|
|
|
for i = 1, #menuItems do
|
2023-02-02 11:28:59 +01:00
|
|
|
local item = menuItems[i]
|
2023-02-07 13:51:58 +01:00
|
|
|
if item.id == id then
|
2023-02-02 11:28:59 +01:00
|
|
|
table.remove(menuItems, i)
|
2023-02-03 13:24:55 +01:00
|
|
|
break
|
2023-01-31 14:07:52 +01:00
|
|
|
end
|
|
|
|
|
end
|
2023-02-02 11:28:59 +01:00
|
|
|
if isOpen then
|
2023-02-03 13:24:55 +01:00
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'refreshItems',
|
|
|
|
|
data = menuItems
|
|
|
|
|
})
|
2023-02-02 11:28:59 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-02-04 22:13:12 +01:00
|
|
|
RegisterNUICallback('radialClick', function(index, cb)
|
|
|
|
|
cb(1)
|
|
|
|
|
local item = not currentRadial and menuItems[index + 1] or currentRadial.items[index + 1]
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-02-04 22:13:12 +01:00
|
|
|
if item.menu then lib.showRadial(item.menu) end
|
|
|
|
|
if item.onSelect then item.onSelect() end
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-02-07 13:46:13 +01:00
|
|
|
lib.hideRadial()
|
2023-02-04 22:13:12 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterNUICallback('radialBack', function(_, cb)
|
|
|
|
|
cb(1)
|
|
|
|
|
if currentRadial.menu then
|
2023-02-08 00:19:51 +11:00
|
|
|
return lib.showRadial(currentRadial.menu)
|
2023-02-04 22:13:12 +01:00
|
|
|
end
|
2023-02-08 00:19:51 +11:00
|
|
|
|
|
|
|
|
currentRadial = nil
|
|
|
|
|
|
|
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'openRadialMenu',
|
|
|
|
|
data = {
|
|
|
|
|
items = menuItems
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-02-04 22:13:12 +01:00
|
|
|
end)
|
|
|
|
|
|
2023-02-02 11:28:59 +01:00
|
|
|
local function openRadial()
|
2023-02-08 00:19:51 +11:00
|
|
|
if #menuItems == 0 then return end
|
2023-02-02 11:28:59 +01:00
|
|
|
isOpen = true
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-01-31 14:07:52 +01:00
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'openRadialMenu',
|
|
|
|
|
data = {
|
2023-02-03 13:24:55 +01:00
|
|
|
items = menuItems
|
2023-01-31 14:07:52 +01:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
SetNuiFocus(true, true)
|
|
|
|
|
SetNuiFocusKeepInput(true)
|
2023-02-04 22:13:12 +01:00
|
|
|
SetCursorLocation(0.5, 0.5)
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-01-31 14:07:52 +01:00
|
|
|
CreateThread(function()
|
|
|
|
|
while isOpen do
|
|
|
|
|
DisablePlayerFiring(cache.playerId, true)
|
|
|
|
|
DisableControlAction(0, 1, true)
|
|
|
|
|
DisableControlAction(0, 2, true)
|
|
|
|
|
Wait(0)
|
|
|
|
|
end
|
|
|
|
|
end)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function closeRadial()
|
2023-02-08 00:19:51 +11:00
|
|
|
if not isOpen then return end
|
|
|
|
|
|
2023-01-31 14:07:52 +01:00
|
|
|
SendNUIMessage({
|
|
|
|
|
action = 'openRadialMenu',
|
|
|
|
|
data = false
|
|
|
|
|
})
|
|
|
|
|
SetNuiFocus(false, false)
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-01-31 14:07:52 +01:00
|
|
|
isOpen = false
|
2023-02-08 00:19:51 +11:00
|
|
|
|
2023-02-04 22:13:12 +01:00
|
|
|
if currentRadial then currentRadial = nil end
|
2023-01-31 14:07:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
RegisterCommand('+ox_lib-radial', openRadial)
|
|
|
|
|
|
|
|
|
|
RegisterCommand('-ox_lib-radial', closeRadial)
|
|
|
|
|
|
2023-02-08 00:19:51 +11:00
|
|
|
RegisterKeyMapping('+ox_lib-radial', 'Open radial menu', 'keyboard', 'z')
|