LUADOC - Farming Simulator 22

VehicleHUDExtension

Description
Custom vehicle HUD drawing extension. This serves as the base class for custom specific drawing cases of vehicles in the HUD, e.g. MixerWagon fill levels. To create new HUD extensions for vehicle specializations: 1. sub-class this base class 2. source() the sub-class module after its corresponding specialization's table has been declared 3. call VehicleHUDExtension.registerHUDExtension([specialization], [HUDextension]) in sub-class module
Functions

addComponentForCleanup

Description
Add a display component for cleanup on delete(). Added components must support delete() themselves or they will be ignored.
Definition
addComponentForCleanup()
Code
54function VehicleHUDExtension:addComponentForCleanup(component)
55 if component.delete then
56 table.insert(self.displayComponents, component)
57 end
58end

canDraw

Description
Determine if this HUD extension is in a valid state for a call to draw() in the current frame. Override in sub-classes with custom logic.
Definition
canDraw()
Return Values
boolIftrue, the HUD extension should be drawn in the current frame.
Code
71function VehicleHUDExtension:canDraw()
72 return true
73end

createHUDExtensionForSpecialization

Description
HUD extension factory method, creates a HUD extension for a given vehicle specialization.
Definition
createHUDExtensionForSpecialization(table spec, table vehicle, float uiScale, table uiTextColor, float uiTextSize)
Arguments
tablespecSpecialization reference
tablevehicleVehicle which has the given specialization
floatuiScaleCurrent UI scale
tableuiTextColorHUD text drawing color as an RGBA array
floatuiTextSizeHUD text size
Return Values
tableHUDextension instance or nil of no extension has been registered for the given specialization
Code
112function VehicleHUDExtension.createHUDExtensionForSpecialization(spec, vehicle, uiScale, uiTextColor, uiTextSize)
113 local extType = registry[spec]
114 local extension = nil
115 if extType then
116 extension = extType.new(vehicle, uiScale, uiTextColor, uiTextSize)
117 end
118
119 return extension
120end

delete

Description
Delete this instance and clean up resources.
Definition
delete()
Code
44function VehicleHUDExtension:delete()
45 for k, component in pairs(self.displayComponents) do
46 component:delete()
47 self.displayComponents[k] = nil
48 end
49end

draw

Description
Draw HUD extension.
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
81function VehicleHUDExtension:draw(leftPosX, rightPosX, posY)
82end

getDisplayHeight

Description
Get this HUD extension's display height. Override in subclasses.
Definition
getDisplayHeight()
Code
63function VehicleHUDExtension:getDisplayHeight()
64 return 0
65end

getPriority

Description
Priority index to define rendering order
Definition
getPriority()
Code
86function VehicleHUDExtension:getPriority()
87 return 0
88end

hasHUDExtensionForSpecialization

Description
Check if there is a HUD extension for a given specialization.
Definition
hasHUDExtensionForSpecialization()
Code
124function VehicleHUDExtension.hasHUDExtensionForSpecialization(spec)
125 return not not registry[spec]
126end

new

Description
Base constructor for vehicle HUD extensions.
Definition
new(table class_mt, table vehicle, float uiScale, table uiTextColor, float uiTextSize)
Arguments
tableclass_mtSub-class metatable
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
26function VehicleHUDExtension.new(class_mt, vehicle, uiScale, uiTextColor, uiTextSize)
27 local self = setmetatable({}, class_mt or VehicleHUDExtension_mt)
28
29 -- vehicle specialization reference which provides the display data
30 self.vehicle = vehicle
31
32 self.uiTextColor = uiTextColor
33 self.uiTextSize = uiTextSize
34 self.uiScale = uiScale
35
36 -- array of created display components which need to be deleted
37 self.displayComponents = {}
38
39 return self
40end

registerHUDExtension

Description
Register a HUD extension for a specialization.
Definition
registerHUDExtension(table specializationType, table hudExtensionType)
Arguments
tablespecializationTypeVehicle specialization class type table
tablehudExtensionTypeHUD extension class type table corresponding to the given vehicle specialization
Code
100function VehicleHUDExtension.registerHUDExtension(spec, hudExtensionType)
101 registry[spec] = hudExtensionType
102end

sortHUDExtensions

Description
Sort function to sort hud extensions based on prio
Definition
sortHUDExtensions()
Code
130function VehicleHUDExtension.sortHUDExtensions(extensionA, extensionB)
131 return extensionA:getPriority() > extensionB:getPriority()
132end