Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(radial): added canSelect functin and onEvent field #658

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions resource/interface/client/radial.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
---@field label string
---@field menu? string
---@field onSelect? fun(currentMenu: string | nil, itemIndex: number) | string
---@field canSelect? fun()
---@field onEvent? string
---@field [string] any
---@field keepOpen? boolean
---@field iconWidth? number
Expand Down Expand Up @@ -209,22 +211,45 @@ RegisterNUICallback('radialClick', function(index, cb)
end

local menuResource = currentRadial and currentRadial.resource or item.resource
local success, resp = true, ""

if item.canSelect then
success, resp = pcall(item.canSelect, currentMenu, itemIndex)
if success and resp then
if item.menu then
menuHistory[#menuHistory + 1] = { id = currentRadial and currentRadial.id, option = item.menu }
showRadial(item.menu)
elseif not item.keepOpen then
lib.hideRadial()
end

if item.onEvent then TriggerEvent(item.onEvent, currentMenu, itemIndex) end
if item.onSelect then
if type(item.onSelect) == 'string' then
return exports[menuResource][item.onSelect](0, currentMenu, itemIndex)
end

if item.menu then
menuHistory[#menuHistory + 1] = { id = currentRadial and currentRadial.id, option = item.menu }
showRadial(item.menu)
elseif not item.keepOpen then
lib.hideRadial()
end
item.onSelect(currentMenu, itemIndex)
end
else
error(resp)
end
else
if item.menu then
menuHistory[#menuHistory + 1] = { id = currentRadial and currentRadial.id, option = item.menu }
showRadial(item.menu)
elseif not item.keepOpen then
lib.hideRadial()
end

local onSelect = item.onSelect
if item.onEvent then TriggerEvent(item.onEvent, currentMenu, itemIndex) end
if item.onSelect then
if type(item.onSelect) == 'string' then
return exports[menuResource][item.onSelect](0, currentMenu, itemIndex)
end

if onSelect then
if type(onSelect) == 'string' then
return exports[menuResource][onSelect](0, currentMenu, itemIndex)
item.onSelect(currentMenu, itemIndex)
end

onSelect(currentMenu, itemIndex)
end
end)

Expand Down