LUADOC - Farming Simulator 22

ExtendedSowingMachineHUDExtension

Description
Custom HUD drawing extension for precision farming sowing machines Displays the current seed rate
Parent
VehicleHUDExtension
Functions

canDraw

Description
Determine if the HUD extension should be drawn.
Definition
canDraw()
Code
123function ExtendedSowingMachineHUDExtension:canDraw()
124 if not self.vehicle:getIsActiveForInput(true, true) then
125 return false
126 end
127
128 return true
129end

delete

Description
Delete this instance and clean up resources.
Definition
delete()
Code
102function ExtendedSowingMachineHUDExtension:delete()
103 ExtendedSowingMachineHUDExtension:superClass().delete(self)
104 g_messageCenter:unsubscribeAll(self)
105end

draw

Description
Draw mixing ratio information for a mixing wagon when it is active.
Definition
draw(float leftPosX, float rightPosX, float posY)
Arguments
floatleftPosXLeft input help panel column start position
floatrightPosXRight input help panel column start position
floatposYCurrent input help panel drawing vertical offset
Return Values
floatModifiedinput help panel drawing vertical offset
Code
172function ExtendedSowingMachineHUDExtension:draw(leftPosX, rightPosX, posY)
173 if not self:canDraw() then
174 return
175 end
176
177 setTextColor(unpack(self.uiTextColor))
178 setTextBold(true)
179 setTextAlignment(RenderText.ALIGN_LEFT)
180 setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_MIDDLE)
181 renderDoubleText(leftPosX, posY + self.displayHeight * 0.55, self.textHeightHeadline, self.texts.headline, self.textMaxWidthHeadline)
182 setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_BASELINE)
183 setTextBold(false)
184
185 local spec = self.extendedSowingMachine
186 local seedsFruitType = self.vehicle.spec_sowingMachine.workAreaParameters.seedsFruitType
187 local lastSeedRate = spec.lastSeedRate
188 local lastSeedRateIndex = spec.lastSeedRateIndex
189
190 if not spec.seedRateAutoMode then
191 lastSeedRateIndex = spec.manualSeedRate
192 lastSeedRate = self.seedRateMap:getSeedRateByFruitTypeAndIndex(seedsFruitType, lastSeedRateIndex)
193 end
194
195 local isSupported = self.seedRateMap:getIsFruitTypeSupported(seedsFruitType)
196 if isSupported then
197 setTextAlignment(RenderText.ALIGN_CENTER)
198 local currentRatePosX = leftPosX + self.rateTextOffsetX
199 local currentRatePosY = posY + self.displayHeight * 0.5 - self.rateTextHeight * 0.5 + self.rateTextOffsetY
200 renderDoubleText(currentRatePosX, currentRatePosY, self.rateTextHeight, string.format(self.texts.seedRate, lastSeedRate))
201
202 for i=1, #self.dots do
203 local dotOverlay = self.dots[i]
204
205 local dotPosX = currentRatePosX + (i / 2 - 1) * self.dotsFullWidth - dotOverlay.width * 0.5
206 local dotPosY = currentRatePosY - dotOverlay.height * 1.5
207
208 dotOverlay:setPosition(dotPosX, dotPosY)
209
210 if i > lastSeedRateIndex then
211 dotOverlay:setUVs(self.dotEmptyUVs)
212 dotOverlay:setColor(1, 1, 1, 0.4)
213 dotOverlay:render()
214 else
215 dotOverlay:setUVs(self.dotFilledUVs)
216 dotOverlay:setColor(1, 1, 1, 0.4)
217 dotOverlay:render()
218
219 local displayValues = self.seedRateMap:getDisplayValues()
220 local displayValue = displayValues[lastSeedRateIndex]
221 local color = displayValue.colors[self.isColorBlindMode][1]
222
223 dotOverlay:setUVs(self.dotFillUVs)
224 dotOverlay:setColor(color[1], color[2], color[3], 1)
225 dotOverlay:render()
226 end
227
228 if not spec.seedRateAutoMode and spec.seedRateRecommendation ~= nil then
229 if i <= spec.seedRateRecommendation then
230 self.recommendOverlay:setPosition(dotPosX + dotOverlay.width * 0.5 - self.recommendOverlay.width * 0.5, dotPosY - self.recommendOverlay.height + self.recommendOverlayOffsetY)
231 self.recommendOverlay:render()
232 end
233 end
234 end
235
236 if spec.seedRateAutoMode then
237 setTextAlignment(RenderText.ALIGN_CENTER)
238 setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_MIDDLE)
239 renderDoubleText(leftPosX + self.modeTextOffsetX, posY + self.displayHeight * 0.52, self.modeTextHeight, self.texts.auto)
240 end
241 else
242 local fillType = g_fillTypeManager:getFillTypeByIndex(g_fruitTypeManager:getFillTypeIndexByFruitTypeIndex(seedsFruitType))
243 if fillType ~= nil then
244 setTextAlignment(RenderText.ALIGN_CENTER)
245 setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_MIDDLE)
246 renderDoubleText(leftPosX + self.naTextOffsetX, posY + self.displayHeight * 0.52, self.naTextHeight, string.format(self.texts.notAvailable, fillType.title))
247 end
248 end
249
250 local uvIndex = lastSeedRateIndex
251 if uvIndex == 0 or not isSupported then
252 uvIndex = 2
253 end
254 self.seedsOverlay:setUVs(self.seedsOverlayUVs[math.max(math.min(uvIndex, 3), 1)])
255
256 self.seedsOverlay:setPosition(rightPosX - self.seedsOverlay.width, posY + self.displayHeight * 0.5 - self.seedsOverlay.height * 0.5)
257 self.seedsOverlay:render()
258
259 setTextAlignment(RenderText.ALIGN_LEFT)
260 setTextVerticalAlignment(RenderText.VERTICAL_ALIGN_BASELINE)
261
262 return posY
263end

getDisplayHeight

Description
Get this HUD extension's display height.
Definition
getDisplayHeight()
Return Values
floatDisplayheight in screen space
Code
134function ExtendedSowingMachineHUDExtension:getDisplayHeight()
135 return self:canDraw() and self.displayHeight or 0
136end

getHelpEntryCountReduction

Description
Returns how many help entry slots should be removed for display of the hud extension
Definition
getHelpEntryCountReduction()
Return Values
integernumSLotsnumSLots
Code
141function ExtendedSowingMachineHUDExtension:getHelpEntryCountReduction()
142 return self:canDraw() and 1 or 0
143end

getPriority

Description
Priority index to define rendering order
Definition
getPriority()
Code
117function ExtendedSowingMachineHUDExtension:getPriority()
118 return 1
119end

new

Description
Create a new instance of ExtendedSowingMachineHUDExtension.
Definition
new(table vehicle, float uiScale, table uiTextColor, float uiTextSize)
Arguments
tablevehicleVehicle which has the specialization required by a sub-class
floatuiScaleCurrent UI scale
tableuiTextColorHUD text drawing color as an RGBA array
floatuiTextSizeHUD text size
Code
25function ExtendedSowingMachineHUDExtension.new(vehicle, uiScale, uiTextColor, uiTextSize)
26 local self = VehicleHUDExtension.new(ExtendedSowingMachineHUDExtension_mt, vehicle, uiScale, uiTextColor, uiTextSize)
27 self.extendedSowingMachine = vehicle.spec_extendedSowingMachine
28
29 local _
30
31 _, self.displayHeight = getNormalizedScreenValues(0, 41 * uiScale)
32
33 self.uiTextColor = uiTextColor
34
35 _, self.textHeightHeadline = getNormalizedScreenValues(0, 20 * uiScale)
36 _, self.textOffsetHeadline = getNormalizedScreenValues(0, 3 * uiScale)
37 self.textMaxWidthHeadline, _ = getNormalizedScreenValues(190 * uiScale, 0)
38
39 self.rateTextOffsetX, self.rateTextHeight = getNormalizedScreenValues(330 * uiScale, 15 * uiScale)
40 _, self.rateTextOffsetY = getNormalizedScreenValues(0 * uiScale, 10 * uiScale)
41
42 self.modeTextOffsetX, self.modeTextHeight = getNormalizedScreenValues(230 * uiScale, 15 * uiScale)
43 _, self.modeTextOffset = getNormalizedScreenValues(0, 2 * uiScale)
44
45 self.naTextOffsetX, self.naTextHeight = getNormalizedScreenValues(290 * uiScale, 12 * uiScale)
46 _, self.naTextOffset = getNormalizedScreenValues(0, 1 * uiScale)
47
48 self.dotEmptyUVs = GuiUtils.getUVs(ExtendedSowingMachineHUDExtension.UV.DOT_EMPTY)
49 self.dotFilledUVs = GuiUtils.getUVs(ExtendedSowingMachineHUDExtension.UV.DOT_FILLED)
50 self.dotFillUVs = GuiUtils.getUVs(ExtendedSowingMachineHUDExtension.UV.DOT_FILL)
51
52 self.dots = {}
53 local dotWidth, dotHeight = getNormalizedScreenValues(12 * uiScale, 12 * uiScale)
54 for i=1, 3 do
55 local dotOverlay = Overlay.new(ExtendedSowingMachineHUDExtension.GUI_ELEMENTS, 0, 0, dotWidth, dotHeight)
56 dotOverlay:setUVs(self.dotEmptyUVs)
57 dotOverlay:setColor(1, 1, 1, 1)
58 table.insert(self.dots, dotOverlay)
59 self:addComponentForCleanup(dotOverlay)
60 end
61
62 self.dotsFullWidth, _ = getNormalizedScreenValues(35 * uiScale, 0 * uiScale)
63
64 self.seedsOverlayUVs = {}
65 self.seedsOverlayUVs[1] = GuiUtils.getUVs(ExtendedSowingMachineHUDExtension.UV.SEEDS[1])
66 self.seedsOverlayUVs[2] = GuiUtils.getUVs(ExtendedSowingMachineHUDExtension.UV.SEEDS[2])
67 self.seedsOverlayUVs[3] = GuiUtils.getUVs(ExtendedSowingMachineHUDExtension.UV.SEEDS[3])
68
69 local width, height = getNormalizedScreenValues(30 * uiScale, 30 * uiScale)
70 self.seedsOverlay = Overlay.new(ExtendedSowingMachineHUDExtension.GUI_ELEMENTS, 0, 0, width, height)
71 self.seedsOverlay:setUVs(self.seedsOverlayUVs[1])
72 self.seedsOverlay:setColor(1, 1, 1, 1)
73 self:addComponentForCleanup(self.seedsOverlay)
74
75 self.seedOverlayOffsetX, _ = getNormalizedScreenValues(0 * uiScale, 0)
76
77 width, height = getNormalizedScreenValues(9 * uiScale, 4 * uiScale)
78 self.recommendOverlay = Overlay.new(ExtendedSowingMachineHUDExtension.GUI_ELEMENTS, 0, 0, width, height)
79 self.recommendOverlay:setUVs(GuiUtils.getUVs(ExtendedSowingMachineHUDExtension.UV.BAR))
80 self.recommendOverlay:setColor(0.25, 0.25, 0.25, 1)
81 self:addComponentForCleanup(self.recommendOverlay)
82
83 _, self.recommendOverlayOffsetY = getNormalizedScreenValues(0, 0 * uiScale)
84
85 self.texts = {}
86 self.texts.headline = g_i18n:getText("hudExtensionSowingMachine_headline", ExtendedSowingMachineHUDExtension.MOD_NAME)
87 self.texts.seedRate = g_i18n:getText("hudExtensionSowingMachine_seedRate", ExtendedSowingMachineHUDExtension.MOD_NAME)
88 self.texts.auto = g_i18n:getText("hudExtensionSowingMachine_auto", ExtendedSowingMachineHUDExtension.MOD_NAME)
89 self.texts.notAvailable = g_i18n:getText("hudExtensionSowingMachine_notAvailable", ExtendedSowingMachineHUDExtension.MOD_NAME)
90
91 self.seedRateMap = g_precisionFarming.seedRateMap
92
93 self.isColorBlindMode = g_gameSettings:getValue(GameSettings.SETTING.USE_COLORBLIND_MODE) or false
94
95 g_messageCenter:subscribe(MessageType.SETTING_CHANGED[GameSettings.SETTING.USE_COLORBLIND_MODE], self.setColorBlindMode, self)
96
97 return self
98end

setColorBlindMode

Description
Determine if the HUD extension should be drawn.
Definition
setColorBlindMode()
Code
109function ExtendedSowingMachineHUDExtension:setColorBlindMode(isActive)
110 if isActive ~= self.isColorBlindMode then
111 self.isColorBlindMode = isActive
112 end
113end