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(modules): Scaleform class #584

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
11 changes: 11 additions & 0 deletions modules/lib.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local isServer = IsDuplicityVersion()

---@class Scaleform
local scaleform = require 'modules.scaleform'

local qbx = {}
qbx.string = {}
qbx.math = {}
Expand Down Expand Up @@ -560,6 +563,14 @@ else

ReleaseSoundId(soundId)
end

-- Create a new scaleform instance
-- Uses ox lib class system to create an easy to use scaleform handler
---@param scaleformName string
---@return Scaleform
function qbx.newScaleform(scaleformName)
return scaleform:new(scaleformName)
end
end

_ENV.qbx = qbx
208 changes: 208 additions & 0 deletions modules/scaleform.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
---@class Scaleform : OxClass
---@field name string
---@field scaleform number
---@field draw boolean
---@field renderTarget number
---@field targetName string
---@field handle number
local Scaleform = lib.class('Scaleform')

---@param name string
---@return nil
---@description Create a new scaleform class
function Scaleform:constructor(name)
Mycroft-Studios marked this conversation as resolved.
Show resolved Hide resolved
self.name = name -- Set the name

local scaleform = lib.requestScaleformMovie(name) -- Request the scaleform movie

if not scaleform then -- If the scaleform is nil
return error(('Failed to request scaleform movie - [%s]'):format(name)) -- Error the failed scaleform request
end

self.handle = scaleform -- Set the scaleform handle
self.draw = false -- Set the draw to false
self.fullScreen = true -- Set the full screen to false
end

---@param name string
---@param args table
---@return nil
---@description Request a scaleform method with parameters
function Scaleform:MethodArgs(name, args)
if not self.handle then -- If the scaleform handle is nil
return error('Scaleform handle is nil') -- Error the scaleform handle is nil
end
if type(args) ~= 'table' then -- If the type of args is not a table
return error('Args must be a table') -- Error args must be a table
end

BeginScaleformMovieMethod(self.handle, name) -- Begin the scaleform movie method
for i=1, #args do -- loop through the args
local arg = args[i] -- Set the value to the current arg
if type(arg) == 'string' then -- If the type of v is a string
ScaleformMovieMethodAddParamPlayerNameString(arg) -- Add the player name string
elseif type(arg) == 'number' then -- If the type of v is a number
if math.type(arg) == 'integer' then -- If the math type of v is an integer
ScaleformMovieMethodAddParamInt(arg) -- Add the integer
else -- If the math type of v is not an integer
ScaleformMovieMethodAddParamFloat(arg) -- Add the float
end
elseif type(arg) == 'boolean' then -- If the type of v is a boolean
ScaleformMovieMethodAddParamBool(arg) -- Add the boolean
else
error(('Unsupported Parameter type [%s]'):format(type(arg))) -- Error unsupported type
end
end
EndScaleformMovieMethod() -- End the scaleform movie method
end

---@param name string
---@return nil
---@description Request a scaleform method with no return value or parameters
function Scaleform:Method(name)
Mycroft-Studios marked this conversation as resolved.
Show resolved Hide resolved
if not self.handle then -- If the scaleform handle is nil
return error('Scaleform handle is nil') -- Error the scaleform handle is nil
end

BeginScaleformMovieMethod(self.handle, name) -- Begin the scaleform movie method
EndScaleformMovieMethod() -- End the scaleform movie method
end

---@param name string
---@param type string
---@return number|string | boolean
---@description Request a scaleform method with a return value
function Scaleform:MethodReturn(name, type)
if not self.handle then -- If the scaleform handle is nil
return error('Scaleform handle is nil') -- Error the scaleform handle is nil
end

BeginScaleformMovieMethod(self.handle, name) -- Begin the scaleform movie method
local result = EndScaleformMovieMethodReturnValue() -- End the scaleform movie method with a return value

local timeout = 0
while not IsScaleformMovieMethodReturnValueReady(result) do -- While the return value is not ready
Wait(0) -- Wait 0
timeout = timeout + 1 -- Increment the timeout
if timeout > 1000 then -- If the timeout is greater than 1000
Mycroft-Studios marked this conversation as resolved.
Show resolved Hide resolved
error(('Return value failed - [%s]'):format(name)) -- Error the timeout waiting for scaleform method return value
return false
end
end -- End the while loop

if type == "int" then -- If the type is an integer
return GetScaleformMovieMethodReturnValueInt(result) -- Get the return value as an integer
elseif type == "bool" then
return GetScaleformMovieMethodReturnValueBool(result) -- Get the return value as a boolean
else -- If the type is not an integer
return GetScaleformMovieMethodReturnValueString(result) -- Get the return value as a string
end
end

---@param isFullscreen boolean
---@return nil
---@description Set the scaleform to render in full screen
function Scaleform:SetFullScreen(isFullscreen)
self.fullScreen = isFullscreen
end

---@param x number
---@param y number
---@param width number
---@param height number
---@return nil
---@description Set the properties of the scaleform (Requires SetFullScreen to be false)
function Scaleform:SetProperties(x, y, width, height)
if self.fullScreen then -- If the full screen is true
return error('Cannot set properties when full screen is enabled') -- Error cannot set properties when full screen is enabled
end
self.x = x
self.y = y
self.width = width
self.height = height
end


---@param name string
---@param model string|number
---@return nil
---@description Create a render target for the scaleform - optional , only if you want to render the scaleform in 3D
function Scaleform:RenderTarget(name, model)
Mycroft-Studios marked this conversation as resolved.
Show resolved Hide resolved
if type(model) == 'string' then -- If the type of model is a string
model = joaat(model) -- Convert the model to a hash
end

if not IsNamedRendertargetRegistered(name) then -- If the named render target is not registered
RegisterNamedRendertarget(name, false) -- Register the named render target

if not IsNamedRendertargetLinked(model) then -- If the named render target is not linked
LinkNamedRendertarget(model) -- Link the named render target
end

self.renderTarget = GetNamedRendertargetRenderId(name) -- Get the named render target render id
self.targetName = name -- Set the target name
end
end

---@param shouldDraw boolean
---@return nil
---@description Draw the scaleform
function Scaleform:Draw(shouldDraw)
Mycroft-Studios marked this conversation as resolved.
Show resolved Hide resolved
if self.draw == shouldDraw then -- If the draw is equal to should draw
return -- Return
end

self.draw = shouldDraw -- Set the draw to should draw
if shouldDraw then -- If should draw is true

CreateThread(function() -- Create a thread
while self.draw do -- While the draw is true

if self.renderTarget then -- If the render target is true
SetTextRenderId(self.renderTarget) -- Set the text render id
SetScriptGfxDrawOrder(4) -- Set the script gfx draw order
SetScriptGfxDrawBehindPausemenu(true) -- allow it to draw behind pause menu
SetScaleformFitRendertarget(self.handle, true)
end

if self.fullScreen then
DrawScaleformMovieFullscreen(self.handle, 255, 255, 255, 255, 0)
else
if not self.x or not self.y or not self.width or not self.height then
error('Properties not set for scaleform') -- Error properties not set for scaleform
DrawScaleformMovieFullscreen(self.handle, 255, 255, 255, 255, 0)
Mycroft-Studios marked this conversation as resolved.
Show resolved Hide resolved
else
DrawScaleformMovie(self.handle, self.x, self.y, self.width, self.height, 255, 255, 255, 255, 0)
end
end

if self.renderTarget then -- If the render target is true
SetTextRenderId(1) -- Reset the text render id
end
D4isDAVID marked this conversation as resolved.
Show resolved Hide resolved

Wait(0)
end
end)

end
end

---@return nil
---@description Dispose of the scaleform
function Scaleform:Dispose()
if self.handle then -- If the handle exists
SetScaleformMovieAsNoLongerNeeded(self.handle) -- Set the scaleform movie as no longer needed
end

if self.renderTarget then -- If the render target exists
ReleaseNamedRendertarget(self.targetName) -- Release the named render target
Mycroft-Studios marked this conversation as resolved.
Show resolved Hide resolved
end

-- Reset the values
self.handle = nil -- Set the handle to nil
self.renderTarget = nil -- Set the render target to nil
self.draw = false -- Set the draw to false
end

---@return Scaleform
return Scaleform