-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInit.lua
More file actions
324 lines (282 loc) · 9.82 KB
/
Init.lua
File metadata and controls
324 lines (282 loc) · 9.82 KB
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
----------------------------------------------------
-- CooldownCursor: Init
-- Event handler and event registration
----------------------------------------------------
local addonName, addonTable = ...
local C = addonTable.Constants
local State = addonTable.State
local Internal = addonTable.Internal
local ShowSpellIcon = Internal.ShowSpellIcon
local ApplyShowBehavior = Internal.ApplyShowBehavior
local RemoveIconForSpell = Internal.RemoveIconForSpell
local UpdateChargeCount = Internal.UpdateChargeCount
local RefreshScreenMetrics = Internal.RefreshScreenMetrics
local ApplyPositionMode = Internal.ApplyPositionMode
local IsProcOverlayEnabled = Internal.IsProcOverlayEnabled
local IsProcOutlineEnabled = Internal.IsProcOutlineEnabled
local SHOW_WHEN_STATE = C.SHOW_WHEN_STATE
local SHOW_BEHAVIOR = C.SHOW_BEHAVIOR
local CooldownCursor = addonTable.Frame
----------------------------------------------------
-- Show-when state helper
----------------------------------------------------
-- Returns true if the current combat state does NOT match showWhen,
-- meaning events should be suppressed. Handles combat transitions
-- by hiding icons when leaving the allowed state.
local function CheckShowWhenState(combatTransition)
local showWhen = CooldownCursorDB.global.showWhen
if not showWhen or showWhen == SHOW_WHEN_STATE.ALWAYS then
return false
end
if showWhen == SHOW_WHEN_STATE.COMBAT then
if not State.inCombat then
-- Leaving combat with COMBAT mode: hide icons
if combatTransition then
CooldownCursor:HideIconNow()
end
return true
end
elseif showWhen == SHOW_WHEN_STATE.NON_COMBAT then
if State.inCombat then
-- Entering combat with NON_COMBAT mode: hide icons
if combatTransition then
CooldownCursor:HideIconNow()
end
return true
end
end
return false
end
----------------------------------------------------
-- Mounted state helper
----------------------------------------------------
local wasMounted = false
local function CheckMountedState()
if not CooldownCursorDB.global.hideWhileMounted then return false end
local mounted = IsMounted()
if mounted then
if not wasMounted then
wasMounted = true
CooldownCursor:HideAllIcons(true)
end
return true -- signal: skip further processing
end
if wasMounted then
wasMounted = false
if not CheckShowWhenState() then
ApplyShowBehavior()
end
end
return false
end
----------------------------------------------------
-- Guard helper
-- Returns true if the event should be suppressed
-- due to combat/show-when state or mounted state.
----------------------------------------------------
local function IsGuarded()
return CheckShowWhenState() or CheckMountedState()
end
----------------------------------------------------
-- Spell event debounce + processing
----------------------------------------------------
local pendingSpellTimers = {}
local function ProcessSpellEvent(spellID, hasCharges)
if pendingSpellTimers[spellID] then return end
pendingSpellTimers[spellID] = true
-- Delay slightly to allow cooldown to register
C_Timer.After(0.01, function()
pendingSpellTimers[spellID] = nil
local durationObj = C_Spell.GetSpellCooldownDuration(spellID)
if not durationObj then return end
local inRange = C_Spell.IsSpellInRange(spellID)
if inRange == false and
CooldownCursorDB.global.showBehavior ~= SHOW_BEHAVIOR.OFF_COOLDOWN then
return
end
if hasCharges then
local iconFrame = ShowSpellIcon(spellID, durationObj)
UpdateChargeCount(iconFrame, spellID)
else
ShowSpellIcon(spellID, durationObj)
end
ApplyShowBehavior()
end)
end
----------------------------------------------------
-- Shared handler for spell cast / cooldown events
-- Covers: UNIT_SPELLCAST_SENT, UNIT_SPELLCAST_SUCCEEDED,
-- UNIT_SPELLCAST_FAILED, SPELL_UPDATE_COOLDOWN
----------------------------------------------------
local SPELL_CAST_EVENTS = {
UNIT_SPELLCAST_FAILED = true,
UNIT_SPELLCAST_SENT = true,
UNIT_SPELLCAST_SUCCEEDED = true,
SPELL_UPDATE_COOLDOWN = true,
}
local function HandleSpellCastEvent(self, event, ...)
if CooldownCursorDB.global.enabled == false then return end
if IsGuarded() then return end
local spellID
if event == "SPELL_UPDATE_COOLDOWN" then
-- provides spellID, baseSpellID, category, startRecoveryCategory
spellID = ...
elseif event == "UNIT_SPELLCAST_SENT" then
local unit, _, _, sid = ...
if unit ~= "player" then return end
spellID = sid
else
local unit, _, sid = ...
if unit ~= "player" then return end
spellID = sid
end
if not spellID then
ApplyShowBehavior()
return
end
local show, rule = self:GetSpellRule(spellID)
if not show then return end
local hasCharges = rule and rule.metadata and rule.metadata.hasCharges or false
ProcessSpellEvent(spellID, hasCharges)
end
----------------------------------------------------
-- Event handler dispatch table
----------------------------------------------------
local eventHandlers = {}
eventHandlers.ADDON_LOADED = function(self, name)
if name ~= addonName then return end
self:ApplyDefaults()
RefreshScreenMetrics()
self:InitMultiIconSystem()
self:UpdateDisplay()
if ApplyPositionMode then ApplyPositionMode() end
self:InitAce3Options()
self:UnregisterEvent("ADDON_LOADED")
State.inCombat = InCombatLockdown()
end
eventHandlers.PLAYER_SPECIALIZATION_CHANGED = function(self, unit)
if unit ~= "player" then return end
wipe(State.knownSpellCache)
self:InvalidateSpellBookCache()
self:HideAllIcons(true)
ApplyShowBehavior()
end
eventHandlers.PLAYER_MOUNT_DISPLAY_CHANGED = function(_)
CheckMountedState()
end
eventHandlers.PLAYER_REGEN_DISABLED = function(_)
State.inCombat = true
if not CheckShowWhenState(true) then
ApplyShowBehavior()
end
end
eventHandlers.PLAYER_REGEN_ENABLED = function(_)
State.inCombat = false
if not CheckShowWhenState(true) then
ApplyShowBehavior()
end
end
eventHandlers.UI_SCALE_CHANGED = function(_)
RefreshScreenMetrics()
end
eventHandlers.DISPLAY_SIZE_CHANGED = eventHandlers.UI_SCALE_CHANGED
eventHandlers.SPELL_UPDATE_USABLE = function(_)
if IsGuarded() then return end
ApplyShowBehavior()
end
eventHandlers.PLAYER_ENTERING_WORLD = function(_)
if IsGuarded() then return end
ApplyShowBehavior()
end
eventHandlers.SPELL_UPDATE_CHARGES = function(_)
if IsGuarded() then return end
if CooldownCursorDB.global.showCharges == false then return end
for spellID, iconData in pairs(State.activeIcons) do
if iconData and iconData.iconFrame and iconData.hasCharges then
UpdateChargeCount(iconData.iconFrame, spellID)
end
end
ApplyShowBehavior()
end
eventHandlers.SPELL_ACTIVATION_OVERLAY_GLOW_SHOW = function(self, spellID)
if not spellID then return end
if CooldownCursorDB.global.showProcs == false then return end
if IsGuarded() then return end
local show = self:GetSpellRule(spellID)
if not show then return end
State.activeProcSpells[spellID] = true
State.procCapableSpells[spellID] = true
local hadIcon = State.activeIcons[spellID] ~= nil
local durationObj = C_Spell.GetSpellCooldownDuration(spellID)
local iconFrame, iconData = ShowSpellIcon(spellID, durationObj, true)
if iconFrame and iconData then
iconData.procActive = true
iconFrame.procActive = true
if not hadIcon then
iconData.procOnly = true
iconFrame.procOnly = true
end
if iconFrame.procOverlay then
if IsProcOverlayEnabled() then
iconFrame.procOverlay:Show()
else
iconFrame.procOverlay:Hide()
end
end
if iconFrame.procOutline and IsProcOutlineEnabled() then
iconFrame.procOutline:Show()
end
end
end
eventHandlers.SPELL_ACTIVATION_OVERLAY_GLOW_HIDE = function(_, spellID)
if not spellID then return end
if CooldownCursorDB.global.showProcs == false then return end
State.activeProcSpells[spellID] = nil
local iconData = State.activeIcons[spellID]
if not (iconData and iconData.iconFrame) then return end
local iconFrame = iconData.iconFrame
iconData.procActive = false
iconFrame.procActive = false
if iconFrame.procOverlay then iconFrame.procOverlay:Hide() end
if iconFrame.procOutline then iconFrame.procOutline:Hide() end
if iconData.procOnly then
RemoveIconForSpell(spellID, true)
end
end
-- Wire the four spell cast/cooldown events to the shared handler
for eventName in pairs(SPELL_CAST_EVENTS) do
eventHandlers[eventName] = function(self, ...)
HandleSpellCastEvent(self, eventName, ...)
end
end
----------------------------------------------------
-- Event dispatcher
----------------------------------------------------
CooldownCursor:SetScript("OnEvent", function(self, event, ...)
if Internal.LogEvent then
Internal.LogEvent(event, ...)
end
local handler = eventHandlers[event]
if handler then
handler(self, ...)
end
end)
----------------------------------------------------
-- Register events
----------------------------------------------------
CooldownCursor:RegisterEvent("ADDON_LOADED")
CooldownCursor:RegisterEvent("UNIT_SPELLCAST_SENT")
CooldownCursor:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
CooldownCursor:RegisterEvent("SPELL_UPDATE_COOLDOWN")
CooldownCursor:RegisterEvent("SPELL_UPDATE_USABLE")
CooldownCursor:RegisterEvent("UNIT_SPELLCAST_FAILED")
CooldownCursor:RegisterEvent("PLAYER_REGEN_DISABLED")
CooldownCursor:RegisterEvent("PLAYER_REGEN_ENABLED")
CooldownCursor:RegisterEvent("SPELL_ACTIVATION_OVERLAY_GLOW_SHOW")
CooldownCursor:RegisterEvent("SPELL_ACTIVATION_OVERLAY_GLOW_HIDE")
CooldownCursor:RegisterEvent("SPELL_UPDATE_CHARGES")
CooldownCursor:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
CooldownCursor:RegisterEvent("UI_SCALE_CHANGED")
CooldownCursor:RegisterEvent("DISPLAY_SIZE_CHANGED")
CooldownCursor:RegisterEvent("PLAYER_MOUNT_DISPLAY_CHANGED")
CooldownCursor:RegisterEvent("PLAYER_ENTERING_WORLD")