Back to Home

ESO Lua File v101043

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
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
ZO_KeybindButtonMixin = {}
function ZO_KeybindButtonMixin:GetKeybind()
    return ZO_Keybindings_ShouldUseGamepadAction(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)
end
function ZO_KeybindButtonMixin:SetText(text, narrationText)
    self.nameText = text
    self.nameTextNarration = narrationText
    self.nameLabel:SetText(self.nameText)
end
function ZO_KeybindButtonMixin:SetCustomKeyText(keyText)
    self.customKeyText = true
end
function ZO_KeybindButtonMixin:SetCustomKeyIcon(keyIcon)
    local key = ("|t%f%%:%f%%:%s|t"):format(200, 200, keyIcon)
    self.customKeyText = true
    self.keyLabel:SetText(key)
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)
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, showAsHold)
    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, showAsHold)
    else
    end
end
function ZO_KeybindButtonMixin:SetCallback(callback)
    self.callback = callback
end
function ZO_KeybindButtonMixin:GetKeybindButtonDescriptorReference()
    return self.keybindDescriptorReference
end
--Generate narration data for this keybind button
--If the keybind is not visible, nothing will be returned
function ZO_KeybindButtonMixin:GetKeybindButtonNarrationData()
    local visible = self.keybindDescriptorReference and self.keybindDescriptorReference.visible or true
    if type(visible) == "function" then
        visible = visible()
    end
    if visible then
        local narrationData =
        {
            name = self.nameTextNarration or self.nameText,
            keybindName = ZO_Keybindings_GetHighestPriorityNarrationStringFromAction(self:GetKeybind()) or GetString(SI_ACTION_IS_NOT_BOUND),
            enabled = self.enabled,
        }
        return narrationData
    end
end
function ZO_KeybindButtonMixin:SetKeybindButtonDescriptor(keybindDescriptor)
    self.keybindDescriptorReference = keybindDescriptor
    if keybindDescriptor.name then
        local name = keybindDescriptor.name
        if type(keybindDescriptor.name) == "function" then
            name = keybindDescriptor.name()
        end
       self:SetText(name)
    end
    
    if keybindDescriptor.keybind then
        self:SetKeybind(keybindDescriptor.keybind)
    end
    if keybindDescriptor.callback then
        self:SetCallback(keybindDescriptor.callback)
    end
    if keybindDescriptor.sound then
        self:SetClickSound(keybindDescriptor.sound)
    end
    if keybindDescriptor.customKeyText then
        self:SetCustomKeyText(keybindDescriptor.customKeyText)
    end
    if keybindDescriptor.customKeyIcon then
        self:SetCustomKeyIcon(keybindDescriptor.customKeyIcon)
    end
end
function ZO_KeybindButtonMixin:OnClicked()
    local visible = self.keybindDescriptorReference and self.keybindDescriptorReference.visible or true
    if type(visible) == "function" then
        visible = visible()
    end
    if visible then
        if self.enabled then
            if self.clickSound then
                if type(self.clickSound) == "function" then
                    PlaySound(self.clickSound())
                else
                    PlaySound(self.clickSound)
                end
            end
            if self.callback then
                self.callback(self)
            end
        else
            PlaySound(SOUNDS.KEYBIND_BUTTON_DISABLED)
        end
    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
    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)
    table.insert(g_keybindButtons, self)
end
    self:SetText(text)
end
ZO_ChromaKeybindButtonMixin = {}
function ZO_ChromaKeybindButtonMixin:SetChromaEnabled(enabled)
    if self.chromaEnabled ~= enabled then
        self.chromaEnabled = enabled
        self:UpdateChromaEffect()
    end
end
function ZO_ChromaKeybindButtonMixin:AddChromaEffect()
    if ZO_RZCHROMA_EFFECTS and self:AreChromaEffectsEnabled() then
        local keybindAction = self:GetKeyboardKeybind()
        if keybindAction then
            ZO_RZCHROMA_EFFECTS:AddKeybindActionEffect(keybindAction)
        end
    end
end
function ZO_ChromaKeybindButtonMixin:RemoveChromaEffect()
    if ZO_RZCHROMA_EFFECTS then
        local keybindAction = self:GetKeyboardKeybind()
        if keybindAction then
            ZO_RZCHROMA_EFFECTS:RemoveKeybindActionEffect(keybindAction)
        end
    end
end
function ZO_ChromaKeybindButtonMixin:UpdateChromaEffect()
    if ZO_RZCHROMA_EFFECTS and not self:IsHidden() then
        local keybindAction = self:GetKeyboardKeybind()
        if keybindAction then
            if self:AreChromaEffectsEnabled() then
                ZO_RZCHROMA_EFFECTS:AddKeybindActionEffect(keybindAction)
            else
                ZO_RZCHROMA_EFFECTS:RemoveKeybindActionEffect(keybindAction)
            end
        end
    end
end
function ZO_ChromaKeybindButtonMixin:SetKeybind(keybind, showUnbound, gamepadPreferredKeybind, alwaysPreferGamepadMode)
    local previousKeybind = self:GetKeyboardKeybind()
    local refreshKeybind = keybind ~= previousKeybind and not self:IsHidden()
    if refreshKeybind then
        self:RemoveChromaEffect()
    end
    ZO_KeybindButtonMixin.SetKeybind(self, keybind, showUnbound, gamepadPreferredKeybind, alwaysPreferGamepadMode)
    if refreshKeybind then
        self:AddChromaEffect()
    end
end
function ZO_ChromaKeybindButtonMixin:UpdateEnabledState()
    ZO_KeybindButtonMixin.UpdateEnabledState(self)
end
function ZO_ChromaKeybindButtonMixin:AreChromaEffectsEnabled()
    return self.chromaEnabled and self:IsEnabled()
end
    zo_mixin(self, ZO_ChromaKeybindButtonMixin)
    self:SetChromaEnabled(true)
end
    self:SetText(text)
end
    if self.AddChromaEffect then
        self:AddChromaEffect()
    end
end
    if self.RemoveChromaEffect then
        self:RemoveChromaEffect()
    end
end