Skip to content

Commit

Permalink
Add components
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalyptus committed May 24, 2021
1 parent 23d22e3 commit 5ee213b
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 3 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

36 changes: 36 additions & 0 deletions src/components/Audio.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
--- @author Dylan MALANDAIN, Kalyptus
--- @version 1.0.0
--- File created at [24/05/2021 00:00]
---

Audio = {}

---PlaySound
---
--- Reference : N/A
---
---@param Library string
---@param Sound string
---@param IsLooped boolean
---@return nil
---@public
function Audio.PlaySound(Library, Sound, IsLooped)
local audioId
if not IsLooped then
PlaySoundFrontend(-1, Sound, Library, true)
else
if not audioId then
Citizen.CreateThread(function()
audioId = GetSoundId()
PlaySoundFrontend(audioId, Sound, Library, true)
Citizen.Wait(0.01)
StopSound(audioId)
ReleaseSoundId(audioId)
audioId = nil
end)
end
end
end


163 changes: 163 additions & 0 deletions src/components/Graphics.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
---
--- @author Dylan MALANDAIN, Kalyptus
--- @version 1.0.0
--- File created at [24/05/2021 00:00]
---

local function StringToArray(str)
local charCount = #str
local strCount = math.ceil(charCount / 99)
local strings = {}

for i = 1, strCount do
local start = (i - 1) * 99 + 1
local clamp = math.clamp(#string.sub(str, start), 0, 99)
local finish = ((i ~= 1) and (start - 1) or 0) + clamp

strings[i] = string.sub(str, start, finish)
end

return strings
end

local function AddText(str)
local str = tostring(str)
local charCount = #str

if charCount < 100 then
AddTextComponentSubstringPlayerName(str)
else
local strings = StringToArray(str)
for s = 1, #strings do
AddTextComponentSubstringPlayerName(strings[s])
end
end
end

local function RText(text, x, y, font, scale, r, g, b, a, alignment, dropShadow, outline, wordWrap)
local Text, X, Y = text, (x or 0) / 1920, (y or 0) / 1080
SetTextFont(font or 0)
SetTextScale(1.0, scale or 0)
SetTextColour(r or 255, g or 255, b or 255, a or 255)
if dropShadow then
SetTextDropShadow()
end
if outline then
SetTextOutline()
end
if alignment ~= nil then
if alignment == 1 or alignment == "Center" or alignment == "Centre" then
SetTextCentre(true)
elseif alignment == 2 or alignment == "Right" then
SetTextRightJustify(true)
end
end
if wordWrap and wordWrap ~= 0 then
if alignment == 1 or alignment == "Center" or alignment == "Centre" then
SetTextWrap(X - ((wordWrap / 1920) / 2), X + ((wordWrap / 1920) / 2))
elseif alignment == 2 or alignment == "Right" then
SetTextWrap(0, X)
else
SetTextWrap(X, X + (wordWrap / 1920))
end
else
if alignment == 2 or alignment == "Right" then
SetTextWrap(0, X)
end
end
return Text, X, Y
end

Graphics = {};

function Graphics.MeasureStringWidth(str, font, scale)
BeginTextCommandGetWidth("CELL_EMAIL_BCON")
AddTextComponentSubstringPlayerName(str)
SetTextFont(font or 0)
SetTextScale(1.0, scale or 0)
return EndTextCommandGetWidth(true) * 1920
end

function Graphics.Rectangle(x, y, width, height, r, g, b, a)
local X, Y, Width, Height = (x or 0) / 1920, (y or 0) / 1080, (width or 0) / 1920, (height or 0) / 1080
DrawRect(X + Width * 0.5, Y + Height * 0.5, Width, Height, r or 255, g or 255, b or 255, a or 255)
end

function Graphics.Sprite(dictionary, name, x, y, width, height, heading, r, g, b, a)
local X, Y, Width, Height = (x or 0) / 1920, (y or 0) / 1080, (width or 0) / 1920, (height or 0) / 1080

if not HasStreamedTextureDictLoaded(dictionary) then
RequestStreamedTextureDict(dictionary, true)
end

DrawSprite(dictionary, name, X + Width * 0.5, Y + Height * 0.5, Width, Height, heading or 0, r or 255, g or 255, b or 255, a or 255)
end

function Graphics.GetLineCount(text, x, y, font, scale, r, g, b, a, alignment, dropShadow, outline, wordWrap)
local Text, X, Y = RText(text, x, y, font, scale, r, g, b, a, alignment, dropShadow, outline, wordWrap)
BeginTextCommandLineCount("CELL_EMAIL_BCON")
AddText(Text)
return EndTextCommandLineCount(X, Y)
end

function Graphics.Text(text, x, y, font, scale, r, g, b, a, alignment, dropShadow, outline, wordWrap)
local Text, X, Y = RText(text, x, y, font, scale, r, g, b, a, alignment, dropShadow, outline, wordWrap)
BeginTextCommandDisplayText("CELL_EMAIL_BCON")
AddText(Text)
EndTextCommandDisplayText(X, Y)
end

function Graphics.IsMouseInBounds(X, Y, Width, Height)
local MX, MY = math.round(GetControlNormal(2, 239) * 1920) / 1920, math.round(GetControlNormal(2, 240) * 1080) / 1080
X, Y = X / 1920, Y / 1080
Width, Height = Width / 1920, Height / 1080
return (MX >= X and MX <= X + Width) and (MY > Y and MY < Y + Height)
end

function Graphics.ConvertToPixel(x, y)
return (x * 1920), (y * 1080)
end

function Graphics.ScreenToWorld(distance, flags)
local camRot = GetGameplayCamRot(0)
local camPos = GetGameplayCamCoord()
local mouse = vector2(GetControlNormal(2, 239), GetControlNormal(2, 240))
local cam3DPos, forwardDir = Graphics.ScreenRelToWorld(camPos, camRot, mouse)
local direction = camPos + forwardDir * distance
local rayHandle = StartExpensiveSynchronousShapeTestLosProbe(cam3DPos, direction, flags, 0, 0)
local _, hit, endCoords, surfaceNormal, entityHit = GetShapeTestResult(rayHandle)
return (hit == 1 and true or false), endCoords, surfaceNormal, entityHit, (entityHit >= 1 and GetEntityType(entityHit) or 0), direction, mouse
end

function Graphics.ScreenRelToWorld(camPos, camRot, cursor)
local camForward = Graphics.RotationToDirection(camRot)
local rotUp = vector3(camRot.x + 1.0, camRot.y, camRot.z)
local rotDown = vector3(camRot.x - 1.0, camRot.y, camRot.z)
local rotLeft = vector3(camRot.x, camRot.y, camRot.z - 1.0)
local rotRight = vector3(camRot.x, camRot.y, camRot.z + 1.0)
local camRight = Graphics.RotationToDirection(rotRight) - Graphics.RotationToDirection(rotLeft)
local camUp = Graphics.RotationToDirection(rotUp) - Graphics.RotationToDirection(rotDown)
local rollRad = -(camRot.y * math.pi / 180.0)
local camRightRoll = camRight * math.cos(rollRad) - camUp * math.sin(rollRad)
local camUpRoll = camRight * math.sin(rollRad) + camUp * math.cos(rollRad)
local point3DZero = camPos + camForward * 1.0
local point3D = point3DZero + camRightRoll + camUpRoll
local point2D = Graphics.World3DToScreen2D(point3D)
local point2DZero = Graphics.World3DToScreen2D(point3DZero)
local scaleX = (cursor.x - point2DZero.x) / (point2D.x - point2DZero.x)
local scaleY = (cursor.y - point2DZero.y) / (point2D.y - point2DZero.y)
local point3Dret = point3DZero + camRightRoll * scaleX + camUpRoll * scaleY
local forwardDir = camForward + camRightRoll * scaleX + camUpRoll * scaleY
return point3Dret, forwardDir
end

function Graphics.RotationToDirection(rotation)
local x, z = (rotation.x * math.pi / 180.0), (rotation.z * math.pi / 180.0)
local num = math.abs(math.cos(x))
return vector3((-math.sin(z) * num), (math.cos(z) * num), math.sin(x))
end

function Graphics.World3DToScreen2D(pos)
local _, sX, sY = GetScreenCoordFromWorldCoord(pos.x, pos.y, pos.z)
return vector2(sX, sY)
end
23 changes: 23 additions & 0 deletions src/components/Keys.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
--- @author Dylan MALANDAIN, Kalyptus
--- @version 1.0.0
--- File created at [24/05/2021 00:00]
---

Keys = {};

---Register
---@param Controls string
---@param ControlName string
---@param Description string
---@param Action function
---@return Keys
---@public
function Keys.Register(Controls, ControlName, Description, Action)
RegisterKeyMapping(string.format('keys-%s', ControlName), Description, "keyboard", Controls)
RegisterCommand(string.format('keys-%s', ControlName), function()
if (Action ~= nil) then
Action();
end
end, false)
end
13 changes: 13 additions & 0 deletions src/components/Util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
--- @author Dylan MALANDAIN, Kalyptus
--- @version 1.0.0
--- File created at [24/05/2021 09:57]
---

function math.round(num, numDecimalPlaces)
return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

function string.starts(String, Start)
return string.sub(String, 1, string.len(Start)) == Start
end
83 changes: 83 additions & 0 deletions src/components/Visual.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
--- @author Dylan MALANDAIN, Kalyptus
--- @version 1.0.0
--- File created at [24/05/2021 00:00]
---

Visual = {};

local function AddLongString(txt)
for i = 100, string.len(txt), 99 do
local sub = string.sub(txt, i, i + 99)
AddTextComponentSubstringPlayerName(sub)
end
end

function Visual.Notification(args)
if (not args.dict) and (args.name )then
args.dict = args.name
end
if not HasStreamedTextureDictLoaded(args.dict) then
RequestStreamedTextureDict(args.dict, false)
while not HasStreamedTextureDictLoaded(args.dict) do Wait(0) end
end
if (args.backId) then
ThefeedNextPostBackgroundColor(args.backId)
end
BeginTextCommandThefeedPost("jamyfafi")
if (args.message) then
AddTextComponentSubstringPlayerName(args.message)
if string.len(args.message) > 99 then
AddLongString(args.message)
end
end
if (args.title) and (args.subtitle) and (args.name) then
EndTextCommandThefeedPostMessagetext(args.dict or "CHAR_DEFAULT", args.name or "CHAR_DEFAULT", true, args.icon or 0, args.title or "", args.subtitle or "")
SetStreamedTextureDictAsNoLongerNeeded(args.dict)
end
EndTextCommandThefeedPostTicker(false, true)
end

function Visual.Subtitle(text, time)
ClearPrints()
BeginTextCommandPrint("STRING")
AddTextComponentSubstringPlayerName(text)
EndTextCommandPrint(time and math.ceil(time) or 0, true)
end

function Visual.FloatingHelpText(text, sound, loop)
BeginTextCommandDisplayHelp("jamyfafi")
AddTextComponentSubstringPlayerName(text)
if string.len(text) > 99 then
AddLongString(text)
end
EndTextCommandDisplayHelp(0, loop or 0, sound or true, -1)
end

function Visual.Prompt(text, spinner)
BeginTextCommandBusyspinnerOn("STRING")
AddTextComponentSubstringPlayerName(text)
EndTextCommandBusyspinnerOn(spinner or 1)
end

function Visual.PromptDuration(duration, text, spinner)
Citizen.CreateThread(function()
Citizen.Wait(0)
Visual.Prompt(text, spinner)
Citizen.Wait(duration)
if (BusyspinnerIsOn()) then
BusyspinnerOff();
end
end)
end

function Visual.FloatingHelpTextToEntity(text, x, y)
SetFloatingHelpTextScreenPosition(1, x, y)
SetFloatingHelpTextStyle(1, 1, 2, -1, 3, 0)
BeginTextCommandDisplayHelp("jamyfafi")
AddTextComponentSubstringPlayerName(text)
if string.len(text) > 99 then
AddLongString(text)
end
EndTextCommandDisplayHelp(2, false, false, -1)
end

0 comments on commit 5ee213b

Please sign in to comment.