LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

ContextActionDisplay

Description
Player context action display element. Displays information about the current interaction context. Includes action names and current input scheme button glyphs.
Parent
HUDDisplayElement
Functions

createActionIcons

Description
Create action context icons. Only one of these will be visible at any time.
Definition
createActionIcons()
Code
318function ContextActionDisplay:createActionIcons(hudAtlasPath, baseX, baseY)
319 local posX, posY = getNormalizedScreenValues(unpack(ContextActionDisplay.POSITION.CONTEXT_ICON))
320 local width, height = getNormalizedScreenValues(unpack(ContextActionDisplay.SIZE.CONTEXT_ICON))
321
322 local centerY = baseY + (self:getHeight() - height) * 0.5 + posY
323
324 for _, iconName in pairs(ContextActionDisplay.CONTEXT_ICON) do
325 local iconOverlay = Overlay.new(hudAtlasPath, baseX + posX, centerY, width, height)
326 local uvs = ContextActionDisplay.UV[iconName]
327 iconOverlay:setUVs(GuiUtils.getUVs(uvs))
328 iconOverlay:setColor(unpack(ContextActionDisplay.COLOR.CONTEXT_ICON))
329
330 local iconElement = HUDElement.new(iconOverlay)
331 iconElement:setVisible(false)
332
333 self.contextIconElements[iconName] = iconElement
334 self:addChild(iconElement)
335 end
336end

createBackground

Description
Create an empty background overlay as a base frame for this element.
Definition
createBackground()
Code
266function ContextActionDisplay.createBackground()
267 local width, height = getNormalizedScreenValues(unpack(ContextActionDisplay.SIZE.BACKGROUND))
268 local posX, posY = ContextActionDisplay.getBackgroundPosition(1, width)
269
270 local overlay = Overlay.new(nil, posX, posY, width, height) -- empty overlay, only used as a positioning frame
271
272 return overlay
273end

createComponents

Description
Create display components.
Definition
createComponents(string hudAtlasPath, table inputDisplayManager)
Arguments
stringhudAtlasPathPath to HUD atlas texture
tableinputDisplayManagerInputDisplayManager reference
Code
279function ContextActionDisplay:createComponents(hudAtlasPath, inputDisplayManager)
280 local baseX, baseY = self:getPosition()
281 self:createFrame(hudAtlasPath, baseX, baseY)
282 self:createInputGlyph(hudAtlasPath, baseX, baseY, inputDisplayManager)
283 self:createActionIcons(hudAtlasPath, baseX, baseY)
284
285 self:createFadeBackground(hudAtlasPath)
286
287
288 self:storeOriginalPosition()
289end

createFrame

Description
Create the context display frame.
Definition
createFrame()
Code
309function ContextActionDisplay:createFrame(hudAtlasPath, baseX, baseY)
310 -- local frame = HUDFrameElement.new(hudAtlasPath, baseX, baseY, self:getWidth(), self:getHeight())
311 -- frame:setColor(unpack(HUD.COLOR.FRAME_BACKGROUND))
312 -- self:addChild(frame)
313end

createInputGlyph

Description
Create the input glyph element.
Definition
createInputGlyph()
Code
293function ContextActionDisplay:createInputGlyph(hudAtlasPath, baseX, baseY, inputDisplayManager)
294 local width, height = getNormalizedScreenValues(unpack(ContextActionDisplay.SIZE.INPUT_ICON))
295 local offX, offY = getNormalizedScreenValues(unpack(ContextActionDisplay.POSITION.INPUT_ICON))
296 local element = InputGlyphElement.new(inputDisplayManager, width, height)
297
298 local posX, posY = baseX + offX, baseY + offY + (self:getHeight() - height) * 0.5
299
300 element:setPosition(posX, posY)
301 element:setKeyboardGlyphColor(ContextActionDisplay.COLOR.INPUT_ICON)
302
303 self.inputGlyphElement = element
304 self:addChild(element)
305end

draw

Description
Draw the context action display.
Definition
draw()
Code
165function ContextActionDisplay:draw()
166 if self.contextAction ~= "" and self.contextEventHelpElement ~= nil then
167 self.inputGlyphElement:setAction(self.contextAction) -- updates input mode and glyphs if necessary
168
169 ContextActionDisplay:superClass().draw(self)
170 local _, baseY = self:getPosition()
171
172 setTextColor(unpack(ContextActionDisplay.COLOR.ACTION_TEXT))
173 setTextBold(true)
174 setTextAlignment(RenderText.ALIGN_LEFT)
175
176 local height = self:getHeight()
177
178 local posX, posY = self.rightSideX, baseY + height * 0.5 + self.targetTextSize * 0.5 + self.actionTextOffsetY
179 renderText(posX, posY, self.actionTextSize, self.actionText)
180
181 posY = baseY + height * 0.5
182
183 setTextColor(unpack(ContextActionDisplay.COLOR.TARGET_TEXT))
184 setTextBold(false)
185
186 local width = self:getWidth()
187 local textWrapWidth = width - self.targetTextOffsetX - self.contextIconSizeX - self.inputGlyphElement:getWidth() * 2 - self.contextIconOffsetX
188
189 setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_MIDDLE)
190 setTextAlignment(RenderText.ALIGN_CENTER)
191 renderText(0.5, posY, self.targetTextSize, self.targetText)
192
193 setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_BASELINE)
194
195 if g_uiDebugEnabled then
196 local yPixel = 1 / g_screenHeight
197 setOverlayColor(GuiElement.debugOverlay, 0, 1, 1, 1)
198 renderOverlay(GuiElement.debugOverlay, posX, posY, textWrapWidth, yPixel)
199 end
200 end
201end

getBackgroundPosition

Description
Get the position of the background element, which provides this element's absolute position.
Definition
getBackgroundPosition(scale Current, float width)
Arguments
scaleCurrentUI scale
floatwidthScaled background width in pixels
Return Values
floatXposition in screen space
floatYposition in screen space
Code
255function ContextActionDisplay.getBackgroundPosition(scale, width)
256 local offX, offY = getNormalizedScreenValues(unpack(ContextActionDisplay.POSITION.BACKGROUND))
257 return 0.5 - width * 0.5 - offX * scale, g_safeFrameOffsetY - offY * scale
258end

new

Description
Create a new instance of ContextActionDisplay.
Definition
new(string hudAtlasPath)
Arguments
stringhudAtlasPathPath to the HUD texture atlas
Code
31function ContextActionDisplay.new(hudAtlasPath, inputDisplayManager)
32 local backgroundOverlay = ContextActionDisplay.createBackground()
33 local self = ContextActionDisplay:superClass().new(backgroundOverlay, nil, ContextActionDisplay_mt)
34
35 self.uiScale = 1.0
36 self.inputDisplayManager = inputDisplayManager
37
38 self.inputGlyphElement = nil
39
40 self.contextIconElements = {}
41 self.contextAction = ""
42 self.contextIconName = ""
43 self.targetText = ""
44 self.actionText = ""
45 self.contextPriority = -math.huge
46
47 self.contextIconElementRightX = 0
48 self.contextIconOffsetX, self.contextIconOffsetY = 0, 0
49 self.contextIconSizeX = 0
50 self.actionTextOffsetX, self.actionTextOffsetY = 0, 0
51 self.actionTextSize = 0
52 self.targetTextOffsetX, self.targetTextOffsetY = 0, 0
53 self.targetTextSize = 0
54 self.borderOffsetX = 0
55
56 self.displayTime = 0
57
58 self:createComponents(hudAtlasPath, inputDisplayManager)
59
60 return self
61end

resetContext

Description
Reset context state after drawing. The context must be set anew on each frame.
Definition
resetContext()
Code
151function ContextActionDisplay:resetContext()
152 self.contextAction = ""
153 self.contextIconName = ""
154 self.targetText = ""
155 self.actionText = ""
156 self.contextPriority = -math.huge
157end

setContext

Description
Sets the current action context. This must be called each frame when a given context is active. The highest priority context is displayed or the one which was set the latest if two or more contexts have the same priority.
Definition
setContext(string contextAction, string contextIconName, string targetText, int priority, string actionText)
Arguments
stringcontextActionInput action name of the context action
stringcontextIconNameName of the icon to display for the action context, use one of ContextActionDisplay.CONTEXT_ICON
stringtargetTextDisplay text which describes the context action target
intpriority[optional, default=0] Context priority, a higher number has higher priority.
stringactionText[optional] Context action description, if different from context action description
Code
72function ContextActionDisplay:setContext(contextAction, contextIconName, targetText, priority, actionText)
73 if priority == nil then
74 priority = 0
75 end
76
77 if priority >= self.contextPriority and self.contextIconElements[contextIconName] ~= nil then
78 self.contextAction = contextAction
79 self.contextIconName = contextIconName
80 self.targetText = targetText
81 self.contextPriority = priority
82
83 local eventHelpElement = self.inputDisplayManager:getEventHelpElementForAction(self.contextAction)
84 self.contextEventHelpElement = eventHelpElement
85
86 if eventHelpElement ~= nil then
87 self.inputGlyphElement:setAction(contextAction)
88 self.actionText = utf8ToUpper(actionText or eventHelpElement.textRight or eventHelpElement.textLeft)
89
90
91 -- Position directly left of the target text. We center this text so we use the position
92 -- to determine icon position
93 local targetTextWidth = getTextWidth(self.targetTextSize, self.targetText)
94 self.rightSideX = 0.5 - targetTextWidth * 0.5
95
96
97 local contextIconWidth = 0
98
99 local posX = self.rightSideX + self.contextIconOffsetX
100 for name, element in pairs(self.contextIconElements) do
101 element:setPosition(posX - element:getWidth(), nil) -- no change to Y position
102
103 if name == self.contextIconName then
104 contextIconWidth = element:getWidth()
105 end
106 end
107
108 posX = posX - self.inputGlyphElement:getWidth() + self.inputIconOffsetX - contextIconWidth
109 self.inputGlyphElement:setPosition(posX, nil)
110 end
111
112 if not self:getVisible() then
113 self:setVisible(true, true)
114 end
115 end
116
117 for name, element in pairs(self.contextIconElements) do
118 element:setVisible(name == self.contextIconName)
119 end
120
121 -- always refresh display time:
122 self.displayTime = ContextActionDisplay.MIN_DISPLAY_DURATION
123end

setScale

Description
Set the scale of this element.
Definition
setScale()
Code
213function ContextActionDisplay:setScale(uiScale)
214 ContextActionDisplay:superClass().setScale(self, uiScale, uiScale)
215
216 local currentVisibility = self:getVisible()
217 self:setVisible(true, false)
218
219 self.uiScale = uiScale
220 local posX, posY = ContextActionDisplay.getBackgroundPosition(uiScale, self:getWidth())
221 self:setPosition(posX, posY)
222
223 self:storeOriginalPosition()
224 self:setVisible(currentVisibility, false)
225
226 self:storeScaledValues()
227
228 -- Special case because this display needs to cover the whole width at all times
229 self.fadeBackgroundElement:setDimension(1)
230 self.fadeBackgroundElement:setPosition(0, 0)
231end

storeScaledValues

Description
Store scaled positioning, size and offset values.
Definition
storeScaledValues()
Code
235function ContextActionDisplay:storeScaledValues()
236 self.contextIconOffsetX, self.contextIconOffsetY = self:scalePixelToScreenVector(ContextActionDisplay.POSITION.CONTEXT_ICON)
237 self.contextIconSizeX = self:scalePixelToScreenWidth(ContextActionDisplay.SIZE.CONTEXT_ICON[1])
238 self.borderOffsetX = self:scalePixelToScreenWidth(ContextActionDisplay.OFFSET.X)
239
240 self.inputIconOffsetX, self.inputIconOffsetX = self:scalePixelToScreenVector(ContextActionDisplay.POSITION.INPUT_ICON)
241
242 self.actionTextOffsetX, self.actionTextOffsetY = self:scalePixelToScreenVector(ContextActionDisplay.POSITION.ACTION_TEXT)
243 self.actionTextSize = self:scalePixelToScreenHeight(ContextActionDisplay.TEXT_SIZE.ACTION_TEXT)
244
245 self.targetTextOffsetX, self.targetTextOffsetY = self:scalePixelToScreenVector(ContextActionDisplay.POSITION.TARGET_TEXT)
246 self.targetTextSize = self:scalePixelToScreenHeight(ContextActionDisplay.TEXT_SIZE.TARGET_TEXT)
247end

update

Description
Update the context action display state.
Definition
update()
Code
131function ContextActionDisplay:update(dt)
132 ContextActionDisplay:superClass().update(self, dt)
133
134 self.displayTime = self.displayTime - dt
135 local isVisible = self:getVisible()
136
137 if self.displayTime <= 0 and isVisible and self.animation:getFinished() then
138 self:setVisible(false, true)
139 end
140
141 if not self.animation:getFinished() then
142 self:storeScaledValues()
143 elseif self.contextAction ~= "" and not isVisible then
144 self:resetContext() -- reset context data when move-out animation has finished
145 end
146end