forked from JumpyDuke/UniversalProcessKit_FS15
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUPK_Switcher.lua
380 lines (341 loc) · 14.4 KB
/
UPK_Switcher.lua
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
-- by mor2000
--------------------
-- Switcher
local UPK_Switcher_mt = ClassUPK(UPK_Switcher, UniversalProcessKit)
InitObjectClass(UPK_Switcher, "UPK_Switcher")
UniversalProcessKit.addModule("switcher",UPK_Switcher)
UPK_Switcher.MODE_SWITCH = 1
UPK_Switcher.MODE_STACK = 2
UPK_Switcher.MODE_STACKREVERSE = 3
UPK_Switcher.MODE_MATERIAL = 4
UPK_Switcher.MODE_SILENT = 5
function UPK_Switcher:new(nodeId,parent)
printFn('UPK_Switcher:new(',nodeId,', ',parent,')')
local self = UniversalProcessKit:new(nodeId,parent, UPK_Switcher_mt)
registerObjectClassName(self, "UPK_Switcher")
self.hidingPosition = getVectorFromUserAttribute(nodeId, "hidingPosition", "0 -10 0")
self.maxCapacity = 0
self.fillLevelsCopy = {}
-- fill types
self.switchAtFillTypes={}
local switchAtFillTypesArr = getArrayFromUserAttribute(nodeId, "fillTypes")
for _,fillType in pairs(UniversalProcessKit.fillTypeNameToInt(switchAtFillTypesArr)) do
local flbs = self:getFillLevelBubbleShellFromFillType(fillType)
if flbs~=nil and flbs~=self then
flbs:registerOnFillLevelChangeFunc(self,"onFillLevelChange")
end
self:printAll('listening to ',fillType)
self.switchAtFillTypes[fillType] = true
self.fillLevelsCopy[fillType] = self:getFillLevel(fillType)
self.maxCapacity = mathmax(self.maxCapacity, self:getCapacity(fillType) or 0)
self:printAll('fillLevel is ',self:getFillLevel(fillType))
self:printAll('capacity is ',self:getCapacity(fillType))
end
self.fillTypeChoiceMax = getStringFromUserAttribute(nodeId, "fillTypeChoice", "max")=="max"
-- get materials
self.switchMaterials = {}
local nrMaterials = getNumMaterials(nodeId) or 0
for i=1,nrMaterials do
local materialId = getMaterial(nodeId, (i-1))
table.insert(self.switchMaterials,materialId)
end
-- shapes
self.switchFillTypeShapes={}
self.shapePositions={}
self.useFillTypes=false
local fillTypeString = getStringFromUserAttribute(nodeId, "switchFillTypes")
if fillTypeString~=nil then
local modeStr = getStringFromUserAttribute(nodeId, "mode", "switch")
if modeStr=="material" then
self.mode = UPK_Switcher.MODE_MATERIAL
elseif modeStr=="silent" then
self.mode = UPK_Switcher.MODE_SILENT
else
self.mode = UPK_Switcher.MODE_SWITCH
end
if self.mode == UPK_Switcher.MODE_MATERIAL then
self:printInfo("#self.switchMaterials ",#self.switchMaterials)
self:printInfo("#self.maxfillLevelPerShape ",#self.maxfillLevelPerShape)
local fillTypesPerShape=Utils.splitString(",",fillTypeString)
for i=1,mathmin(#self.switchMaterials,#fillTypesPerShape) do
local fillTypesInShape=gmatch(fillTypesPerShape[i], "%S+")
for _,fillType in pairs(UniversalProcessKit.fillTypeNameToInt(fillTypesInShape)) do
local flbs = self:getFillLevelBubbleShellFromFillType(fillType)
if flbs~=nil and flbs~=self then
flbs:registerOnFillLevelChangeFunc(self,"onFillLevelChange")
end
self:printInfo("assigning "..tostring(UniversalProcessKit.fillTypeIntToName[fillType])..' ('..tostring(v)..") to material "..tostring(i))
--self.fillLevelsCopy[fillType] = self:getFillLevel(fillType)
self.switchAtFillTypes[fillType] = true
self.switchFillTypeShapes[fillType] = i
self.useFillTypes=true
end
end
elseif self.mode~=UPK_Switcher.MODE_SILENT then
local fillTypesPerShape=Utils.splitString(",",fillTypeString)
local numChildren = getNumOfChildren(self.nodeId)
for i=1,mathmin(numChildren,#fillTypesPerShape) do
local childId = getChildAt(nodeId, i-1)
setVisibility(childId,false)
self.shapePositions[childId]=__c({getTranslation(childId)})
UniversalProcessKit.setTranslation(childId,unpack(self.shapePositions[childId]+self.hidingPosition))
local fillTypesInShape=gmatch(fillTypesPerShape[i], "%S+")
for _,fillType in pairs(UniversalProcessKit.fillTypeNameToInt(fillTypesInShape)) do
local flbs = self:getFillLevelBubbleShellFromFillType(fillType)
if flbs~=nil and flbs~=self then
flbs:registerOnFillLevelChangeFunc(self,"onFillLevelChange")
end
self:printInfo("assigning "..tostring(UniversalProcessKit.fillTypeIntToName[fillType])..' ('..tostring(v)..") to ".."\""..tostring(getName(childId)).."\" ("..tostring(childId)..")")
--self.fillLevelsCopy[fillType] = self:getFillLevel(fillType)
self.switchAtFillTypes[fillType] = true
self.switchFillTypeShapes[fillType]=childId
self.useFillTypes=true
end
end
else
local fillTypesPerShape=Utils.splitString(",",fillTypeString)
for i=1,#fillTypesPerShape do
local fillTypesInShape=gmatch(fillTypesPerShape[i], "%S+")
for _,fillType in pairs(UniversalProcessKit.fillTypeNameToInt(fillTypesInShape)) do
local flbs = self:getFillLevelBubbleShellFromFillType(fillType)
if flbs~=nil and flbs~=self then
flbs:registerOnFillLevelChangeFunc(self,"onFillLevelChange")
end
self:printInfo("assigning "..tostring(UniversalProcessKit.fillTypeIntToName[fillType])..' ('..tostring(v)..") to dummy "..tostring(i))
--self.fillLevelsCopy[fillType] = self:getFillLevel(fillType)
self.switchAtFillTypes[fillType] = true
self.switchFillTypeShapes[fillType] = i
self.useFillTypes=true
end
end
end
end
self.switchFillLevels={}
self.maxfillLevelPerShape={}
self.useFillLevels=false
local fillLevelString = getStringFromUserAttribute(nodeId, "switchFillLevels")
if fillLevelString~=nil then
local modeStr = getStringFromUserAttribute(nodeId, "mode", "switch")
if modeStr=="stack" then
self.mode = UPK_Switcher.MODE_STACK
elseif modeStr=="stackReverse" then
self.mode = UPK_Switcher.MODE_STACKREVERSE
elseif modeStr=="material" then
self.mode = UPK_Switcher.MODE_MATERIAL
elseif modeStr=="silent" then
self.mode = UPK_Switcher.MODE_SILENT
else
self.mode = UPK_Switcher.MODE_SWITCH
end
local tmparr = Utils.splitString(" ",fillLevelString)
for i=1,#tmparr do
local maxFillLevel=tonumber(tmparr[i])
if maxFillLevel~=nil then
table.insert(self.maxfillLevelPerShape,maxFillLevel)
else
self:printInfo('Warning: couldn\'t convert \"'..tostring(v)..'\" to number')
end
end
table.insert(self.maxfillLevelPerShape,math.huge)
if self.mode == UPK_Switcher.MODE_MATERIAL then
self:printInfo("#self.switchMaterials "..tostring(#self.switchMaterials))
self:printInfo("#self.maxfillLevelPerShape "..tostring(#self.maxfillLevelPerShape))
for i=1,mathmin(#self.switchMaterials,#self.maxfillLevelPerShape) do
-- nothing to do
self:printInfo("assigning max fillLevel of "..tostring(self.maxfillLevelPerShape[i]).." to material "..tostring(self.switchMaterials[i]))
self.useFillLevels=true
end
elseif self.mode~=UPK_Switcher.MODE_SILENT then
local numChildren = mathmin(getNumOfChildren(nodeId),#self.maxfillLevelPerShape)
for i=1,numChildren do
local childId = getChildAt(self.nodeId, i-1)
setVisibility(childId,false)
self.shapePositions[childId]=__c({getTranslation(childId)})
UniversalProcessKit.setTranslation(childId,unpack(self.shapePositions[childId]+self.hidingPosition))
table.insert(self.switchFillLevels,childId)
self:printInfo("assigning max fillLevel of "..tostring(self.maxfillLevelPerShape[i]).." to ".."\""..tostring(getName(childId)).."\" ("..tostring(childId)..")")
self.useFillLevels=true
end
else
self.useFillLevels=true
end
end
if (self.useFillTypes and self.useFillLevels) or (not self.useFillTypes and not self.useFillLevels) then
self:printErr('Error: switcher requires to set either switchFillTypes or switchFillLevels')
return false
end
self:getActionUserAttributes('OnSwitchUp')
self:getActionUserAttributes('OnSwitchDown')
self:printFn('UPK_Switcher:new done')
return self
end
--[[
function UPK_Switcher:delete()
UPK_Switcher:superClass().delete(self)
end
]]--
function UPK_Switcher:postLoad()
self:printFn('UPK_Switcher:postLoad()')
UPK_Switcher:superClass().postLoad(self)
for fillType,_ in pairs(self.fillLevelsCopy) do
local fillLevel = self:getFillLevel(fillType) or 0
self.fillLevelsCopy[fillType] = fillLevel
self:onFillLevelChange(fillLevel, fillLevel, fillType)
end
end;
function UPK_Switcher:onFillLevelChange(deltaFillLevel, newFillLevel, fillType)
self:printFn('UPK_Switcher:onFillLevelChange(',deltaFillLevel,', ',newFillLevel,', ',fillType,')')
if self.switchAtFillTypes[fillType]==true and self.isEnabled then
if self.useFillTypes then
self.fillLevelsCopy[fillType] = newFillLevel -- self:getFillLevel(fillType) -- may not be newFillLevel in fifo or filo
local useFillType=nil
local tmpminfillLevel = math.huge
local tmpmaxfillLevel = 0
for k,v in pairs(self.switchAtFillTypes) do
if v then
local fillLevel = self.fillLevelsCopy[k] or 0
if self.fillTypeChoiceMax then
if fillLevel>tmpmaxfillLevel then
--self:print('new max fill level: '..tostring(fillLevel))
tmpmaxfillLevel=fillLevel
useFillType = k
end
else
if fillLevel<tmpminfillLevel then
tmpminfillLevel=fillLevel
useFillType = k
end
end
end
end
--self:print('usefilltype is '..tostring(useFillType))
if useFillType~=nil then
local shapeToShow=nil
if fillType~=nil and fillType~=UniversalProcessKit.FILLTYPE_UNKNOWN and useFillType~=self.oldFillType then
shapeToShow=self.switchFillTypeShapes[useFillType]
--self:print('shapeToShow is '..tostring(shapeToShow))
end
if shapeToShow~=nil and shapeToShow~=self.oldShapeToShow then
if self.mode==UPK_Switcher.MODE_MATERIAL then
local materialId = self.switchMaterials[shapeToShow]
setMaterial(self.nodeId, materialId)
elseif self.mode~=UPK_Switcher.MODE_SILENT then
if self.oldShapeToShow~=nil then
setVisibility(self.oldShapeToShow,false)
UniversalProcessKit.setTranslation(self.oldShapeToShow,unpack((self.shapePositions[self.oldShapeToShow]+self.hidingPosition) or {}))
end
--self:print('showing '..tostring(shapeToShow))
setVisibility(shapeToShow,true)
local x,y,z=unpack(self.shapePositions[shapeToShow] or {})
if x~=nil and y~=nil and z~=nil then
UniversalProcessKit.setTranslation(shapeToShow,x,y,z)
end
end
if self.oldShapeToShow~=nil then
if shapeToShow>self.oldShapeToShow then
self:printAll('OnSwitchUp')
self:operateAction('OnSwitchUp')
elseif self.oldShapeToShow>shapeToShow then
self:printAll('OnSwitchDown')
self:operateAction('OnSwitchDown')
end
end
self.oldShapeToShow=shapeToShow
end
self.oldFillType=useFillType
else
if self.mode~=UPK_Switcher.MODE_MATERIAL and self.mode~=UPK_Switcher.MODE_SILENT then
if self.oldShapeToShow~=nil then
setVisibility(self.oldShapeToShow,false)
UniversalProcessKit.setTranslation(self.oldShapeToShow,unpack((self.shapePositions[self.oldShapeToShow]+self.hidingPosition) or {}))
self.oldShapeToShow=nil
self.oldFillType=nil
end
end
end
elseif self.useFillLevels then
self.fillLevelsCopy[fillType] = newFillLevel -- self:getFillLevel(fillType) -- may not be newFillLevel in fifo or filo
--self:print('self.fillLevelsCopy[fillType] '..tostring(self.fillLevelsCopy[fillType]))
local fillLevel = 0
if self.fillTypeChoiceMax then
fillLevel = max(self.fillLevelsCopy) or 0
else
fillLevel = min(self.fillLevelsCopy) or 0
end
if fillLevel ~= self.currentFillLevel then
self:printAll('fillLevel ~= self.currentFillLevel')
local newShapeToShow=self:getShapeFromFillLevel(fillLevel)
self:printAll('self.oldShapeToShow ',self.oldShapeToShow)
self:printAll('newShapeToShow ',newShapeToShow)
if newShapeToShow~=nil and newShapeToShow~=self.oldShapeToShow then
if self.mode==UPK_Switcher.MODE_MATERIAL then
local materialId = self.switchMaterials[newShapeToShow]
self:printAll('set to materialId ',materialId)
setMaterial(self.nodeId, materialId)
elseif self.mode~=UPK_Switcher.MODE_SILENT then
local newShapeId=self.switchFillLevels[newShapeToShow]
setVisibility(newShapeId,true)
UniversalProcessKit.setTranslation(newShapeId,unpack((self.shapePositions[newShapeId]) or {}))
if self.oldShapeToShow~=nil then
if self.mode==UPK_Switcher.MODE_SWITCH then
local shapeId=self.switchFillLevels[self.oldShapeToShow]
setVisibility(shapeId,false)
UniversalProcessKit.setTranslation(shapeId,unpack((self.shapePositions[shapeId]+self.hidingPosition) or {}))
elseif self.mode==UPK_Switcher.MODE_STACK then
if newShapeToShow<self.oldShapeToShow then
for i=self.oldShapeToShow,(newShapeToShow+1),-1 do
local shapeId=self.switchFillLevels[i]
setVisibility(shapeId,false)
UniversalProcessKit.setTranslation(shapeId,unpack((self.shapePositions[shapeId]+self.hidingPosition) or {}))
end
end
elseif self.mode==UPK_Switcher.MODE_STACKREVERSE then
if newShapeToShow>self.oldShapeToShow then
for i=self.oldShapeToShow,(newShapeToShow-1) do
local shapeId=self.switchFillLevels[i]
setVisibility(shapeId,false)
UniversalProcessKit.setTranslation(shapeId,unpack((self.shapePositions[shapeId]+self.hidingPosition) or {}))
end
end
end
else
if self.mode==UPK_Switcher.MODE_STACK then
for i=1,(newShapeToShow-1) do
local shapeId=self.switchFillLevels[i]
setVisibility(shapeId,true)
UniversalProcessKit.setTranslation(shapeId,unpack((self.shapePositions[shapeId]) or {}))
end
elseif self.mode==UPK_Switcher.MODE_STACKREVERSE then
for i=getShapeFromFillLevel(math.huge),(newShapeToShow+1),-1 do
local shapeId=self.switchFillLevels[i]
setVisibility(shapeId,true)
UniversalProcessKit.setTranslation(shapeId,unpack((self.shapePositions[shapeId]) or {}))
end
end
end
end
if self.oldShapeToShow~=nil then
if newShapeToShow>self.oldShapeToShow then
self:printAll('OnSwitchUp')
self:operateAction('OnSwitchUp')
elseif self.oldShapeToShow>newShapeToShow then
self:printAll('OnSwitchDown')
self:operateAction('OnSwitchDown')
end
end
self.oldShapeToShow=newShapeToShow
end
self.currentFillLevel=fillLevel
end
end
end
end
function UPK_Switcher:getShapeFromFillLevel(fillLevel)
self:printFn('UPK_Switcher:getShapeFromFillLevel(',fillLevel,')')
for i=1,#self.maxfillLevelPerShape do
if fillLevel<self.maxfillLevelPerShape[i] then
return i
end
end
return nil
end