This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Neuron.lua
642 lines (514 loc) · 19.2 KB
/
Neuron.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
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
-- Neuron is a World of Warcraft® user interface addon.
-- Copyright (c) 2017-2023 Britt W. Yazel
-- Copyright (c) 2006-2014 Connor H. Chenoweth
-- This code is licensed under the MIT license (see LICENSE for details)
local _, addonTable = ...
local Spec = addonTable.utilities.Spec
local DBFixer = addonTable.utilities.DBFixer
local Array = addonTable.utilities.Array
local ButtonBinder = addonTable.overlay.ButtonBinder
local ButtonEditor = addonTable.overlay.ButtonEditor
local BarEditor = addonTable.overlay.BarEditor
---@class Neuron : AceAddon-3.0 @define The main addon object for the Neuron Action Bar addon
addonTable.Neuron = LibStub("AceAddon-3.0"):NewAddon(CreateFrame("Frame", nil, UIParent), "Neuron", "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0", "AceTimer-3.0", "AceSerializer-3.0")
local Neuron = addonTable.Neuron
local DB
local LibDeflate = LibStub:GetLibrary("LibDeflate")
local L = LibStub("AceLocale-3.0"):GetLocale("Neuron")
local LATEST_VERSION_NUM = "1.4.1" --this variable is set to popup a welcome message upon updating/installing. Only change it if you want to pop up a message after the users next update
--prepare the Neuron table with some sub-tables that will be used down the road
Neuron.bars = {} --this table will be our main handle for all of our bars.
Neuron.registeredBarData = {}
--these are the database tables that are going to hold our data. They are global because every .lua file needs access to them
Neuron.itemCache = {} --Stores a cache of all items that have been seen by a Neuron button
Neuron.spellCache = {} --Stores a cache of all spells that have been seen by a Neuron button
Neuron.barEditMode = false
Neuron.buttonEditMode = false
Neuron.bindingMode = false
Neuron.isWoWClassicEra = WOW_PROJECT_ID == WOW_PROJECT_CLASSIC
Neuron.isWoWWrathClassic = WOW_PROJECT_ID == WOW_PROJECT_WRATH_CLASSIC
Neuron.isWoWRetail = WOW_PROJECT_ID == WOW_PROJECT_MAINLINE
Neuron.STRATAS = {
[1] = "BACKGROUND",
[2] = "LOW",
[3] = "MEDIUM",
[4] = "HIGH",
[5] = "DIALOG",
[6] = "TOOLTIP"
}
Neuron.TIMERLIMIT = 4
Neuron.SNAPTO_TOLERANCE = 28
Neuron.DEBUG = true
-------------------------------------------------------------------------
--------------------Start of Functions-----------------------------------
-------------------------------------------------------------------------
--- **OnInitialize**, which is called directly after the addon is fully loaded.
--- do init tasks here, like loading the Saved Variables
--- or setting up slash commands.
function Neuron:OnInitialize()
Neuron.db = LibStub("AceDB-3.0"):New("NeuronProfilesDB", addonTable.databaseDefaults)
--Check if the current database needs to be migrated, and attempt the migration
Neuron.db = DBFixer.databaseMigration(Neuron.db)
DB = Neuron.db.profile
Neuron.db.RegisterCallback(Neuron, "OnProfileChanged", "RefreshConfig")
Neuron.db.RegisterCallback(Neuron, "OnProfileCopied", "RefreshConfig")
Neuron.db.RegisterCallback(Neuron, "OnProfileReset", "RefreshConfig")
Neuron.db.RegisterCallback(Neuron, "OnDatabaseReset", "RefreshConfig")
--load saved variables into working variable containers
Neuron.itemCache = DB.NeuronItemCache
Neuron.spellCache = DB.NeuronSpellCache
Neuron.class = select(2, UnitClass("player"))
Neuron:UpdateStanceStrings()
StaticPopupDialogs["ReloadUI"] = {
text = L["ReloadUI"],
button1 = OKAY,
OnAccept = function()
ReloadUI()
end,
preferredIndex = 3, -- avoid some UI taint, see http://www.wowace.com/announcements/how-to-avoid-some-ui-taint/
}
--Initialize the Minimap Icon
Neuron:Minimap_IconInitialize()
--Initialize the chat commands (i.e. /neuron)
--Neuron:RegisterChatCommand("neuron", "slashHandler")
--build all bar and button frames and run initial setup
Neuron.registeredBarData = Neuron:RegisterBars(DB)
if DB.firstRun then
Neuron:InitializeEmptyDatabase(DB)
end
Neuron:CreateBarsAndButtons(DB)
end
--- **OnEnable** which gets called during the PLAYER_LOGIN event, when most of the data provided by the game is already present.
--- Do more initialization here, that really enables the use of your addon.
--- Register Events, Hook functions, Create Frames, Get information from
--- the game that wasn't available in OnInitialize
function Neuron:OnEnable()
if Neuron.DEBUG then
_G.Neuron = Neuron
end
Neuron:RegisterEvent("PLAYER_REGEN_DISABLED")
Neuron:RegisterEvent("PLAYER_ENTERING_WORLD")
Neuron:RegisterEvent("SPELLS_CHANGED")
Neuron:RegisterEvent("CHARACTER_POINTS_CHANGED")
Neuron:RegisterEvent("LEARNED_SPELL_IN_TAB")
Neuron:UpdateStanceStrings()
--this allows for the "Esc" key to disable the Edit Mode instead of bringing up the game menu, but only if an edit mode is activated.
if not Neuron:IsHooked(GameMenuFrame, "OnUpdate") then
Neuron:HookScript(GameMenuFrame, "OnUpdate", function(self)
if Neuron.barEditMode then
HideUIPanel(self)
Neuron:ToggleBarEditMode(false)
end
if Neuron.buttonEditMode then
HideUIPanel(self)
Neuron:ToggleButtonEditMode(false)
end
if Neuron.bindingMode then
HideUIPanel(self)
Neuron:ToggleBindingMode(false)
end
end)
end
Neuron:LoginMessage()
--Load all bars and buttons
for _,v in pairs(Neuron.bars) do
v:Load()
end
--this is a hack for 10.0. They broke everything with regard to the way addons interface with
--SecureActionButtons see SecureTemplates.lua SecureActionButton_OnClick() for more information
SetCVar("ActionButtonUseKeyDown", 0)
Neuron.NeuronGUI:LoadInterfaceOptions()
end
--- **OnDisable**, which is only called when your addon is manually being disabled.
--- Unhook, Unregister Events, Hide frames that you created.
--- You would probably only use an OnDisable if you want to
--- build a "standby" mode, or be able to toggle modules on/off.
function Neuron:OnDisable()
SetCVar("ActionButtonUseKeyDown", 1)
end
-------------------------------------------------
function Neuron:PLAYER_REGEN_DISABLED()
if Neuron.buttonEditMode then
Neuron:ToggleButtonEditMode(false)
end
if Neuron.bindingMode then
Neuron:ToggleBindingMode(false)
end
if Neuron.barEditMode then
Neuron:ToggleBarEditMode(false)
end
end
function Neuron:PLAYER_ENTERING_WORLD()
DB.firstRun = false
Neuron:UpdateSpellCache()
Neuron:UpdateStanceStrings()
--Fix for Titan causing the Main Bar to not be hidden
if IsAddOnLoaded("Titan") then
TitanUtils_AddonAdjust("MainMenuBar", true)
end
Neuron:HideBlizzardUI(DB)
end
function Neuron:ACTIVE_TALENT_GROUP_CHANGED()
Neuron:UpdateSpellCache()
Neuron:UpdateStanceStrings()
end
function Neuron:LEARNED_SPELL_IN_TAB()
Neuron:UpdateSpellCache()
Neuron:UpdateStanceStrings()
end
function Neuron:CHARACTER_POINTS_CHANGED()
Neuron:UpdateSpellCache()
Neuron:UpdateStanceStrings()
end
function Neuron:SPELLS_CHANGED()
Neuron:UpdateSpellCache()
Neuron:UpdateStanceStrings()
end
-------------------------------------------------------------------------
--------------------Profiles---------------------------------------------
-------------------------------------------------------------------------
function Neuron:RefreshConfig(db, profile)
StaticPopup_Show("ReloadUI")
Neuron.pendingReload = true
end
-----------------------------------------------------------------
function Neuron:LoginMessage()
--displays a info window on login for either fresh installs or updates
if not DB.updateWarning or DB.updateWarning ~= LATEST_VERSION_NUM then
if not IsAddOnLoaded("Masque") then
print(" ")
print(" You do not currently have Masque installed or enabled.")
print(" Please consider using Masque for enhancing the visual appearance of Neuron's action buttons.")
print(" We recommend using Masque: Neuron, the theme made by Soyier for use with Neuron.")
print(" ")
end
end
DB.updateWarning = LATEST_VERSION_NUM
if Spec.active(true) > 4 then
print(" ")
Neuron:Print("Warning: You do not currently have a specialization selected. Changes to any buttons which have 'Multi Spec' set will not persist.")
print(" ")
end
--Shadowlands warning that will show as long as a player has one button on their ZoneAbilityBar for Shadowlands content
if Neuron.isWoWRetail and UnitLevel("player") >= 50 and Neuron.db.profile.ZoneAbilityBar[1] and #Neuron.db.profile.ZoneAbilityBar[1].buttons == 1 then
print(" ")
Neuron:Print(WrapTextInColorCode("IMPORTANT: Shadowlands content now requires multiple Zone Ability Buttons. Please add at least 3 buttons to your Zone Ability Bar to support this new functionality.", "FF00FFEC"))
print(" ")
end
end
--- Creates a table containing provided data
-- @param index, bookType, spellName, altName, spellID, altSpellID, spellType, icon
-- @return curSpell: Table containing provided data
function Neuron:SetSpellInfo(index, bookType, spellType, spellName, spellID, icon, altName, altSpellID, altIcon)
local curSpell = {}
curSpell.index = index
curSpell.booktype = bookType
curSpell.spellType = spellType
curSpell.spellName = spellName
curSpell.spellID = spellID
curSpell.icon = icon
curSpell.altName = altName
curSpell.altSpellID = altSpellID
curSpell.altIcon = altIcon
return curSpell
end
--- "()" indexes added because the Blizzard macro parser uses that to determine the difference of a spell versus a usable item if the two happen to have the same name.
--- I forgot this fact and removed using "()" and it made some macros not represent the right spell /sigh. This note is here so I do not forget again :P - Maul
--- Scans Character Spell Book and creates a table of all known spells. This table is used to refrence macro spell info to generate tooltips and cooldowns.
--- If a spell is not displaying its tooltip or cooldown, then the spell in the macro probably is not in the database
function Neuron:UpdateSpellCache()
local sIndexMax = 0
local numTabs = GetNumSpellTabs()
for i=1,numTabs do
local _, _, _, numSlots = GetSpellTabInfo(i)
sIndexMax = sIndexMax + numSlots
end
for i = 1,sIndexMax do
local spellName, _ = GetSpellBookItemName(i, BOOKTYPE_SPELL) --this returns the baseSpell name, even if it is augmented by talents. I.e. Roll and Chi Torpedo
local spellType, spellID = GetSpellBookItemInfo(i, BOOKTYPE_SPELL)
local isPassive
if spellName then
isPassive = IsPassiveSpell(i, BOOKTYPE_SPELL)
end
local icon = GetSpellTexture(spellID)
local altName
local altSpellID
local altIcon
if (spellName and spellType ~= "FUTURESPELL") and not isPassive then
altName, _, altIcon, _, _, _, altSpellID = GetSpellInfo(spellName)
if spellID == altSpellID then
altSpellID = nil
altName = nil
altIcon = nil
end
local spellData = Neuron:SetSpellInfo(i, BOOKTYPE_SPELL, spellType, spellName, spellID, icon, altName, altSpellID, altIcon)
Neuron.spellCache[(spellName):lower()] = spellData
Neuron.spellCache[(spellName):lower().."()"] = spellData
--reverse main and alt so we can put both in the table accurately
local altSpellData = Neuron:SetSpellInfo(i, BOOKTYPE_SPELL, spellType, altName, altSpellID, altIcon, spellName, spellID, icon)
if altName and altName ~= spellName then
Neuron.spellCache[(altName):lower()] = altSpellData
Neuron.spellCache[(altName):lower().."()"] = altSpellData
end
end
end
if Neuron.isWoWRetail then
for i = 1, select("#", GetProfessions()) do
local index = select(i, GetProfessions())
if index then
local _, _, _, _, numSpells, spelloffset = GetProfessionInfo(index)
for j=1,numSpells do
local offsetIndex = j + spelloffset
local spellName, _ = GetSpellBookItemName(offsetIndex, BOOKTYPE_PROFESSION)
local spellType, spellID = GetSpellBookItemInfo(offsetIndex, BOOKTYPE_PROFESSION)
local icon
if spellName and spellType ~= "FUTURESPELL" then
icon = GetSpellTexture(spellID)
local spellData = Neuron:SetSpellInfo(offsetIndex, BOOKTYPE_PROFESSION, spellType, spellName, spellID, icon,nil, nil, nil)
Neuron.spellCache[(spellName):lower()] = spellData
Neuron.spellCache[(spellName):lower().."()"] = spellData
end
end
end
end
end
end
function Neuron:ToggleMainMenu()
---need to run the command twice for some reason. The first one only seems to open the Interface panel
InterfaceOptionsFrame_OpenToCategory("Neuron");
InterfaceOptionsFrame_OpenToCategory("Neuron");
end
function Neuron:ToggleBarEditMode(show)
if show then
Neuron.barEditMode = true
Neuron:ToggleButtonEditMode(false)
Neuron:ToggleBindingMode(false)
for _, bar in pairs(Neuron.bars) do
bar.editFrame =
bar.editFrame or
BarEditor.allocate(bar, function(overlay, button, down)
overlay.bar:OnClick(button, down)
end)
bar:UpdateObjectVisibility(true)
bar:UpdateBarStatus(true)
bar:UpdateObjectStatus()
end
--if there is no bar selected, default to the first in the BarList
--TODO: This logic may be unintuitive. Should probably be fixed
if not Neuron.currentBar and #Neuron.bars then
Neuron.Bar.ChangeSelectedBar(Neuron.bars[1])
elseif Neuron.currentBar then
BarEditor.activate(Neuron.currentBar.editFrame)
end
else
Neuron.barEditMode = false
for _, bar in pairs(Neuron.bars) do
local overlay = bar.editFrame
bar.editFrame = nil
if overlay then
BarEditor.free(overlay)
end
bar:UpdateObjectVisibility()
bar:UpdateBarStatus()
bar:UpdateObjectStatus()
end
end
end
function Neuron:ToggleButtonEditMode(show)
local isActionBar = function(bar)
return bar and bar.class == "ActionBar"
end
local isStatusBar = function(bar)
return
bar and (
bar.class == "XPBar" or
bar.class == "RepBar" or
bar.class == "CastBar" or
bar.class == "MirrorBar"
)
end
local bars = Array.concatenate(
Array.filter(isActionBar, Neuron.bars),
Array.filter(isStatusBar, Neuron.bars)
)
if show then
Neuron.buttonEditMode = true
Neuron:ToggleBarEditMode(false)
Neuron:ToggleBindingMode(false)
local currentButton =
Neuron.currentButton or
(
(isActionBar(Neuron.currentBar) or isStatusBar(Neuron.currentBar))
and unpack(Neuron.currentBar.buttons)
) or
Array.foldl(
function(button, bar) return button or unpack(bar.buttons) end,
nil,
bars
)
if not currentButton then
Neuron.buttonEditMode = false
return
end
for _, bar in pairs(bars) do
for _, button in pairs(bar.buttons) do
button.editFrame = button.editFrame or ButtonEditor.allocate(
button,
isActionBar(bar) and "corners" or "sides",
function(btn)
Neuron.Button.ChangeSelectedButton(btn)
if addonTable.NeuronEditor then
Neuron.NeuronGUI:RefreshEditor()
end
end
)
end
bar:UpdateObjectVisibility(true)
bar:UpdateBarStatus(true)
bar:UpdateObjectStatus()
bar:UpdateObjectUsability()
end
-- change the button, but also manually activate it
-- just in case it was already the current button and
-- so if the change is a noop, we still show the recticle
Neuron.Button.ChangeSelectedButton(currentButton)
ButtonEditor.activate(currentButton.editFrame)
else
Neuron.buttonEditMode = false
for _, bar in pairs(bars) do
for _, button in pairs(bar.buttons) do
if button.editFrame then
ButtonEditor.free(button.editFrame)
button.editFrame = nil
end
end
bar:UpdateObjectVisibility()
bar:UpdateBarStatus()
bar:UpdateObjectStatus()
bar:UpdateObjectUsability()
end
end
end
--- Processes the change to a key bind
--- @param targetButton Button
--- @param key string @The key to be used
local function processKeyBinding(targetButton, key)
--if the button is locked, warn the user as to the locked status
if targetButton.keys and targetButton.keys.hotKeyLock then
UIErrorsFrame:AddMessage(L["Bindings_Locked_Notice"], 1.0, 1.0, 1.0, 1.0, UIERRORS_HOLD_TIME)
return
end
--if the key being pressed is escape, clear the bindings on the button
if key == "ESCAPE" then
ClearOverrideBindings(targetButton)
targetButton.keys.hotKeys = ":"
targetButton:ApplyBindings()
--if the key is anything else, keybind the button to this key
elseif key then --checks to see if another keybind already has that key, and if so clears it from the other button
--check to see if any other button has this key bound to it, ignoring locked buttons, and if so remove the key from the other button
for _, bar in pairs(Neuron.bars) do
for _, button in pairs(bar.buttons) do
if button.keys then
if targetButton ~= button and not button.keys.hotKeyLock then
button.keys.hotKeys:gsub("[^:]+", function(binding)
if key == binding then
local newkey = binding:gsub("%-", "%%-")
button.keys.hotKeys = button.keys.hotKeys:gsub(newkey..":", "")
button:ApplyBindings()
end
end)
end
end
end
end
--search the current hotKeys to see if our new key is missing, and if so add it
local found
targetButton.keys.hotKeys:gsub("[^:]+", function(binding)
if binding == key then
found = true
end
end)
if not found then
targetButton.keys.hotKeys = targetButton.keys.hotKeys..key..":"
end
targetButton:ApplyBindings()
end
end
function Neuron:ToggleBindingMode(show)
local isBindable = function(bar)
return bar and (
bar.class == "ActionBar" or
bar.class == "ExtraBar" or
bar.class == "ZoneAbilityBar" or
bar.class == "PetBar"
)
end
local bars = Array.filter(isBindable, Neuron.bars)
if show then
Neuron.bindingMode = true
Neuron:ToggleButtonEditMode(false)
Neuron:ToggleBarEditMode(false)
for _, bar in pairs(bars) do
for _, button in pairs(bar.buttons) do
button.keybindFrame = button.keybindFrame or ButtonBinder.allocate(button, processKeyBinding)
end
bar:UpdateObjectVisibility(true)
bar:UpdateBarStatus(true)
bar:UpdateObjectStatus()
bar:UpdateObjectUsability()
end
else
Neuron.bindingMode = false
for _, bar in pairs(bars) do
for _, button in pairs(bar.buttons) do
if button.keybindFrame then
ButtonBinder.free(button.keybindFrame)
button.keybindFrame = nil
end
end
bar:UpdateObjectVisibility()
bar:UpdateBarStatus()
bar:UpdateObjectStatus()
bar:UpdateObjectUsability()
end
end
end
function Neuron:GetSerializedAndCompressedProfile()
local uncompressed = Neuron:Serialize(Neuron.db.profile) --serialize the database into a string value
local compressed = LibDeflate:CompressZlib(uncompressed) --compress the data
local encoded = LibDeflate:EncodeForPrint(compressed) --encode the data for print for copy+paste
return encoded
end
function Neuron:SetSerializedAndCompressedProfile(input)
--check if the input is empty
if input == "" then
Neuron:Print(L["No data to import."].." "..L["Aborting."])
return
end
--decode and check if decoding worked properly
local decoded = LibDeflate:DecodeForPrint(input)
if decoded == nil then
Neuron:Print(L["Decoding failed."].." "..L["Aborting."])
return
end
--uncompress and check if uncompresion worked properly
local uncompressed = LibDeflate:DecompressZlib(decoded)
if uncompressed == nil then
Neuron:Print(L["Decompression failed."].." "..L["Aborting."])
return
end
--deserialize the data and return it back into a table format
local result, newProfile = Neuron:Deserialize(uncompressed)
if result == true and newProfile then --if we successfully deserialize, load the new table and reload
for k,v in pairs(newProfile) do
if type(v) == "table" then
Neuron.db.profile[k] = CopyTable(v)
else
Neuron.db.profile[k] = v
end
end
ReloadUI()
else
Neuron:Print(L["Data import Failed."].." "..L["Aborting."])
end
end