LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

GuiUtils

Description
GUI utility functions
Functions

checkOverlayOverlap

Description
Check if a point lies within or a hotspot overlaps an overlay.
Definition
checkOverlayOverlap(posX Point, posY Point, overlayX Overlay, overlayY Overlay, overlaySizeX Overlay, overlaySizeY Overlay, hotspot If)
Arguments
posXPointor hotspot x position
posYPointor hotspot y position
overlayXOverlayx position
overlayYOverlayy position
overlaySizeXOverlaywidth
overlaySizeYOverlayheight
hotspotIfprovided as an array having 4 numbers for the bounding points of a rectangle {minX, minY, maxX, maxY}, will be checked if it overlaps the overlay area given by the other parameters.
Code
125function GuiUtils.checkOverlayOverlap(posX, posY, overlayX, overlayY, overlaySizeX, overlaySizeY, hotspot)
126 if hotspot ~= nil and #hotspot == 4 then
127 return posX >= overlayX + hotspot[1] and posX <= overlayX + overlaySizeX + hotspot[3] and posY >= overlayY + hotspot[2] and posY <= overlayY + overlaySizeY + hotspot[4]
128 else
129 return posX >= overlayX and posX <= overlayX + overlaySizeX and posY >= overlayY and posY <= overlayY + overlaySizeY
130 end
131end

get2DArray

Description
Transform an attribute string representing a 2D array into an actual array.
Definition
get2DArray(str Attribute, defaultValue Default)
Arguments
strAttributestring containing exactly 2 numbers
defaultValueDefaultvalue to return if the "str" parameter value is nil or invalid for transformation.
Return Values
Arrayofthe 2 converted values as numbers: {value1, value2}
Code
62function GuiUtils.get2DArray(str, defaultValue)
63 if str ~= nil then
64 local parts = StringUtil.splitString(" ", str)
65 local x,y = unpack(parts)
66 if x ~= nil and y ~= nil then
67 return {Utils.evaluateFormula(x), Utils.evaluateFormula(y)}
68 end
69 end
70
71 return defaultValue
72end

get4DArray

Description
Transform an attribute string representing a 4D array into an actual array.
Definition
get4DArray(str Attribute, defaultValue Default)
Arguments
strAttributestring containing exactly 4 numbers
defaultValueDefaultvalue to return if the "str" parameter value is nil or invalid for transformation.
Return Values
Arrayofthe 4 converted values as numbers: {value1, value2, value3, value4}
Code
79function GuiUtils.get4DArray(str, defaultValue)
80 local w,x,y,z = StringUtil.getVectorFromString(str)
81 if w ~= nil and x ~= nil and y ~= nil and z ~= nil then
82 return {w, x, y, z}
83 end
84
85 return defaultValue
86end

getColorArray

Description
Transform an attribute string representing a 4D color array into an actual array.
Definition
getColorArray(str Attribute, defaultValue Default)
Arguments
strAttributestring containing exactly 4 numbers
defaultValueDefaultvalue to return if the "str" parameter value is nil or invalid for transformation.
Return Values
Arrayofthe 4 converted values as numbers: {red, green, blue, alpha}
Code
93function GuiUtils.getColorArray(colorStr, defaultValue)
94 local r,g,b,a = StringUtil.getVectorFromString(colorStr)
95 if r ~= nil and g ~= nil and b ~= nil and a ~= nil then
96 return {r, g, b, a}
97 end
98 return defaultValue
99end

getNormalizedValues

Description
Transform an attribute string representing a list of numbers into an array and normalize the values.
Definition
getNormalizedValues(str Attribute, refSize Reference, defaultValue Default)
Arguments
strAttributestring containing numbers, either raw or with a pixel unit designation on each number (e.g. "12 24" or "12px 24px")
refSizeReferencesize for normalization, e.g. a reference screen resolution used to scale pixel values, {sizeX, sizeY}
defaultValueDefaultvalue to return if the "str" parameter value is nil
Return Values
Arrayofnormalized values
Code
18function GuiUtils.getNormalizedValues(str, refSize, defaultValue)
19 if str ~= nil then
20 local parts = StringUtil.splitString(" ", str)
21 local values = {}
22 for k, part in pairs(parts) do
23 local isPixelValue, isDisplayPixelValue = false, false
24 local value = part
25 if string.find(value, "px") ~= nil then
26 isPixelValue = true
27 value = string.gsub(value, "px", "")
28 elseif string.find(value, "dp") ~= nil then
29 isDisplayPixelValue = true
30 value = string.gsub(value, "dp", "")
31 end
32
33 value = Utils.evaluateFormula(value)
34
35 if isPixelValue then
36 -- refSize stores only 2 values (width, height). As str can contains more than 2 values we have to do a
37 -- loop match with this modulo operation (1->1, 2->2, 3->1, 4->2, 5->1, 6->1...)
38 value = value / refSize[((k + 1) % 2) + 1]
39 elseif isDisplayPixelValue then
40 local s = (k + 1) % 2
41 if s == 0 then -- horizontal
42 value = value / g_screenWidth
43 else -- vertical
44 value = value / g_screenHeight
45 end
46 end
47
48 table.insert(values, value)
49 end
50
51 return values
52 end
53
54 return defaultValue
55end

getUVs

Description
Transform an attribute string representing a UV array into an actual array and normalize the values.
Definition
getUVs(str Attribute, ref Texture, defaultValue Default)
Arguments
strAttributestring containing exactly 4 numbers, order and format: "x[px] y[px] sizeX[px] sizeY[px]"
refTexturereference size used to normalize pixel UV coordinates into unit sized UV coordinates
defaultValueDefaultvalue to return if the "str" parameter value is nil or invalid for transformation.
Return Values
Arrayofthe UV coordinates as {u1, v1, u2, v2, u3, v3, u4, v4}
Code
107function GuiUtils.getUVs(str, ref, defaultValue)
108 if str ~= nil then
109 local uvs = GuiUtils.getNormalizedValues(str, ref)
110 return {uvs[1], 1 - uvs[2] - uvs[4], uvs[1], 1 - uvs[2], uvs[1] + uvs[3], 1 - uvs[2] - uvs[4], uvs[1] + uvs[3], 1 - uvs[2]}
111 end
112
113 return defaultValue
114end