-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoUF_SwingTimer.lua
360 lines (285 loc) · 11.3 KB
/
oUF_SwingTimer.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
--[[
# Element: SwingTimer
Handles the visibility and updating of two status bar that tracks main-hand and off-hand swing timing.
## Widgets
SwingTimer - A `Frame` to hold a `Button`s representing debuffs.
## Sub-Widgets
MainHand - A `StatusBar` to represent mian-hand weapon swing.
OffHand - A `StatusBar` to represent off-hand weapon swing.
## Example
local element = CreateFrame("Frame", nil, frame)
element:SetPoint("BOTTOM", UIParent, "BOTTOM", 0, 200)
element:SetSize(200, 45)
do
local statusbar = CreateFrame("StatusBar", nil, element)
statusbar:SetSize(200, 20)
statusbar:SetPoint("TOP", element, "TOP", 0, 0)
statusbar:SetStatusBarTexture(texture)
statusbar:SetStatusBarColor(0.8, 0.4, 0.4)
statusbar:CreateBackdrop()
local bg = statusbar:CreateTexture(nil, "BACKGROUND")
bg:SetAllPoints(statusbar)
bg:SetTexture(texture)
bg.multiplier = C.general.background.multiplier or 0.15
statusbar.bg = bg
local text = statusbar:CreateFontString(nil, "OVERLAY")
text:SetPoint("RIGHT", statusbar, "RIGHT", -5, 0)
text:SetFontObject(fontObject)
text:SetText("0.0s")
statusbar.Text = text
element.MainHand = statusbar
end
do
local statusbar = CreateFrame("StatusBar", nil, element)
statusbar:SetPoint("TOP", element.MainHand or element, "BOTTOM", 0, -5)
statusbar:SetSize(200, 20)
statusbar:SetStatusBarTexture(texture)
statusbar:SetStatusBarColor(0.8, 0.4, 0.4)
statusbar:CreateBackdrop()
local bg = statusbar:CreateTexture(nil, "BACKGROUND")
bg:SetAllPoints(statusbar)
bg:SetTexture(texture)
bg.multiplier = C.general.background.multiplier or 0.15
statusbar.bg = bg
local text = statusbar:CreateFontString(nil, "OVERLAY")
text:SetPoint("RIGHT", statusbar, "RIGHT", -5, 0)
text:SetFontObject(fontObject)
text:SetText("0.0s")
statusbar.Text = text
element.OffHand = statusbar
end
-- register with oUF
self.SwingTimer = element
--]]
local _, ns = ...
local oUF = ns.oUF
assert(oUF, "oUF_SwingTimer was unable to locate oUF install.")
local COLORS = {
["main-hand"] = oUF:CreateColor(0.80, 0.30, 0.10),
["off-hand"] = oUF:CreateColor(0.10, 0.10, 0.80)
}
local COMBAT_EVENTS = {
["SWING_DAMAGE"] = true,
["RANGE_DAMAGE"] = true,
["SWING_MISSED"] = true,
["RANGE_MISSED"] = true,
["SPELL_CAST_SUCCESS"] = true,
}
local SWING_RESET_SPELLS = {
[78] = true, -- Heroic Strike (Rank 1)
[284] = true, -- Heroic Strike (Rank 2)
-- Add more spell IDs as needed
}
local function UpdateColor(self, arg, ...)
local element = self.SwingTimer
local color = COLORS[arg or "none"]
local frame = (arg == "main-hand") and element.MainHand or element.OffHand
if frame and color then
frame:SetStatusBarColor(color.r, color.g, color.b)
local bg = frame.bg
if bg then
local mu = bg.multiplier or 1
bg:SetVertexColor(color.r * mu, color.g * mu, color.b * mu)
end
end
end
local function OnUpdate(self, elapsed)
self.updateInterval = (self.updateInterval or 0) - elapsed
if self.updateInterval <= 0 then
local now = GetTime()
-- update main-hand bar
do
local statusbar = self.MainHand
if statusbar:IsShown() then
local max = statusbar.speed or 0
local value = now - (statusbar.startTime or now)
if value >= max then
value = 0
statusbar.startTime = now
end
statusbar:SetValue(value)
if statusbar.Text then
statusbar.Text:SetFormattedText("%.1f / %.1f", value, max)
end
end
end
-- update off-hand bar
do
local statusbar = self.OffHand
if statusbar:IsShown() then
local max = statusbar.speed or 0
local value = now - (statusbar.startTime or now)
if value >= max then
value = 0
statusbar.startTime = now
end
statusbar:SetValue(value)
if statusbar.Text then
statusbar.Text:SetFormattedText("%.1f / %.1f", value, max)
end
end
end
self.updateInterval = 0.01
end
end
local function Reset(self, event, isOffHand)
local element = self.SwingTimer
if not element then return end
local statusbar = isOffHand and element.OffHand or element.MainHand
statusbar.startTime = GetTime()
statusbar:SetValue(0)
end
local function Update(self, event, unit)
local element = self.SwingTimer
if not element then return end
local mainhand = element.MainHand
local offhand = element.OffHand
if event == "UNIT_ATTACK_SPEED" or event == "ForceUpdate" then
local mainSpeed, offSpeed = UnitAttackSpeed("player")
local rangedSpeed, _, _, _, _, _ = UnitRangedDamage("player")
mainhand.speed = mainSpeed or 0
mainhand:SetMinMaxValues(0, mainhand.speed)
if not mainhand.speed then
mainhand:Hide()
end
offhand.speed = offSpeed or rangedSpeed or 0
if not offhand.speed then
offhand:Hide()
end
elseif event == "COMBAT_LOG_EVENT_UNFILTERED" then
local _, subevent, _, sourceGUID, _, _, _, _ = CombatLogGetCurrentEventInfo()
-- ignore events
if not COMBAT_EVENTS[subevent] then return end
-- ignore events not from player
if sourceGUID ~= element.__guid then return end
if subevent == "SWING_DAMAGE" or subevent == "RANGE_DAMAGE" then
local index = (subevent == "RANGE_DAMAGE") and 15 or 12
local amount, overkill, school, resisted, blocked, absorbed, critical, glancing, crushing, isOffHand = select(index, CombatLogGetCurrentEventInfo())
if (subevent == "RANGE_DAMAGE" and element.__class == "PRIEST") then
isOffHand = true
end
Reset(self, event .. '.' .. subevent, isOffHand or false)
elseif subevent == "SWING_MISSED" or subevent == "RANGE_MISSED" then
local index = (subevent == "RANGE_MISSED") and 15 or 12
local missType, isOffHand, amountMissed, critical = select(index, CombatLogGetCurrentEventInfo())
if (subevent == "RANGE_MISSED" and element.__class == "PRIEST") then
isOffHand = true
end
Reset(self, event .. '.' .. subevent, isOffHand or false)
elseif subevent == "" then
local spellID, spellName, spellSchool, _ = select(12, CombatLogGetCurrentEventInfo())
if SWING_RESET_SPELLS[spellID] then
Reset(self, event .. '.' .. subevent, false)
Reset(self, event .. '.' .. subevent, true)
end
else
return
end
elseif event == "PLAYER_REGEN_ENABLED" then
element:Hide()
element:SetScript("OnUpdate", nil)
elseif event == "PLAYER_REGEN_DISABLED" then
element:Show()
element:SetScript("OnUpdate", OnUpdate)
end
--[[ Callback: SwingTimer:PreUpdate()
Called before the element has been updated.
* self - the SwingTimer element
--]]
if(element.PreUpdate) then
element:PreUpdate()
end
local now = GetTime()
mainhand:SetMinMaxValues(0, mainhand.speed)
mainhand:SetValue(mainhand.startTime and (now - mainhand.startTime) or 0)
offhand:SetMinMaxValues(0, offhand.speed)
offhand:SetValue(offhand.startTime and (now - offhand.startTime) or 0)
--[[ Callback: SwingTimer:PostUpdate(cur, max)
Called after the element has been updated.
* self - the SwingTimer element
* cur - the amount of staggered damage (number)
* max - the player's maximum possible health value (number)
--]]
if(element.PostUpdate) then
element:PostUpdate(cur, max)
end
end
local function Path(self, ...)
--[[ Override: SwingTimer.Override(self, event, unit)
Used to completely override the internal update function.
* self - the parent object
* event - the event triggering the update (string)
* unit - the unit accompanying the event (string)
--]]
(self.SwingTimer.Override or Update)(self, ...);
--[[ Override: SwingTimer.UpdateColor(self, event, unit)
Used to completely override the internal function for updating the widgets' colors.
* self - the parent object
* event - the event triggering the update (string)
* unit - the unit accompanying the event (string)
--]]
local fn = (self.SwingTimer.UpdateColor or UpdateColor)
fn(self, "main-hand", ...)
fn(self, "off-hand", ...)
end
local function Visibility(self, event, unit)
local element = self.SwingTimer
local _, class = UnitClass(unit)
local mainSpeed, offSpeed = UnitAttackSpeed("player")
local rangedSpeed, _, _, _, _, _ = UnitRangedDamage("player")
element.MainHand.speed = mainSpeed
element.OffHand.speed = offSpeed or rangedSpeed
-- if offSpeed or rangedSpeed then
-- element.OffHand:Show()
-- else
-- element.OffHand:Hide()
-- end
Path(self, event, unit)
end
local function VisibilityPath(self, ...)
--[[ Override: SwingTimer.OverrideVisibility(self, event, unit)
Used to completely override the internal visibility toggling function.
* self - the parent object
* event - the event triggering the update (string)
* unit - the unit accompanying the event (string)
--]]
(self.SwingTimer.OverrideVisibility or Visibility)(self, ...)
end
local function ForceUpdate(element)
VisibilityPath(element.__owner, "ForceUpdate", element.__owner.unit)
end
local function Enable(self, unit)
local element = self.SwingTimer
if element and UnitIsUnit(unit, "player") then
element.__owner = self
element.ForceUpdate = ForceUpdate
self:RegisterEvent("UNIT_ATTACK_SPEED", Path)
self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED", Path, true)
self:RegisterEvent("PLAYER_REGEN_ENABLED", Path, true)
self:RegisterEvent("PLAYER_REGEN_DISABLED", Path, true)
for _, frame in next, ({ element.MainHand, element.OffHand }) do
if frame and frame:IsObjectType('StatusBar') and not frame:GetStatusBarTexture() then
frame:SetStatusBarTexture([[Interface\TargetingFrame\UI-StatusBar]])
end
end
local _, class = UnitClass("player")
element.__class = class
element.__guid = UnitGUID("player")
element.updateInterval = 0
-- element:SetScript("OnUpdate", OnUpdate)
element:Hide()
return true
end
end
local function Disable(self)
local element = self.SwingTimer
if element then
element:SetScript("OnUpdate", nil)
element:Hide()
self:UnregisterEvent("UNIT_ATTACK_SPEED", Path)
self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED", Path)
self:UnregisterEvent("PLAYER_REGEN_ENABLED", Path)
self:UnregisterEvent("PLAYER_REGEN_DISABLED", Path)
end
end
oUF:AddElement("SwingTimer", VisibilityPath, Enable, Disable)