LUADOC - Farming Simulator 22

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
68function HUDElement:addChild(childHudElement)
69 if childHudElement.parent == self then
70 return
71 end
72
73 if childHudElement.parent ~= nil then
74 childHudElement.parent:removeChild(childHudElement)
75 end
76
77 table.insert(self.children, childHudElement)
78 childHudElement.parent = self
79end

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
46function HUDElement:delete()
47 if self.overlay ~= nil then
48 self.overlay:delete()
49 self.overlay = nil
50 end
51
52 if self.parent ~= nil then
53 self.parent:removeChild(self)
54 end
55
56 self.parent = nil
57
58 for k, v in pairs(self.children) do
59 v.parent = nil -- saves the call to removeChild() on delete(), see above
60 v:delete()
61 self.children[k] = nil
62 end
63end

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
151function HUDElement:getPosition()
152 return self.overlay:getPosition()
153end

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
143function HUDElement:getRotationPivot()
144 return self.pivotX, self.pivotY
145end

getScale

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

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(overlay, parentHudElement, customMt)
22 local self = setmetatable({}, customMt or HUDElement_mt)
23
24 self.overlay = overlay
25 self.children = {}
26
27 self.pivotX = 0
28 self.pivotY = 0
29 self.defaultPivotX = 0
30 self.defaultPivotY = 0
31
32 -- animation
33 self.animation = TweenSequence.NO_SEQUENCE
34
35 self.parent = nil
36 if parentHudElement then
37 parentHudElement:addChild(self)
38 end
39
40 return self
41end

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
339function HUDElement:normalizeUVPivot(uvPivot, size, uvs)
340 return self:scalePixelToScreenWidth(uvPivot[1] * size[1] / uvs[3]),
341 self:scalePixelToScreenHeight(uvPivot[2] * size[2] / uvs[4])
342end

removeChild

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

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
324function HUDElement:scalePixelToScreenHeight(height)
325 return height * self.overlay.scaleHeight * g_aspectScaleY / g_referenceScreenHeight
326end

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 --#debug assertWithCallstack(vector2D ~= nil)
317 return vector2D[1] * self.overlay.scaleWidth * g_aspectScaleX / g_referenceScreenWidth,
318 vector2D[2] * self.overlay.scaleHeight * g_aspectScaleY / g_referenceScreenHeight
319end

scalePixelToScreenWidth

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

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
200function HUDElement:setAlignment(vertical, horizontal)
201 self.overlay:setAlignment(vertical, horizontal)
202end

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
99function HUDElement:setPosition(x, y)
100 local prevX, prevY = self:getPosition()
101
102 -- substitute omitted parameters with current values to mirror Overlay behavior:
103 x = x or prevX
104 y = y or prevY
105
106 self.overlay:setPosition(x, y)
107
108 if #self.children > 0 then -- move children with self
109 local moveX, moveY = x - prevX, y - prevY
110
111 for _, child in pairs(self.children) do
112 local childX, childY = child:getPosition()
113
114 assertWithCallstack(childY ~= nil)
115 child:setPosition(childX + moveX, childY + moveY)
116 end
117 end
118end

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
126function HUDElement:setRotation(rotation, centerX, centerY)
127 self.overlay:setRotation(rotation, centerX or self.pivotX, centerY or self.pivotY)
128end

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
134function HUDElement:setRotationPivot(pivotX, pivotY)
135 self.pivotX, self.pivotY = pivotX or self.defaultPivotX, pivotY or self.defaultPivotY
136 self.defaultPivotX, self.defaultPivotY = pivotX or self.defaultPivotX, pivotY or self.defaultPivotY
137end

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
160function HUDElement:setScale(scaleWidth, scaleHeight)
161 local prevSelfX, prevSelfY = self:getPosition()
162 local prevScaleWidth, prevScaleHeight = self:getScale()
163 self.overlay:setScale(scaleWidth, scaleHeight)
164 local selfX, selfY = self:getPosition()
165
166 if #self.children > 0 then
167 local changeFactorX, changeFactorY = scaleWidth / prevScaleWidth, scaleHeight / prevScaleHeight
168
169 for _, child in pairs(self.children) do
170 local childScaleWidth, childScaleHeight = child:getScale()
171
172 local childPrevX, childPrevY = child:getPosition()
173 local offX = childPrevX - prevSelfX
174 local offY = childPrevY - prevSelfY
175 local posX = selfX + offX * changeFactorX
176 local posY = selfY + offY * changeFactorY
177
178 child:setPosition(posX, posY)
179 child:setScale(childScaleWidth * changeFactorX, childScaleHeight * changeFactorY)
180 end
181 end
182
183 self.pivotX = self.defaultPivotX * scaleWidth
184 self.pivotY = self.defaultPivotY * scaleHeight
185end

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
206function HUDElement:setVisible(isVisible)
207 if self.overlay ~= nil then
208 self.overlay.visible = isVisible
209 end
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