LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

HUDTextDisplay

Description
HUD text display. Displays a formatted single-line text with optional animations.
Parent
HUDDisplayElement
Functions

draw

Description
Draw the text.
Definition
draw()
Code
142function HUDTextDisplay:draw()
143 setTextBold(self.textBold)
144 local posX, posY = self:getPosition()
145 setTextAlignment(self.textAlignment)
146
147 setTextWrapWidth(0.9)
148
149 if self.hasShadow then
150 local offset = self.screenTextSize * HUDTextDisplay.SHADOW_OFFSET_FACTOR
151 local r, g, b, a = unpack(self.shadowColor)
152 setTextColor(r, g, b, a * self.overlay.a)
153 renderText(posX + offset, posY - offset, self.screenTextSize, self.text)
154 end
155
156 local r, g, b, a = unpack(self.textColor)
157 setTextColor(r, g, b, a * self.overlay.a)
158 renderText(posX, posY, self.screenTextSize, self.text)
159
160 setTextAlignment(RenderText.ALIGN_LEFT)
161 setTextWrapWidth(0)
162 setTextBold(false)
163 setTextColor(1, 1, 1, 1)
164end

new

Description
Create a new HUDTextDisplay.
Definition
new(float posX, float posY, float textSize, int textAlignment, table textColor, bool textBool)
Arguments
floatposXScreen space X position of the text display
floatposYScreen space Y position of the text display
floattextSizeText size in reference resolution pixels
inttextAlignmentText alignment as one of RenderText.[ALIGN_LEFT | ALIGN_CENTER | ALIGN_RIGHT]
tabletextColorText display color as an array {r, g, b, a}
booltextBoolIf true, will render the text in bold
Return Values
tableHUDTextDisplayinstance
Code
27function HUDTextDisplay.new(posX, posY, textSize, textAlignment, textColor, textBold)
28 local backgroundOverlay = Overlay.new(nil, 0, 0, 0, 0)
29 backgroundOverlay:setColor(1, 1, 1, 1)
30 local self = HUDTextDisplay:superClass().new(backgroundOverlay, nil, HUDTextDisplay_mt)
31
32 self.initialPosX = posX
33 self.initialPosY = posY
34 self.text = "" -- must be set in a separate call which will correctly set up boundaries and position
35 self.textSize = textSize or 0
36 self.screenTextSize = self:scalePixelToScreenHeight(self.textSize)
37 self.textAlignment = textAlignment or RenderText.ALIGN_LEFT
38 self.textColor = textColor or {1, 1, 1, 1}
39 self.textBold = textBold or false
40
41 self.hasShadow = false
42 self.shadowColor = {0, 0, 0, 1}
43
44 return self
45end

setAlpha

Description
Set the global alpha value for this text display. The alpha value will be multiplied with any text color alpha channel value.
Definition
setAlpha()
Code
101function HUDTextDisplay:setAlpha(alpha)
102 self:setColor(nil, nil, nil, alpha)
103end

setAnimation

Description
Set an animation tween (sequence) for this text display. The animation can be played when calling HUDTextDisplay:setVisible() with the "animate" paramter set to true.
Definition
setAnimation()
Code
127function HUDTextDisplay:setAnimation(animationTween)
128 self:storeOriginalPosition()
129 self.animation = animationTween or TweenSequence.NO_SEQUENCE
130end

setScale

Description
Set the text display UI scale.
Definition
setScale()
Code
73function HUDTextDisplay:setScale(uiScale)
74 HUDTextDisplay:superClass().setScale(self, uiScale)
75
76 self.screenTextSize = self:scalePixelToScreenHeight(self.textSize)
77end

setText

Description
Set the text to display.
Definition
setText(string text, float textSize, int textAlignment, table textColor, bool textBool)
Arguments
stringtextDisplay text
floattextSizeText size in reference resolution pixels
inttextAlignmentText alignment as one of RenderText.[ALIGN_LEFT | ALIGN_CENTER | ALIGN_RIGHT]
tabletextColorText display color as an array {r, g, b, a}
booltextBoolIf true, will render the text in bold
Code
54function HUDTextDisplay:setText(text, textSize, textAlignment, textColor, textBold)
55 -- assign values with initial values as defaults
56 self.text = text or self.text
57 self.textSize = textSize or self.textSize
58 self.screenTextSize = self:scalePixelToScreenHeight(self.textSize)
59 self.textAlignment = textAlignment or self.textAlignment
60 self.textColor = textColor or self.textColor
61 self.textBold = textBold or self.textBold
62
63 local width, height = getTextWidth(self.screenTextSize, self.text), getTextHeight(self.screenTextSize, self.text)
64 self:setDimension(width, height)
65
66 local posX, posY = self.initialPosX, self.initialPosY
67
68 self:setPosition(posX, posY)
69end

setTextColorChannels

Description
Set the text color by channels. Use for dynamic changes and animation.
Definition
setTextColorChannels()
Code
108function HUDTextDisplay:setTextColorChannels(r, g, b, a)
109 self.textColor[1] = r
110 self.textColor[2] = g
111 self.textColor[3] = b
112 self.textColor[4] = a
113end

setTextShadow

Description
Set the text shadow state.
Definition
setTextShadow(bool isShadowEnabled, table shadowColor)
Arguments
boolisShadowEnabledIf true, will cause a shadow to be rendered under the text
tableshadowColorShadow text color as an array {r, g, b, a}
Code
119function HUDTextDisplay:setTextShadow(isShadowEnabled, shadowColor)
120 self.hasShadow = isShadowEnabled or self.hasShadow
121 self.shadowColor = shadowColor or self.shadowColor
122end

setVisible

Description
Set this element's visibility.
Definition
setVisible(bool isVisible, bool animate)
Arguments
boolisVisibleVisibility state
boolanimateIf true, will play the currently set animation on becoming visible or and reset it when necessary.
Code
83function HUDTextDisplay:setVisible(isVisible, animate)
84 -- shadow parent behavior which includes repositioning
85 HUDElement.setVisible(self, isVisible)
86
87 if animate then
88 if not isVisible or not self.animation:getFinished() then
89 self.animation:reset()
90 end
91
92 if isVisible then
93 self.animation:start()
94 end
95 end
96end

update

Description
Update this element's state.
Definition
update()
Code
134function HUDTextDisplay:update(dt)
135 if self:getVisible() then
136 HUDTextDisplay:superClass().update(self, dt)
137 end
138end