LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

GameInfoDisplayMobile

Description
Vehicle Steering Slider for Mobile Version
Parent
HUDDisplayElement
Functions

createBackground

Description
Create an empty background overlay as a base frame for this element.
Definition
createBackground()
Code
407function GameInfoDisplayMobile.createBackground()
408 local width, height = getNormalizedScreenValues(unpack(GameInfoDisplayMobile.SIZE.BACKGROUND))
409 local posX, posY = GameInfoDisplayMobile.getBackgroundPosition(1, width)
410
411 return Overlay.new(nil, posX, posY, width, height) -- empty overlay, only used as a positioning frame
412end

createBorders

Description
Definition
createBorders()
Code
81function GameInfoDisplayMobile:createBorders()
82 -- bottom
83 local border = self:createHUDElement({0, 0}, {GameInfoDisplayMobile.SIZE.BACKGROUND[1], 2}, HUDElement.UV.FILL, GameInfoDisplayMobile.COLOR.BORDER)
84 table.insert(self.elements, border)
85
86 -- left
87 border = self:createHUDElement({0, 2}, {2, GameInfoDisplayMobile.SIZE.BACKGROUND[2]}, HUDElement.UV.FILL, GameInfoDisplayMobile.COLOR.BORDER)
88 table.insert(self.elements, border)
89
90 -- right
91 border = self:createHUDElement({GameInfoDisplayMobile.SIZE.BACKGROUND[1] - 2, 2}, {2, GameInfoDisplayMobile.SIZE.BACKGROUND[2]}, HUDElement.UV.FILL, GameInfoDisplayMobile.COLOR.BORDER)
92 table.insert(self.elements, border)
93
94 border = self:createHUDElement({GameInfoDisplayMobile.POSITION.CENTER_BACKGROUND[1]+GameInfoDisplayMobile.SIZE.CENTER_BACKGROUND[1]+GameInfoDisplayMobile.POSITION.CENTER_BACKGROUND[1]/2, 2}, {2, GameInfoDisplayMobile.SIZE.BACKGROUND[2]}, HUDElement.UV.FILL, GameInfoDisplayMobile.COLOR.BORDER)
95 table.insert(self.elements, border)
96
97 border = self:createHUDElement({GameInfoDisplayMobile.POSITION.CENTER_BACKGROUND[1]-2, 2}, {2, GameInfoDisplayMobile.SIZE.BACKGROUND[2]}, HUDElement.UV.FILL, GameInfoDisplayMobile.COLOR.BORDER)
98 table.insert(self.elements, border)
99
100 border = self:createHUDElement({GameInfoDisplayMobile.POSITION.CENTER_BACKGROUND[1]+GameInfoDisplayMobile.SIZE.CENTER_BACKGROUND[1], 2}, {2, GameInfoDisplayMobile.SIZE.BACKGROUND[2]}, HUDElement.UV.FILL, GameInfoDisplayMobile.COLOR.BORDER)
101 table.insert(self.elements, border)
102end

createButtonElement

Description
Definition
createButtonElement()
Code
121function GameInfoDisplayMobile:createButtonElement(callback, position, size, iconSize, uvs, color, backgroundColor, iconColorPressed, inputAction)
122 local button = {}
123 button.callback = callback
124 button.display = callback
125
126 button.backgroundElement = self:createHUDElement(position, size, HUDElement.UV.FILL, backgroundColor)
127 button.iconColorPressed = iconColorPressed
128
129 local posX, posY = position[1] + size[1] * 0.5 - iconSize[1] * 0.5, position[2] + size[2] * 0.5 - iconSize[2] * 0.5
130 button.iconElement = self:createHUDElement({posX, posY}, iconSize, uvs, color)
131 button.iconColor = color
132
133 table.insert(self.elements, button.backgroundElement)
134 table.insert(self.elements, button.iconElement)
135
136 local pressButton = function(button)
137 button.iconElement:setColor(unpack(button.iconColorPressed))
138 end
139
140 local releaseButton = function(button)
141 button.iconElement:setColor(unpack(button.iconColor))
142 end
143
144 local buttonCb = function(target, x, y, isCancel)
145 if not isCancel then
146 callback(target, x, y)
147 end
148 end
149
150 self.hud:addTouchButton(button.backgroundElement.overlay, 0, 0, buttonCb, self, TouchHandler.TRIGGER_UP, {button})
151 self.hud:addTouchButton(button.backgroundElement.overlay, 0, 0, pressButton, button, TouchHandler.TRIGGER_DOWN)
152 self.hud:addTouchButton(button.backgroundElement.overlay, 0, 0, releaseButton, button, TouchHandler.TRIGGER_UP)
153
154 if inputAction ~= nil then
155 local glyphWidth, glyphHeight = getNormalizedScreenValues(unpack(GameInfoDisplayMobile.SIZE.INPUT_GLYPH))
156
157 local baseX, baseY = self:getPosition()
158 local sPosX, sPosY = getNormalizedScreenValues(unpack(position))
159 local sSizeX, sSizeY = getNormalizedScreenValues(unpack(size))
160
161 local glyphElement = InputGlyphElement.new(g_inputDisplayManager, glyphWidth, glyphHeight)
162
163 glyphElement:setAction(inputAction)
164 glyphElement:setButtonGlyphColor(GameInfoDisplayMobile.COLOR.INPUT_GLYPH)
165
166 local actualWidth = glyphElement:getGlyphWidth()
167 glyphElement:setPosition(baseX + sPosX + sSizeX - actualWidth, baseY + sPosY + sSizeY - glyphHeight)
168
169 table.insert(self.elements, glyphElement)
170 table.insert(self.inputGlyphs, glyphElement)
171 end
172end

createHUDElement

Description
Definition
createHUDElement()
Code
106function GameInfoDisplayMobile:createHUDElement(position, size, uvs, color)
107 local baseX, baseY = self:getPosition()
108 local posX, posY = getNormalizedScreenValues(unpack(position))
109 local sizeX, sizeY = getNormalizedScreenValues(unpack(size))
110 local overlay = Overlay.new(self.hudAtlasPath, baseX + posX, baseY + posY, sizeX, sizeY)
111 overlay:setUVs(GuiUtils.getUVs(uvs))
112 if color ~= nil then
113 overlay:setColor(unpack(color))
114 end
115
116 return HUDElement.new(overlay)
117end

createInfoElement

Description
Definition
createInfoElement()
Code
176function GameInfoDisplayMobile:createInfoElement(valueFunc, position, size, iconSize, iconOffset, iconPadding, uvs, color, textOffset, textSize, textBold)
177 local infoElement = {}
178 infoElement.position = position
179 infoElement.size = size
180
181 infoElement.icon = self:createHUDElement(position, iconSize, uvs, color)
182 infoElement.iconSize = iconSize
183 infoElement.iconOffset = iconOffset
184 infoElement.iconPadding = iconPadding
185
186 local _, textSizeY = getNormalizedScreenValues(0, textSize[2])
187 infoElement.textSize = textSizeY
188
189 local textPosX, textPosY = getNormalizedScreenValues(position[1] + size[1] - textOffset[1], position[2] + size[2] / 2 - textSize[2] / 2 + textOffset[2])
190 infoElement.textPos = {textPosX, textPosY}
191 infoElement.textOffset = textOffset
192 infoElement.textAlign = RenderText.ALIGN_RIGHT
193 infoElement.textBold = textBold
194 infoElement.valueFunc = valueFunc
195
196 local baseX, baseY = self:getPosition()
197 local iconPosX, iconPosY = getNormalizedScreenValues(infoElement.position[1] + infoElement.iconOffset[1] - infoElement.iconPadding[1], infoElement.position[2] + infoElement.iconOffset[2] + infoElement.size[2] / 2 - infoElement.iconSize[2] / 2)
198 infoElement.icon:setPosition(baseX + iconPosX, baseY + iconPosY)
199
200 table.insert(self.elements, infoElement.icon)
201 table.insert(self.infoElements, infoElement)
202end

createSeparators

Description
Definition
createSeparators()
Code
72function GameInfoDisplayMobile:createSeparators()
73 local posX = GameInfoDisplayMobile.POSITION.CENTER_BACKGROUND[1] + GameInfoDisplayMobile.POSITION.SEPARATOR_01[1]
74 local posY = GameInfoDisplayMobile.POSITION.CENTER_BACKGROUND[2] + GameInfoDisplayMobile.POSITION.SEPARATOR_01[2]
75 local separator = self:createHUDElement({posX, posY}, GameInfoDisplayMobile.SIZE.SEPARATOR, HUDElement.UV.FILL, GameInfoDisplayMobile.COLOR.SEPARATOR)
76 table.insert(self.elements, separator)
77end

draw

Description
Definition
draw()
Code
351function GameInfoDisplayMobile:draw()
352 GameInfoDisplayMobile:superClass().draw(self)
353
354 for _, infoElement in ipairs(self.infoElements) do
355 self:updateInfoElement(infoElement)
356 end
357end

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
394function GameInfoDisplayMobile.getBackgroundPosition(scale, width)
395 local offX, offY = getNormalizedScreenValues(unpack(GameInfoDisplayMobile.POSITION.BACKGROUND))
396 local sizeX, _ = getNormalizedScreenValues(unpack(GameInfoDisplayMobile.SIZE.BACKGROUND))
397
398 return 0.5 - sizeX / 2 - offX, 1 - g_safeFrameOffsetY - offY * scale
399end

getFuelValue

Description
Definition
getFuelValue()
Code
233function GameInfoDisplayMobile:getFuelValue()
234 if self.vehicle ~= nil and self.vehicle.getConsumerFillUnitIndex ~= nil then
235 local fillUnitIndex = self.vehicle:getConsumerFillUnitIndex(FillType.DIESEL)
236 if fillUnitIndex ~= nil then
237 local fillLevelPct = self.vehicle:getFillUnitFillLevelPercentage(fillUnitIndex)
238 return string.format("%d%%", fillLevelPct * 100)
239 end
240-- else
241-- if self.isRideable then
242-- return string.format("%d%%", math.min(self.vehicle:getHorseRidingScale(), 1) * 100)
243-- end
244 end
245
246 return "-"
247end

getMoneyValue

Description
Definition
getMoneyValue()
Code
221function GameInfoDisplayMobile:getMoneyValue()
222 if g_currentMission.player ~= nil then
223 local farm = g_farmManager:getFarmById(g_currentMission.player.farmId)
224
225 return g_i18n:formatMoney(farm.money, 0, false, true)
226 end
227
228 return "0"
229end

getTimeValue

Description
Definition
getTimeValue()
Code
251function GameInfoDisplayMobile:getTimeValue()
252 local currentTime = self.environment.dayTime / (1000 * 60 * 60)
253 local timeHours = math.floor(currentTime)
254 local timeMinutes = math.floor((currentTime - timeHours) * 60)
255
256 return string.format("%02d:%02d", timeHours, timeMinutes)
257end

new

Description
Creates a new GameInfoDisplayMobile instance.
Definition
new(string hudAtlasPath)
Arguments
stringhudAtlasPathPath to the HUD texture atlas.
Code
18function GameInfoDisplayMobile.new(hud, hudAtlasPath, moneyUnit, l10n)
19 local backgroundOverlay = GameInfoDisplayMobile.createBackground()
20 local self = GameInfoDisplayMobile:superClass().new(backgroundOverlay, nil, GameInfoDisplayMobile_mt)
21
22 self.hud = hud
23 self.uiScale = 1.0
24 self.hudAtlasPath = hudAtlasPath
25 self.moneyUnit = moneyUnit
26 self.l10n = l10n
27
28 self.vehicle = nil
29 self.isRideable = false
30
31 self.elements = {}
32 self.infoElements = {}
33 self.inputGlyphs = {}
34
35 self.backgroundHudElement = self:createHUDElement(GameInfoDisplayMobile.POSITION.CENTER_BACKGROUND, GameInfoDisplayMobile.SIZE.CENTER_BACKGROUND, GameInfoDisplayMobile.UV.BACKGROUND)
36 table.insert(self.elements, self.backgroundHudElement)
37
38 self:createSeparators()
39 self:createBorders()
40
41 self:createButtonElement(self.onOpenShop, GameInfoDisplayMobile.POSITION.SHOP_ICON, GameInfoDisplayMobile.SIZE.SHOP_ICON, GameInfoDisplayMobile.SIZE.BUTTON_ICON, GameInfoDisplayMobile.UV.SHOP, GameInfoDisplayMobile.COLOR.BUTTON, GameInfoDisplayMobile.COLOR.BUTTON_BACKGROUND, GameInfoDisplayMobile.COLOR.BUTTON_SELECTED, InputAction.TOGGLE_STORE)
42 self:createButtonElement(self.onOpenMap, GameInfoDisplayMobile.POSITION.MAP_ICON, GameInfoDisplayMobile.SIZE.MAP_ICON, GameInfoDisplayMobile.SIZE.BUTTON_ICON, GameInfoDisplayMobile.UV.MAP, GameInfoDisplayMobile.COLOR.BUTTON, GameInfoDisplayMobile.COLOR.BUTTON_BACKGROUND, GameInfoDisplayMobile.COLOR.BUTTON_SELECTED, InputAction.TOGGLE_MAP)
43 self:createButtonElement(self.onOpenMenu, GameInfoDisplayMobile.POSITION.MENU_ICON, GameInfoDisplayMobile.SIZE.MENU_ICON, GameInfoDisplayMobile.SIZE.BUTTON_ICON, GameInfoDisplayMobile.UV.MENU, GameInfoDisplayMobile.COLOR.BUTTON, GameInfoDisplayMobile.COLOR.BUTTON_BACKGROUND, GameInfoDisplayMobile.COLOR.BUTTON_SELECTED, InputAction.MENU)
44
45 self:createInfoElement(self.getMoneyValue, GameInfoDisplayMobile.POSITION.MONEY_AREA, GameInfoDisplayMobile.SIZE.MONEY_AREA, GameInfoDisplayMobile.SIZE.MONEY_ICON, GameInfoDisplayMobile.SIZE.ICON_OFFSET_MONEY, GameInfoDisplayMobile.SIZE.ICON_PADDING, GameInfoDisplayMobile.UV.SHOP, GameInfoDisplayMobile.COLOR.BUTTON, GameInfoDisplayMobile.SIZE.TEXT_OFFSET_MONEY, GameInfoDisplayMobile.SIZE.TEXT_SIZE, true)
46 self:createInfoElement(self.getFuelValue, GameInfoDisplayMobile.POSITION.FUEL_AREA, GameInfoDisplayMobile.SIZE.FUEL_AREA, GameInfoDisplayMobile.SIZE.FUEL_ICON, GameInfoDisplayMobile.SIZE.ICON_OFFSET_FUEL, GameInfoDisplayMobile.SIZE.ICON_PADDING, GameInfoDisplayMobile.UV.FUEL, GameInfoDisplayMobile.COLOR.BUTTON, GameInfoDisplayMobile.SIZE.TEXT_OFFSET_FUEL, GameInfoDisplayMobile.SIZE.TEXT_SIZE, true)
47
48 for _, element in ipairs(self.elements) do
49 self:addChild(element)
50 end
51
52 return self
53end

onOpenMap

Description
Definition
onOpenMap()
Code
267function GameInfoDisplayMobile:onOpenMap()
268 g_currentMission:onToggleMap()
269end

onOpenMenu

Description
Definition
onOpenMenu()
Code
273function GameInfoDisplayMobile:onOpenMenu()
274 g_currentMission:onToggleMenu()
275end

onOpenShop

Description
Definition
onOpenShop()
Code
261function GameInfoDisplayMobile:onOpenShop()
262 g_currentMission:onToggleStore()
263end

setEnvironment

Description
Set the environment reference to use for weather information display.
Definition
setEnvironment(table environment)
Arguments
tableenvironmentEnvironment reference, do not change
Code
306function GameInfoDisplayMobile:setEnvironment(environment)
307 self.environment = environment
308end

setMissionInfo

Description
Set the mission information reference for base information display.
Definition
setMissionInfo(table missionInfo)
Arguments
tablemissionInfoMissionInfo reference, do not change
Code
299function GameInfoDisplayMobile:setMissionInfo(missionInfo)
300 self.missionInfo = missionInfo
301end

setMissionStats

Description
Set the MissionStats reference for displaying information.
Definition
setMissionStats(table missionStats)
Arguments
tablemissionStatsMissionStats reference, do not change
Code
292function GameInfoDisplayMobile:setMissionStats(missionStats)
293 self.missionStats = missionStats
294end

setMoneyUnit

Description
Set the money unit for displaying the account balance.
Definition
setMoneyUnit(int moneyUnit)
Arguments
intmoneyUnitMoney unit ID, any of [GS_MONEY_EURO | GS_MONEY_POUND | GS_MONEY_DOLLAR]. Invalid values are substituted by GS_MONEY_DOLLAR.
Code
280function GameInfoDisplayMobile:setMoneyUnit(moneyUnit)
281 if moneyUnit ~= GS_MONEY_EURO and moneyUnit ~= GS_MONEY_POUND and moneyUnit ~= GS_MONEY_DOLLAR then
282 moneyUnit = GS_MONEY_DOLLAR
283 end
284
285 self.moneyUnit = moneyUnit
286 self.infoElements[1].icon:setUVs(GuiUtils.getUVs(GameInfoDisplayMobile.UV.MONEY_ICON[moneyUnit]))
287end

setMoneyVisible

Description
Set visibility of the money display.
Definition
setMoneyVisible()
Code
312function GameInfoDisplayMobile:setMoneyVisible(isVisible)
313end

setScale

Description
Set this element's scale.
Definition
setScale()
Code
374function GameInfoDisplayMobile:setScale(uiScale)
375 GameInfoDisplayMobile:superClass().setScale(self, uiScale, uiScale)
376
377 local currentVisibility = self:getVisible()
378 self:setVisible(true, false)
379
380 self.uiScale = uiScale
381 local posX, posY = GameInfoDisplayMobile.getBackgroundPosition(uiScale, self:getWidth())
382 self:setPosition(posX, posY)
383
384 self:storeOriginalPosition()
385 self:setVisible(currentVisibility, false)
386end

setTemperatureVisible

Description
Set visibility of temperature display.
Definition
setTemperatureVisible()
Code
322function GameInfoDisplayMobile:setTemperatureVisible(isVisible)
323end

setTimeVisible

Description
Set visibility of time display.
Definition
setTimeVisible()
Code
317function GameInfoDisplayMobile:setTimeVisible(isVisible)
318end

setTutorialProgress

Description
Set the current tutorial progress values.
Definition
setTutorialProgress(float progress)
Arguments
floatprogressProgress expressed as a number between 0 and 1
Code
341function GameInfoDisplayMobile:setTutorialProgress(progress)
342end

setTutorialVisible

Description
Set visibility of tutorial progress display.
Definition
setTutorialVisible()
Code
335function GameInfoDisplayMobile:setTutorialVisible(isVisible)
336end

setVehicle

Description
Definition
setVehicle()
Code
57function GameInfoDisplayMobile:setVehicle(vehicle)
58 self.vehicle = vehicle
59
60 if vehicle ~= nil then
61 self.isRideable = SpecializationUtil.hasSpecialization(Rideable, vehicle.specializations)
62
63 self.infoElements[2].icon:setUVs(GuiUtils.getUVs(self.isRideable and GameInfoDisplayMobile.UV.RIDING or GameInfoDisplayMobile.UV.FUEL))
64 else
65 self.isRideable = false
66 self.infoElements[2].icon:setUVs(GuiUtils.getUVs(GameInfoDisplayMobile.UV.FUEL))
67 end
68end

setWeatherVisible

Description
Set visibility of weather display.
Definition
setWeatherVisible()
Code
327function GameInfoDisplayMobile:setWeatherVisible(isVisible)
328end

updateInfoElement

Description
Definition
updateInfoElement()
Code
206function GameInfoDisplayMobile:updateInfoElement(infoElement)
207 if infoElement.icon:getVisible() then
208 local baseX, baseY = self:getPosition()
209
210 setTextColor(1, 1, 1, 1)
211 setTextBold(infoElement.textBold)
212 setTextAlignment(infoElement.textAlign)
213 renderText(baseX + infoElement.textPos[1], baseY + infoElement.textPos[2], infoElement.textSize, infoElement.valueFunc(self))
214 setTextAlignment(RenderText.ALIGN_LEFT)
215 setTextBold(false)
216 end
217end