-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibEditMode.lua
More file actions
897 lines (735 loc) · 31.6 KB
/
LibEditMode.lua
File metadata and controls
897 lines (735 loc) · 31.6 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
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
local MINOR = 15
local _, ns = ...
local lib
if ns.LibEditMode then
lib = ns.LibEditMode
else
lib = LibStub:NewLibrary('LibEditMode', MINOR)
if not lib then
-- this or a newer version is already loaded
return
end
end
lib.internal = {} -- internal methods, do not use directly
local internal = lib.internal
-- keep a variable stored for the latest version, used to avoid hooks and events
-- firing for older versions of the library when it has been "globally" upgraded with LibStub
lib.hookVersion = MINOR
lib.frameSelections = lib.frameSelections or {}
lib.frameCallbacks = lib.frameCallbacks or {}
lib.frameDefaults = lib.frameDefaults or {}
lib.frameSettings = lib.frameSettings or {}
lib.frameButtons = lib.frameButtons or {}
lib.anonCallbacksEnter = lib.anonCallbacksEnter or {}
lib.anonCallbacksExit = lib.anonCallbacksExit or {}
lib.anonCallbacksLayout = lib.anonCallbacksLayout or {}
lib.anonCallbacksCreate = lib.anonCallbacksCreate or {}
lib.anonCallbacksRename = lib.anonCallbacksRename or {}
lib.anonCallbacksDelete = lib.anonCallbacksDelete or {}
lib.systemSettings = lib.systemSettings or {}
lib.subSystemSettings = lib.subSystemSettings or {}
lib.systemButtons = lib.systemButtons or {}
lib.subSystemButtons = lib.subSystemButtons or {}
lib.layoutCache = lib.layoutCache or {}
local layoutNames = setmetatable({'Modern', 'Classic'}, {
__index = function(t, key)
if key > 2 then
-- the first 2 indices are reserved for 'Modern' and 'Classic' layouts, and anything
-- else are custom ones, although GetLayouts() doesn't return data for the 'Modern'
-- and 'Classic' layouts, so we'll have to substract and check
local layouts = lib.layoutCache
if (key - 2) <= #layouts then
return layouts[key - 2].layoutName
end
else
-- also work for 'Modern' and 'Classic'
rawget(t, key)
end
end
})
local function resetDialogs()
if internal.dialog then
internal.dialog:Hide()
end
if internal.extension then
internal.extension:Hide()
end
end
local function resetSelection()
for frame, selection in next, lib.frameSelections do
if selection.isSelected then
frame:SetMovable(false)
end
if not lib.isEditing then
selection:Hide()
selection.isSelected = false
else
selection:ShowHighlighted()
end
end
end
local function onDragStart(self)
if InCombatLockdown() then
-- TODO: maybe add a warning?
return
end
self:RegisterEvent('PLAYER_REGEN_DISABLED')
self.parent:StartMoving()
end
local function normalizePosition(frame)
-- ripped out of LibWindow-1.1, which is Public Domain
local parent = frame:GetParent()
if not parent then
return
end
local scale = frame:GetScale()
if not scale then
return
end
local left = frame:GetLeft() * scale
local top = frame:GetTop() * scale
local right = frame:GetRight() * scale
local bottom = frame:GetBottom() * scale
local parentWidth, parentHeight = parent:GetSize()
local x, y, point
if left < (parentWidth - right) and left < math.abs((left + right) / 2 - parentWidth / 2) then
x = left
point = 'LEFT'
elseif (parentWidth - right) < math.abs((left + right) / 2 - parentWidth / 2) then
x = right - parentWidth
point = 'RIGHT'
else
x = (left + right) / 2 - parentWidth / 2
point = ''
end
if bottom < (parentHeight - top) and bottom < math.abs((bottom + top) / 2 - parentHeight / 2) then
y = bottom
point = 'BOTTOM' .. point
elseif (parentHeight - top) < math.abs((bottom + top) / 2 - parentHeight / 2) then
y = top - parentHeight
point = 'TOP' .. point
else
y = (bottom + top) / 2 - parentHeight / 2
point = '' .. point
end
if point == '' then
point = 'CENTER'
end
return point, x / scale, y / scale
end
local function updatePosition(selection, xDelta, yDelta)
if InCombatLockdown() then
-- TODO: maybe add a warning?
return
end
local parent = selection.parent
local point, x, y = normalizePosition(parent)
x, y = x + (xDelta or 0), y + (yDelta or 0)
parent:ClearAllPoints()
parent:SetPoint(point, x, y)
internal:TriggerCallback(parent, point, x, y)
if selection.isSelected then
internal.dialog:Update(selection)
end
end
local function onDragStop(self)
if InCombatLockdown() then
return
end
local parent = self.parent
parent:StopMovingOrSizing()
self:UnregisterEvent('PLAYER_REGEN_DISABLED')
-- TODO: snap position to grid
-- FrameXML/EditModeUtil.lua
updatePosition(self)
end
local function onMouseDown(self) -- replacement for EditModeSystemMixin:SelectSystem()
if InCombatLockdown() then
-- TODO: maybe add a warning?
return
end
EventRegistry:TriggerEvent('EditModeExternal.hideDialog')
EditModeManagerFrame:ClearSelectedSystem() -- taint
if not self.isSelected then
self.parent:SetMovable(true)
self:ShowSelected(true)
if internal.dialog.selection ~= self then
internal.dialog:Reset()
end
internal.dialog:Update(self)
end
end
local function onEditModeEnter()
lib.isEditing = true
resetDialogs()
resetSelection()
for _, callback in next, lib.anonCallbacksEnter do
securecallfunction(callback)
end
end
local function onEditModeExit()
lib.isEditing = false
resetDialogs()
resetSelection()
for _, callback in next, lib.anonCallbacksExit do
securecallfunction(callback)
end
end
local function onEditModeChanged(_, layoutInfo)
local activeLayout = layoutInfo.activeLayout
if activeLayout ~= lib.activeLayout then
lib.activeLayout = activeLayout
-- update cache
lib.layoutCache = C_EditMode.GetLayouts().layouts
-- trigger callbacks
for _, callback in next, lib.anonCallbacksLayout do
securecallfunction(callback, layoutNames[activeLayout], activeLayout)
end
-- update dialog
if internal.dialog and internal.dialog.selection then
internal.dialog:Update(internal.dialog.selection)
end
-- TODO: we should update the position of the button here, let the user not deal with that
end
end
local function onSpecChanged(_, unit)
if unit ~= 'player' then
return
end
onEditModeChanged(nil, C_EditMode.GetLayouts())
end
local function onEditModeLayoutChanged()
local layoutInfo = C_EditMode.GetLayouts()
local layouts = layoutInfo.layouts
if #layouts > #lib.layoutCache then
-- a layout was created
for index, layout in next, layouts do
if not lib.layoutCache[index] then
for _, callback in next, lib.anonCallbacksCreate do
securecallfunction(callback, layout.layoutName, index, lib._layoutCopySource and lib._layoutCopySource.layoutName)
end
end
end
-- the game automatically switches to the newly created layout, which triggers
-- onEditModeChanged so we don't have to deal with that
elseif #layouts < #lib.layoutCache then
-- a layout was deleted
local newNames = {}
for _, layout in next, layouts do
newNames[layout.layoutName] = true
end
for _, layout in next, lib.layoutCache do
if not newNames[layout.layoutName] then
for _, callback in next, lib.anonCallbacksDelete do
securecallfunction(callback, layout.layoutName)
end
break
end
end
-- if the deleted layout was the current one the game automatically switches to Modern,
-- which triggers onEditModeChanged so we don't have to deal with that
else
-- a layout was renamed
for index, layout in next, layouts do
if layout.layoutName ~= lib.layoutCache[index].layoutName then
for _, callback in next, lib.anonCallbacksRename do
securecallfunction(callback, lib.layoutCache[index].layoutName, layout.layoutName, index)
end
if index == (lib.activeLayout - 2) then
-- the currently active layout was renamed, we trigger a layout update
lib.activeLayout = nil
onEditModeChanged(nil, layoutInfo)
return -- no need to proceed, the remaining tasks are already handled
end
break
end
end
end
lib._layoutCopySource = nil
lib.layoutCache = layouts
end
do -- deal with hooks and events
-- listen for layout changes
EventRegistry:RegisterFrameEventAndCallback('EDIT_MODE_LAYOUTS_UPDATED', function(...)
if lib.hookVersion == MINOR then
onEditModeChanged(...)
end
end)
EventRegistry:RegisterFrameEventAndCallback('PLAYER_SPECIALIZATION_CHANGED', function(...)
if lib.hookVersion == MINOR then
onSpecChanged(...)
end
end)
EventRegistry:RegisterCallback('EditMode.SavedLayouts', function(...)
if lib.hookVersion == MINOR then
onEditModeLayoutChanged(...)
end
end)
-- hook EditMode shown state, since QuickKeybindMode will hide/show EditMode
EditModeManagerFrame:HookScript('OnShow', function(...)
if lib.hookVersion == MINOR then
onEditModeEnter(...)
end
end)
EditModeManagerFrame:HookScript('OnHide', function(...)
if lib.hookVersion == MINOR then
onEditModeExit(...)
end
end)
-- we don't want any custom frames dangling around
EditModeSystemSettingsDialog:HookScript('OnHide', function(...)
if lib.hookVersion == MINOR then
resetDialogs(...)
end
end)
-- unselect our selections whenever a system is selected and try to add an extension
hooksecurefunc(EditModeManagerFrame, 'SelectSystem', function(_, systemFrame)
if lib.hookVersion == MINOR then
resetDialogs()
resetSelection()
if internal.dialog then
internal.dialog:Reset() -- can this be moved to resetDialogs ?
end
local systemID = systemFrame.system
local subSystemID = systemFrame.systemIndex
local isKnownSystem = lib.systemSettings[systemID] or lib.systemButtons[systemID]
local isKnownSubSystem = subSystemID and ((lib.subSystemSettings[systemID] and lib.subSystemSettings[systemID][subSystemID]) or (lib.subSystemButtons[systemID] and lib.subSystemButtons[systemID][subSystemID]))
if isKnownSystem or isKnownSubSystem then
internal.extension:Update(systemID, isKnownSubSystem and subSystemID or nil)
end
end
end)
hooksecurefunc(EditModeManagerFrame, 'ShowNewLayoutDialog', function(_, sourceLayout)
if lib.hookVersion == MINOR then
lib._layoutCopySource = sourceLayout
end
end)
end
-- custom global callback hook that all addons that add custom dialogs should respond to
EventRegistry:RegisterCallback('EditModeExternal.hideDialog', function()
resetDialogs()
resetSelection()
end)
--[[ LibEditMode:AddFrame(_frame, callback, default_) 
Register a frame to be controlled by the Edit Mode.
* `frame`: frame widget to be controlled
* `callback`: callback that triggers whenever the frame has been repositioned
* `default`: table containing the default position of the frame
* `name`: name of the system, if nil, the frame's name will be used
The `default` table must contain the following entries:
* `point`: relative anchor point, e.g. `"CENTER"` _(string)_
* `x`: horizontal offset from the anchor point _(number)_
* `y`: vertical offset from the anchor point _(number)_
--]]
function lib:AddFrame(frame, callback, default, name)
local selection = CreateFrame('Frame', nil, frame, 'EditModeSystemSelectionTemplate')
selection:SetAllPoints()
selection:SetScript('OnMouseDown', onMouseDown)
selection:SetScript('OnDragStart', onDragStart)
selection:SetScript('OnDragStop', onDragStop)
selection:SetScript('OnEvent', onDragStop)
selection:Hide()
-- as of 11.2 the template requires a system name to work correctly
selection.system = {
GetSystemName = function()
return name or frame.editModeName or frame:GetName()
end
}
lib.frameSelections[frame] = selection
lib.frameCallbacks[frame] = callback
lib.frameDefaults[frame] = default
if not internal.dialog then
internal.dialog = internal:CreateDialog()
internal.dialog:HookScript('OnHide', function()
resetSelection()
end)
-- fetch layout info in case EDIT_MODE_LAYOUTS_UPDATED already fired
if lib.layoutCache then
onEditModeChanged(nil, C_EditMode.GetLayouts()) -- introduces a little latency
end
end
end
--[[ LibEditMode:AddFrameSettings(_frame, settings_) 
Register extra settings that will be displayed in a dialog attached to the frame in the Edit Mode.
* `frame`: frame widget already registered with [AddFrame](#libeditmodeaddframeframe-callback-default-)
* `settings`: table containing [SettingObject](Types#settingobject) entries _(table, number indexed)_
--]]
function lib:AddFrameSettings(frame, settings)
if not lib.frameSelections[frame] then
error('frame must be registered')
end
lib.frameSettings[frame] = settings
end
--[[ LibEditMode:EnableFrameSetting(_frame, settingName_) 
Enables a setting on a frame.
* `frame`: frame widget already registered with [AddFrame](#libeditmodeaddframeframe-callback-default-)
* `settingName`: a setting already registered with [AddFrameSettings](#libeditmodeaddframesettingsframe-settings-)
--]]
function lib:EnableFrameSetting(frame, settingName)
local settings = internal:GetFrameSettings(frame)
if settings then
for _, setting in next, settings do
if setting.name == settingName then
setting.disabled = false
internal.dialog:Update(internal.dialog.selection)
break
end
end
end
end
--[[ LibEditMode:DisableFrameSetting(_frame, settingName_) 
Disables a setting on a frame.
* `frame`: frame widget already registered with [AddFrame](#libeditmodeaddframeframe-callback-default-)
* `settingName`: a setting already registered with [AddFrameSettings](#libeditmodeaddframesettingsframe-settings-)
--]]
function lib:DisableFrameSetting(frame, settingName)
local settings = internal:GetFrameSettings(frame)
if settings then
for _, setting in next, settings do
if setting.name == settingName then
setting.disabled = true
internal.dialog:Update(internal.dialog.selection)
break
end
end
end
end
--[[ LibEditMode:AddFrameSettingsButton(_frame, data_) 
> :warning: Deprecated. Please use [`LibEditMode:AddFrameSettingsButtons(frame, buttons)`](#libeditmodeaddframesettingsbuttonsframe-buttons-) instead.
Register extra button that will be displayed in a dialog attached to the frame in the Edit Mode.
* `frame`: frame widget already registered with [AddFrame](#libeditmodeaddframeframe-callback-default-)
* `data`: [ButtonObject](Types#buttonobject) _(table)_
--]]
function lib:AddFrameSettingsButton(frame, data)
if not lib.frameButtons[frame] then
lib.frameButtons[frame] = {}
end
table.insert(lib.frameButtons[frame], data)
end
--[[ LibEditMode:AddFrameSettingsButtons(_frame, buttons_) 
Register extra buttons that will be displayed in a dialog attached to the frame in the Edit Mode.
* `frame`: frame widget already registered with [AddFrame](#libeditmodeaddframeframe-callback-default-)
* `buttons`: table containing [ButtonObject](Types#buttonobject) entries _(table, number indexed)_
--]]
function lib:AddFrameSettingsButtons(frame, buttons)
if not lib.frameButtons[frame] then
lib.frameButtons[frame] = {}
end
for _, button in next, buttons do
table.insert(lib.frameButtons[frame], button)
end
end
--[[ LibEditMode:RefreshFrameSettings(_frame_) 
Refresh the dialog attached to the frame.
--]]
function lib:RefreshFrameSettings(frame)
local selection = lib.frameSelections[frame]
if selection and internal.dialog and internal.dialog.selection == selection and internal.dialog:IsVisible() then
internal.dialog:Update(selection)
end
end
--[[ LibEditMode:AddSystemSettings(_systemID, settings[, subSystemID]_) 
Register extra settings for a Blizzard system, it will be displayed in an dialog attached to the system's dialog in the Edit Mode.
* `systemID`: the ID of a system registered with the Edit Mode. See [`Enum.EditModeSystem`](https://warcraft.wiki.gg/wiki/EDIT_MODE_LAYOUTS_UPDATED).
* `settings`: table containing [SettingObject](Types#settingobject) entries _(table, number indexed)_
* `subSystemID`: optional ID of a subsystem of a system registered with the Edit Mode. See [`Enum.EditMode...SystemIndices`](https://github.com/Gethe/wow-ui-source/blob/live/Interface/AddOns/Blizzard_APIDocumentationGenerated/EditModeManagerConstantsDocumentation.lua).
--]]
function lib:AddSystemSettings(systemID, settings, subSystemID)
if subSystemID then
if not lib.subSystemSettings[systemID] then
lib.subSystemSettings[systemID] = {}
end
if not lib.subSystemSettings[systemID][subSystemID] then
lib.subSystemSettings[systemID][subSystemID] = {}
end
for _, setting in next, settings do
table.insert(lib.subSystemSettings[systemID][subSystemID], setting)
end
else
if not lib.systemSettings[systemID] then
lib.systemSettings[systemID] = {}
end
for _, setting in next, settings do
table.insert(lib.systemSettings[systemID], setting)
end
end
if not internal.extension then
internal.extension = internal:CreateExtension()
end
-- fetch layout info in case EDIT_MODE_LAYOUTS_UPDATED already fired
if lib.layoutCache then
onEditModeChanged(nil, C_EditMode.GetLayouts()) -- introduces a little latency
end
end
--[[ LibEditMode:EnableSystemSetting(_systemID, settingName[, subSystemID]_) 
Enables a setting on a frame.
* `systemID`: the ID of a system registered with the Edit Mode. See [`Enum.EditModeSystem`](https://warcraft.wiki.gg/wiki/EDIT_MODE_LAYOUTS_UPDATED).
* `settingName`: a setting already registered with [AddSystemSettings](#libeditmodeaddsystemsettingssystemid-settings-)
* `subSystemID`: optional ID of a subsystem of a system registered with the Edit Mode. See [`Enum.EditMode...SystemIndices`](https://github.com/Gethe/wow-ui-source/blob/live/Interface/AddOns/Blizzard_APIDocumentationGenerated/EditModeManagerConstantsDocumentation.lua).
--]]
function lib:EnableSystemSetting(systemID, settingName, subSystemID)
local settings = internal:GetSystemSettings(systemID, subSystemID)
if settings then
for _, setting in next, settings do
if setting.name == settingName then
setting.disabled = false
internal.extension:Update(internal.extension.systemID, internal.extension.subSystemID)
break
end
end
end
end
--[[ LibEditMode:DisableSystemSetting(_systemID, settingName[, subSystemID]_) 
Disables a setting on a frame.
* `systemID`: the ID of a system registered with the Edit Mode. See [`Enum.EditModeSystem`](https://warcraft.wiki.gg/wiki/EDIT_MODE_LAYOUTS_UPDATED).
* `settingName`: a setting already registered with [AddSystemSettings](#libeditmodeaddsystemsettingssystemid-settings-)
* `subSystemID`: optional ID of a subsystem of a system registered with the Edit Mode. See [`Enum.EditMode...SystemIndices`](https://github.com/Gethe/wow-ui-source/blob/live/Interface/AddOns/Blizzard_APIDocumentationGenerated/EditModeManagerConstantsDocumentation.lua).
--]]
function lib:DisableSystemSetting(systemID, settingName, subSystemID)
local settings = internal:GetSystemSettings(systemID, subSystemID)
if settings then
for _, setting in next, settings do
if setting.name == settingName then
setting.disabled = true
internal.extension:Update(internal.extension.systemID, internal.extension.subSystemID)
break
end
end
end
end
--[[ LibEditMode:AddSystemSettingsButtons(_systemID, buttons[, subSystemID]_) 
Register extra buttons for a Blizzard system, it will be displayed in a dialog attached to the system's dialog in the Edit Mode.
* `systemID`: the ID of a system registered with the Edit Mode. See [`Enum.EditModeSystem`](https://warcraft.wiki.gg/wiki/EDIT_MODE_LAYOUTS_UPDATED).
* `buttons`: table containing [ButtonObject](Types#buttonobject) entries _(table, number indexed)_
* `subSystemID`: optional ID of a subsystem of a system registered with the Edit Mode. See [`Enum.EditMode...SystemIndices`](https://github.com/Gethe/wow-ui-source/blob/live/Interface/AddOns/Blizzard_APIDocumentationGenerated/EditModeManagerConstantsDocumentation.lua).
--]]
function lib:AddSystemSettingsButtons(systemID, buttons, subSystemID)
if subSystemID then
if not lib.subSystemButtons[systemID] then
lib.subSystemButtons[systemID] = {}
end
if not lib.subSystemButtons[systemID][subSystemID] then
lib.subSystemButtons[systemID][subSystemID] = {}
end
for _, setting in next, buttons do
table.insert(lib.subSystemButtons[systemID][subSystemID], setting)
end
else
if not lib.systemButtons[systemID] then
lib.systemButtons[systemID] = {}
end
for _, button in next, buttons do
table.insert(lib.systemButtons[systemID], button)
end
end
if not internal.extension then
internal.extension = internal:CreateExtension()
end
-- fetch layout info in case EDIT_MODE_LAYOUTS_UPDATED already fired
if lib.layoutCache then
onEditModeChanged(nil, C_EditMode.GetLayouts()) -- introduces a little latency
end
end
--[[ LibEditMode:RegisterCallback(_event, callback_) 
Register extra callbacks whenever an event within the Edit Mode triggers.
* `event`: event name _(string)_
* `callback`: function that will be triggered with the event _(function)_
Possible events:
* `enter`: triggered when the Edit Mode is entered
* `exit`: triggered when the Edit Mode is exited
* `layout`: triggered when the Edit Mode layout is changed (which also occurs at login)
* signature:
* `layoutName`: name of the layout
* `layoutIndex`: index of the layout
* `create`: triggered when a Edit Mode layout has been created
* signature:
* `layoutName`: name of the new layout
* `layoutIndex`: index of the layout
* `sourceLayoutName`: name of the layout it was copied from, if it was copied
* `rename`: triggered when a Edit Mode layout has been renamed
* signature:
* `oldLayoutName`: name of the layout that got renamed
* `newLayoutName`: new name of the layout
* `layoutIndex`: index of the layout
* `delete`: triggered when a Edit Mode layout has been deleted
* signature:
* `layoutName`: name of the layout that got deleted
Example:
```lua
LibEditMode:RegisterCallback('rename', function(oldLayoutName, newLayoutName, layoutIndex)
-- do something
end)
```
--]]
function lib:RegisterCallback(event, callback)
assert(event and type(event) == 'string', 'event must be a string')
assert(callback and type(callback) == 'function', 'callback must be a function')
if event == 'enter' then
table.insert(lib.anonCallbacksEnter, callback)
elseif event == 'exit' then
table.insert(lib.anonCallbacksExit, callback)
elseif event == 'layout' then
table.insert(lib.anonCallbacksLayout, callback)
-- if there's none, then onEditModeChanged will take care of it
if lib.activeLayout then
securecallfunction(callback, layoutNames[lib.activeLayout], lib.activeLayout)
end
elseif event == 'create' then
table.insert(lib.anonCallbacksCreate, callback)
elseif event == 'rename' then
table.insert(lib.anonCallbacksRename, callback)
elseif event == 'delete' then
table.insert(lib.anonCallbacksDelete, callback)
else
error('invalid callback event "' .. event .. '"')
end
end
--[[ LibEditMode:GetActiveLayout() 
Returns the active Edit Mode layout.
This will not return valid data until after the layout has been loaded from the server.
Data will be available for the ["layout" callback](#libeditmoderegistercallbackevent-callback).
--]]
function lib:GetActiveLayout()
return lib.activeLayout
end
--[[ LibEditMode:GetActiveLayoutName() 
Returns the active Edit Mode layout name.
This will not return valid data until after the layout has been loaded from the server.
Data will be available for the ["layout" callback](#libeditmoderegistercallbackevent-callback).
--]]
function lib:GetActiveLayoutName()
return lib.activeLayout and layoutNames[lib.activeLayout]
end
--[[ LibEditMode:IsInEditMode() 
Returns whether the Edit Mode is currently active.
--]]
function lib:IsInEditMode()
return not not lib.isEditing
end
--[[ LibEditMode:GetFrameDefaultPosition(_frame_) 
Returns the default position table registered with the frame.
* `frame`: registered frame to return positions for
Returns:
* `defaultPosition`: table registered with the frame in [AddFrame](#libeditmodeaddframeframe-callback-default-) _(table)_
--]]
function lib:GetFrameDefaultPosition(frame)
return lib.frameDefaults[frame]
end
function internal:TriggerCallback(frame, ...)
if lib.frameCallbacks[frame] then
securecallfunction(lib.frameCallbacks[frame], frame, layoutNames[lib.activeLayout], ...)
end
end
function internal:GetFrameSettings(frame)
if lib.frameSettings[frame] then
return lib.frameSettings[frame], #lib.frameSettings[frame]
else
return nil, 0
end
end
function internal:GetFrameButtons(frame)
if lib.frameButtons[frame] then
return lib.frameButtons[frame], #lib.frameButtons[frame]
else
return nil, 0
end
end
function internal:MoveParent(selection, x, y)
updatePosition(selection, x, y)
end
function internal:GetSystemSettings(systemID, subSystemID)
if subSystemID and lib.subSystemSettings[systemID] and lib.subSystemSettings[systemID][subSystemID] then
return lib.subSystemSettings[systemID][subSystemID], #lib.subSystemSettings[systemID][subSystemID]
elseif lib.systemSettings[systemID] then
return lib.systemSettings[systemID], #lib.systemSettings[systemID]
else
return nil, 0
end
end
function internal:GetSystemSettingsButtons(systemID, subSystemID)
if subSystemID and lib.subSystemButtons[systemID] and lib.subSystemButtons[systemID][subSystemID] then
return lib.subSystemButtons[systemID][subSystemID], #lib.subSystemButtons[systemID][subSystemID]
elseif lib.systemButtons[systemID] then
return lib.systemButtons[systemID], #lib.systemButtons[systemID]
else
return nil, 0
end
end
--[[ Types:header
## SettingObject 
Table containing the following entries:
| key | value | type | required |
|:---------|:---------------------------------------|:----------------------------|:---------|
| kind | setting type | [SettingType](#settingtype) | yes |
| name | label for the setting | string | yes |
| desc | description for the setting | string | no |
| default | default value for the setting | any | yes |
| get | getter for the current value | function | yes |
| set | setter for the new value | function | yes |
| disabled | whether the setting should be disabled | boolean/function | no |
| hidden | whether the setting should be hidden | boolean/function | no |
- The getter passes `layoutName` as the sole argument and expects a value in return.
- The setter passes (`layoutName`, `newValue`, `fromReset`) and expects no returns.
- The description is shown in a tooltip.
- The `disabled` and `hidden` options, if added as functions, must return a boolean
Depending on the setting type there are additional required and optional entries:
### Dropdown 
| key | value | type | required |
|:----------|:----------------------------------------------------------------------------------------------------------------------|:---------------|:---------|
| values | indexed table containing [DropdownOption](#dropdownoption)s or a function that returns a compatible table | table/function | no |
| multiple | whether the dropdown should allow selecing multiple options | boolean | no |
| generator | [Dropdown `SetupMenu` "generator" (callback)](https://warcraft.wiki.gg/wiki/Patch_11.0.0/API_changes#New_menu_system) | function | no |
| height | max height of the menu | integer | no |
- Either `values` or `generator` is required, the former for simple menues and the latter for complex ones.
- They are not exclusive, but `generator` takes precedence (e.g. `values` will be available but not used).
- `generator` signature is `(dropdown, rootDescription, settingObject)` - `settingObject` being the addition to the default arguments.
- getters and setters are not handled using `generator`, and must be handled by the layout
## DropdownOption 
Table containing the following entries:
| key | value | type | required |
|:--------|:-------------------------------------------------------------------|---------|:---------|
| text | text rendered in the dropdown | string | yes |
| value | value the text represents, defaults to the text if not provided | any | no |
### Slider 
| key | value | type | required | default |
|:----------|:----------------------------------|:---------|:---------|:--------|
| minValue | lower bound for the slider | number | no | 0 |
| maxValue | upper bound for the slider | number | no | 1 |
| valueStep | step increment between each value | number | no | 1 |
| formatter | formatter for the display value | function | no | |
- The formatter passes `value` as the sole argument and expects a number value in return.
### ColorPicker 
| key | value | type | required | default |
|:-----------|:---------------------------------|:--------|:---------|:--------|
| hasOpacity | whether or not to enable opacity | boolean | no | false |
The `default` field and the getter expects a [ColorMixin](https://warcraft.wiki.gg/wiki/ColorMixin) object, and the setter will pass one as its value.
Even if `hasOpacity` is set to `false` (which is the default value) the ColorMixin object will contain an alpha value, this is the default behavior of the ColorMixin.
### Divider
| key | value | type | required | default |
|:----------|:---------------------------------|:--------|:---------|:--------|
| hideLabel | whether or not to hide the label | boolean | no | false |
### Expander
| key | value | type | required | default |
|:---------------|:---------------------------------|:--------|:---------|:--------|
| hideArrow | whether or not to hide the arrow | boolean | no | false |
| expandedLabel | text to display when expanded | string | no | |
| collapsedLabel | text to display when collapsed | string | no | |
For `expandedLabel` or `collapsedLabel` to work _both_ have to be defined. Otherwise the setting `name` will be used.
## ButtonObject 
Table containing the following entries:
| key | value | type | required |
|:------|:--------------------------------|----------|:---------|
| text | text rendered on the button | string | yes |
| click | callback when button is clicked | function | yes |
## SettingType 
Table containing available setting types.
One of:
- `Dropdown`
- `Checkbox`
- `Slider`
- `Divider`
- `ColorPicker`
- `Expander`
--]]
lib.SettingType = CopyTable(Enum.EditModeSettingDisplayType)
lib.SettingType.ColorPicker = 10 -- leave some room for blizzard expansion