Skip to content

Commit

Permalink
castbar: Improve empowered cast handling (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
ls- authored Jul 24, 2023
1 parent 9c7e554 commit 031157c
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion elements/castbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ A default texture will be applied to the StatusBar and Texture widgets if they d
.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)
.numStages - The number of empowerment stages of the current spell (number?)
.curStage - The current empowerment stage of the spell. It updates only if the PostUpdateStage callback is
defined (number?)
.stagePoints - The timestamps (in seconds) for each empowerment stage (table)
## Examples
Expand Down Expand Up @@ -102,6 +106,10 @@ local function resetAttributes(self)
self.empowering = nil
self.notInterruptible = nil
self.spellID = nil
self.numStages = nil
self.curStage = nil

table.wipe(self.stagePoints)

for _, pip in next, self.Pips do
pip:Hide()
Expand All @@ -117,6 +125,8 @@ local function UpdatePips(element, numStages)
local stageMaxValue = element.max * 1000
local isHoriz = element:GetOrientation() == 'HORIZONTAL'
local elementSize = isHoriz and element:GetWidth() or element:GetHeight()
element.numStages = numStages
element.curStage = 0 -- NOTE: Updates only if the PostUpdateStage callback is present

for stage = 1, numStages do
local duration
Expand All @@ -128,6 +138,7 @@ local function UpdatePips(element, numStages)

if(duration > CASTBAR_STAGE_DURATION_INVALID) then
stageTotalDuration = stageTotalDuration + duration
element.stagePoints[stage] = stageTotalDuration / 1000

local portion = stageTotalDuration / stageMaxValue
local offset = elementSize * portion
Expand Down Expand Up @@ -173,6 +184,16 @@ local function UpdatePips(element, numStages)
end
end
end

--[[ Callback: Castbar:PostUpdatePips(numStages)
Called after the element has updated stage separators (pips) in an empowered cast.
* self - the Castbar widget
* numStages - the number of stages in the current cast (number)
--]]
if(element.PostUpdatePips) then
element:PostUpdatePips(numStages)
end
end

local function CastStart(self, event, unit)
Expand Down Expand Up @@ -454,6 +475,29 @@ local function onUpdate(self, elapsed)
end
end

--[[ 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

self:SetValue(self.duration)
elseif(self.holdTime > 0) then
self.holdTime = self.holdTime - elapsed
Expand Down Expand Up @@ -492,7 +536,8 @@ local function Enable(self, unit)
self:RegisterEvent('UNIT_SPELLCAST_NOT_INTERRUPTIBLE', CastInterruptible)

element.holdTime = 0
element.Pips = {}
element.stagePoints = {}
element.Pips = element.Pips or {}

element:SetScript('OnUpdate', element.OnUpdate or onUpdate)

Expand Down

0 comments on commit 031157c

Please sign in to comment.