-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDashboardValueType.lua
More file actions
183 lines (123 loc) · 4.09 KB
/
DashboardValueType.lua
File metadata and controls
183 lines (123 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
---Animation value with one or multiple floats for AnimatedVehicle
local DashboardValueType_mt = Class(DashboardValueType)
---
function DashboardValueType.new(specName, name, customMt)
local self = setmetatable({}, customMt or DashboardValueType_mt)
self.specName = specName
self.name = name
self.fullName = specName .. "." .. name
self.valueObject = nil
self.loadFunction = nil
self.stateFunction = nil
self.valueFactor = 1
self.valueCompare = nil
self.idleValue = nil
self.functions = {}
self.value, self.min, self.max = 0, 0, 1
return self
end
---
function DashboardValueType:setXMLKey(xmlKey)
self.xmlKey = xmlKey
end
---
function DashboardValueType:loadFromXML(xmlFile, vehicle)
if self.xmlKey ~= nil then
vehicle:loadDashboardsFromXML(xmlFile, self.xmlKey, self)
else
local rootName = xmlFile:getRootName()
vehicle:loadDashboardsFromXML(xmlFile, rootName .. "." .. self.specName .. ".dashboards", self)
end
end
---
function DashboardValueType:setAdditionalFunctions(loadFunction, stateFunction)
self.loadFunction = loadFunction
self.stateFunction = stateFunction
end
---
function DashboardValueType:setValueFactor(valueFactor)
self.valueFactor = valueFactor
end
---
function DashboardValueType:setValueCompare(...)
self.valueCompare = {}
for i = 1, select("#", ...) do
self.valueCompare[select(i, ...)] = true
end
end
---
function DashboardValueType:setIdleValue(idleValue)
self.idleValue = idleValue
end
---
function DashboardValueType:setValue(object, func)
self:setFunction("value", object, func)
end
---
function DashboardValueType:setCenter(centerFunc)
self:setFunction("center", self.valueObject, centerFunc)
end
---
function DashboardValueType:setRange(min, max)
self:setFunction("min", self.valueObject, min)
self:setFunction("max", self.valueObject, max)
end
---
function DashboardValueType:setInterpolationSpeed(interpolationSpeed)
self:setFunction("interpolationSpeed", self.valueObject, interpolationSpeed)
end
---
function DashboardValueType:getInterpolationSpeed(dashboard)
return self:getFunctionValue("interpolationSpeed", dashboard)
end
---
function DashboardValueType:getValue(dashboard)
local value = self:getFunctionValue("value", dashboard)
if self.valueCompare ~= nil then
value = self.valueCompare[value] == true
end
local isNumber = type(value) == "number"
local min, max, center
if isNumber then
if self.valueFactor ~= nil then
value = value * self.valueFactor
end
min = self:getFunctionValue("min", dashboard)
max = self:getFunctionValue("max", dashboard)
center = self:getFunctionValue("center", dashboard)
end
return value, min, max, center, isNumber
end
---
function DashboardValueType:setFunction(id, object, func)
self.valueObject = object
local data = {}
data.valueObject = object
data.valueFunction = func
data.isValue = type(data.valueFunction) == "number" or type(data.valueFunction) == "boolean"
data.isFunction = type(data.valueFunction) == "function"
data.isString = type(data.valueFunction) == "string"
if data.isString then
data.isSubValue = type(data.valueObject[data.valueFunction]) == "number" or type(data.valueObject[data.valueFunction]) == "boolean"
data.isSubFunction = type(data.valueObject[data.valueFunction]) == "function"
end
self.functions[id] = data
end
---
function DashboardValueType:getFunctionValue(id, dashboard)
local data = self.functions[id]
if data ~= nil then
if data.isValue then
return data.valueFunction
elseif data.isFunction then
return data.valueFunction(data.valueObject, dashboard)
elseif data.isString then
if data.isSubValue then
return data.valueObject[data.valueFunction]
elseif data.isSubFunction then
return data.valueObject[data.valueFunction](data.valueObject, dashboard)
end
end
end
return nil
end