LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

HUDElement

Description
Lightweight HUD UI element. Wraps an Overlay instance to display and provides a transform hierarchy of child HUDElement instances.
Functions

addChild

Description
Add a child HUD element to this element.
Definition
addChild(table childHudElement)
Arguments
tablechildHudElementHUDElement instance which is added as a child.
Code
72function HUDElement:addChild(childHudElement)
73 if childHudElement.parent == self then
74 return
75 end
76
77 if childHudElement.parent ~= nil then
78 childHudElement.parent:removeChild(childHudElement)
79 end
80
81 table.insert(self.children, childHudElement)
82 childHudElement.parent = self
83end

delete

Description
Delete this HUD element and all its children. This will also delete the overlay and thus release its engine handle.
Definition
delete()
Code
50function HUDElement:delete()
51 if self.overlay ~= nil then
52 self.overlay:delete()
53 self.overlay = nil
54 end
55
56 if self.parent ~= nil then
57 self.parent:removeChild(self)
58 end
59
60 self.parent = nil
61
62 for k, v in pairs(self.children) do
63 v.parent = nil -- saves the call to removeChild() on delete(), see above
64 v:delete()
65 self.children[k] = nil
66 end
67end

draw

Description
Draw this HUD element and all of its children in order of addition.
Definition
draw()
Code
298function HUDElement:draw()
299 if self.overlay.visible then
300 self.overlay:render()
301
302 for _, child in ipairs(self.children) do
303 child:draw()
304 end
305 end
306end

getAlpha

Description
Get this HUD element's color alpha value.
Definition
getAlpha()
Return Values
floatAlphavalue
Code
231function HUDElement:getAlpha()
232 return self.overlay.a
233end

getColor

Description
Get this HUD element's color.
Definition
getColor()
Return Values
floatRedvalue
floatGreenvalue
floatBluevalue
floatAlphavalue
Code
224function HUDElement:getColor()
225 return self.overlay.r, self.overlay.g, self.overlay.b, self.overlay.a
226end

getHeight

Description
Get this HUD element's height in screen space.
Definition
getHeight()
Code
243function HUDElement:getHeight()
244 return self.overlay.height
245end

getPosition

Description
Get this HUD element's position.
Definition
getPosition()
Return Values
floatXposition in screen space
floatYposition in screen space
Code
153function HUDElement:getPosition()
154 return self.overlay:getPosition()
155end

getRotationPivot

Description
Get this HUD element's rotation pivot point.
Definition
getRotationPivot()
Return Values
floatPivotx position offset from element position in screen space
floatPivoty position offset from element position in screen space
Code
145function HUDElement:getRotationPivot()
146 return self.pivotX, self.pivotY
147end

getScale

Description
Get this HUD element's scale.
Definition
getScale()
Return Values
Widthscalefactor
Heightscalefactor
Code
193function HUDElement:getScale()
194 return self.overlay:getScale()
195end

getVisible

Description
Get this HUD element's visibility.
Definition
getVisible()
Code
214function HUDElement:getVisible()
215 return self.overlay.visible
216end

getWidth

Description
Get this HUD element's width in screen space.
Definition
getWidth()
Code
237function HUDElement:getWidth()
238 return self.overlay.width
239end

new

Description
Create a new HUD element.
Definition
new(table subClass, table overlay, table parentHudElement)
Arguments
tablesubClassSubclass metatable for inheritance
tableoverlayWrapped Overlay instance
tableparentHudElement[optional] Parent HUD element of the newly created HUD element
Return Values
tableHUDElementinstance
Code
21function HUDElement.new(subClass, overlay, parentHudElement)
22 if not subClass or subClass == HUDElement then
23 subClass = HUDElement_mt
24 end
25
26 local self = setmetatable({}, subClass)
27
28 self.overlay = overlay
29 self.children = {}
30
31 self.pivotX = 0
32 self.pivotY = 0
33 self.defaultPivotX = 0
34 self.defaultPivotY = 0
35
36 -- animation
37 self.animation = TweenSequence.NO_SEQUENCE
38
39 self.parent = nil
40 if parentHudElement then
41 parentHudElement:addChild(self)
42 end
43
44 return self
45end

normalizeUVPivot

Description
Convert a texture space pivot to an element-local pivot.
Definition
normalizeUVPivot(table uvPivot, table uvs)
Arguments
tableuvPivotArray of two pixel pivot coordinates in texture space
tableuvsArray of UV coordinates as {x, y, width, height}
Code
338function HUDElement:normalizeUVPivot(uvPivot, size, uvs)
339 return self:scalePixelToScreenWidth(uvPivot[1] * size[1] / uvs[3]),
340 self:scalePixelToScreenHeight(uvPivot[2] * size[2] / uvs[4])
341end

removeChild

Description
Remove a child HUD element from this element.
Definition
removeChild(table childHudElement)
Arguments
tablechildHudElementHUDElement instance which is removed as a child.
Code
88function HUDElement:removeChild(childHudElement)
89 if childHudElement.parent == self then
90 for i, child in ipairs(self.children) do
91 if child == childHudElement then
92 child.parent = nil
93 table.remove(self.children, i)
94 return
95 end
96 end
97 end
98end

resetDimensions

Description
Reset this HUD element's dimensions to their default values. Resets width, height, scale and pivot.
Definition
resetDimensions()
Code
257function HUDElement:resetDimensions()
258 self.overlay:resetDimensions()
259 self.pivotX = self.defaultPivotX
260 self.pivotY = self.defaultPivotY
261end

scalePixelToScreenHeight

Description
Convert a vertical pixel value into scaled screen space value.
Definition
scalePixelToScreenHeight(float height)
Arguments
floatheightVertical pixel value
Code
323function HUDElement:scalePixelToScreenHeight(height)
324 return height * self.overlay.scaleHeight * g_aspectScaleY / g_referenceScreenHeight
325end

scalePixelToScreenVector

Description
Convert a vector from pixel values into scaled screen space values.
Definition
scalePixelToScreenVector(table vector2D)
Arguments
tablevector2DArray of two pixel values
Code
315function HUDElement:scalePixelToScreenVector(vector2D)
316 return vector2D[1] * self.overlay.scaleWidth * g_aspectScaleX / g_referenceScreenWidth,
317 vector2D[2] * self.overlay.scaleHeight * g_aspectScaleY / g_referenceScreenHeight
318end

scalePixelToScreenWidth

Description
Convert a horizontal pixel value into scaled screen space value.
Definition
scalePixelToScreenWidth(float width)
Arguments
floatwidthHorizontal pixel value
Code
330function HUDElement:scalePixelToScreenWidth(width)
331 return width * self.overlay.scaleWidth * g_aspectScaleX / g_referenceScreenWidth
332end

setAlignment

Description
Set this HUD element's positional alignment. See Overlay:setAlignment for positioning logic.
Definition
setAlignment(int vertical, int horizontal)
Arguments
intverticalVertical alignment value [Overlay.ALIGN_VERTICAL_BOTTOM | Overlay.ALIGN_VERTICAL_MIDDLE | Overlay.ALIGN_VERTICAL_TOP]
inthorizontalHorizontal alignment value [Overlay.ALIGN_HORIZONTAL_LEFT | Overlay.ALIGN_HORIZONTAL_CENTER | Overlay.ALIGN_HORIZONTAL_RIGHT]
Code
202function HUDElement:setAlignment(vertical, horizontal)
203 self.overlay:setAlignment(vertical, horizontal)
204end

setAlpha

Description
Set this HUD element overlay's color alpha value only.
Definition
setAlpha()
Code
272function HUDElement:setAlpha(alpha)
273 self.overlay:setColor(nil, nil, nil, alpha)
274end

setColor

Description
Set this HUD element overlay's color. Children are unaffected.
Definition
setColor()
Code
266function HUDElement:setColor(r, g, b, a)
267 self.overlay:setColor(r, g, b, a)
268end

setDimension

Description
Set this HUD element's width and height. Either value can be omitted (== nil) for no change.
Definition
setDimension()
Code
250function HUDElement:setDimension(width, height)
251 self.overlay:setDimension(width, height)
252end

setImage

Description
Set this HUD element overlay's image file.
Definition
setImage()
Code
278function HUDElement:setImage(imageFilename)
279 self.overlay:setImage(imageFilename)
280end

setPosition

Description
Set a HUD element's absolute screen space position. If the element has any children, they will be moved with this element.
Definition
setPosition()
Code
103function HUDElement:setPosition(x, y)
104 local prevX, prevY = self:getPosition()
105
106 -- substitute omitted parameters with current values to mirror Overlay behavior:
107 x = x or prevX
108 y = y or prevY
109
110 self.overlay:setPosition(x, y)
111
112 if #self.children > 0 then -- move children with self
113 local moveX, moveY = x - prevX, y - prevY
114
115 for _, child in pairs(self.children) do
116 local childX, childY = child:getPosition()
117 child:setPosition(childX + moveX, childY + moveY)
118 end
119 end
120end

setRotation

Description
Set this HUD element's rotation. Does not affect children. If no center position is given, the element's pivot values are used (default to 0)
Definition
setRotation(float rotation, float centerX, float centerY)
Arguments
floatrotationRotation in radians
floatcenterX[optional] Rotation pivot X position offset from overlay position in screen space
floatcenterY[optional] Rotation pivot Y position offset from overlay position in screen space
Code
128function HUDElement:setRotation(rotation, centerX, centerY)
129 self.overlay:setRotation(rotation, centerX or self.pivotX, centerY or self.pivotY)
130end

setRotationPivot

Description
Set this HUD element's rotation pivot point.
Definition
setRotationPivot(float pivotX, float pivotY)
Arguments
floatpivotXPivot x position offset from element position in screen space
floatpivotYPivot y position offset from element position in screen space
Code
136function HUDElement:setRotationPivot(pivotX, pivotY)
137 self.pivotX, self.pivotY = pivotX or self.defaultPivotX, pivotY or self.defaultPivotY
138 self.defaultPivotX, self.defaultPivotY = pivotX or self.defaultPivotX, pivotY or self.defaultPivotY
139end

setScale

Description
Set this HUD element's scale. This will move and scale children proportionally.
Definition
setScale(float scaleWidth, float scaleHeight)
Arguments
floatscaleWidthWidth scale factor
floatscaleHeightHeight scale factor
Code
162function HUDElement:setScale(scaleWidth, scaleHeight)
163 local prevSelfX, prevSelfY = self:getPosition()
164 local prevScaleWidth, prevScaleHeight = self:getScale()
165 self.overlay:setScale(scaleWidth, scaleHeight)
166 local selfX, selfY = self:getPosition()
167
168 if #self.children > 0 then
169 local changeFactorX, changeFactorY = scaleWidth / prevScaleWidth, scaleHeight / prevScaleHeight
170
171 for _, child in pairs(self.children) do
172 local childScaleWidth, childScaleHeight = child:getScale()
173
174 local childPrevX, childPrevY = child:getPosition()
175 local offX = childPrevX - prevSelfX
176 local offY = childPrevY - prevSelfY
177 local posX = selfX + offX * changeFactorX
178 local posY = selfY + offY * changeFactorY
179
180 child:setPosition(posX, posY)
181 child:setScale(childScaleWidth * changeFactorX, childScaleHeight * changeFactorY)
182 end
183 end
184
185 self.pivotX = self.defaultPivotX * scaleWidth
186 self.pivotY = self.defaultPivotY * scaleHeight
187end

setUVs

Description
Set this HUD element overlay's UV coordinates.
Definition
setUVs()
Code
284function HUDElement:setUVs(uvs)
285 self.overlay:setUVs(uvs)
286end

setVisible

Description
Set this HUD element's visibility.
Definition
setVisible()
Code
208function HUDElement:setVisible(isVisible)
209 self.overlay.visible = isVisible
210end

update

Description
Update this HUD element's state.
Definition
update()
Code
290function HUDElement:update(dt)
291 if not self.animation:getFinished() then
292 self.animation:update(dt)
293 end
294end