-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdropdown.lua
1730 lines (1419 loc) · 51.8 KB
/
dropdown.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
--[=[
On selecting an option it calls the 'onclick' function of the option with the parameters: dropdownObject, fixedValue, option.value
Example:
local onClickFunc = function(dropdownObject, fixedValue, value)
--fixedValue is the value set by dropdownObject:SetFixedParameter(any)
--the fixed value will be the same for any option selected in the dropdown
--do something
end
]=]
---@class df_dropdown : table, frame, df_widgets
---@field func function
---@field SetTemplate fun(self:df_dropdown, template:table|string)
---@field SetFixedParameter fun(self:df_dropdown, value:any) is sent as 2nd argument to the callback function, the value is the same no matter which option is selected
---@field BuildDropDownFontList fun(self:df_dropdown, onClick:function, icon:any, iconTexcoord:table?, iconSize:table?):table make a dropdown list with all fonts available, on select a font, call the function onClick
---@field SetFunction fun(self:df_dropdown, func:function)
---@field SetEmptyTextAndIcon fun(self:df_dropdown, text:string, icon:any)
---@field Select fun(self:df_dropdown, optionName:string|number, byOptionNumber:boolean?, bOnlyShown:boolean?, runCallback:boolean?):boolean
---@field SelectDelayed fun(self:df_dropdown, optionName:string|number, byOptionNumber:boolean?, bOnlyShown:boolean?, runCallback:boolean?) --call Select() after a random delay
---@field Open fun(self:df_dropdown)
---@field Close fun(self:df_dropdown)
---@field Refresh fun(self:df_dropdown)
---@field GetValue fun(self:df_dropdown):any
---@field GetFunction fun(self:df_dropdown):function
---@field GetMenuSize fun(self:df_dropdown):number, number
---@field SetMenuSize fun(self:df_dropdown, width:number, height:number)
---@field Disable fun(self:df_dropdown)
---@field Enable fun(self:df_dropdown)
---@class dropdownoption : table
---@field value any
---@field label string text shown in the dropdown option
---@field onclick fun(dropdownObject:table, fixedValue:any, value:any)? function to call when the option is selected
---@field icon string|number? texture
---@field color any any color format
---@field font string?
---@field texcoord number[]? left, right, top, bottom
---@field iconcolor any any color format
---@field iconsize number[]? width, height
---@field languageId string?
---@field rightbutton function? function to call on right click
---@field statusbar string|number? statusbar texture
---@field statusbarcolor any any color format
---@field rightTexture string|number? texture
---@field centerTexture string|number? texture
---@type detailsframework
local DF = _G ["DetailsFramework"]
if (not DF or not DetailsFrameworkCanLoad) then
return
end
local _
local loadedAPIDropDownFunctions = false
do
local metaPrototype = {
WidgetType = "dropdown",
dversion = DF.dversion,
}
--check if there's a metaPrototype already existing
if (_G[DF.GlobalWidgetControlNames["dropdown"]]) then
--get the already existing metaPrototype
local oldMetaPrototype = _G[DF.GlobalWidgetControlNames["dropdown"]]
--check if is older
if ( (not oldMetaPrototype.dversion) or (oldMetaPrototype.dversion < DF.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[DF.GlobalWidgetControlNames["dropdown"]] = metaPrototype
end
end
local DropDownMetaFunctions = _G[DF.GlobalWidgetControlNames["dropdown"]]
DF:Mixin(DropDownMetaFunctions, DF.SetPointMixin)
DF:Mixin(DropDownMetaFunctions, DF.FrameMixin)
DF:Mixin(DropDownMetaFunctions, DF.TooltipHandlerMixin)
DF:Mixin(DropDownMetaFunctions, DF.ScriptHookMixin)
DF:Mixin(DropDownMetaFunctions, DF.Language.LanguageMixin)
------------------------------------------------------------------------------------------------------------
--metatables
DropDownMetaFunctions.__call = function(object, value)
--unknown
end
------------------------------------------------------------------------------------------------------------
--members
--selected value
local gmemberValue = function(object)
return object:GetValue()
end
--tooltip
local gmemberTooltip = function(object)
return object:GetTooltip()
end
--shown
local gmemberShown = function(object)
return object:IsShown()
end
--frame width
local gmemberWidth = function(object)
return object.button:GetWidth()
end
--frame height
local gmemberHeight = function(object)
return object.button:GetHeight()
end
--current text
local gmemberText = function(object)
return object.label:GetText()
end
--menu creation function
local gmemberFunction = function(object)
return object:GetFunction()
end
--menu width
local gmemberMenuWidth = function(object)
return rawget(object, "realsizeW")
end
--menu height
local gmemberMenuHeight = function(object)
return rawget(object, "realsizeH")
end
DropDownMetaFunctions.GetMembers = DropDownMetaFunctions.GetMembers or {}
DropDownMetaFunctions.GetMembers["value"] = gmemberValue
DropDownMetaFunctions.GetMembers["text"] = gmemberText
DropDownMetaFunctions.GetMembers["shown"] = gmemberShown
DropDownMetaFunctions.GetMembers["width"] = gmemberWidth
DropDownMetaFunctions.GetMembers["menuwidth"] = gmemberMenuWidth
DropDownMetaFunctions.GetMembers["height"] = gmemberHeight
DropDownMetaFunctions.GetMembers["menuheight"] = gmemberMenuHeight
DropDownMetaFunctions.GetMembers["tooltip"] = gmemberTooltip
DropDownMetaFunctions.GetMembers["func"] = gmemberFunction
DropDownMetaFunctions.__index = function(object, key)
local func = DropDownMetaFunctions.GetMembers[key]
if (func) then
return func(object, key)
end
local alreadyHaveKey = rawget(object, key)
if (alreadyHaveKey) then
return alreadyHaveKey
end
return DropDownMetaFunctions[key]
end
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--tooltip
local smemberTooltip = function(object, value)
return object:SetTooltip(value)
end
--show
local smemberShow = function(object, value)
if (value) then
return object:Show()
else
return object:Hide()
end
end
--hide
local smemberHide = function(object, value)
if (not value) then
return object:Show()
else
return object:Hide()
end
end
--frame width
local smemberWidth = function(object, value)
return object.dropdown:SetWidth(value)
end
--frame height
local smemberHeight = function(object, value)
return object.dropdown:SetHeight(value)
end
--menu creation function
local smemberFunction = function(object, value)
return object:SetFunction(value)
end
--menu width
local smemberMenuWidth = function(object, value)
object:SetMenuSize(value, nil)
end
--menu height
local smemberMenuHeight = function(object, value)
object:SetMenuSize(nil, value)
end
DropDownMetaFunctions.SetMembers = DropDownMetaFunctions.SetMembers or {}
DropDownMetaFunctions.SetMembers["tooltip"] = smemberTooltip
DropDownMetaFunctions.SetMembers["show"] = smemberShow
DropDownMetaFunctions.SetMembers["hide"] = smemberHide
DropDownMetaFunctions.SetMembers["width"] = smemberWidth
DropDownMetaFunctions.SetMembers["menuwidth"] = smemberMenuWidth
DropDownMetaFunctions.SetMembers["height"] = smemberHeight
DropDownMetaFunctions.SetMembers["menuheight"] = smemberMenuHeight
DropDownMetaFunctions.SetMembers["func"] = smemberFunction
DropDownMetaFunctions.__newindex = function(object, key, value)
local func = DropDownMetaFunctions.SetMembers[key]
if (func) then
return func(object, value)
else
return rawset(object, key, value)
end
end
------------------------------------------------------------------------------------------------------------
--menu width and height
function DropDownMetaFunctions:SetMenuSize(width, height)
if (width) then
rawset(self, "realsizeW", width)
end
if (height) then
rawset(self, "realsizeH", height)
end
end
function DropDownMetaFunctions:GetMenuSize()
return rawget(self, "realsizeW"), rawget(self, "realsizeH")
end
--function
function DropDownMetaFunctions:SetFunction(func)
return rawset(self, "func", func)
end
function DropDownMetaFunctions:GetFunction()
return rawget(self, "func")
end
--value
function DropDownMetaFunctions:GetValue()
return rawget(self, "myvalue")
end
function DropDownMetaFunctions:SetValue(value)
return rawset(self, "myvalue", value)
end
--frame levels
function DropDownMetaFunctions:SetFrameLevel(level, frame)
if (not frame) then
return self.dropdown:SetFrameLevel(level)
else
local framelevel = frame:GetFrameLevel(frame) + level
return self.dropdown:SetFrameLevel(framelevel)
end
end
--enabled
function DropDownMetaFunctions:IsEnabled()
return self.dropdown:IsEnabled()
end
function DropDownMetaFunctions:Enable()
self:SetAlpha(1)
rawset(self, "lockdown", false)
if (self.OnEnable) then
self.OnEnable(self)
end
end
function DropDownMetaFunctions:Disable()
self:SetAlpha(.4)
rawset(self, "lockdown", true)
if (self.OnDisable) then
self.OnDisable(self)
end
end
--fixed value
function DropDownMetaFunctions:SetFixedParameter(value)
rawset(self, "FixedValue", value)
end
------------------------------------------------------------------------------------------------------------
--scripts
local lastOpened = false
local isOptionVisible = function(self, thisOption)
if (type(thisOption.shown) == "boolean") then
return thisOption.shown
elseif (type(thisOption.shown) == "function") then
local result = DF:Dispatch(thisOption.shown, self)
return result
end
return true
end
--return a table containing all frames of options in the menu
function DropDownMetaFunctions:GetMenuFrames() --not tested
if (self.MyObject) then
self = self.MyObject
end
return self.menus
end
function DropDownMetaFunctions:GetFrameForOption(optionsTable, value) --not tested
if (self.MyObject) then
self = self.MyObject
end
if (type(value) == "string") then
for i = 1, #optionsTable do
local thisOption = optionsTable[i]
if (thisOption.value == value or thisOption.label == value) then
return self.menus[i]
end
end
elseif (type(value) == "number") then
return self.menus[value]
end
end
function DropDownMetaFunctions:Refresh()
local state, optionsTable = xpcall(self.func, geterrorhandler(), self)
if (#optionsTable == 0) then
self:NoOption(true)
self.no_options = true
return false
elseif (self.no_options) then
self.no_options = false
self:NoOption(false)
self:NoOptionSelected()
return true
end
return true
end
function DropDownMetaFunctions:NoOptionSelected()
if (self.no_options) then
return
end
self.label:SetText(self.empty_text or "no option selected")
self.label:SetPoint("left", self.icon, "right", 2, 0)
self.label:SetTextColor(1, 1, 1, 0.4)
if (self.empty_icon) then
self.icon:SetTexture(self.empty_icon)
else
self.icon:SetTexture([[Interface\COMMON\UI-ModelControlPanel]])
self.icon:SetTexCoord(0.625, 0.78125, 0.328125, 0.390625)
end
self.icon:SetVertexColor(1, 1, 1, 0.4)
self.last_select = nil
end
function DropDownMetaFunctions:NoOption(state)
if (state) then
self:Disable()
self:SetAlpha(0.5)
self.no_options = true
self.label:SetText("no options")
self.label:SetPoint("left", self.icon, "right", 2, 0)
self.label:SetTextColor(1, 1, 1, 0.4)
self.icon:SetTexture([[Interface\CHARACTERFRAME\UI-Player-PlayTimeUnhealthy]])
self.icon:SetTexCoord(0, 1, 0, 1)
self.icon:SetVertexColor(1, 1, 1, 0.4)
else
self.no_options = false
self:Enable()
self:SetAlpha(1)
end
end
--@button: the frame button of the option
--button.table refers to the optionTable
local runCallbackFunctionForButton = function(button)
--exec function if any
if (button.table.onclick) then
--need: the the callback func, the object of the dropdown (capsule), the object (capsule) of the button to get FixedValue and the last need the value of the optionTable
xpcall(button.table.onclick, geterrorhandler(), button:GetParent():GetParent():GetParent().MyObject, button.object.FixedValue, button.table.value)
button:GetParent():GetParent():GetParent().MyObject:RunHooksForWidget("OnOptionSelected", button:GetParent():GetParent():GetParent().MyObject, button.object.FixedValue, button.table.value)
end
end
local canRunCallbackFunctionForOption = function(canRunCallback, optionTable, dropdownObject)
if (canRunCallback) then
local fixedValue = rawget(dropdownObject, "FixedValue")
if (optionTable.onclick) then
xpcall(optionTable.onclick, geterrorhandler(), dropdownObject, fixedValue, optionTable.value)
dropdownObject:RunHooksForWidget("OnOptionSelected", dropdownObject, fixedValue, optionTable.value)
end
end
end
function DropDownMetaFunctions:SelectDelayed(optionName, byOptionNumber, bOnlyShown, runCallback)
DF.Schedules.After(DF.Math.RandomFraction(0.016, 0.3), function()
self:Select(optionName, byOptionNumber, bOnlyShown, runCallback)
end)
end
---if bOnlyShown is true it'll first create a table with visible options that has .shown and then select in this table the index passed (if byOptionNumber)
---@param optionName string value or string shown in the name of the option
---@param byOptionNumber number the option name is considered a number and selects the index of the menu
---@param bOnlyShown boolean the selected option index when selecting by option number must be visible
---@param runCallback function run the callback (onclick) function after selecting the option
---@return boolean
function DropDownMetaFunctions:Select(optionName, byOptionNumber, bOnlyShown, runCallback)
if (type(optionName) == "boolean" and not optionName) then
self:NoOptionSelected()
return false
end
local runOkay, optionsTable = xpcall(self.func, geterrorhandler(), self)
if (type(optionsTable) ~= "table") then
error("optionsTable for Dropdown:Select() is not of type 'table'. Check if the dropdown menu function is returning a table.")
end
if (#optionsTable == 0) then
self:NoOption(true)
return true
else
self:NoOption(false)
end
if (byOptionNumber and type(optionName) == "number") then
local optionIndex = optionName
if (bOnlyShown) then
local onlyShownOptions = {}
for i = 1, #optionsTable do
local thisOption = optionsTable[i]
if (thisOption.shown) then
--only accept a function or a boolean into shown member
if (type(thisOption.shown) == "function") then
local isOptionShown = DF:Dispatch(thisOption.shown, self)
if (isOptionShown) then
onlyShownOptions[#onlyShownOptions+1] = thisOption
end
elseif (type(thisOption.shown) == "boolean" and thisOption.shown) then
onlyShownOptions[#onlyShownOptions+1] = thisOption
end
end
end
local optionTableSelected = onlyShownOptions[optionIndex]
if (not optionTableSelected) then
self:NoOptionSelected()
return false
end
self:Selected(optionTableSelected)
canRunCallbackFunctionForOption(runCallback, optionTableSelected, self)
return true
else
local optionTableSelected = optionsTable[optionIndex]
--is an invalid index?
if (not optionTableSelected) then
self:NoOptionSelected()
return false
end
self:Selected(optionTableSelected)
canRunCallbackFunctionForOption(runCallback, optionTableSelected, self)
return true
end
else
for i = 1, #optionsTable do
local thisOption = optionsTable[i]
if ((thisOption.label == optionName or thisOption.value == optionName) and isOptionVisible(self, thisOption)) then
self:Selected(thisOption)
canRunCallbackFunctionForOption(runCallback, thisOption, self)
return true
end
end
end
return false
end
function DropDownMetaFunctions:SetEmptyTextAndIcon(text, icon)
if (text) then
self.empty_text = text
end
if (icon) then
self.empty_icon = icon
end
self:Selected(self.last_select)
end
function DropDownMetaFunctions:Selected(thisOption)
if (not thisOption) then
--does not have any options?
if (not self:Refresh()) then
self.last_select = nil
return
end
--exists options but none selected
self:NoOptionSelected()
return
end
self.last_select = thisOption
self:NoOption(false)
local addonId = self.addonId
local languageId = thisOption.languageId
local phraseId = thisOption.phraseId
local overrideFont
if (addonId) then
local thisLanguageId = languageId or DF.Language.GetLanguageIdForAddonId(addonId)
if (thisLanguageId) then
if (thisLanguageId ~= self.label.languageId) then
local newFont = DF.Language.GetFontForLanguageID(thisLanguageId)
self.label.languageId = thisLanguageId
overrideFont = newFont
end
end
end
---@type fontstring
local thisLabel = self.label
local parentWidth = self:GetWidth()
if (addonId and phraseId) then
self.label:SetText(DF.Language.GetText(addonId, phraseId))
else
thisLabel:SetText(thisOption.label)
thisLabel:SetWordWrap(false)
thisLabel:SetIndentedWordWrap(false)
thisLabel:SetWidth(parentWidth + 30)
DF:TruncateText(thisLabel, parentWidth-30)
thisLabel:Show()
end
self.icon:SetTexture(thisOption.icon)
if (thisOption.icon) then
self.label:SetPoint("left", self.icon, "right", 2, 0)
if (thisOption.texcoord) then
self.icon:SetTexCoord(unpack(thisOption.texcoord))
else
self.icon:SetTexCoord(0, 1, 0, 1)
end
if (thisOption.iconcolor) then
local r, g, b, a = DF:ParseColors(thisOption.iconcolor)
self.icon:SetVertexColor(r, g, b, a)
else
self.icon:SetVertexColor(1, 1, 1, 1)
end
if (thisOption.iconsize) then
self.icon:SetSize(thisOption.iconsize[1], thisOption.iconsize[2])
else
self.icon:SetSize(self:GetHeight()-4, self:GetHeight()-4)
end
else
self.label:SetPoint("left", self.label:GetParent(), "left", 4, 0)
end
if (thisOption.centerTexture) then
self.dropdown.centerTexture:SetTexture(thisOption.centerTexture)
else
self.dropdown.centerTexture:SetTexture("")
end
if (thisOption.rightTexture) then
self.dropdown.rightTexture:SetTexture(thisOption.rightTexture)
else
self.dropdown.rightTexture:SetTexture("")
end
if (thisOption.statusbar) then
self.statusbar:SetTexture(thisOption.statusbar)
if (thisOption.statusbarcolor) then
self.statusbar:SetVertexColor(unpack(thisOption.statusbarcolor))
else
self.statusbar:SetVertexColor(1, 1, 1, 1)
end
else
self.statusbar:SetVertexColor(0, 0, 0, 0)
end
if (self.widget.__rcorners) then
self.statusbar:SetPoint("topleft", self.widget, "topleft", 2, -2)
self.statusbar:SetPoint("bottomright", self.widget, "bottomright", -2, 2)
end
if (thisOption.color) then
local r, g, b, a = DF:ParseColors(thisOption.color)
self.label:SetTextColor(r, g, b, a)
else
self.label:SetTextColor(1, 1, 1, 1)
end
if (overrideFont) then
self.label:SetFont(overrideFont, 10)
elseif (thisOption.font) then
self.label:SetFont(thisOption.font, 10)
else
self.label:SetFont("GameFontHighlightSmall", 10)
end
self:SetValue(thisOption.value)
end
--on click on any option in the dropdown
function DetailsFrameworkDropDownOptionClick(button)
--update name and icon on main frame
button.object:Selected(button.table)
--close menu frame
button.object:Close()
--run callbacks
runCallbackFunctionForButton(button)
--set the value of selected option in main object
button.object.myvalue = button.table.value
button.object.myvaluelabel = button.table.label
end
--on click on the dropdown show the menu frame with the options to select
function DropDownMetaFunctions:Open()
self.dropdown.dropdownframe:Show()
self.dropdown.dropdownborder:Show()
self.opened = true
if (lastOpened) then
lastOpened:Close()
end
lastOpened = self
end
--close the menu showing the options
function DropDownMetaFunctions:Close()
--when menu is being close, just hide the border and the script will call back this again
if (self.dropdown.dropdownborder:IsShown()) then
self.dropdown.dropdownborder:Hide()
return
end
self.dropdown.dropdownframe:Hide()
local selectedTexture = _G[self:GetName() .. "_ScrollFrame_ScrollChild_SelectedTexture"]
selectedTexture:Hide()
self.opened = false
lastOpened = false
end
--close by escape key
function DetailsFrameworkDropDownOptionsFrameOnHide(self)
self:GetParent().MyObject:Close()
end
--on enter an option in the menu dropdown
function DetailsFrameworkDropDownOptionOnEnter(self)
if (self.table.desc) then
GameCooltip2:Preset(2)
local addonId = self.table.addonId
if (addonId) then
local phraseId = self.table.desc
local text = DF.Language.GetText(addonId, phraseId)
GameCooltip2:AddLine(text or phraseId)
else
GameCooltip2:AddLine(self.table.desc)
end
if (self.table.descfont) then
GameCooltip2:SetOption("TextFont", self.table.descfont)
end
if (self.table.tooltipwidth) then
GameCooltip2:SetOption("FixedWidth", self.table.tooltipwidth)
end
GameCooltip2:SetHost(self, "topleft", "topright", 10, 0)
GameCooltip2:ShowCooltip(nil, "tooltip")
self.tooltip = true
end
if (self.table.audiocue) then
if (DF.CurrentSoundHandle) then
StopSound(DF.CurrentSoundHandle, 0.1)
end
local willPlay, soundHandle = PlaySoundFile(self.table.audiocue, "Master")
if (willPlay) then
DF.CurrentSoundHandle = soundHandle
end
end
self:GetParent().mouseover:SetPoint("left", self)
self:GetParent().mouseover:Show()
end
--on leave an option on the menu dropdown
function DetailsFrameworkDropDownOptionOnLeave(frame)
if (frame.table.desc) then
GameCooltip2:ShowMe(false)
end
frame:GetParent().mouseover:Hide()
end
--@button is the raw button frame, object is the button capsule
--click on the main dropdown frame (not the menu options popup)
function DetailsFrameworkDropDownOnMouseDown(button, buttontype)
local object = button.MyObject
--~click to open
if (not object.opened and not rawget(object, "lockdown")) then
local optionsTable = DF:Dispatch(object.func, object)
object.builtMenu = optionsTable
local frameWitdh = object.realsizeW
--has at least 1 option?
if (optionsTable and optionsTable[1]) then
local scrollFrame = _G[button:GetName() .. "_ScrollFrame"]
local scrollChild = _G[button:GetName() .. "_ScrollFrame_ScrollChild"]
local scrollBorder = _G[button:GetName() .. "_Border"]
local selectedTexture = _G[button:GetName() .. "_ScrollFrame_ScrollChild_SelectedTexture"]
local mouseOverTexture = _G[button:GetName() .. "_ScrollFrame_ScrollChild_MouseOverTexture"]
local i = 1
local showing = 0
local currentText = button.text:GetText() or ""
local currentIndex
if (object.OnMouseDownHook) then
local interrupt = object.OnMouseDownHook(button, buttontype, optionsTable, scrollFrame, scrollChild, selectedTexture)
if (interrupt) then
return
end
end
for tindex, thisOption in ipairs(optionsTable) do
local bIsOptionVisible = isOptionVisible(button, thisOption)
---@cast thisOption dropdownoption
if (bIsOptionVisible) then
local thisOptionFrame = object.menus[i]
showing = showing + 1
if (not thisOptionFrame) then
local name = button:GetName() .. "Row" .. i
local parent = scrollChild
thisOptionFrame = DF:CreateDropdownButton(parent, name)
local optionIndex = i - 1
thisOptionFrame:SetPoint("topleft", parent, "topleft", 1, (-optionIndex * 20))
thisOptionFrame:SetPoint("topright", parent, "topright", 0, (-optionIndex * 20))
thisOptionFrame.object = object
object.menus[i] = thisOptionFrame
end
thisOptionFrame:SetFrameStrata(thisOptionFrame:GetParent():GetFrameStrata())
thisOptionFrame:SetFrameLevel(thisOptionFrame:GetParent():GetFrameLevel() + 10)
if (thisOption.rightTexture) then
thisOptionFrame.rightTexture:SetTexture(thisOption.rightTexture)
else
thisOptionFrame.rightTexture:SetTexture("")
end
if (thisOption.centerTexture) then
thisOptionFrame.centerTexture:SetTexture(thisOption.centerTexture)
else
thisOptionFrame.centerTexture:SetTexture("")
end
thisOptionFrame.icon:SetTexture(thisOption.icon)
if (thisOption.icon) then
thisOptionFrame.label:SetPoint("left", thisOptionFrame.icon, "right", 5, 0)
if (thisOption.texcoord) then
thisOptionFrame.icon:SetTexCoord(unpack(thisOption.texcoord))
else
thisOptionFrame.icon:SetTexCoord(0, 1, 0, 1)
end
if (thisOption.iconcolor) then
local r, g, b, a = DF:ParseColors(thisOption.iconcolor)
thisOptionFrame.icon:SetVertexColor(r, g, b, a)
else
thisOptionFrame.icon:SetVertexColor(1, 1, 1, 1)
end
else
thisOptionFrame.label:SetPoint("left", thisOptionFrame.statusbar, "left", 2, 0)
end
if (thisOption.iconsize) then
thisOptionFrame.icon:SetSize(thisOption.iconsize[1], thisOption.iconsize[2])
else
thisOptionFrame.icon:SetSize(thisOptionFrame:GetHeight()-6, thisOptionFrame:GetHeight()-6)
end
if (thisOption.statusbar) then
thisOptionFrame.statusbar:SetTexture(thisOption.statusbar)
if (thisOption.statusbarcolor) then
thisOptionFrame.statusbar:SetVertexColor(unpack(thisOption.statusbarcolor))
else
thisOptionFrame.statusbar:SetVertexColor(1, 1, 1, 1)
end
else
thisOptionFrame.statusbar:SetVertexColor(0, 0, 0, 0)
end
--an extra button in the right side of the row
--run a given function passing the button in the first argument, the row on 2nd and the thisOption in the 3rd
if (thisOption.rightbutton) then
DF:Dispatch(thisOption.rightbutton, thisOptionFrame.rightButton, thisOptionFrame, thisOption)
else
thisOptionFrame.rightButton:Hide()
end
local overrideFont
local languageId = thisOption.languageId
if (languageId) then
if (languageId ~= thisOptionFrame.label.languageId) then
local newFont = DF.Language.GetFontForLanguageID(languageId)
thisOptionFrame.label.languageId = languageId
overrideFont = newFont
end
else
languageId = DF.Language.DetectLanguageId(thisOption.label)
if (languageId ~= thisOptionFrame.label.languageId) then
local newFont = DF.Language.GetFontForLanguageID(languageId)
thisOptionFrame.label.languageId = languageId
overrideFont = newFont
end
end
thisOptionFrame.label:SetText(thisOption.label)
if (overrideFont) then
thisOptionFrame.label:SetFont(overrideFont, 10.5)
elseif (thisOption.font) then
thisOptionFrame.label:SetFont(thisOption.font, 10.5)
else
thisOptionFrame.label:SetFont("GameFontHighlightSmall", 10.5)
end
if (currentText and currentText == thisOption.label) then
if (thisOption.icon) then
selectedTexture:SetPoint("left", thisOptionFrame.icon, "left", -3, 0)
else
selectedTexture:SetPoint("left", thisOptionFrame.statusbar, "left", 0, 0)
end
selectedTexture:Show()
selectedTexture:SetVertexColor(1, 1, 0, .5)
selectedTexture:SetTexCoord(0, 29/32, 5/32, 27/32)
currentIndex = tindex
currentText = nil
end
if (thisOption.color) then
local r, g, b, a = DF:ParseColors(thisOption.color)
thisOptionFrame.label:SetTextColor(r, g, b, a)
else
thisOptionFrame.label:SetTextColor(1, 1, 1, 1)
end
thisOptionFrame.table = thisOption
local labelwitdh = thisOptionFrame.label:GetStringWidth()
if (labelwitdh + 40 > frameWitdh) then
frameWitdh = labelwitdh + 40
end
thisOptionFrame:Show()
i = i + 1
end
end
if (currentText) then
selectedTexture:Hide()
else
selectedTexture:SetWidth(frameWitdh - 20)
end
for o = showing + 1, #object.menus do
object.menus[o]:Hide()
end
local size = object.realsizeH
if (showing * 20 > size) then
--show scrollbar and setup scroll
object:ShowScroll()
scrollFrame:EnableMouseWheel(true)
object.scroll:Altura(size-35) --height
object.scroll:SetMinMaxValues(0, (showing * 20) - size + 2)
--width
scrollBorder:SetWidth(frameWitdh+20)
scrollFrame:SetWidth(frameWitdh+20)
scrollChild:SetWidth(frameWitdh+20)
--height
scrollBorder:SetHeight(size+2)
scrollFrame:SetHeight(size+2)
scrollChild:SetHeight((showing * 20) + 20)
--mouse over texture
mouseOverTexture:SetWidth(frameWitdh - 7)
--selected
selectedTexture:SetWidth(frameWitdh - 9)
for index, row in ipairs(object.menus) do
row:SetPoint("topright", scrollChild, "topright", -22, ((-index-1) * 20) - 5)
end
else
--hide scrollbar and disable wheel
object:HideScroll()
scrollFrame:EnableMouseWheel(false)
--width
scrollBorder:SetWidth(frameWitdh)
scrollFrame:SetWidth(frameWitdh)
scrollChild:SetWidth(frameWitdh)
--height
scrollBorder:SetHeight((showing * 20) + 1)
scrollFrame:SetHeight((showing * 20) + 1)
--mouse over texture
mouseOverTexture:SetWidth(frameWitdh - 1)
--selected
selectedTexture:SetWidth(frameWitdh - 1)
for index, row in ipairs(object.menus) do
row:SetPoint("topright", scrollChild, "topright", -5, ((-index-1) * 20) -5)
end
end
if (object.myvaluelabel and currentIndex and scrollFrame.slider:IsShown()) then
object.scroll:SetValue(max((currentIndex * 20) - 80, 0))
else
object.scroll:SetValue(0)
end
object:Open()
else
--clear menu
end