-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcastbar.lua
More file actions
664 lines (538 loc) · 19.7 KB
/
Copy pathcastbar.lua
File metadata and controls
664 lines (538 loc) · 19.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
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
--[[
# Element: Castbar
Handles the visibility and updating of spell castbars.
---@class oUFCastbarElement : Frame
---@field Icon Texture|nil Spell icon
---@field SafeZone Texture|nil Latency safe zone
---@field Shield Texture|nil Interrupt/steal indicator
---@field Spark Texture|nil Cast progress spark
---@field Text FontString|nil Spell name text
---@field Time FontString|nil Duration timer
---@field casting boolean Currently casting
---@field channeling boolean Currently channeling
---@field empowering boolean Currently empowering
---@field spellID number|nil Current spell ID
---@field spellName string|nil Current spell name
## Widget
Castbar - A `StatusBar` to represent spell cast/channel progress.
## Sub-Widgets
.Icon - A `Texture` to represent spell icon.
.SafeZone - A `Texture` to represent latency.
.Shield - A `Texture` to represent if it's possible to interrupt or spell steal.
.Spark - A `Texture` to represent the castbar's edge.
.Text - A `FontString` to represent spell name.
.Time - A `FontString` to represent spell duration.
## Notes
A default texture will be applied to the StatusBar and Texture widgets if they don't have a texture or a color set.
## Options
.timeToHold - Indicates for how many seconds the castbar should be visible after a _FAILED or _INTERRUPTED
event. Defaults to 0 (number)
.hideTradeSkills - Makes the element ignore casts related to crafting professions (boolean)
.smoothing - Which status bar smoothing method to use, defaults to `Enum.StatusBarInterpolation.Immediate` (number)
## Attributes
.castID - A unique identifier of the currently cast spell (number?)
.casting - Indicates whether the current spell is an ordinary cast (boolean)
.channeling - Indicates whether the current spell is a channeled cast (boolean)
.empowering - Indicates whether the current spell is an empowering cast (boolean)
.notInterruptible - Indicates whether the current spell is interruptible (boolean)
.spellID - The spell identifier of the currently cast/channeled/empowering spell (number)
.spellName - The name of the spell currently being cast/channeled/empowered (string)
## Examples
-- Position and size
local Castbar = CreateFrame('StatusBar', nil, self)
Castbar:SetSize(20, 20)
Castbar:SetPoint('TOP')
Castbar:SetPoint('LEFT')
Castbar:SetPoint('RIGHT')
-- Add a spark
local Spark = Castbar:CreateTexture(nil, 'OVERLAY')
Spark:SetSize(20, 20)
Spark:SetBlendMode('ADD')
Spark:SetPoint('CENTER', Castbar:GetStatusBarTexture(), 'RIGHT', 0, 0)
-- Add a timer
local Time = Castbar:CreateFontString(nil, 'OVERLAY', 'GameFontNormalSmall')
Time:SetPoint('RIGHT', Castbar)
-- Add spell text
local Text = Castbar:CreateFontString(nil, 'OVERLAY', 'GameFontNormalSmall')
Text:SetPoint('LEFT', Castbar)
-- Add spell icon
local Icon = Castbar:CreateTexture(nil, 'OVERLAY')
Icon:SetSize(20, 20)
Icon:SetPoint('TOPLEFT', Castbar, 'TOPLEFT')
-- Add Shield
local Shield = Castbar:CreateTexture(nil, 'OVERLAY')
Shield:SetSize(20, 20)
Shield:SetPoint('CENTER', Castbar)
-- Add safezone
local SafeZone = Castbar:CreateTexture(nil, 'OVERLAY')
-- Register it with oUF
Castbar.Spark = Spark
Castbar.Time = Time
Castbar.Text = Text
Castbar.Icon = Icon
Castbar.Shield = Shield
Castbar.SafeZone = SafeZone
self.Castbar = Castbar
--]]
local _, ns = ...
local oUF = ns.oUF
local Private = oUF.Private
local FALLBACK_ICON = 136243 -- Interface\ICONS\Trade_Engineering
local FAILED = _G.FAILED or 'Failed'
local INTERRUPTED = _G.INTERRUPTED or 'Interrupted'
local function resetAttributes(self)
self.castID = nil
self.casting = nil
self.channeling = nil
self.empowering = nil
self.notInterruptible = nil
self.spellID = nil
self.spellName = nil
for _, pip in next, self.Pips do
pip:Hide()
end
end
local function CreatePip(element)
return CreateFrame('Frame', nil, element, 'CastingBarFrameStagePipTemplate')
end
local function UpdatePips(element, stages)
local isHoriz = element:GetOrientation() == 'HORIZONTAL'
local elementSize = isHoriz and element:GetWidth() or element:GetHeight()
local lastOffset = 0
for stage, stageSection in next, stages do
local offset = lastOffset + (elementSize * stageSection)
lastOffset = offset
local pip = element.Pips[stage]
if(not pip) then
--[[ Override: Castbar:CreatePip(stage)
Creates a "pip" for the given stage, used for empowered casts.
* self - the Castbar widget
* stage - the empowered stage for which the pip should be created (number)
## Returns
* pip - a frame used to depict an empowered stage boundary, typically with a line texture (frame)
--]]
pip = (element.CreatePip or CreatePip) (element, stage)
element.Pips[stage] = pip
end
pip:ClearAllPoints()
pip:Show()
if(isHoriz) then
if(pip.RotateTextures) then
pip:RotateTextures(0)
end
if(element:GetReverseFill()) then
pip:SetPoint('TOP', element, 'TOPRIGHT', -offset, 0)
pip:SetPoint('BOTTOM', element, 'BOTTOMRIGHT', -offset, 0)
else
pip:SetPoint('TOP', element, 'TOPLEFT', offset, 0)
pip:SetPoint('BOTTOM', element, 'BOTTOMLEFT', offset, 0)
end
else
if(pip.RotateTextures) then
pip:RotateTextures(1.5708)
end
if(element:GetReverseFill()) then
pip:SetPoint('LEFT', element, 'TOPLEFT', 0, -offset)
pip:SetPoint('RIGHT', element, 'TOPRIGHT', 0, -offset)
else
pip:SetPoint('LEFT', element, 'BOTTOMLEFT', 0, offset)
pip:SetPoint('RIGHT', element, 'BOTTOMRIGHT', 0, offset)
end
end
end
--[[ Callback: Castbar:PostUpdatePips(stages)
Called after the element has updated stage separators (pips) in an empowered cast.
* self - the Castbar widget
* stages - stages with percentage of each stage (table)
--]]
if(element.PostUpdatePips) then
element:PostUpdatePips(stages)
end
end
--[[ Override: Castbar:ShouldShow(unit)
Handles check for which unit the castbar should show for.
Defaults to the object unit.
* self - the Castbar widget
* unit - the unit for which the update has been triggered (string)
--]]
local function ShouldShow(element, unit)
return element.__owner.unit == unit
end
local function CastStart(self, event, unit)
local element = self.Castbar
if(not (element.ShouldShow or ShouldShow) (element, unit)) then
return
end
local direction, duration = Enum.StatusBarTimerDirection.ElapsedTime
local name, text, texture, startTime, endTime, isTradeSkill, _, notInterruptible, spellID, castID = UnitCastingInfo(unit)
if(name) then
element.casting = true
duration = UnitCastingDuration(unit)
else
local isEmpowered
name, text, texture, startTime, endTime, isTradeSkill, notInterruptible, spellID, isEmpowered, _, castID = UnitChannelInfo(unit)
if(isEmpowered) then
element.empowering = true
duration = UnitEmpoweredChannelDuration(unit)
else
element.channeling = true
duration = UnitChannelDuration(unit)
direction = Enum.StatusBarTimerDirection.RemainingTime
end
end
if(not name or (isTradeSkill and element.hideTradeSkills)) then
-- don't cancel hold time when we swap targets
if(not (event == 'PLAYER_TARGET_CHANGED' and element.holdTime and element.holdTime > 0)) then
resetAttributes(element)
element:Hide()
end
return
end
element.delay = 0
element.notInterruptible = notInterruptible
element.holdTime = 0
element.castID = castID
element.spellID = spellID
element.spellName = text
if(unit == 'player') then
-- we can only read these variables for players
element.startTime = startTime / 1000
if(self.empowering) then
element.endTime = (endTime + GetUnitEmpowerHoldAtMaxTime(unit)) / 1000
else
element.endTime = endTime / 1000
end
end
element:SetTimerDuration(duration, element.smoothing, direction)
if(element.Icon) then element.Icon:SetTexture(texture or FALLBACK_ICON) end
if(element.Shield) then element.Shield:SetAlphaFromBoolean(notInterruptible, 1, 0) end
if(element.Spark) then element.Spark:Show() end
if(element.Text) then element.Text:SetText(text) end
if(element.Time) then element.Time:SetText() end
local safeZone = element.SafeZone
if(safeZone and unit == 'player') then
local isHoriz = element:GetOrientation() == 'HORIZONTAL'
safeZone:ClearAllPoints()
safeZone:SetPoint(isHoriz and 'TOP' or 'LEFT')
safeZone:SetPoint(isHoriz and 'BOTTOM' or 'RIGHT')
if(element.channeling) then
safeZone:SetPoint(element:GetReverseFill() and (isHoriz and 'RIGHT' or 'TOP') or (isHoriz and 'LEFT' or 'BOTTOM'))
else
safeZone:SetPoint(element:GetReverseFill() and (isHoriz and 'LEFT' or 'BOTTOM') or (isHoriz and 'RIGHT' or 'TOP'))
end
if(element.empowering) then
endTime = endTime + GetUnitEmpowerHoldAtMaxTime(unit)
end
local ratio = (select(4, GetNetStats())) / (endTime - startTime)
if(ratio > 1) then
ratio = 1
end
safeZone[isHoriz and 'SetWidth' or 'SetHeight'](safeZone, element[isHoriz and 'GetWidth' or 'GetHeight'](element) * ratio)
end
if(element.empowering) then
--[[ Override: Castbar:UpdatePips(stages)
Handles updates for stage separators (pips) in an empowered cast.
* self - the Castbar widget
* stages - stages with percentage of each stage (table)
--]]
(element.UpdatePips or UpdatePips) (element, UnitEmpoweredStagePercentages(unit))
end
--[[ Callback: Castbar:PostCastStart(unit)
Called after the element has been updated upon a spell cast or channel start.
* self - the Castbar widget
* unit - the unit for which the update has been triggered (string)
--]]
if(element.PostCastStart) then
element:PostCastStart(unit)
end
element:Show()
end
local function CastUpdate(self, event, unit, _, _, castID)
local element = self.Castbar
if(not (element.ShouldShow or ShouldShow) (element, unit)) then
return
end
if(not element:IsShown() or not castID or element.castID ~= castID) then
return
end
local direction, duration, name, startTime, _ = Enum.StatusBarTimerDirection.ElapsedTime
if(event == 'UNIT_SPELLCAST_DELAYED') then
name, _, _, startTime = UnitCastingInfo(unit)
duration = UnitCastingDuration(unit)
else
name, _, _, startTime = UnitChannelInfo(unit)
if(event == 'UNIT_SPELLCAST_EMPOWER_UPDATE') then
duration = UnitEmpoweredChannelDuration(unit)
else
duration = UnitChannelDuration(unit)
direction = Enum.StatusBarTimerDirection.RemainingTime
end
end
if(not name) then return end
if(unit == 'player') then
-- we can only calculate delay for players
startTime = startTime / 1000
local delta
if(element.channeling) then
delta = element.startTime - startTime
else
delta = startTime - element.startTime
end
if(delta < 0) then
delta = 0
end
element.delay = element.delay + delta
end
element:SetTimerDuration(duration, element.smoothing, direction)
--[[ Callback: Castbar:PostCastUpdate(unit)
Called after the element has been updated when a spell cast or channel has been updated.
* self - the Castbar widget
* unit - the unit that the update has been triggered (string)
--]]
if(element.PostCastUpdate) then
return element:PostCastUpdate(unit)
end
end
local function CastStop(self, event, unit, _, _, ...)
local element = self.Castbar
if(not (element.ShouldShow or ShouldShow) (element, unit)) then
return
end
local castID, interruptedBy, empowerComplete
if(event == 'UNIT_SPELLCAST_STOP') then
castID = ...
elseif(event == 'UNIT_SPELLCAST_EMPOWER_STOP') then
empowerComplete, interruptedBy, castID = ...
elseif(event == 'UNIT_SPELLCAST_CHANNEL_STOP') then
interruptedBy, castID = ...
end
if(not element:IsShown() or not castID or element.castID ~= castID) then
return
end
if(element.Spark) then element.Spark:Hide() end
if(interruptedBy) then
if(element.Text) then element.Text:SetText(INTERRUPTED) end
element.holdTime = element.timeToHold or 0
-- force filled castbar
element:SetMinMaxValues(0, 1)
element:SetValue(1)
end
if(interruptedBy) then
--[[ Callback: Castbar:PostCastInterrupted(unit, interruptedBy)
Called after the element has been updated when a spell cast or channel has stopped.
* self - the Castbar widget
* unit - the unit for which the update has been triggered (string)
* interruptedBy - GUID of whomever interrupted the cast (string)
--]]
if(element.PostCastInterrupted) then
element:PostCastInterrupted(unit, interruptedBy)
end
else
--[[ Callback: Castbar:PostCastStop(unit[, empowerComplete])
Called after the element has been updated when a spell cast or channel has stopped.
* self - the Castbar widget
* unit - the unit for which the update has been triggered (string)
* empowerComplete - if the empowered cast was complete (boolean?)
--]]
if(element.PostCastStop) then
element:PostCastStop(unit, empowerComplete)
end
end
resetAttributes(element)
end
local function CastFail(self, event, unit, _, _, ...)
local element = self.Castbar
if(not (element.ShouldShow or ShouldShow) (element, unit)) then
return
end
local castID, interruptedBy
if(event == 'UNIT_SPELLCAST_INTERRUPTED') then
interruptedBy, castID = ...
elseif(event == 'UNIT_SPELLCAST_FAILED') then
castID = ...
end
if(not element:IsShown() or not castID or element.castID ~= castID) then
return
end
if(element.Text) then
element.Text:SetText(event == 'UNIT_SPELLCAST_FAILED' and FAILED or INTERRUPTED)
end
if(element.Spark) then element.Spark:Hide() end
element.holdTime = element.timeToHold or 0
-- force filled castbar
element:SetMinMaxValues(0, 1)
element:SetValue(1)
if(interruptedBy) then
if(element.PostCastInterrupted) then
element:PostCastInterrupted(unit, interruptedBy)
end
else
--[[ Callback: Castbar:PostCastFail(unit)
Called after the element has been updated upon a failed or interrupted spell cast.
* self - the Castbar widget
* unit - the unit for which the update has been triggered (string)
--]]
if(element.PostCastFail) then
element:PostCastFail(unit)
end
end
resetAttributes(element)
end
local function CastInterruptible(self, event, unit)
local element = self.Castbar
if(not (element.ShouldShow or ShouldShow) (element, unit)) then
return
end
if(not element:IsShown()) then return end
-- ISSUE: we can't verify if this is for an active cast/channel/empower without castID
element.notInterruptible = event == 'UNIT_SPELLCAST_NOT_INTERRUPTIBLE'
if(element.Shield) then element.Shield:SetAlphaFromBoolean(element.notInterruptible, 1, 0) end
--[[ Callback: Castbar:PostCastInterruptible(unit)
Called after the element has been updated when a spell cast has become interruptible or uninterruptible.
* self - the Castbar widget
* unit - the unit for which the update has been triggered (string)
--]]
if(element.PostCastInterruptible) then
return element:PostCastInterruptible(unit)
end
end
local function onUpdate(self, elapsed)
if(self.casting or self.channeling or self.empowering) then
if(self.Time) then
local durationObject = self:GetTimerDuration() -- can be nil
if durationObject then
if(self.delay ~= 0) then
--[[ Override: Castbar:CustomDelayText(duration)
Used to completely override the updating of the .Time sub-widget when there is a delay to adjust for.
* self - the Castbar widget
* duration - a [Duration](https://warcraft.wiki.gg/wiki/ScriptObject_DurationObject) object for the Castbar
--]]
if(self.CustomDelayText) then
self:CustomDelayText(durationObject)
else
local duration = durationObject:GetRemainingDuration()
self.Time:SetFormattedText('%.1f|cffff0000%s%.2f|r', duration, self.channeling and '-' or '+', self.delay)
end
else
--[[ Override: Castbar:CustomTimeText(duration)
Used to completely override the updating of the .Time sub-widget.
* self - the Castbar widget
* duration - a [Duration](https://warcraft.wiki.gg/wiki/ScriptObject_DurationObject) object for the Castbar
--]]
if(self.CustomTimeText) then
self:CustomTimeText(durationObject)
else
self.Time:SetFormattedText('%.1f', durationObject:GetRemainingDuration())
end
end
end
end
-- ISSUE: we have no way to get this information any more, Blizzard is aware
-- --[[ Callback: Castbar:PostUpdateStage(stage)
-- Called after the current stage changes.
-- * self - the Castbar widget
-- * stage - the stage of the empowered cast (number)
-- --]]
-- if(self.empowering and self.PostUpdateStage) then
-- local old = self.curStage
-- for i = old + 1, self.numStages do
-- if(self.stagePoints[i]) then
-- if(self.duration > self.stagePoints[i]) then
-- self.curStage = i
-- if(self.curStage ~= old) then
-- self:PostUpdateStage(i)
-- end
-- else
-- break
-- end
-- end
-- end
-- end
elseif(self.holdTime > 0) then
self.holdTime = self.holdTime - elapsed
else
resetAttributes(self)
self:Hide()
end
end
local function Update(...)
CastStart(...)
end
local function ForceUpdate(element)
return Update(element.__owner, 'ForceUpdate', element.__owner.unit)
end
local eventMethods = {
UNIT_SPELLCAST_START = CastStart,
UNIT_SPELLCAST_CHANNEL_START = CastStart,
UNIT_SPELLCAST_EMPOWER_START = CastStart,
UNIT_SPELLCAST_STOP = CastStop,
UNIT_SPELLCAST_CHANNEL_STOP = CastStop,
UNIT_SPELLCAST_EMPOWER_STOP = CastStop,
UNIT_SPELLCAST_DELAYED = CastUpdate,
UNIT_SPELLCAST_CHANNEL_UPDATE = CastUpdate,
UNIT_SPELLCAST_EMPOWER_UPDATE = CastUpdate,
UNIT_SPELLCAST_FAILED = CastFail,
UNIT_SPELLCAST_INTERRUPTED = CastFail,
UNIT_SPELLCAST_INTERRUPTIBLE = CastInterruptible,
UNIT_SPELLCAST_NOT_INTERRUPTIBLE = CastInterruptible,
}
local function Enable(self, unit)
local element = self.Castbar
-- Enable castbar for all units (modern WoW supports casting info for *target units)
if(element and unit) then
element.__owner = self
element.ForceUpdate = ForceUpdate
if(not element.smoothing) then
element.smoothing = Enum.StatusBarInterpolation.Immediate
end
for event, method in next, eventMethods do
Private.SmartRegisterUnitEvent(self, event, unit, method)
end
element.holdTime = 0
element.Pips = element.Pips or {}
element:SetScript('OnUpdate', element.OnUpdate or onUpdate)
if(self.unit == 'player' and not (self.hasChildren or self.isChild or self.isNamePlate)) then
PlayerCastingBarFrame:UnregisterAllEvents()
PlayerCastingBarFrame:Hide()
PetCastingBarFrame:UnregisterAllEvents()
PetCastingBarFrame:Hide()
end
if(element:IsObjectType('StatusBar') and not element:GetStatusBarTexture()) then
element:SetStatusBarTexture([[Interface\TargetingFrame\UI-StatusBar]])
end
local spark = element.Spark
if(spark and spark:IsObjectType('Texture') and not spark:GetTexture()) then
spark:SetTexture([[Interface\CastingBar\UI-CastingBar-Spark]])
end
local shield = element.Shield
if(shield and shield:IsObjectType('Texture') and not shield:GetTexture()) then
shield:SetTexture([[Interface\CastingBar\UI-CastingBar-Small-Shield]])
end
local safeZone = element.SafeZone
if(safeZone and safeZone:IsObjectType('Texture') and not safeZone:GetTexture()) then
safeZone:SetColorTexture(1, 0, 0)
end
element:Hide()
return true
end
end
local function Disable(self)
local element = self.Castbar
if(element) then
element:Hide()
for event, method in next, eventMethods do
self:UnregisterEvent(event, method)
end
element:SetScript('OnUpdate', nil)
if(self.unit == 'player' and not (self.hasChildren or self.isChild or self.isNamePlate)) then
for event in next, eventMethods do
PlayerCastingBarFrame:RegisterUnitEvent(event, 'player')
PetCastingBarFrame:RegisterUnitEvent(event, 'pet')
end
PlayerCastingBarFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
PetCastingBarFrame:RegisterEvent('PLAYER_ENTERING_WORLD')
PetCastingBarFrame:RegisterEvent('UNIT_PET')
end
end
end
oUF:AddElement('Castbar', Update, Enable, Disable)