This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cooldown.lua
284 lines (225 loc) · 6.65 KB
/
cooldown.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
-- hooks for watching cooldown events
local _, Addon = ...
local GCD_SPELL_ID = 61304
local COOLDOWN_TYPE_LOSS_OF_CONTROL = COOLDOWN_TYPE_LOSS_OF_CONTROL
local GetSpellCooldown = GetSpellCooldown
local GetTime = GetTime
local cooldowns = {}
local Cooldown = {}
-- queries
function Cooldown:CanShow()
if self.noCooldownCount then
return false
end
local start, duration = self._tcc_start, self._tcc_duration
-- no active cooldown
if start <= 0 or duration <= 0 then
return false
end
-- ensure we have settings for this cooldown
local settings = Addon.Config
-- at least min duration
if duration < settings.minDuration then
return false
end
-- hide text if we don't want to display it for this kind of cooldown
if settings.enableCooldownStyles and not settings.cooldownStyles[self._tcc_kind or 'default'].text then
return false
end
local t = GetTime()
-- expired cooldowns
if (start + duration) <= t then
return false
end
-- future cooldowns that don't start for at least a day
-- these are probably buggy ones
if (start - t) > 86400 then
return false
end
-- filter GCD
local gcdStart, gcdDuration = GetSpellCooldown(GCD_SPELL_ID)
if start == gcdStart and duration == gcdDuration then
return false
end
return true
end
function Cooldown:GetKind()
if self.currentCooldownType == COOLDOWN_TYPE_LOSS_OF_CONTROL then
return 'loc'
end
local parent = self:GetParent()
if parent and parent.chargeCooldown == self then
return 'charge'
end
return 'default'
end
function Cooldown:GetPriority()
if self._tcc_kind == 'charge' then
return 2
end
return 1
end
-- actions
function Cooldown:Initialize()
if cooldowns[self] then
return
end
cooldowns[self] = true
self:HookScript('OnShow', Cooldown.OnVisibilityUpdated)
self:HookScript('OnHide', Cooldown.OnVisibilityUpdated)
-- this is a hack to make sure that text for charge cooldowns can appear
-- above the charge cooldown itself, as charge cooldowns have a TOOLTIP
-- frame level
if Addon.Config.enableCooldownStyles then
local parent = self:GetParent()
if parent and parent.chargeCooldown == self then
local cooldown = parent.cooldown
if cooldown then
self:SetFrameStrata(cooldown:GetFrameStrata())
self:SetFrameLevel(cooldown:GetFrameLevel() + 7)
end
end
end
end
function Cooldown:ShowText()
local oldDisplay = self._tcc_display
local newDisplay = Addon.Display:GetOrCreate(self:GetParent() or self)
if oldDisplay ~= newDisplay then
self._tcc_display = newDisplay
if oldDisplay then
oldDisplay:RemoveCooldown(self)
end
end
if newDisplay then
newDisplay:AddCooldown(self)
end
end
function Cooldown:HideText()
local display = self._tcc_display
if display then
display:RemoveCooldown(self)
self._tcc_display = nil
end
end
function Cooldown:UpdateText()
if self._tcc_show and self:IsVisible() then
Cooldown.ShowText(self)
else
Cooldown.HideText(self)
end
end
function Cooldown:UpdateStyle()
local style = Addon.Config.cooldownStyles[self._tcc_kind or 'default']
local drawSwipe = style.swipe
if drawSwipe ~= 'default' then
self:SetDrawSwipe(drawSwipe)
end
local drawEdge = style.edge
if drawEdge ~= 'default' then
self:SetDrawEdge(drawEdge)
end
local drawBling = style.bling
if drawBling ~= 'default' then
self:SetDrawBling(drawBling)
end
end
do
local pending = {}
local updater = CreateFrame('Frame')
updater:Hide()
updater:SetScript(
'OnUpdate',
function(self)
if Addon.Config.enableCooldownStyles then
for cooldown in pairs(pending) do
Cooldown.UpdateStyle(cooldown)
Cooldown.UpdateText(cooldown)
pending[cooldown] = nil
end
else
for cooldown in pairs(pending) do
Cooldown.UpdateText(cooldown)
pending[cooldown] = nil
end
end
self:Hide()
end
)
function Cooldown:RequestUpdate()
if not pending[self] then
pending[self] = true
updater:Show()
end
end
end
function Cooldown:Refresh(force)
local start, duration = self:GetCooldownTimes()
start = (start or 0) / 1000
duration = (duration or 0) / 1000
if force then
self._tcc_start = nil
self._tcc_duration = nil
end
Cooldown.Initialize(self)
Cooldown.SetTimer(self, start, duration)
end
function Cooldown:SetTimer(start, duration)
if self._tcc_start == start and self._tcc_duration == duration then
return
end
self._tcc_start = start
self._tcc_duration = duration
self._tcc_kind = Cooldown.GetKind(self)
self._tcc_priority = Cooldown.GetPriority(self)
self._tcc_show = Cooldown.CanShow(self)
Cooldown.RequestUpdate(self)
end
function Cooldown:SetNoCooldownCount(disable, owner)
owner = owner or true
if disable then
if not self.noCooldownCount then
self.noCooldownCount = owner
Cooldown.Refresh(self, true)
end
elseif self.noCooldownCount == owner then
self.noCooldownCount = nil
Cooldown.Refresh(self, true)
end
end
-- events
function Cooldown:OnSetCooldown(start, duration)
if self.noCooldownCount or self:IsForbidden() then
return
end
start = start or 0
duration = duration or 0
Cooldown.Initialize(self)
Cooldown.SetTimer(self, start, duration)
end
function Cooldown:OnSetCooldownDuration()
if self.noCooldownCount or self:IsForbidden() then
return
end
Cooldown.Refresh(self)
end
function Cooldown:SetDisplayAsPercentage()
if self.noCooldownCount or self:IsForbidden() then
return
end
Cooldown.SetNoCooldownCount(self, true)
end
function Cooldown:OnVisibilityUpdated()
if self.noCooldownCount or self:IsForbidden() then
return
end
Cooldown.RequestUpdate(self)
end
-- misc
function Cooldown:SetupHooks()
local cooldown_mt = getmetatable(ActionButton1Cooldown).__index
hooksecurefunc(cooldown_mt, 'SetCooldown', Cooldown.OnSetCooldown)
hooksecurefunc(cooldown_mt, 'SetCooldownDuration', Cooldown.OnSetCooldownDuration)
hooksecurefunc('CooldownFrame_SetDisplayAsPercentage', Cooldown.SetDisplayAsPercentage)
end
-- exports
Addon.Cooldown = Cooldown