LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

GuiProfile

Description
GUI element display profile. Holds GuiElement property data for re-use similar to a HTML/CSS definition.
Functions

getBool

Description
Get a boolean value from this profile (and its ancestors) by name.
Definition
getBool(name Name, default Default)
Arguments
nameNameof attribute value to retrieve
defaultDefaultvalue to use if the attribute is not defined.
Code
143function GuiProfile:getBool(name, default)
144 local value = self:getValue(name)
145 local ret = default
146 if value ~= nil and value ~= "nil" then
147 ret = (value:lower() == "true")
148 end
149
150 return ret
151end

getNumber

Description
Get a number value from this profile (and its ancestors) by name.
Definition
getNumber(name Name, default Default)
Arguments
nameNameof attribute value to retrieve
defaultDefaultvalue to use if the attribute is not defined.
Code
157function GuiProfile:getNumber(name, default)
158 local value = self:getValue(name)
159 local ret = default
160 if value ~= nil and value ~= "nil" then
161 ret = tonumber(value)
162 end
163
164 return ret
165end

getValue

Description
Get a string value from this profile (and its ancestors) by name.
Definition
getValue(name Name, default Default)
Arguments
nameNameof attribute value to retrieve
defaultDefaultvalue to use if the attribute is not defined.
Code
105function GuiProfile:getValue(name, default)
106 local ret = default
107
108 -- Try a special case
109 if self.values[name .. g_baseUIPostfix] ~= nil and self.values[name .. g_baseUIPostfix] ~= "nil" then
110 ret = self.values[name .. g_baseUIPostfix]
111
112 -- Try definition in the profile
113 elseif self.values[name] ~= nil and self.values[name] ~= "nil" then
114 ret = self.values[name]
115
116 -- Try the profile itself
117 else
118 if self.parent ~= nil then
119 -- Try parent
120 local parentProfile
121 if self.isVariant then
122 parentProfile = self.profiles[self.parent]
123 else
124 -- Follow the path of special variants so top-level variants update all children
125 parentProfile = g_gui:getProfile(self.parent)
126 end
127
128 if parentProfile ~= nil and parentProfile ~= "nil" then
129 ret = parentProfile:getValue(name, default)
130 else
131 print("Warning: Parent-profile '" .. self.parent .. "' not found for profile '" .. self.name .. "'")
132 end
133 end
134 end
135
136 return ret
137end

loadFromXML

Description
Load profile data from XML.
Definition
loadFromXML(xmlFile XML, key Profile, presets Table, isTrait Whether)
Arguments
xmlFileXMLfile handle
keyProfileXML element node path
presetsTableof presets for symbol resolution, {preset name=preset value}
isTraitWhetherthis profile is a trait
Return Values
Trueifprofile values could be loaded, false otherwise.
Code
38function GuiProfile:loadFromXML(xmlFile, key, presets, isTrait, isVariant)
39 local name = getXMLString(xmlFile, key .. "#name")
40 if name == nil then
41 return false
42 end
43
44 self.name = name
45 self.isTrait = isTrait or false
46 self.parent = getXMLString(xmlFile, key .. "#extends")
47 self.isVariant = isVariant
48
49 if self.parent == self.name then
50 error("Profile " .. name .. " extends itself")
51 end
52
53 -- If this is not a trait, resolve traits
54 if not isTrait then
55 local traits = getXMLString(xmlFile, key .. "#with")
56 if traits ~= nil then
57 local traitNames = traits:split(" ")
58
59 -- Copy all values, overwriting previous ones.
60 -- This is resolving of the traits.
61 for i = #traitNames, 1, -1 do
62 local traitName = traitNames[i]
63 local trait = self.traits[traitName]
64
65 if trait ~= nil then
66 for traitValueName, value in pairs(trait.values) do
67 self.values[traitValueName] = value
68 end
69 else
70 print("Warning: Trait-profile '" .. traitName .. "' not found for trait '" .. self.name .. "'")
71 end
72 end
73 end
74 end
75
76 local i = 0
77 while true do
78 local k = key .. ".Value(" .. i .. ")"
79 local valueName = getXMLString(xmlFile, k .. "#name")
80 local value = getXMLString(xmlFile, k .. "#value")
81 if valueName == nil or value == nil then
82 break
83 end
84
85 if value:startsWith("$preset_") then
86 local preset = string.gsub(value, "$preset_", "")
87 if presets[preset] ~= nil then
88 value = presets[preset]
89 else
90 print("Warning: Preset '" .. preset .. "' is not defined in GuiProfile!")
91 end
92 end
93
94 self.values[valueName] = value
95 i = i + 1
96 end
97
98 return true
99end

new

Description
Create a new GuiProfile.
Definition
new(profiles Reference, traits Reference)
Arguments
profilesReferenceto loaded profiles table for inheritance checking.
traitsReferenceto loaded traits table for inheritance checking.
Return Values
NewGuiProfileinstance
Code
19function GuiProfile.new(profiles, traits)
20 local self = setmetatable({}, GuiProfile_mt)
21
22 self.values = {}
23 self.name = ""
24 self.profiles = profiles
25 self.traits = traits
26 self.parent = nil
27
28 return self
29end