LUADOC - Farming Simulator 19

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
69function InputGlyphElement:delete()
70 InputGlyphElement:superClass().delete(self)
71
72 self.keyboardOverlay:delete()
73 self:deleteOverlayCopies()
74end

deleteOverlayCopies

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

draw

Description
Draw the input glyph(s).
Definition
draw()
Code
324function InputGlyphElement:draw()
325 if #self.actionNames == 0 or not self.overlay.visible then
326 return
327 end
328
329 local posX, posY = self:getPosition()
330
331 if next(self.buttonOverlays) then
332 for _, actionName in ipairs(self.actionNames) do
333 posX = self:drawControllerButtons(self.buttonOverlays[actionName], posX, posY)
334 end
335 elseif next(self.keyNames) then
336 for _, actionName in ipairs(self.actionNames) do
337 posX = self:drawKeyboardKeys(self.keyNames[actionName], posX, posY)
338 end
339 end
340
341 if self.actionText ~= nil then
342 self:drawActionText(posX, posY)
343 end
344end

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
411function InputGlyphElement:drawActionText(posX, posY)
412 setTextAlignment(RenderText.ALIGN_LEFT)
413 setTextBold(self.bold)
414 setTextColor(unpack(self.color))
415
416 renderText(posX + self.textOffsetX, posY + self.actionTextSize * 0.5, self.actionTextSize, self.displayText)
417end

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
352function InputGlyphElement:drawControllerButtons(buttonOverlays, posX, posY)
353 local padding = 0
354 for i, overlay in ipairs(buttonOverlays) do
355 if i > 1 then
356 local separatorType = self.separators[i - 1]
357 local separatorOverlay = self.orOverlay
358 local separatorWidth = 0
359 local separatorHeight = 0
360 if separatorType == InputHelpElement.SEPARATOR.COMBO_INPUT then
361 separatorOverlay = self.plusOverlay
362 separatorWidth, separatorHeight = self.plusIconSizeX, self.plusIconSizeY
363 elseif separatorType == InputHelpElement.SEPARATOR.ANY_INPUT then
364 separatorWidth, separatorHeight = self.orIconSizeX, self.orIconSizeY
365 end
366
367 separatorOverlay:setColor(nil, nil, nil, self.buttonColor[4])
368 separatorOverlay:setPosition(posX, posY + separatorHeight)
369 separatorOverlay:setDimension(separatorWidth, separatorHeight)
370 separatorOverlay:render()
371
372 separatorOverlay:setColor(nil, nil, nil, 1) -- reset alpha
373
374 separatorOverlay:resetDimensions()
375 posX = posX + separatorWidth + self.glyphOffsetX
376 end
377
378 overlay:setPosition(posX, posY + self.iconSizeY * 0.5) -- controller symbols are vertically aligned to middle
379 overlay:setDimension(self.iconSizeX, self.iconSizeY)
380 overlay:render()
381
382 overlay:resetDimensions()
383
384 padding = i < #buttonOverlays and self.glyphOffsetX or 0
385 posX = posX + self.iconSizeX + padding
386 end
387
388 return posX
389end

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
397function InputGlyphElement:drawKeyboardKeys(keyNames, posX, posY)
398 local padding = 0
399 for i, key in ipairs(keyNames) do
400 padding = i < #keyNames and self.glyphOffsetX or 0
401 posX = posX + self.keyboardOverlay:renderButton(key, posX, posY, self.iconSizeY) + padding
402 end
403
404 return posX
405end

getGlyphWidth

Description
Get the screen space width required by the glyphs used to display input in the current input context.
Definition
getGlyphWidth()
Code
287function InputGlyphElement:getGlyphWidth()
288 local width = 0
289 if next(self.buttonOverlays) then
290 for _, actionName in ipairs(self.actionNames) do
291 for i, overlay in ipairs(self.buttonOverlays[actionName]) do
292 if i > 1 then
293 local separatorType = self.separators[i - 1]
294 local separatorOverlay = self.orOverlay
295 local separatorWidth = 0
296 if separatorType == InputHelpElement.SEPARATOR.COMBO_INPUT then
297 separatorOverlay = self.plusOverlay
298 separatorWidth = self.plusIconSizeX
299 elseif separatorType == InputHelpElement.SEPARATOR.ANY_INPUT then
300 separatorWidth = self.orIconSizeX
301 end
302
303 width = width + separatorWidth + self.glyphOffsetX
304 end
305
306 local padding = i < #self.buttonOverlays[actionName] and self.glyphOffsetX or 0
307 width = width + self.iconSizeX + padding
308 end
309 end
310 elseif next(self.keyNames) then
311 for _, actionName in ipairs(self.actionNames) do
312 for i, key in ipairs(self.keyNames[actionName]) do
313 local padding = i < #self.keyNames[actionName] and self.glyphOffsetX or 0
314 width = width + self.keyboardOverlay:getButtonWidth(key, self.iconSizeY) + padding
315 end
316 end
317 end
318
319 return width
320end

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(InputGlyphElement_mt, backgroundOverlay, nil)
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.separators = {} -- {i=InputHelpElement.SEPARATOR}
44 self.keyNames = {} -- action name -> key names
45
46 self.color = {1, 1, 1, 1} -- RGBA array
47 self.buttonColor = {1, 1, 1, 1} -- RGBA array
48 self.overlayCopies = {} -- contains overlay copies which need to be deleted
49
50 self.baseWidth, self.baseHeight = baseWidth, baseHeight
51
52 self.glyphOffsetX = 0
53 self.textOffsetX = 0
54 self.iconSizeX, self.iconSizeY = baseWidth, baseHeight
55 self.plusIconSizeX, self.plusIconSizeY = baseWidth * 0.5, baseHeight * 0.5
56 self.orIconSizeX, self.orIconSizeY = baseWidth * 0.5, baseHeight * 0.5
57
58 self.alignX, self.alignY = 1, 1
59 self.alignmentOffsetX, self.alignmentOffsetY = 0, 0
60 self.lowerCase = false
61 self.upperCase = false
62 self.bold = false
63
64 return self
65end

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, integer comboMask)
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!
integercomboMask[optional] If non-nil, will remove gamepad bindings that have not got the matching comboMask, and bindings which are not gamepad or panel (PS4 only - other platforms no effect)
Code
165function InputGlyphElement:setAction(actionName, actionText, actionTextSize, noModifiers, copyOverlays, comboMask)
166 -- use this instance's action names array instead of creating a new one each time this is called
167 clearTable(self.actionNames)
168 table.insert(self.actionNames, actionName)
169 self:setActions(self.actionNames, actionText, actionTextSize, noModifiers, copyOverlays, comboMask)
170end

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, integer comboMask)
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!
integercomboMask[optional] If non-nil, will remove gamepad bindings that have not got the matching comboMask, and bindings which are not gamepad or panel (PS4 only - other platforms no effect)
Code
183function InputGlyphElement:setActions(actionNames, actionText, actionTextSize, noModifiers, copyOverlays, comboMask)
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 clearTable(self.buttonOverlays)
194 clearTable(self.keyNames)
195 self:deleteOverlayCopies()
196
197 local hasButtons = false
198 local hasKeys = false
199 local isDoubleAction = #actionNames == 2
200
201 for i, actionName in ipairs(actionNames) do
202 local actionName2 = nil
203 if isDoubleAction then
204 actionName2 = actionNames[i + 1]
205 end
206
207 local helpElement = self.inputDisplayManager:getControllerSymbolOverlays(actionName, actionName2, "", noModifiers, comboMask)
208 local buttonOverlays = helpElement.buttons
209 self.separators = helpElement.separators
210
211 if copyOverlays then
212 local originals = buttonOverlays
213 buttonOverlays = {}
214
215 for _, overlay in ipairs(originals) do
216 -- TODO: make overlay:clone() function
217 local overlayCopy = Overlay:new(overlay.filename, overlay.x, overlay.y, overlay.defaultWidth, overlay.defaultHeight)
218 overlayCopy:setUVs(overlay.uvs)
219 overlayCopy:setAlignment(overlay.alignmentVertical, overlay.alignmentHorizontal)
220
221 table.insert(self.overlayCopies, overlayCopy)
222 table.insert(buttonOverlays, overlayCopy)
223 end
224 end
225
226 if #buttonOverlays > 0 then
227 self.buttonOverlays[actionName] = {}
228 for _, overlay in ipairs(buttonOverlays) do
229 table.insert(self.buttonOverlays[actionName], overlay)
230 end
231
232 hasButtons = true
233 end
234
235 if #helpElement.keys > 0 then
236 self.keyNames[actionName] = {}
237 for _, key in ipairs(helpElement.keys) do
238 table.insert(self.keyNames[actionName], key)
239 end
240
241 hasKeys = true
242 end
243
244 if isDoubleAction then
245 table.remove(self.actionNames, 2)
246 break -- should have resolved everything now
247 end
248 end
249
250 if hasButtons then
251 for _, buttonOverlays in pairs(self.buttonOverlays) do
252 for i, overlay in ipairs(buttonOverlays) do
253 if i > 1 then -- TODO: use separator types to get width
254 width = width + self.plusIconSizeX + self.glyphOffsetX
255 end
256
257 width = width + self.iconSizeX + (i < #buttonOverlays and self.glyphOffsetX or 0)
258 end
259 end
260 elseif hasKeys then
261 for _, keyNames in pairs(self.keyNames) do
262 for _, key in ipairs(keyNames) do
263 width = width + self.keyboardOverlay:getButtonWidth(key, height)
264 end
265 end
266 end
267
268 -- adjust this element's size so other elements can correctly offset from this
269 self:setDimension(width, height)
270end

setBold

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

setButtonGlyphColor

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

setKeyboardGlyphColor

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

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
120function InputGlyphElement:setLowerCase(enableLowerCase)
121 self.lowerCase = enableLowerCase
122 self.upperCase = self.upperCase and not enableLowerCase
123 self:updateDisplayText()
124end

setScale

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

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
111function InputGlyphElement:setUpperCase(enableUpperCase)
112 self.upperCase = enableUpperCase
113 self.lowerCase = self.lowerCase and not enableUpperCase
114 self:updateDisplayText()
115end

updateDisplayText

Description
Update the display text from the set action text according to current casing settings.
Definition
updateDisplayText()
Code
274function InputGlyphElement:updateDisplayText()
275 if self.actionText ~= nil then
276 self.displayText = self.actionText
277 if self.upperCase then
278 self.displayText = utf8ToUpper(self.actionText)
279 elseif self.lowerCase then
280 self.displayText = utf8ToLower(self.actionText)
281 end
282 end
283end