2022-10-16 12:49:47 +11:00
|
|
|
---@class KeybindProps
|
2022-10-14 17:08:20 -06:00
|
|
|
---@field name string
|
|
|
|
|
---@field description string
|
|
|
|
|
---@field defaultKey? string
|
|
|
|
|
---@field disabled? boolean
|
|
|
|
|
---@field disable? fun(self: CKeybind, toggle: boolean)
|
|
|
|
|
---@field onPressed? fun(self: CKeybind)
|
|
|
|
|
---@field onReleased? fun(self: CKeybind)
|
2022-10-16 12:49:47 +11:00
|
|
|
---@field [string] any
|
|
|
|
|
|
|
|
|
|
---@class CKeybind : KeybindProps
|
|
|
|
|
---@field currentKey string
|
2022-10-14 17:08:20 -06:00
|
|
|
|
|
|
|
|
local keybinds = {}
|
|
|
|
|
|
|
|
|
|
local function disableKeybind(self, toggle)
|
|
|
|
|
keybinds[self.name].disabled = toggle
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local IsPauseMenuActive = IsPauseMenuActive
|
|
|
|
|
|
2022-10-16 12:49:47 +11:00
|
|
|
---@param data KeybindProps
|
2022-10-14 17:08:20 -06:00
|
|
|
---@return CKeybind
|
|
|
|
|
function lib.addKeybind(data)
|
|
|
|
|
data.defaultKey = data.defaultKey or ''
|
2022-10-15 19:41:49 -06:00
|
|
|
data.currentKey = GetControlInstructionalButton(0, joaat('+' .. data.name) | 0x80000000, true):sub(3)
|
2022-10-14 17:08:20 -06:00
|
|
|
data.disabled = data.disabled or false
|
|
|
|
|
data.disable = disableKeybind
|
|
|
|
|
|
|
|
|
|
RegisterCommand('+' .. data.name, function()
|
|
|
|
|
if not data.onPressed or data.disabled or IsPauseMenuActive() then return end
|
|
|
|
|
data:onPressed()
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
RegisterCommand('-' .. data.name, function()
|
|
|
|
|
if not data.onReleased or data.disabled or IsPauseMenuActive() then return end
|
|
|
|
|
data:onReleased()
|
|
|
|
|
end)
|
|
|
|
|
|
2022-10-15 19:41:49 -06:00
|
|
|
RegisterKeyMapping('+' .. data.name, data.description, 'keyboard', data.defaultKey)
|
2022-10-14 17:08:20 -06:00
|
|
|
|
|
|
|
|
SetTimeout(500, function()
|
|
|
|
|
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
|
|
|
|
|
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
keybinds[data.name] = data
|
2022-10-16 12:49:47 +11:00
|
|
|
---@cast data -KeybindProps
|
2022-10-14 17:08:20 -06:00
|
|
|
return data
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return lib.addKeybind
|