Back to Home

ESO Lua File v100020

libraries/zo_keybindbutton/zo_keybindbutton.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
ZO_KeybindButtonMixin = {}
function ZO_KeybindButtonMixin:GetKeybind()
    return (IsInGamepadPreferredMode() or self.alwaysPreferGamepadMode) and self.gamepadPreferredKeybind or self.keybind
end
function ZO_KeybindButtonMixin:GetKeyboardKeybind()
    return self.keybind
end
function ZO_KeybindButtonMixin:GetGamepadKeybind()
    return self.gamepadPreferredKeybind
end
function ZO_KeybindButtonMixin:UpdateEnabledState()
    self.nameLabel:SetEnabled(self.enabled)
    local keybindEnabled = self.keybindEnabled and self.enabled
    local keybindColor = keybindEnabled and ZO_SELECTED_TEXT or ZO_DISABLED_TEXT
    self.keyLabel:SetColor(keybindColor:UnpackRGBA())
    self.keyLabel:SetAlpha((self:GetKeybind() or self.customKeyText) and 1 or 0)
    ZO_KeyMarkupLabel_SetEdgeFileColor(self.keyLabel, keybindColor)
end
function ZO_KeybindButtonMixin:SetEnabled(enabled)
    if(enabled ~= self.enabled) then
        self.enabled = enabled
        self:UpdateEnabledState()
    end
end
function ZO_KeybindButtonMixin:SetState(buttonState, locked)
    self:SetEnabled(buttonState ~= BSTATE_DISABLED)
end
function ZO_KeybindButtonMixin:SetKeybindEnabled(enabled)
    if(enabled ~= self.keybindEnabled) then
        self.keybindEnabled = enabled
        self:UpdateEnabledState()
    end
end
function ZO_KeybindButtonMixin:IsEnabled()
    return self.enabled
end
function ZO_KeybindButtonMixin:SetClickSound(clickSound)
    self.clickSound = clickSound
end
function ZO_KeybindButtonMixin:SetText(text)
    self.nameLabel:SetText(text)
end
function ZO_KeybindButtonMixin:SetCustomKeyText(keyText)
    self.customKeyText = true
    self.keyLabel:SetText(ZO_Keybindings_GenerateKeyMarkup(keyText))
    self:UpdateEnabledState()
end
function ZO_KeybindButtonMixin:SetCustomKeyIcon(keyIcon)
    local key = ("|t%f%%:%f%%:%s|t"):format(200, 200, keyIcon)
    self.customKeyText = true
    self.keyLabel:SetText(key)
    self:UpdateEnabledState()
end
function ZO_KeybindButtonMixin:ShowKeyIcon()
    self.keyLabel:SetHidden(false)
end
function ZO_KeybindButtonMixin:HideKeyIcon()
    self.keyLabel:SetHidden(true)
end
function ZO_KeybindButtonMixin:SetupStyle(styleInfo)
    KEYBIND_STRIP:SetupButtonStyle(self, styleInfo or KEYBIND_STRIP:GetStyle())
end
function ZO_KeybindButtonMixin:SetKeybindEnabledInEdit(enabled)
    self.keybindEnabledInEdit = enabled
end
function ZO_KeybindButtonMixin:SetMouseOverEnabled(enabled)
end
function ZO_KeybindButtonMixin:SetNormalTextColor(color)
    ZO_SelectableLabel_SetNormalColor(self.nameLabel, color)
end
function ZO_KeybindButtonMixin:SetNameFont(font)
    self.nameLabel:SetFont(font)
end
function ZO_KeybindButtonMixin:SetKeyFont(font)
    self.keyLabel:SetFont(font)
end
function ZO_KeybindButtonMixin:AdjustBindingAnchors(wideSpacing)
    if(not self:GetUsingCustomAnchors()) then
        self.keyLabel:SetAnchor(RIGHT, self.nameLabel, LEFT, wideSpacing and -15 or 0)
    end
end
function ZO_KeybindButtonMixin:SetUsingCustomAnchors(useCustom)
    self.usingCustomAnchors = useCustom
end
function ZO_KeybindButtonMixin:GetUsingCustomAnchors()
    return self.usingCustomAnchors
end
local function OnKeybindLabelChanged(label, bindingText, key, mod1, mod2, mod3, mod4)
end
function ZO_KeybindButtonMixin:SetKeybind(keybind, showUnbound, gamepadPreferredKeybind, alwaysPreferGamepadMode)
    local hadKeybind = self:GetKeybind() ~= nil
    self.keybind = keybind
    self.gamepadPreferredKeybind = gamepadPreferredKeybind
    self.alwaysPreferGamepadMode = alwaysPreferGamepadMode
    keybind = self:GetKeybind()
    if keybind then
        if hadKeybind then
            ZO_Keybindings_UnregisterLabelForBindingUpdate(self.keyLabel)
        end
        ZO_Keybindings_RegisterLabelForBindingUpdate(self.keyLabel, self.keybind, showUnbound, self.gamepadPreferredKeybind, OnKeybindLabelChanged, alwaysPreferGamepadMode)
    else
        ZO_Keybindings_UnregisterLabelForBindingUpdate(self.keyLabel)
    end
    self:UpdateEnabledState()
end
function ZO_KeybindButtonMixin:SetCallback(callback)
    self.callback = callback
end
function ZO_KeybindButtonMixin:OnClicked()
    if(self.enabled) then
        if(self.clickSound) then
            PlaySound(self.clickSound)
        end
        if(self.callback) then
            self.callback(self)
        end
    else
        PlaySound(SOUNDS.KEYBIND_BUTTON_DISABLED)
    end
end
local g_cooldownButtons = {}
-- Should only be used for Floating Keybind Buttons
-- Otherwise use ZO_KeybindStrip:TriggerCooldown
function ZO_KeybindButtonMixin:SetCooldown(durationMs)
    self.cooldownDurationMs = durationMs
    self.cooldownStartTimeMs = GetFrameTimeMilliseconds()
    --Check for duplicates
    for i, button in ipairs(g_cooldownButtons) do
        if button == self then
            return
        end
    end
    self.baseText = self.nameLabel:GetText()
    table.insert(g_cooldownButtons, self)
end
local g_areKeybindsEnabled
local g_keybindButtons = {}
local g_numDisabledReferences = 0
local function OnUpdate()
    local shouldKeybindsBeEnabled = WINDOW_MANAGER:GetFocusControl() == nil and g_numDisabledReferences == 0
    if shouldKeybindsBeEnabled ~= g_areKeybindsEnabled then
        g_areKeybindsEnabled = shouldKeybindsBeEnabled
        for i = 1, #g_keybindButtons do
            local currentKeybindButton = g_keybindButtons[i]
            if not currentKeybindButton.keybindEnabledInEdit then
                currentKeybindButton:SetKeybindEnabled(shouldKeybindsBeEnabled)
            end
        end
    end
    local currentTimeMs = GetFrameTimeMilliseconds()
    for i = #g_cooldownButtons, 1, -1 do
        local button = g_cooldownButtons[i]
        local timeDifferenceMs = currentTimeMs - button.cooldownStartTimeMs
        local shouldBeEnabled = timeDifferenceMs > button.cooldownDurationMs
        if shouldBeEnabled then
            button:SetText(button.baseText)
            button.cooldownDurationMs = nil
            button.cooldownStartTimeMs = nil
            table.remove(g_cooldownButtons, i)
        else
            local secondsTillEnabled = zo_ceil((button.cooldownStartTimeMs + button.cooldownDurationMs - currentTimeMs) / 1000)
            button:SetText(zo_strformat(SI_BINDING_NAME_COOLDOWN_FORMAT, button.baseText, secondsTillEnabled))
        end
        button:SetEnabled(shouldBeEnabled)
    end
end
EVENT_MANAGER:RegisterForUpdate("KeybindButtonUpdate", 0, OnUpdate)
    g_numDisabledReferences = g_numDisabledReferences + 1
end
    g_numDisabledReferences = g_numDisabledReferences - 1
end
function ZO_KeybindButtonTemplate_OnMouseUp(self, button, upInside)
    if upInside and (button == MOUSE_BUTTON_INDEX_LEFT) and self.enabled then
        self:OnClicked()
    end
end
    self.nameLabel = self:GetNamedChild("NameLabel")
    self.keyLabel = self:GetNamedChild("KeyLabel")
    self.keyLabel.owner = self
    self.enabled = true
    self.keybindEnabled = true
    zo_mixin(self, ZO_KeybindButtonMixin)
    ZO_KeyMarkupLabel_SetCustomOffsets(self.keyLabel, -5, 5, -2, 3)
    ZO_SelectableLabel_OnInitialized(self.nameLabel)
    table.insert(g_keybindButtons, self)
    self:UpdateEnabledState()
end
function ZO_KeybindButtonTemplate_Setup(self, keybind, callbackFunction, text)
    self:SetKeybind(keybind)
    self:SetText(text)
    self:SetCallback(callbackFunction)
end