332 | function 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 |
388 | end |
435 | function 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 |
544 | end |
210 | function 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() |
273 | end |
21 | function 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 |
152 | end |
819 | function 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 |
849 | end |
868 | function 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 |
934 | end |
548 | function 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 |
590 | end |
620 | function 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 |
673 | end |
938 | function 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 |
990 | end |