LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

InputGlyphElement

Description
Input glyph display element. Displays a key or button glyph for an input.
Parent
HUDElement
Functions

delete

Description
Delete this element and release its resources.
Definition
delete()
Code
71function InputGlyphElement:delete()
72 InputGlyphElement:superClass().delete(self)
73
74 self.keyboardOverlay:delete()
75 self:deleteOverlayCopies()
76end

deleteOverlayCopies

Description
Delete any overlay copies.
Definition
deleteOverlayCopies()
Code
88function InputGlyphElement:deleteOverlayCopies()
89 for k, v in pairs(self.overlayCopies) do
90 v:delete()
91 self.overlayCopies[k] = nil
92 end
93end

draw

Description
Draw the input glyph(s).
Definition
draw()
Code
332function InputGlyphElement:draw(clipX1, clipY1, clipX2, clipY2)
333 if #self.actionNames == 0 or not self.overlay.visible then
334 return
335 end
336
337 local posX, posY = self:getPosition()
338
339 if self.hasButtonOverlays then
340 for _, actionName in ipairs(self.actionNames) do
341 posX = self:drawControllerButtons(self.buttonOverlays[actionName], posX, posY, clipX1, clipY1, clipX2, clipY2)
342 end
343 elseif self.hasKeyNames then
344 for _, actionName in ipairs(self.actionNames) do
345 posX = self:drawKeyboardKeys(self.keyNames[actionName], posX, posY, clipX1, clipY1, clipX2, clipY2)
346 end
347 end
348
349 if self.actionText ~= nil then
350 self:drawActionText(posX, posY, clipX1, clipY1, clipX2, clipY2)
351 end
352end

drawActionText

Description
Draw the action text after the input glyphs.
Definition
drawActionText(float posX, float posY)
Arguments
floatposXDrawing X position in screen space
floatposYDrawing Y position in screen space
Code
418function InputGlyphElement:drawActionText(posX, posY, clipX1, clipY1, clipX2, clipY2)
419 setTextAlignment(RenderText.ALIGN_LEFT)
420 setTextBold(self.bold)
421 setTextColor(unpack(self.color))
422
423 if clipX1 ~= nil then
424 setTextClipArea(clipX1, clipY1, clipX2, clipY2)
425 end
426
427 renderText(posX + self.textOffsetX, posY + self.actionTextSize * 0.5, self.actionTextSize, self.displayText)
428
429 if clipX1 ~= nil then
430 setTextClipArea(0, 0, 1, 1)
431 end
432end

drawControllerButtons

Description
Draw controller button glyphs.
Definition
drawControllerButtons(table Array, float posX, float posY)
Arguments
tableArrayof controller button glyph overlays
floatposXInitial drawing X position in screen space
floatposYInitial drawing Y position in screen space
Return Values
floatXposition in screen space after the last glyph
Code
360function InputGlyphElement:drawControllerButtons(buttonOverlays, posX, posY, clipX1, clipY1, clipX2, clipY2)
361 for i, overlay in ipairs(buttonOverlays) do
362 if i > 1 then
363 local separatorType = self.separators[i - 1]
364 local separatorOverlay = self.orOverlay
365 local separatorWidth = 0
366 local separatorHeight = 0
367 if separatorType == InputHelpElement.SEPARATOR.COMBO_INPUT then
368 separatorOverlay = self.plusOverlay
369 separatorWidth, separatorHeight = self.plusIconSizeX, self.plusIconSizeY
370 elseif separatorType == InputHelpElement.SEPARATOR.ANY_INPUT then
371 separatorWidth, separatorHeight = self.orIconSizeX, self.orIconSizeY
372 end
373
374 separatorOverlay:setColor(nil, nil, nil, self.buttonColor[4])
375 separatorOverlay:setPosition(posX, posY + separatorHeight)
376 separatorOverlay:setDimension(separatorWidth, separatorHeight)
377 separatorOverlay:render(clipX1, clipY1, clipX2, clipY2)
378
379 separatorOverlay:setColor(nil, nil, nil, 1) -- reset alpha
380
381 separatorOverlay:resetDimensions()
382 posX = posX + separatorWidth + self.glyphOffsetX
383 end
384
385 overlay:setPosition(posX, posY + self.iconSizeY * 0.5) -- controller symbols are vertically aligned to middle
386 overlay:setDimension(self.iconSizeX, self.iconSizeY)
387 overlay:setColor(unpack(self.buttonColor))
388 overlay:render(clipX1, clipY1, clipX2, clipY2)
389
390 overlay:resetDimensions()
391
392 local padding = i < #buttonOverlays and self.glyphOffsetX or 0
393 posX = posX + self.iconSizeX + padding
394 end
395
396 return posX
397end

drawKeyboardKeys

Description
Draw keyboard key glyphs.
Definition
drawKeyboardKeys(table Array, float posX, float posY)
Arguments
tableArrayof keyboard key names
floatposXInitial drawing X position in screen space
floatposYInitial drawing Y position in screen space
Return Values
floatXposition in screen space after the last glyph
Code
405function InputGlyphElement:drawKeyboardKeys(keyNames, posX, posY, clipX1, clipY1, clipX2, clipY2)
406 for i, key in ipairs(keyNames) do
407 local padding = i < #keyNames and self.glyphOffsetX or 0
408 posX = posX + self.keyboardOverlay:renderButton(key, posX, posY, self.iconSizeY, nil, true, clipX1, clipY1, clipX2, clipY2) + padding
409 end
410
411 return posX
412end

getGlyphWidth

Description
Get the screen space width required by the glyphs used to display input in the current input context.
Definition
getGlyphWidth()
Code
297function InputGlyphElement:getGlyphWidth()
298 local width = 0
299 if self.hasButtonOverlays then
300 for _, actionName in ipairs(self.actionNames) do
301 for i, _ in ipairs(self.buttonOverlays[actionName]) do
302 if i > 1 then
303 local separatorType = self.separators[i - 1]
304 local separatorWidth = 0
305 if separatorType == InputHelpElement.SEPARATOR.COMBO_INPUT then
306 separatorWidth = self.plusIconSizeX
307 elseif separatorType == InputHelpElement.SEPARATOR.ANY_INPUT then
308 separatorWidth = self.orIconSizeX
309 end
310
311 width = width + separatorWidth + self.glyphOffsetX
312 end
313
314 local padding = i < #self.buttonOverlays[actionName] and self.glyphOffsetX or 0
315 width = width + self.iconSizeX + padding
316 end
317 end
318 elseif self.hasKeyNames then
319 for _, actionName in ipairs(self.actionNames) do
320 for i, key in ipairs(self.keyNames[actionName]) do
321 local padding = i < #self.keyNames[actionName] and self.glyphOffsetX or 0
322 width = width + self.keyboardOverlay:getButtonWidth(key, self.iconSizeY) + padding
323 end
324 end
325 end
326
327 return width
328end

new

Description
Create a new instance of InputGlyphElement.
Definition
new(table inputDisplayManager, float baseWidth, float baseHeight)
Arguments
tableinputDisplayManagerInputDisplayManager reference
floatbaseWidthDefault width of this element in screen space
floatbaseHeightDefault height of this element in screen space
Code
27function InputGlyphElement.new(inputDisplayManager, baseWidth, baseHeight)
28 local backgroundOverlay = Overlay.new(nil, 0, 0, baseWidth, baseHeight)
29 local self = InputGlyphElement:superClass().new(backgroundOverlay, nil, InputGlyphElement_mt)
30
31 self.inputDisplayManager = inputDisplayManager
32 self.plusOverlay = inputDisplayManager:getPlusOverlay()
33 self.orOverlay = inputDisplayManager:getOrOverlay()
34 self.keyboardOverlay = ButtonOverlay.new()
35
36 self.actionNames = {}
37 self.actionText = nil -- optional action text to display next to the glyph
38 self.displayText = nil -- lower or upper cased version of actionText for rendering
39 self.actionTextSize = InputGlyphElement.DEFAULT_TEXT_SIZE
40 self.inputHelpElement = nil
41
42 self.buttonOverlays = {} -- action name -> overlays
43 self.hasButtonOverlays = false
44 self.separators = {} -- {i=InputHelpElement.SEPARATOR}
45 self.keyNames = {} -- action name -> key names
46 self.hasKeyNames = false
47
48 self.color = {1, 1, 1, 1} -- RGBA array
49 self.buttonColor = {1, 1, 1, 1} -- RGBA array
50 self.overlayCopies = {} -- contains overlay copies which need to be deleted
51
52 self.baseWidth, self.baseHeight = baseWidth, baseHeight
53
54 self.glyphOffsetX = 0
55 self.textOffsetX = 0
56 self.iconSizeX, self.iconSizeY = baseWidth, baseHeight
57 self.plusIconSizeX, self.plusIconSizeY = baseWidth * 0.5, baseHeight * 0.5
58 self.orIconSizeX, self.orIconSizeY = baseWidth * 0.5, baseHeight * 0.5
59
60 self.alignX, self.alignY = 1, 1
61 self.alignmentOffsetX, self.alignmentOffsetY = 0, 0
62 self.lowerCase = false
63 self.upperCase = false
64 self.bold = false
65
66 return self
67end

setAction

Description
Set the action whose input glyphs need to be displayed by this element.
Definition
setAction(string actionName, string actionText, float actionTextSize, bool noModifiers, bool copyOverlays)
Arguments
stringactionNameInputAction name
stringactionText[optional] Additional action text to display after the glyph
floatactionTextSize[optional] Additional action text size in screen space
boolnoModifiers[optional] If true, will only show the input glyph of the last unmodified input binding axis
boolcopyOverlays[optional] If true, will create and handle a separate copy of an input glyph. Do not use this when updating the action each frame!
Code
166function InputGlyphElement:setAction(actionName, actionText, actionTextSize, noModifiers, copyOverlays)
167 -- use this instance's action names array instead of creating a new one each time this is called
168 clearTable(self.actionNames)
169 table.insert(self.actionNames, actionName)
170 self:setActions(self.actionNames, actionText, actionTextSize, noModifiers, copyOverlays)
171end

setActions

Description
Set multiple actions whose input glyphs need to be displayed by this element. If exactly two actions are passed in, they will be interpreted as belonging to the same axis and the system tries to resolved the actions to a combined glyph. Otherwise, the glyphs will just be displayed in order of the actions.
Definition
setActions(table actionNames, string actionText, float actionTextSize, bool noModifiers, bool copyOverlays)
Arguments
tableactionNamesInputAction names array
stringactionText[optional] Additional action text to display after the glyph
floatactionTextSize[optional] Additional action text size in screen space
boolnoModifiers[optional] If true, will only show the input glyph of the last unmodified input binding axis
boolcopyOverlays[optional] If true, will create and handle a separate copy of an input glyph. Do not use this when updating the action each frame!
Code
183function InputGlyphElement:setActions(actionNames, actionText, actionTextSize, noModifiers, copyOverlays)
184 self.actionNames = actionNames
185 self.actionText = actionText
186 self.actionTextSize = actionTextSize or InputGlyphElement.DEFAULT_TEXT_SIZE
187
188 self:updateDisplayText() -- apply lower / upper case if necessary
189
190 local height = self:getHeight()
191 local width = 0
192
193 self:deleteOverlayCopies()
194
195 local isDoubleAction = #actionNames == 2
196
197 for i, actionName in ipairs(actionNames) do
198 local actionName2 = nil
199 if isDoubleAction then
200 actionName2 = actionNames[i + 1]
201 end
202
203 local helpElement = self.inputDisplayManager:getControllerSymbolOverlays(actionName, actionName2, "", noModifiers)
204 local buttonOverlays = helpElement.buttons
205 self.separators = helpElement.separators
206
207 if copyOverlays then
208 local originals = buttonOverlays
209 buttonOverlays = {}
210
211 for _, overlay in ipairs(originals) do
212 -- TODO: make overlay:clone() function
213 local overlayCopy = Overlay.new(overlay.filename, overlay.x, overlay.y, overlay.defaultWidth, overlay.defaultHeight)
214 overlayCopy:setUVs(overlay.uvs)
215 overlayCopy:setAlignment(overlay.alignmentVertical, overlay.alignmentHorizontal)
216
217 table.insert(self.overlayCopies, overlayCopy)
218 table.insert(buttonOverlays, overlayCopy)
219 end
220 end
221
222 if self.buttonOverlays[actionName] == nil then
223 self.buttonOverlays[actionName] = {}
224 else
225 for j=1, #self.buttonOverlays[actionName] do
226 self.buttonOverlays[actionName][j] = nil
227 end
228 end
229 self.hasButtonOverlays = false
230
231 if #buttonOverlays > 0 then
232 for _, overlay in ipairs(buttonOverlays) do
233 table.insert(self.buttonOverlays[actionName], overlay)
234 self.hasButtonOverlays = true
235 end
236 end
237
238 if self.keyNames[actionName] == nil then
239 self.keyNames[actionName] = {}
240 else
241 for j=1, #self.keyNames[actionName] do
242 self.keyNames[actionName][j] = nil
243 end
244 end
245 self.hasKeyNames = false
246
247 if #helpElement.keys > 0 then
248 for _, key in ipairs(helpElement.keys) do
249 table.insert(self.keyNames[actionName], key)
250 self.hasKeyNames = true
251 end
252 end
253
254 if isDoubleAction then
255 table.remove(self.actionNames, 2)
256 break -- should have resolved everything now
257 end
258 end
259
260 if self.hasButtonOverlays then
261 for _, buttonOverlays in pairs(self.buttonOverlays) do
262 for i, _ in ipairs(buttonOverlays) do
263 if i > 1 then -- TODO: use separator types to get width
264 width = width + self.plusIconSizeX + self.glyphOffsetX
265 end
266
267 width = width + self.iconSizeX + (i < #buttonOverlays and self.glyphOffsetX or 0)
268 end
269 end
270 elseif self.hasKeyNames then
271 for _, keyNames in pairs(self.keyNames) do
272 for _, key in ipairs(keyNames) do
273 width = width + self.keyboardOverlay:getButtonWidth(key, height)
274 end
275 end
276 end
277
278 -- adjust this element's size so other elements can correctly offset from this
279 self:setDimension(width, height)
280end

setBold

Description
Set the glyph text to be displayed in bold print or not.
Definition
setBold()
Code
130function InputGlyphElement:setBold(isBold)
131 self.bold = isBold
132end

setButtonGlyphColor

Description
Set the color for button glyphs.
Definition
setButtonGlyphColor(table color)
Arguments
tablecolorColor as an RGBA array
Code
145function InputGlyphElement:setButtonGlyphColor(color)
146 self.buttonColor = color
147
148 for _, actionName in ipairs(self.actionNames) do
149 local buttonOverlays = self.buttonOverlays[actionName]
150 if buttonOverlays ~= nil then -- safety-catch to avoid errors for invalid setups (will just not show icon)
151 for _, overlay in pairs(buttonOverlays) do
152 overlay:setColor(unpack(color))
153 end
154 end
155 end
156end

setKeyboardGlyphColor

Description
Set the button frame color for the keyboard glyphs.
Definition
setKeyboardGlyphColor(table color)
Arguments
tablecolorColor as an RGBA array
Code
137function InputGlyphElement:setKeyboardGlyphColor(color)
138 self.color = color
139 self.keyboardOverlay:setColor(unpack(color))
140end

setLowerCase

Description
Set the glyph text to be displayed in all lower case or not. This resets the upper case setting if lower case is enabled.
Definition
setLowerCase()
Code
122function InputGlyphElement:setLowerCase(enableLowerCase)
123 self.lowerCase = enableLowerCase
124 self.upperCase = self.upperCase and not enableLowerCase
125 self:updateDisplayText()
126end

setScale

Description
Set the scale of this element.
Definition
setScale(float widthScale, float heightScale)
Arguments
floatwidthScaleWidth scale factor
floatheightScaleHeight scale factor
Code
99function InputGlyphElement:setScale(widthScale, heightScale)
100 InputGlyphElement:superClass().setScale(self, widthScale, heightScale)
101
102 self.glyphOffsetX = self:scalePixelToScreenWidth(InputGlyphElement.GLYPH_OFFSET_X)
103 self.textOffsetX = self:scalePixelToScreenWidth(InputGlyphElement.TEXT_OFFSET_X)
104
105 self.iconSizeX, self.iconSizeY = self.baseWidth * widthScale, self.baseHeight * heightScale
106 self.plusIconSizeX, self.plusIconSizeY = self.iconSizeX * 0.5, self.iconSizeY * 0.5
107 self.orIconSizeX, self.orIconSizeY = self.iconSizeX * 0.5, self.iconSizeY * 0.5
108end

setUpperCase

Description
Set the glyph text to be displayed in all upper case or not. This resets the lower case setting if upper case is enabled.
Definition
setUpperCase()
Code
113function InputGlyphElement:setUpperCase(enableUpperCase)
114 self.upperCase = enableUpperCase
115 self.lowerCase = self.lowerCase and not enableUpperCase
116 self:updateDisplayText()
117end

updateDisplayText

Description
Update the display text from the set action text according to current casing settings.
Definition
updateDisplayText()
Code
284function InputGlyphElement:updateDisplayText()
285 if self.actionText ~= nil then
286 self.displayText = self.actionText
287 if self.upperCase then
288 self.displayText = utf8ToUpper(self.actionText)
289 elseif self.lowerCase then
290 self.displayText = utf8ToLower(self.actionText)
291 end
292 end
293end