Clean DUI until i found a better way to do it

This commit is contained in:
Johann Kraus authored and GitHub committed 2025-05-16 01:45:50 -03:00
1 parent 8665899043
commit 1c19fffb31
2 files changed
+274 -274

No files matched your search

+273 -273
View File
@@ -1,329 +1,329 @@
-- DUI (Direct User Interface) Manager for FiveM -- -- DUI (Direct User Interface) Manager for FiveM
-- Basic table-based implementation with full mouse support -- -- Basic table-based implementation with full mouse support
---@class DUI -- ---@class DUI
DUI = {} -- DUI = {}
-- Constants -- -- Constants
local DEFAULT_WIDTH <const> = 1280 -- local DEFAULT_WIDTH <const> = 1280
local DEFAULT_HEIGHT <const> = 720 -- local DEFAULT_HEIGHT <const> = 720
-- Store active DUI instances -- -- Store active DUI instances
local activeInstances = {} -- local activeInstances = {}
local instanceCounter = 0 -- local instanceCounter = 0
-- Mouse tracking state -- -- Mouse tracking state
local mouseState = { -- local mouseState = {
tracking = false, -- tracking = false,
activeId = nil, -- activeId = nil,
lastX = 0, -- lastX = 0,
lastY = 0, -- lastY = 0,
isPressed = false, -- isPressed = false,
currentButton = nil -- currentButton = nil
} -- }
-- Mouse button constants -- -- Mouse button constants
local MOUSE_BUTTONS <const> = { -- local MOUSE_BUTTONS <const> = {
LEFT = "left", -- LEFT = "left",
MIDDLE = "middle", -- MIDDLE = "middle",
RIGHT = "right" -- RIGHT = "right"
} -- }
---@class DUIInstance -- ---@class DUIInstance
---@field id number Unique identifier for this DUI instance -- ---@field id number Unique identifier for this DUI instance
---@field url string URL loaded in the DUI -- ---@field url string URL loaded in the DUI
---@field width number Width of the DUI -- ---@field width number Width of the DUI
---@field height number Height of the DUI -- ---@field height number Height of the DUI
---@field handle number DUI browser handle -- ---@field handle number DUI browser handle
---@field txd string Texture dictionary name -- ---@field txd string Texture dictionary name
---@field txn string Texture name -- ---@field txn string Texture name
---@field active boolean Whether the DUI is active -- ---@field active boolean Whether the DUI is active
---@field trackMouse boolean Whether mouse tracking is enabled for this instance -- ---@field trackMouse boolean Whether mouse tracking is enabled for this instance
---@field mouseScale table<string, number> Scale factors for mouse coordinates -- ---@field mouseScale table<string, number> Scale factors for mouse coordinates
-- Create a new DUI instance -- -- Create a new DUI instance
---@param url string URL to load -- ---@param url string URL to load
---@param width? number Width of the DUI (default: 1280) -- ---@param width? number Width of the DUI (default: 1280)
---@param height? number Height of the DUI (default: 720) -- ---@param height? number Height of the DUI (default: 720)
---@return number|nil id The DUI instance ID if successful, nil if failed -- ---@return number|nil id The DUI instance ID if successful, nil if failed
function DUI.Create(url, width, height) -- function DUI.Create(url, width, height)
if not url or type(url) ~= "string" then -- if not url or type(url) ~= "string" then
return print("DUI.Create: URL is required and must be a string") -- return print("DUI.Create: URL is required and must be a string")
end -- end
width = width or DEFAULT_WIDTH -- width = width or DEFAULT_WIDTH
height = height or DEFAULT_HEIGHT -- height = height or DEFAULT_HEIGHT
instanceCounter = instanceCounter + 1 -- instanceCounter = instanceCounter + 1
local id = instanceCounter -- local id = instanceCounter
local handle = CreateDui(url, width, height) -- local handle = CreateDui(url, width, height)
if not handle then -- if not handle then
return print("DUI.Create: Failed to create DUI instance") -- return print("DUI.Create: Failed to create DUI instance")
end -- end
local instance = { -- local instance = {
id = id, -- id = id,
url = url, -- url = url,
width = width, -- width = width,
height = height, -- height = height,
handle = handle, -- handle = handle,
txd = "dui_" .. id, -- txd = "dui_" .. id,
txn = "texture_" .. id, -- txn = "texture_" .. id,
active = true, -- active = true,
trackMouse = false, -- trackMouse = false,
mouseScale = { -- mouseScale = {
x = 1.0, -- x = 1.0,
y = 1.0 -- y = 1.0
} -- }
} -- }
-- Create runtime texture -- -- Create runtime texture
local duiHandle = GetDuiHandle(handle) -- local duiHandle = GetDuiHandle(handle)
CreateRuntimeTextureFromDuiHandle(CreateRuntimeTxd(instance.txd), instance.txn, duiHandle) -- CreateRuntimeTextureFromDuiHandle(CreateRuntimeTxd(instance.txd), instance.txn, duiHandle)
activeInstances[id] = instance -- activeInstances[id] = instance
return id -- return id
end -- end
-- Destroy a DUI instance -- -- Destroy a DUI instance
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.Destroy(id) -- function DUI.Destroy(id)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
if instance.handle then -- if instance.handle then
DestroyDui(instance.handle) -- DestroyDui(instance.handle)
end -- end
activeInstances[id] = nil -- activeInstances[id] = nil
return true -- return true
end -- end
-- Set URL for a DUI instance -- -- Set URL for a DUI instance
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param url string New URL to load -- ---@param url string New URL to load
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.SetURL(id, url) -- function DUI.SetURL(id, url)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
SetDuiUrl(instance.handle, url) -- SetDuiUrl(instance.handle, url)
instance.url = url -- instance.url = url
return true -- return true
end -- end
-- Send a message to the DUI -- -- Send a message to the DUI
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param message table Message to send (will be JSON encoded) -- ---@param message table Message to send (will be JSON encoded)
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.SendMessage(id, message) -- function DUI.SendMessage(id, message)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
SendDuiMessage(instance.handle, json.encode(message)) -- SendDuiMessage(instance.handle, json.encode(message))
return true -- return true
end -- end
-- Mouse interaction functions -- -- Mouse interaction functions
-- Move mouse cursor on DUI -- -- Move mouse cursor on DUI
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param x number X coordinate -- ---@param x number X coordinate
---@param y number Y coordinate -- ---@param y number Y coordinate
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.MoveMouse(id, x, y) -- function DUI.MoveMouse(id, x, y)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
SendDuiMouseMove(instance.handle, x, y) -- SendDuiMouseMove(instance.handle, x, y)
return true -- return true
end -- end
-- Simulate mouse button press -- -- Simulate mouse button press
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param button "left"|"middle"|"right" Mouse button to simulate -- ---@param button "left"|"middle"|"right" Mouse button to simulate
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.MouseDown(id, button) -- function DUI.MouseDown(id, button)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
if not MOUSE_BUTTONS[button:upper()] then -- if not MOUSE_BUTTONS[button:upper()] then
return print("DUI.MouseDown: Invalid button. Must be 'left', 'middle', or 'right'") -- return print("DUI.MouseDown: Invalid button. Must be 'left', 'middle', or 'right'")
end -- end
-- Update mouse state -- -- Update mouse state
if instance.trackMouse then -- if instance.trackMouse then
mouseState.isPressed = true -- mouseState.isPressed = true
mouseState.currentButton = button -- mouseState.currentButton = button
end -- end
SendDuiMouseDown(instance.handle, button) -- SendDuiMouseDown(instance.handle, button)
return true -- return true
end -- end
-- Simulate mouse button release -- -- Simulate mouse button release
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param button "left"|"middle"|"right" Mouse button to simulate -- ---@param button "left"|"middle"|"right" Mouse button to simulate
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.MouseUp(id, button) -- function DUI.MouseUp(id, button)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
if not MOUSE_BUTTONS[button:upper()] then -- if not MOUSE_BUTTONS[button:upper()] then
return print("DUI.MouseUp: Invalid button. Must be 'left', 'middle', or 'right'") -- return print("DUI.MouseUp: Invalid button. Must be 'left', 'middle', or 'right'")
end -- end
-- Update mouse state -- -- Update mouse state
if instance.trackMouse then -- if instance.trackMouse then
mouseState.isPressed = false -- mouseState.isPressed = false
mouseState.currentButton = nil -- mouseState.currentButton = nil
end -- end
SendDuiMouseUp(instance.handle, button) -- SendDuiMouseUp(instance.handle, button)
return true -- return true
end -- end
-- Simulate mouse wheel movement -- -- Simulate mouse wheel movement
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param deltaY number Vertical scroll amount -- ---@param deltaY number Vertical scroll amount
---@param deltaX number Horizontal scroll amount -- ---@param deltaX number Horizontal scroll amount
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.MouseWheel(id, deltaY, deltaX) -- function DUI.MouseWheel(id, deltaY, deltaX)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
SendDuiMouseWheel(instance.handle, deltaY, deltaX) -- SendDuiMouseWheel(instance.handle, deltaY, deltaX)
return true -- return true
end -- end
-- Click helper function (combines MoveMouse, MouseDown, and MouseUp) -- -- Click helper function (combines MoveMouse, MouseDown, and MouseUp)
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param x number X coordinate -- ---@param x number X coordinate
---@param y number Y coordinate -- ---@param y number Y coordinate
---@param button? "left"|"middle"|"right" Mouse button to click (default: "left") -- ---@param button? "left"|"middle"|"right" Mouse button to click (default: "left")
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.Click(id, x, y, button) -- function DUI.Click(id, x, y, button)
button = button or "left" -- button = button or "left"
if not DUI.MoveMouse(id, x, y) then return false end -- if not DUI.MoveMouse(id, x, y) then return false end
if not DUI.MouseDown(id, button) then return false end -- if not DUI.MouseDown(id, button) then return false end
-- Small delay to simulate real click -- -- Small delay to simulate real click
Citizen.Wait(50) -- Citizen.Wait(50)
return DUI.MouseUp(id, button) -- return DUI.MouseUp(id, button)
end -- end
-- Enable or disable mouse tracking for a DUI instance -- -- Enable or disable mouse tracking for a DUI instance
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@param enabled boolean Whether to enable mouse tracking -- ---@param enabled boolean Whether to enable mouse tracking
---@param scaleX? number Scale factor for X coordinates (default: 1.0) -- ---@param scaleX? number Scale factor for X coordinates (default: 1.0)
---@param scaleY? number Scale factor for Y coordinates (default: 1.0) -- ---@param scaleY? number Scale factor for Y coordinates (default: 1.0)
---@return boolean success Whether the operation was successful -- ---@return boolean success Whether the operation was successful
function DUI.TrackMouse(id, enabled, scaleX, scaleY) -- function DUI.TrackMouse(id, enabled, scaleX, scaleY)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return false end -- if not instance then return false end
instance.trackMouse = enabled -- instance.trackMouse = enabled
instance.mouseScale.x = scaleX or 1.0 -- instance.mouseScale.x = scaleX or 1.0
instance.mouseScale.y = scaleY or 1.0 -- instance.mouseScale.y = scaleY or 1.0
if enabled then -- if enabled then
mouseState.tracking = true -- mouseState.tracking = true
mouseState.activeId = id -- mouseState.activeId = id
elseif mouseState.activeId == id then -- elseif mouseState.activeId == id then
mouseState.tracking = false -- mouseState.tracking = false
mouseState.activeId = nil -- mouseState.activeId = nil
end -- end
return true -- return true
end -- end
-- Update mouse coordinates from screen position -- -- Update mouse coordinates from screen position
---@param screenX number Screen X coordinate -- ---@param screenX number Screen X coordinate
---@param screenY number Screen Y coordinate -- ---@param screenY number Screen Y coordinate
---@return boolean updated Whether coordinates were updated -- ---@return boolean updated Whether coordinates were updated
local function UpdateMousePosition(screenX, screenY) -- local function UpdateMousePosition(screenX, screenY)
if not mouseState.tracking or not mouseState.activeId then return false end -- if not mouseState.tracking or not mouseState.activeId then return false end
local instance = activeInstances[mouseState.activeId] -- local instance = activeInstances[mouseState.activeId]
if not instance or not instance.trackMouse then return false end -- if not instance or not instance.trackMouse then return false end
-- Scale coordinates based on DUI dimensions and scale factors -- -- Scale coordinates based on DUI dimensions and scale factors
local scaledX = screenX * instance.mouseScale.x -- local scaledX = screenX * instance.mouseScale.x
local scaledY = screenY * instance.mouseScale.y -- local scaledY = screenY * instance.mouseScale.y
-- Only update if position changed -- -- Only update if position changed
if scaledX ~= mouseState.lastX or scaledY ~= mouseState.lastY then -- if scaledX ~= mouseState.lastX or scaledY ~= mouseState.lastY then
mouseState.lastX = scaledX -- mouseState.lastX = scaledX
mouseState.lastY = scaledY -- mouseState.lastY = scaledY
DUI.MoveMouse(mouseState.activeId, scaledX, scaledY) -- DUI.MoveMouse(mouseState.activeId, scaledX, scaledY)
return true -- return true
end -- end
return false -- return false
end -- end
-- Mouse tracking thread -- -- Mouse tracking thread
Citizen.CreateThread(function() -- Citizen.CreateThread(function()
while true do -- while true do
if mouseState.tracking and mouseState.activeId then -- if mouseState.tracking and mouseState.activeId then
local instance = activeInstances[mouseState.activeId] -- local instance = activeInstances[mouseState.activeId]
if instance and instance.trackMouse then -- if instance and instance.trackMouse then
-- Get current cursor position -- -- Get current cursor position
local screenX, screenY = GetNuiCursorPosition() -- local screenX, screenY = GetNuiCursorPosition()
UpdateMousePosition(screenX, screenY) -- UpdateMousePosition(screenX, screenY)
-- Handle held mouse buttons -- -- Handle held mouse buttons
if mouseState.isPressed and mouseState.currentButton then -- if mouseState.isPressed and mouseState.currentButton then
-- Keep the button pressed state -- -- Keep the button pressed state
SendDuiMouseDown(instance.handle, mouseState.currentButton) -- SendDuiMouseDown(instance.handle, mouseState.currentButton)
end -- end
end -- end
end -- end
Citizen.Wait(0) -- Citizen.Wait(0)
end -- end
end) -- end)
-- Utility functions -- -- Utility functions
-- Get textures for a DUI instance -- -- Get textures for a DUI instance
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@return string|nil txd, string|nil txn Texture dictionary and name, or nil if instance not found -- ---@return string|nil txd, string|nil txn Texture dictionary and name, or nil if instance not found
function DUI.GetTextures(id) -- function DUI.GetTextures(id)
local instance = activeInstances[id] -- local instance = activeInstances[id]
if not instance then return nil, nil end -- if not instance then return nil, nil end
return instance.txd, instance.txn -- return instance.txd, instance.txn
end -- end
-- Check if a DUI instance exists -- -- Check if a DUI instance exists
---@param id number DUI instance ID -- ---@param id number DUI instance ID
---@return boolean exists Whether the instance exists and is active -- ---@return boolean exists Whether the instance exists and is active
function DUI.Exists(id) -- function DUI.Exists(id)
local instance = activeInstances[id] -- local instance = activeInstances[id]
return instance ~= nil and instance.active -- return instance ~= nil and instance.active
end -- end
-- Get all active DUI instances -- -- Get all active DUI instances
---@return table<number, DUIInstance> instances Table of active DUI instances -- ---@return table<number, DUIInstance> instances Table of active DUI instances
function DUI.GetActiveInstances() -- function DUI.GetActiveInstances()
return activeInstances -- return activeInstances
end -- end
-- Cleanup all DUI instances -- -- Cleanup all DUI instances
function DUI.CleanupAll() -- function DUI.CleanupAll()
for id in pairs(activeInstances) do -- for id in pairs(activeInstances) do
DUI.Destroy(id) -- DUI.Destroy(id)
end -- end
end -- end
-- Automatic cleanup on resource stop -- -- Automatic cleanup on resource stop
AddEventHandler('onResourceStop', function(resourceName) -- AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() ~= resourceName then return end -- if GetCurrentResourceName() ~= resourceName then return end
DUI.CleanupAll() -- DUI.CleanupAll()
end) -- end)
return DUI -- return DUI
+1 -1
View File
@@ -80,7 +80,7 @@ cLib.ClientStateBag = ClientStateBag or Require("lib/statebags/client/client.lua
cLib.Marker = Marker or Require("lib/markers/client/markers.lua") cLib.Marker = Marker or Require("lib/markers/client/markers.lua")
cLib.Anim = Anim or Require("lib/anim/client/client.lua") cLib.Anim = Anim or Require("lib/anim/client/client.lua")
cLib.Cutscene = Cutscene or Require("lib/cutscenes/client/cutscene.lua") cLib.Cutscene = Cutscene or Require("lib/cutscenes/client/cutscene.lua")
cLib.DUI = DUI or Require("lib/dui/client/dui.lua") --cLib.DUI = DUI or Require("lib/dui/client/dui.lua")
cLib.Particle = Particle or Require("lib/particles/client/particles.lua") cLib.Particle = Particle or Require("lib/particles/client/particles.lua")
return cLib return cLib