LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

ControlBarDisplay

Description
Control bar display for Mobile Version
Parent
HUDDisplayElement
Functions

addControlButton

Description
Definition
addControlButton()
Code
332function ControlBarDisplay:addControlButton(allowedFunc, callback, callbackTarget, triggerType, uvs, prio, inputAction)
333 local controlButton = {}
334
335 local pos, size, posOffsetX, _ = self:getScaledPosAndSize(ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET, ControlBarDisplay.SIZE.BUTTON_SIZE, 0.8)
336
337 local overlay = self:createOverlayElement(pos, size, uvs, ControlBarDisplay.COLOR.BUTTON)
338 local icon = HUDElement.new(overlay)
339 icon.overlayOffsetX = posOffsetX
340
341 local frame = self:createFrame(ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET, ControlBarDisplay.SIZE.BUTTON_SIZE, ControlBarDisplay.COLOR.BUTTON_BACKGROUND, false)
342
343 controlButton.elements = {}
344 table.insert(controlButton.elements, frame)
345 table.insert(controlButton.elements, icon)
346
347 local glyphElement, offsetX
348 if inputAction ~= nil then
349 glyphElement, offsetX = self:createInputGlyph(ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET, ControlBarDisplay.SIZE.BUTTON_SIZE, inputAction)
350 table.insert(controlButton.elements, glyphElement)
351 glyphElement.overlayOffsetXScreenSpace = offsetX
352
353 controlButton.glyphElement = glyphElement
354 end
355
356 local buttonCallback = function()
357 if allowedFunc ~= nil then
358 local allowed, warning = allowedFunc(callbackTarget)
359 if not allowed then
360 g_currentMission:showBlinkingWarning(warning, 2500)
361
362 return
363 end
364 end
365
366 callback(callbackTarget)
367 end
368
369 controlButton.frame = frame
370 controlButton.overlay = overlay
371 controlButton.buttons = {}
372 controlButton.buttons[1] = self.hud:addTouchButton(overlay, 0.2, 0.2, buttonCallback, callbackTarget, triggerType)
373 controlButton.buttons[2] = self.hud:addTouchButton(overlay, 0.2, 0.2, self.pressButtonCallback, icon, TouchHandler.TRIGGER_DOWN)
374 controlButton.buttons[3] = self.hud:addTouchButton(overlay, 0.2, 0.2, self.releaseButtonCallback, icon, TouchHandler.TRIGGER_UP)
375 controlButton.prio = prio
376 controlButton.visible = true
377
378 for _, element in ipairs(controlButton.elements) do
379 table.insert(self.hudElements, element)
380 end
381
382 table.insert(self.controlButtons, controlButton)
383
384 self:updateControlButtons()
385 self:updateHudElements()
386
387 return controlButton
388end

addFillLevelDisplay

Description
Definition
addFillLevelDisplay()
Code
435function ControlBarDisplay:addFillLevelDisplay(vehicle, fillUnitIndex)
436 local fillLevelDisplay = {}
437
438 -- fill level bar
439 local posX, posY = unpack(ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET)
440 local sizeX, sizeY = unpack(ControlBarDisplay.SIZE.BUTTON_SIZE)
441
442 local xOffset = (ControlBarDisplay.SIZE.BUTTON_SIZE[1] - ControlBarDisplay.SIZE.FILL_LEVEL_BAR[1]) / 2 + ControlBarDisplay.POSITION.FILL_LEVEL_BAR[1] + 1
443 local position = {posX + xOffset, posY + ControlBarDisplay.POSITION.FILL_LEVEL_BAR[2]}
444 local fillLevelBarBackgroundOverlay = self:createOverlayElement(position, ControlBarDisplay.SIZE.FILL_LEVEL_BAR, ControlBarDisplay.UV.FILL_LEVEL_BAR, ControlBarDisplay.COLOR.FILL_LEVEL_BAR_BACKGROUND)
445 local fillLevelBarBackground = HUDElement.new(fillLevelBarBackgroundOverlay)
446 fillLevelBarBackground.overlayOffsetX = xOffset
447 fillLevelBarBackground.currentUVs = GuiUtils.getUVs(ControlBarDisplay.UV.FILL_LEVEL_BAR)
448 fillLevelBarBackground.defaultPosOffset = {getNormalizedScreenValues(xOffset, ControlBarDisplay.POSITION.FILL_LEVEL_BAR[2])}
449 fillLevelBarBackground.defaultPos = {getNormalizedScreenValues(posX, posY)}
450 fillLevelBarBackground.defaultSize = {getNormalizedScreenValues(unpack(ControlBarDisplay.SIZE.FILL_LEVEL_BAR))}
451
452 local fillLevelBarOverlay = self:createOverlayElement(position, ControlBarDisplay.SIZE.FILL_LEVEL_BAR, ControlBarDisplay.UV.FILL_LEVEL_BAR, ControlBarDisplay.COLOR.FILL_LEVEL_BAR)
453 local fillLevelBar = HUDElement.new(fillLevelBarOverlay)
454 fillLevelBar.overlayOffsetX = xOffset
455 fillLevelDisplay.normUVs = GuiUtils.getUVs(ControlBarDisplay.UV.FILL_LEVEL_BAR)
456 fillLevelDisplay.currentUVs = GuiUtils.getUVs(ControlBarDisplay.UV.FILL_LEVEL_BAR)
457
458 -- icon
459 local barOffset = ControlBarDisplay.SIZE.FILL_LEVEL_BAR[2] + ControlBarDisplay.POSITION.FILL_LEVEL_BAR[2]
460 local offsetPos = {ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET[1] + barOffset, ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET[2] + barOffset}
461 local pos, size, posOffsetX, _ = self:getScaledPosAndSize(offsetPos, {ControlBarDisplay.SIZE.BUTTON_SIZE[1] - barOffset / 2, ControlBarDisplay.SIZE.BUTTON_SIZE[2] - barOffset - 2}, 0.7)
462 local iconOverlay = self:createOverlayElement(pos, size, HUDElement.UV.FILL, ControlBarDisplay.COLOR.BUTTON)
463 local icon = HUDElement.new(iconOverlay)
464 icon.overlayOffsetX = posOffsetX + (barOffset + 2) / 4
465
466 -- frame
467 local frame = self:createFrame(ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET, ControlBarDisplay.SIZE.BUTTON_SIZE, ControlBarDisplay.COLOR.BUTTON_BACKGROUND, false)
468
469 fillLevelDisplay.elements = {}
470 table.insert(fillLevelDisplay.elements, frame)
471 table.insert(fillLevelDisplay.elements, fillLevelBarBackground)
472 table.insert(fillLevelDisplay.elements, fillLevelBar)
473 table.insert(fillLevelDisplay.elements, icon)
474
475 fillLevelDisplay.isSowingMachine = false
476 if vehicle.getSowingMachineFillUnitIndex ~= nil then
477 if vehicle:getSowingMachineFillUnitIndex() == fillUnitIndex then
478 fillLevelDisplay.isSowingMachine = true
479 end
480 end
481
482 local buttonCallbackDown = function(display, x, y)
483 icon:setColor(unpack(ControlBarDisplay.COLOR.BUTTON_ACTIVE))
484 end
485 local buttonCallbackUp = function(display, x, y)
486 icon:setColor(unpack(ControlBarDisplay.COLOR.BUTTON))
487 end
488 local inputAction
489
490 if fillLevelDisplay.isSowingMachine then
491 buttonCallbackUp = function(display, x, y)
492 if not display.vehicle:getIsAIActive() then
493 display.vehicle:changeSeedIndex()
494 end
495 icon:setColor(unpack(ControlBarDisplay.COLOR.BUTTON))
496 end
497 inputAction = InputAction.TOGGLE_SEEDS
498 end
499
500 if inputAction ~= nil then
501 local offsetX
502 fillLevelDisplay.glyphElement, offsetX = self:createInputGlyph({0,0}, ControlBarDisplay.SIZE.BUTTON_SIZE, inputAction)
503 table.insert(fillLevelDisplay.elements, fillLevelDisplay.glyphElement)
504 fillLevelDisplay.glyphElement.overlayOffsetXScreenSpace = offsetX
505 end
506
507 fillLevelDisplay.fillLevelBar = fillLevelBarOverlay
508 fillLevelDisplay.fillLevelBarBackground = fillLevelBarBackground
509 fillLevelDisplay.frame = frame
510 fillLevelDisplay.icon = icon
511 fillLevelDisplay.buttons = {}
512 fillLevelDisplay.buttons[1] = self.hud:addTouchButton(frame.overlay, 0, 0, buttonCallbackDown, fillLevelDisplay, TouchHandler.TRIGGER_DOWN)
513 fillLevelDisplay.buttons[2] = self.hud:addTouchButton(frame.overlay, 0, 0, buttonCallbackUp, fillLevelDisplay, TouchHandler.TRIGGER_UP)
514 fillLevelDisplay.prio = fillLevelDisplay.isSowingMachine and 1 or 2
515 fillLevelDisplay.vehicle = vehicle
516 fillLevelDisplay.fillUnitIndex = fillUnitIndex
517 fillLevelDisplay.visibleFunc = function(display)
518 -- always display sowing machine fill units to set seed type for ai
519 if fillLevelDisplay.isSowingMachine then
520 return true
521 end
522
523 if display.vehicle:getFillUnitExists(display.fillUnitIndex) then
524 return display.vehicle:getFillUnitFillLevelPercentage(display.fillUnitIndex) > 0
525 end
526
527 return false
528 end
529 fillLevelDisplay.visible = true
530 fillLevelDisplay.lastFillLevel = 0
531 fillLevelDisplay.lastFillLevelPct = 0
532
533 for _, element in ipairs(fillLevelDisplay.elements) do
534 table.insert(self.hudElements, element)
535 end
536
537 table.insert(self.controlButtons, fillLevelDisplay)
538 table.insert(self.fillTypeDisplays, fillLevelDisplay)
539
540 self:updateControlButtons()
541 self:updateHudElements()
542
543 return fillLevelDisplay
544end

addFillLevelDisplaysFromVehicles

Description
Definition
addFillLevelDisplaysFromVehicles()
Code
417function ControlBarDisplay:addFillLevelDisplaysFromVehicles(vehicles)
418 for i=#self.fillTypeDisplays, 1, -1 do
419 self:removeFillLevelDisplay(self.fillTypeDisplays[i])
420 end
421
422 for _, subVehicle in ipairs(vehicles) do
423 if subVehicle.getFillUnits ~= nil then
424 for i, fillUnit in ipairs(subVehicle:getFillUnits()) do
425 if fillUnit.showOnHud then
426 self:addFillLevelDisplay(subVehicle, i)
427 end
428 end
429 end
430 end
431end

createBackground

Description
Create an empty background overlay as a base frame for this element.
Definition
createBackground()
Code
1029function ControlBarDisplay.createBackground()
1030 local width, height = getNormalizedScreenValues(unpack(ControlBarDisplay.SIZE.BACKGROUND))
1031 local posX, posY = ControlBarDisplay.getBackgroundPosition(1, width)
1032
1033 return Overlay.new(nil, posX, posY, width, height) -- empty overlay, only used as a positioning frame
1034end

createComponents

Description
Set the currently controlled vehicle which provides display data.
Definition
createComponents(table vehicle)
Arguments
tablevehicleCurrently controlled vehicle
Code
210function ControlBarDisplay:createComponents()
211 for _, button in pairs(self.buttons) do
212 self.hud:removeTouchButton(button)
213 end
214
215 self.buttons = {}
216 self.controlButtons = {}
217
218 for _, element in ipairs(self.hudElements) do
219 self:removeChild(element)
220 element:delete()
221 end
222 self.hudElements = {}
223 self.switchElements = {}
224
225 -- create vehicle switch buttons
226 local frame = self:createFrame(ControlBarDisplay.POSITION.BUTTON_OFFSET, ControlBarDisplay.SIZE.BUTTON_SIZE, ControlBarDisplay.COLOR.BUTTON_BACKGROUND, false)
227 table.insert(self.hudElements, frame)
228 table.insert(self.switchElements, frame)
229
230 local pos = {ControlBarDisplay.POSITION.BUTTON_OFFSET[1] - ControlBarDisplay.POSITION.SWITCH_ARROW_OFFSET[1] + (ControlBarDisplay.SIZE.BUTTON_SIZE[1] - ControlBarDisplay.SIZE.SWITCH_ARROW[1]) / 2, ControlBarDisplay.POSITION.BUTTON_OFFSET[2] + ControlBarDisplay.POSITION.SWITCH_ARROW_OFFSET[2] + (ControlBarDisplay.SIZE.BUTTON_SIZE[2] - ControlBarDisplay.SIZE.SWITCH_ARROW[2]) / 2}
231 local swLeftOverlay = self:createOverlayElement(pos, ControlBarDisplay.SIZE.SWITCH_ARROW, ControlBarDisplay.UV.VEHICLE_LEFT, ControlBarDisplay.COLOR.BUTTON)
232 local swLeftElement = HUDElement.new(swLeftOverlay)
233 table.insert(self.hudElements, swLeftElement)
234 table.insert(self.switchElements, swLeftElement)
235 local touchOffsetX = {0.1, 0.4}
236 table.insert(self.buttons, self.hud:addTouchButton(swLeftOverlay, touchOffsetX, 0.5, self.onSwitchLeft, self, TouchHandler.TRIGGER_UP))
237 table.insert(self.buttons, self.hud:addTouchButton(swLeftOverlay, touchOffsetX, 0.5, self.pressButtonCallback, swLeftOverlay, TouchHandler.TRIGGER_DOWN))
238 table.insert(self.buttons, self.hud:addTouchButton(swLeftOverlay, touchOffsetX, 0.5, self.releaseButtonCallback, swLeftOverlay, TouchHandler.TRIGGER_UP))
239
240 pos = {ControlBarDisplay.POSITION.BUTTON_OFFSET[1] * 2 + ControlBarDisplay.SIZE.BUTTON_SIZE[1] - 1, ControlBarDisplay.POSITION.BUTTON_OFFSET[2]}
241 frame = self:createFrame(pos, ControlBarDisplay.SIZE.BUTTON_SIZE, ControlBarDisplay.COLOR.BUTTON_BACKGROUND, false)
242 table.insert(self.hudElements, frame)
243 table.insert(self.switchElements, frame)
244
245 pos[1], pos[2] = pos[1] + ControlBarDisplay.POSITION.SWITCH_ARROW_OFFSET[1] + (ControlBarDisplay.SIZE.BUTTON_SIZE[1] - ControlBarDisplay.SIZE.SWITCH_ARROW[1]) / 2, pos[2] + ControlBarDisplay.POSITION.SWITCH_ARROW_OFFSET[2] + (ControlBarDisplay.SIZE.BUTTON_SIZE[2] - ControlBarDisplay.SIZE.SWITCH_ARROW[2]) / 2
246 local swRightOverlay = self:createOverlayElement(pos, ControlBarDisplay.SIZE.SWITCH_ARROW, ControlBarDisplay.UV.VEHICLE_RIGHT, ControlBarDisplay.COLOR.BUTTON)
247 local swRightElement = HUDElement.new(swRightOverlay)
248 table.insert(self.hudElements, swRightElement)
249 table.insert(self.switchElements, swRightElement)
250 touchOffsetX = {touchOffsetX[2], touchOffsetX[1]}
251 table.insert(self.buttons, self.hud:addTouchButton(swRightOverlay, touchOffsetX, 0.5, self.onSwitchRight, self, TouchHandler.TRIGGER_UP))
252 table.insert(self.buttons, self.hud:addTouchButton(swRightOverlay, touchOffsetX, 0.5, self.pressButtonCallback, swRightOverlay, TouchHandler.TRIGGER_DOWN))
253 table.insert(self.buttons, self.hud:addTouchButton(swRightOverlay, touchOffsetX, 0.5, self.releaseButtonCallback, swRightOverlay, TouchHandler.TRIGGER_UP))
254
255 -- switch vehicle icon
256 local posX = ControlBarDisplay.POSITION.BUTTON_OFFSET[1] * 2 + ControlBarDisplay.SIZE.BUTTON_SIZE[1] * 0.5 + (ControlBarDisplay.SIZE.BUTTON_SIZE[1] - ControlBarDisplay.SIZE.SWITCH_ICON[1]) / 2
257 local posY = ControlBarDisplay.POSITION.BUTTON_OFFSET[2] + (ControlBarDisplay.SIZE.BUTTON_SIZE[2] - ControlBarDisplay.SIZE.SWITCH_ICON[2]) / 2
258
259 local swIcBackgroundOverlay = self:createOverlayElement({posX+ControlBarDisplay.POSITION.SWITCH_ICON_OFFSET[1] + (ControlBarDisplay.SIZE.SWITCH_ICON[1] - ControlBarDisplay.SIZE.SWITCH_ICON_BACKGROUND[1]) / 2, posY+ControlBarDisplay.POSITION.SWITCH_ICON_OFFSET[2] + (ControlBarDisplay.SIZE.SWITCH_ICON[2] - ControlBarDisplay.SIZE.SWITCH_ICON_BACKGROUND[2]) / 2}, ControlBarDisplay.SIZE.SWITCH_ICON_BACKGROUND, HUDElement.UV.FILL, ControlBarDisplay.COLOR.BUTTON_BACKGROUND)
260 local swIcBackgroundElement = HUDElement.new(swIcBackgroundOverlay)
261 table.insert(self.hudElements, swIcBackgroundElement)
262 table.insert(self.switchElements, swIcBackgroundElement)
263
264 local swIcOverlay = self:createOverlayElement({posX+ControlBarDisplay.POSITION.SWITCH_ICON_OFFSET[1], posY+ControlBarDisplay.POSITION.SWITCH_ICON_OFFSET[2]}, ControlBarDisplay.SIZE.SWITCH_ICON, ControlBarDisplay.UV.SWITCH_ICON, ControlBarDisplay.COLOR.SWITCH_ICON)
265 local swIcElement = HUDElement.new(swIcOverlay)
266 table.insert(self.hudElements, swIcElement)
267 table.insert(self.switchElements, swIcElement)
268
269 self.gamepadOffset = {getNormalizedScreenValues(unpack(ControlBarDisplay.POSITION.GAMEPAD_OFFSET))}
270 self.aiOffset = {getNormalizedScreenValues(unpack(ControlBarDisplay.POSITION.AI_OFFSET))}
271
272 self:updateHudElements()
273end

createFrame

Description
Definition
createFrame()
Code
290function ControlBarDisplay:createFrame(pos, size, backgroundColor, showBar, showSideLines)
291 local baseX, baseY = self:getPosition()
292 local posX, posY = getNormalizedScreenValues(unpack(pos))
293 local sizeX, sizeY = getNormalizedScreenValues(unpack(size))
294 local frame = HUDFrameElement.new(self.hudAtlasPath, baseX + posX, baseY + posY, sizeX, sizeY, nil, showBar, 2)
295 frame:setColor(unpack(backgroundColor))
296 frame:setFrameColor(unpack(ControlBarDisplay.COLOR.FRAME))
297
298 if showSideLines == false then
299 frame:setLeftLineVisible(false)
300 frame:setRightLineVisible(false)
301 end
302
303 return frame
304end

createOverlayArea

Description
Definition
createOverlayArea()
Code
308function ControlBarDisplay:createOverlayArea(pos, size)
309 local baseX, baseY = self:getPosition()
310 local posX, posY = getNormalizedScreenValues(unpack(pos))
311 local sizeX, sizeY = getNormalizedScreenValues(unpack(size))
312 local backgroundOverlay = Overlay.new(self.hudAtlasPath, baseX + posX, baseY + posY, sizeX, sizeY)
313 backgroundOverlay:setColor(0, 0, 0, 0)
314
315 return HUDElement.new(backgroundOverlay)
316end

createOverlayElement

Description
Definition
createOverlayElement()
Code
277function ControlBarDisplay:createOverlayElement(pos, size, uvs, color)
278 local baseX, baseY = self:getPosition()
279 local posX, posY = getNormalizedScreenValues(unpack(pos))
280 local sizeX, sizeY = getNormalizedScreenValues(unpack(size))
281 local overlay = Overlay.new(self.hudAtlasPath, baseX + posX, baseY + posY, sizeX, sizeY)
282 overlay:setUVs(GuiUtils.getUVs(uvs))
283 overlay:setColor(unpack(color))
284
285 return overlay
286end

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
1018function ControlBarDisplay.getBackgroundPosition(scale, width)
1019 local offX, offY = getNormalizedScreenValues(unpack(ControlBarDisplay.POSITION.BACKGROUND))
1020 return g_safeFrameOffsetX + offX * scale, g_safeFrameOffsetY - offY * scale
1021end

getScaledPosAndSize

Description
Definition
getScaledPosAndSize()
Code
320function ControlBarDisplay:getScaledPosAndSize(pos, size, scale)
321 pos = {unpack(pos)}
322 size = {unpack(size)}
323 local posOffsetX, posOffsetY = size[1] * (1 - scale) / 2, size[2] * (1 - scale) / 2
324 pos[1], pos[2] = pos[1] + posOffsetX, pos[2] + posOffsetY
325 size[1], size[2] = size[1] * scale, size[2] * scale
326
327 return pos, size, posOffsetX, posOffsetY
328end

new

Description
Creates a new ControlBarDisplay instance.
Definition
new(string hudAtlasPath)
Arguments
stringhudAtlasPathPath to the HUD texture atlas.
Code
21function ControlBarDisplay.new(hud, hudAtlasPath)
22 local backgroundOverlay = ControlBarDisplay.createBackground()
23 local self = ControlBarDisplay:superClass().new(backgroundOverlay, nil, ControlBarDisplay_mt)
24
25 self.hud = hud
26 self.uiScale = 1.0
27 self.hudAtlasPath = hudAtlasPath
28
29 self.vehicle = nil -- currently controlled vehicle
30 self.player = nil
31
32 self.hudElements = {}
33 self.buttons = {}
34 self.controlButtons = {}
35 self.inputGlyphs = {}
36 self.fillTypeDisplays = {}
37 self.numVehicles = 0
38 self.lastInputHelpMode = GS_INPUT_HELP_MODE_KEYBOARD
39 self.lastAIState = false
40 self.lastGyroscopeSteeringState = false
41
42 self.vehicleControls = {}
43
44 self.vehicleControls["attach"] = {
45 availableFunc="getShowDetachAttachedImplement",
46 actionFunc="detachAttachedImplement",
47 triggerType=TouchHandler.TRIGGER_UP,
48 fullTapNeeded=true,
49 controlButton=nil,
50 uvs=ControlBarDisplay.UV.DETACH,
51 prio=3,
52 inputAction = InputAction.ATTACH
53 }
54
55 self.vehicleControls["turnOn"] = {
56 allowedFunc="getAreControlledActionsAllowed",
57 availableFunc="getAreControlledActionsAvailable",
58 actionFunc="playControlledActions",
59 triggerType=TouchHandler.TRIGGER_UP,
60 fullTapNeeded=true,
61 directionFunc="getActionControllerDirection",
62 controlButton=nil,
63 uvs=ControlBarDisplay.UV.TURN_ON,
64 iconColor_pos = ControlBarDisplay.COLOR.BUTTON,
65 iconColor_neg = ControlBarDisplay.COLOR.BUTTON_ACTIVE,
66 prio=4,
67 inputAction = InputAction.VEHICLE_ACTION_CONTROL
68 }
69
70 self.vehicleControls["ai"] = {
71 availableFunc="getCanToggleAIVehicle",
72 actionFunc="toggleAIVehicle",
73 triggerType=TouchHandler.TRIGGER_UP,
74 fullTapNeeded=true,
75 directionFunc="getIsAIActive",
76 controlButton=nil,
77 uvs=ControlBarDisplay.UV.AI,
78 iconColor_pos = ControlBarDisplay.COLOR.BUTTON_ACTIVE,
79 iconColor_neg = ControlBarDisplay.COLOR.BUTTON,
80 prio=5,
81 inputAction = InputAction.TOGGLE_AI
82 }
83
84 self.vehicleControls["leave"] = {
85 availableFunc="getIsEntered",
86 actionFunc="doLeaveVehicle",
87 triggerType=TouchHandler.TRIGGER_UP,
88 fullTapNeeded=true,
89 controlButton=nil,
90 uvs=ControlBarDisplay.UV.LEAVE_VEHICLE,
91 iconColor_pos = ControlBarDisplay.COLOR.BUTTON_ACTIVE,
92 iconColor_neg = ControlBarDisplay.COLOR.BUTTON,
93 prio=6,
94 inputAction = InputAction.ENTER
95 }
96
97 self.playerControls = {}
98 self.playerControls["enter_vehicle"] = {
99 availableFunc="getCanEnterVehicle",
100 actionFunc="onInputEnter",
101 triggerType=TouchHandler.TRIGGER_UP,
102 fullTapNeeded=true,
103 controlButton=nil,
104 uvs=ControlBarDisplay.UV.ENTER_VEHICLE,
105 iconColor_pos = ControlBarDisplay.COLOR.BUTTON_ACTIVE,
106 iconColor_neg = ControlBarDisplay.COLOR.BUTTON,
107 prio=6,
108 inputAction = InputAction.ENTER
109 }
110
111 self.playerControls["enter_horse"] = {
112 availableFunc="getCanEnterRideable",
113 actionFunc="onInputEnter",
114 triggerType=TouchHandler.TRIGGER_UP,
115 fullTapNeeded=true,
116 controlButton=nil,
117 uvs=ControlBarDisplay.UV.ENTER_HORSE,
118 iconColor_pos = ControlBarDisplay.COLOR.BUTTON_ACTIVE,
119 iconColor_neg = ControlBarDisplay.COLOR.BUTTON,
120 prio=6,
121 inputAction = InputAction.ENTER
122 }
123
124 self.playerControls["ride"] = {
125 availableFunc="getIsRideStateAvailable",
126 actionFunc="activateRideState",
127 triggerType=TouchHandler.TRIGGER_UP,
128 fullTapNeeded=true,
129 controlButton=nil,
130 uvs=ControlBarDisplay.UV.ENTER_HORSE,
131 iconColor_pos = ControlBarDisplay.COLOR.BUTTON_ACTIVE,
132 iconColor_neg = ControlBarDisplay.COLOR.BUTTON,
133 prio=7,
134 inputAction = InputAction.ENTER
135 }
136
137 self.pressButtonCallback = function(backgroundElement)
138 if backgroundElement.overlayId ~= nil and entityExists(backgroundElement.overlayId) then
139 backgroundElement:setColor(unpack(ControlBarDisplay.COLOR.BUTTON_ACTIVE))
140 end
141 end
142
143 self.releaseButtonCallback = function(backgroundElement)
144 if backgroundElement.overlayId ~= nil and entityExists(backgroundElement.overlayId) then
145 backgroundElement:setColor(unpack(ControlBarDisplay.COLOR.BUTTON))
146 end
147 end
148
149 self:createComponents()
150
151 return self
152end

onAIVehicleStateChanged

Description
Definition
onAIVehicleStateChanged()
Code
803function ControlBarDisplay:onAIVehicleStateChanged(state, vehicle, force)
804 self.lastAIState = state
805
806 self:updatePositionState(force)
807end

onGyroscopeSteeringChanged

Description
Definition
onGyroscopeSteeringChanged()
Code
811function ControlBarDisplay:onGyroscopeSteeringChanged(state)
812 self.lastGyroscopeSteeringState = state
813
814 self:updatePositionState()
815end

onInputHelpModeChange

Description
Definition
onInputHelpModeChange()
Code
790function ControlBarDisplay:onInputHelpModeChange(inputHelpMode, force)
791 self.lastInputHelpMode = inputHelpMode
792
793 local showGlyphs = inputHelpMode == GS_INPUT_HELP_MODE_GAMEPAD
794 for _, glyph in ipairs(self.inputGlyphs) do
795 glyph:setVisible(showGlyphs)
796 end
797
798 self:updatePositionState(force)
799end

onSwitchLeft

Description
Definition
onSwitchLeft()
Code
757function ControlBarDisplay:onSwitchLeft(x, y, isCancel)
758 if not isCancel then
759 g_currentMission:toggleVehicle(-1)
760 end
761end

onSwitchRight

Description
Definition
onSwitchRight()
Code
765function ControlBarDisplay:onSwitchRight(x, y, isCancel)
766 if not isCancel then
767 g_currentMission:toggleVehicle(1)
768 end
769end

removeControlButton

Description
Definition
removeControlButton()
Code
710function ControlBarDisplay:removeControlButton(controlButton)
711 for _, element in ipairs(controlButton.elements) do
712 if element.overlay ~= nil and not element:isa(InputGlyphElement) then
713 element.overlay:delete()
714 end
715
716 self:removeChild(element)
717 end
718
719 for _, button in pairs(controlButton.buttons) do
720 self.hud:removeTouchButton(button)
721 end
722 for _, element in ipairs(controlButton.elements) do
723 table.removeElement(self.hudElements, element)
724 end
725 table.removeElement(self.controlButtons, controlButton)
726
727 if controlButton.glyphElement ~= nil then
728 table.removeElement(self.inputGlyphs, controlButton.glyphElement)
729 end
730
731 self:updateControlButtons()
732end

removeControls

Description
Definition
removeControls()
Code
156function ControlBarDisplay:removeControls()
157 if self.vehicle == nil then
158 for i=#self.fillTypeDisplays, 1, -1 do
159 self:removeFillLevelDisplay(self.fillTypeDisplays[i])
160 end
161 end
162
163 for _, vehicleControl in pairs(self.vehicleControls) do
164 if vehicleControl.controlButton ~= nil then
165 self:removeControlButton(vehicleControl.controlButton)
166 end
167 vehicleControl.controlButton = nil
168 end
169
170 for _, playerControl in pairs(self.playerControls) do
171 if playerControl.controlButton ~= nil then
172 self:removeControlButton(playerControl.controlButton)
173 end
174 playerControl.controlButton = nil
175 end
176end

removeFillLevelDisplay

Description
Definition
removeFillLevelDisplay()
Code
736function ControlBarDisplay:removeFillLevelDisplay(fillLevelDisplay)
737 table.removeElement(self.fillTypeDisplays, fillLevelDisplay)
738 self:removeControlButton(fillLevelDisplay)
739
740 if fillLevelDisplay.glyphElement ~= nil then
741 table.removeElement(self.inputGlyphs, fillLevelDisplay.glyphElement)
742 end
743end

setButtonVisibility

Description
Definition
setButtonVisibility()
Code
594function ControlBarDisplay:setButtonVisibility(controlButton, visible)
595 for _, element in ipairs(controlButton.elements) do
596 element:setVisible(visible)
597 end
598
599 if controlButton.buttons ~= nil then
600 for _, button in pairs(controlButton.buttons) do
601 if visible then
602 self.hud:showTouchButton(button)
603 else
604 self.hud:hideTouchButton(button)
605 end
606 end
607 end
608
609 if controlButton.glyphElement ~= nil and visible then
610 if self.lastInputHelpMode ~= GS_INPUT_HELP_MODE_GAMEPAD then
611 controlButton.glyphElement:setVisible(false)
612 end
613 end
614
615 controlButton.visible = visible
616end

setControlButtonDirection

Description
Definition
setControlButtonDirection()
Code
677function ControlBarDisplay:setControlButtonDirection(controlButton, vehicleControl, direction)
678 if vehicleControl.uvs_pos ~= nil and vehicleControl.uvs_neg ~= nil then
679 local uvs = direction == 1 and vehicleControl.uvs_pos or vehicleControl.uvs_neg
680 controlButton.overlay:setUVs(GuiUtils.getUVs(uvs))
681 end
682
683 if vehicleControl.iconColor_pos ~= nil and vehicleControl.iconColor_neg ~= nil then
684 local color = direction == 1 and vehicleControl.iconColor_pos or vehicleControl.iconColor_neg
685 controlButton.overlay:setColor(unpack(color))
686 end
687
688 if vehicleControl.bottomBarSize_pos ~= nil and vehicleControl.bottomBarSize_neg ~= nil then
689 local size = direction == 1 and vehicleControl.bottomBarSize_pos or vehicleControl.bottomBarSize_neg
690 local _, barHeight = getNormalizedScreenValues(0, size[2])
691 controlButton.frame:setBottomBarHeight(barHeight)
692 end
693
694 if vehicleControl.bottomBarColor_pos ~= nil and vehicleControl.bottomBarColor_neg ~= nil then
695 local color = direction == 1 and vehicleControl.bottomBarColor_pos or vehicleControl.bottomBarColor_neg
696 controlButton.frame:setBottomBarColor(unpack(color))
697 end
698end

setControlButtonIcon

Description
Definition
setControlButtonIcon()
Code
702function ControlBarDisplay:setControlButtonIcon(controlButton, vehicleControl, iconFilename)
703 if controlButton.overlay ~= nil then
704 controlButton.overlay:setImage(iconFilename)
705 end
706end

setPlayer

Description
Set the reference to the current player.
Definition
setPlayer()
Code
199function ControlBarDisplay:setPlayer(player)
200 self.player = player
201
202 self:removeControls()
203
204 self:updatePositionState()
205end

setPositionState

Description
Definition
setPositionState()
Code
819function ControlBarDisplay:setPositionState(state, force)
820 if state ~= self.lastPositionState then
821 local startX, startY = self:getPosition()
822 local targetX, targetY = self.origX, self.origY
823 local startAlpha, endAlpha = 1, 1
824 local speed = ControlBarDisplay.MOVE_ANIMATION_DURATION / 5
825
826 if state == ControlBarDisplay.STATE_TOUCH or state == ControlBarDisplay.STATE_AI_TOUCH then
827 if self.lastPositionState == ControlBarDisplay.STATE_CONTROLLER then
828 startAlpha, endAlpha = 0, 1
829 end
830 elseif state == ControlBarDisplay.STATE_CONTROLLER then
831 targetX, targetY = unpack(self.gamepadOffset)
832 targetX, targetY = self.origX + targetX, self.origY + targetY
833 startAlpha, endAlpha = 1, 0
834 speed = ControlBarDisplay.MOVE_ANIMATION_DURATION
835 end
836
837 if force then
838 speed = 0.01
839 end
840
841 local sequence = TweenSequence.new(self)
842 sequence:insertTween(MultiValueTween.new(self.setSwitchIconsAlpha, {startAlpha}, {endAlpha}, speed / 2), 0)
843 sequence:insertTween(MultiValueTween.new(self.setPosition, {startX, startY}, {targetX, targetY}, speed), 0)
844 sequence:start()
845 self.animation = sequence
846
847 self.lastPositionState = state
848 end
849end

setScale

Description
Set this element's scale.
Definition
setScale()
Code
998function ControlBarDisplay:setScale(uiScale)
999 ControlBarDisplay:superClass().setScale(self, uiScale, uiScale)
1000
1001 local currentVisibility = self:getVisible()
1002 self:setVisible(true, false)
1003
1004 self.uiScale = uiScale
1005 local posX, posY = ControlBarDisplay.getBackgroundPosition(uiScale, self:getWidth())
1006 self:setPosition(posX, posY)
1007
1008 self:storeOriginalPosition()
1009 self:setVisible(currentVisibility, false)
1010end

setSwitchIconsAlpha

Description
Definition
setSwitchIconsAlpha()
Code
853function ControlBarDisplay:setSwitchIconsAlpha(alphaValue)
854 for _, element in ipairs(self.switchElements) do
855 element:setColor(nil, nil, nil, alphaValue)
856 if element.setFrameColor ~= nil then
857 element:setFrameColor(nil, nil, nil, alphaValue)
858 end
859 end
860end

setVehicle

Description
Set the currently controlled vehicle which provides display data.
Definition
setVehicle(table vehicle)
Arguments
tablevehicleCurrently controlled vehicle
Code
181function ControlBarDisplay:setVehicle(vehicle)
182 self.vehicle = vehicle
183
184 self:removeControls()
185
186 if vehicle ~= nil then
187 if vehicle.getChildVehicles ~= nil then
188 local vehicles = vehicle:getChildVehicles()
189 self.numVehicles = #vehicles
190 self:addFillLevelDisplaysFromVehicles(vehicles)
191 end
192 end
193
194 self:updatePositionState()
195end

update

Description
Update the fill levels state.
Definition
update()
Code
868function ControlBarDisplay:update(dt)
869 ControlBarDisplay:superClass().update(self, dt)
870
871 local vehicle = self.vehicle
872 if vehicle ~= nil then
873 for name, vehicleControl in pairs(self.vehicleControls) do
874 local updated = false
875 local _, isAvailable = self:updateVehicleControl(vehicle, vehicleControl, name)
876 if not isAvailable then
877 if vehicleControl.useAttachables then
878 if vehicle.getAttachedImplements ~= nil then
879 for _, implement in ipairs(vehicle:getAttachedImplements()) do
880 local hasAvailable, isAvailable = self:updateVehicleControl(implement.object, vehicleControl, name)
881 if hasAvailable and isAvailable then
882 updated = true
883 break
884 end
885 end
886 end
887 end
888 else
889 updated = true
890 end
891
892 -- if the vehicle control is not updated probably the parent vehicle was detached
893 if not updated then
894 if vehicleControl.controlButton ~= nil then
895 self:removeControlButton(vehicleControl.controlButton)
896 vehicleControl.controlButton = nil
897 vehicleControl.lastCustomIcon = nil
898 vehicleControl.lastDirection = nil
899 end
900 end
901 end
902
903 if vehicle.getChildVehicles ~= nil then
904 local vehicles = vehicle:getChildVehicles()
905 if #vehicles ~= self.numVehicles then
906 self.numVehicles = #vehicles
907 self:addFillLevelDisplaysFromVehicles(vehicles)
908 end
909 end
910
911 local displayIndex = 1
912 for i=1, #self.fillTypeDisplays do
913 self:updateFillTypeDisplay(self.fillTypeDisplays[i], dt, displayIndex)
914
915 if self.fillTypeDisplays[i].visible then
916 displayIndex = displayIndex + 1
917 end
918 end
919 end
920
921 if self.player ~= nil then
922 for name, playerControl in pairs(self.playerControls) do
923 self:updateVehicleControl(self.player, playerControl, name)
924 end
925 end
926
927 -- debug enable mouse
928 if vehicle ~= nil then
929 if not g_gui:getIsGuiVisible() then
930 local show = Utils.getNoNil(Input.isKeyPressed(Input.KEY_lctrl), false)
931 g_inputBinding:setShowMouseCursor(show, false)
932 end
933 end
934end

updateControlButtons

Description
Definition
updateControlButtons()
Code
548function ControlBarDisplay:updateControlButtons()
549 local baseX, _ = self:getPosition()
550 local buttonsByPrio = {}
551 for _, controlButton in pairs(self.controlButtons) do
552 local visible = true
553 if controlButton.visibleFunc ~= nil then
554 visible = controlButton.visibleFunc(controlButton)
555 end
556
557 if visible then
558 table.insert(buttonsByPrio, controlButton)
559 end
560 self:setButtonVisibility(controlButton, visible)
561 end
562 table.sort(buttonsByPrio, function(a, b) return a.prio < b.prio end)
563
564
565 for index, controlButton in ipairs(buttonsByPrio) do
566 local posX = ControlBarDisplay.POSITION.CONTROL_BUTTON_OFFSET[1] + ControlBarDisplay.SIZE.BUTTON_SIZE[1] * (index - 1)
567 if index > 1 then
568 posX = posX - 1
569 end
570 posX, _ = getNormalizedScreenValues(posX, 0)
571
572 for _, element in ipairs(controlButton.elements) do
573 local offset, _ = getNormalizedScreenValues(element.overlayOffsetX or 0, 0)
574 offset = offset + (element.overlayOffsetXScreenSpace or 0)
575 element:setPosition(baseX + posX + offset)
576 end
577
578 if controlButton.frame ~= nil then
579 -- first button need to be smaller since we dont have a right border
580 local width = ControlBarDisplay.SIZE.BUTTON_SIZE[1]
581 if index == 1 then
582 width = width - HUDFrameElement.THICKNESS.FRAME
583 end
584 width, _ = getNormalizedScreenValues(width, 0)
585 controlButton.frame:setDimension(width)
586
587 controlButton.frame:setRightLineVisible(index == #buttonsByPrio)
588 end
589 end
590end

updateFillTypeDisplay

Description
Definition
updateFillTypeDisplay()
Code
620function ControlBarDisplay:updateFillTypeDisplay(fillLevelDisplay, dt, index)
621 local fillLevel = 0
622 local fillLevelPct = 0
623 local fillTypeIcon = ""
624 if fillLevelDisplay.vehicle:getFillUnitExists(fillLevelDisplay.fillUnitIndex) then
625 fillLevel = fillLevelDisplay.vehicle:getFillUnitFillLevel(fillLevelDisplay.fillUnitIndex)
626 fillLevelPct = fillLevelDisplay.vehicle:getFillUnitFillLevelPercentage(fillLevelDisplay.fillUnitIndex)
627
628 if fillLevelDisplay.isSowingMachine then
629 fillTypeIcon = fillLevelDisplay.vehicle:getCurrentSeedTypeIcon()
630 else
631 local fillTypeIndex = fillLevelDisplay.vehicle:getFillUnitFillType(fillLevelDisplay.fillUnitIndex)
632 if fillTypeIndex ~= nil then
633 local fillUnit = fillLevelDisplay.vehicle:getFillUnitByIndex(fillLevelDisplay.fillUnitIndex)
634 if fillUnit.fillTypeToDisplay ~= FillType.UNKNOWN then
635 fillTypeIndex = fillUnit.fillTypeToDisplay
636 end
637
638 local fillType = g_fillTypeManager:getFillTypeByIndex(fillTypeIndex)
639 fillTypeIcon = fillType.hudOverlayFilename
640 end
641 end
642
643 -- update control button visibility
644 if (fillLevelPct > 0 and not fillLevelDisplay.visible) or (fillLevelPct <= 0 and fillLevelDisplay.visible) then
645 self:updateControlButtons()
646 end
647 end
648
649 fillLevelDisplay.currentUVs[5] = (fillLevelDisplay.normUVs[5] - fillLevelDisplay.normUVs[1]) * fillLevelPct + fillLevelDisplay.normUVs[1]
650 fillLevelDisplay.currentUVs[7] = (fillLevelDisplay.normUVs[7] - fillLevelDisplay.normUVs[3]) * fillLevelPct + fillLevelDisplay.normUVs[3]
651 fillLevelDisplay.fillLevelBar:setUVs(fillLevelDisplay.currentUVs)
652 fillLevelDisplay.fillLevelBar:setScale(fillLevelPct, 1)
653
654 local fillLevelBarBackground = fillLevelDisplay.fillLevelBarBackground
655 fillLevelBarBackground.currentUVs[1] = (fillLevelDisplay.normUVs[5] - fillLevelDisplay.normUVs[1]) * (fillLevelPct) + fillLevelDisplay.normUVs[1]
656 fillLevelBarBackground.currentUVs[3] = (fillLevelDisplay.normUVs[7] - fillLevelDisplay.normUVs[3]) * (fillLevelPct) + fillLevelDisplay.normUVs[3]
657 fillLevelBarBackground:setUVs(fillLevelBarBackground.currentUVs)
658
659 local baseX, _ = self:getPosition()
660 local offsetX = ControlBarDisplay.SIZE.BUTTON_SIZE[1] * (index - 1) - (index > 1 and 1 or 0)
661 offsetX, _ = getNormalizedScreenValues(offsetX, 0)
662 fillLevelBarBackground:setPosition(baseX + offsetX + fillLevelBarBackground.defaultPos[1] + fillLevelBarBackground.defaultPosOffset[1] + fillLevelBarBackground.defaultSize[1] * fillLevelPct)
663 fillLevelBarBackground:setScale(1 - fillLevelPct, 1)
664
665 if fillTypeIcon ~= "" then
666 if fillLevelDisplay.icon.overlay ~= nil then
667 fillLevelDisplay.icon:setImage(fillTypeIcon)
668 end
669 end
670
671 fillLevelDisplay.lastFillLevel = fillLevel
672 fillLevelDisplay.lastFillLevelPct = fillLevelPct
673end

updateHudElements

Description
Definition
updateHudElements()
Code
747function ControlBarDisplay:updateHudElements()
748 for _, element in ipairs(self.hudElements) do
749 if element.parent ~= self then
750 self:addChild(element)
751 end
752 end
753end

updatePositionState

Description
Definition
updatePositionState()
Code
773function ControlBarDisplay:updatePositionState(force)
774 if self.player ~= nil and self.lastInputHelpMode ~= GS_INPUT_HELP_MODE_GAMEPAD then
775 return self:setPositionState(ControlBarDisplay.STATE_TOUCH, force)
776 end
777
778 if self.lastInputHelpMode == GS_INPUT_HELP_MODE_GAMEPAD
779 or (self.vehicle ~= nil and self.vehicle:getIsAIActive())
780 or self.lastGyroscopeSteeringState then
781 self:setPositionState(ControlBarDisplay.STATE_CONTROLLER, force)
782 return
783 end
784
785 self:setPositionState(ControlBarDisplay.STATE_TOUCH, force)
786end

updateVehicleControl

Description
Definition
updateVehicleControl()
Code
938function ControlBarDisplay:updateVehicleControl(vehicle, vehicleControl, name)
939 local availableFunc = vehicle[vehicleControl.availableFunc]
940
941 if availableFunc ~= nil then
942 if availableFunc(vehicle) then
943 if vehicleControl.controlButton == nil then
944 local allowedFunc = vehicle[vehicleControl.allowedFunc]
945 local actionFunc = vehicle[vehicleControl.actionFunc]
946
947 vehicleControl.controlButton = self:addControlButton(allowedFunc, actionFunc, vehicle, vehicleControl.triggerType, vehicleControl.uvs or vehicleControl.uvs_pos, vehicleControl.prio, vehicleControl.inputAction)
948 vehicleControl.lastDirection = nil
949 end
950 else
951 if vehicleControl.controlButton ~= nil then
952 self:removeControlButton(vehicleControl.controlButton)
953 vehicleControl.controlButton = nil
954 vehicleControl.lastCustomIcon = nil
955 vehicleControl.lastDirection = nil
956 end
957 end
958
959 if vehicleControl.controlButton ~= nil then
960 if vehicleControl.directionFunc ~= nil then
961 local directionFunc = vehicle[vehicleControl.directionFunc]
962 if directionFunc ~= nil then
963 local direction = directionFunc(vehicle)
964 if type(direction) == "boolean" then
965 direction = direction and 1 or -1
966 end
967 if direction ~= vehicleControl.lastDirection then
968 self:setControlButtonDirection(vehicleControl.controlButton, vehicleControl, direction)
969 vehicleControl.lastDirection = direction
970 end
971 end
972 end
973
974 if vehicleControl.customIconFunc ~= nil then
975 local customIconFunc = vehicle[vehicleControl.customIconFunc]
976 if customIconFunc ~= nil then
977 local customIcon = customIconFunc(vehicle)
978 if customIcon ~= vehicleControl.lastCustomIcon and customIcon ~= nil then
979 self:setControlButtonIcon(vehicleControl.controlButton, vehicleControl, customIcon)
980 vehicleControl.lastCustomIcon = customIcon
981 end
982 end
983 end
984 end
985
986 return true, vehicleControl.controlButton ~= nil
987 end
988
989 return false, false
990end