Back to Home

ESO Lua File v100018

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
local 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))
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)
    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
    end
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
function ZO_KeybindButtonTemplate_OnMouseUp(self, button, upInside)
    if(upInside and (button == MOUSE_BUTTON_INDEX_LEFT) and self.enabled) then
        self:OnClicked()
    end
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
            if(not g_keybindButtons[i].keybindEnabledInEdit) then
                g_keybindButtons[i]:SetKeybindEnabled(shouldKeybindsBeEnabled)
            end
        end
    end
end
EVENT_MANAGER:RegisterForUpdate("KeybindButtonUpdate", 0, OnUpdate)
    g_numDisabledReferences = g_numDisabledReferences + 1
end
    g_numDisabledReferences = g_numDisabledReferences - 1
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
function ZO_KeybindButtonTemplate_Setup(self, keybind, callbackFunction, text)
    self:SetKeybind(keybind)
    self:SetText(text)
    self:SetCallback(callbackFunction)
end