-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy patheditor.lua
More file actions
2341 lines (2029 loc) · 103 KB
/
Copy patheditor.lua
File metadata and controls
2341 lines (2029 loc) · 103 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
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
local detailsFramework = _G["DetailsFramework"]
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
---@cast detailsFramework detailsframework
local CreateFrame = CreateFrame
local unpack = unpack
local wipe = table.wipe
--maximum number of entries kept in the undo / redo stacks; oldest entries are dropped beyond this.
local MAX_UNDO_STACK = 50
--shallow equality for undo snapshots. used to skip pushing undo entries when nothing actually
--changed - this happens when BuildMenuVolatile fires set() during widget construction with the
--current value, which would otherwise create no-op undo entries that wipe the redo stack.
local function snapshotsEqual(a, b)
if (a == b) then
--same reference, same scalar, or both nil
return true
end
if (type(a) ~= "table" or type(b) ~= "table") then
return false
end
for k, v in pairs(a) do
if (b[k] ~= v) then
return false
end
end
for k in pairs(b) do
if (a[k] == nil) then
return false
end
end
return true
end
--[=[
file description: this file has the code for the object editor
the object editor itself is a frame and has a scrollframe as canvas showing another frame where there's the options for the editing object
--]=]
--the editor doesn't know which key in the profileTable holds the current value for an attribute, so it uses a map table to find it.
--the mapTable is a table with the attribute name as a key, and the value is the profile key. For example, {["size"] = "text_size"} means profileTable["text_size"] = 10.
---@class df_editor : frame, df_optionsmixin, df_editormixin
---@field options table
---@field registeredObjects df_editor_objectinfo[]
---@field registeredObjectsByID table<any, df_editor_objectinfo>
---@field editingObject uiobject
---@field editingProfileTable table
---@field editingProfileMap table
---@field editingOptions df_editobjectoptions
---@field editingExtraOptions table
---@field registeredObjectInfo df_editor_objectinfo
---@field onEditCallback function
---@field optionsFrame frame
---@field overTheTopFrame frame
---@field objectSelector df_scrollbox
---@field moverObject moverobject
---@field canvasScrollBox df_canvasscrollbox
---@field ObjectBackgroundTexture texture
---@field AnchorFrames df_editor_anchorframes
---@field SelectedTextures texture[]
---@field undoHistory undostate[]
---@field redoHistory undostate[]
---@field UndoButton df_button?
---@field RedoButton df_button?
---each undo entry is a pair of closures. coalesceKey lets adjacent entries from the same widget
---(e.g. continuous slider drag) merge into one stack entry instead of N.
---@class undostate : table
---@field objectId any
---@field coalesceKey string
---@field undo fun()
---@field redo fun()
---@class df_editor_attribute
---@field key string?
---@field label string?
---@field widget string?
---@field type string?
---@field default any?
---@field minvalue number?
---@field maxvalue number?
---@field step number?
---@field usedecimals boolean?
---@field subkey string?
---@field desc string? tooltip text shown when the user hovers the widget
---@field profileTable table? per-extra override of the registration's profileTable, so an option can read/write against a different scope (e.g., the profile root on a registration bound to a sub-table)
---@field setter fun(object:any, value:any)?
---@field dropdownFunc function?
---@field onenter? fun(widget:any) optional callback for live preview
---@field onleave? fun(widget:any) optional callback (paired with onenter to revert the preview)
---@field text? string label widget: literal text to display when type == "label"
---@field name? string label widget: localization key fallback when type == "label"
---@field get? fun():string label widget: dynamic text getter when type == "label"
---@field text_template? table label widget: font template applied when type == "label"
---@field color? any label widget: text color applied when type == "label"
---@field namePhraseId? string label widget: phrase id for language table lookup when type == "label"
---@class df_editor_objectinfo : table
---@field object uiobject alias for objects[1]; the canonical "primary" member kept for backward compatibility with external consumers that read .object directly
---@field objects uiobject[] every UIObject bound to this registration; length >= 1. multi-member registrations have the same options/profile mapping driving all members
---@field label string
---@field id any
---@field profiletable table
---@field profilekeymap table
---@field subtablepath string?
---@field extraoptions table
---@field callback fun(object:df_editor_objectinfo, key:string, value:any, profileTable:table, profileKey:string)
---@field options df_editobjectoptions
---@field selectButton button alias for selectButtons[1]
---@field selectButtons button[] one click-to-select overlay per member widget
---@field activeMemberIndex number? which member was last clicked (or 1 by default); brackets/mover follow this member
---@field refFrame frame usually the parent of the object registered
---@field parentId any? id of the parent registration. when set, this entry renders nested under that parent in the object selector and is hidden while the parent is collapsed
---@field isExpanded boolean only meaningful for parents (entries that have at least one child registration). controls whether the children are shown in the selector. defaults to false
---@class df_editor_mover_movinginfo : table
---@field startX number
---@field startY number
---@field restingX number
---@field restingY number
--which object attributes are used to build the editor menu for each object type
local attributes = {
---@type df_editor_attribute[]
Texture = {
{
key = "texture",
label = "Texture",
widget = "selectstatusbartexture",
default = "",
setter = function(widget, value) widget:SetTexture(value) end,
},
{
key = "width",
label = "Width",
widget = "range",
minvalue = 5,
maxvalue = 120,
setter = function(widget, value) widget:SetWidth(value) end,
},
{
key = "height",
label = "Height",
widget = "range",
minvalue = 5,
maxvalue = 120,
setter = function(widget, value) widget:SetHeight(value) end,
},
{
key = "vertexcolor",
label = "Color",
widget = "color",
setter = function(widget, value) widget:SetVertexColor(unpack(value)) end,
},
{
key = "alpha",
label = "Alpha",
widget = "range",
minvalue = 0,
maxvalue = 1,
usedecimals = true,
setter = function(widget, value) widget:SetAlpha(value) end,
},
{widget = "blank"},
{
key = "anchor",
label = "Anchor",
widget = "anchordropdown",
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
{
key = "anchoroffsetx",
label = "Anchor X Offset",
widget = "range",
minvalue = -120,
maxvalue = 120,
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
{
key = "anchoroffsety",
label = "Anchor Y Offset",
widget = "range",
minvalue = -120,
maxvalue = 120,
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
},
FontString = {
{
key = "text",
label = "Text",
widget = "textentry",
default = "font string text",
setter = function(widget, value) widget:SetText(value) end,
},
{
key = "size",
label = "Size",
widget = "range",
minvalue = 5,
maxvalue = 120,
setter = function(widget, value) widget:SetFont(widget:GetFont(), value, select(3, widget:GetFont())) end,
},
{
key = "font",
label = "Font",
widget = "fontdropdown",
setter = function(widget, value)
local font = LibStub:GetLibrary("LibSharedMedia-3.0"):Fetch("font", value)
widget:SetFont(font, select(2, widget:GetFont()))
end,
},
{
key = "color",
label = "Color",
widget = "color",
setter = function(widget, value) widget:SetTextColor(unpack(value)) end,
},
{
key = "alpha",
label = "Alpha",
widget = "range",
minvalue = 0,
maxvalue = 1,
usedecimals = true,
setter = function(widget, value) widget:SetAlpha(value) end,
},
{widget = "blank"},
{
key = "shadow",
label = "Draw Shadow",
widget = "toggle",
setter = function(widget, value) widget:SetShadowColor(widget:GetShadowColor(), select(2, widget:GetShadowColor()), select(3, widget:GetShadowColor()), value and 0.5 or 0) end,
},
{
key = "shadowcolor",
label = "Shadow Color",
widget = "color",
setter = function(widget, value) widget:SetShadowColor(unpack(value)) end,
},
{
key = "shadowoffsetx",
label = "Shadow X Offset",
widget = "range",
minvalue = -10,
maxvalue = 10,
setter = function(widget, value) widget:SetShadowOffset(value, select(2, widget:GetShadowOffset())) end,
},
{
key = "shadowoffsety",
label = "Shadow Y Offset",
widget = "range",
minvalue = -10,
maxvalue = 10,
setter = function(widget, value) widget:SetShadowOffset(widget:GetShadowOffset(), value) end,
},
{
key = "outline",
label = "Outline",
widget = "outlinedropdown",
setter = function(widget, value) widget:SetFont(widget:GetFont(), select(2, widget:GetFont()), value) end,
},
{widget = "blank"},
{
key = "anchor",
label = "Anchor",
widget = "anchordropdown",
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
{
key = "anchoroffsetx",
label = "Anchor X Offset",
widget = "range",
minvalue = -120,
maxvalue = 120,
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
{
key = "anchoroffsety",
label = "Anchor Y Offset",
widget = "range",
minvalue = -120,
maxvalue = 120,
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
{
key = "rotation",
label = "Rotation",
widget = "range",
usedecimals = true,
minvalue = 0,
maxvalue = math.pi*2,
setter = function(widget, value) widget:SetRotation(value) end,
},
{
key = "scale",
label = "Scale",
widget = "range",
usedecimals = true,
minvalue = 0.65,
maxvalue = 2.5,
setter = function(widget, value) widget:SetScale(value) end,
},
},
Frame = {
{
key = "width",
label = "Width",
widget = "range",
minvalue = 5,
maxvalue = 800,
setter = function(widget, value) widget:SetWidth(value) end,
},
{
key = "height",
label = "Height",
widget = "range",
minvalue = 5,
maxvalue = 600,
setter = function(widget, value) widget:SetHeight(value) end,
},
--alpha
{
key = "alpha",
label = "Alpha",
widget = "range",
minvalue = 0,
maxvalue = 1,
usedecimals = true,
setter = function(widget, value) widget:SetAlpha(value) end,
},
--frame strata
{
key = "framestrata",
label = "Frame Strata",
widget = "selectframestrata",
setter = function(widget, value) widget:SetFrameStrata(value) end,
},
{widget = "blank"},
{
key = "anchor",
label = "Anchor",
widget = "anchordropdown",
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
{
key = "anchoroffsetx",
label = "Anchor X Offset",
widget = "range",
minvalue = -400,
maxvalue = 400,
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
{
key = "anchoroffsety",
label = "Anchor Y Offset",
widget = "range",
minvalue = -300,
maxvalue = 300,
setter = function(widget, value) detailsFramework:SetAnchor(widget, value, widget:GetParent()) end,
},
},
}
---@class df_editormixin : table
---@field CreateMoverFrame fun(self:df_editor):df_editor_mover[]
---@field CreateObjectSelectionList fun(self:df_editor, scroll_width:number, scroll_height:number, scroll_lines:number, scroll_line_height:number):df_scrollbox
---@field GetAllRegisteredObjects fun(self:df_editor):df_editor_objectinfo[]
---@field GetEditingObject fun(self:df_editor):uiobject
---@field GetEditingObjectIndex fun(self:df_editor):number?
---@field GetEditingOptions fun(self:df_editor):df_editobjectoptions
---@field GetExtraOptions fun(self:df_editor):table
---@field GetEditingProfile fun(self:df_editor):table, table
---@field GetOnEditCallback fun(self:df_editor):function
---@field GetOptionsFrame fun(self:df_editor):df_menu
---@field GetCanvasScrollBox fun(self:df_editor):df_canvasscrollbox
---@field GetObjectSelector fun(self:df_editor):df_scrollbox
---@field GetOverTheTopFrame fun(self:df_editor):frame
---@field GetMoverObject fun(self:df_editor):moverobject
---@field GetObjectById fun(self:df_editor, id:string):df_editor_objectinfo
---@field GetObjectByRef fun(self:df_editor, object:uiobject):df_editor_objectinfo
---@field GetObjectByIndex fun(self:df_editor, index:number):df_editor_objectinfo
---@field GetObjectByObjectInfo fun(self:df_editor, objectInfo:df_editor_objectinfo):df_editor_objectinfo
---@field GetEditingRegisteredObject fun(self:df_editor):df_editor_objectinfo
---@field EditObject fun(self:df_editor, object:df_editor_objectinfo, activeMember:uiobject?) when activeMember is omitted, falls back to the registration's last-clicked member (activeMemberIndex) or to objects[1].
---@field EditObjectById fun(self:df_editor, id:any)
---@field EditObjectByIndex fun(self:df_editor, index:number)
---@field ClearEditing fun(self:df_editor) tear down editor UI and clear all editing state
---@field CreateUndoManager fun(self:df_editor)
---@field AddToUndoHistory fun(self:df_editor, state:undostate)
---@field AddMoverUndoState fun(self:df_editor, registeredObject:df_editor_objectinfo, anchorTable:table, oldX:number, oldY:number, newX:number, newY:number)
---@field Undo fun(self:df_editor)
---@field Redo fun(self:df_editor)
---@field RefreshUndoButtons fun(self:df_editor) update enable/disable state of the toolbar undo/redo buttons
---@field PrepareObjectForEditing fun(self:df_editor)
---@field StartObjectMovement fun(self:df_editor, anchorSettings:df_anchor)
---@field StopObjectMovement fun(self:df_editor)
---@field RegisterObject fun(self:df_editor, object:uiobject|uiobject[], localizedLabel:string, id:string, profileTable:table, subTablePath:string, profileKeyMap:table, extraOptions:table?, callback:function?, options:df_editobjectoptions?, refFrame:frame):df_editor_objectinfo register one or more widgets under a single logical entry. When an array of widgets is passed, all members share the same option set and any in-place selection click selects the registration with the clicked member becoming the brackets/mover focus. All members must share the same object type.
---@field UnregisterObject fun(self:df_editor, object:uiobject)
---@field OnHide fun(self:df_editor)
---@field OnShow fun(self:df_editor)
---@field CreateSelectedTextures fun(self:df_editor)
---@field ShowSelectedTextures fun(self:df_editor, object:uiobject) 90 degree corner on each corner of the object
---@field HideSelectedTextures fun(self:df_editor) hide the four corner highlight textures
---@field SetSelectedBackgroundColor fun(self:df_editor, r:number, g:number, b:number, a:number) set the color of the filled rectangle drawn under the object being edited (defaults to magenta 1,0,1,0.25)
---@field GetProfileTableFromObject fun(self:df_editor, object:df_editor_objectinfo):table
---@field UpdateProfileTableOnAllRegisteredObjects fun(self:df_editor, profileTable:table)
---@field UpdateProfileTable fun(self:df_editor, identifier:any, profileTable:table):boolean change the profile table, identifier is the ID, index or object reference of the registered object info
---@field UpdateProfileSubTablePath fun(self:df_editor, identifier:any, subTablePath:string) change the subTablePath, identifier is the ID, index, object reference of the registered object info
---@field Refresh fun(self:df_editor) re-start editing the object that is currently being edited
---@class df_editobjectoptions : table
---@field use_colon boolean? if true a colon is shown after the option name
---@field can_move boolean? if true the object can be moved
---@field can_click boolean? if true the live-preview click-to-select overlay is shown for this object
---@field icon any atlasName atlasTable (from DF:CreateAtlas) or texture path|id
---@field parentId any? id of the parent registration. nests this entry under that parent in the object selector. selecting a nested entry auto-expands its parent
---@type df_editobjectoptions
local editObjectDefaultOptions = {
use_colon = false,
can_move = true,
can_click = true,
}
---@class df_editor_defaultoptions : table
---@field width number
---@field height number
---@field options_width number
---@field create_object_list boolean
---@field object_list_width number
---@field object_list_height number
---@field object_list_lines number
---@field object_list_line_height number
---@field text_template table
---@field dropdown_template table
---@field switch_template table
---@field button_template table
---@field slider_template table
---@field no_anchor_points boolean
---@field start_editing_callback fun(editorFrame: df_editor, registeredObject: df_editor_objectinfo)?
---@field selection_texture string
---@field selection_size number
---@field show_undo_buttons boolean
---@type df_editor_defaultoptions
local editorDefaultOptions = {
width = 400,
height = 548,
options_width = 340,
create_object_list = true,
object_list_width = 200,
object_list_height = 420,
object_list_lines = 20,
object_list_line_height = 20,
dropdown_template = detailsFramework:GetTemplate("dropdown", "OPTIONS_DROPDOWN_TEMPLATE"),
switch_template = detailsFramework:GetTemplate("switch", "OPTIONS_CHECKBOX_TEMPLATE"),
button_template = detailsFramework:GetTemplate("button", "OPTIONS_BUTTON_TEMPLATE"),
slider_template = detailsFramework:GetTemplate("slider", "OPTIONS_SLIDER_TEMPLATE"),
text_template = detailsFramework:GetTemplate("font", "OPTIONS_FONT_TEMPLATE"),
no_anchor_points = false,
start_editing_callback = nil,
selection_texture = "GM_BehaviorMessage_CornerTopLeft_Frame",
selection_size = 8,
show_undo_buttons = true,
}
local getParentTable = function(profileTable, profileKey)
local parentPath
if (profileKey:match("%]$")) then
parentPath = profileKey:gsub("%s*%[.*%]%s*$", "")
else
parentPath = profileKey:gsub("%.[^.]*$", "")
end
local parentTable = detailsFramework.table.getfrompath(profileTable, parentPath)
return parentTable
end
---@param self df_editor
---@param identifier any
---@return df_editor_objectinfo?
local getRegisteredObject = function(self, identifier)
if (type(identifier) == "string") then
return self:GetObjectById(identifier)
elseif (type(identifier) == "number") then
return self:GetObjectByIndex(identifier)
elseif (type(identifier) == "table" and identifier.GetObjectType) then
return self:GetObjectByRef(identifier)
elseif (type(identifier) == "table" and identifier.refFrame) then
return self:GetObjectByObjectInfo(identifier)
end
return nil
end
detailsFramework.EditorMixin = {
---@param self df_editor
GetEditingObject = function(self)
return self.editingObject
end,
---@param self df_editor
Refresh = function(self)
local registeredObject = self:GetEditingRegisteredObject()
if (registeredObject) then
self:EditObject(registeredObject)
end
end,
---@param self df_editor
GetEditingObjectIndex = function(self)
local object = self:GetEditingObject()
local registeredObjects = self:GetAllRegisteredObjects()
for i = 1, #registeredObjects do
local objectRegistered = registeredObjects[i]
--multi-member registrations: the editing widget may be any member, not just objects[1].
local members = objectRegistered.objects or {objectRegistered.object}
for j = 1, #members do
if (members[j] == object) then
return i
end
end
end
end,
---@param self df_editor
---@return df_editobjectoptions
GetEditingOptions = function(self)
return self.editingOptions
end,
---@param self df_editor
---@return table
GetExtraOptions = function(self)
return self.editingExtraOptions
end,
---@param self df_editor
---@return table, table
GetEditingProfile = function(self)
return self.editingProfileTable, self.editingProfileMap
end,
---@param self df_editor
---@return function
GetOnEditCallback = function(self)
return self.onEditCallback
end,
GetOptionsFrame = function(self)
return self.optionsFrame
end,
GetOverTheTopFrame = function(self)
return self.overTheTopFrame
end,
GetMoverObject = function(self)
return self.moverObject
end,
GetCanvasScrollBox = function(self)
return self.canvasScrollBox
end,
GetObjectSelector = function(self)
return self.objectSelector
end,
---@param self df_editor
CreateSelectedTextures = function(self)
self.SelectedTextures = {}
local anchors = {"topleft", "topright", "bottomleft", "bottomright"}
for i = 1, #anchors do
local thisAnchor = anchors[i]
local texture = self:GetOverTheTopFrame():CreateTexture(nil, "overlay")
detailsFramework:SetTexture(texture, self.options.selection_texture)
texture:SetDrawLayer("overlay", 6)
texture:Hide()
self.SelectedTextures[i] = texture
if (thisAnchor == "topright") then
texture:SetTexCoord(1, 0, 0, 1)
elseif (thisAnchor == "bottomleft") then
texture:SetTexCoord(0, 1, 1, 0)
elseif (thisAnchor == "bottomright") then
texture:SetTexCoord(1, 0, 1, 0)
end
end
end,
--90 degree corner on each corner of the object
ShowSelectedTextures = function(self, object)
local textures = self.SelectedTextures
local size = self.options.selection_size
--topleft
textures[1]:ClearAllPoints()
textures[1]:SetPoint("topleft", object, "topleft", -3, 3)
textures[1]:SetSize(size, size)
textures[1]:Show()
--topright
textures[2]:ClearAllPoints()
textures[2]:SetPoint("topright", object, "topright", 3, 3)
textures[2]:SetSize(size, size)
textures[2]:Show()
--bottomleft
textures[3]:ClearAllPoints()
textures[3]:SetPoint("bottomleft", object, "bottomleft", -3, -3)
textures[3]:SetSize(size, size)
textures[3]:Show()
--bottomright
textures[4]:ClearAllPoints()
textures[4]:SetPoint("bottomright", object, "bottomright", 3, -3)
textures[4]:SetSize(size, size)
textures[4]:Show()
end,
--symmetric to ShowSelectedTextures. called from OnHide and ClearEditing so the corner
--highlights don't linger over a previously-edited widget when the editor closes or the
--active object is unregistered.
---@param self df_editor
HideSelectedTextures = function(self)
local textures = self.SelectedTextures
for i = 1, #textures do
textures[i]:Hide()
end
end,
--set the color of the magenta rectangle drawn under the object being edited.
--default is (1, 0, 1, 0.25); change it per-editor to better contrast against the consumer's preview.
---@param self df_editor
---@param r number
---@param g number
---@param b number
---@param a number
SetSelectedBackgroundColor = function(self, r, g, b, a)
---@diagnostic disable-next-line: undefined-field
self.ObjectBackgroundTexture:SetColorTexture(r, g, b, a)
end,
---@class df_editor_anchorframes : table
---@field anchorFrames table
---@field DisableAllAnchors fun(self:df_editor_anchorframes)
---@field SetupAnchorsForObject fun(self:df_editor_anchorframes, anchorTable:table)
---@field SelectAnchorPoint fun(self:df_editor_anchorframes)
---@field GetAnchorFrame fun(self:df_editor_anchorframes, index:number):frame
---@field SetNotInUseForAllAnchors fun(self:df_editor_anchorframes)
---@field SetNotInUse fun(self:df_editor_anchorframes, anchorFrame:frame)
---@field SetInUse fun(self:df_editor_anchorframes, anchorFrame:frame)
---@field CreateNineAnchors fun(self:df_editor_anchorframes)
--screen position offset is the XY screen position offset from the bottom left of the screen, they are always positive, they go from 0 to screen width or height
CreateAnchorFrames = function(editorFrame)
--table containing 9 buttons, each one in a different position of the object, indexes one to nine in this order: topleft, left, bottomleft, bottom, bottomright, right, topright, top, center
editorFrame.AnchorFrames = {
anchorFrames = {},
DisableAllAnchors = function(self)
for i = 1, 9 do
local anchorFrame = self:GetAnchorFrame(i)
self:SetNotInUse(anchorFrame)
anchorFrame:Hide()
end
end,
SetupAnchorsForObject = function(self, anchorTable)
editorFrame.AnchorFrames:DisableAllAnchors()
local registeredObject = editorFrame:GetEditingRegisteredObject()
if (registeredObject.refFrame) then
for i = 1, 9 do
local anchorFrame = self.anchorFrames[i]
local anchorName = detailsFramework.AnchorPointsByIndex[i]
anchorFrame:ClearAllPoints()
anchorFrame:SetPoint(anchorName, registeredObject.refFrame, anchorName, 0, 0)
anchorFrame:Show()
end
local sideSelected = anchorTable.side
local anchorFrameSelected = self:GetAnchorFrame(detailsFramework.InsidePointsToAnchor[sideSelected] or sideSelected)
self:SetInUse(anchorFrameSelected)
self.anchorTable = anchorTable
end
end,
--when the user click on one of the anchor points, change the anchor side setting and recalculate the xy offset of the new point related to the same point in the object
SelectAnchorPoint = function(anchorFrame)
editorFrame.AnchorFrames:SetNotInUseForAllAnchors()
--change the color of the anchor point to show it's selected
anchorFrame.Texture:SetColorTexture(1, 0, 0, 0.5)
--get the object being edited in the editor
local object = editorFrame:GetEditingObject()
--get the xy of the nine points of the object
local ninePoints = detailsFramework.Math.GetNinePoints(object)
--get the coordinates of the anchorIndex within the ninePoints table
--the xy point in here is the XY screen position offset from the bottom left of the screen
local screenPoint = ninePoints[anchorFrame.anchorIndex]
local objectScreenPosX = screenPoint.x
local objectScreenPosY = screenPoint.y
--get the screen position offset of the anchor
local anchorScreenPosX, anchorScreenPosY = anchorFrame:GetCenter()
--calculate the xy offset of the anchor point related to the object
local offsetX = objectScreenPosX - anchorScreenPosX
local offsetY = objectScreenPosY - anchorScreenPosY
--get the anchor settings table
local anchorTable = editorFrame.AnchorFrames.anchorTable
--set the anchor settings
anchorTable.x = offsetX
anchorTable.y = offsetY
anchorTable.side = detailsFramework.AnchorPointsToInside[anchorFrame.anchorIndex]
C_Timer.After(0, function()
editorFrame:PrepareObjectForEditing()
end)
end,
GetAnchorFrame = function(self, anchorIndex)
return self.anchorFrames[anchorIndex]
end,
SetNotInUseForAllAnchors = function(self)
for i = 1, 9 do
local anchorFrame = self:GetAnchorFrame(i)
self:SetNotInUse(anchorFrame)
end
end,
SetNotInUse = function(self, anchorFrame)
anchorFrame.Texture:SetColorTexture(1, 1, 1, 0.5)
end,
SetInUse = function(self, anchorFrame)
anchorFrame.Texture:SetColorTexture(1, 0, 0, 0.5)
end,
CreateNineAnchors = function(self)
local overTheTopFrame = editorFrame:GetOverTheTopFrame()
for i = 1, 9 do
local anchorFrame = CreateFrame("button", "$parentAnchorFrame" .. i, overTheTopFrame, "BackdropTemplate")
anchorFrame:SetSize(8, 8)
anchorFrame:SetBackdrop({bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], tileSize = 64, tile = true})
anchorFrame:SetBackdropColor(1, 0, 0, 0.5)
anchorFrame:SetFrameStrata("TOOLTIP")
anchorFrame:SetFrameLevel(10)
anchorFrame.anchorIndex = i
anchorFrame:Hide()
anchorFrame:SetScript("OnClick", editorFrame.AnchorFrames.SelectAnchorPoint)
self.anchorFrames[i] = anchorFrame
anchorFrame.Texture = anchorFrame:CreateTexture("$parentTexture", "border")
anchorFrame.Texture:SetColorTexture(1, 1, 1, 0.5)
anchorFrame.Texture:SetAllPoints(anchorFrame)
end
end,
}
editorFrame.AnchorFrames:CreateNineAnchors()
return editorFrame.AnchorFrames
end,
---@class moverobject : table
---@field lastMoveInfo table
---@field MoverFrame df_editor_mover
---@field Setup fun(self:moverobject, object:uiobject, registeredObject:df_editor_objectinfo, onTickWhileMoving:function, onTickNotMoving:function)
---@field Hide fun(self:moverobject)
---@field Stop fun(self:moverobject)
---@field UpdatePosition fun(self:moverobject, moverFrame:df_editor_mover)
---@class df_editor_mover : frame
---@field MovingInfo df_editor_mover_movinginfo
---@field MoverIcon texture
---@field OnTickWhileMoving fun(self:df_editor_mover, deltaTime:number)
---@field OnTickNotMoving fun(self:df_editor_mover, deltaTime:number)
---create a frame to move the object, the frame is attached into the bottom right of the selected object
---@param editorFrame df_editor
---@return moverobject
CreateMoverFrame = function(editorFrame)
--frame that is used to move the object
---@type moverobject
---@diagnostic disable-next-line: missing-fields
local moverObject = {
Setup = function(self, object, registeredObject, onTickWhileMoving, onTickNotMoving)
local moverFrame = self.MoverFrame
moverFrame:Show()
moverFrame:EnableMouse(true)
moverFrame.OnTickWhileMoving = onTickWhileMoving
moverFrame.OnTickNotMoving = onTickNotMoving
moverFrame:SetScript("OnMouseDown", function()
--save the current position of the object
local startX, startY = moverFrame:GetCenter()
moverFrame.MovingInfo.startX = startX
moverFrame.MovingInfo.startY = startY
--snapshot the anchor offsets at drag start so OnMouseUp can push an
--accurate undo entry. captured here (not at StartObjectMovement time)
--so that anchor changes between drags don't pollute the snapshot.
local moveInfo = self.lastMoveInfo
if (moveInfo and moveInfo.anchorSettings) then
moveInfo.preMoveX = moveInfo.anchorSettings.x
moveInfo.preMoveY = moveInfo.anchorSettings.y
end
--start moving
moverFrame:SetScript("OnUpdate", onTickWhileMoving)
moverFrame:StartMoving()
end)
moverFrame:SetScript("OnMouseUp", function()
self:Stop()
moverFrame:EnableMouse(true)
--save the current position of the object selected
local x, y = object:GetCenter()
moverFrame.MovingInfo.restingX = x
moverFrame.MovingInfo.restingY = y
moverFrame:SetScript("OnUpdate", onTickNotMoving)
--push undo entry for the drag, but only if the offsets actually changed.
--skips entries for click-without-drag.
local moveInfo = self.lastMoveInfo
if (moveInfo and moveInfo.anchorSettings and moveInfo.preMoveX ~= nil) then
local anchor = moveInfo.anchorSettings
local oldX, oldY = moveInfo.preMoveX, moveInfo.preMoveY
local newX, newY = anchor.x, anchor.y
if (oldX ~= newX or oldY ~= newY) then
local edited = editorFrame:GetEditingRegisteredObject()
if (edited) then
editorFrame:AddMoverUndoState(edited, anchor, oldX, oldY, newX, newY)
end
end
end
end)
--anchor the mover so it covers the entire widget (topleft + bottomright pair
--overrides SetSize). this lets the user click anywhere on the widget to drag it,
--matching the purple ObjectBackgroundTexture that visually frames the same area.
moverFrame:ClearAllPoints()
moverFrame:SetPoint("topleft", object, "topleft", 0, 0)
moverFrame:SetPoint("bottomright", object, "bottomright", 0, 0)
local x, y = object:GetCenter()
moverFrame.MovingInfo.restingX = x
moverFrame.MovingInfo.restingY = y
moverFrame:SetScript("OnUpdate", onTickNotMoving)
end,
Hide = function(self)
self.MoverFrame:Hide()
end,
Stop = function(self)
local moverFrame = self.MoverFrame
moverFrame:StopMovingOrSizing()
moverFrame:SetScript("OnUpdate", nil)
end,
UpdatePosition = function(self, moverFrame)
local thisMoverFrame = self.MoverFrame
if (thisMoverFrame ~= moverFrame) then --what?
thisMoverFrame.OnTickNotMoving(thisMoverFrame, 0)
end
end,
}
---@type df_editor_mover
local moverFrame = CreateFrame("button", editorFrame:GetName() .. "MoverFrame", UIParent, "BackdropTemplate")
moverObject.MoverFrame = moverFrame
moverFrame:SetFrameStrata("TOOLTIP")
moverFrame:SetSize(16, 16)
moverFrame:SetClampedToScreen(true)
moverFrame:EnableMouse(true)
moverFrame:SetMovable(true)
moverFrame:SetFrameLevel(4)
moverFrame.MovingInfo = {
startX = 0,
startY = 0,
restingX = 0,
restingY = 0,
}
--create the mover texture background
moverFrame:SetBackdrop({bgFile = [[Interface\Tooltips\UI-Tooltip-Background]], tileSize = 64, tile = true})
moverFrame:SetBackdropColor(1, 0, 0, 0.2)
--white dot in the middle of the mover frame
moverFrame.MoverIcon = moverFrame:CreateTexture("$parentMoverIcon", "overlay")
moverFrame.MoverIcon:SetTexture([[Interface\CHATFRAME\CHATFRAMEBACKGROUND]])
moverFrame.MoverIcon:SetSize(6, 6) --default: 6
moverFrame.MoverIcon:SetPoint("center", moverFrame, "center", 0, 0)
return moverObject
end,
EditObjectById = function(self, id)
---@type df_editor_objectinfo
local objectRegistered = self:GetObjectById(id)
assert(type(objectRegistered) == "table", "EditObjectById() object not found.")
self:EditObject(objectRegistered)
end,
EditObjectByIndex = function(self, index)
---@type df_editor_objectinfo
local objectRegistered = self:GetObjectByIndex(index)
assert(type(objectRegistered) == "table", "EditObjectById() object not found.")
self:EditObject(objectRegistered)
end,
---@param self df_editor
---@param registeredObject df_editor_objectinfo
---@param activeMember uiobject? specific member widget to focus brackets/mover on; defaults to last-clicked member or members[1]
EditObject = function(self, registeredObject, activeMember)
--clear previous values
self.editingObject = nil
self.editingProfileMap = nil
self.editingProfileTable = nil
self.editingOptions = nil
self.editingExtraOptions = nil
self.onEditCallback = nil
--resolve which member becomes the visual focus (brackets, mover, ObjectBackgroundTexture).
--priority: explicit activeMember arg > registration's last-clicked memory > members[1].
--activeMemberIndex is remembered on the registration so subsequent EditObject(reg) calls
--(left-list clicks, OnShow restore, Undo replay) preserve the user's last in-place click.
local members = registeredObject.objects
local active = activeMember
if (not active) then
local lastIndex = registeredObject.activeMemberIndex
active = (lastIndex and members[lastIndex]) or members[1]
end
if (active) then
for i = 1, #members do
if (members[i] == active) then
registeredObject.activeMemberIndex = i
break
end
end
end
local object = active or registeredObject.object
local profileKeyMap = registeredObject.profilekeymap
local extraOptions = registeredObject.extraoptions
local callback = registeredObject.callback
local options = registeredObject.options
local profileTable = self:GetProfileTableFromObject(registeredObject)
assert(type(profileTable) == "table", "EditObject() profileTable is invalid.")
--as there's no other place which this members are set, there is no need to create setter functions
self.registeredObjectInfo = registeredObject
self.editingObject = object
self.editingProfileMap = profileKeyMap
self.editingProfileTable = profileTable
self.editingOptions = options
self.editingExtraOptions = extraOptions
if (type(callback) == "function") then
self.onEditCallback = callback
end
self:PrepareObjectForEditing()