LUADOC - Farming Simulator 19

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
147function HUDTextDisplay:draw()
148 setTextBold(self.textBold)
149 local posX, posY = self:getPosition()
150 -- NOTE: alignment is factored into background overlay position, use left alignment now
151 setTextAlignment(RenderText.ALIGN_LEFT)
152
153 if self.hasShadow then
154 local offset = self.screenTextSize * HUDTextDisplay.SHADOW_OFFSET_FACTOR
155 local r, g, b, a = unpack(self.shadowColor)
156 setTextColor(r, g, b, a * self.overlay.a)
157 renderText(posX + offset, posY - offset, self.screenTextSize, self.text)
158 end
159
160 local r, g, b, a = unpack(self.textColor)
161 setTextColor(r, g, b, a * self.overlay.a)
162 renderText(posX, posY, self.screenTextSize, self.text)
163
164 setTextBold(false)
165 setTextColor(1, 1, 1, 1)
166end

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(HUDTextDisplay_mt, backgroundOverlay, nil)
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
106function HUDTextDisplay:setAlpha(alpha)
107 self:setColor(nil, nil, nil, alpha)
108end

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
132function HUDTextDisplay:setAnimation(animationTween)
133 self:storeOriginalPosition()
134 self.animation = animationTween or TweenSequence.NO_SEQUENCE
135end

setScale

Description
Set the text display UI scale.
Definition
setScale()
Code
78function HUDTextDisplay:setScale(uiScale)
79 HUDTextDisplay:superClass().setScale(self, uiScale)
80
81 self.screenTextSize = self:scalePixelToScreenHeight(self.textSize)
82end

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 if self.textAlignment == RenderText.ALIGN_CENTER then
68 posX = posX - width * 0.5
69 elseif self.textAlignment == RenderText.ALIGN_RIGHT then
70 posX = posX - width
71 end
72
73 self:setPosition(posX, posY)
74end

setTextColorChannels

Description
Set the text color by channels. Use for dynamic changes and animation.
Definition
setTextColorChannels()
Code
113function HUDTextDisplay:setTextColorChannels(r, g, b, a)
114 self.textColor[1] = r
115 self.textColor[2] = g
116 self.textColor[3] = b
117 self.textColor[4] = a
118end

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
124function HUDTextDisplay:setTextShadow(isShadowEnabled, shadowColor)
125 self.hasShadow = isShadowEnabled or self.hasShadow
126 self.shadowColor = shadowColor or self.shadowColor
127end

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
88function HUDTextDisplay:setVisible(isVisible, animate)
89 -- shadow parent behavior which includes repositioning
90 HUDElement.setVisible(self, isVisible)
91
92 if animate then
93 if not isVisible or not self.animation:getFinished() then
94 self.animation:reset()
95 end
96
97 if isVisible then
98 self.animation:start()
99 end
100 end
101end

update

Description
Update this element's state.
Definition
update()
Code
139function HUDTextDisplay:update(dt)
140 if self:getVisible() then
141 HUDTextDisplay:superClass().update(self, dt)
142 end
143end