-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMoLibUI.lua
1749 lines (1647 loc) · 55.9 KB
/
MoLibUI.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
--[[
MoLib (GUI part) -- (c) 2019 moorea@ymail.com (MooreaTv)
Covered by the GNU Lesser General Public License v3.0 (LGPLv3)
NO WARRANTY
(contact the author if you need a different license)
]] --
-- our name, our empty default (and unused) anonymous ns
local addonName, _ns = ...
local ML = _G[addonName]
if ML.isLegacy then
function GetPhysicalScreenSize()
local width, height = strmatch(({GetScreenResolutions()})[GetCurrentResolution()], "(%d+)x(%d+)")
return tonumber(width), tonumber(height)
end
end
--[[ let's use the proper one below in new ShowConfigPanel/ConfigPanel instead of this:
if InterfaceOptionsFrame_OpenToCategory == nil then
-- Stolen from https://github.com/Gethe/wow-ui-source/blob/10.2.7/Interface/AddOns/Blizzard_Settings_Shared/Blizzard_Deprecated.lua
function InterfaceOptionsFrame_OpenToCategory(categoryIDOrFrame)
if type(categoryIDOrFrame) == "table" then
local categoryID = categoryIDOrFrame.name;
return Settings.OpenToCategory(categoryID);
else
return Settings.OpenToCategory(categoryIDOrFrame);
end
end
end
]]
function ML:ShowConfigPanel(p)
if ML.configCategory then
Settings.OpenToCategory(ML.configCategory.ID)
return
end
-- Show config panel
-- used to be/need InterfaceOptionsList_DisplayPanel(BVP.optionsPanel)
-- also used to need InterfaceOptionsFrame which is gone in dragonflight
if InterfaceOptionsFrame ~= nil then
InterfaceOptionsFrame:Show()
end
InterfaceOptionsFrame_OpenToCategory(p) -- gets our name selected
end
function ML:ConfigPanel(frame)
if Settings == nil then
-- Older code
return InterfaceOptions_AddCategory(frame)
end
ML.configCategory = Settings.RegisterCanvasLayoutCategory(frame, ML.name)
Settings.RegisterAddOnCategory(ML.configCategory)
frame.OnCommit = frame.okay
frame.OnDefault = frame.default
frame.OnRefresh = frame.refresh
end
function ML:GetMouseFocus()
if GetMouseFocus ~= nil then
return GetMouseFocus()
end
return GetMouseFoci()[1]
end
ML.id = 0
function ML:NextId()
self.id = self.id + 1
return self.id
end
-- use 2 or 4 for sizes so there are 1/2, 1/4 points
-- can also be used for numbers passing .1, .01 etc for n digits precision
function ML:round(x, precision)
precision = precision or 1
if precision < 1 then
-- when called with .1, .01 ... and we turn it into 1/10 1/100 ...
local p = math.floor(1 / precision + 0.5)
return math.floor(x * p + 0.5) / p
end
local i = math.floor(x / precision + 0.5) * precision
return i
end
-- for sizes, don't shrink but also... 0.9 because floating pt math
-- also works symmetrically for negative numbers (ie round up -1.3 -> -2)
function ML:roundUp(x, precision)
precision = precision or 1
local sign = 1
if x < 0 then
sign = -1
x = -x
end
local i = math.ceil(x / precision - 0.1) * precision
return sign * i
end
function ML:scale(x, s, precision)
return ML:round(x * s, precision) / s
end
function ML:scaleUp(x, s, precision)
return ML:roundUp(x * s, precision) / s
end
-- Makes sure the frame has the right pixel alignment and same padding with respect to
-- bottom right corner that it has on top left with its children objects.
-- returns a new scale to potentially be used if not recalculating the bottom right margins
function ML:SnapFrame(f)
return self:PixelPerfectSnap(f, 2, true) -- 2 so we get dividable by 2 dimensions, true = from top and not bottom corner
end
function ML:NormalizeFont(font)
if not font then
self:Debug(4, "leaving unspecified font %", font)
return font
end
local fontObj = font
if type(font) == "string" then
if _G[font] then
fontObj = _G[font]
else
ML:DebugStack("Invalid font name %", font)
return nil
end
end
return fontObj
end
-- WARNING, Y axis is such as positive is down, unlike rest of the wow api which has + offset going up
-- but all the negative numbers all over, just for Y axis, got to me
-- physical screen based attached frame (used to be WorldFrame but that can be moved)
function ML.ScreenFrame(addon)
return addon:Frame(nil, nil, nil, addon:PixelPerfectFrame(true))
end
if ML.isLegacy and C_Timer == nil then
C_Timer = {}
C_Timer.timers = {}
local timerFrame = CreateFrame("Frame", "C_TimerFrame")
function ML.TimerOnUpdate(_self, elapsed)
ML:Debug(9, "C_TimerOnUpdate elapsed %", elapsed)
for i, v in ipairs(C_Timer.timers) do
ML:Debug(9, "C_TimerOnUpdate timer %: %", i, v)
if v.cancelled then
table.remove(C_Timer.timers, i)
break
end
if v.timeLeft < 0 then
v.callback()
if v.iterations ~= nil then
v.iterations = v.iterations - 1
if v.iterations <= 0 then
table.remove(C_Timer.timers, i)
end
end
v.timeLeft = v.duration
else
v.timeLeft = v.timeLeft - elapsed
end
end
end
timerFrame:SetScript("OnUpdate",ML.TimerOnUpdate)
function C_Timer.NewTicker(duration, callback, iterations)
local t = {
duration = duration,
timeLeft = duration,
callback = callback,
iterations = iterations,
cancelled = false,
Cancel = function(self)
self.cancelled = true
end,
IsCancelled = function(self)
return self.cancelled
end
}
table.insert(C_Timer.timers, t)
return t
end
function C_Timer.NewTimer(duration, callback)
C_Timer.NewTicker(duration, callback, 1)
end
function C_Timer.After(duration, callback)
C_Timer.NewTicker(duration, callback, 1)
end
function GetServerTime()
return time()
end
end
function ML:SetDebugBackground(f, alpha, ...)
f.bg = f:CreateTexture(nil, "BACKGROUND")
if ML.isLegacy then
f.bg:SetTexture(...)
else
f.bg:SetColorTexture(...)
f.bg:SetIgnoreParentAlpha(true)
end
f.bg:SetAlpha(alpha)
f.bg:SetAllPoints()
end
-- parent should be null or a child or grandchild of a pixelPerfectFrame()
function ML.Frame(addon, name, global, template, parent, typ) -- to not shadow self below but really call with Addon:Frame(name)
local f = CreateFrame(typ or "Frame", global, parent or addon:PixelPerfectFrame(), template)
f:SetSize(1, 1) -- need a starting size for most operations
local ct = f.CreateTexture
f.CreateTexture = function(w,...)
local t = ct(w,...)
if t.SetColorTexture == nil then
t.SetColorTexture = t.SetTexture
end
return t
end
if addon.debug and addon.debug >= 8 then
addon:Debug(8, "Debug level 8 is on, putting debug background on frame %", name)
addon:SetDebugBackground(f, 0.2, .1, .2, .7)
end
f.name = name
f.children = {}
f.numObjects = 0
-- either a font name or a font object, convert to object
f.SetDefaultFont = function(w, font)
w.defaultFont = addon:NormalizeFont(font)
end
f.Snap = function(w)
addon:GridUpdateHeader(w)
w:setSizeToChildren()
addon:SnapFrame(w)
end
f.Scale = function(w, ...)
w:setScale(...)
end
f.ChangeScale = function(w, newScale)
addon:ChangeScale(w, newScale)
end
f.Init = function(self)
addon:Debug("Calling Init() on all % children", #self.children)
for _, v in ipairs(self.children) do
v:Init()
end
end
-- returns opposite corners 4 coordinates:
-- BottomRight(x,y) , TopLeft(x,y)
f.calcCorners = function(self)
local minX = 99999999
local maxX = 0
local minY = 99999999
local maxY = 0
local numChildren = 0
local fx = self:GetLeft()
local fy = self:GetTop()
for _, v in ipairs(self.children) do
local x = v:GetRight() or 0
local l = v:GetLeft() or 0
local y = v:GetBottom() or 0
local t = v:GetTop() or 0
local extraW = v.extraWidth or 0
local extraH = v.extraHeight or 0
-- ignore nil strings (which have wild width somehow)
local validChildren = (v.GetText == nil) or (v:GetText() ~= nil and #v:GetText() > 0)
if v.GetStringWidth and validChildren then
-- recover from the ... truncation
local curW = ML:round(x - l, 0.001)
local strWextra = ML:round(v:GetStringWidth() + extraW, 0.001)
if strWextra ~= curW then
local nx = l + v:GetStringWidth() + extraW -- not rounding here
addon:Debug(4, "changing font coords for % % to % because of str width % vs cur w %", v:GetText(), x, nx,
strWextra, curW)
x = nx
-- We need to ignore/fix the height as it's also wrong and GetStringHeight() is also not 1 line...
local _, fh = v:GetFont()
y = t - fh
else
addon:Debug(8, "not changing % w % strW %", v:GetText(), curW, strWextra)
end
extraW = 0
end
if validChildren then
maxX = math.max(maxX, x + extraW)
minX = math.min(minX, l)
maxY = math.max(maxY, t)
minY = math.min(minY, y - extraH)
numChildren = numChildren + 1
if v.mirror then
v.mirror:SetSize(x-l, y-t)
v.mirror:SetPoint("BOTTOMLEFT", self, "TOPLEFT", l-fx, t-fy)
end
end
end
addon:Debug(6, "Found corners for % children to be topleft % , % to bottomright %, %", numChildren, maxX, minY,
minX, maxY)
return maxX, minY, minX, maxY
end
f.setSizeToChildren = function(self)
local mx, my, l, t = self:calcCorners()
local x = self:GetLeft()
local y = self:GetTop()
if not x or not y then
addon:Warning("Frame has no left or top! % %", x, y)
return
end
local w = mx - l
local h = t - my
local paddingX = 2 * (l - x)
local paddingY = 2 * (y - t)
addon:Debug(4, "Calculated bottom right x % y % -> w % h % padding % x %", x, y, w, h, paddingX, paddingY)
self:SetWidth(w + paddingX)
self:SetHeight(h + paddingY)
end
-- Scales a frame so the children objects fill up the frame in width or height
-- (aspect ratio isn't changed)
-- This is used for the dbox full screen splash for instance.
-- TODO: combine scale change with pixel perfect to do change directly right
f.setScale = function(self, overridePadding)
local mx, my, l, t = self:calcCorners()
local x = self:GetLeft()
local y = self:GetTop()
if not x or not y then
addon:DebugStack("Frame has no left or top! % % in setScale", x, y)
end
local w = mx - l
local h = t - my
local paddingX = 2 * (l - x)
local paddingY = 2 * (y - t)
addon:Debug(6, "setScale bottom right x % y % -> w % h % padding % x %", x, y, w, h, paddingX, paddingY)
local nw = w
local nh = h
if overridePadding ~= nil then
--[[ local firstChild = self.children[1]
local pt1, _, pt2, x, y = firstChild:GetPoint()
if pt1:match("TOP") then
addon:Debug("Adjusting first child top y anchor from % to %", y, overridePadding)
y = -overridePadding
firstChild:SetPoint(pt1, self, pt2, x, y)
end
if pt1:match("LEFT") then
addon:Debug("Adjusting first child left x anchor from % to %", x, overridePadding)
firstChild:SetPoint(pt1, self, pt2, overridePadding, y) -- use the adjusted y for TOPLEFT
end
]]
paddingX = 2 * overridePadding
paddingY = 2 * overridePadding
end
nw = nw + paddingX
nh = nh + paddingY
local cw = self:GetWidth() -- current
local ch = self:GetHeight()
local sX = cw / nw
local sY = ch / nh
local scale = math.min(sX, sY)
if addon.NO_SNAPSCALE then
addon:DebugStack("NO_SNAPSCALE: not changing SCALE to % (sx % sy %)", scale, sX, sY)
return
end
self:ChangeScale(self:GetScale() * scale)
addon:Debug(5, "calculated scale x % scale y % for nw % nh % -> % -> %", sX, sY, nw, nh, scale, self:GetScale())
end
-- Used instead of SetPoint directly to move 2 linked object (eg textures for animation group) together
local setPoint = function(sf, pt, ...)
addon:Debug(8, "setting point %", pt)
sf:SetPoint(pt, ...)
if sf.linked then
sf.linked:SetPoint(pt, ...)
end
end
-- place inside the parent at offset x,y from corner of parent
local placeInside = function(sf, x, y, point)
point = point or "TOPLEFT"
x = x or 16
y = y or 16
sf:setPoint(point, x, -y)
return sf
end
-- place below (previously placed item typically)
local placeBelow = function(sf, below, x, y, point1, point2)
x = x or 0
y = y or 8
sf:setPoint(point1 or "TOPLEFT", below, point2 or "BOTTOMLEFT", x, -y)
return sf
end
-- place to the right of last widget
local placeRight = function(sf, nextTo, x, y, point1, point2)
x = x or 16
y = y or 0
sf:setPoint(point1 or "TOPLEFT", nextTo, point2 or "TOPRIGHT", x, -y)
return sf
end
-- place to the left of last widget
local placeLeft = function(sf, nextTo, x, y, point1, point2)
x = x or -16
y = y or 0
sf:setPoint(point1 or "TOPRIGHT", nextTo, point2 or "TOPLEFT", x, -y)
return sf
end
-- Place (below) relative to previous one. optOffsetX is relative to the left margin
-- established by first widget placed (placeInside)
-- such as changing the order of widgets doesn't change the left/right offset
-- in other words, offsetX is absolute to the left margin instead of relative to the previously placed object
f.Place = function(self, object, optOffsetX, optOffsetY, point1, point2)
self.numObjects = self.numObjects + 1
addon:Debug(7, "called Place % n % o %", self.name, self.numObjects, self.leftMargin)
if self.numObjects == 1 then
-- first object: place inside
object:placeInside(optOffsetX, optOffsetY, point1)
self.leftMargin = 0
else
optOffsetX = optOffsetX or 0
local y = (optOffsetY or 8) + (self.lastAdded.extraHeight or 0)
-- subsequent, place after the previous one but relative to initial left margin
object:placeBelow(self.lastAdded, optOffsetX - self.leftMargin, y, point1, point2)
self.leftMargin = optOffsetX
end
self.lastAdded = object
self.lastLeft = object
return object
end
f.PlaceRight = function(self, object, optOffsetX, optOffsetY, point1, point2)
self.numObjects = self.numObjects + 1
if self.numObjects == 1 then
addon:ErrorAndThrow("PlaceRight() should not be the first call, Place() should")
end
-- place to the right of previous one on the left
-- if the previous widget has text, add the text length (eg for check buttons)
local x = (optOffsetX or 16) + (self.lastLeft.extraWidth or 0)
object:placeRight(self.lastLeft, x, optOffsetY, point1, point2)
self.lastLeft = object
return object
end
-- doesn't change lastLeft, meant to be called to put 1 thing to the left of a centered object atm
f.PlaceLeft = function(self, object, optOffsetX, optOffsetY, point1, point2)
self.numObjects = self.numObjects + 1
if self.numObjects == 1 then
addon:ErrorAndThrow("PlaceLeft() should not be the first call, Place() should")
end
-- place to the left of previous one
-- if the previous widget has text, add the text length (eg for check buttons)
local x = (optOffsetX or -16)
object:placeLeft(self.lastLeft, x, optOffsetY, point1, point2)
-- self.lastLeft = object
return object
end
f.PlaceGrid = function(self, object, x, y, optOffsetX, optOffsetY)
if not self.grid then
self.grid = {}
end
if not self.grid[x] then
self.grid[x] = {}
end
self.grid[x][y] = object
object.inGrid = {x, y}
if x == 1 then
if y == 1 then
object:Place(optOffsetX, optOffsetY)
else
self.numObjects = self.numObjects + 1
local prev = self.grid[1][y - 1]
local yO = (optOffsetY or 8) + (prev.extraHeight or 0)
object:placeBelow(prev, optOffsetX, yO)
end
else
self.numObjects = self.numObjects + 1
if y == 1 then
local prev = self.grid[x - 1][1]
object:placeRight(prev, optOffsetX, optOffsetY)
else
local leftAnchor = self.grid[x][1]
object:setPoint("LEFT", leftAnchor, "LEFT", optOffsetX, 0)
local topAnchor = self.grid[1][y]
local yAdjustment = addon:WidgetHeightAdjustment(object) - addon:WidgetHeightAdjustment(topAnchor)
object:setPoint("BOTTOM", topAnchor, "BOTTOM", 0, (optOffsetY or 0) + yAdjustment)
end
end
end
-- To be used by the various factories/sub widget creation to add common methods to them
-- (learned after coming up with this pattern on my own that that this seems to be
-- called Mixins in blizzard code, though that doesn't cover forwarding or children tracking)
function f:addMethods(widget)
widget.setPoint = setPoint
widget.placeInside = placeInside
widget.placeBelow = placeBelow
widget.placeRight = placeRight
widget.placeLeft = placeLeft
widget.parent = self
widget.Place = function(...)
-- add missing parent as first arg
widget.parent:Place(...)
return widget -- because :Place is typically last, so don't return parent/self but the widget
end
widget.PlaceRight = function(...)
widget.parent:PlaceRight(...)
return widget
end
widget.PlaceLeft = function(...)
widget.parent:PlaceLeft(...)
return widget
end
widget.PlaceGrid = function(...)
widget.parent:PlaceGrid(...)
return widget
end
if not widget.Init then
widget.Init = function(w)
addon:Debug(7, "Nothing special to init in %", w:GetObjectType())
end
end
-- piggy back on 1 to decide both as it doesn't make sense to only define one of the two
if not widget.DoDisable then
widget.DoDisable = widget.Disable
widget.DoEnable = widget.Enable
end
widget.Snap = function(w)
w:setSizeToChildren()
addon:SnapFrame(w)
end
table.insert(self.children, widget) -- keep track of children objects
end
f.addText = function(self, text, font)
local fontObj = addon:NormalizeFont(font) or self.defaultFont or GameFontHighlightSmall -- different default?
local t = self:CreateFontString(nil, "ARTWORK", nil)
t:SetFontObject(fontObj)
if self.defaultTextColor then
t:SetTextColor(unpack(self.defaultTextColor))
end
t:SetText(text)
t:SetJustifyH("LEFT")
t:SetJustifyV("TOP")
self:addMethods(t)
if t.SetMaxLines == nil then
t.SetMaxLines = function () end -- isLegacy case
end
return t
end
f.addTextFrame = function(self, text, font, tfTemplate)
local t = self:addText(text, font)
local cf = CreateFrame("Frame", nil, self, tfTemplate)
cf.name = "textframe"
cf:SetAllPoints(t)
cf:Show()
if addon.debug then
addon:Debug("Debug level is on, putting debug background on text frame %", text)
addon:SetDebugBackground(cf, 0.5, .2, .7, .7)
end
t.frame = cf
return t
end
f.addTextButton = function(self, text, font, tooltipText, cb, bTemplate)
local t = self:addText(text, font)
local cf = CreateFrame("Button", nil, self, bTemplate)
cf.name = "textbutton"
if addon.debug then
addon:Debug("Debug level is on, putting debug background on text frame %", text)
addon:SetDebugBackground(cf, 0.5, .7, .2, .7)
end
self:addButtonBehavior(cf, text, tooltipText, cb)
t.button = cf
t.mirror = cf
return t
end
--[[ f.drawRectangle = function(self, layer)
local r = self:CreateTexture(nil, layer or "BACKGROUND")
self:addMethods(r)
return r
end
]]
-- adds a line of given thickness and color
f.addLine = function(self, thickness, r, g, b, a, layer, dontaddtochildren)
if addon.isLegacy then
-- TODO support at least horizontal and vertical lines
return {}
end
local l = self:CreateLine(nil, layer or "BACKGROUND")
l.originalThickness = thickness or 1
l:SetThickness(l.originalThickness)
l:SetColorTexture(r or 1, g or 1, b or 1, a or 1)
if not dontaddtochildren then
self:addMethods(l)
end
return l
end
-- adds a border, thickness is in pixels (will be altered based on scale)
-- the border isn't added to regular children
f.addBorder = function(self, padX, padY, thickness, r, g, b, alpha, layer)
if addon.isLegacy then
-- TODO support at least horizontal and vertical lines
return {}
end
padX = padX or 0.5
padY = padY or 0.5
layer = layer or "BACKGROUND"
r = r or 1
g = g or 1
b = b or 1
alpha = alpha or 1
thickness = thickness or 1
if not f.border then
f.border = {}
end
-- true argument to addLine == only store the line in the border list, so it doesn't get wiped/handled like regular children
local top = self:addLine(thickness, r, g, b, alpha, layer, true)
top:SetStartPoint("TOPLEFT", padX, -padY)
top:SetEndPoint("TOPRIGHT", -padX, -padY)
top:SetIgnoreParentAlpha(true)
table.insert(f.border, top)
local left = self:addLine(thickness, r, g, b, alpha, layer, true)
left:SetStartPoint("TOPLEFT", padX, -padY)
left:SetEndPoint("BOTTOMLEFT", padX, padY)
left:SetIgnoreParentAlpha(true)
table.insert(f.border, left)
local bottom = self:addLine(thickness, r, g, b, alpha, layer, true)
bottom:SetStartPoint("BOTTOMLEFT", padX, padY)
bottom:SetEndPoint("BOTTOMRIGHT", -padX, padY)
bottom:SetIgnoreParentAlpha(true)
table.insert(f.border, bottom)
local right = self:addLine(thickness, r, g, b, alpha, layer, true)
right:SetStartPoint("BOTTOMRIGHT", -padX, padY)
right:SetEndPoint("TOPRIGHT", -padX, -padY)
right:SetIgnoreParentAlpha(true)
table.insert(f.border, right)
self:updateBorder()
end
f.updateBorder = function(self)
if not self.border or #self.border == 0 then
return
end
local s = self:GetScale()
for _, b in ipairs(self.border) do
b:SetThickness(b.originalThickness / s)
end
end
-- creates a texture so it can be placed
-- (arguments are optional)
f.addTexture = function(self, layer)
local t = self:CreateTexture(nil, layer or "BACKGROUND")
addon:Debug(8, "textures starts with % points", t:GetNumPoints())
self:addMethods(t)
return t
end
-- Add an animation of 2 textures (typically glow)
f.addAnimatedTexture = function(self, baseId, glowId, duration, glowAlpha, looping, layer)
local base = self:addTexture(layer)
base:SetTexture(baseId)
if not addon.isLegacy and not base:IsObjectLoaded() then
addon:Warning("Texture % not loaded yet... use ML:PreloadTextures()...", baseId)
base:SetSize(64, 64)
end
addon:Debug(1, "Setting base texture % - height = %", baseId, base:GetHeight())
local glow = self:CreateTexture(nil, layer or "BACKGROUND")
glow:SetTexture(glowId)
glow:SetBlendMode("ADD")
glow:SetAlpha(0) -- start with no change
if not addon.isLegacy then
glow:SetIgnoreParentAlpha(true)
end
local ag = glow:CreateAnimationGroup()
base.animationGroup = ag
local anim = ag:CreateAnimation("Alpha")
if not addon.isLegacy then
anim:SetFromAlpha(0)
anim:SetToAlpha(glowAlpha or 0.2)
end
ag:SetLooping(looping or "BOUNCE")
anim:SetDuration(duration or 2)
base.linked = glow
ag:Play()
return base
end
f.addCheckBox = function(self, text, tooltip, optCallback)
local lname = nil
if addon.isLegacy then
lname = "MoLib" .. self.name .. "ChkBox".. tostring(addon:NextId())
end
local c = CreateFrame("CheckButton", lname, self, "InterfaceOptionsCheckButtonTemplate")
addon:Debug(8, "check box starts with % points", c:GetNumPoints())
if c.Text == nil then
c.Text = _G[c:GetName().."Text"]
end
c.Text:SetText(text)
if tooltip then
c.tooltipText = tooltip
if c:GetScript("OnEnter") == nil then
c:SetScript("OnEnter", function()
addon:ShowToolTip(c)
end)
c:SetScript("OnLeave", function()
GameTooltip:Hide()
end)
end
end
self:addMethods(c)
c.extraWidth = c.Text:GetWidth()
if optCallback then
c:SetScript("OnClick", optCallback)
else
-- Work around bug in 9.0.2 where non existent SetValue is called through OnClick
c:SetScript("OnClick", nil)
end
-- if not c.SetValue then
-- c.SetValue = function()
-- end
-- end
return c
end
-- create a slider with the range [minV...maxV] and optional step, low/high labels and optional
-- strings to print in parenthesis after the text title
f.addSlider = function(self, text, tooltip, minV, maxV, step, lowL, highL, valueLabels)
minV = minV or 0
maxV = maxV or 10
step = step or 1
lowL = lowL or tostring(minV)
highL = highL or tostring(maxV)
local lname= nil
if addon.isLegacy then
lname = "MoLib" .. self.name .. "Slider".. tostring(addon:NextId())
end
local toc = select(4, GetBuildInfo())
if toc == 11504 then
if _G["molibSliderWarning"] == nil then
addon:Warning("Blizzard broke OptionsSliderTemplate - set values by hand meanwhile")
_G["molibSliderWarning"] = true
end
local s = self:addText("Broken slider:" .. text)
s.SetValue = function(ws, v)
ws:SetText("Broken slider:" .. text .. ": " .. v)
ws.value = v
end
s.GetValue = function(ws) return ws.value end
s.DoDisable = function() end
s.DoEnable = function() end
return s
end
local s = CreateFrame("Slider", lname, self, "OptionsSliderTemplate")
if s.Text == nil then
s.Text = _G[name.."Text"]
s.Low = _G[name.."Low"]
s.High = _G[name.."High"]
end
s.DoDisable = BlizzardOptionsPanel_Slider_Disable -- what does enable/disable do ? seems we need to call these
s.DoEnable = BlizzardOptionsPanel_Slider_Enable
s:SetValueStep(step)
if s.SetStepsPerPage ~= nil then -- doesn't exist in isLegacy
s:SetStepsPerPage(step)
s:SetObeyStepOnDrag(true)
end
s:SetMinMaxValues(minV, maxV)
s.Text:SetFontObject(GameFontNormal)
-- not centered, so changing (value) doesn't wobble the whole thing
-- (justifyH left alone didn't work because the point is also centered)
s.Text:SetPoint("LEFT", s, "TOPLEFT", 6, 0)
s.Text:SetJustifyH("LEFT")
s.Text:SetText(text)
if tooltip then
s.tooltipText = tooltip
end
s.Low:SetText(lowL)
s.High:SetText(highL)
s:SetScript("OnValueChanged", function(w, value)
local sVal = tostring(ML:round(value, 0.001))
if valueLabels and valueLabels[value] then
sVal = valueLabels[value]
elseif valueLabels and valueLabels[sVal] then
sVal = valueLabels[sVal]
else
if value == minV then
sVal = lowL
elseif value == maxV then
sVal = highL
end
end
w.Text:SetText(text .. ": " .. sVal)
if w.callBack then
w:callBack(value)
end
end)
self:addMethods(s)
return s
end
f.addButtonBehavior = function(_self, c, text, tooltip, cb)
if tooltip then
c.tooltipText = tooltip -- TODO: style/font of tooltip for button is wrong
end
local callback = cb
if type(cb) == "string" then
addon:Debug(4, "Setting callback for % to call Slash(%)", text, cb)
callback = function()
addon.Slash(cb)
end
addon:Debug(4, "Keeping original function for %", text)
end
if callback then
c:SetScript("OnClick", callback)
end
c:SetScript("OnEnter", function()
addon:Debug(7, "Show button tool tip...")
addon:ShowToolTip(c)
end)
c:SetScript("OnLeave", function()
addon:Debug(7, "Hide button tool tip...")
GameTooltip:Hide()
end)
end
-- the call back is either a function or a command to send to addon.Slash
f.addButton = function(self, text, tooltip, cb)
local lname= nil
if addon.isLegacy then
lname = "MoLib" .. self.name .. "Button".. tostring(addon:NextId())
end
local c = CreateFrame("Button", lname, self, "UIPanelButtonTemplate")
if c.Text == nil then
c.Text = _G[c:GetName().."Text"]
end
c.Text:SetText(text)
c:SetWidth(c.Text:GetStringWidth() + 20) -- need some extra spaces for corners
self:addButtonBehavior(c, text, tooltip, cb)
self:addMethods(c)
return c
end
f.addEditBox = function(self)
local e = CreateFrame("EditBox", nil, self)
e:SetFontObject(GameFontNormal)
if self.defaultTextColor then
e:SetTextColor(unpack(self.defaultTextColor))
end
self:addMethods(e)
return e
end
f.addScrollingFrame = function(self, width, height, noInset)
width = width or 400
height = height or 300
local s = CreateFrame("ScrollFrame", nil, self, "UIPanelScrollFrameTemplate")
s:SetSize(width, height)
if not noInset then
local inset = CreateFrame("Frame", nil, s, "InsetFrameTemplate")
inset:SetPoint("BOTTOMLEFT", -4, -4)
inset:SetPoint("TOPRIGHT", 4, 4)
s.extraHeight = 8 -- inset is 4+4 outside
s.inset = inset
end
s.extraWidth = 24 -- scrollbar is outside
s.setScrollChild = function(p, c) -- set scroll child relationship and fix the frame level (classic)
if p.inset then
c:SetFrameLevel(p.inset:GetFrameLevel() + 1)
end
p:SetScrollChild(c)
end
s.addScrollChild = function(p, frameType) -- nil frameType for our container frame
local c
if frameType then
c = CreateFrame(frameType, nil, s)
else
c = addon:Frame(nil, nil, nil, p)
end
p:setScrollChild(c)
return c
end
self:addMethods(s)
return s
end
f.addScrollEditFrame = function(self, width, height, font, noInset)
local s = self:addScrollingFrame(width, height, noInset)
local e = s:addScrollChild("EditBox")
e:SetWidth(width)
e:SetFontObject(font or f.defaultFont or ChatFontNormal)
if self.defaultTextColor then
e:SetTextColor(unpack(self.defaultTextColor))
end
e:SetMultiLine(true)
-- e:GetRegions():SetNonSpaceWrap(false)
-- e:GetRegions():SetWordWrap(false)
s.editBox = e
return s
end
local function dropdownInit(d)
addon:Debug(5, "drop down init called initDone=%", d.initDone)
if d.initDone then
return
end
addon:Debug(5, "drop down first time init called")
d.initDone = true
UIDropDownMenu_JustifyText(d, "CENTER")
UIDropDownMenu_Initialize(d, function(_w, _level, _menuList)
for _, v in ipairs(d.options) do
addon:Debug(5, "Creating dropdown entry %", v)
local info = UIDropDownMenu_CreateInfo() -- don't put it outside the loop!
info.tooltipOnButton = true
info.text = v.text
info.tooltipTitle = v.text
info.tooltipText = v.tooltip
info.value = v.value
info.func = function(entry)
if d.cb then
d.cb(entry.value)
end
UIDropDownMenu_SetSelectedID(d, entry:GetID())
end
UIDropDownMenu_AddButton(info)
end
end)
UIDropDownMenu_SetText(d, d.text)
-- Uh? one global for all dropdowns?? also possible taint issues
local width = _G["DropDownList1"] and _G["DropDownList1"].maxWidth or 0
addon:Debug(4, "Found dropdown width to be %", width)
if width > 0 then
UIDropDownMenu_SetWidth(d, width)
end
end
-- Note that trying to reuse the blizzard dropdown code instead of duplicating it cause some tainting
-- because said code uses a bunch of globals notably UIDROPDOWNMENU_MENU_LEVEL
-- create/show those widgets as late as possible
f.addDrop = function(self, text, tooltip, cb, options)
-- local name = self.name .. "drop" .. self.numObjects
local d = CreateFrame("Frame", nil, self, "UIDropDownMenuTemplate")
d.tooltipTitle = "Testing dropdown tooltip 1" -- not working/showing (so far)
d.tooltipText = tooltip
d.options = options
d.cb = cb
d.text = text
d.tooltipOnButton = true
d.Init = dropdownInit
self:addMethods(d)
self.lastDropDown = d
return d
end
if ML.widgetDemo then
f:addText("Testing 1 2 3... demo widgets:"):Place(50, 20)
local _cb1 = f:addCheckBox("A test checkbox", "A sample tooltip"):Place(0, 20) -- A: not here
local cb2 = f:addCheckBox("Another checkbox", "Another tooltip"):Place()
cb2:SetChecked(true)
local s2 = f:addSlider("Test slider", "Test slide tooltip", 1, 4, 1, "Test low", "Test high",
{"Value 1", "Value 2", "Third one", "4th value"}):Place(16, 30)
s2:SetValue(4)
f:addText("Real UI:"):Place(50, 40)
end
return f
end
function ML:Table(f, data)
for y, l in ipairs(data) do
for x, c in ipairs(l) do
self:Debug("adding at % %: %", x, y, c)
if type(c) == "string" then
c = f:addText(c)
end
c:PlaceGrid(x, y)
end
end
end
function ML:GetFullWidth(w)
return (w.GetStringWidth and w:GetStringWidth() or w:GetWidth()) + (w.extraWidth or 0)
end
-- todo: make up my mind about widget/frame/toplevel(addon) functions...
function ML:GridUpdateHeader(f)
if not f.grid then
return
end
for x, c in ipairs(f.grid) do