Back to Home

ESO Lua File v100018

libraries/zo_keybindstrip/zo_keybindstrip.lua

[◄ back to folders ]
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
ZO_KeybindStrip = ZO_Object:New()
KEYBIND_STRIP_ALIGN_LEFT = 1
KEYBIND_STRIP_ALIGN_CENTER = 2
KEYBIND_STRIP_ALIGN_RIGHT = 3
KEYBIND_STRIP_DISABLED_ALERT = "alert"
KEYBIND_STRIP_DISABLED_DIALOG = "dialog"
function ZO_KeybindStrip:New(...)
    local keybindStrip = ZO_Object.New(self)
    keybindStrip:Initialize(...)
    return keybindStrip
end
local DOWN = false
local UP = true
function ZO_KeybindStrip:Initialize(control, keybindButtonTemplate, styleInfo)
    self.control = control
    self.centerParent = control:GetNamedChild("CenterParent") or control
    self.keybinds = {}
    self.keybindGroups = {}
    self.cooldownKeybinds = {}
    self.keybindStateStack = {}
    self:SetStyle(styleInfo)
    local function OnButtonClicked(button)
        local keybindButtonDescriptor = button.keybindButtonDescriptor
        if self:FilterSceneHiding(keybindButtonDescriptor) then
            if keybindButtonDescriptor.callback then
                keybindButtonDescriptor.callback(DOWN)
            end
            if keybindButtonDescriptor.handlesKeyUp or keybindButtonDescriptor.keybindButtonGroupDescriptor and keybindButtonDescriptor.keybindButtonGroupDescriptor.handlesKeyUp then
                if keybindButtonDescriptor.callback then
                    keybindButtonDescriptor.callback(UP)
                end
            end
        end
    end
    local function CreateButton(objectPool)
        -- NOTE: Whatever template is used here is expected to have the interface defined by
        -- ZO_KeybindButtonTemplate_OnInitialized. This should set up a few control references (for the name and key labels)
        -- as well as defining some state management functions...see that object for more details.
        local button = ZO_ObjectPool_CreateControl(keybindButtonTemplate, objectPool, control)
        button:SetCallback(OnButtonClicked)
        return button
    end
    local function Reset(control)
        control:SetHidden(true)
        control:ClearAnchors()
        if control.customKeybindControl then
            control.customKeybindControl:SetParent(nil)
            control.customKeybindControl:SetHidden(true)
            control.customKeybindControl = nil
        end
    end
    
    self.keybindButtonPool = ZO_ObjectPool:New(CreateButton, Reset)
    self.centerButtons = nil
    self.leftButtons = nil
    self.rightButtons = nil
    local function UpdateBindingLabels()
        for keybind, buttonOrEtherealDescriptor in pairs(self.keybinds) do
            if type(buttonOrEtherealDescriptor) == "userdata" then
                self:SetUpButton(buttonOrEtherealDescriptor)
            end
        end
        self:UpdateAnchors()
    end
    control:RegisterForEvent(EVENT_KEYBINDING_SET, UpdateBindingLabels)
    control:RegisterForEvent(EVENT_KEYBINDING_CLEARED, UpdateBindingLabels)
    control:RegisterForEvent(EVENT_KEYBINDINGS_LOADED, UpdateBindingLabels)
    control:RegisterForEvent(EVENT_GAMEPAD_PREFERRED_MODE_CHANGED, UpdateBindingLabels)
    local function OnUpdate(control, time)
        self:UpdateCooldowns(time)
    end
    control:SetHandler("OnUpdate", OnUpdate)
end
-- Keybind State Stack Management
function ZO_KeybindStrip:PushKeybindGroupState()
    local background
    if not ZO_KeybindStripMungeBackground:IsHidden() then
        background = ZO_KeybindStripMungeBackground
    else
        background = ZO_KeybindStripGamepadBackground
    end
    
    local state = 
    {
        keybindGroups = {},
        individualButtons = {},
        drawTier = self.control:GetDrawTier(),
        drawLayer = self.control:GetDrawLayer(),
        drawLevel = self.control:GetDrawLevel(),
        backgroundTier = background:GetDrawTier(),
        backgroundLayer = background:GetDrawLayer(),
        backgroundLevel = background:GetDrawLevel(),
        allowDefaultExit = self.allowDefaultExit
    }
    --copy all the keybind groups into the state and remove them from the strip
    for key, value in pairs(self.keybindGroups) do
        state.keybindGroups[key] = value
    end
    local topStateIndex = self:GetTopKeybindStateIndex()
    self:RemoveAllKeyButtonGroups(topStateIndex)
    --now copy/overwrite any left over buttons to the state
    for key, value in pairs(self.keybinds) do
        state.individualButtons[key] = value.keybindButtonDescriptor
        self:RemoveKeybindButton(value.keybindButtonDescriptor or value, topStateIndex)
    end
    table.insert(self.keybindStateStack, state)
    self:UpdateAnchors()
    return self:GetTopKeybindStateIndex()
end
function ZO_KeybindStrip:PopKeybindGroupState()
    local numStates = #self.keybindStateStack
    if(numStates > 0) then
        local topStateIndex = self:GetTopKeybindStateIndex()
        self:RemoveAllKeyButtonGroups(topStateIndex)
        for key, value in pairs(self.keybinds) do
            self:RemoveKeybindButton(value.keybindButtonDescriptor or value, topStateIndex)
        end
        local state = table.remove(self.keybindStateStack, numStates)
        topStateIndex = self:GetTopKeybindStateIndex()
        self:SetDrawOrder(state.drawTier, state.drawLayer, state.drawLevel)
        self:SetBackgroundDrawOrder(state.backgroundTier, state.backgroundLayer, state.backgroundLevel)
        self.allowDefaultExit = state.allowDefaultExit
        for i, descriptorGroup in pairs(state.keybindGroups) do
            self:AddKeybindButtonGroup(descriptorGroup, topStateIndex)
        end
        for i, keybind in pairs(state.individualButtons) do
            if(self:HasKeybindButton(keybind, topStateIndex)) then
                self:UpdateKeybindButton(keybind, topStateIndex)
            else
                self:AddKeybindButton(keybind, topStateIndex)
            end
        end
        
        self:UpdateAnchors()
    end
end
function ZO_KeybindStrip:RemoveAllKeyButtonGroups(stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return ZO_KeybindStrip.RemoveAllKeyButtonGroupsStack(state)
    end
    if next(self.keybindGroups) ~= nil then
        local prevKeybindGroups = ZO_ShallowTableCopy(self.keybindGroups)
        for _, groupDescriptor in pairs(prevKeybindGroups) do
            self:RemoveKeybindButtonGroup(groupDescriptor, stateIndex)
        end
    end
end
function ZO_KeybindStrip:ClearKeybindGroupStateStack()
    --remove all keybind buttons and loose buttons as well as any states
    local topStateIndex = self:GetTopKeybindStateIndex()
    self:RemoveAllKeyButtonGroups(topStateIndex)
    for key, value in pairs(self.keybinds) do
        self:RemoveKeybindButton(value.keybindButtonDescriptor or value, topStateIndex)
    end
    ZO_ClearTable(self.keybindStateStack)
end
function ZO_KeybindStrip:SetDrawOrder(tier, layer, level)
    if tier then
         self.control:SetDrawTier(tier)
    end
    if layer then
         self.control:SetDrawLayer(layer)
    end
    if level then
         self.control:SetDrawLevel(level)
    end
end
function ZO_KeybindStrip:SetBackgroundDrawOrder(tier, layer, level)
    if ZO_KeybindStripMungeBackground then
        if tier then 
            ZO_KeybindStripMungeBackground:SetDrawTier(tier)
        end
        if layer then 
            ZO_KeybindStripMungeBackground:SetDrawLayer(layer)
        end
        if level then 
            ZO_KeybindStripMungeBackground:SetDrawLevel(level)
        end
    end
    if ZO_KeybindStripGamepadBackground then
        if tier then 
            ZO_KeybindStripGamepadBackground:SetDrawTier(tier)
        end
        if layer then 
            ZO_KeybindStripGamepadBackground:SetDrawLayer(layer)
        end
        if level then 
            ZO_KeybindStripGamepadBackground:SetDrawLevel(level)
        end
    end
end
function ZO_KeybindStrip:GetKeybindState(stateIndex)
    stateIndex = stateIndex or 1
    assert(stateIndex > 0)
    if stateIndex <= #self.keybindStateStack then
        return self.keybindStateStack[stateIndex]
    end
    return nil
end
function ZO_KeybindStrip:GetTopKeybindStateIndex()
    return #self.keybindStateStack + 1
end
--- PRIVATE FUNCTIONS ---
local function GetDescriptorFromButton(buttonOrEtherealDescriptor)
    if type(buttonOrEtherealDescriptor) == "userdata" then
        return buttonOrEtherealDescriptor.keybindButtonDescriptor
    end
    return buttonOrEtherealDescriptor
end
local function GetValueFromRawOrFunction(keybindButtonDescriptor, key)
    local value
    if keybindButtonDescriptor[key] == nil then
        value = keybindButtonDescriptor.keybindButtonGroupDescriptor and keybindButtonDescriptor.keybindButtonGroupDescriptor[key]
    else
        value = keybindButtonDescriptor[key]
    end
    if type(value) == "function" then
        return value(keybindButtonDescriptor, keybindButtonDescriptor.keybindButtonGroupDescriptor)
    end
    return value
end
local function AddKeybindButtonGroupStack(keybindButtonGroupDescriptor, state)
    if not state.keybindGroups[keybindButtonGroupDescriptor] then
        state.keybindGroups[keybindButtonGroupDescriptor] = keybindButtonGroupDescriptor
        return true
    end
    return false
end
local function AddKeybindButtonStack(keybindButtonDescriptor, state)
    -- Asserting here usually means that a key is already bound (typically because someone forgot to remove a keybinding).
    assert(state.individualButtons[keybindButtonDescriptor.keybind] == nil)
    state.individualButtons[keybindButtonDescriptor.keybind] = keybindButtonDescriptor
end
function ZO_KeybindStrip.RemoveKeybindButtonGroupStack(keybindButtonGroupDescriptor, state)
    if state.keybindGroups[keybindButtonGroupDescriptor] then
        state.keybindGroups[keybindButtonGroupDescriptor] = nil
        return true
    end
    return false
end
local function RemoveKeybindButtonStack(keybindButtonDescriptor, state)
    state.individualButtons[keybindButtonDescriptor.keybind] = nil
end
local function UpdateKeybindButtonGroupStack(keybindButtonDescriptor, state)
    if state.keybindGroups[keybindButtonDescriptor] then
        return true
    end
    return false
end
local function UpdateKeybindButtonStack(keybindButtonDescriptor, state)
    -- do nothing
end
local function UpdateCurrentKeybindButtonGroupsStack(stateIndex)
    -- do nothing
end
function ZO_KeybindStrip.RemoveAllKeyButtonGroupsStack(state)
    if next(state.keybindGroups) ~= nil then
        local prevKeybindGroups = ZO_ShallowTableCopy(state.keybindGroups)
        for _, groupDescriptor in pairs(prevKeybindGroups) do
            ZO_KeybindStrip.RemoveKeybindButtonGroupStack(groupDescriptor, state)
        end
    end
end
--- END OF PRIVATE FUNCTIONS ---
--[[
An example keybind button descriptor
local descriptor = {
alignment = KEYBIND_STRIP_ALIGN_CENTER, -- defaults to KEYBIND_STRIP_ALIGN_RIGHT
order = 100 -- or a function that returns the relative ordering within the alignment, where a lower number is anchored first (right to left for left alignment, left to right for center and right), default is 0 - buttons with the same order are ordered by insertion
name = "Exit", -- or function that returns a name
keybind = "UI_SHORTCUT_PRIMARY",
customKeybindControl = nil, -- control or a function that returns a control to display instead of the normal keybind label
callback = function(up) DoSomething() end, -- First and only parameter is whether its a key up or down, ups will only be sent if handlesKeyUp flag is set
visible = function(descriptor) return IsUsePossible(descriptor.something) end, -- An optional predicate, if present returning true indicates that this descriptor is visible, otherwise it is not
icon = "IconPath/Icon.dds", -- or a function that returns an icon path, an optional icon to display to the right of the name
handlesKeyUp = true, -- indicates that the callback would like to be informed of key ups in addition to downs, by default only downs are sent
disabledDuringSceneHiding = false, -- indicates that the callback is disabled during Scene Hiding, by default it is false
ethereal = false, -- if true, indicates that the button has no physical presence and will only be considered when a keybind is used, which means that name, alignment, etc properties are ignored. This cannot be a function that returns a value.
}
]]
--
function ZO_KeybindStrip:AddKeybindButton(keybindButtonDescriptor, stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return AddKeybindButtonStack(keybindButtonDescriptor, state)
    end
    -- Asserting here usually means that a key is already bound (typically because someone forgot to remove a keybinding).
    local currentSceneName = ""
    if SCENE_MANAGER then
        local currentScene = SCENE_MANAGER:GetCurrentScene()
        if currentScene then
            currentSceneName = currentScene:GetName()
        end
    end
    local existingButtonOrEtheralDescriptor = self.keybinds[keybindButtonDescriptor.keybind]
    if existingButtonOrEtheralDescriptor then
        local existingDescriptor = GetDescriptorFromButton(existingButtonOrEtheralDescriptor)
        local existingSceneName = ""
        local existingDescriptorName = ""
        if existingDescriptor then
            --We tried to re-add the same exact button, just return
            if existingDescriptor == keybindButtonDescriptor then
                return
            end
            existingSceneName = existingDescriptor.addedForSceneName
            local descriptorName = GetValueFromRawOrFunction(existingDescriptor, "name")
            if descriptorName then
                existingDescriptorName = descriptorName
            end
        end
        local newDescriptorName = GetValueFromRawOrFunction(keybindButtonDescriptor, "name") or ""
        local context = string.format("Duplicate Keybind: %s. Before: %s (%s). After: %s (%s).", keybindButtonDescriptor.keybind, existingSceneName, existingDescriptorName, currentSceneName, newDescriptorName)
        assert(false, context)
    end
    keybindButtonDescriptor.addedForSceneName = currentSceneName
    if keybindButtonDescriptor.ethereal then
        self.keybinds[keybindButtonDescriptor.keybind] = keybindButtonDescriptor
    else
        local button, key = self.keybindButtonPool:AcquireObject()
        button.keybindButtonDescriptor = keybindButtonDescriptor
        button.key = key
        self.insertionId = (self.insertionId or 0) + 1
        button.insertionOrder = self.insertionId
        self.keybinds[keybindButtonDescriptor.keybind] = button
        if not self.batchUpdating then
            -- clear this out in case it was previously in a group
            keybindButtonDescriptor.keybindButtonGroupDescriptor = nil
        end
        self:AddButtonToAnchors(button)
        if not self.batchUpdating then
            self:SetUpButton(button)
            self:UpdateAnchors()
        end
        return button
    end
end
local function CompareKeybindButtonDescriptor(first, second)
    return  (first == nil) or (second == nil) or
            ((first.keybind == second.keybind) and
            (first.name == second.name) and
            (first.callback == second.callback))
end
function ZO_KeybindStrip:RemoveKeybindButton(keybindButtonDescriptor, stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return RemoveKeybindButtonStack(keybindButtonDescriptor, state)
    end
    local buttonOrEtherealDescriptor = self.keybinds[keybindButtonDescriptor.keybind]     
    if buttonOrEtherealDescriptor and CompareKeybindButtonDescriptor(buttonOrEtherealDescriptor.keybindButtonDescriptor, keybindButtonDescriptor) then
        self.keybinds[keybindButtonDescriptor.keybind] = nil
        keybindButtonDescriptor.addedForSceneName = nil
        for i, cooldownKeybindDescriptor in ipairs(self.cooldownKeybinds) do
            if cooldownKeybindDescriptor == keybindButtonDescriptor and not cooldownKeybindDescriptor.shouldCooldownPersist then
                table.remove(self.cooldownKeybinds, i)
                cooldownKeybindDescriptor.cooldown = nil
                cooldownKeybindDescriptor.cooldownStart = nil
                break
            end
        end
        if type(buttonOrEtherealDescriptor) == "userdata" then
            self.keybindButtonPool:ReleaseObject(buttonOrEtherealDescriptor.key)
            self:RemoveButtonFromAnchors(buttonOrEtherealDescriptor)
            if not self.batchUpdating then
                self:UpdateAnchors()
            end
        end
    end
end
function ZO_KeybindStrip:UpdateKeybindButton(keybindButtonDescriptor, stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return UpdateKeybindButtonStack(keybindButtonDescriptor, state)
    end
    local buttonOrEtherealDescriptor = self.keybinds[keybindButtonDescriptor.keybind]
    if buttonOrEtherealDescriptor then
        if type(buttonOrEtherealDescriptor) == "userdata" then
            if buttonOrEtherealDescriptor.keybindButtonDescriptor == keybindButtonDescriptor then
                local UPDATE_ONLY = true
                self:SetUpButton(buttonOrEtherealDescriptor, UPDATE_ONLY)
                if not self.batchUpdating then
                    self:UpdateAnchors()
                end
            else
                self:RemoveKeybindButton(buttonOrEtherealDescriptor.keybindButtonDescriptor)
                self:AddKeybindButton(keybindButtonDescriptor)
            end
        else
            if keybindButtonDescriptor ~= buttonOrEtherealDescriptor then
                self:RemoveKeybindButton(buttonOrEtherealDescriptor)
                self:AddKeybindButton(keybindButtonDescriptor)
            end
        end
    end
end
function ZO_KeybindStrip:HasKeybindButton(keybindButtonDescriptor)
    return self.keybinds[keybindButtonDescriptor.keybind] ~= nil
end
--[[
An example keybind button group descriptor
local keybindButtonGroup = {
-- Anything added here will be inherited by individual descriptors, for example adding an alignment of left here will cause individial descriptors to all be aligned left
-- unless they specify their own alignment
alignment = KEYBIND_STRIP_ALIGN_LEFT,
{
name = "Exit", -- or function that returns a name
keybind = "UI_SHORTCUT_PRIMARY",
callback = function(up) DoSomething() end, -- First and only parameter is whether its a key up or down, ups will only be sent if handlesKeyUp flag is set
handlesKeyUp = true, -- indicates that the callback would like to be informed of key ups in addition to downs, by default only downs are sent
},
{
alignment = KEYBIND_STRIP_ALIGN_CENTER, -- override the alignment set un the group
name = "Use", -- or function that returns a name
keybind = "UI_SHORTCUT_SECONDARY",
callback = function(up) UseSomething() end, -- First and only parameter is whether its a key up or down, ups will only be sent if handlesKeyUp flag is set
visible = function(descriptor) return IsUsePossible(descriptor.something) end,
sound = SOUNDS.POSITIVE_CLICK, -- An audio hook that plays when the button is pressed
},
}
]]
--
function ZO_KeybindStrip:AddKeybindButtonGroup(keybindButtonGroupDescriptor, stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return AddKeybindButtonGroupStack(keybindButtonGroupDescriptor, state)
    end
    if not self.keybindGroups[keybindButtonGroupDescriptor] then
        self.batchUpdating = true
        self.keybindGroups[keybindButtonGroupDescriptor] = keybindButtonGroupDescriptor
        for i, keybindButtonDescriptor in ipairs(keybindButtonGroupDescriptor) do
            keybindButtonDescriptor.keybindButtonGroupDescriptor = keybindButtonGroupDescriptor
            local button = self:AddKeybindButton(keybindButtonDescriptor, stateIndex)
            if button then
                self:SetUpButton(button)
            end
        end
        self:UpdateAnchors()
        self.batchUpdating = false
        return true
    end
    return false
end
function ZO_KeybindStrip:RemoveKeybindButtonGroup(keybindButtonGroupDescriptor, stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return ZO_KeybindStrip.RemoveKeybindButtonGroupStack(keybindButtonGroupDescriptor, state)
    end
    if self.keybindGroups[keybindButtonGroupDescriptor] then
        self.batchUpdating = true
        for i, keybindButtonDescriptor in ipairs(keybindButtonGroupDescriptor) do
            self:RemoveKeybindButton(keybindButtonDescriptor, stateIndex)
            keybindButtonDescriptor.keybindButtonGroupDescriptor = nil
        end
        self.keybindGroups[keybindButtonGroupDescriptor] = nil
        self:UpdateAnchors()
        self.batchUpdating = false
        return true
    end
    return false
end
function ZO_KeybindStrip:UpdateCurrentKeybindButtonGroups(stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return UpdateCurrentKeybindButtonGroupsStack(state)
    end
    for keybindButtonDescriptor, group in pairs(self.keybindGroups) do
        self:UpdateKeybindButtonGroup(keybindButtonDescriptor, stateIndex)
    end
end
function ZO_KeybindStrip:UpdateKeybindButtonGroup(keybindButtonGroupDescriptor, stateIndex)
    local state = self:GetKeybindState(stateIndex)
    if state then
        return UpdateKeybindButtonGroupStack(keybindButtonGroupDescriptor, state)
    end
    if self.keybindGroups[keybindButtonGroupDescriptor] then
        self.batchUpdating = true
        for i, keybindButtonDescriptor in ipairs(keybindButtonGroupDescriptor) do
            self:UpdateKeybindButton(keybindButtonDescriptor, stateIndex)
        end
        self:UpdateAnchors()
        self.batchUpdating = false
        return true
    end
    return false
end
function ZO_KeybindStrip:HasKeybindButtonGroup(keybindButtonGroupDescriptor)
    return self.keybindGroups[keybindButtonGroupDescriptor] ~= nil
end
function ZO_KeybindStrip:FilterSceneHiding(keybindButtonDescriptor)
    if keybindButtonDescriptor.disabledDuringSceneHiding then
        local currentScene = SCENE_MANAGER:GetCurrentScene()
        if currentScene and currentScene:GetState() == SCENE_HIDING then
            return false
        end
    end
    return true
end
function ZO_KeybindStrip:TryHandlingKeybindDown(keybind)
    if not self.control:IsHidden() then
        local buttonOrEtherealDescriptor = self.keybinds[keybind]
        if buttonOrEtherealDescriptor and (not buttonOrEtherealDescriptor.IsControlHidden or not buttonOrEtherealDescriptor:IsControlHidden()) then
            local keybindButtonDescriptor = GetDescriptorFromButton(buttonOrEtherealDescriptor)
            local enabled, disabledAlertText, disabledAlertType = GetValueFromRawOrFunction(keybindButtonDescriptor, "enabled")
            local cooldown = GetValueFromRawOrFunction(keybindButtonDescriptor, "cooldown")
            if cooldown then
                enabled = false
            end
            if enabled ~= false then
                if self:FilterSceneHiding(keybindButtonDescriptor) then
                    if keybindButtonDescriptor.callback then
                        local sound = keybindButtonDescriptor.sound
                        ClearMenu()
                        keybindButtonDescriptor.callback(DOWN)
                        keybindButtonDescriptor.handledDown = true
                        if sound then
                            PlaySound(sound)
                        end
                    end
                end
                return true
            elseif disabledAlertText then
                if disabledAlertType == KEYBIND_STRIP_DISABLED_DIALOG then
                    ZO_Dialogs_ShowPlatformDialog("KEYBIND_STRIP_DISABLED_DIALOG", nil, {mainTextParams = {disabledAlertText}})
                else
                    ZO_Alert(UI_ALERT_CATEGORY_ALERT, nil, disabledAlertText)
                    PlaySound(SOUNDS.GENERAL_ALERT_ERROR)
                end
            end
        end
    end
    return false
end
function ZO_KeybindStrip:TryHandlingKeybindUp(keybind)
    if not self.control:IsHidden() then
        local buttonOrEtherealDescriptor = self.keybinds[keybind]
        if buttonOrEtherealDescriptor and (not buttonOrEtherealDescriptor.IsControlHidden or not buttonOrEtherealDescriptor:IsControlHidden()) then
            local keybindButtonDescriptor = GetDescriptorFromButton(buttonOrEtherealDescriptor)
            local enabled = GetValueFromRawOrFunction(buttonOrEtherealDescriptor, "enabled")
            local cooldown = GetValueFromRawOrFunction(buttonOrEtherealDescriptor, "cooldown")
            if cooldown then
                enabled = false
            end
            local handledPreviousDown = keybindButtonDescriptor.handledDown
            keybindButtonDescriptor.handledDown = nil
            if enabled ~= false or handledPreviousDown then
                if keybindButtonDescriptor.handlesKeyUp or keybindButtonDescriptor.keybindButtonGroupDescriptor and keybindButtonDescriptor.keybindButtonGroupDescriptor.handlesKeyUp then
                    if self:FilterSceneHiding(keybindButtonDescriptor) then
                        if keybindButtonDescriptor.callback then
                            ClearMenu()
                            keybindButtonDescriptor.callback(UP)
                        end
                    end
                    return true
                end
            end
        end
    end
    return false
end
function ZO_KeybindStrip:TriggerCooldown(keybindButtonDescriptor, duration, stateIndex, shouldCooldownPersist)
    keybindButtonDescriptor.cooldown = duration / 1000
    keybindButtonDescriptor.cooldownStart = GetFrameTimeMilliseconds() / 1000
    keybindButtonDescriptor.shouldCooldownPersist = shouldCooldownPersist
    if self.keybinds[keybindButtonDescriptor.keybind] and self.keybinds[keybindButtonDescriptor.keybind].keybindButtonDescriptor == keybindButtonDescriptor then
        self:UpdateKeybindButton(keybindButtonDescriptor, stateIndex)
    end
    --Check for duplicates
    for i = 1, #self.cooldownKeybinds do
        local descriptor = self.cooldownKeybinds[i]
        if descriptor == keybindButtonDescriptor then
            return
        end
    end
    table.insert(self.cooldownKeybinds, keybindButtonDescriptor)
end
function ZO_KeybindStrip:UpdateCooldowns(time)
    for i = #self.cooldownKeybinds, 1, -1 do
        local descriptor = self.cooldownKeybinds[i]
        local cooldownStart = descriptor.cooldownStart
        local difference = time - cooldownStart
        local newCooldown = descriptor.cooldown - difference
        --Only update for changes in the seconds count
        local oldCeiling = zo_ceil(descriptor.cooldown)
        local newCeiling = zo_ceil(newCooldown)
        if oldCeiling ~= newCeiling then
            descriptor.cooldownStart = time
            descriptor.cooldown = newCooldown
            if descriptor.cooldown <= 0 then
                descriptor.cooldown = nil
                descriptor.cooldownStart = nil
                descriptor.shouldCooldownPersist = nil
                table.remove(self.cooldownKeybinds, i)
            end
            local activeDescriptor = self.keybinds[descriptor.keybind]
            if activeDescriptor then
                if not activeDescriptor.ethereal then
                    activeDescriptor = activeDescriptor.keybindButtonDescriptor
                end
                if descriptor == activeDescriptor then
                    self:UpdateKeybindButton(descriptor, self:GetTopKeybindStateIndex())
                end
            end
        end
    end
end
function ZO_KeybindStrip:SetHidden(hidden)
    self.control:SetHidden(hidden)
end
--[[
A style table provides font and other information to describe the look of the keybind buttons
An example descriptor:
{
nameFont = "ZoFontKeybindStripDescription",
nameFontColor = ZO_NORMAL_TEXT,
keyFont = "ZoFontKeybindStripKey",
resizeToFitPadding = 40,
leftAnchorOffset = 0,
centerAnchorOffset = 0,
rightAnchorOffset = 0,
}
]]
function ZO_KeybindStrip:SetStyle(styleInfo)
    if self.styleInfo ~= styleInfo then
        self.styleInfo = styleInfo
        for keybind, buttonOrEtherealDescriptor in pairs(self.keybinds) do
            if type(buttonOrEtherealDescriptor) == "userdata" then
                self:SetUpButton(buttonOrEtherealDescriptor)
            end
        end
        self:UpdateAnchors()
        self:SetDrawOrder(styleInfo.drawTier, styleInfo.drawLayer, styleInfo.drawLevel)
        self:SetBackgroundStyle(styleInfo)
        if self.onStyleChanged then
            self.onStyleChanged(self, styleInfo)
        end
    end
end
function ZO_KeybindStrip:SetBackgroundStyle(styleInfo)
    -- even though we are only refreshing the background style, we are still changing the style overall
    -- store the potential style change so that future calls to SetStyle can refresh everything appropriately
    if self.styleInfo ~= styleInfo then
        self.styleInfo = styleInfo
    end
    self:SetBackgroundDrawOrder(styleInfo.backgroundDrawTier, styleInfo.backgroundDrawLayer, styleInfo.backgroundDrawLevel)
end
function ZO_KeybindStrip:GetStyle()
    return self.styleInfo
end 
function ZO_KeybindStrip:SetOnStyleChangedCallback(onStyleChanged)
end
-- implementation functions below
function ZO_KeybindStrip:RemoveButtonFromAnchors(button)
    local keybindButtonDescriptor = button.keybindButtonDescriptor
    local anchorTable = self:GetAnchorTableFromAlignment(keybindButtonDescriptor.alignment or keybindButtonDescriptor.keybindButtonGroupDescriptor and keybindButtonDescriptor.keybindButtonGroupDescriptor.alignment)
    for i=1, #anchorTable do
        if anchorTable[i] == button then
            table.remove(anchorTable, i)
            break
        end
    end
end
function ZO_KeybindStrip:AddButtonToAnchors(button)
    local keybindButtonDescriptor = button.keybindButtonDescriptor
    local anchorTable = self:GetAnchorTableFromAlignment(keybindButtonDescriptor.alignment or keybindButtonDescriptor.keybindButtonGroupDescriptor and keybindButtonDescriptor.keybindButtonGroupDescriptor.alignment)
    anchorTable[#anchorTable + 1] = button
end
function ZO_KeybindStrip:SetupButtonStyle(button, styleInfo)
    if styleInfo then
        if styleInfo.nameFont then
            button:SetNameFont(styleInfo.nameFont)
        end
        if styleInfo.nameFontColor then
            button:SetNormalTextColor(styleInfo.nameFontColor)
        end
        if styleInfo.keyFont then
            button:SetKeyFont(styleInfo.keyFont)
        end
        if styleInfo.resizeToFitPadding then
            button:SetResizeToFitPadding(styleInfo.resizeToFitPadding)
        end
        if styleInfo.modifyTextType then
            button.nameLabel:SetModifyTextType(styleInfo.modifyTextType)
        end
        return styleInfo.alwaysPreferGamepadMode
    end
    return nil
end
function ZO_KeybindStrip:GetAnchorTableFromAlignment(alignment)
    if alignment == KEYBIND_STRIP_ALIGN_LEFT then
        self.leftButtons = self.leftButtons or {}
        return self.leftButtons
    end
    if alignment == KEYBIND_STRIP_ALIGN_CENTER then
        self.centerButtons = self.centerButtons or {}
        return self.centerButtons
    end
    self.rightButtons = self.rightButtons or {}
    return self.rightButtons
end
do
    local function IsVisible(keybindButtonDescriptor)
        local visibilityFunction = keybindButtonDescriptor.visible or keybindButtonDescriptor.keybindButtonGroupDescriptor and keybindButtonDescriptor.keybindButtonGroupDescriptor.visible
        return not visibilityFunction or visibilityFunction(keybindButtonDescriptor, keybindButtonDescriptor.keybindButtonGroupDescriptor)
    end
    function ZO_KeybindStrip:SetUpButton(button, updateOnly)
        local keybindButtonDescriptor = button.keybindButtonDescriptor
        local isVisible = IsVisible(button.keybindButtonDescriptor)
        local wasVisible = not button:IsHidden()
        local customKeybindControl = GetValueFromRawOrFunction(keybindButtonDescriptor, "customKeybindControl")
        if isVisible then
            if customKeybindControl then
                customKeybindControl:SetHidden(false)
                customKeybindControl:SetParent(button)
                customKeybindControl:ClearAnchors()
                customKeybindControl:SetAnchor(RIGHT, button.keyLabel, RIGHT)
                button.keyLabel:SetHidden(true)
                button.customKeybindControl = customKeybindControl
                button:SetResizeToFitPadding(0) -- This should be handled on the custom control
            else
                button.keyLabel:SetHidden(false)
            end
            local keybind = keybindButtonDescriptor.keybind or keybindButtonDescriptor.keybindButtonGroupDescriptor and keybindButtonGroupDescriptor.keybindButtonGroupDescriptor.keybind
            
            local buttonKeybindChanging = button:GetKeybind() ~= keybind
            local suppressUpdate = (updateOnly and isVisible == wasVisible)
            -- updateOnly is used only when we are trying to update keybinds from UpdateKeybindButton so we know that this setup is coming from an update
            local alwaysPreferGamepadMode = self:SetupButtonStyle(button, self.styleInfo)
            if not suppressUpdate or buttonKeybindChanging then
                button:SetKeybind(keybind, nil, nil, alwaysPreferGamepadMode)
            end
            local enabled = GetValueFromRawOrFunction(keybindButtonDescriptor, "enabled")
            if(enabled == nil) then enabled = true end
            local cooldown = GetValueFromRawOrFunction(keybindButtonDescriptor, "cooldown")
            if cooldown then
                enabled = false
            end
            button:SetEnabled(enabled)
            local name = GetValueFromRawOrFunction(keybindButtonDescriptor, "name")
            if cooldown then
                local seconds = zo_ceil(cooldown)
                name = zo_strformat(SI_BINDING_NAME_COOLDOWN_FORMAT, name, seconds)
            end
            button.nameLabel:SetText(name)
            local iconPath = GetValueFromRawOrFunction(keybindButtonDescriptor, "icon")
            if iconPath and iconPath ~= "" then
                if not button.icon then
                    button.icon = CreateControl("$(parent)Icon", button, CT_TEXTURE)
                    button.icon:SetDimensions(20, 20)
                    button.icon:SetAnchor(LEFT, button.nameLabel, RIGHT, 0, -5)
                end
                button.icon:SetTexture(iconPath)
                button.icon:SetHidden(false)
            else
                if button.icon then
                    button.icon:SetHidden(true)
                end
            end
        else
            if customKeybindControl then
                customKeybindControl:SetHidden(true)
            end
        end
        button:SetHidden(not isVisible)
    end
    local function KeyboardSort(buttonLeft, buttonRight)
        local leftOrder = GetValueFromRawOrFunction(buttonLeft.keybindButtonDescriptor, "order") or 0
        local rightOrder = GetValueFromRawOrFunction(buttonRight.keybindButtonDescriptor, "order") or 0
        if leftOrder == rightOrder then
            return buttonLeft.insertionOrder < buttonRight.insertionOrder
        end
        return leftOrder < rightOrder
    end
    local GAMEPAD_BUTTON_ORDER = {
            UI_SHORTCUT_EXIT = 0,
            UI_SHORTCUT_PRIMARY = 1,
            UI_SHORTCUT_NEGATIVE = 2,
            UI_SHORTCUT_SECONDARY = 3,
            UI_SHORTCUT_TERTIARY = 4,
            UI_SHORTCUT_LEFT_STICK = 5,
            UI_SHORTCUT_RIGHT_STICK = 6,
            UI_SHORTCUT_LEFT_TRIGGER = 7,
            UI_SHORTCUT_RIGHT_TRIGGER = 8,
            DIALOG_PRIMARY = 1,
            DIALOG_NEGATIVE = 2,
            DIALOG_SECONDARY = 3,
            DIALOG_TERTIARY = 4,
        }
    local function GamepadSort(buttonLeft, buttonRight)
        local leftKeybindDescriptor = buttonLeft.keybindButtonDescriptor
        local rightKeybindDescriptor = buttonRight.keybindButtonDescriptor
        local leftOrder = GetValueFromRawOrFunction(leftKeybindDescriptor, "gamepadOrder") or GAMEPAD_BUTTON_ORDER[GetValueFromRawOrFunction(leftKeybindDescriptor, "keybind")] or GetValueFromRawOrFunction(leftKeybindDescriptor, "order") or 0
        local rightOrder = GetValueFromRawOrFunction(rightKeybindDescriptor, "gamepadOrder") or GAMEPAD_BUTTON_ORDER[GetValueFromRawOrFunction(rightKeybindDescriptor, "keybind")] or GetValueFromRawOrFunction(rightKeybindDescriptor, "order") or 0
        if leftOrder == rightOrder then
            return buttonLeft.insertionOrder < buttonRight.insertionOrder
        end
        return leftOrder < rightOrder
    end
    function ZO_KeybindStrip:UpdateAnchorsInternal(anchorTable, anchor, relativeAnchor, parent, startOffset, yOffset)
        if anchorTable and #anchorTable > 0 then
            if IsInGamepadPreferredMode() then
                table.sort(anchorTable, GamepadSort)
            else
                table.sort(anchorTable, KeyboardSort)
            end
            for i, button in ipairs(anchorTable) do
                button:ClearAnchors()
            end
            local prevButton
            for i, button in ipairs(anchorTable) do
                local isVisible = IsVisible(button.keybindButtonDescriptor)
                    local wasVisible = not button:IsHidden()
                    if isVisible and not wasVisible then
                         local UPDATE_ONLY = true
                         self:SetUpButton(button, UPDATE_ONLY)
                    end
                if isVisible then
                    button:SetParent(parent)
                    if prevButton then
                        button:SetAnchor(anchor, prevButton, relativeAnchor)
                    else
                        button:SetAnchor(anchor, nil, anchor, startOffset, yOffset)
                    end
                    prevButton = button
                end
            end
                        
            return anchorTable
        end
        return nil
    end
    function ZO_KeybindStrip:UpdateAnchors()
        local yOffset = self.styleInfo and self.styleInfo.yAnchorOffset or 0
        self.leftButtons = self:UpdateAnchorsInternal(self.leftButtons, LEFT, RIGHT, self.control, self.styleInfo and self.styleInfo.leftAnchorOffset or 0, yOffset)
        self.rightButtons = self:UpdateAnchorsInternal(self.rightButtons, RIGHT, LEFT, self.control, self.styleInfo and self.styleInfo.rightAnchorOffset or 0, yOffset)
        self.centerButtons = self:UpdateAnchorsInternal(self.centerButtons, LEFT, RIGHT, self.centerParent, self.styleInfo and self.styleInfo.centerAnchorOffset or 0, yOffset)
    end
end