LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

VehicleSchemaDisplay

Description
HUD vehicle schema display. Displays a schematic view of the current vehicle configuration.
Parent
HUDDisplayElement
Functions

animateDocking

Description
Animate docking / undocking from input help display.
Definition
animateDocking(float startX, float startY, float targetX, float targetY, bool isDocking)
Arguments
floatstartXScreen space starting X position of animation
floatstartYScreen space starting Y position of animation
floattargetXScreen space target X position of animation
floattargetYScreen space target Y position of animation
boolisDockingIf true, moving to docking position. If false, moving to stand-alone position.
Code
179function VehicleSchemaDisplay:animateDocking(startX, startY, targetX, targetY, isDocking)
180 local sequence = TweenSequence.new(self)
181 local lateDockInstant = HUDDisplayElement.MOVE_ANIMATION_DURATION * 0.5
182 if not isDocking then
183 sequence:addInterval(HUDDisplayElement.MOVE_ANIMATION_DURATION) -- synchronize with input help element
184 lateDockInstant = lateDockInstant + HUDDisplayElement.MOVE_ANIMATION_DURATION
185 end
186
187 sequence:addTween(MultiValueTween.new(self.setPosition, {startX, startY}, {targetX, targetY}, HUDDisplayElement.MOVE_ANIMATION_DURATION))
188 -- set docked state in the middle of the animation:
189 sequence:insertCallback(self.lateSetDocked, isDocking, lateDockInstant)
190
191 sequence:start()
192 self.animation = sequence
193end

collectVehicleSchemaDisplayOverlays

Description
Recursively get vehicle schema overlay parts for a vehicle configuration.
Definition
collectVehicleSchemaDisplayOverlays()
Code
197function VehicleSchemaDisplay:collectVehicleSchemaDisplayOverlays(overlays, depth, vehicle, rootVehicle, parentOverlay, x, y, rotation, invertingX)
198 if vehicle.getAttachedImplements == nil then
199 return
200 end
201
202 local attachedImplements = vehicle:getAttachedImplements()
203 for _, implement in pairs(attachedImplements) do
204 local object = implement.object
205 if object ~= nil and object.schemaOverlay ~= nil then
206 local selected = object:getIsSelected()
207 local turnedOn = object:getUseTurnedOnSchema()
208 local jointDesc = vehicle.schemaOverlay.attacherJoints[implement.jointDescIndex]
209
210 if jointDesc ~= nil then
211 local invertX = invertingX ~= jointDesc.invertX
212 local overlay = self:getSchemaOverlayForState(object.schemaOverlay, true)
213
214 local baseY = y + jointDesc.y * parentOverlay.height
215 local baseX
216 if invertX then
217 baseX = x + jointDesc.x * parentOverlay.width
218 else
219 baseX = x - overlay.width + (1 - jointDesc.x) * parentOverlay.width
220 end
221
222 local rot = rotation + jointDesc.rotation
223
224 local offsetX, offsetY
225 if invertX then
226 offsetX = -object.schemaOverlay.offsetX * overlay.width
227 else
228 offsetX = object.schemaOverlay.offsetX * overlay.width
229 end
230
231 offsetY = object.schemaOverlay.offsetY * overlay.height
232 local rotatedX = offsetX * math.cos(rot) - offsetY * math.sin(rot)
233 local rotatedY = offsetX * math.sin(rot) + offsetY * math.cos(rot)
234 baseX = baseX - rotatedX
235 baseY = baseY - rotatedY
236
237 local isLowered = object.getIsLowered ~= nil and object:getIsLowered(true)
238 if not isLowered then
239 local widthOffset, heightOffset = getNormalizedScreenValues(jointDesc.liftedOffsetX, jointDesc.liftedOffsetY)
240 baseX = baseX + widthOffset
241 baseY = baseY + heightOffset * 0.5
242 end
243
244 local additionalText = object:getAdditionalSchemaText()
245
246 table.insert(overlays, {
247 overlay = overlay,
248 additionalText = additionalText,
249 x = baseX,
250 y = baseY,
251 rotation = rot,
252 invertX = not invertX,
253 invisibleBorderRight = object.schemaOverlay.invisibleBorderRight,
254 invisibleBorderLeft = object.schemaOverlay.invisibleBorderLeft,
255 selected = selected,
256 turnedOn = turnedOn
257 })
258
259 if depth <= VehicleSchemaDisplay.MAX_SCHEMA_COLLECTION_DEPTH then
260 self:collectVehicleSchemaDisplayOverlays(overlays, depth + 1, object, rootVehicle, overlay, baseX, baseY, rot, invertX)
261 end
262 end
263 end
264 end
265end

createBackground

Description
Create an empty background positioning overlay.
Definition
createBackground()
Code
476function VehicleSchemaDisplay.createBackground()
477 local width, height = getNormalizedScreenValues(unpack(VehicleSchemaDisplay.SIZE.SELF))
478 local posX, posY = VehicleSchemaDisplay.getBackgroundPosition(false, 1.0)
479
480 return Overlay.new(nil, posX, posY, width, height)
481end

delete

Description
Delete this element. Also deletes all loaded vehicle schema overlays.
Definition
delete()
Code
43function VehicleSchemaDisplay:delete()
44 VehicleSchemaDisplay:superClass().delete(self)
45
46 if self.overlayFront ~= nil then
47 self.overlayFront:delete()
48 end
49 if self.overlayMiddle ~= nil then
50 self.overlayMiddle:delete()
51 end
52 if self.overlayBack ~= nil then
53 self.overlayBack:delete()
54 end
55
56 for k, v in pairs(self.vehicleSchemaOverlays) do
57 v:delete()
58 self.vehicleSchemaOverlays[k] = nil
59 end
60end

draw

Description
Draw the vehicle schema display. Only draws the schema if a controlled vehicle is set.
Definition
draw()
Code
165function VehicleSchemaDisplay:draw()
166 if self.vehicle ~= nil then
167 VehicleSchemaDisplay:superClass().draw(self)
168 self:drawVehicleSchemaOverlays(self.vehicle)
169 end
170end

drawVehicleSchemaOverlays

Description
Draw vehicle schema icons for a given vehicle.
Definition
drawVehicleSchemaOverlays(table vehicle)
Arguments
tablevehicleCurrent vehicle
Code
328function VehicleSchemaDisplay:drawVehicleSchemaOverlays(vehicle)
329 vehicle = vehicle.rootVehicle
330
331 if vehicle.schemaOverlay ~= nil then
332 local overlays, overlayHeight = self:getVehicleSchemaOverlays(vehicle)
333
334 local x, y = self:getPosition()
335 local baseX, baseY = x, y
336
337 baseY = baseY + (self:getHeight() - overlayHeight) * 0.5 -- vertically center icon base in panel
338 if self.isDocked then
339 baseX = baseX + self:getWidth() -- right-align when docked to input help
340 end
341
342 local minX, maxX = self:getSchemaDelimiters(overlays)
343
344 -- dynamically scale schemas if going over size limit:
345 local scale = 1
346 local sizeX = maxX - minX
347 if sizeX > self.maxSchemaWidth then
348 scale = self.maxSchemaWidth / sizeX
349 end
350
351 local barOffsetX = self:updateBarComponents(baseX, y, sizeX, self.overlay.height * self.uiScale, self.isDocked)
352 self.overlayFront:render()
353 self.overlayMiddle:render()
354 self.overlayBack:render()
355
356 local newPosX = baseX
357 if self.isDocked then
358 newPosX = newPosX - maxX * scale - barOffsetX
359 else
360 newPosX = newPosX - minX * scale + barOffsetX
361 end
362
363 for _, overlayDesc in pairs(overlays) do
364 local overlay = overlayDesc.overlay
365 local width, height = overlay.width, overlay.height
366
367 overlay:setInvertX(overlayDesc.invertX)
368 overlay:setPosition(newPosX + overlayDesc.x, baseY + overlayDesc.y)
369 overlay:setRotation(overlayDesc.rotation, 0, 0)
370 overlay:setDimension(width * scale, height * scale)
371
372 local color = overlayDesc.turnedOn and VehicleSchemaDisplay.COLOR.TURNED_ON or VehicleSchemaDisplay.COLOR.DEFAULT
373 overlay:setColor(color[1], color[2], color[3], overlayDesc.selected and 1 or 0.5)
374
375 overlay:render()
376
377 if overlayDesc.additionalText ~= nil then
378 local posX = newPosX + overlayDesc.x + (width * scale) * 0.5
379 local posY = baseY + overlayDesc.y + (height * scale * 0.85)
380 setTextBold(false)
381 setTextColor(1, 1, 1, 1)
382 setTextAlignment(RenderText.ALIGN_CENTER)
383 renderText(posX, posY, getCorrectTextSize(0.008), overlayDesc.additionalText)
384 setTextAlignment(RenderText.ALIGN_LEFT)
385 setTextColor(1, 1, 1, 1)
386 end
387
388 -- reset dimension
389 overlay:setDimension(width, height)
390 end
391 end
392end

getBackgroundPosition

Description
Get the vehicle schema's base background position.
Definition
getBackgroundPosition(bool isDocked, float uiScale)
Arguments
boolisDockedIf true, the vehicle schema is docked to the input help display
floatuiScaleCurrent UI scale
Code
457function VehicleSchemaDisplay.getBackgroundPosition(isDocked, uiScale)
458 local width, height = getNormalizedScreenValues(unpack(VehicleSchemaDisplay.SIZE.SELF))
459
460 local posX, posY = g_safeFrameOffsetX, 1 - g_safeFrameOffsetY - height * uiScale -- top left anchored
461 if isDocked then
462 local offX, offY = getNormalizedScreenValues(unpack(VehicleSchemaDisplay.POSITION.SELF_DOCKED))
463 posX = posX + (offX - width) * uiScale
464 posY = posY + offY * uiScale
465 end
466
467 return posX, posY
468end

getSchemaDelimiters

Description
Get minimum and maximum screen space X positions of vehicle schema overlay descriptions. The returned positions are relative to the position of the root vehicle schema overlay.
Definition
getSchemaDelimiters(table overlayDescriptions)
Arguments
tableoverlayDescriptionsArray of overlay descriptions, see VehicleSchemaDisplay:getVehicleSchemaOverlays()
Return Values
floatMinimumX position (left)
floatMaximumX position (right)
Code
300function VehicleSchemaDisplay:getSchemaDelimiters(overlayDescriptions)
301 local minX = math.huge
302 local maxX = -math.huge
303 for _, overlayDesc in pairs(overlayDescriptions) do
304 local overlay = overlayDesc.overlay
305
306 local cosRot = math.cos(overlayDesc.rotation)
307 local sinRot = math.sin(overlayDesc.rotation)
308
309 local offX = overlayDesc.invisibleBorderLeft * overlay.width
310 local dx = overlay.width + (overlayDesc.invisibleBorderRight + overlayDesc.invisibleBorderLeft) * overlay.width
311
312 local dy = overlay.height
313 local x = overlayDesc.x + offX * cosRot
314 local dx2 = dx * cosRot
315 local dx3 = -dy * sinRot
316 local dx4 = dx2 + dx3
317
318 maxX = math.max(maxX, x, x + dx2, x + dx3, x + dx4)
319 minX = math.min(minX, x, x + dx2, x + dx3, x + dx4)
320 end
321
322 return minX, maxX
323end

getSchemaOverlayForState

Description
Get a schema overlay for a given vehicle's schema overlay data and current state.
Definition
getSchemaOverlayForState(table schemaOverlayData, bool isTurnedOn, bool isSelected, bool isImplement)
Arguments
tableschemaOverlayDataVehicleSchemaOverlayData instance of the current vehicle
boolisTurnedOnTrue if the vehicle is currently turned on, i.e. its function is active
boolisSelectedTrue if the vehicle is currently selected for input
boolisImplementTrue if the vehicle is an implement (i.e. attached to a motorized vehicle), false if it's the root vehicle
Return Values
tableSchemaOverlay instance
Code
401function VehicleSchemaDisplay:getSchemaOverlayForState(schemaOverlayData, isImplement, iconOverride)
402 local schemaName
403
404 schemaName = schemaOverlayData.schemaName
405
406 -- Backwards compatibility
407 if schemaName == "DEFAULT_IMPLEMENT" then
408 schemaName = "IMPLEMENT"
409 elseif schemaName == "DEFAULT_VEHICLE" then
410 schemaName = "VEHICLE"
411 end
412
413 if not schemaName or schemaName == "" or self.vehicleSchemaOverlays[schemaName] == nil then
414 schemaName = isImplement and VehicleSchemaOverlayData.SCHEMA_OVERLAY.IMPLEMENT or VehicleSchemaOverlayData.SCHEMA_OVERLAY.VEHICLE
415 end
416
417 return self.vehicleSchemaOverlays[schemaName]
418end

getVehicleSchemaOverlays

Description
Get a vehicle configuration's schema overlays, including the root vehicle.
Definition
getVehicleSchemaOverlays()
Return Values
tableArrayof overlay descriptions: {overlay=overlay, x=0, y=0, rotation=0, invertX=false, invisibleBorderRight=vehicle.schemaOverlay.invisibleBorderRight, invisibleBorderLeft=vehicle.schemaOverlay.invisibleBorderLeft}
floatScreenspace height of root vehicle schema overlay
Code
271function VehicleSchemaDisplay:getVehicleSchemaOverlays(vehicle)
272 local overlay = self:getSchemaOverlayForState(vehicle.schemaOverlay, false)
273 local additionalText = vehicle:getAdditionalSchemaText()
274 local overlays = {}
275
276 table.insert(overlays, {
277 overlay = overlay,
278 additionalText = additionalText,
279 x = 0,
280 y = 0,
281 rotation = 0,
282 invertX = false,
283 invisibleBorderRight = vehicle.schemaOverlay.invisibleBorderRight,
284 invisibleBorderLeft = vehicle.schemaOverlay.invisibleBorderLeft,
285 turnedOn = vehicle:getUseTurnedOnSchema(),
286 selected = vehicle:getIsSelected()
287 })
288
289 self:collectVehicleSchemaDisplayOverlays(overlays, 1, vehicle, vehicle, overlay, 0, 0, 0, false)
290
291 return overlays, overlay.height
292end

lateSetDocked

Description
Animation method to set docked state at a delayed time by callback.
Definition
lateSetDocked()
Code
136function VehicleSchemaDisplay:lateSetDocked(isDocked)
137 self.isDocked = isDocked
138end

loadVehicleSchemaOverlays

Description
Load vehicle schema overlays from global and mod definitions.
Definition
loadVehicleSchemaOverlays()
Code
64function VehicleSchemaDisplay:loadVehicleSchemaOverlays()
65 local xmlFile = loadXMLFile("VehicleSchemaDisplayOverlays", VehicleSchemaDisplay.SCHEMA_OVERLAY_DEFINITIONS_PATH)
66 self:loadVehicleSchemaOverlaysFromXML(xmlFile)
67 delete(xmlFile)
68
69 for _, modDesc in ipairs(self.modManager:getActiveMods()) do
70 xmlFile = loadXMLFile("VehicleSchemaDisplay ModFile", modDesc.modFile)
71 if xmlFile ~= 0 then
72 self:loadVehicleSchemaOverlaysFromXML(xmlFile, modDesc.modFile)
73 delete(xmlFile)
74 end
75 end
76
77 self:storeScaledValues()
78end

loadVehicleSchemaOverlaysFromXML

Description
Load and create vehicle schema overlays from XML definitions.
Definition
loadVehicleSchemaOverlaysFromXML(int xmlFile, string modPath)
Arguments
intxmlFileXML file handle of vehicle schema definitions
stringmodPathPath to the current mod description or nil for the base game
Code
84function VehicleSchemaDisplay:loadVehicleSchemaOverlaysFromXML(xmlFile, modPath)
85 local rootPath = "vehicleSchemaOverlays"
86 local baseDirectory = ""
87 local prefix = ""
88 if modPath then
89 rootPath = "modDesc.vehicleSchemaOverlays"
90 local modName, dir = Utils.getModNameAndBaseDirectory(modPath)
91 baseDirectory = dir
92 prefix = modName
93 end
94
95 local atlasPath = getXMLString(xmlFile, rootPath .. "#filename")
96 local imageSize = GuiUtils.get2DArray(getXMLString(xmlFile, rootPath .. "#imageSize"), {1024, 1024})
97
98 local i = 0
99 while true do
100 local baseName = string.format("%s.overlay(%d)", rootPath, i)
101 if not hasXMLProperty(xmlFile, baseName) then
102 break -- no more overlay definitions
103 end
104
105 local baseOverlayName = getXMLString(xmlFile, baseName .. "#name")
106 local uvString = getXMLString(xmlFile, baseName .. "#uvs") or string.format("0px 0px %ipx %ipx", imageSize[1], imageSize[2])
107 local uvs = GuiUtils.getUVs(uvString, imageSize)
108
109 local sizeString = getXMLString(xmlFile, baseName .. "#size") or string.format("%ipx %ipx",
110 VehicleSchemaDisplay.SIZE.ICON[1], VehicleSchemaDisplay.SIZE.ICON[1])
111 local size = GuiUtils.getNormalizedValues(sizeString, {1, 1}) -- remove pixel units but do not change numbers
112
113 if baseOverlayName then
114 local overlayName = prefix .. baseOverlayName
115
116 local atlasFileName = Utils.getFilename(atlasPath, baseDirectory)
117 local schemaOverlay = Overlay.new(atlasFileName, 0, 0, size[1], size[2]) -- store pixel size to be scaled later
118 schemaOverlay:setUVs(uvs)
119
120 self.vehicleSchemaOverlays[overlayName] = schemaOverlay
121 end
122
123 i = i + 1
124 end
125end

new

Description
Create a new instance of VehicleSchemaDisplay.
Definition
new(table modManager)
Arguments
tablemodManagerModManager reference
Code
22function VehicleSchemaDisplay.new(modManager)
23 local backgroundOverlay = VehicleSchemaDisplay.createBackground()
24 local self = VehicleSchemaDisplay:superClass().new(backgroundOverlay, nil, VehicleSchemaDisplay_mt)
25
26 self:createBackgroundBar()
27
28 self.modManager = modManager
29
30 self.vehicle = nil -- currently controlled vehicle
31 self.isDocked = false -- If true, the schema display is docked to the input help display
32 self.vehicleSchemaOverlays = {} -- schema name -> overlay
33
34 self.iconSizeX, self.iconSizeY = 0, 0 -- schema overlay icon size
35 self.maxSchemaWidth = 0 -- maximum width of vehicle configuration schema
36
37 return self
38end

setDocked

Description
Set the schema's docking state. This element's position is updated based on the docking state.
Definition
setDocked(bool isDocked)
Arguments
boolisDockedIf true, the schema should be display docked to the HUD input help display. Otherwise, it will take the input help's place in the top left corner.
Code
145function VehicleSchemaDisplay:setDocked(isDocked, animate)
146 local targetX, targetY = VehicleSchemaDisplay.getBackgroundPosition(isDocked, self:getScale())
147 if animate and self.animation:getFinished() then
148 local startX, startY = self:getPosition()
149
150 self:animateDocking(startX, startY, targetX, targetY, isDocked)
151 else
152 self.animation:stop()
153 self.isDocked = isDocked
154 self:setPosition(targetX, targetY)
155 end
156end

setScale

Description
Set this element's UI scale.
Definition
setScale(float uiScale)
Arguments
floatuiScaleUI scale factor
Code
427function VehicleSchemaDisplay:setScale(uiScale)
428 VehicleSchemaDisplay:superClass().setScale(self, uiScale, uiScale)
429
430 local posX, posY = VehicleSchemaDisplay.getBackgroundPosition(self.isDocked, uiScale)
431 self:setPosition(posX, posY)
432
433 self:storeScaledValues()
434
435 self.uiScale = uiScale
436end

setVehicle

Description
Set the currently controlled vehicle to display its schematic view.
Definition
setVehicle(table vehicle)
Arguments
tablevehicleVehicle reference
Code
130function VehicleSchemaDisplay:setVehicle(vehicle)
131 self.vehicle = vehicle
132end

storeScaledValues

Description
Store scaled positioning, size and offset values.
Definition
storeScaledValues()
Code
440function VehicleSchemaDisplay:storeScaledValues()
441 self.iconSizeX, self.iconSizeY = self:scalePixelToScreenVector(VehicleSchemaDisplay.SIZE.ICON)
442 self.maxSchemaWidth = self:scalePixelToScreenWidth(VehicleSchemaDisplay.MAX_SCHEMA_WIDTH)
443
444 for _, overlay in pairs(self.vehicleSchemaOverlays) do
445 overlay:resetDimensions()
446
447 local pixelSize = {overlay.defaultWidth, overlay.defaultHeight}
448 local width, height = self:scalePixelToScreenVector(pixelSize)
449 overlay:setDimension(width, height)
450 end
451end