forked from brittyazel/Neuron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuron-KeyBinder.lua
442 lines (334 loc) · 11.7 KB
/
Neuron-KeyBinder.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
--Neuron, a World of Warcraft® user interface addon.
--This file is part of Neuron.
--
--Neuron is free software: you can redistribute it and/or modify
--it under the terms of the GNU General Public License as published by
--the Free Software Foundation, either version 3 of the License, or
--(at your option) any later version.
--
--Neuron is distributed in the hope that it will be useful,
--but WITHOUT ANY WARRANTY; without even the implied warranty of
--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
--GNU General Public License for more details.
--
--You should have received a copy of the GNU General Public License
--along with Foobar. If not, see <https://www.gnu.org/licenses/>.
--
--Copyright for portions of Neuron are held by Connor Chenoweth,
--a.k.a Maul, 2014 as part of his original project, Ion. All other
--copyrights for Neuron are held by Britt Yazel, 2017-2018.
local DEFAULT_VIRTUAL_KEY = "LeftButton"
local NEURON_VIRTUAL_KEY = "Hotkey"
local VIRTUAL_KEY_LIST = {
DEFAULT_VIRTUAL_KEY,
NEURON_VIRTUAL_KEY
}
local NeuronBinder = {}
Neuron.NeuronBinder = NeuronBinder
local L = LibStub("AceLocale-3.0"):GetLocale("Neuron")
--- Returns a list of the available spell icon filenames for use in macros
-- @param N/A
-- @return text field of the key modifiers currently being pressed
function NeuronBinder:GetModifier()
local modifier
if (IsAltKeyDown()) then
modifier = "ALT-"
end
if (IsControlKeyDown()) then
if (modifier) then
modifier = modifier.."CTRL-";
else
modifier = "CTRL-";
end
end
if (IsShiftKeyDown()) then
if (modifier) then
modifier = modifier.."SHIFT-";
else
modifier = "SHIFT-";
end
end
return modifier
end
--- Returns the keybind for a given button
-- @param Button: The button to keybindings to look up
-- @return bindkeys: The current key that is bound to the selected button
function NeuronBinder:GetBindkeyList(button)
if (not button.data) then return L["None"] end
local bindkeys = button.keys.hotKeyText:gsub(":", ", ")
bindkeys = bindkeys:gsub("^, ", "")
bindkeys = bindkeys:gsub(", $", "")
if (strlen(bindkeys) < 1) then
bindkeys = L["None"]
end
return bindkeys
end
--- Returns the text value of a keybind
-- @param key: The key to look up
-- @return keytext: The text value for the key
function NeuronBinder:GetKeyText(key)
local keytext
if (key:find("Button")) then
keytext = key:gsub("([Bb][Uu][Tt][Tt][Oo][Nn])(%d+)","m%2")
elseif (key:find("NUMPAD")) then
keytext = key:gsub("NUMPAD","n")
keytext = keytext:gsub("DIVIDE","/")
keytext = keytext:gsub("MULTIPLY","*")
keytext = keytext:gsub("MINUS","-")
keytext = keytext:gsub("PLUS","+")
keytext = keytext:gsub("DECIMAL",".")
elseif (key:find("MOUSEWHEEL")) then
keytext = key:gsub("MOUSEWHEEL","mw")
keytext = keytext:gsub("UP","U")
keytext = keytext:gsub("DOWN","D")
else
keytext = key
end
keytext = keytext:gsub("ALT%-","a")
keytext = keytext:gsub("CTRL%-","c")
keytext = keytext:gsub("SHIFT%-","s")
keytext = keytext:gsub("INSERT","Ins")
keytext = keytext:gsub("DELETE","Del")
keytext = keytext:gsub("HOME","Home")
keytext = keytext:gsub("END","End")
keytext = keytext:gsub("PAGEUP","PgUp")
keytext = keytext:gsub("PAGEDOWN","PgDn")
keytext = keytext:gsub("BACKSPACE","Bksp")
keytext = keytext:gsub("SPACE","Spc")
return keytext
end
--- Clears the bindings of a given button
-- @param button: The button to clear
-- @param key: ?
function NeuronBinder:ClearBindings(button, key)
if (key) then
SetOverrideBinding(button, true, key, nil)
local newkey = key:gsub("%-", "%%-")
button.keys.hotKeys = button.keys.hotKeys:gsub(newkey..":", "")
local keytext = NeuronBinder:GetKeyText(key)
button.keys.hotKeyText = button.keys.hotKeyText:gsub(keytext..":", "")
else
local bindCmdPrefix = "CLICK " .. button:GetName()
---clear bindings for all virtual keys (LeftButton, Hotkey, ...)
---using the bindCmdPrefix and concatenating it with each virtual key
for _, virtualKey in ipairs(VIRTUAL_KEY_LIST) do
local bindCmd = bindCmdPrefix .. ":" .. virtualKey
while (GetBindingKey(bindCmd)) do
SetBinding(GetBindingKey(bindCmd), nil)
end
end
ClearOverrideBindings(button)
button.keys.hotKeys = ":"
button.keys.hotKeyText = ":"
end
NeuronBinder:ApplyBindings(button)
end
--- Sets a keybinding to a button
-- @param button: The button to set keybinding for
-- @param key: The key to be used
function NeuronBinder:SetNeuronBinding(button, key)
local found
gsub(button.keys.hotKeys, "[^:]+", function(binding) if(binding == key) then found = true end end)
if (not found) then
local keytext = NeuronBinder:GetKeyText(key)
button.keys.hotKeys = button.keys.hotKeys..key..":"
button.keys.hotKeyText = button.keys.hotKeyText..keytext..":"
end
NeuronBinder:ApplyBindings(button)
end
--- Applys binding to button
-- @param button: The button to apply settings go
function NeuronBinder:ApplyBindings(button)
button:SetAttribute("hotkeypri", button.keys.hotKeyPri)
local virtualKey
---checks if the button is a Neuron action or a special Blizzard action (such as a zone ability)
---this is necessary because Blizzard buttons usually won't work and can give very weird results
---if clicked with a virtual key other than the default "LeftButton"
if (button.class == "ActionBar") then
virtualKey = NEURON_VIRTUAL_KEY
else
virtualKey = DEFAULT_VIRTUAL_KEY
end
if (button:IsVisible() or button:GetParent():GetAttribute("concealed")) then
gsub(button.keys.hotKeys, "[^:]+", function(key) SetOverrideBindingClick(button, button.keys.hotKeyPri, key, button:GetName(), virtualKey) end)
end
button:SetAttribute("hotkeys", button.keys.hotKeys)
button.hotkey:SetText(button.keys.hotKeyText:match("^:([^:]+)") or "")
if (button.bindText) then
button.hotkey:Show()
else
button.hotkey:Hide()
end
if (GetCurrentBindingSet() > 0 and GetCurrentBindingSet() < 3) then SaveBindings(GetCurrentBindingSet()) end
end
--- Processes the change to a key bind (i think)
-- @param button: The button to set keybinding for
-- @param key: The key to be used
function NeuronBinder:ProcessBinding(binder, key, button)
if (button and button.keys and button.keys.hotKeyLock) then
UIErrorsFrame:AddMessage(L["Bindings_Locked_Notice"], 1.0, 1.0, 1.0, 1.0, UIERRORS_HOLD_TIME)
return
end
if (key == "ESCAPE") then
NeuronBinder:ClearBindings(button)
elseif (key) then
for _,binder in pairs(Neuron.BINDIndex) do
if (button ~= binder.button and binder.button.keys and not binder.button.keys.hotKeyLock) then
binder.button.keys.hotKeys:gsub("[^:]+", function(binding) if (key == binding) then NeuronBinder:ClearBindings(binder.button, binding) NeuronBinder:ApplyBindings(binder.button) end end)
end
end
NeuronBinder:SetNeuronBinding(button, key)
end
if (binder:IsVisible()) then
NeuronBinder:OnEnter(binder)
end
end
--- OnShow Event handler
function NeuronBinder:OnShow(binder)
local button = binder.button
if (button) then
if (button.bar) then
binder:SetFrameLevel(button.bar:GetFrameLevel()+1)
end
local priority = ""
if (button.keys.hotKeyPri) then
priority = "|cff00ff00"..L["Priority"].."|r\n"
end
if (button.keys.hotKeyLock) then
binder.type:SetText(priority.."|cfff00000"..L["Locked"].."|r")
else
binder.type:SetText(priority.."|cffffffff"..L["Bind"].."|r")
end
end
end
--- OnHide Event handler
function NeuronBinder:OnHide(binder)
end
--- OnEnter Event handler
function NeuronBinder:OnEnter(binder)
local button = binder.button
local name = binder.bindType:gsub("^%l", string.upper).. " " .. button.id --default to "button #" in the case that a button is empty
---TODO:we should definitely added name strings for pets/companions as well. This was just to get it going
if binder.button.spellID then
name = GetSpellInfo(binder.button.spellID)
elseif binder.button.actionSpell then
name = binder.button.actionSpell
elseif binder.button.macroitem then
name = binder.button.macroitem
elseif binder.button.macrospell then
name = binder.button.macrospell --this is kind of a catch-all
end
if not name then
name = "Button"
end
binder.select:Show()
GameTooltip:SetOwner(binder, "ANCHOR_RIGHT")
GameTooltip:ClearLines()
GameTooltip:SetText("Neuron", 1.0, 1.0, 1.0)
GameTooltip:AddLine(L["Keybind_Tooltip_1"] .. ": |cffffffff" .. name .. "|r")
GameTooltip:AddLine(L["Keybind_Tooltip_2"] .. ": |cffffffff" .. NeuronBinder:GetBindkeyList(button) .. "|r")
GameTooltip:AddLine(" ")
GameTooltip:AddLine(L["Keybind_Tooltip_3"])
GameTooltip:AddLine(L["Keybind_Tooltip_4"])
GameTooltip:AddLine(L["Keybind_Tooltip_5"])
GameTooltip:Show()
end
--- OnLeave Event handler
function NeuronBinder:OnLeave(binder)
binder.select:Hide()
GameTooltip:Hide()
end
--- OnUpdate Event handler
function NeuronBinder:OnUpdate(binder)
if(Neuron.enteredWorld) then
if (binder:IsMouseOver()) then
binder:EnableKeyboard(true)
else
binder:EnableKeyboard(false)
end
end
end
--- OnClick Event handler
-- @param button: The button that was clicked
function NeuronBinder:OnClick(binder, buttonpressed)
if (buttonpressed == "LeftButton") then
if (binder.button.keys.hotKeyLock) then
binder.button.keys.hotKeyLock = false
else
binder.button.keys.hotKeyLock = true
end
NeuronBinder:OnShow(binder)
return
end
if (buttonpressed == "RightButton") then
if (binder.button.keys.hotKeyPri) then
binder.button.keys.hotKeyPri = false
else
binder.button.keys.hotKeyPri = true
end
NeuronBinder:ApplyBindings(binder.button)
NeuronBinder:OnShow(binder)
return
end
local modifier, key = NeuronBinder:GetModifier()
if (buttonpressed == "MiddleButton") then
key = "Button3"
else
key = buttonpressed
end
if (modifier) then
key = modifier..key
end
NeuronBinder:ProcessBinding(binder, key, binder.button)
end
--- OnKeyDown Event handler
-- @param key: The key that was pressed
function NeuronBinder:OnKeyDown(binder, key)
if (key:find("ALT") or key:find("SHIFT") or key:find("CTRL") or key:find("PRINTSCREEN")) then
return
end
local modifier = NeuronBinder:GetModifier()
if (modifier) then
key = modifier..key
end
NeuronBinder:ProcessBinding(binder, key, binder.button)
end
--- OnMouseWheel Event handler
-- @param delta: direction mouse wheel moved
function NeuronBinder:OnMouseWheel(binder, delta)
local modifier, key, action = NeuronBinder:GetModifier()
if (delta > 0) then
key = "MOUSEWHEELUP"
action = "MousewheelUp"
else
key = "MOUSEWHEELDOWN"
action = "MousewheelDown"
end
if (modifier) then
key = modifier..key
end
NeuronBinder:ProcessBinding(binder, key, binder.button)
end
function NeuronBinder:CreateBindFrame(button)
local binder = CreateFrame("Button", button:GetName().."BindFrame", button, "NeuronBindFrameTemplate")
setmetatable(binder, { __index = CreateFrame("Button") })
binder:EnableMouseWheel(true)
binder:RegisterForClicks("AnyDown")
binder:SetAllPoints(button)
binder:SetScript("OnShow", function(self) NeuronBinder:OnShow(self) end)
binder:SetScript("OnHide", function(self) NeuronBinder:OnHide(self) end)
binder:SetScript("OnEnter", function(self) NeuronBinder:OnEnter(self) end)
binder:SetScript("OnLeave", function(self) NeuronBinder:OnLeave(self) end)
binder:SetScript("OnClick", function(self, button) NeuronBinder:OnClick(self, button) end)
binder:SetScript("OnKeyDown", function(self, key) NeuronBinder:OnKeyDown(self, key) end)
binder:SetScript("OnMouseWheel", function(self, delta) NeuronBinder:OnMouseWheel(self, delta) end)
binder:SetScript("OnUpdate", function(self) NeuronBinder:OnUpdate(self) end)
binder.type:SetText(L["Bind"])
binder.button = button
binder.bindType = "button"
button.binder = binder
button:SetAttribute("hotkeypri", button.keys.hotKeyPri)
button:SetAttribute("hotkeys", button.keys.hotKeys)
Neuron.BINDIndex[button.class..button.bar.DB.id.."_"..button.id] = binder
binder:Hide()
end