-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbutton.lua
1546 lines (1287 loc) · 51 KB
/
button.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
--[=[
callback format:
function(button, clickType, param1, param2)
end
--]=]
local detailsFramework = _G["DetailsFramework"]
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
local _
local unpack = unpack
local emptyFunction = function() end
local APIButtonFunctions = false
do
local metaPrototype = {
WidgetType = "button",
dversion = detailsFramework.dversion
}
--check if there's a metaPrototype already existing
if (_G[detailsFramework.GlobalWidgetControlNames["button"]]) then
--get the already existing metaPrototype
local oldMetaPrototype = _G[detailsFramework.GlobalWidgetControlNames["button"]]
--check if is older
if ( (not oldMetaPrototype.dversion) or (oldMetaPrototype.dversion < detailsFramework.dversion) ) then
--the version is older them the currently loading one
--copy the new values into the old metatable
for funcName, _ in pairs(metaPrototype) do
oldMetaPrototype[funcName] = metaPrototype[funcName]
end
end
else
--first time loading the framework
_G[detailsFramework.GlobalWidgetControlNames["button"]] = metaPrototype
end
end
local ButtonMetaFunctions = _G[detailsFramework.GlobalWidgetControlNames["button"]]
detailsFramework:Mixin(ButtonMetaFunctions, detailsFramework.SetPointMixin)
detailsFramework:Mixin(ButtonMetaFunctions, detailsFramework.FrameMixin)
detailsFramework:Mixin(ButtonMetaFunctions, detailsFramework.TooltipHandlerMixin)
detailsFramework:Mixin(ButtonMetaFunctions, detailsFramework.ScriptHookMixin)
------------------------------------------------------------------------------------------------------------
--metatables
ButtonMetaFunctions.__call = function(self)
local frameWidget = self.widget
detailsFramework:CoreDispatch((frameWidget:GetName() or "Button") .. ":__call()", self.func, frameWidget, "LeftButton", self.param1, self.param2)
end
------------------------------------------------------------------------------------------------------------
--members
--tooltip
local gmember_tooltip = function(object)
return object:GetTooltip()
end
--shown
local gmember_shown = function(object)
return object:IsShown()
end
--frame width
local gmember_width = function(object)
return object.button:GetWidth()
end
--frame height
local gmember_height = function(object)
return object.button:GetHeight()
end
--text
local gmember_text = function(object)
return object.button.text:GetText()
end
--function
local gmember_function = function(object)
return rawget(object, "func")
end
--text color
local gmember_textcolor = function(object)
return object.button.text:GetTextColor()
end
--text font
local gmember_textfont = function(object)
local fontface = object.button.text:GetFont()
return fontface
end
--text size
local gmember_textsize = function(object)
local _, fontsize = object.button.text:GetFont()
return fontsize
end
--texture
local gmember_texture = function(object)
return {object.button:GetNormalTexture(), object.button:GetHighlightTexture(), object.button:GetPushedTexture(), object.button:GetDisabledTexture()}
end
--locked
local gmember_locked = function(object)
return rawget(object, "is_locked")
end
ButtonMetaFunctions.GetMembers = ButtonMetaFunctions.GetMembers or {}
ButtonMetaFunctions.GetMembers["tooltip"] = gmember_tooltip
ButtonMetaFunctions.GetMembers["shown"] = gmember_shown
ButtonMetaFunctions.GetMembers["width"] = gmember_width
ButtonMetaFunctions.GetMembers["height"] = gmember_height
ButtonMetaFunctions.GetMembers["text"] = gmember_text
ButtonMetaFunctions.GetMembers["clickfunction"] = gmember_function
ButtonMetaFunctions.GetMembers["texture"] = gmember_texture
ButtonMetaFunctions.GetMembers["locked"] = gmember_locked
ButtonMetaFunctions.GetMembers["fontcolor"] = gmember_textcolor
ButtonMetaFunctions.GetMembers["fontface"] = gmember_textfont
ButtonMetaFunctions.GetMembers["fontsize"] = gmember_textsize
ButtonMetaFunctions.GetMembers["textcolor"] = gmember_textcolor --alias
ButtonMetaFunctions.GetMembers["textfont"] = gmember_textfont --alias
ButtonMetaFunctions.GetMembers["textsize"] = gmember_textsize --alias
ButtonMetaFunctions.__index = function(object, key)
local func = ButtonMetaFunctions.GetMembers[key]
if (func) then
return func(object, key)
end
local alreadyHaveKey = rawget(object, key)
if (alreadyHaveKey) then
return alreadyHaveKey
end
return ButtonMetaFunctions[key]
end
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
--tooltip
local smember_tooltip = function(object, value)
return object:SetTooltip (value)
end
--show
local smember_show = function(object, value)
if (value) then
return object:Show()
else
return object:Hide()
end
end
--hide
local smember_hide = function(object, value)
if (not value) then
return object:Show()
else
return object:Hide()
end
end
--frame width
local smember_width = function(object, value)
return object.button:SetWidth(value)
end
--frame height
local smember_height = function(object, value)
return object.button:SetHeight(value)
end
--text
local smember_text = function(object, value)
return object.button.text:SetText(value)
end
--function
local smember_function = function(object, value)
return rawset(object, "func", value)
end
--param1
local smember_param1 = function(object, value)
return rawset(object, "param1", value)
end
--param2
local smember_param2 = function(object, value)
return rawset(object, "param2", value)
end
--text color
local smember_textcolor = function(object, value)
local value1, value2, value3, value4 = detailsFramework:ParseColors(value)
return object.button.text:SetTextColor(value1, value2, value3, value4)
end
--text font
local smember_textfont = function(object, value)
return detailsFramework:SetFontFace (object.button.text, value)
end
--text size
local smember_textsize = function(object, value)
return detailsFramework:SetFontSize(object.button.text, value)
end
--texture
local smember_texture = function(object, value)
return detailsFramework:SetButtonTexture(object, value, 0, 1, 0, 1)
end
--locked
local smember_locked = function(object, value)
if (value) then
object.button:SetMovable(false)
return rawset(object, "is_locked", true)
else
object.button:SetMovable(true)
rawset(object, "is_locked", false)
return
end
end
--text align
local smember_textalign = function(object, value)
if (value == "left" or value == "<") then
object.button.text:SetPoint("left", object.button, "left", 2, 0)
object.capsule_textalign = "left"
elseif (value == "center" or value == "|") then
object.button.text:SetPoint("center", object.button, "center", 0, 0)
object.capsule_textalign = "center"
elseif (value == "right" or value == ">") then
object.button.text:SetPoint("right", object.button, "right", -2, 0)
object.capsule_textalign = "right"
end
end
ButtonMetaFunctions.SetMembers= ButtonMetaFunctions.SetMembers or {}
ButtonMetaFunctions.SetMembers["tooltip"] = smember_tooltip
ButtonMetaFunctions.SetMembers["show"] = smember_show
ButtonMetaFunctions.SetMembers["hide"] = smember_hide
ButtonMetaFunctions.SetMembers["width"] = smember_width
ButtonMetaFunctions.SetMembers["height"] = smember_height
ButtonMetaFunctions.SetMembers["text"] = smember_text
ButtonMetaFunctions.SetMembers["clickfunction"] = smember_function
ButtonMetaFunctions.SetMembers["param1"] = smember_param1
ButtonMetaFunctions.SetMembers["param2"] = smember_param2
ButtonMetaFunctions.SetMembers["textcolor"] = smember_textcolor
ButtonMetaFunctions.SetMembers["textfont"] = smember_textfont
ButtonMetaFunctions.SetMembers["textsize"] = smember_textsize
ButtonMetaFunctions.SetMembers["fontcolor"] = smember_textcolor--alias
ButtonMetaFunctions.SetMembers["fontface"] = smember_textfont--alias
ButtonMetaFunctions.SetMembers["fontsize"] = smember_textsize--alias
ButtonMetaFunctions.SetMembers["texture"] = smember_texture
ButtonMetaFunctions.SetMembers["locked"] = smember_locked
ButtonMetaFunctions.SetMembers["textalign"] = smember_textalign
ButtonMetaFunctions.__newindex = function(object, key, value)
local func = ButtonMetaFunctions.SetMembers[key]
if (func) then
return func(object, value)
else
return rawset(object, key, value)
end
end
------------------------------------------------------------------------------------------------------------
--methods
---change the function which will be called when the button is pressed
---callback function will receive the blizzard button as first parameter, click type as second, param1 and param2 as third and fourth
---@param func function
---@param param1 any
---@param param2 any
---@param clickType string|nil
function ButtonMetaFunctions:SetClickFunction(func, param1, param2, clickType)
if (not clickType or string.find(string.lower(clickType), "left")) then
if (func) then
rawset(self, "func", func)
else
rawset(self, "func", emptyFunction)
end
if (param1 ~= nil) then
rawset(self, "param1", param1)
end
if (param2 ~= nil) then
rawset(self, "param2", param2)
end
elseif (clickType or string.find(string.lower(clickType), "right")) then
if (func) then
rawset(self, "funcright", func)
else
rawset(self, "funcright", emptyFunction)
end
end
end
function ButtonMetaFunctions:SetParameters(param1, param2)
if (param1 ~= nil) then
rawset(self, "param1", param1)
end
if (param2 ~= nil) then
rawset(self, "param2", param2)
end
end
---set the text shown on the button
---@param text string
function ButtonMetaFunctions:SetText(text)
self.button.text:SetText(text)
end
---set the text shown on the button and truncate it if it's too long
function ButtonMetaFunctions:SetTextTruncated(text, maxWidth)
self.button.text:SetText(text)
detailsFramework:TruncateText(self.button.text, maxWidth)
end
---set the color of the button text
---@param ... any
function ButtonMetaFunctions:SetTextColor(...)
local red, green, blue, alpha = detailsFramework:ParseColors(...)
self.button.text:SetTextColor(red, green, blue, alpha)
end
ButtonMetaFunctions.SetFontColor = ButtonMetaFunctions.SetTextColor --alias
---set the size of the button text
---@param ... number
function ButtonMetaFunctions:SetFontSize(...)
detailsFramework:SetFontSize(self.button.text, ...)
end
---set the font into the button text
---@param font string
function ButtonMetaFunctions:SetFontFace(font)
detailsFramework:SetFontFace(self.button.text, font)
end
---comment
---@param normalTexture any
---@param highlightTexture any
---@param pressedTexture any
---@param disabledTexture any
function ButtonMetaFunctions:SetTexture(normalTexture, highlightTexture, pressedTexture, disabledTexture)
if (normalTexture) then
self.button:SetNormalTexture(normalTexture)
elseif (type(normalTexture) ~= "boolean") then
self.button:SetNormalTexture("")
end
if (type(highlightTexture) == "boolean") then
if (highlightTexture and normalTexture and type(normalTexture) ~= "boolean") then
self.button:SetHighlightTexture(normalTexture, "ADD")
end
elseif (highlightTexture == nil) then
self.button:SetHighlightTexture("")
else
self.button:SetHighlightTexture(highlightTexture, "ADD")
end
if (type(pressedTexture) == "boolean") then
if (pressedTexture and normalTexture and type(normalTexture) ~= "boolean") then
self.button:SetPushedTexture(normalTexture)
end
elseif (pressedTexture == nil) then
self.button:SetPushedTexture("")
else
self.button:SetPushedTexture(pressedTexture, "ADD")
end
if (type(disabledTexture) == "boolean") then
if (disabledTexture and normalTexture and type(normalTexture) ~= "boolean") then
self.button:SetDisabledTexture(normalTexture)
end
elseif (disabledTexture == nil) then
self.button:SetDisabledTexture("")
else
self.button:SetDisabledTexture(disabledTexture, "ADD")
end
end
---return the texture set into the icon with SetIcon()
---@return number|nil texture
function ButtonMetaFunctions:GetIconTexture()
if (self.icon) then
return self.icon:GetTexture()
end
end
local noColor = {1, 1, 1, 1}
---add an icon to the left of the button text
---short method truncates the text: false = do nothing, nil = increate the button width, 1 = decrease the font size, 2 = truncate the text
---@param self table
---@param texture any
---@param width number|nil
---@param height number|nil
---@param layout "background|border|overlay|artwork"|nil
---@param texcoord table|nil
---@param overlay any
---@param textDistance number|nil
---@param leftPadding number|nil
---@param textHeight number|nil
---@param shortMethod any
---@param filterMode any
function ButtonMetaFunctions:SetIcon(texture, width, height, layout, texcoord, overlay, textDistance, leftPadding, textHeight, shortMethod, filterMode)
if (not self.icon) then
self.icon = self:CreateTexture(nil, "artwork")
self.icon:SetSize(self.height * 0.8, self.height * 0.8)
self.icon:SetPoint("left", self.widget, "left", 4 + (leftPadding or 0), 0)
self.icon.leftPadding = leftPadding or 0
self.widget.text:ClearAllPoints()
self.widget.text:SetPoint("left", self.icon, "right", textDistance or 2, 0 + (textHeight or 0))
end
overlay = overlay or noColor
local red, green, blue, alpha = detailsFramework:ParseColors(overlay or noColor)
local left, right, top, bottom = texcoord and texcoord[1], texcoord and texcoord[2], texcoord and texcoord[3], texcoord and texcoord[4]
texture, width, height, left, right, top, bottom, red, green, blue, alpha = detailsFramework:ParseTexture(texture, width, height, left, right, top, bottom, red, green, blue, alpha)
if (red == nil) then
red, green, blue, alpha = 1, 1, 1, 1
end
if (type(texture) == "string") then
local isAtlas = C_Texture.GetAtlasInfo(texture)
if (isAtlas) then
self.icon:SetAtlas(texture)
elseif (detailsFramework:IsHtmlColor(texture)) then
local r, g, b, a = detailsFramework:ParseColors(texture)
self.icon:SetColorTexture(r, g, b, a)
else
self.icon:SetTexture(texture, nil, nil, filterMode)
end
else
self.icon:SetTexture(texture, nil, nil, filterMode)
end
self.icon:SetSize(width or self.height * 0.8, height or self.height * 0.8)
self.icon:SetDrawLayer(layout or "artwork")
self.icon:SetTexCoord(left, right, top, bottom)
self.icon:SetVertexColor(red, green, blue, alpha)
local buttonWidth = self.button:GetWidth()
local iconWidth = self.icon:GetWidth()
local textWidth = self.button.text:GetStringWidth()
if (textWidth > buttonWidth - 15 - iconWidth) then
if (shortMethod == false) then
elseif (not shortMethod) then
local new_width = textWidth + 15 + iconWidth
self.button:SetWidth(new_width)
elseif (shortMethod == 1) then
local loop = true
local textSize = 11
while (loop) do
if (textWidth + 15 + iconWidth < buttonWidth or textSize < 8) then
loop = false
break
else
detailsFramework:SetFontSize(self.button.text, textSize)
textWidth = self.button.text:GetStringWidth()
textSize = textSize - 1
end
end
elseif (shortMethod == 2) then
detailsFramework:TruncateText(self.button.text, self:GetWidth() - self.icon:GetWidth() - 15)
end
end
end
---@param self df_button
---@param filterMode texturefilter
function ButtonMetaFunctions:SetIconFilterMode(filterMode)
if (self.icon) then
self.icon:SetTexture(self.icon:GetTexture(), nil, nil, filterMode)
end
end
---query if the button is enabled or not
---@return boolean
function ButtonMetaFunctions:IsEnabled()
return self.button:IsEnabled()
end
---enable the button making it clickable and not grayed out
---@return unknown
function ButtonMetaFunctions:Enable()
return self.button:Enable()
end
---disable the button making it unclickable and grayed out
---@return unknown
function ButtonMetaFunctions:Disable()
if (self.color_texture) then
self.color_texture:SetVertexColor(0.14, 0.14, 0.14)
end
return self.button:Disable()
end
---@param enable boolean
function ButtonMetaFunctions:SetEnabled(enable)
if (enable) then
self:Enable()
else
self:Disable()
end
end
---simulate a click on the button
function ButtonMetaFunctions:Exec()
local frameWidget = self.widget
detailsFramework:CoreDispatch((frameWidget:GetName() or "Button") .. ":Exec()", self.func, frameWidget, "LeftButton", self.param1, self.param2)
end
---simulate a click on the button, but this function is called with a different name
function ButtonMetaFunctions:Click()
local frameWidget = self.widget
detailsFramework:CoreDispatch((frameWidget:GetName() or "Button") .. ":Click()", self.func, frameWidget, "LeftButton", self.param1, self.param2)
end
---simulate a right click on the button
function ButtonMetaFunctions:RightClick()
local frameWidget = self.widget
detailsFramework:CoreDispatch((frameWidget:GetName() or "Button") .. ":RightClick()", self.funcright, frameWidget, "RightButton", self.param1, self.param2)
end
--custom textures
function ButtonMetaFunctions:InstallCustomTexture()
--function deprecated, now just set a the standard template
self:SetTemplate(detailsFramework:GetTemplate("button", "OPTIONS_BUTTON_TEMPLATE"))
end
------------------------------------------------------------------------------------------------------------
--scripts
local OnEnter = function(button)
local object = button.MyObject
local kill = object:RunHooksForWidget("OnEnter", button, object)
if (kill) then
return
end
object.is_mouse_over = true
if (button.texture) then
if (button.texture.coords) then
button.texture:SetTexCoord(unpack(button.texture.coords.Highlight))
else
button.texture:SetTexCoord(0, 1, 0.24609375, 0.49609375)
end
end
if (object.onenter_backdrop_border_color) then
button:SetBackdropBorderColor(unpack(object.onenter_backdrop_border_color))
end
if (object.onenter_backdrop) then
button:SetBackdropColor(unpack(object.onenter_backdrop))
end
object:ShowTooltip()
end
local OnLeave = function(button)
local object = button.MyObject
local kill = object:RunHooksForWidget("OnLeave", button, object)
if (kill) then
return
end
object.is_mouse_over = false
if (button.texture and not object.is_mouse_down) then
if (button.texture.coords) then
button.texture:SetTexCoord(unpack(button.texture.coords.Normal))
else
button.texture:SetTexCoord(0, 1, 0, 0.24609375)
end
end
if (object.onleave_backdrop_border_color) then
button:SetBackdropBorderColor(unpack(object.onleave_backdrop_border_color))
end
if (object.onleave_backdrop) then
button:SetBackdropColor(unpack(object.onleave_backdrop))
end
object:HideTooltip()
end
local OnHide = function(button)
local object = button.MyObject
local kill = object:RunHooksForWidget("OnHide", button, object)
if (kill) then
return
end
end
local OnShow = function(button)
local object = button.MyObject
local kill = object:RunHooksForWidget("OnShow", button, object)
if (kill) then
return
end
end
local OnMouseDown = function(button, buttontype)
if (not button:IsEnabled()) then
return
end
local object = button.MyObject
local kill = object:RunHooksForWidget("OnMouseDown", button, object)
if (kill) then
return
end
object.is_mouse_down = true
if (button.texture) then
if (button.texture.coords) then
button.texture:SetTexCoord(unpack(button.texture.coords.Pushed))
else
button.texture:SetTexCoord(0, 1, 0.5078125, 0.75)
end
end
if (object.capsule_textalign) then
if (object.icon) then
object.icon:SetPoint("left", button, "left", 5 + (object.icon.leftPadding or 0), -1)
elseif (object.capsule_textalign == "left") then
button.text:SetPoint("left", button, "left", 3, -1)
elseif (object.capsule_textalign == "center") then
button.text:SetPoint("center", button, "center", 1, -1)
elseif (object.capsule_textalign == "right") then
button.text:SetPoint("right", button, "right", -1, -1)
end
else
if (object.icon) then
object.icon:SetPoint("left", button, "left", 5 + (object.icon.leftPadding or 0), -1)
else
button.text:SetPoint("center", button,"center", 1, -1)
end
end
button.mouse_down = GetTime()
local x, y = GetCursorPosition()
button.x = math.floor(x)
button.y = math.floor(y)
if (not object.container.isLocked and object.container:IsMovable()) then
if (not button.isLocked and button:IsMovable()) then
object.container.isMoving = true
object.container:StartMoving()
end
end
if (object.options.OnGrab) then
if (type(object.options.OnGrab) == "string" and object.options.OnGrab == "PassClick") then
if (buttontype == "LeftButton") then
detailsFramework:CoreDispatch((button:GetName() or "Button") .. ":OnMouseDown()", object.func, button, buttontype, object.param1, object.param2)
else
detailsFramework:CoreDispatch((button:GetName() or "Button") .. ":OnMouseDown()", object.funcright, button, buttontype, object.param1, object.param2)
end
end
end
end
local OnMouseUp = function(button, buttonType)
if (not button:IsEnabled()) then
return
end
local object = button.MyObject
local kill = object:RunHooksForWidget("OnMouseUp", button, object)
if (kill) then
return
end
object.is_mouse_down = false
if (button.texture) then
if (button.texture.coords) then
if (object.is_mouse_over) then
button.texture:SetTexCoord(unpack(button.texture.coords.Highlight))
else
--button.texture:SetTexCoord(unpack(coords.Normal))
end
else
if (object.is_mouse_over) then
button.texture:SetTexCoord(0, 1, 0.24609375, 0.49609375)
else
button.texture:SetTexCoord(0, 1, 0, 0.24609375)
end
end
end
if (object.capsule_textalign) then
if (object.icon) then
object.icon:SetPoint("left", button, "left", 4 + (object.icon.leftPadding or 0), 0)
elseif (object.capsule_textalign == "left") then
button.text:SetPoint("left", button, "left", 2, 0)
elseif (object.capsule_textalign == "center") then
button.text:SetPoint("center", button, "center", 0, 0)
elseif (object.capsule_textalign == "right") then
button.text:SetPoint("right", button, "right", -2, 0)
end
else
if (object.icon) then
object.icon:SetPoint("left", button, "left", 4 + (object.icon.leftPadding or 0), 0)
else
button.text:SetPoint("center", button,"center", 0, 0)
end
end
if (object.container.isMoving) then
object.container:StopMovingOrSizing()
object.container.isMoving = false
end
local x, y = GetCursorPosition()
x = math.floor(x)
y = math.floor(y)
button.mouse_down = button.mouse_down or 0 --avoid issues when the button was pressed while disabled and release when enabled
if ((x == button.x and y == button.y) or (button.mouse_down + 0.5 > GetTime() and button:IsMouseOver())) then
if (buttonType == "LeftButton") then
detailsFramework:CoreDispatch((button:GetName() or "Button") .. ":OnMouseUp()", object.func, button, buttonType, object.param1, object.param2)
else
detailsFramework:CoreDispatch((button:GetName() or "Button") .. ":OnMouseUp()", object.funcright, button, buttonType, object.param1, object.param2)
end
end
end
------------------------------------------------------------------------------------------------------------
---receives a table where the keys are settings and the values are the values to set
---this is the list of keys the table support:
---width, height, icon|table, textcolor, textsize, textfont, textalign, backdrop, backdropcolor, backdropbordercolor, onentercolor, onleavecolor, onenterbordercolor, onleavebordercolor
---@param template table|string
function ButtonMetaFunctions:SetTemplate(template)
template = detailsFramework:ParseTemplate(self.type, template)
if (not template) then
detailsFramework:Error("template not found")
return
end
if (template.width) then
PixelUtil.SetWidth(self.button, template.width)
end
if (template.height) then
PixelUtil.SetHeight(self.button, template.height)
end
if (template.backdrop) then
self:SetBackdrop(template.backdrop)
end
if (template.backdropcolor) then
local r, g, b, a = detailsFramework:ParseColors(template.backdropcolor)
self:SetBackdropColor(r, g, b, a)
self.onleave_backdrop = {r, g, b, a}
end
if (template.backdropbordercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.backdropbordercolor)
self:SetBackdropBorderColor(r, g, b, a)
self.onleave_backdrop_border_color = {r, g, b, a}
end
if (template.onentercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onentercolor)
self.onenter_backdrop = {r, g, b, a}
end
if (template.onleavecolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onleavecolor)
self.onleave_backdrop = {r, g, b, a}
end
if (template.onenterbordercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onenterbordercolor)
self.onenter_backdrop_border_color = {r, g, b, a}
end
if (template.onleavebordercolor) then
local r, g, b, a = detailsFramework:ParseColors(template.onleavebordercolor)
self.onleave_backdrop_border_color = {r, g, b, a}
end
if (template.icon) then
local iconInfo = template.icon
self:SetIcon(iconInfo.texture, iconInfo.width, iconInfo.height, iconInfo.layout, iconInfo.texcoord, iconInfo.color, iconInfo.textdistance, iconInfo.leftPadding)
end
if (template.textsize) then
self.textsize = template.textsize
end
if (template.textfont) then
self.textfont = template.textfont
end
if (template.textcolor) then
self.textcolor = template.textcolor
end
if (template.textalign) then
self.textalign = template.textalign
end
if (template.rounded_corner) then
self:SetBackdrop(nil)
detailsFramework:AddRoundedCornersToFrame(self.widget or self, template.rounded_corner)
--check if this is a color picker button
if (self.__iscolorpicker) then
self.color_texture:SetTexture([[Interface\CHARACTERFRAME\TempPortraitAlphaMaskSmall]], "CLAMP", "CLAMP", "TRILINEAR")
self.color_texture:SetDrawLayer("overlay", 7)
self.color_texture:SetPoint("topleft", self.widget, "topleft", 2, -2)
self.color_texture:SetPoint("bottomright", self.widget, "bottomright", -2, 2)
self.background_texture:SetDrawLayer("overlay", 6)
self.background_texture:SetPoint("topleft", self.color_texture, "topleft", 2, -2)
self.background_texture:SetPoint("bottomright", self.color_texture, "bottomright", -2, 2)
self.widget.texture_disabled:SetTexture([[Interface\CHARACTERFRAME\TempPortraitAlphaMaskSmall]], "CLAMP", "CLAMP", "TRILINEAR")
end
end
end
------------------------------------------------------------------------------------------------------------
--object constructor
local onDisableFunc = function(self)
self.texture_disabled:Show()
self.texture_disabled:SetVertexColor(0.1, 0.1, 0.1)
self.texture_disabled:SetAlpha(.834)
end
local onEnableFunc = function(self)
self.texture_disabled:Hide()
end
local createButtonWidgets = function(self)
self:SetSize(100, 20)
self.text = self:CreateFontString("$parent_Text", "ARTWORK", "GameFontNormal")
self.text:SetJustifyH("CENTER")
self.text:SetPoint("CENTER", self, "CENTER", 0, 0)
self:SetFontString(self.text)
detailsFramework:SetFontSize(self.text, 10)
self.texture_disabled = self:CreateTexture("$parent_TextureDisabled", "OVERLAY")
self.texture_disabled:SetAllPoints()
self.texture_disabled:Hide()
self.texture_disabled:SetTexture("Interface\\Tooltips\\UI-Tooltip-Background")
self:SetScript("OnDisable", onDisableFunc)
self:SetScript("OnEnable", onEnableFunc)
end
---@class df_button : button, df_scripthookmixin, df_widgets
---@field widget button
---@field tooltip string
---@field shown boolean
---@field width number
---@field height number
---@field text string
---@field clickfunction function
---@field texture string
---@field locked boolean
---@field fontcolor any
---@field fontface string
---@field fontsize number
---@field textcolor any
---@field textfont string
---@field textsize number
---@field icon texture created after calling SetIcon()
---@field SetTemplate fun(self: df_button, template: table|string) set the button visual by a template
---@field RightClick fun(self: df_button) right click the button executing its right click function
---@field Exec fun(self: df_button) execute the button function for the left button
---@field Disable fun(self: df_button) disable the button
---@field Enable fun(self: df_button) enable the button
---@field SetEnabled fun(self: df_button, enable: boolean) enable or disable the button
---@field IsEnabled fun(self: df_button) : boolean returns true if the button is enabled
---@field SetIcon fun(self: df_button,texture: string|number, width: number|nil, height: number|nil, layout: string|nil, texcoord: table|nil, overlay: table|nil, textDistance: number|nil, leftPadding: number|nil, textHeight: number|nil, shortMethod: any|nil)
---@field GetIconTexture fun(self: df_button) : string returns the texture path of the button icon
---@field SetTexture fun(self: df_button, normalTexture: any, highlightTexture: any, pressedTexture: any, disabledTexture: any) set the regular button textures
---@field SetFontFace fun(self: df_button, font: string) set the button font
---@field SetFontSize fun(self: df_button, size: number) set the button font size
---@field SetTextColor fun(self: df_button, color: any) set the button text color
---@field SetText fun(self: df_button, text: string) set the button text
---@field SetTextTruncated fun(self: df_button, text: string, maxWidth: number) set the button text and truncate it if it's too long
---@field SetParameters fun(self: df_button, param1: any, param2: any) set the parameters for the button callback function
---@field SetClickFunction fun(self: df_button, func: function, param1: any, param2: any, clickType: "left"|"right"|nil)
---@field SetIconFilterMode fun(self: df_button, filterMode: any) set the filter mode for the icon, execute after SetIcon()
---create a Details Framework button
---@param parent frame
---@param callback function
---@param width number
---@param height number
---@param text any
---@param param1 any|nil
---@param param2 any|nil
---@param texture any|nil
---@param member string|nil
---@param name string|nil
---@param shortMethod boolean|nil
---@param buttonTemplate table|nil
---@param textTemplate table|nil
---@return df_button
function detailsFramework:CreateButton(parent, callback, width, height, text, param1, param2, texture, member, name, shortMethod, buttonTemplate, textTemplate)
return detailsFramework:NewButton(parent, parent, name, member, width, height, callback, param1, param2, texture, text, shortMethod, buttonTemplate, textTemplate)
end
---@return df_button
function detailsFramework:NewButton(parent, container, name, member, width, height, func, param1, param2, texture, text, shortMethod, buttonTemplate, textTemplate)
if (not parent) then
error("Details! FrameWork: parent not found.", 2)
end
if (not name) then
local parentName = parent:GetName()
if (parentName) then
name = parentName .. "Button" .. detailsFramework.ButtonCounter
else
name = "DetailsFrameworkButtonNumber" .. detailsFramework.ButtonCounter
end
detailsFramework.ButtonCounter = detailsFramework.ButtonCounter + 1
end
local buttonObject = {type = "button", dframework = true}
if (member) then
parent[member] = buttonObject
end
if (parent.dframework) then
parent = parent.widget
end
--container is used to move the 'container' frame when attempt to move the button
buttonObject.container = container or parent
--default members
buttonObject.is_locked = true
buttonObject.options = {OnGrab = false}
buttonObject.button = CreateFrame("button", name, parent, "BackdropTemplate")
detailsFramework:Mixin(buttonObject.button, detailsFramework.WidgetFunctions)
createButtonWidgets(buttonObject.button)
PixelUtil.SetSize(buttonObject.button, width or 100, height or 20)
buttonObject.widget = buttonObject.button
buttonObject.button.MyObject = buttonObject
if (not APIButtonFunctions) then
APIButtonFunctions = true
local idx = getmetatable(buttonObject.button).__index
for funcName, funcAddress in pairs(idx) do
if (not ButtonMetaFunctions[funcName]) then