mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
refactor(client/radial): use lib.addKeybind and tweak exports
Merge lib.hideRadial with closeRadial, trigger onReleased. Trigger radial submenu after onSelect. Keep showRadial as a local method.
This commit is contained in:
@@ -8,7 +8,7 @@ function lib.registerRadial(radial)
|
|||||||
menus[radial.id] = radial
|
menus[radial.id] = radial
|
||||||
end
|
end
|
||||||
|
|
||||||
function lib.showRadial(id)
|
local function 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
|
||||||
@@ -25,10 +25,17 @@ function lib.showRadial(id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function lib.hideRadial()
|
function lib.hideRadial()
|
||||||
|
if not isOpen then return end
|
||||||
|
|
||||||
SendNUIMessage({
|
SendNUIMessage({
|
||||||
action = 'openRadialMenu',
|
action = 'openRadialMenu',
|
||||||
data = false
|
data = false
|
||||||
})
|
})
|
||||||
|
|
||||||
|
SetNuiFocus(false, false)
|
||||||
|
|
||||||
|
isOpen = false
|
||||||
|
currentRadial = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function lib.addRadialItem(items)
|
function lib.addRadialItem(items)
|
||||||
@@ -68,8 +75,8 @@ 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.onSelect then item.onSelect() end
|
if item.onSelect then item.onSelect() end
|
||||||
|
if item.menu then return showRadial(item.menu) end
|
||||||
|
|
||||||
lib.hideRadial()
|
lib.hideRadial()
|
||||||
end)
|
end)
|
||||||
@@ -77,7 +84,7 @@ end)
|
|||||||
RegisterNUICallback('radialBack', function(_, cb)
|
RegisterNUICallback('radialBack', function(_, cb)
|
||||||
cb(1)
|
cb(1)
|
||||||
if currentRadial.menu then
|
if currentRadial.menu then
|
||||||
return lib.showRadial(currentRadial.menu)
|
return showRadial(currentRadial.menu)
|
||||||
end
|
end
|
||||||
|
|
||||||
currentRadial = nil
|
currentRadial = nil
|
||||||
@@ -90,49 +97,34 @@ RegisterNUICallback('radialBack', function(_, cb)
|
|||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local function openRadial()
|
lib.addKeybind({
|
||||||
if #menuItems == 0 then return end
|
name = 'ox_lib-radial',
|
||||||
isOpen = true
|
description = 'Open radial menu',
|
||||||
|
defaultKey = 'z',
|
||||||
|
onPressed = function()
|
||||||
|
if isOpen or #menuItems == 0 then return end
|
||||||
|
|
||||||
SendNUIMessage({
|
isOpen = true
|
||||||
action = 'openRadialMenu',
|
|
||||||
data = {
|
SendNUIMessage({
|
||||||
items = menuItems
|
action = 'openRadialMenu',
|
||||||
}
|
data = {
|
||||||
})
|
items = menuItems
|
||||||
SetNuiFocus(true, true)
|
}
|
||||||
SetNuiFocusKeepInput(true)
|
})
|
||||||
SetCursorLocation(0.5, 0.5)
|
SetNuiFocus(true, true)
|
||||||
|
SetNuiFocusKeepInput(true)
|
||||||
|
SetCursorLocation(0.5, 0.5)
|
||||||
|
|
||||||
CreateThread(function()
|
|
||||||
while isOpen do
|
while isOpen do
|
||||||
DisablePlayerFiring(cache.playerId, true)
|
DisablePlayerFiring(cache.playerId, true)
|
||||||
DisableControlAction(0, 1, true)
|
DisableControlAction(0, 1, true)
|
||||||
DisableControlAction(0, 2, true)
|
DisableControlAction(0, 2, true)
|
||||||
Wait(0)
|
Wait(0)
|
||||||
end
|
end
|
||||||
end)
|
end,
|
||||||
end
|
onReleased = lib.hideRadial,
|
||||||
|
})
|
||||||
local function closeRadial()
|
|
||||||
if not isOpen then return end
|
|
||||||
|
|
||||||
SendNUIMessage({
|
|
||||||
action = 'openRadialMenu',
|
|
||||||
data = false
|
|
||||||
})
|
|
||||||
SetNuiFocus(false, false)
|
|
||||||
|
|
||||||
isOpen = false
|
|
||||||
|
|
||||||
if currentRadial then currentRadial = nil end
|
|
||||||
end
|
|
||||||
|
|
||||||
RegisterCommand('+ox_lib-radial', openRadial)
|
|
||||||
|
|
||||||
RegisterCommand('-ox_lib-radial', closeRadial)
|
|
||||||
|
|
||||||
RegisterKeyMapping('+ox_lib-radial', 'Open radial menu', 'keyboard', 'z')
|
|
||||||
|
|
||||||
AddEventHandler('onClientResourceStop', function(resource)
|
AddEventHandler('onClientResourceStop', function(resource)
|
||||||
for i = #menuItems, 1, -1 do
|
for i = #menuItems, 1, -1 do
|
||||||
|
|||||||
Reference in New Issue
Block a user