Repository navigation
Expand file tree
/
Copy pathChargeTracking.lua
More file actions
414 lines (361 loc) · 13.7 KB
/
Copy pathChargeTracking.lua
File metadata and controls
414 lines (361 loc) · 13.7 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
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
setfenv(1, _G.SlackHacks)
local module = Self:NewModule("ChargeTracking", "AceEvent-3.0")
Self.ChargeTracking = module
local HOLY_SHOCK_SPELL_ID = 20473
local CHARGE_ICON_SIZE = 36
local container
local isEditing = false
local selection
local trackedCharges
local maxCharges = 2 -- static fact about Holy Shock; used whenever the real value isn't safely readable
local rechargeDuration -- cached seconds; refreshed opportunistically whenever it's a clean number
local rechargeTimer
local function isSecret(value)
return _G.issecretvalue and _G.issecretvalue(value) or false
end
local function isCleanNumber(value)
return not isSecret(value) and type(value) == "number"
end
local function stopRechargeTimer()
if rechargeTimer then
rechargeTimer:Cancel()
rechargeTimer = nil
end
end
local function createChargeTrackingFrame()
if container then return end
container = CreateFrame("Frame", "SlackHacksHolyShockChargeTracker", UIParent)
container:SetSize(CHARGE_ICON_SIZE, CHARGE_ICON_SIZE)
container:SetMovable(true)
container:SetClampedToScreen(true)
container:SetDontSavePosition(true)
container:Hide()
-- The TotemFrame ring art bakes its thickness in as a fixed ~13% of its own size, so scaling it never
-- makes it visually thinner -- the hole shrinks right along with the outer edge. A plain color clipped
-- by the same circular mask has no such ratio, so its thickness is just however much bigger it is than
-- the icon, giving direct control over how thin the ring appears.
local border = container:CreateTexture(nil, "BACKGROUND")
border:SetPoint("CENTER")
border:SetSize(CHARGE_ICON_SIZE + 2, CHARGE_ICON_SIZE + 2)
border:SetColorTexture(1, 0.82, 0)
local borderMask = container:CreateMaskTexture(nil, "BACKGROUND")
borderMask:SetAllPoints(border)
borderMask:SetAtlas("CircleMaskScalable", false)
border:AddMaskTexture(borderMask)
local icon = container:CreateTexture(nil, "BORDER")
icon:SetAllPoints(container)
icon:SetTexture(C_Spell.GetSpellTexture(HOLY_SHOCK_SPELL_ID))
local iconMask = container:CreateMaskTexture(nil, "BORDER")
iconMask:SetAllPoints(icon)
iconMask:SetAtlas("CircleMaskScalable", false)
icon:AddMaskTexture(iconMask)
local cooldown = CreateFrame("Cooldown", nil, container, "CooldownFrameTemplate")
cooldown:SetAllPoints(container)
cooldown:SetHideCountdownNumbers(false)
cooldown:SetDrawBling(false)
cooldown:SetDrawEdge(true)
-- Same corner/font convention Blizzard action buttons use for their charge count text.
local count = container:CreateFontString(nil, "OVERLAY", "NumberFontNormal")
count:SetPoint("BOTTOMRIGHT", container, "BOTTOMRIGHT", -2, 2)
container.icon = icon
container.cooldown = cooldown
container.border = border
container.count = count
end
local function getPaladinDb()
if not db.profile.combat then db.profile.combat = {} end
if not db.profile.combat.paladin then db.profile.combat.paladin = {} end
return db.profile.combat.paladin
end
local function updatePosition()
if not container then return end
local p = getPaladinDb()
local point = p.point or "CENTER"
local relativePoint = p.relativePoint or "CENTER"
local x = p.x or 0
local y = p.y or 0
container:ClearAllPoints()
container:SetPoint(point, UIParent, relativePoint, x, y)
end
local function onDragStart()
if InCombatLockdown() then return end
if container then
container:StartMoving()
end
end
local function onDragStop()
if not container then return end
container:StopMovingOrSizing()
if InCombatLockdown() then return end
local point, relativeTo, relativePoint, x, y = container:GetPoint(1)
if point then
local p = getPaladinDb()
p.point = point
p.relativePoint = relativePoint or point
p.x = math.floor(x + 0.5)
p.y = math.floor(y + 0.5)
end
updatePosition()
end
local function createSelection()
if selection then return selection end
if not container then createChargeTrackingFrame() end
local success, sel = pcall(CreateFrame, "Frame", "SlackHacksHolyShockEditModeSelection", container, "EditModeSystemSelectionTemplate")
if success and sel then
selection = sel
selection:SetAllPoints(container)
selection:SetFrameStrata(container:GetFrameStrata())
selection:SetFrameLevel(container:GetFrameLevel() + 20)
selection:EnableMouse(true)
selection:RegisterForDrag("LeftButton")
selection:SetScript("OnDragStart", onDragStart)
selection:SetScript("OnDragStop", onDragStop)
selection.system = {
GetSystemName = function()
return "Holy Shock Charges"
end
}
if selection.SetSelectionText then
selection:SetSelectionText("Holy Shock Charges")
elseif selection.Label then
selection.Label:SetText("Holy Shock Charges")
end
selection:SetScript("OnMouseDown", function(self)
if InCombatLockdown() then return end
if EditModeManagerFrame and EditModeManagerFrame.ClearSelectedSystem then
EditModeManagerFrame:ClearSelectedSystem()
end
if self.ShowSelected then
self:ShowSelected(true)
end
end)
else
local f = CreateFrame("Frame", "SlackHacksHolyShockEditModeSelection", container, "BackdropTemplate")
f:SetAllPoints(container)
f:SetFrameStrata(container:GetFrameStrata())
f:SetFrameLevel(container:GetFrameLevel() + 20)
f:EnableMouse(true)
f:RegisterForDrag("LeftButton")
f:SetBackdrop({
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
edgeSize = 12,
insets = { left = 2, right = 2, top = 2, bottom = 2 }
})
f:SetBackdropBorderColor(0.2, 0.6, 1.0, 0.8)
local label = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
label:SetPoint("CENTER", f, "CENTER", 0, 0)
label:SetText("Holy Shock Charges")
f.Label = label
f.ShowHighlighted = function(self)
self:SetBackdropBorderColor(0.2, 0.6, 1.0, 0.8)
self:Show()
end
f.ShowSelected = function(self)
self:SetBackdropBorderColor(1.0, 0.82, 0.0, 1.0)
self:Show()
end
f:SetScript("OnDragStart", onDragStart)
f:SetScript("OnDragStop", onDragStop)
f:SetScript("OnMouseDown", function(self)
self:ShowSelected()
end)
selection = f
end
return selection
end
local function shouldTrack(inCombat)
if inCombat == nil then
inCombat = InCombatLockdown()
end
local p = getPaladinDb()
return isRetail() and getClassName() == "PALADIN" and getSpecName() == "HOLY" and p.trackHolyShockCharges and (not p.onlyInCombat or inCombat)
end
local function enterEditMode()
if isEditing or InCombatLockdown() then return end
isEditing = true
module:Refresh()
end
local function exitEditMode()
if not isEditing then return end
isEditing = false
if selection then
selection:Hide()
end
module:Refresh()
end
function module:RefreshDisplay(chargeInfo)
if isEditing then return end
if not container or not container:IsShown() or trackedCharges == nil then return end
chargeInfo = chargeInfo or C_Spell.GetSpellCharges(HOLY_SHOCK_SPELL_ID)
if not chargeInfo then return end
-- trackedCharges is our own locally-simulated number (never read from a secret field), so it's always
-- safe to display directly -- unlike currentCharges, which can be secret and isn't safe to format/compare.
container.count:SetText(trackedCharges)
if trackedCharges >= maxCharges then
container.cooldown:Clear()
container.icon:SetDesaturated(false)
container.icon:SetAlpha(1)
else
-- The real timing fields may be secret; SetCooldown is a sanctioned direct sink for them either way.
container.cooldown:SetCooldown(chargeInfo.cooldownStartTime, chargeInfo.cooldownDuration, chargeInfo.chargeModRate)
container.icon:SetDesaturated(trackedCharges == 0)
container.icon:SetAlpha(trackedCharges == 0 and 0.55 or 1)
end
end
local function startRechargeTimer()
if rechargeTimer or not rechargeDuration or trackedCharges == nil or trackedCharges >= maxCharges then return end
rechargeTimer = C_Timer.NewTimer(rechargeDuration, function()
rechargeTimer = nil
trackedCharges = math.min((trackedCharges or 0) + 1, maxCharges)
module:RefreshDisplay()
startRechargeTimer() -- only one charge recharges at a time; chain if still below max
end)
end
-- Learns the static facts (max charges, recharge duration) whenever they happen to be clean, and
-- resyncs our local simulation to the real charge count/remaining time whenever THAT is clean too.
-- Neither is guaranteed to be readable in combat, so this is opportunistic, not combat-gated.
function module:UpdateHolyShockCharges()
if isEditing then return end
if not container or not container:IsShown() then return end
local chargeInfo = C_Spell.GetSpellCharges(HOLY_SHOCK_SPELL_ID)
if not chargeInfo then return end
if isCleanNumber(chargeInfo.maxCharges) and chargeInfo.maxCharges > 0 then
maxCharges = chargeInfo.maxCharges
end
if isCleanNumber(chargeInfo.cooldownDuration) and chargeInfo.cooldownDuration > 0 then
rechargeDuration = chargeInfo.cooldownDuration
end
if isCleanNumber(chargeInfo.currentCharges) then
trackedCharges = chargeInfo.currentCharges
stopRechargeTimer()
if trackedCharges < maxCharges then
if isCleanNumber(chargeInfo.cooldownStartTime) and isCleanNumber(chargeInfo.cooldownDuration) then
local remaining = (chargeInfo.cooldownStartTime + chargeInfo.cooldownDuration) - GetTime()
if remaining > 0 then
rechargeTimer = C_Timer.NewTimer(remaining, function()
rechargeTimer = nil
trackedCharges = math.min((trackedCharges or 0) + 1, maxCharges)
module:RefreshDisplay()
startRechargeTimer()
end)
end
else
startRechargeTimer()
end
end
elseif trackedCharges == nil then
trackedCharges = maxCharges -- best guess until either a real read or a cast tells us otherwise
end
self:RefreshDisplay(chargeInfo)
end
function module:OnHolyShockCast(_, unitTarget, _, spellID)
if isEditing then return end
if unitTarget ~= "player" or spellID ~= HOLY_SHOCK_SPELL_ID then return end
if trackedCharges == nil then trackedCharges = maxCharges end
trackedCharges = math.max(trackedCharges - 1, 0)
startRechargeTimer() -- no-op if a recharge is already in progress (only one runs at a time)
self:RefreshDisplay()
end
local function updateVisibility(inCombat)
if not container then return end
if isEditing then
local p = getPaladinDb()
local isHolyPaladin = isRetail() and getClassName() == "PALADIN" and getSpecName() == "HOLY" and p.trackHolyShockCharges
if isHolyPaladin then
updatePosition()
container:Show()
container.count:SetText(maxCharges)
container.cooldown:Clear()
container.icon:SetDesaturated(false)
container.icon:SetAlpha(1)
local sel = createSelection()
if sel then
sel:Show()
if sel.ShowHighlighted then
sel:ShowHighlighted()
end
end
else
if selection then selection:Hide() end
container:Hide()
end
return
end
if shouldTrack(inCombat) then
if selection then selection:Hide() end
updatePosition()
container:Show()
module:UpdateHolyShockCharges()
else
if selection then selection:Hide() end
container:Hide()
stopRechargeTimer()
end
end
function module:Refresh(inCombat)
createChargeTrackingFrame()
stopRechargeTimer()
trackedCharges = nil -- resync from scratch
updateVisibility(inCombat)
end
function module:OnDatabaseReset()
isEditing = false
if selection then selection:Hide() end
self:Refresh()
end
function module:PLAYER_REGEN_DISABLED()
if isEditing then
exitEditMode()
end
updateVisibility(true)
end
function module:PLAYER_REGEN_ENABLED()
updateVisibility(false)
end
function module:OnInitialize()
createChargeTrackingFrame()
db:RegisterCallback("OnDatabaseReset", module.OnDatabaseReset, module)
if EventRegistry and EventRegistry.RegisterCallback then
EventRegistry:RegisterCallback("EditMode.Enter", enterEditMode, module)
EventRegistry:RegisterCallback("EditMode.Exit", exitEditMode, module)
end
if EditModeManagerFrame then
hooksecurefunc(EditModeManagerFrame, "EnterEditMode", enterEditMode)
hooksecurefunc(EditModeManagerFrame, "ExitEditMode", exitEditMode)
if EditModeManagerFrame.SelectSystem then
hooksecurefunc(EditModeManagerFrame, "SelectSystem", function()
if selection and selection.ShowHighlighted then
selection:ShowHighlighted()
end
end)
end
end
if EditModeSystemSettingsDialog then
EditModeSystemSettingsDialog:HookScript("OnShow", function()
if selection and selection.ShowHighlighted then
selection:ShowHighlighted()
end
end)
end
end
function module:OnEnable()
self:RegisterEvent("PLAYER_ENTERING_WORLD", "Refresh")
self:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED", "Refresh")
self:RegisterEvent("SPELL_UPDATE_CHARGES", "UpdateHolyShockCharges")
self:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED", "OnHolyShockCast")
self:RegisterEvent("PLAYER_REGEN_DISABLED")
self:RegisterEvent("PLAYER_REGEN_ENABLED")
if EditModeManagerFrame and EditModeManagerFrame.IsEditModeActive and EditModeManagerFrame:IsEditModeActive() then
enterEditMode()
else
self:Refresh()
end
end
function module:OnDisable()
self:UnregisterAllEvents()
stopRechargeTimer()
if isEditing then
exitEditMode()
end
if container then container:Hide() end
end