mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
refactor(keybinds): rename module, tweaks (#127)
This commit is contained in:
48
imports/addKeybind/client.lua
Normal file
48
imports/addKeybind/client.lua
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
---@class CKeybind
|
||||||
|
---@field name string
|
||||||
|
---@field description string
|
||||||
|
---@field defaultKey? string
|
||||||
|
---@field keybind string
|
||||||
|
---@field disabled? boolean
|
||||||
|
---@field disable? fun(self: CKeybind, toggle: boolean)
|
||||||
|
---@field onPressed? fun(self: CKeybind)
|
||||||
|
---@field onReleased? fun(self: CKeybind)
|
||||||
|
|
||||||
|
local keybinds = {}
|
||||||
|
|
||||||
|
local function disableKeybind(self, toggle)
|
||||||
|
keybinds[self.name].disabled = toggle
|
||||||
|
end
|
||||||
|
|
||||||
|
local IsPauseMenuActive = IsPauseMenuActive
|
||||||
|
|
||||||
|
---@param data CKeybind
|
||||||
|
---@return CKeybind
|
||||||
|
function lib.addKeybind(data)
|
||||||
|
data.defaultKey = data.defaultKey or ''
|
||||||
|
data.keybind = GetControlInstructionalButton(0, joaat('+' .. data.name) | 0x80000000, true):sub(3)
|
||||||
|
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)
|
||||||
|
|
||||||
|
RegisterKeyMapping('+' .. data.name, data.description, 'keyboard', data.keybind)
|
||||||
|
|
||||||
|
SetTimeout(500, function()
|
||||||
|
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
|
||||||
|
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
|
||||||
|
end)
|
||||||
|
|
||||||
|
keybinds[data.name] = data
|
||||||
|
return data
|
||||||
|
end
|
||||||
|
|
||||||
|
return lib.addKeybind
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
---@class CKeybind
|
|
||||||
---@field name string
|
|
||||||
---@field description string
|
|
||||||
---@field keybind string
|
|
||||||
---@field disabled? boolean
|
|
||||||
---@field disable? fun(self: CKeybind, toggle: boolean)
|
|
||||||
---@field onPressed? fun(self: CKeybind)
|
|
||||||
---@field onReleased? fun(self: CKeybind)
|
|
||||||
|
|
||||||
local keybinds = {}
|
|
||||||
|
|
||||||
local function disableKeybind(self, toggle)
|
|
||||||
keybinds[self.name].disabled = toggle
|
|
||||||
end
|
|
||||||
|
|
||||||
local IsPauseMenuActive = IsPauseMenuActive
|
|
||||||
|
|
||||||
lib.keybinds = {
|
|
||||||
---@param data CKeybind
|
|
||||||
---@return CKeybind
|
|
||||||
new = function(data)
|
|
||||||
data.keybind = data.keybind or ''
|
|
||||||
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)
|
|
||||||
|
|
||||||
RegisterKeyMapping('+' .. data.name, data.description, 'keyboard', data.keybind)
|
|
||||||
|
|
||||||
SetTimeout(500, function()
|
|
||||||
TriggerEvent('chat:removeSuggestion', ('/+%s'):format(data.name))
|
|
||||||
TriggerEvent('chat:removeSuggestion', ('/-%s'):format(data.name))
|
|
||||||
end)
|
|
||||||
|
|
||||||
keybinds[data.name] = data
|
|
||||||
return data
|
|
||||||
end,
|
|
||||||
|
|
||||||
---@return CKeybind?
|
|
||||||
get = function(name)
|
|
||||||
return keybinds[name]
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
return lib.keybinds
|
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
local isOpen = false
|
local isOpen = false
|
||||||
|
|
||||||
---@param text string
|
---@param text string
|
||||||
---@param options TextUIOptions
|
---@param options? TextUIOptions
|
||||||
function lib.showTextUI(text, options)
|
function lib.showTextUI(text, options)
|
||||||
if not options then options = {} end
|
if not options then options = {} end
|
||||||
options.text = text
|
options.text = text
|
||||||
|
|||||||
Reference in New Issue
Block a user