-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkeybind.lua
1556 lines (1286 loc) · 58.7 KB
/
keybind.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
---@type detailsframework
local detailsFramework = _G["DetailsFramework"]
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
--[=[[
bugs:
-t verificar se esta mostrando o indicador de keybinds repetidas
-t fazer o sort de maneira que fique melhor
-t fazer um indicador dizendo que a keybind esta desabilitada por causa da load condition
-t fazer o debug do puf mostrar as keybinds (string)
- quando iniciar uma edição, fazer um indicador the diga que aquela linha esta esta sendo editada
- transferir o montagem do código seguro das keybinds no puf para o framework
tried to edit a spell binding:
2x Details/Libs/DF/keybind.lua:1080: attempt to index local 'actionId' (a number value)
[string "@Details/Libs/DF/keybind.lua"]:1160: in function <Details/Libs/DF/keybind.lua:1143>
[string "=[C]"]: in function `xpcall'
[string "@Details/Libs/DF/fw.lua"]:4864: in function `CoreDispatch'
[string "@Details/Libs/DF/button.lua"]:720: in function <Details/Libs/DF/button.lua:656>
--]=]
local _
local IsShiftKeyDown = _G["IsShiftKeyDown"]
local IsControlKeyDown = _G["IsControlKeyDown"]
local IsAltKeyDown = _G["IsAltKeyDown"]
local CreateFrame = _G["CreateFrame"]
local GetSpellInfo = _G["GetSpellInfo"] or function(spellID) if not spellID then return nil end local si = C_Spell.GetSpellInfo(spellID) if si then return si.name, nil, si.iconID, si.castTime, si.minRange, si.maxRange, si.spellID, si.originalIconID end end
local unpack = unpack ---@diagnostic disable-line
---@alias actionidentifier string a string in the format of "spell-spellId" or "macro-macroName" or "system-target", etc, used to pass information about the action more easily
---@class keybind_scroll_data : {key1:string, key2:any, key3:any, key4:string, key5:boolean, key6:number, key7:string}
---@class df_keybind : table
---@field name string
---@field action string|number
---@field keybind string
---@field macro string
---@field conditions table
---@field icon any
---@class df_editkeybindframe : frame
---@field bIsEditing boolean
---@field actionIdentifier actionidentifier
---@field conditionsFailLoadReasonText fontstring
---@field keybindTable df_keybind
---@field nameEditBox df_textentry
---@field iconPickerButton df_button
---@field conditionsButton df_button
---@field editMacroEditBox df_luaeditor
---@field cancelButton df_button
---@field saveButton df_button
---@field deleteMacroButton df_button
---@field Disable fun(self:df_editkeybindframe)
---@field Enable fun(self:df_editkeybindframe)
---@class df_selectkeybindbutton : button
---@field actionIdentifier actionidentifier
---@field keybindTable df_keybind
---@field keybindScrollData keybind_scroll_data
---@class df_keybindscrollline : frame, df_headerfunctions
---@field bIsSeparator boolean
---@field keybindScrollLine boolean
---@field backgroundTexture texture
---@field highlightTexture texture
---@field separatorTitleText fontstring
---@field spellIconTexture texture
---@field actionNameFontString fontstring
---@field setKeybindButton df_button
---@field clearKeybindButton df_button
---@field editKeybindSettingsButton df_button
---@field SetAsSeparator function
local keybindPrototype = {
name = "", --a name or alias to call this keybind
action = "", --which action this keybind will do, can be a spellId for spell casting, a macro text or targetting like "target", "focus", "togglemenu"
keybind = "",
macro = "",
conditions = detailsFramework:UpdateLoadConditionsTable({}),
icon = "",
}
---@type {action: string, keybind: string, icon: string, name: string}[]
local defaultMouseKeybinds = {
{action = "target", name = _G["TARGET"], keybind = "type1", icon = [[Interface\MINIMAP\TRACKING\Target]]}, --default: left mouse button
{action = "togglemenu", name = _G["SLASH_TEXTTOSPEECH_MENU"], keybind = "type2", icon = [[Interface\BUTTONS\UI-GuildButton-PublicNote-Up]]}, --default: right mouse button
{action = "focus", name = _G["FOCUS"], keybind = "type3", icon = [[Interface\MINIMAP\TRACKING\Focus]]} --default: middle mouse button
}
local defaultMouseKeybindsKV = {
["target"] = defaultMouseKeybinds[1],
["togglemenu"] = defaultMouseKeybinds[2],
["focus"] = defaultMouseKeybinds[3],
}
---@class df_keybindscroll : df_scrollbox
---@field UpdateScroll fun(self:df_keybindscroll)
---@class df_keybindframe : frame, df_optionsmixin
---@field options table
---@field data table
---@field keybindData table
---@field keybindScrollData keybind_scroll_data
---@field Header df_headerframe
---@field actionId number? the actionId is the spell Id or an actionId or a macro text
---@field button button? the button which got clicked to start editing a keybind
---@field bIsListening boolean if the frame is wayting the user to press a keybind (listening key inputs)
---@field bIsKeybindFrame boolean
---@field keybindScroll df_keybindscroll
---@field keybindListener frame
---@field editKeybindFrame df_editkeybindframe
---@field callback function
---@field ClearKeybind fun(self:button, buttonPresed:string, actionIdentifier:actionidentifier, keybindTable:any)
---@field CreateKeybindScroll fun(self:df_keybindframe)
---@field CreateKeybindListener fun(self:df_keybindframe)
---@field CreateEditPanel fun(self:df_keybindframe)
---@field CreateKeybindScrollLine fun(self:df_keybindframe, index:number)
---@field DeleteMacro fun(self:df_keybindframe)
---@field FindKeybindTable fun(self:df_keybindframe, keybindType:string, actionId:any, actionIdentifier:actionidentifier?) : df_keybind?, number?
---@field GetEditPanel fun(self:df_keybindframe) : df_editkeybindframe
---@field GetPressedModifiers fun() : string
---@field GetListeningActionId fun(self:df_keybindframe) : number
---@field GetListeningState fun(self:df_keybindframe) : boolean, any, button, keybind_scroll_data
---@field GetKeybindData fun(self:df_keybindframe) : df_keybind[]
---@field GetKeybindListener fun(self:df_keybindframe) : frame
---@field GetKeybindScroll fun(self:df_keybindframe) : df_keybindscroll
---@field GetKeybindCallback fun(self:df_keybindframe):function
---@field GetKeybindModifiers fun(keybind:string) : string
---@field GetKeybindTypeAndActionFromIdentifier fun(self:df_keybindframe, actionIdentifier:actionidentifier) : string, any
---@field IsListening fun(self:df_keybindframe) : boolean
---@field IsEditingKeybindSettings fun(self:df_keybindframe) : boolean, string, df_keybind
---@field IsKeybindActionMacro fun(self:df_keybindframe, actionId:any) : boolean
---@field CallKeybindChangeCallback fun(self:df_keybindframe, type:string, keybindTable:df_keybind?, keybindPressed:string?, removedIndex:number?, macroText:string?)
---@field OnKeybindNameChange fun(self:df_keybindframe, name:string)
---@field OnKeybindMacroChange fun(self:df_keybindframe, macroText:string)
---@field OnKeybindIconChange fun(self:df_keybindframe, iconTexture:string)
---@field OnUserClickedToChooseKeybind fun(self:df_keybindframe, button:button, actionIdentifier:actionidentifier, keybindTable:df_keybind|false)
---@field OnUserPressedKeybind fun(self:df_keybindframe, key:string)
---@field SaveKeybindToKeybindData fun(self:df_keybindframe, actionId:any, pressedKeybind:any, bJustCreated:boolean)
---@field SetClearButtonsEnabled fun(self:df_keybindframe, enabled:boolean)
---@field SetEditButtonsEnabled fun(self:df_keybindframe, enabled:boolean)
---@field SetListeningState fun(self:df_keybindframe, value:boolean, actionIdentifier:actionidentifier?, button:button?, keybindScrollData:keybind_scroll_data?)
---@field SetKeybindData fun(self:df_keybindframe, keybindData:table)
---@field SetKeybindCallback fun(self:df_keybindframe, callback:function)
---@field StartEditingKeybindSettings fun(self:frame, button:string, actionIdentifier:actionidentifier, keybindTable:df_keybind)
---@field StopEditingKeybindSettings fun(self:df_keybindframe)
---@field SwitchSpec fun(self:button, button:string, newSpecId:number)
detailsFramework:NewColor("BLIZZ_OPTIONS_COLOR", 1, 0.8196, 0, 1)
local DARK_BUTTON_TEMPLATE = detailsFramework:InstallTemplate("button", "DARK_BUTTON_TEMPLATE", {backdropcolor = {.1, .1, .1, .98}}, "OPTIONS_BUTTON_TEMPLATE")
---only called from OnUserPressedKeybind() when the a keybindTable is not found for the action
---@return df_keybind
local createNewKeybindTable = function(name, keybind, macro, actionId, iconTexture)
---@type df_keybind
local newMacroTable = detailsFramework.table.copy({}, keybindPrototype)
newMacroTable.name = name or "My New Macro" --if a name isn't passed, it's a macro
newMacroTable.keybind = keybind or ""
newMacroTable.macro = macro or ""
newMacroTable.action = actionId
newMacroTable.icon = iconTexture or ""
return newMacroTable
end
---return a number representing the sort order of a spell
---@param keybindData any
---@param spellName string
---@param bIsAvailable any
---@return number
local getSpellSortOrder = function(keybindData, spellName, bIsAvailable)
local sortScore = 0
if (not bIsAvailable) then
sortScore = sortScore + 5000
end
if (not keybindData) then
sortScore = sortScore + 300
end
sortScore = sortScore + string.byte(spellName)
return sortScore
end
local default_options = {
width = 580,
height = 500,
edit_width = 400,
edit_height = 0,
scroll_width = 580,
scroll_height = 480,
amount_lines = 18,
line_height = 26,
show_spells = true,
show_unitcontrols = true,
show_macros = true,
can_modify_keybind_data = true, --if false, won't change the data table passed on the constructor or the one returned by GetKeybindData
}
local headerTable = {
{text = "", width = 34}, --spell icon
{text = "", width = 200},
{text = "Keybind", width = 260},
{text = "Clear", width = 40},
{text = "Edit", width = 40},
}
local headerOptions = {
padding = 2,
backdrop_color = {0, 0, 0, 0},
}
local ignoredKeys = {
["LSHIFT"] = true,
["RSHIFT"] = true,
["LCTRL"] = true,
["RCTRL"] = true,
["LALT"] = true,
["RALT"] = true,
["UNKNOWN"] = true,
}
local mouseButtonToClickType = {
["LeftButton"] = "type1",
["RightButton"] = "type2",
["MiddleButton"] = "type3",
["Button4"] = "type4",
["Button5"] = "type5",
["Button6"] = "type6",
["Button7"] = "type7",
["Button8"] = "type8",
["Button9"] = "type9",
["Button10"] = "type10",
["Button11"] = "type11",
["Button12"] = "type12",
["Button13"] = "type13",
["Button14"] = "type14",
["Button15"] = "type15",
["Button16"] = "type16",
}
local roundedCornerPreset = {
roundness = 5,
color = {.075, .075, .075, 1},
border_color = {.05, .05, .05, 1},
horizontal_border_size_offset = 8,
}
--> helpers
local getMainFrame = function(UIObject)
if (UIObject.bIsKeybindFrame) then
return UIObject
end
local parentFrame = UIObject:GetParent()
for i = 1, 5 do
if (parentFrame.bIsKeybindFrame) then
return parentFrame
end
parentFrame = parentFrame:GetParent()
end
end
local setAsSeparator = function(line, bIsSeparator, titleText)
if (bIsSeparator) then
line.spellIconTexture:Hide()
line.actionNameFontString:Hide()
line.setKeybindButton:Hide()
line.clearKeybindButton:Hide()
line.editKeybindSettingsButton:Hide()
line.separatorTitleText:Show()
line.separatorTitleText:SetText(titleText)
line.bIsSeparator = true
line.backgroundTexture:Hide()
else
line.spellIconTexture:Show()
line.actionNameFontString:Show()
line.setKeybindButton:Show()
line.clearKeybindButton:Show()
line.editKeybindSettingsButton:Show()
line.separatorTitleText:Hide()
line.bIsSeparator = false
line.backgroundTexture:Show()
end
end
---@class df_keybindmixin
detailsFramework.KeybindMixin = {
bIsListening = false,
CurrentKeybindEditingSet = {},
---and the player change spec, the list of spells will change, hence need to update the keybind list if the panel is open
--if isn't open, the keybinds will be updated when the panel is opened
OnSpecChanged = function(self, button, newSpecId) --switch_spec
--quick hide and show as a feedback to the player that the spec was changed
--C_Timer.After (.04, function() EnemyGridOptionsPanelFrameKeybindScroill:Hide() end) --!need to defined the scroll frame
--C_Timer.After (.06, function() EnemyGridOptionsPanelFrameKeybindScroill:Show() end) --!need to defined the scroll frame
--EnemyGridOptionsPanelFrameKeybindScroill:UpdateScroll() --!need to defined the scroll frame
end,
---return true if the keybindFrame is waiting for the player to press a keybind
---@param self df_keybindframe
---@return boolean bIsListening
IsListening = function(self)
return self.bIsListening
end,
---return the actionId which the frame is currently listening for a keybind
---@param self df_keybindframe
GetListeningActionId = function(self)
return self.actionId
end,
---set the keybindFrame to listen for a keybind
---@param self df_keybindframe
---@param value boolean
---@param actionId number?
---@param button button?
SetListeningState = function(self, value, actionId, button, keybindScrollData)
self.bIsListening = value
self.actionId = actionId
self.button = button
self.keybindScrollData = keybindScrollData
self:SetClearButtonsEnabled(not value)
self:SetEditButtonsEnabled(not value)
end,
---get the listening state
---@param self df_keybindframe
---@return boolean
---@return number
---@return button
---@return keybind_scroll_data
GetListeningState = function(self)
return self.bIsListening, self.actionId, self.button, self.keybindScrollData
end,
---return the frame which wait for keybinds
---@param self df_keybindframe
---@return frame
GetKeybindListener = function(self)
return self.keybindListener
end,
---get the scroll frame
---@param self df_keybindframe
---@return df_keybindscroll
GetKeybindScroll = function(self)
return self.keybindScroll
end,
---keybind listener is the frame which reads the key pressed by the player when setting a keybind for an ability
CreateKeybindListener = function(self)
local keybindListener = CreateFrame ("frame", nil, self, "BackdropTemplate")
keybindListener:SetFrameStrata("tooltip")
keybindListener:SetSize(200, 60)
detailsFramework:ApplyStandardBackdrop(keybindListener)
self.keybindListener = keybindListener
keybindListener.text = detailsFramework:CreateLabel(keybindListener, "- Press a keyboard key to bind.\n- Click to bind a mouse button.\n- Press escape to cancel.", 11, "orange")
keybindListener.text:SetPoint("center", keybindListener, "center", 0, 0)
keybindListener:Hide()
end,
---callback from the clear button
---@param self button
---@param button any
---@param actionIdentifier string
---@param keybindTable any
ClearKeybind = function(self, button, actionIdentifier, keybindTable) --~clear
if (not keybindTable) then
return
end
---@type df_keybindframe
local keyBindFrame = getMainFrame(self)
local keybindType, actionId = keyBindFrame:GetKeybindTypeAndActionFromIdentifier(actionIdentifier)
local _, index = keyBindFrame:FindKeybindTable(keybindType, actionId, actionIdentifier)
if (index) then
if (keybindType == "macro") then
keybindTable.keybind = ""
else
if (keyBindFrame.options.can_modify_keybind_data) then
local keybindData = keyBindFrame:GetKeybindData()
table.remove(keybindData, index)
end
keyBindFrame:CallKeybindChangeCallback("removed", nil, nil, index)
end
end
local bIsEditingKeybind = keyBindFrame:IsEditingKeybindSettings()
if (bIsEditingKeybind) then
keyBindFrame:StopEditingKeybindSettings()
end
local keybindScroll = keyBindFrame:GetKeybindScroll()
keybindScroll:UpdateScroll()
end,
---@param self df_keybindframe
DeleteMacro = function(self)
local bIsEditingKeybind, actionIdentifier, keybindTable = self:IsEditingKeybindSettings()
if (not bIsEditingKeybind) then
return
end
if (not self:IsKeybindActionMacro(keybindTable.action)) then
return
end
local _, index = self:FindKeybindTable("macro", keybindTable.action, actionIdentifier)
if (index) then
if (self.options.can_modify_keybind_data) then
local keybindData = self:GetKeybindData()
table.remove(keybindData, index)
end
self:CallKeybindChangeCallback("removed", nil, nil, index)
end
self:StopEditingKeybindSettings()
local keybindScroll = self:GetKeybindScroll()
keybindScroll:UpdateScroll()
end,
---return a string with the modifiers of a keybind
---@param keybind string
---@return string
GetKeybindModifiers = function(keybind)
local modifier = ""
keybind = string.upper(keybind)
if (keybind:find("SHIFT-")) then
modifier = "SHIFT-"
end
if (keybind:find("CTRL-")) then
modifier = modifier .. "CTRL-"
end
if (keybind:find("ALT-")) then
modifier = modifier .. "ALT-"
end
return modifier
end,
---return a string with the modifiers of the key pressed by the player
---@return string
GetPressedModifiers = function()
return (IsShiftKeyDown() and "SHIFT-" or "") .. (IsControlKeyDown() and "CTRL-" or "") .. (IsAltKeyDown() and "ALT-" or "")
end,
---comment
---@param self df_keybindframe
---@param keybindTable df_keybind
---@param pressedKeybind any
---@param bKeybindJustCreated boolean
SaveKeybindToKeybindData = function(self, keybindTable, pressedKeybind, bKeybindJustCreated)
local keybindData = self:GetKeybindData() --from savedVariables
--if the keybindTable was created by the function which called this function, then need to add the keybind into the saved variables table
if (bKeybindJustCreated) then
table.insert(keybindData, keybindTable)
end
keybindTable.keybind = pressedKeybind
end,
---return the keybindData table if exists
---@param self df_keybindframe
---@param keybindType string
---@param actionId any
---@param actionIdentifier actionidentifier?
---@return df_keybind?, number?
FindKeybindTable = function(self, keybindType, actionId, actionIdentifier)
local keybindData = self:GetKeybindData()
if (keybindType == "spell" or keybindType == "system") then
for i = 1, #keybindData do
local keybindTable = keybindData[i]
if (keybindTable.action == actionId) then
return keybindTable, i
end
end
end
if (keybindType == "macro") then
for i = 1, #keybindData do
local keybindTable = keybindData[i]
if (keybindTable.action == actionIdentifier) then
return keybindTable, i
end
end
end
end,
---comment
---@param self df_keybindframe
---@param actionIdentifier any
---@return string
---@return string|number
GetKeybindTypeAndActionFromIdentifier = function(self, actionIdentifier)
---@type string, string
local keybindType, actionId = actionIdentifier:match("([^%-]+)%-(.+)")
if (keybindType == "spell") then
return keybindType, tonumber(actionId) and tonumber(actionId) or 0
end
return keybindType, actionId
end,
---when the user selected a keybind for an action, this function is called
---@param self df_keybindframe
---@param keyPressed any keyboard or mouse key to be used to perform the choosen action
OnUserPressedKeybind = function(self, keyPressed) --called from OnUserClickedToChooseKeybind and from OnKeyDown() script
--if the player presses a control key, ignore it
if (ignoredKeys[keyPressed]) then
return
end
local keybindListener = self:GetKeybindListener()
--exit the process if 'esc' is pressed
if (keyPressed == "ESCAPE") then
self:SetListeningState(false)
keybindListener:Hide()
self:SetScript("OnKeyDown", nil)
return
end
local modifiers = self:GetPressedModifiers()
local pressedKeybind = modifiers .. keyPressed
local bIsListening, actionIdentifier, button, keybindScrollData = self:GetListeningState()
local bKeybindJustCreated = false
--keybindType can be 'macro', 'spell' or 'system'
--actionId can be a spellId, a macro name or some other action like 'target' 'focus' 'togglemenu'
local keybindType, actionId = self:GetKeybindTypeAndActionFromIdentifier(actionIdentifier)
local keybindTable = self:FindKeybindTable(keybindType, actionId, actionIdentifier)
if (not keybindTable) then
local iconTexture = keybindScrollData[2]
--create a new keybindTable
if (keybindType == "spell") then
local spellId = actionId
local spellName = GetSpellInfo(spellId)
if (spellName) then
--actionId is the spellId
keybindTable = createNewKeybindTable(spellName, pressedKeybind, "", actionId, iconTexture)
end
elseif (keybindType == "system") then
local defaultKeybind = defaultMouseKeybindsKV[actionId]
--actionId is an action like 'target' 'focus' 'togglemenu'
keybindTable = createNewKeybindTable(defaultKeybind.name, pressedKeybind, "", actionId, iconTexture)
elseif (keybindType == "macro") then
local macroName = "New Macro"
--actionId is the word 'macro'
keybindTable = createNewKeybindTable(macroName, pressedKeybind, "/say hi", "macro", iconTexture)
end
bKeybindJustCreated = true
end
if (self.options.can_modify_keybind_data) then
--if the options for this frame allows it to change the keybind in the addon savedVariables, then do it
self:SaveKeybindToKeybindData(keybindTable, pressedKeybind, bKeybindJustCreated)
end
self:CallKeybindChangeCallback("modified", keybindTable, pressedKeybind)
self:SetListeningState(false)
self:SetScript("OnKeyDown", nil)
keybindListener:Hide()
--dumpt({"modifier", modifier, "newKeybind", newKeybind, "actionId", actionId})
local keybindScroll = self:GetKeybindScroll()
keybindScroll:UpdateScroll()
end,
---callback for when the user click in to define a keybind to an action
---@param self df_selectkeybindbutton
---@param button any
---@param actionIdentifier actionidentifier
---@param keybindTable df_keybindframe
OnUserClickedToChooseKeybind = function(self, button, actionIdentifier, keybindTable)
---@type df_keybindframe
local keyBindFrame = getMainFrame(self)
local bIsListening, _, frameButton = keyBindFrame:GetListeningState()
--if the listener is already listening for a keybind while the player clicks on another OnUserClickedToChooseKeybind button, then cancel the previous listener
if (bIsListening and (self == frameButton)) then
--if the frame is already listening, it could be a mouse click to set the keybind
local clickType = mouseButtonToClickType[button]
keyBindFrame:OnUserPressedKeybind(clickType)
return
end
bIsListening = true
local keybindScrollData = self.keybindScrollData
keyBindFrame:SetListeningState(bIsListening, actionIdentifier, self, keybindScrollData)
keyBindFrame:SetScript("OnKeyDown", keyBindFrame.OnUserPressedKeybind)
local keybindListener = keyBindFrame:GetKeybindListener()
keybindListener:ClearAllPoints()
keybindListener:SetPoint("bottom", self, "top", 0, 0)
keybindListener:Show()
local bIsEditingKeybind = keyBindFrame:IsEditingKeybindSettings()
if (bIsEditingKeybind) then
keyBindFrame:StopEditingKeybindSettings()
end
end,
SetClearButtonsEnabled = function(self, bIsEnabled)
local keybindScroll = self:GetKeybindScroll()
local lines = keybindScroll:GetLines()
for i = 1, #lines do
local line = lines[i]
if (bIsEnabled) then
--can only set enabled if the keybind isn't empty
if (line.setKeybindButton.text ~= "") then
line.clearKeybindButton:Enable()
end
else
line.clearKeybindButton:Disable()
end
end
end,
SetEditButtonsEnabled = function(self, bIsEnabled)
local keybindScroll = self:GetKeybindScroll()
local lines = keybindScroll:GetLines()
for i = 1, #lines do
local line = lines[i]
if (bIsEnabled) then
--can only set enabled if the keybind isn't empty
if (line.setKeybindButton.text ~= "") then
line.editKeybindSettingsButton:Enable()
end
else
line.editKeybindSettingsButton:Disable()
end
end
end,
RefreshKeybindScroll = function(self, scrollData, offset, totalLines) --~refresh
local keyBindFrame = getMainFrame(self)
---@type table<string, any[]>
local repeatedKeybinds = {}
---@cast scrollData keybind_scroll_data[]
--build a list of repeated keybinds
for index = 1, #scrollData do
local keybindScrollData = scrollData[index]
local actionName, iconTexture, actionId, keybindData, bIsAvailable = unpack(keybindScrollData)
---@cast keybindData df_keybind
if (bIsAvailable) then
if (type(keybindData) == "table" and keybindData.keybind and keybindData.keybind ~= "") then
repeatedKeybinds[keybindData.keybind] = repeatedKeybinds[keybindData.keybind] or {}
table.insert(repeatedKeybinds[keybindData.keybind], keybindData)
end
end
end
--local bIsListening = keyBindFrame:GetListeningState()
--local bIsEditingKeybind = keyBindFrame:IsEditingKeybindSettings()
local lastKeybindActionType = ""
--refresh the scroll bar
for i = 1, totalLines do
local index = i + offset
---@type keybind_scroll_data
local keybindScrollData = scrollData[index]
if (keybindScrollData) then
local line = self:GetLine(i)
---@type string, number, any, df_keybind|false, boolean, number, string
local actionName, iconTexture, actionId, keybindTable, bIsAvailable, sortNumber, actionIdentifier = unpack(keybindScrollData)
if (actionName == "@separator") then
line:SetAsSeparator(true, iconTexture)
else
line:SetAsSeparator(false)
--if the keybindData doesn't exists, means the user did not set a keybind for this action yet
--in this case keybindData is a false boolean
--keydindText is the text showing which keyboard or mouse button need to be pressed to activate the action
--if the keybind isn't set, use an empty string
local keydindText = keybindTable and keybindTable.keybind or ""
keydindText = keydindText:gsub("type1", _G["LEFT_BUTTON_STRING"])
keydindText = keydindText:gsub("type2", _G["RIGHT_BUTTON_STRING"])
keydindText = keydindText:gsub("type3", _G["MIDDLE_BUTTON_STRING"])
if (keydindText:match("type%d")) then
local buttonId = keydindText:match("type(%d)")
buttonId = tonumber(buttonId)
if (buttonId) then
local buttonName = _G["BUTTON_" .. buttonId .. "_STRING"]
if (buttonName and type(buttonName) == "string") then
keydindText = keydindText:gsub("type" .. buttonId, buttonName)
end
end
end
keydindText = keydindText:gsub("%-", " - ")
line.setKeybindButton.text = keydindText
--start editing keybind button
line.setKeybindButton:SetClickFunction(keyBindFrame.OnUserClickedToChooseKeybind, actionIdentifier, keybindTable, "left")
line.setKeybindButton:SetClickFunction(keyBindFrame.OnUserClickedToChooseKeybind, actionIdentifier, keybindTable, "right")
--clear keybind button
if (keydindText ~= "") then
line.clearKeybindButton:Enable()
line.clearKeybindButton:SetClickFunction(keyBindFrame.ClearKeybind, actionIdentifier, keybindTable)
line.editKeybindSettingsButton:Enable()
line.editKeybindSettingsButton:SetClickFunction(keyBindFrame.StartEditingKeybindSettings, actionIdentifier, keybindTable)
else
line.clearKeybindButton:Disable()
line.editKeybindSettingsButton:Disable()
end
local setKeybindButtonWidget = line.setKeybindButton.widget
setKeybindButtonWidget.keybindScrollData = keybindScrollData
line.spellIconTexture:SetTexture(iconTexture)
if (not bIsAvailable) then
line.spellIconTexture:SetDesaturated(true)
line.setKeybindButton.widget:SetColor(0, 0, 0, 0.1)
detailsFramework:SetFontColor(line.actionNameFontString, "gray")
else
line.spellIconTexture:SetDesaturated(false)
line.setKeybindButton.widget:SetColor(unpack(roundedCornerPreset.color))
detailsFramework:SetFontColor(line.actionNameFontString, "BLIZZ_OPTIONS_COLOR")
end
line.spellIconTexture:SetTexCoord(.1, .9, .1, .9)
line.actionNameFontString:SetText(actionName)
line.setKeybindButton.widget:SetBorderCornerColor(0, 0, 0, 0)
--check for repeated keybind
if (keybindTable and bIsAvailable) then
local keybind = keybindTable.keybind
local keybindTables = repeatedKeybinds[keybind]
if (keybindTables and #keybindTables > 1) then
line.setKeybindButton.widget:SetBorderCornerColor(1, .68, 0, 1)
end
end
end
end
end
end,
---run when the mouse enters a scroll line
---@param self df_keybindscrollline
OnEnterScrollLine = function(self)
local keyBindFrame = getMainFrame(self)
local editPanel = keyBindFrame:GetEditPanel()
editPanel.conditionsFailLoadReasonText:SetText("")
if (self.bIsSeparator) then
return
end
if (not self.keybindScrollLine) then
--when the mouse enters a child frame, the self is the child frame, not the scroll line
self = self:GetParent() ---@diagnostic disable-line getting the parent from df_keybindscrollline would result in type frame making invalid convertion
---@cast self df_keybindscrollline
end
self.highlightTexture:Show()
--if the keybind is a macro, preview the macro text in the edit panel's lua edit box
local bIsEditingKeybind = keyBindFrame:IsEditingKeybindSettings()
if (not bIsEditingKeybind) then
local keybindScrollData = self.setKeybindButton.widget["keybindScrollData"]
if (keybindScrollData) then
local actionName, iconTexture, actionId, keybindTable, bIsAvailable, sortNumber, actionIdentifier = unpack(keybindScrollData)
---@cast keybindTable df_keybind
if (actionName ~= "@separator" and keybindTable) then
if (keybindTable.macro and keybindTable.macro ~= "") then
---@type df_editkeybindframe
editPanel.editMacroEditBox:SetText(keybindTable.macro)
end
local loadCondition = keybindTable.conditions
local bCanLoad, reason = detailsFramework:PassLoadFilters(loadCondition)
if (not bCanLoad) then
editPanel.conditionsFailLoadReasonText:SetText("This keybind can't be loaded because it's conditions are not met:\n- " .. (reason or ""))
else
editPanel.conditionsFailLoadReasonText:SetText("")
end
end
end
end
end,
---run when the mouse leaves a scroll line
---@param self df_keybindscrollline
OnLeaveScrollLine = function(self)
if (self.bIsSeparator) then
return
end
if (not self.keybindScrollLine) then
--when the mouse enters a child frame, the self is the child frame, not the scroll line
self = self:GetParent() ---@diagnostic disable-line getting the parent from df_keybindscrollline would result in type frame making invalid convertion
---@cast self df_keybindscrollline
end
self.highlightTexture:Hide()
--if the keybind is a macro, a preview might be showing in the edit panel's lua edit box, hide it
local keyBindFrame = getMainFrame(self)
local bIsEditingKeybind = keyBindFrame:IsEditingKeybindSettings()
if (not bIsEditingKeybind) then
local editPanel = keyBindFrame:GetEditPanel()
editPanel.editMacroEditBox:SetText("")
end
end,
---@param keybindScroll frame
---@param index number
CreateKeybindScrollLine = function(keybindScroll, index) --~create
local keyBindFrame = getMainFrame(keybindScroll)
---@type df_keybindscrollline
local line = CreateFrame("frame", "$parentLine" .. index, keybindScroll)
line:SetSize(keyBindFrame.options.width - 10, keyBindFrame.options.line_height)
line:SetPoint("topleft", keyBindFrame, "topleft", 1, -22 - (index-1) * keyBindFrame.options.line_height)
line:EnableMouse(true)
line.keybindScrollLine = true
--detailsFramework:ApplyStandardBackdrop(line, index % 2 == 0)
--line:SetBackdropBorderColor(0, 0, 0, 0)
line.backgroundTexture = line:CreateTexture("$parentBackgroundTexture", "background")
line.backgroundTexture:SetAllPoints()
if (index % 2 == 0) then
line.backgroundTexture:SetColorTexture(0, 0, 0, 0.1)
else
line.backgroundTexture:SetColorTexture(0, 0, 0, 0)
end
line.highlightTexture = line:CreateTexture(nil, "border")
line.highlightTexture:SetAllPoints()
line.highlightTexture:SetColorTexture(1, 1, 1, .1)
line.highlightTexture:Hide()
line:SetScript("OnEnter", keyBindFrame.OnEnterScrollLine)
line:SetScript("OnLeave", keyBindFrame.OnLeaveScrollLine)
detailsFramework:Mixin(line, detailsFramework.HeaderFunctions)
local options_text_template = detailsFramework:GetTemplate("font", "OPTIONS_FONT_TEMPLATE")
local options_dropdown_template = detailsFramework:GetTemplate("dropdown", "OPTIONS_DROPDOWN_TEMPLATE")
local options_switch_template = detailsFramework:GetTemplate("switch", "OPTIONS_CHECKBOX_TEMPLATE")
local options_slider_template = detailsFramework:GetTemplate("slider", "OPTIONS_SLIDER_TEMPLATE")
local options_button_template = detailsFramework:GetTemplate("button", "OPTIONS_BUTTON_TEMPLATE")
line.separatorTitleText = line:CreateFontString("$parentSeparatorTitleText", "overlay", "GameFontNormal")
line.separatorTitleText:SetPoint("center", line, "center", 0, 0)
line.spellIconTexture = line:CreateTexture("$parentIcon", "overlay")
line.spellIconTexture:SetSize(keyBindFrame.options.line_height - 2, keyBindFrame.options.line_height - 2)
line.actionNameFontString = line:CreateFontString("$parentName", "overlay", "GameFontNormal")
detailsFramework:SetFontColor(line.actionNameFontString, "BLIZZ_OPTIONS_COLOR")
detailsFramework:SetFontSize(line.actionNameFontString, 12)
---@type df_button
line.setKeybindButton = detailsFramework:CreateButton(line, function()end, headerTable[3].width, keyBindFrame.options.line_height-6, "", nil, nil, nil, "SetNewKeybindButton", "$parentSetNewKeybindButton", 0, nil, options_text_template)
line.setKeybindButton.textcolor = "white"
line.setKeybindButton.textsize = 10
line.setKeybindButton:SetHook("OnEnter", keyBindFrame.OnEnterScrollLine)
line.setKeybindButton:SetHook("OnLeave", keyBindFrame.OnLeaveScrollLine)
detailsFramework:AddRoundedCornersToFrame(line.setKeybindButton, roundedCornerPreset)
---@type df_button
line.clearKeybindButton = detailsFramework:CreateButton(line, keyBindFrame.ClearKeybind, 16, keyBindFrame.options.line_height-2, "", nil, nil, nil, "DeleteKeybindButton", "$parentDeleteKeybindButton", 2, nil, options_text_template)
line.clearKeybindButton:SetBackdropBorderColor(0, 0, 0, 0)
line.clearKeybindButton:SetIcon([[Interface\COMMON\CommonIcons]], nil, nil, nil, {0.1264, 0.2514, 0.5048, 0.7548}, nil, nil, 4)
---@type df_button
line.editKeybindSettingsButton = detailsFramework:CreateButton(line, keyBindFrame.StartEditingKeybindSettings, 16, keyBindFrame.options.line_height-2, "", nil, nil, nil, "EditKeybindButton", "$parentEditKeybindButton", 2, nil, options_text_template)
line.editKeybindSettingsButton:SetBackdropBorderColor(0, 0, 0, 0)
line.editKeybindSettingsButton:SetIcon([[Interface\BUTTONS\UI-GuildButton-PublicNote-Disabled]])
line:AddFrameToHeaderAlignment(line.spellIconTexture)
line:AddFrameToHeaderAlignment(line.actionNameFontString)
line:AddFrameToHeaderAlignment(line.setKeybindButton)
line:AddFrameToHeaderAlignment(line.clearKeybindButton)
line:AddFrameToHeaderAlignment(line.editKeybindSettingsButton)
line:AlignWithHeader(keyBindFrame.Header, "left")
line.SetAsSeparator = setAsSeparator
return line
end,
---comment
---@param self df_keybindframe
CreateKeybindScroll = function(self) --~scroll
local scroll_width = self.options.scroll_width
local scroll_height = self.options.scroll_height
local scroll_lines = self.options.amount_lines
local scroll_line_height = self.options.line_height
--~header
---@type df_headerframe
self.Header = DetailsFramework:CreateHeader(self, headerTable, headerOptions)
self.Header:SetPoint("topleft", self, "topleft", 0, 0)
--this is a hack to hide the background black texture column of the header
--making the area more clean for the create macro button
self.Header.columnHeadersCreated[2].Center:SetAlpha(0)
local onClickCreateMacroButton = function() --~macro
local newMacroName = "New @Macro (" .. math.random(10000, 99999) .. ")"
local actionIdentifier = "macro-" .. newMacroName
local keybindTable = createNewKeybindTable(newMacroName, "", "/say Hi", actionIdentifier, 136377)
local pressedKeybind = ""
if (self.options.can_modify_keybind_data) then
--if the options for this frame allows it to change the keybind in the addon savedVariables, then do it
local bKeybindJustCreated = true
self:SaveKeybindToKeybindData(keybindTable, pressedKeybind, bKeybindJustCreated)
end
self:CallKeybindChangeCallback("modified", keybindTable, pressedKeybind)
local keybindScroll = self:GetKeybindScroll()
keybindScroll:UpdateScroll()
--start editing this keybindTable
self:StartEditingKeybindSettings("LeftButton", actionIdentifier, keybindTable)
--get the keybind editor frame
---@type df_editkeybindframe
local keybindEditor = self:GetEditPanel()
local macroEditBox = keybindEditor.editMacroEditBox
macroEditBox:SetText(keybindTable.macro)
macroEditBox:SetFocus()
end
local createMacroButton = detailsFramework:CreateButton(self.Header, onClickCreateMacroButton, 200, 32, "Create Macro Keybind", nil, nil, nil, "CreateMacroButton", "$parentCreateMacroButton", 0, DARK_BUTTON_TEMPLATE, detailsFramework:GetTemplate("font", "OPTIONS_FONT_TEMPLATE"))
createMacroButton:SetPoint("left", self.Header, "left", 2, 0)
createMacroButton:SetFrameLevel(self.Header:GetFrameLevel()+10)
createMacroButton:SetIcon(136377)
createMacroButton:SetTemplate("OPTIONS_CIRCLEBUTTON_TEMPLATE")
createMacroButton:SetScale(0.9)
createMacroButton:SetFontSize(13)
local keybindScroll = detailsFramework:CreateScrollBox(self, "$parentScrollBox", detailsFramework.KeybindMixin.RefreshKeybindScroll, {}, scroll_width, scroll_height, scroll_lines, scroll_line_height)
---@cast keybindScroll df_keybindscroll
detailsFramework:ReskinSlider(keybindScroll)
keybindScroll:SetPoint("topleft", self.Header, "bottomleft", 0, -5)
self.keybindScroll = keybindScroll
keybindScroll:SetBackdropColor(0, 0, 0, 0)
keybindScroll:SetBackdropBorderColor(0, 0, 0, 0)
keybindScroll.__background:SetAlpha(0)
for i = 1, scroll_lines do
keybindScroll:CreateLine(self.CreateKeybindScrollLine)
end
--scroll data constructor