forked from spring/spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidgets.lua
2153 lines (1780 loc) · 49.2 KB
/
widgets.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: widgets.lua
-- brief: the widget manager, a call-in router
-- author: Dave Rodgers
--
-- Copyright (C) 2007.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function pwl() -- ??? (print widget list)
for k,v in ipairs(widgetHandler.widgets) do
print(k, v.whInfo.layer, v.whInfo.name)
end
end
include("keysym.h.lua")
include("utils.lua")
include("system.lua")
include("callins.lua")
include("savetable.lua")
local gl = gl
local CONFIG_FILENAME = LUAUI_DIRNAME .. 'Config/' .. Game.modShortName .. '.lua'
local WIDGET_DIRNAME = LUAUI_DIRNAME .. 'Widgets/'
local SELECTOR_BASENAME = 'selector.lua'
local SAFEWRAP = 1
-- 0: disabled
-- 1: enabled, but can be overriden by widget.GetInfo().unsafe
-- 2: always enabled
local SAFEDRAW = false -- requires SAFEWRAP to work
local glPopAttrib = gl.PopAttrib
local glPushAttrib = gl.PushAttrib
local section = 'widgets.lua'
--------------------------------------------------------------------------------
-- install bindings for TweakMode and the Widget Selector
Spring.SendCommands({
"unbindkeyset Any+f11",
"unbindkeyset Ctrl+f11",
"bind f11 luaui selector",
"bind C+f11 luaui tweakgui",
"echo LuaUI: bound F11 to the widget selector",
"echo LuaUI: bound CTRL+F11 to tweak mode"
})
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- the widgetHandler object
--
widgetHandler = {
widgets = {},
configData = {},
orderList = {},
knownWidgets = {},
knownCount = 0,
knownChanged = true,
commands = {},
customCommands = {},
inCommandsChanged = false,
autoUserWidgets = true,
actionHandler = include("actions.lua"),
WG = {}, -- shared table for widgets
globals = {}, -- global vars/funcs
mouseOwner = nil,
ownedButton = 0,
tweakMode = false,
tweakKeys = {},
xViewSize = 1,
yViewSize = 1,
xViewSizeOld = 1,
yViewSizeOld = 1,
}
-- these call-ins are set to 'nil' if not used
-- they are setup in UpdateCallIns()
local flexCallIns = {
'GamePreload',
'GameStart',
'GameOver',
'GamePaused',
'GameFrame',
'GameProgress',
'GameSetup',
'TeamDied',
'TeamChanged',
'SyncedPlayerChanged',
'PlayerChanged',
'PlayerAdded',
'PlayerRemoved',
'ShockFront',
'WorldTooltip',
'MapDrawCmd',
'DefaultCommand',
'UnitCreated',
'UnitFinished',
'UnitFromFactory',
'UnitReverseBuilt',
'UnitDestroyed',
'RenderUnitDestroyed',
'UnitTaken',
'UnitGiven',
'UnitIdle',
'UnitCommand',
'UnitCmdDone',
'UnitDamaged',
'UnitStunned',
'UnitEnteredRadar',
'UnitEnteredLos',
'UnitLeftRadar',
'UnitLeftLos',
'UnitEnteredWater',
'UnitEnteredAir',
'UnitLeftWater',
'UnitLeftAir',
'UnitSeismicPing',
'UnitLoaded',
'UnitUnloaded',
'UnitCloaked',
'UnitDecloaked',
'UnitMoveFailed',
'UnitHarvestStorageFull',
'RecvLuaMsg',
'StockpileChanged',
'DrawGenesis',
'DrawWorld',
'DrawWorldPreUnit',
'DrawWorldPreParticles',
'DrawWorldShadow',
'DrawWorldReflection',
'DrawWorldRefraction',
'DrawGroundPreForward',
'DrawGroundPostForward',
'DrawGroundPreDeferred',
'DrawGroundPostDeferred',
'DrawUnitsPostDeferred',
'DrawFeaturesPostDeferred',
'DrawScreenEffects',
'DrawScreenPost',
'DrawInMiniMap',
'SunChanged',
'RecvSkirmishAIMessage',
}
local flexCallInMap = {}
for _,ci in ipairs(flexCallIns) do
flexCallInMap[ci] = true
end
local callInLists = {
'GamePreload',
'GameStart',
'Shutdown',
'Update',
'TextCommand',
'CommandNotify',
'AddConsoleLine',
'ViewResize',
'DrawScreen',
'KeyPress',
'KeyRelease',
'MousePress',
'MouseWheel',
'JoyAxis',
'JoyHat',
'JoyButtonDown',
'JoyButtonUp',
'IsAbove',
'GetTooltip',
'GroupChanged',
'CommandsChanged',
'TweakMousePress',
'TweakMouseWheel',
'TweakIsAbove',
'TweakGetTooltip',
'RecvFromSynced',
'TextInput',
"TextEditing",
'DownloadQueued',
'DownloadStarted',
'DownloadFinished',
'DownloadFailed',
'DownloadProgress',
-- these use mouseOwner instead of lists
-- 'MouseMove',
-- 'MouseRelease',
-- 'TweakKeyPress',
-- 'TweakKeyRelease',
-- 'TweakMouseMove',
-- 'TweakMouseRelease',
-- uses the DrawScreenList
-- 'TweakDrawScreen',
}
-- append the flex call-ins
for _,uci in ipairs(flexCallIns) do
table.insert(callInLists, uci)
end
-- initialize the call-in lists
do
for _,listname in ipairs(callInLists) do
widgetHandler[listname..'List'] = {}
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- Reverse integer iterator for drawing
--
local function rev_iter(t, key)
if (key <= 1) then
return nil
else
local nkey = key - 1
return nkey, t[nkey]
end
end
local function ripairs(t)
return rev_iter, t, (1 + #t)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widgetHandler:LoadConfigData()
local chunk, err = loadfile(CONFIG_FILENAME)
if (chunk == nil) or (chunk() == nil) or (err) then
if err then
Spring.Log(section, LOG.ERROR, err)
end
return
end
local tmp = {math = {huge = math.huge}}
setfenv(chunk, tmp)
self.orderList = chunk().order
self.configData = chunk().data
if (not self.orderList) then
self.orderList = {} -- safety
end
if (not self.configData) then
self.configData = {} -- safety
end
end
function widgetHandler:SaveConfigData()
-- self:LoadConfigData()
local filetable = {}
for i,w in ipairs(self.widgets) do
if (w.GetConfigData) then
self.configData[w.whInfo.name] = select(2, pcall(w.GetConfigData))
end
self.orderList[w.whInfo.name] = i
end
filetable.order = self.orderList
filetable.data = self.configData
table.save( filetable, CONFIG_FILENAME, '-- Widget Custom data and order, order = 0 disabled widget')
end
function widgetHandler:SendConfigData()
self:LoadConfigData()
for i,w in ipairs(self.widgets) do
local data = self.configData[w.whInfo.name]
if (w.SetConfigData and data) then
w:SetConfigData(data)
end
end
end
--------------------------------------------------------------------------------
local function GetWidgetInfo(name, mode)
do return end -- FIXME
local lines = VFS.LoadFile(name, mode)
local infoLines = {}
for line in lines:gmatch('([^\n]*)\n') do
if (not line:find('^%s*%-%-')) then
if (line:find('[^\r]')) then
break -- not commented, not a blank line
end
end
local s, e, source = line:find('^%s*%-%-%>%>(.*)')
if (source) then
table.insert(infoLines, source)
end
end
local info = {}
local chunk, err = loadstring(table.concat(infoLines, '\n'))
if (not chunk) then
Spring.Log(section, LOG.INFO, 'not loading ' .. name .. ': ' .. err)
else
setfenv(chunk, info)
local success, err = pcall(chunk)
if (not success) then
Spring.Log(section, LOG.INFO, 'not loading ' .. name .. ': ' .. tostring(err))
end
end
for k,v in pairs(info) do
Spring.Log(section, LOG.INFO, name, k, 'type: ' .. type(v), '<'..tostring(v)..'>')
end
end
function widgetHandler:Initialize()
self:LoadConfigData()
local autoUserWidgets = Spring.GetConfigInt('LuaAutoEnableUserWidgets', 1)
self.autoUserWidgets = (autoUserWidgets ~= 0)
-- create the "LuaUI/Config" directory
Spring.CreateDir(LUAUI_DIRNAME .. 'Config')
local unsortedWidgets = {}
-- stuff the raw widgets into unsortedWidgets
local widgetFiles = VFS.DirList(WIDGET_DIRNAME, "*.lua", VFS.RAW_ONLY)
for k,wf in ipairs(widgetFiles) do
GetWidgetInfo(wf, VFS.RAW_ONLY)
local widget = self:LoadWidget(wf, false)
if (widget) then
table.insert(unsortedWidgets, widget)
end
end
-- stuff the zip widgets into unsortedWidgets
local widgetFiles = VFS.DirList(WIDGET_DIRNAME, "*.lua", VFS.ZIP_ONLY)
for k,wf in ipairs(widgetFiles) do
GetWidgetInfo(wf, VFS.ZIP_ONLY)
local widget = self:LoadWidget(wf, true)
if (widget) then
table.insert(unsortedWidgets, widget)
end
end
-- sort the widgets
table.sort(unsortedWidgets, function(w1, w2)
local l1 = w1.whInfo.layer
local l2 = w2.whInfo.layer
if (l1 ~= l2) then
return (l1 < l2)
end
local n1 = w1.whInfo.name
local n2 = w2.whInfo.name
local o1 = self.orderList[n1]
local o2 = self.orderList[n2]
if (o1 ~= o2) then
return (o1 < o2)
else
return (n1 < n2)
end
end)
-- add the widgets
for _,w in ipairs(unsortedWidgets) do
local name = w.whInfo.name
local basename = w.whInfo.basename
local source = self.knownWidgets[name].fromZip and "mod: " or "user:"
Spring.Log(section, LOG.INFO, string.format("Loading widget from %s %-18s <%s> ...", source, name, basename))
widgetHandler:InsertWidget(w)
end
-- save the active widgets, and their ordering
self:SaveConfigData()
end
function widgetHandler:LoadWidget(filename, fromZip)
local basename = Basename(filename)
local text = VFS.LoadFile(filename)
if (text == nil) then
Spring.Log(section, LOG.ERROR, 'Failed to load: ' .. basename .. ' (missing file: ' .. filename ..')')
return nil
end
local chunk, err = loadstring(text, filename)
if (chunk == nil) then
Spring.Log(section, LOG.ERROR, 'Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
local widget = widgetHandler:NewWidget()
setfenv(chunk, widget)
local success, err = pcall(chunk)
if (not success) then
Spring.Log(section, LOG.ERROR, 'Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
if (err == false) then
return nil -- widget asked for a silent death
end
-- raw access to widgetHandler
if (widget.GetInfo and widget:GetInfo().handler) then
widget.widgetHandler = self
end
self:FinalizeWidget(widget, filename, basename)
local name = widget.whInfo.name
if (basename == SELECTOR_BASENAME) then
self.orderList[name] = 1 -- always enabled
end
err = self:ValidateWidget(widget)
if (err) then
Spring.Log(section, LOG.ERROR, 'Failed to load: ' .. basename .. ' (' .. err .. ')')
return nil
end
local knownInfo = self.knownWidgets[name]
if (knownInfo) then
if (knownInfo.active) then
Spring.Log(section, LOG.ERROR, 'Failed to load: ' .. basename .. ' (duplicate name)')
return nil
end
else
-- create a knownInfo table
knownInfo = {}
knownInfo.desc = widget.whInfo.desc
knownInfo.author = widget.whInfo.author
knownInfo.basename = widget.whInfo.basename
knownInfo.filename = widget.whInfo.filename
knownInfo.enabled = widget.whInfo.enabled
knownInfo.fromZip = fromZip
self.knownWidgets[name] = knownInfo
self.knownCount = self.knownCount + 1
self.knownChanged = true
end
knownInfo.active = true
if (widget.GetInfo == nil) then
Spring.Log(section, LOG.ERROR, 'Failed to load: ' .. basename .. ' (no GetInfo() call)')
return nil
end
local info = widget:GetInfo()
local order = self.orderList[name]
if (((order ~= nil) and (order > 0)) or
((order == nil) and -- unknown widget
(info.enabled and (knownInfo.fromZip or self.autoUserWidgets)))) then
-- this will be an active widget
if (order == nil) then
self.orderList[name] = 12345 -- back of the pack
else
self.orderList[name] = order
end
else
self.orderList[name] = 0
self.knownWidgets[name].active = false
return nil
end
-- load the config data
local config = self.configData[name]
if (widget.SetConfigData and config) then
widget:SetConfigData(config)
end
return widget
end
function widgetHandler:NewWidget()
local widget = {}
if (true) then
-- copy the system calls into the widget table
for k,v in pairs(System) do
widget[k] = v
end
widget["SendToUnsynced"] = Spring.SendToUnsynced
else
-- use metatable redirection
setmetatable(widget, {
__index = System,
__metatable = true,
})
end
widget._G = _G -- the global table
widget.WG = self.WG -- the shared table
widget.widget = widget -- easy self referencing
-- wrapped calls (closures)
widget.widgetHandler = {}
local wh = widget.widgetHandler
local self = self
widget.include = function (f) return include(f, widget) end
wh.ForceLayout = function (_) self:ForceLayout() end
wh.RaiseWidget = function (_) self:RaiseWidget(widget) end
wh.LowerWidget = function (_) self:LowerWidget(widget) end
wh.RemoveWidget = function (_) self:RemoveWidget(widget) end
wh.GetCommands = function (_) return self.commands end
wh.InTweakMode = function (_) return self.tweakMode end
wh.GetViewSizes = function (_) return self:GetViewSizes() end
wh.GetHourTimer = function (_) return self:GetHourTimer() end
wh.IsMouseOwner = function (_) return (self.mouseOwner == widget) end
wh.DisownMouse = function (_)
if (self.mouseOwner == widget) then
self.mouseOwner = nil
end
end
wh.UpdateCallIn = function (_, name)
self:UpdateWidgetCallIn(name, widget)
end
wh.RemoveCallIn = function (_, name)
self:RemoveWidgetCallIn(name, widget)
end
wh.AddAction = function (_, cmd, func, data, types)
return self.actionHandler:AddAction(widget, cmd, func, data, types)
end
wh.RemoveAction = function (_, cmd, types)
return self.actionHandler:RemoveAction(widget, cmd, types)
end
wh.AddSyncAction = function(_, cmd, func, help)
return self.actionHandler:AddSyncAction(widget, cmd, func, help)
end
wh.RemoveSyncAction = function(_, cmd)
return self.actionHandler:RemoveSyncAction(widget, cmd)
end
wh.AddLayoutCommand = function (_, cmd)
if (self.inCommandsChanged) then
table.insert(self.customCommands, cmd)
else
Spring.Log(section, LOG.ERROR, "AddLayoutCommand() can only be used in CommandsChanged()")
end
end
wh.RegisterGlobal = function(_, name, value)
return self:RegisterGlobal(widget, name, value)
end
wh.DeregisterGlobal = function(_, name)
return self:DeregisterGlobal(widget, name)
end
wh.SetGlobal = function(_, name, value)
return self:SetGlobal(widget, name, value)
end
wh.ConfigLayoutHandler = function(_, d) self:ConfigLayoutHandler(d) end
return widget
end
function widgetHandler:AddWidgetSyncAction(widget, cmd, func, help)
return self.actionHandler:AddSyncAction(widget, cmd, func, help)
end
function widgetHandler:RemoveWidgetSyncAction(widget, cmd)
return self.actionHandler:RemoveSyncAction(widget, cmd)
end
function widgetHandler:FinalizeWidget(widget, filename, basename)
local wi = {}
wi.filename = filename
wi.basename = basename
if (widget.GetInfo == nil) then
wi.name = basename
wi.layer = 0
else
local info = widget:GetInfo()
wi.name = info.name or basename
wi.layer = info.layer or 0
wi.desc = info.desc or ""
wi.author = info.author or ""
wi.license = info.license or ""
wi.enabled = info.enabled or false
end
widget.whInfo = {} -- a proxy table
local mt = {
__index = wi,
__newindex = function() error("whInfo tables are read-only") end,
__metatable = "protected"
}
setmetatable(widget.whInfo, mt)
end
function widgetHandler:ValidateWidget(widget)
if (widget.GetTooltip and not widget.IsAbove) then
return "Widget has GetTooltip() but not IsAbove()"
end
if (widget.TweakGetTooltip and not widget.TweakIsAbove) then
return "Widget has TweakGetTooltip() but not TweakIsAbove()"
end
return nil
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function SafeWrapFuncNoGL(func, funcName)
local wh = widgetHandler
return function(w, ...)
local r = { pcall(func, w, ...) }
if (r[1]) then
table.remove(r, 1)
return unpack(r)
else
if (funcName ~= 'Shutdown') then
widgetHandler:RemoveWidget(w)
else
Spring.Log(section, LOG.ERROR, 'Error in Shutdown()')
end
local name = w.whInfo.name
Spring.Log(section, LOG.ERROR, r[1])
Spring.Log(section, LOG.ERROR, 'Error in ' .. funcName ..'(): ' .. tostring(r[2]))
Spring.Log(section, LOG.ERROR, 'Removed widget: ' .. name)
return nil
end
end
end
local function SafeWrapFuncGL(func, funcName)
local wh = widgetHandler
return function(w, ...)
glPushAttrib(GL.ALL_ATTRIB_BITS)
local r = { pcall(func, w, ...) }
glPopAttrib()
if (r[1]) then
table.remove(r, 1)
return unpack(r)
else
if (funcName ~= 'Shutdown') then
widgetHandler:RemoveWidget(w)
else
Spring.Log(section, LOG.ERROR, 'Error in Shutdown()')
end
local name = w.whInfo.name
Spring.Log(section, LOG.ERROR, 'Error in ' .. funcName ..'(): ' .. tostring(r[2]))
Spring.Log(section, LOG.ERROR, 'Removed widget: ' .. name)
return nil
end
end
end
local function SafeWrapFunc(func, funcName)
if (not SAFEDRAW) then
return SafeWrapFuncNoGL(func, funcName)
else
if (string.sub(funcName, 1, 4) ~= 'Draw') then
return SafeWrapFuncNoGL(func, funcName)
else
return SafeWrapFuncGL(func, funcName)
end
end
end
local function SafeWrapWidget(widget)
if (SAFEWRAP <= 0) then
return
elseif (SAFEWRAP == 1) then
if (widget.GetInfo and widget.GetInfo().unsafe) then
Spring.LOG(section, LOG.INFO, 'LuaUI: loaded unsafe widget: ' .. widget.whInfo.name)
return
end
end
for _,ciName in ipairs(callInLists) do
if (widget[ciName]) then
widget[ciName] = SafeWrapFunc(widget[ciName], ciName)
end
if (widget.Initialize) then
widget.Initialize = SafeWrapFunc(widget.Initialize, 'Initialize')
end
end
end
--------------------------------------------------------------------------------
local function ArrayInsert(t, f, w)
if (f) then
local layer = w.whInfo.layer
local index = 1
for i,v in ipairs(t) do
if (v == w) then
return -- already in the table
end
if (layer >= v.whInfo.layer) then
index = i + 1
end
end
table.insert(t, index, w)
end
end
local function ArrayRemove(t, w)
for k,v in ipairs(t) do
if (v == w) then
table.remove(t, k)
-- break
end
end
end
function widgetHandler:InsertWidget(widget)
if (widget == nil) then
return
end
SafeWrapWidget(widget)
ArrayInsert(self.widgets, true, widget)
for _,listname in ipairs(callInLists) do
local func = widget[listname]
if (type(func) == 'function') then
ArrayInsert(self[listname..'List'], func, widget)
end
end
self:UpdateCallIns()
if (widget.Initialize) then
widget:Initialize()
end
end
function widgetHandler:RemoveWidget(widget)
if (widget == nil) then
return
end
local name = widget.whInfo.name
if (widget.GetConfigData) then
self.configData[name] = widget:GetConfigData()
end
self.knownWidgets[name].active = false
if (widget.Shutdown) then
widget:Shutdown()
end
ArrayRemove(self.widgets, widget)
self:RemoveWidgetGlobals(widget)
self.actionHandler:RemoveWidgetActions(widget)
for _,listname in ipairs(callInLists) do
ArrayRemove(self[listname..'List'], widget)
end
self:UpdateCallIns()
if (widget.whInfo.basename == SELECTOR_BASENAME) then
Spring.SendCommands({"luaui update"})
end
end
--------------------------------------------------------------------------------
function widgetHandler:UpdateCallIn(name)
local listName = name .. 'List'
if ((name == 'Update') or
(name == 'DrawScreen')) then
return
end
if ((#self[listName] > 0) or
(not flexCallInMap[name]) or
((name == 'GotChatMsg') and actionHandler.HaveChatAction()) or
((name == 'RecvFromSynced'))) then
-- always assign these call-ins
local selffunc = self[name]
_G[name] = function(...)
return selffunc(self, ...)
end
else
_G[name] = nil
end
Script.UpdateCallIn(name)
end
function widgetHandler:UpdateWidgetCallIn(name, w)
local listName = name .. 'List'
local ciList = self[listName]
if (ciList) then
local func = w[name]
if (type(func) == 'function') then
ArrayInsert(ciList, func, w)
else
ArrayRemove(ciList, w)
end
self:UpdateCallIn(name)
else
Spring.Log(section, LOG.ERROR, 'UpdateWidgetCallIn: bad name: ' .. name)
end
end
function widgetHandler:RemoveWidgetCallIn(name, w)
local listName = name .. 'List'
local ciList = self[listName]
if (ciList) then
ArrayRemove(ciList, w)
self:UpdateCallIn(name)
else
Spring.Log(section, LOG.ERROR, 'RemoveWidgetCallIn: bad name: ' .. name)
end
end
function widgetHandler:UpdateCallIns()
for _,name in ipairs(callInLists) do
self:UpdateCallIn(name)
end
end
function widgetHandler:SelectorActive()
for _,w in ipairs(self.widgets) do
if (w.whInfo.basename == SELECTOR_BASENAME) then
return true
end
end
return false
end
--------------------------------------------------------------------------------
function widgetHandler:EnableWidget(name)
local ki = self.knownWidgets[name]
if (not ki) then
Spring.Log(section, LOG.ERROR, "EnableWidget(), could not find widget: " .. tostring(name))
return false
end
if (not ki.active) then
Spring.Log(section, LOG.INFO, 'Loading: '..ki.filename)
local order = widgetHandler.orderList[name]
if (not order or (order <= 0)) then
self.orderList[name] = 1
end
local w = self:LoadWidget(ki.filename)
if (not w) then return false end
self:InsertWidget(w)
self:SaveConfigData()
end
if (not self:SelectorActive()) then
Spring.SendCommands({"luaui update"})
end
return true
end
function widgetHandler:DisableWidget(name)
local ki = self.knownWidgets[name]
if (not ki) then
Spring.Log(section, LOG.ERROR, "DisableWidget(), could not find widget: " .. tostring(name))
return false
end
if (ki.active) then
local w = self:FindWidget(name)
if (not w) then return false end
Spring.Log(section, LOG.INFO, 'Removed: '..ki.filename)
self:RemoveWidget(w) -- deactivate
self.orderList[name] = 0 -- disable
self:SaveConfigData()
end
if (not self:SelectorActive()) then
Spring.SendCommands({"luaui update"})
end
return true
end
function widgetHandler:ToggleWidget(name)
local ki = self.knownWidgets[name]
if (not ki) then
Spring.Log(section, LOG.ERROR, "ToggleWidget(), could not find widget: " .. tostring(name))
return
end
if (ki.active) then
return self:DisableWidget(name)
elseif (self.orderList[name] <= 0) then
return self:EnableWidget(name)
else
-- the widget is not active, but enabled; disable it
self.orderList[name] = 0
self:SaveConfigData()
end
return true
end
--------------------------------------------------------------------------------
local function FindWidgetIndex(t, w)
for k,v in ipairs(t) do
if (v == w) then
return k
end
end
return nil
end
local function FindLowestIndex(t, i, layer)
for x = (i - 1), 1, -1 do
if (t[x].whInfo.layer < layer) then
return x + 1
end
end
return 1
end
function widgetHandler:RaiseWidget(widget)
if (widget == nil) then
return
end
local function Raise(t, f, w)
if (f == nil) then return end
local i = FindWidgetIndex(t, w)
if (i == nil) then return end
local n = FindLowestIndex(t, i, w.whInfo.layer)
if (n and (n < i)) then
table.remove(t, i)
table.insert(t, n, w)
end
end
Raise(self.widgets, true, widget)
for _,listname in ipairs(callInLists) do
Raise(self[listname..'List'], widget[listname], widget)
end
end
local function FindHighestIndex(t, i, layer)
local ts = #t
for x = (i + 1),ts do
if (t[x].whInfo.layer > layer) then
return (x - 1)
end
end
return (ts + 1)
end
function widgetHandler:LowerWidget(widget)
if (widget == nil) then
return