LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

KeyValueInfoHUDBox

Description
Info box with key-value layout
Parent
InfoHUDBox
Functions

addLine

Description
Definition
addLine()
Code
159function KeyValueInfoHUDBox:addLine(key, value, accentuate, accentuateColor)
160 local line
161 local cached = self.cachedLines
162 local numCached = #cached
163 if numCached > 0 then
164 line = self.cachedLines[numCached]
165 self.cachedLines[numCached] = nil
166 else
167 line = {}
168 end
169
170 line.key = key
171 line.value = value or ""
172 line.accentuate = accentuate
173 line.accentuateColor = accentuateColor
174
175 self.activeLines[#self.activeLines + 1] = line
176end

canDraw

Description
Definition
canDraw()
Code
34function KeyValueInfoHUDBox:canDraw()
35 return self.doShowNextFrame
36end

clear

Description
Definition
clear()
Code
109function KeyValueInfoHUDBox:clear()
110 for i = #self.activeLines, 1, -1 do
111 self.cachedLines[#self.cachedLines + 1] = self.activeLines[i]
112 self.activeLines[i] = nil
113 end
114end

draw

Description
Definition
draw()
Code
47function KeyValueInfoHUDBox:draw(posX, posY)
48 local rightX = posX
49 local leftX = posX - self.boxWidth
50 local y = posY
51
52 local height = 2 * self.listMarginHeight + #self.activeLines * self.rowHeight
53 drawFilledRect(leftX, y, self.boxWidth, height, 0, 0, 0, 0.75)
54
55 -- Draw title
56 setTextAlignment(RenderText.ALIGN_LEFT)
57 setTextColor(unpack(KeyValueInfoHUDBox.COLOR.TEXT_DEFAULT))
58 setTextBold(true)
59 renderText(leftX + self.labelTextOffsetX, y + height + self.labelTextOffsetY, self.titleTextSize, self.title)
60 setTextBold(false)
61
62 -- Displayitems
63 y = y + self.listMarginHeight
64 leftX = leftX + self.leftTextOffsetX + self.listMarginWidth
65 rightX = rightX - self.rightTextOffsetX - self.listMarginWidth
66
67 local textAreaX = self.boxWidth - self.leftTextOffsetX - self.listMarginWidth - self.rightTextOffsetX - self.listMarginWidth
68
69 for i = #self.activeLines, 1, -1 do
70 local line = self.activeLines[i]
71
72 setTextBold(true)
73 if line.accentuate then
74 setTextColor(unpack(line.accentuateColor or KeyValueInfoHUDBox.COLOR.TEXT_HIGHLIGHT))
75 end
76
77 setTextAlignment(RenderText.ALIGN_LEFT)
78 renderText(leftX, y + self.leftTextOffsetY, self.rowTextSize, line.key)
79
80 setTextAlignment(RenderText.ALIGN_RIGHT)
81 local maxWidth = textAreaX - 0.025 * self.boxWidth - getTextWidth(self.rowTextSize, line.key)
82 setTextBold(false)
83
84 local text = Utils.limitTextToWidth(line.value, self.rowTextSize, maxWidth, false, "...")
85 renderText(rightX, y + self.rightTextOffsetY, self.rowTextSize, text)
86
87 if line.accentuate then
88 setTextColor(unpack(KeyValueInfoHUDBox.COLOR.TEXT_DEFAULT))
89 end
90
91 if i < #self.activeLines then
92 drawFilledRect(leftX, y, self.rowWidth, 1 / g_screenHeight, unpack(KeyValueInfoHUDBox.COLOR.SEPARATOR))
93 end
94
95 y = y + self.rowHeight
96 end
97
98 setTextAlignment(RenderText.ALIGN_LEFT)
99
100 self.doShowNextFrame = false
101end

getDisplayHeight

Description
Get this HUD extension's display height.
Definition
getDisplayHeight()
Return Values
floatDisplayheight in screen space
Code
41function KeyValueInfoHUDBox:getDisplayHeight()
42 return 2 * self.listMarginHeight + #self.activeLines * self.rowHeight + self.labelTextSize + self.labelTextOffsetY
43end

new

Description
Definition
new()
Code
15function KeyValueInfoHUDBox.new(uiScale)
16 local self = InfoHUDBox.new(KeyValueInfoHUDBox_mt, uiScale)
17
18 self.displayComponents = {}
19
20 self.cachedLines = {}
21 self.activeLines = {}
22
23 self.title = "Unknown Title"
24
25 return self
26end

setScale

Description
Definition
setScale()
Code
190function KeyValueInfoHUDBox:setScale(uiScale)
191 self.uiScale = uiScale
192 self:storeScaledValues()
193end

setTitle

Description
Definition
setTitle()
Code
118function KeyValueInfoHUDBox:setTitle(title)
119 title = utf8ToUpper(title)
120 if title ~= self.title then
121 self.title = title
122
123 -- Try to fit text by wrapping it at max-length and then testing is we lost any characters after
124 -- the first line
125 self.titleTextSize = self:textSizeToFit(self.labelTextSize, self.title, self.boxWidth)
126 end
127end

showNextFrame

Description
Definition
showNextFrame()
Code
180function KeyValueInfoHUDBox:showNextFrame()
181 self.doShowNextFrame = true
182end

storeScaledValues

Description
Definition
storeScaledValues()
Code
197function KeyValueInfoHUDBox:storeScaledValues()
198 local scale = self.uiScale
199
200 local function normalize(x, y)
201 return x * scale * g_aspectScaleX / g_referenceScreenWidth, y * scale * g_aspectScaleY / g_referenceScreenHeight
202 end
203
204 self.boxWidth = normalize(340, 0)
205
206 local _
207 _, self.labelTextSize = normalize(0, HUDElement.TEXT_SIZE.DEFAULT_TITLE)
208 _, self.rowTextSize = normalize(0, HUDElement.TEXT_SIZE.DEFAULT_TEXT)
209 self.titleTextSize = self.labelTextSize
210
211 self.labelTextOffsetX, self.labelTextOffsetY = normalize(0, 3)
212 self.leftTextOffsetX, self.leftTextOffsetY = normalize(0, 6)
213 self.rightTextOffsetX, self.rightTextOffsetY = normalize(0, 6)
214
215 self.rowWidth, self.rowHeight = normalize(308, 26)
216 self.listMarginWidth, self.listMarginHeight = normalize(16, 15)
217end

textSizeToFit

Description
Definition
textSizeToFit()
Code
131function KeyValueInfoHUDBox:textSizeToFit(baseSize, text, maxWidth, minSize)
132 local size = baseSize
133 if minSize == nil then
134 minSize = baseSize / 2
135 end
136
137 setTextWrapWidth(maxWidth)
138 local lengthWithNoLineLimit = getTextLength(size, text, 99999)
139
140 while getTextLength(size, text, 1) < lengthWithNoLineLimit do
141 size = size - baseSize * 0.05
142
143 -- Limit size. Cut off any extra text
144 if size <= baseSize / 2 then
145 -- Undo
146 size = size + baseSize * 0.05
147
148 break
149 end
150 end
151
152 setTextWrapWidth(0)
153
154 return size
155end