-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGGUI.lua
4709 lines (4134 loc) · 173 KB
/
GGUI.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
---@class GGUI-2.1
local GGUI = LibStub:NewLibrary("GGUI-2.1", 27)
if not GGUI then return end -- if version already exists
---@type GGUI_GUTIL
local GUTIL = GGUI_GUTIL
--- CLASSICS insert
---@class Object
local Object = {}
Object.__index = Object
GGUI.Object = Object
function Object:new()
end
function Object:extend()
local cls = {}
for k, v in pairs(self) do
if k:find("__") == 1 then
cls[k] = v
end
end
cls.__index = cls
cls.super = self
setmetatable(cls, self)
return cls
end
function Object:implement(...)
for _, cls in pairs({ ... }) do
for k, v in pairs(cls) do
if self[k] == nil and type(v) == "function" then
self[k] = v
end
end
end
end
function Object:is(T)
local mt = getmetatable(self)
while mt do
if mt == T then
return true
end
mt = getmetatable(mt)
end
return false
end
function Object:__tostring()
return "Object"
end
function Object:__call(...)
local obj = setmetatable({}, self)
obj:new(...)
return obj
end
--- CLASSICS END
-- GGUI CONST
GGUI.CONST = {}
GGUI.CONST.EMPTY_TEXTURE = "Interface\\containerframe\\bagsitemslot2x"
---@class GGUI.AnchorPoint
---@field anchorParent Region?
---@field anchorA FramePoint?
---@field anchorB FramePoint?
---@field offsetX number?
---@field offsetY number?
---@class GGUI.ConstructorOptions
---@field debug? boolean
-- GGUI UTILS
function GGUI:DebugPrint(objectConstructorOptions, text)
if objectConstructorOptions.debug then
print(GUTIL:ColorizeText("GGUI Debug: ", GUTIL.COLORS.EPIC) .. tostring(text))
end
end
---@param errorMessage string
function GGUI:ThrowError(message)
error(GUTIL:ColorizeText("GGUI Error: ", GUTIL.COLORS.RED) .. tostring(message))
end
--- Requires DevTool Addon
function GGUI:DebugTable(objectConstructorOptions, t, label)
if objectConstructorOptions.debug and DevTool then
DevTool:AddData(t, "GGUI: " .. label)
end
end
---@generic T
---@param frame T | GGUI.Frame
---@param onCloseCallback? fun(frame : T)
---@param closeButtonOptions? GGUI.Button.ConstructorOptions
function GGUI:MakeFrameCloseable(frame, onCloseCallback, closeButtonOptions)
closeButtonOptions = closeButtonOptions or {}
closeButtonOptions.parent = closeButtonOptions.parent or frame
closeButtonOptions.anchorParent = closeButtonOptions.anchorParent or frame
closeButtonOptions.anchorA = closeButtonOptions.anchorA or "TOP"
closeButtonOptions.anchorB = closeButtonOptions.anchorB or "TOPRIGHT"
closeButtonOptions.sizeX = closeButtonOptions.sizeX or 25
closeButtonOptions.sizeY = closeButtonOptions.sizeY or 20
closeButtonOptions.offsetX = closeButtonOptions.offsetX or -20
closeButtonOptions.offsetY = closeButtonOptions.offsetY or -10
closeButtonOptions.anchorPoints = closeButtonOptions.anchorPoints
closeButtonOptions.label = closeButtonOptions.label or "X"
closeButtonOptions.clickCallback = function()
frame:Hide()
if onCloseCallback then
onCloseCallback(frame)
end
end
frame.closeButton = GGUI.Button(closeButtonOptions)
end
---@param gFrame GGUI.Frame
function GGUI:MakeFrameMoveable(gFrame)
gFrame.frame.hookFrame:SetMovable(true)
gFrame.frame:HookScript("OnMouseDown", function(self, button)
local anchorParent = select(2, gFrame.frame.hookFrame:GetPoint())
gFrame.preMoveAnchorParent = anchorParent
gFrame.frame.hookFrame:StartMoving()
end)
gFrame.frame:HookScript("OnMouseUp", function(self, button)
gFrame.frame.hookFrame:StopMovingOrSizing()
if not gFrame.preMoveAnchorParent then return end
local x, y = gFrame.frame.hookFrame:GetCenter()
local relativeX, relativeY = gFrame.preMoveAnchorParent:GetCenter()
-- Calculate the offset between the original anchor parent and the new position
local offsetX = x - relativeX
local offsetY = y - relativeY
-- Reapply the anchor point with the offset
gFrame.frame.hookFrame:ClearAllPoints()
gFrame.frame.hookFrame:SetPoint("CENTER", gFrame.preMoveAnchorParent, "CENTER", offsetX, offsetY)
gFrame:SavePosition(offsetX, offsetY)
end)
end
---@param frame Frame
---@param itemLink string
---@param owner Frame
---@param anchor TooltipAnchor
function GGUI:SetItemTooltip(frame, itemLink, owner, anchor)
local function onEnter()
local _, ItemLink = GameTooltip:GetItem()
GameTooltip:SetOwner(owner, anchor);
if ItemLink ~= itemLink then
-- to not set it again and hide the tooltip..
GameTooltip:SetHyperlink(itemLink)
end
GameTooltip:Show();
end
local function onLeave()
GameTooltip:Hide();
end
if itemLink then
frame:SetScript("OnEnter", onEnter)
frame:SetScript("OnLeave", onLeave)
else
frame:SetScript("OnEnter", nil)
frame:SetScript("OnLeave", nil)
end
end
---@param frame Frame
---@param spellID number
---@param owner Frame
---@param anchor TooltipAnchor
function GGUI:SetSpellTooltip(frame, spellID, owner, anchor)
local function onEnter()
local _, currentSpellID = GameTooltip:GetSpell()
GameTooltip:SetOwner(owner, anchor);
if currentSpellID ~= spellID then
-- to not set it again and hide the tooltip..
GameTooltip:SetSpellByID(spellID)
end
GameTooltip:Show();
end
local function onLeave()
GameTooltip:Hide();
end
if spellID then
frame:SetScript("OnEnter", onEnter)
frame:SetScript("OnLeave", onLeave)
else
frame:SetScript("OnEnter", nil)
frame:SetScript("OnLeave", nil)
end
end
function GGUI:SetTooltipsByTooltipOptions(frame, optionsOwner)
local function handleTooltipOnEnter()
if not optionsOwner.tooltipOptions then return end
---@type GGUI.TooltipOptions
local tooltipOptions = optionsOwner.tooltipOptions
GameTooltip:SetOwner(tooltipOptions.owner or frame, tooltipOptions.anchor);
if tooltipOptions.spellID then
local _, currentSpellID = GameTooltip:GetSpell()
if currentSpellID ~= tooltipOptions.spellID then
-- to not set it again and hide the tooltip..
GameTooltip:SetSpellByID(tooltipOptions.spellID)
end
elseif tooltipOptions.itemID then
GameTooltip:SetItemByID(tooltipOptions.itemID)
elseif tooltipOptions.itemLink then
GameTooltip:SetHyperlink(tooltipOptions.itemLink)
elseif tooltipOptions.text then
GameTooltip:SetText(tooltipOptions.text, nil, nil, nil, nil,
tooltipOptions.textWrap)
elseif tooltipOptions.frame then
if tooltipOptions.frameUpdateCallback then
tooltipOptions.frameUpdateCallback(tooltipOptions.frame)
end
GameTooltip_InsertFrame(GameTooltip, tooltipOptions.frame)
end
if tooltipOptions.scale then
GameTooltip:SetScale(tooltipOptions.scale)
else
GameTooltip:SetScale(1)
end
GameTooltip:Show();
end
local function handleTooltipOnLeave()
if not optionsOwner.tooltipOptions then return end
GameTooltip:Hide();
end
frame:HookScript("OnEnter", function()
handleTooltipOnEnter()
end)
frame:HookScript("OnLeave", function()
handleTooltipOnLeave()
end)
end
function GGUI:EnableHyperLinksForFrameAndChilds(frame)
if type(frame) == "table" and frame.SetHyperlinksEnabled and not frame.enabledLinks then -- prevent inf loop by references
frame.enabledLinks = true
frame:SetHyperlinksEnabled(true)
frame:SetScript("OnHyperlinkClick", ChatFrame_OnHyperlinkShow)
for possibleFrame1, possibleFrame2 in pairs(frame) do
GGUI:EnableHyperLinksForFrameAndChilds(possibleFrame1)
GGUI:EnableHyperLinksForFrameAndChilds(possibleFrame2)
end
end
end
---@param region Region
---@param anchorPoints GGUI.AnchorPoint[]
function GGUI:SetPointsByAnchorPoints(region, anchorPoints)
region:ClearAllPoints()
for _, anchorPoint in ipairs(anchorPoints) do
GGUI:SetPointByAnchorPoint(region, anchorPoint)
end
end
---@param region Region
---@param anchorPoint GGUI.AnchorPoint
function GGUI:SetPointByAnchorPoint(region, anchorPoint)
region:SetPoint(anchorPoint.anchorA or "CENTER", anchorPoint.anchorParent,
anchorPoint.anchorB or "CENTER",
anchorPoint.offsetX or 0,
anchorPoint.offsetY or 0)
end
---@param backdropFrame BackdropTemplate
---@param backdropOptions GGUI.BackdropOptions?
function GGUI:SetBackdropByBackdropOptions(backdropFrame, backdropOptions)
if not backdropOptions then return end
if backdropOptions.backdropInfo then
backdropFrame:SetBackdrop(backdropOptions.backdropInfo)
end
if backdropOptions.backdropRGBA and #backdropOptions.backdropRGBA == 4 then
backdropFrame:SetBackdropColor(backdropOptions.backdropRGBA[1], backdropOptions.backdropRGBA[2],
backdropOptions.backdropRGBA[3], backdropOptions.backdropRGBA[4])
end
if backdropOptions.borderRGBA and #backdropOptions.borderRGBA == 4 then
backdropFrame:SetBackdropBorderColor(backdropOptions.borderRGBA[1], backdropOptions.borderRGBA[2],
backdropOptions.borderRGBA[3], backdropOptions.borderRGBA[4])
end
end
---- GGUI Widgets
--- GGUI Widget
---@class GGUI.Widget : Object
GGUI.Widget = GGUI.Object:extend()
function GGUI.Widget:new(frame)
self.frame = frame
self.isGGUI = true
end
--- forward common frame/region methods to original frame
function GGUI.Widget:SetScript(...)
self.frame:SetScript(...)
end
function GGUI.Widget:HookScript(...)
self.frame:HookScript(...)
end
function GGUI.Widget:Show()
self.frame:Show()
end
function GGUI.Widget:Hide()
self.frame:Hide()
end
function GGUI.Widget:SetEnabled(enabled)
self.frame:SetEnabled(enabled)
end
function GGUI.Widget:SetVisible(visible)
if visible then
self:Show()
else
self:Hide()
end
end
function GGUI.Widget:GetHeight()
return self.frame:GetHeight()
end
function GGUI.Widget:GetWidth()
return self.frame:GetWidth()
end
function GGUI.Widget:SetTransparency(transparency)
self.frame:SetBackdropColor(0, 0, 0, transparency) -- TODO: with current color
end
function GGUI.Widget:IsVisible()
return self.frame:IsVisible()
end
function GGUI.Widget:SetPoint(...)
return self.frame:SetPoint(...)
end
--- super constructor needs to be called beforehand so that self.frame is existent
---@param anchorPoints GGUI.AnchorPoint[]
function GGUI.Widget:SetPointsByAnchorPoints(anchorPoints)
GGUI:SetPointsByAnchorPoints(self.frame, anchorPoints)
end
function GGUI.Widget:Raise()
self.frame:Raise()
end
function GGUI.Widget:Lower()
self.frame:Lower()
end
function GGUI.Widget:SetFrameLevel(...)
self.frame:SetFrameLevel(...)
end
--- GGUI Frame
---@class GGUI.FrameStatus[]
---@field statusID string
---@field sizeX? number
---@field sizeY? number
---@field offsetX? number
---@field offsetY? number
---@field anchorA? FramePoint
---@field anchorB? FramePoint
---@field parent? Frame
---@field anchorParent? Region
---@field title? string
---@field activationCallback? function
---@class GGUI.FrameConstructorOptions : GGUI.ConstructorOptions
---@field globalName? string
---@field title? string
---@field titleOptions? GGUI.TextConstructorOptions
---@field parent? Frame
---@field anchorParent? Region DEPRICATED use anchorPoints
---@field anchorA? FramePoint DEPRICATED use anchorPoints
---@field anchorB? FramePoint DEPRICATED use anchorPoints
---@field offsetX? number DEPRICATED use anchorPoints
---@field offsetY? number DEPRICATED use anchorPoints
---@field anchorPoints? GGUI.AnchorPoint[]
---@field sizeX? number
---@field sizeY? number
---@field scale? number
---@field frameID? string
---@field scrollableContent? boolean
---@field closeable? boolean
---@field closeButtonOptions? GGUI.Button.ConstructorOptions
---@field collapseable? boolean
---@field collapsed? boolean
---@field moveable? boolean
---@field frameStrata? FrameStrata if omitted will be same as parent
---@field frameLevel? number if omitted will be 1 higher than parent
---@field onCloseCallback? function
---@field onCollapseCallback? function
---@field onCollapseOpenCallback? function
---@field backdropOptions? GGUI.BackdropOptions
---@field initialStatusID? string
---@field frameTable? table The table where your addon stores its frames for later retrieval
---@field frameConfigTable? table The saved variable table where your addon stores any frame config like position
---@field closeOnClickOutside? boolean
---@field tooltipOptions? GGUI.TooltipOptions
---@field hide? boolean
---@field raiseOnInteraction? boolean
---@class GGUI.BackdropOptions
---@field backdropInfo? backdropInfo
---@field backdropRGBA? table<number>
---@field borderRGBA? table<number>
---@param frameTable table the table where your addon stores your frames
---@param frameID string The ID string you gave the frame
---@return GGUI.Frame | nil
function GGUI:GetFrame(frameTable, frameID)
if not frameTable[frameID] then
return nil
end
return frameTable[frameID]
end
---@class GGUI.Frame : GGUI.Widget
---@overload fun(options:GGUI.FrameConstructorOptions): GGUI.Frame
GGUI.Frame = GGUI.Widget:extend()
---@param options GGUI.FrameConstructorOptions
function GGUI.Frame:new(options)
options = options or {}
-- handle defaults
options.anchorA = options.anchorA or "CENTER"
options.anchorB = options.anchorB or "CENTER"
options.offsetX = options.offsetX or 0
options.offsetY = options.offsetY or 0
options.sizeX = options.sizeX or 100
options.sizeY = options.sizeY or 100
options.scale = options.scale or 1
options.parent = options.parent or UIParent
options.anchorParent = options.anchorParent or UIParent
options.frameTable = options.frameTable or {}
options.frameConfigTable = options.frameConfigTable or {}
local numFrames = GUTIL:Count(options.frameTable) + 1
self.frameConfigTable = options.frameConfigTable
self.originalX = options.sizeX
self.originalY = options.sizeY
self.originalOffsetX = options.offsetX
self.originalOffsetY = options.offsetY
self.originalAnchorParent = options.anchorParent
self.originalAnchorA = options.anchorA
self.originalAnchorB = options.anchorB
self.frameID = options.frameID or ("GGUIFrame" .. numFrames)
self.scrollableContent = options.scrollableContent or false
self.closeable = options.closeable or false
self.collapseable = options.collapseable or false
self.moveable = options.moveable or false
self.frameStrata = options.frameStrata
self.collapsed = false
self.activeStatusID = options.initialStatusID
---@type GGUI.FrameStatus[]
self.statusList = {}
self.onCollapseCallback = options.onCollapseCallback
self.onCollapseOpenCallback = options.onCollapseOpenCallback
self.closeOnClickOutside = options.closeOnClickOutside or false
self.onCloseCallback = options.onCloseCallback
GGUI:DebugTable(options, options, "Frame Options")
local hookFrame = CreateFrame("frame", nil, options.parent)
if not options.anchorPoints then
hookFrame:SetPoint(options.anchorA, options.anchorParent, options.anchorB, options.offsetX, options.offsetY)
else
GGUI:SetPointsByAnchorPoints(hookFrame, options.anchorPoints)
end
local frame = CreateFrame("frame", options.globalName, hookFrame, "BackdropTemplate")
GGUI.Frame.super.new(self, frame)
frame.hookFrame = hookFrame
hookFrame:SetSize(options.sizeX, options.sizeY)
frame:SetSize(options.sizeX, options.sizeY)
frame:SetScale(options.scale)
frame:SetFrameStrata(options.frameStrata or options.parent:GetFrameStrata())
frame:SetFrameLevel(options.frameLevel or (options.parent:GetFrameLevel() + 1))
---@type number sourced by GetTime() which gives the id of the current render frame
self.onShowRenderFrameTimestamp = 0
frame:HookScript("OnShow", function()
self.onShowRenderFrameTimestamp = tonumber(GUTIL:Round(GetTime(), 1))
end)
if options.raiseOnInteraction then
frame:SetToplevel(true)
end
if options.hide then
frame:Hide()
end
if self.closeOnClickOutside then
-- Check for clicks outside the scaled frame
frame:HookScript("OnUpdate", function()
if IsMouseButtonDown("LeftButton") and frame:IsShown() then
if not frame:IsMouseOver() then
local renderFrameTimestamp = tonumber(GUTIL:Round(GetTime(), 1))
-- if render frame time stamp is younger than 2 secs
-- due to the frame being able to set to visible by a button that is not in its mouse over area
if renderFrameTimestamp > (self.onShowRenderFrameTimestamp + 0.3) then
frame:Hide()
if self.onCloseCallback then
self.onCloseCallback()
end
end
end
end
end)
end
local titleOptions = options.titleOptions or {}
titleOptions.parent = titleOptions.parent or frame
titleOptions.anchorPoints = titleOptions.anchorPoints or {}
titleOptions.anchorPoints[1] = titleOptions.anchorPoints[1] or {}
titleOptions.anchorPoints[1].anchorParent = titleOptions.anchorPoints[1].anchorParent or frame
titleOptions.anchorPoints[1].anchorA = titleOptions.anchorPoints[1].anchorA or "TOP"
titleOptions.anchorPoints[1].anchorB = titleOptions.anchorPoints[1].anchorB or "TOP"
titleOptions.anchorPoints[1].offsetY = titleOptions.anchorPoints[1].offsetY or -15
titleOptions.text = options.title or titleOptions.text
self.title = GGUI.Text(titleOptions)
frame:SetPoint("TOP", hookFrame, "TOP", 0, 0)
if options.backdropOptions then
if options.backdropOptions.backdropInfo then
GGUI:DebugPrint(options, "Setting backdrop by backdropoptions")
GGUI:SetBackdropByBackdropOptions(frame, options.backdropOptions)
else
local backdropOptions = options.backdropOptions
backdropOptions.colorR = backdropOptions.colorR or 0
backdropOptions.colorG = backdropOptions.colorG or 0
backdropOptions.colorB = backdropOptions.colorB or 0
backdropOptions.colorA = backdropOptions.colorA or 1
backdropOptions.tile = backdropOptions.tile or false
backdropOptions.tileSize = backdropOptions.tileSize or 32
backdropOptions.borderOptions = backdropOptions.borderOptions or {}
local borderOptions = backdropOptions.borderOptions
borderOptions.colorR = borderOptions.colorR or 0
borderOptions.colorG = borderOptions.colorG or 0
borderOptions.colorB = borderOptions.colorB or 0
borderOptions.colorA = borderOptions.colorA or 1
borderOptions.edgeSize = borderOptions.edgeSize or 16
borderOptions.insets = borderOptions.insets or { left = 8, right = 6, top = 8, bottom = 8 }
frame:SetBackdropBorderColor(borderOptions.colorR, borderOptions.colorG, borderOptions.colorB,
borderOptions.colorA)
frame:SetBackdrop({
bgFile = backdropOptions.bgFile,
edgeFile = borderOptions.edgeFile,
edgeSize = borderOptions.edgeSize,
insets = borderOptions.insets,
edgeInsets = borderOptions.edgeInsets,
tile = backdropOptions.tile,
tileSize = backdropOptions.tileSize,
})
frame:SetBackdropColor(backdropOptions.colorR, backdropOptions.colorG, backdropOptions.colorB,
backdropOptions.colorA)
end
end
if self.closeable then
GGUI:MakeFrameCloseable(frame, options.onCloseCallback, options.closeButtonOptions)
end
if self.collapseable then
GGUI:MakeFrameCollapsable(self)
end
if self.moveable then
GGUI:MakeFrameMoveable(self)
end
if self.scrollableContent then
-- scrollframe
frame.scrollFrame = CreateFrame("ScrollFrame", nil, frame, "UIPanelScrollFrameTemplate")
frame.scrollFrame.scrollChild = CreateFrame("frame")
local scrollFrame = frame.scrollFrame
local scrollChild = scrollFrame.scrollChild
scrollFrame:SetSize(frame:GetWidth(), frame:GetHeight())
scrollFrame:SetPoint("TOP", frame, "TOP", 0, -30)
scrollFrame:SetPoint("LEFT", frame, "LEFT", 20, 0)
scrollFrame:SetPoint("RIGHT", frame, "RIGHT", -35, 0)
scrollFrame:SetPoint("BOTTOM", frame, "BOTTOM", 0, 20)
scrollFrame:SetScrollChild(scrollFrame.scrollChild)
scrollChild:SetWidth(scrollFrame:GetWidth())
scrollChild:SetHeight(1) -- ??
frame.content = scrollChild
else
frame.content = CreateFrame("frame", nil, frame, "BackdropTemplate")
frame.content:SetPoint("TOP", frame, "TOP")
frame.content:SetSize(options.sizeX, options.sizeY)
frame.content:SetFrameStrata(frame:GetFrameStrata())
frame.content:SetFrameLevel(frame:GetFrameLevel() + 1)
end
self.tooltipOptions = options.tooltipOptions
if self.tooltipOptions then
GGUI:SetTooltipsByTooltipOptions(frame.content, self)
end
self.content = frame.content
options.frameTable[self.frameID] = self
return frame
end
function GGUI.Frame:Show()
self.frame:Show()
self.frame.hookFrame:Show()
if not self.collapsed then
self.content:Show()
end
end
function GGUI.Frame:Hide()
self.frame:Hide()
self.frame.hookFrame:Hide()
self.content:Hide()
end
function GGUI.Frame:Raise()
self.frame:Raise()
self.content:Raise()
end
function GGUI.Frame:Lower()
self.content:Lower()
self.frame:Lower()
end
function GGUI.Frame:SetSize(x, y)
self.frame:SetSize(x, y)
if self.frame.scrollFrame then
self.frame.scrollFrame:SetSize(self.frame:GetWidth(), self.frame:GetHeight())
self.frame.scrollFrame:SetPoint("TOP", self.frame, "TOP", 0, -30)
self.frame.scrollFrame:SetPoint("LEFT", self.frame, "LEFT", 20, 0)
self.frame.scrollFrame:SetPoint("RIGHT", self.frame, "RIGHT", -35, 0)
self.frame.scrollFrame:SetPoint("BOTTOM", self.frame, "BOTTOM", 0, 20)
self.frame.scrollFrame.scrollChild:SetWidth(self.frame.scrollFrame:GetWidth())
end
end
function GGUI.Frame:EnableHyperLinksForFrameAndChilds()
GGUI:EnableHyperLinksForFrameAndChilds(self.frame)
end
---@param gFrame GGUI.Frame
function GGUI:MakeFrameCollapsable(gFrame)
local frame = gFrame.frame
local offsetX = frame.closeButton and -43 or -23
frame.collapseButton = GGUI.Button({
parent = frame,
anchorParent = frame,
anchorA = "TOP",
anchorB = "TOPRIGHT",
offsetX = offsetX,
offsetY = -10,
label = " - ",
sizeX = 12,
sizeY = 20,
adjustWidth = true,
clickCallback = function()
if gFrame.collapsed then
gFrame:Decollapse()
else
gFrame:Collapse()
end
end
})
end
function GGUI.Frame:Collapse()
if self.collapseable and self.frame.collapseButton then
self.collapsed = true
-- make smaller and hide content, only show frameTitle
self.frame:SetSize(self.originalX, 40)
self.frame.collapseButton:SetText("+")
self.frame.content:Hide()
if self.frame.scrollFrame then
self.frame.scrollFrame:Hide()
end
if self.onCollapseCallback then
self.onCollapseCallback(self)
end
self.frameConfigTable["collapsed_" .. self.frameID] = true
end
end
function GGUI.Frame:Decollapse()
if self.collapseable and self.frame.collapseButton then
-- restore
self.collapsed = false
self.frame.collapseButton:SetText("-")
self.frame:SetSize(self.originalX, self.originalY)
self.frame.content:Show()
if self.frame.scrollFrame then
self.frame.scrollFrame:Show()
end
if self.onCollapseOpenCallback then
self.onCollapseOpenCallback(self)
end
self.frameConfigTable["collapsed_" .. self.frameID] = false
end
end
function GGUI.Frame:ResetPosition()
self.frame.hookFrame:ClearAllPoints()
self.frame.hookFrame:SetPoint(self.originalAnchorA, self.originalAnchorParent, self.originalAnchorB,
self.originalOffsetX, self.originalOffsetY)
local x, y = self.frame.hookFrame:GetCenter()
local relativeX, relativeY = self.originalAnchorParent:GetCenter()
-- Calculate the offset between the original anchor parent and the new position
local offsetX = x - relativeX
local offsetY = y - relativeY
self:SavePosition(offsetX, offsetY)
end
--- Set a list of predefined GGUI.ButtonStatus
---@param statusList GGUI.FrameStatus[]
function GGUI.Frame:SetStatusList(statusList)
-- map statuslist to their ids
table.foreach(statusList, function(_, status)
if not status.statusID then
GGUI:ThrowError("FrameStatus without statusID")
end
self.statusList[status.statusID] = status
end)
end
function GGUI.Frame:SetStatus(statusID)
local frameStatus = self.statusList[statusID]
self.activeStatusID = statusID
if frameStatus then
if frameStatus.sizeX then
self.frame:SetWidth(frameStatus.sizeX)
end
if frameStatus.sizeY then
self.frame:SetHeight(frameStatus.sizeY)
end
if frameStatus.title then
self.frame.title:SetText(frameStatus.title)
end
if frameStatus.offsetX or frameStatus.offsetY or frameStatus.anchorParent or frameStatus.anchorA or frameStatus.anchorB then
local offsetX = frameStatus.offsetX or self.originalOffsetX
local offsetY = frameStatus.offsetY or self.originalOffsetY
local anchorParent = frameStatus.anchorParent or self.originalAnchorParent
local anchorA = frameStatus.anchorA or self.originalAnchorA
local anchorB = frameStatus.anchorB or self.originalAnchorB
self.frame:ClearAllPoints()
self.frame:SetPoint(anchorA, anchorParent, anchorB, offsetX, offsetY)
end
if frameStatus.activationCallback then
frameStatus.activationCallback(self, statusID)
end
-- if collapsed, restore collapse height
if self.collapseable and self.collapsed then
self:Collapse()
end
end
end
---@return string statusID
function GGUI.Frame:GetStatus()
return tostring(self.activeStatusID)
end
function GGUI.Frame:RestoreSavedConfig(relativeTo)
--local savedPosInfo = GGUI:GetConfig("savedPos_" .. self.frameID)
local savedPosInfo = self.frameConfigTable["savedPos_" .. self.frameID]
if savedPosInfo then
relativeTo = relativeTo or UIParent
self.frame.hookFrame:ClearAllPoints()
self.frame.hookFrame:SetPoint("CENTER", relativeTo, "CENTER", savedPosInfo.offsetX, savedPosInfo.offsetY)
end
if self.collapseable then
if self.frameConfigTable["collapsed_" .. self.frameID] then
self:Collapse()
end
end
end
function GGUI.Frame:SavePosition(offsetX, offsetY)
self.frameConfigTable["savedPos_" .. self.frameID] = {
offsetX = offsetX,
offsetY = offsetY,
}
end
--- GGUI Icon
---@class GGUI.IconConstructorOptions: GGUI.ConstructorOptions
---@field parent? Frame
---@field offsetX? number
---@field offsetY? number
---@field texturePath? string | number
---@field sizeX? number
---@field sizeY? number
---@field qualityIconScale? number
---@field anchorA? FramePoint
---@field anchorB? FramePoint
---@field anchorParent? Region
---@field hideQualityIcon? boolean
---@field isAtlas? boolean
---@field desaturate? boolean
---@class GGUI.Icon : GGUI.Widget
---@overload fun(options:GGUI.IconConstructorOptions): GGUI.Icon
GGUI.Icon = GGUI.Widget:extend()
function GGUI.Icon:new(options)
options = options or {}
options.offsetX = options.offsetX or 0
options.offsetY = options.offsetY or 0
self.defaultTexture = options.texturePath or GGUI.CONST.EMPTY_TEXTURE
options.sizeX = options.sizeX or 40
options.sizeY = options.sizeY or 40
options.anchorA = options.anchorA or "CENTER"
options.anchorB = options.anchorB or "CENTER"
options.qualityIconScale = options.qualityIconScale or 1
self.hideQualityIcon = options.hideQualityIcon or false
---@type ItemMixin?
self.item = nil
self.isAtlas = options.isAtlas or false
self.desaturate = options.desaturate or false
local newIcon = CreateFrame("Button", nil, options.parent, "GameMenuButtonTemplate")
GGUI.Icon.super.new(self, newIcon)
newIcon:SetPoint(options.anchorA, options.anchorParent, options.anchorB, options.offsetX, options.offsetY)
newIcon:SetSize(options.sizeX, options.sizeY)
newIcon:SetNormalFontObject("GameFontNormalLarge")
newIcon:SetHighlightFontObject("GameFontHighlightLarge")
if self.isAtlas then
newIcon:SetNormalAtlas(self.defaultTexture)
else
newIcon:SetNormalTexture(self.defaultTexture)
end
newIcon.qualityIcon = GGUI.QualityIcon({
parent = self.frame,
sizeX = options.sizeX * 0.50 * options.qualityIconScale,
sizeY = options.sizeY * 0.50 * options.qualityIconScale,
anchorParent = newIcon,
anchorA = "TOPLEFT",
anchorB = "TOPLEFT",
offsetX = -options.sizeX * 0.10 * options.qualityIconScale,
offsetY = options.sizeY * 0.10 * options.qualityIconScale,
})
newIcon.qualityIcon:Hide()
self.qualityIcon = newIcon.qualityIcon
self.frame:HookScript("OnClick", function()
if IsShiftKeyDown() and self.item then
self.item:ContinueOnItemLoad(function()
ChatEdit_InsertLink(self.item:GetItemLink())
end)
end
end)
if self.desaturate then
self:Desaturate()
end
end
---@class GGUI.IconSetItemOptions
---@field tooltipOwner? Frame
---@field tooltipAnchor? TooltipAnchor
---@field overrideQuality? number
---@param idLinkOrMixin number | string | ItemMixin
---@param options GGUI.IconSetItemOptions?
function GGUI.Icon:SetItem(idLinkOrMixin, options)
options = options or {}
local gIcon = self
if not idLinkOrMixin then
gIcon.frame:SetScript("OnEnter", nil)
gIcon.frame:SetScript("OnLeave", nil)
gIcon.qualityIcon:Hide()
GGUI:SetItemTooltip(gIcon.frame, nil)
if self.isAtlas then
gIcon.frame:SetNormalAtlas(self.defaultTexture)
else
gIcon.frame:SetNormalTexture(self.defaultTexture)
end
return
end
local item = nil
if type(idLinkOrMixin) == 'number' then
item = Item:CreateFromItemID(idLinkOrMixin)
elseif type(idLinkOrMixin) == 'string' then
item = Item:CreateFromItemLink(idLinkOrMixin)
elseif type(idLinkOrMixin) == 'table' and idLinkOrMixin.ContinueOnItemLoad then -- some small test if its a mixing
item = idLinkOrMixin
end
self.item = item
item:ContinueOnItemLoad(function()
gIcon.frame:SetNormalTexture(item:GetItemIcon())
GGUI:SetItemTooltip(gIcon.frame, item:GetItemLink(), options.tooltipOwner or gIcon.frame,
options.tooltipAnchor or "ANCHOR_RIGHT")
if options.overrideQuality then
gIcon.qualityIcon:SetQuality(options.overrideQuality)
else
local qualityID = GUTIL:GetQualityIDFromLink(item:GetItemLink())
gIcon.qualityIcon:SetQuality(qualityID)
end
if self.hideQualityIcon then
gIcon.qualityIcon:Hide()
end
end)
end
---@param qualityID number
function GGUI.Icon:SetQuality(qualityID)
if qualityID then
self.qualityIcon:SetQuality(qualityID)
self.qualityIcon:Show()
else
self.qualityIcon:Hide()
end
end
function GGUI.Icon:Saturate()
local texture = self.frame:GetNormalTexture()
if texture then
texture:SetVertexColor(1, 1, 1)
end
self.desaturate = false
end
function GGUI.Icon:Desaturate()
local texture = self.frame:GetNormalTexture()
if texture then
texture:SetVertexColor(0.2, 0.2, 0.2)
end
self.desaturate = true
end
--- GGUI.QualityIcon
---@class GGUI.QualityIconConstructorOptions : GGUI.ConstructorOptions
---@field parent Frame
---@field sizeX? number
---@field sizeY? number
---@field anchorParent? Region
---@field anchorA? FramePoint
---@field anchorB? FramePoint
---@field offsetX? number
---@field offsetY? number
---@field initialQuality? number
---@field tooltipOptions? GGUI.TooltipOptions
---@class GGUI.QualityIcon : GGUI.Widget
---@overload fun(options:GGUI.QualityIconConstructorOptions): GGUI.QualityIcon
GGUI.QualityIcon = GGUI.Widget:extend()
function GGUI.QualityIcon:new(options)
options = options or {}