mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-18 15:36:02 +01:00
refactor(client/radial): minor tweaks and formatting
Increment menuSize during lib.addRadialItem (micro-optimisation). Don't open radial menu if no items are registered.
This commit is contained in:
@@ -10,8 +10,11 @@ end
|
|||||||
|
|
||||||
function lib.showRadial(id)
|
function lib.showRadial(id)
|
||||||
local radial = menus[id]
|
local radial = menus[id]
|
||||||
|
|
||||||
if not radial then return error('No radial menu with such id found.') end
|
if not radial then return error('No radial menu with such id found.') end
|
||||||
|
|
||||||
currentRadial = radial
|
currentRadial = radial
|
||||||
|
|
||||||
SendNUIMessage({
|
SendNUIMessage({
|
||||||
action = 'openRadialMenu',
|
action = 'openRadialMenu',
|
||||||
data = {
|
data = {
|
||||||
@@ -29,13 +32,16 @@ function lib.hideRadial()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function lib.addRadialItem(items)
|
function lib.addRadialItem(items)
|
||||||
|
local menuSize = #menuItems
|
||||||
|
|
||||||
if table.type(items) == 'array' then
|
if table.type(items) == 'array' then
|
||||||
for i = 1, #items do
|
for i = 1, #items do
|
||||||
local item = items[i]
|
local item = items[i]
|
||||||
menuItems[#menuItems+1] = item
|
menuSize += 1
|
||||||
|
menuItems[menuSize] = item
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
menuItems[#menuItems+1] = items
|
menuItems[menuSize + 1] = items
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -58,28 +64,33 @@ end
|
|||||||
RegisterNUICallback('radialClick', function(index, cb)
|
RegisterNUICallback('radialClick', function(index, cb)
|
||||||
cb(1)
|
cb(1)
|
||||||
local item = not currentRadial and menuItems[index + 1] or currentRadial.items[index + 1]
|
local item = not currentRadial and menuItems[index + 1] or currentRadial.items[index + 1]
|
||||||
|
|
||||||
if item.menu then lib.showRadial(item.menu) end
|
if item.menu then lib.showRadial(item.menu) end
|
||||||
if item.onSelect then item.onSelect() end
|
if item.onSelect then item.onSelect() end
|
||||||
|
|
||||||
lib.hideRadial()
|
lib.hideRadial()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
RegisterNUICallback('radialBack', function(_, cb)
|
RegisterNUICallback('radialBack', function(_, cb)
|
||||||
cb(1)
|
cb(1)
|
||||||
if currentRadial.menu then
|
if currentRadial.menu then
|
||||||
lib.showRadial(currentRadial.menu)
|
return lib.showRadial(currentRadial.menu)
|
||||||
else
|
|
||||||
currentRadial = nil
|
|
||||||
SendNUIMessage({
|
|
||||||
action = 'openRadialMenu',
|
|
||||||
data = {
|
|
||||||
items = menuItems
|
|
||||||
}
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
currentRadial = nil
|
||||||
|
|
||||||
|
SendNUIMessage({
|
||||||
|
action = 'openRadialMenu',
|
||||||
|
data = {
|
||||||
|
items = menuItems
|
||||||
|
}
|
||||||
|
})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local function openRadial()
|
local function openRadial()
|
||||||
|
if #menuItems == 0 then return end
|
||||||
isOpen = true
|
isOpen = true
|
||||||
|
|
||||||
SendNUIMessage({
|
SendNUIMessage({
|
||||||
action = 'openRadialMenu',
|
action = 'openRadialMenu',
|
||||||
data = {
|
data = {
|
||||||
@@ -89,6 +100,7 @@ local function openRadial()
|
|||||||
SetNuiFocus(true, true)
|
SetNuiFocus(true, true)
|
||||||
SetNuiFocusKeepInput(true)
|
SetNuiFocusKeepInput(true)
|
||||||
SetCursorLocation(0.5, 0.5)
|
SetCursorLocation(0.5, 0.5)
|
||||||
|
|
||||||
CreateThread(function()
|
CreateThread(function()
|
||||||
while isOpen do
|
while isOpen do
|
||||||
DisablePlayerFiring(cache.playerId, true)
|
DisablePlayerFiring(cache.playerId, true)
|
||||||
@@ -100,12 +112,16 @@ local function openRadial()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function closeRadial()
|
local function closeRadial()
|
||||||
|
if not isOpen then return end
|
||||||
|
|
||||||
SendNUIMessage({
|
SendNUIMessage({
|
||||||
action = 'openRadialMenu',
|
action = 'openRadialMenu',
|
||||||
data = false
|
data = false
|
||||||
})
|
})
|
||||||
SetNuiFocus(false, false)
|
SetNuiFocus(false, false)
|
||||||
|
|
||||||
isOpen = false
|
isOpen = false
|
||||||
|
|
||||||
if currentRadial then currentRadial = nil end
|
if currentRadial then currentRadial = nil end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -113,4 +129,4 @@ RegisterCommand('+ox_lib-radial', openRadial)
|
|||||||
|
|
||||||
RegisterCommand('-ox_lib-radial', closeRadial)
|
RegisterCommand('-ox_lib-radial', closeRadial)
|
||||||
|
|
||||||
RegisterKeyMapping('+ox_lib-radial', 'Open radial menu', 'keyboard', 'z')
|
RegisterKeyMapping('+ox_lib-radial', 'Open radial menu', 'keyboard', 'z')
|
||||||
|
|||||||
Reference in New Issue
Block a user