Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 62 additions & 40 deletions AnimationGroup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function AG:New(name, parent)
end

local function CreateAnimationGroup(self, name, inherits_from)
local ag = AG.AnimationGroup:Bind(CreateFrame('Frame', nil, self))
local ag = AG.AnimationGroup:Bind(CreateFrame('Frame', name, self))

ag:__Initialize(self)
ag.__Initialize = nil
Expand All @@ -60,27 +60,48 @@ function CreateFrame(...)
return frame
end


local function OnUpdate(self, elapsed)
if self.paused then
return
end
local in_reverse = self.group.reverse

self.time = self.time + (self.group.reverse and -elapsed or elapsed)
self.time = self.time + (in_reverse and -elapsed or elapsed)
local animation_time = self.time - self.startDelay

if self.time > self.duration or (self.group.reverse and self.time < 0) then
AG:Stop(self)
AG:Fire(self.group, self, 'Finished')
self.progress = animation_time / self.duration
-- Fast min/max to ensure progress is bound within `0 < self.progress < 1'
self.progress = self.progress < 0 and 0 or self.progress
self.progress = self.progress > 1 and 1 or self.progress
Comment thread
laytya marked this conversation as resolved.

return
-- These represent whether the time "pointer" is in the left- or the right delay part, at which it is not animating, but should remain visible.
local is_delayed_start = self.time < self.startDelay and 0 < self.time
local is_delayed_end = (self.totalTime - self.endDelay) < self.time and self.time < self.totalTime

local is_animation_complete = (in_reverse and self.time < 0) or self.totalTime < self.time
self.delayed = is_delayed_start or is_delayed_end

-- We should calculate smoothProgress before Users's callback_handlers
self.smoothProgress = self.smoothing_func(self.progress).y

-- Calling user's callback_handlers
if type(self.handlers["OnUpdate"]) == 'function' then
self.handlers["OnUpdate"](self, elapsed)
end

-- Temporary until all animation types are implemented
-- Calling animations OnUpdate
if self.OnUpdate then
self:OnUpdate(elapsed)
end
end

-- Stop animation after last update
if is_animation_complete then
AG:Stop(self)
AG:Fire(self.group, self, 'Finished')
return
end

end

--[[
Global library routines
Expand Down Expand Up @@ -109,51 +130,45 @@ function AG:StopGroup(group)
AG:Stop(animation)
end
end

self:LoadProperties(group)

group.playing = false
end

function AG:SaveProperties(group)
group.properties.alpha = group.parent:GetAlpha()
group.properties.width = group.parent:GetWidth()
group.properties.height = group.parent:GetHeight()
group.properties.point = { group.parent:GetPoint() }
for k, v in pairs(group:GetAnimations()) do
v:SaveProperties()
end
end

function AG:LoadProperties(group)
group.parent:SetAlpha(group.properties.alpha)
group.parent:SetWidth(group.properties.width)
group.parent:SetHeight(group.properties.height)

local point = group.properties.point
if point and point[1] then
group.parent:SetPoint(unpack(point))
for k, v in pairs(group:GetAnimations()) do
v:LoadProperties()
end
end

function AG:Stop(animation)
animation.time = 0

if animation.OnUpdate then
animation:SetScript('OnUpdate', nil)
animation:_SetScript('OnUpdate', nil)
end

animation.playing = false
end

function AG:Play(animation)
if not animation.playing and animation.group.parent:IsVisible() then
animation.time = animation.group.reverse and animation.duration or 0
if not animation.playing and animation.target:IsVisible() then
animation.totalTime = animation.startDelay + animation.duration + animation.endDelay
animation.time = animation.group.reverse and animation.totalTime or 0
animation.progress = 0
animation.smoothProgress = 0
animation.playing = true
animation:SetScript('OnUpdate', function() OnUpdate(this, arg1) end)
animation:_SetScript('OnUpdate', function() OnUpdate(this, arg1) end)
Comment thread
laytya marked this conversation as resolved.
end

animation.paused = false
end


function AG:Pause(animation)
animation.paused = true
end
Expand All @@ -176,6 +191,7 @@ function AG:Fire(group, animation, signal)

local all_finished = true
local bouncing = group.loop_type == 'BOUNCE'
local repeating = group.loop_type == 'REPEAT'

-- Only animations notify with `FINISHED' signals!
if signal == 'Finished' then
Expand All @@ -194,15 +210,15 @@ function AG:Fire(group, animation, signal)
if callback_handlers[signal] then
group_func = group.handlers['On' .. signal]

if not animation then
if not animation and group.animations[group.order + 1] then
local handler_func
for _, anim in next, group.animations[group.order + 1] do
handler_func = anim.handlers['On' .. signal]
if type(handler_func) == 'function' then
table.insert(func, { anim, handler_func })
end
end
else
elseif animation then
func = animation.handlers['On' .. signal]
end
end
Expand All @@ -222,8 +238,10 @@ function AG:Fire(group, animation, signal)
if (signal == 'Finished' and all_finished) or signal == 'Bounce' then
if group.shifted then
if not group.finishing then
group.reverse = not group.reverse
group.reverse = bouncing and (not group.reverse)
AG:PlayGroup(group)
group_func = group.handlers['OnLoop']
table.insert(args, group.reverse and 'REVERSE' or 'FORWARD')
end
group.shifted = false
else
Expand Down Expand Up @@ -253,15 +271,10 @@ function AG:Fire(group, animation, signal)
-- must therefore be performed AFTER the animation callback and BEFORE the
-- group's callback
if (signal == 'Finished' and all_finished and shift) then
if (group.finishing and bouncing) or (not bouncing) then
if (group.finishing and (bouncing or repeating)) or not (bouncing or repeating) then
AG:StopGroup(group)

group_func = group.handlers['OnFinished']
table.insert(args, group.finishing)
else
group_func = group.handlers['OnLoop']

table.insert(args, group.reverse and 'REVERSE' or 'FORWARD')
end
end

Expand All @@ -271,15 +284,24 @@ function AG:Fire(group, animation, signal)
end

-- We `bounce' if the boundary orders have no animations
if (not group.playing and shift and
(not group.finishing) and bouncing) then
group.reverse = not group.reverse
if (signal == 'Finished' and not group.playing and shift and
(not group.finishing) and (bouncing or repeating)) then
if repeating then
group.order = -1
else
group.reverse = not group.reverse
end
group_func = group.handlers['OnLoop']
table.insert(args, group.reverse and 'REVERSE' or 'FORWARD')
if type(group_func) == 'function' then
group_func(group, unpack(args))
end
AG:Fire(group, nil, 'Bounce')
end
end

function AG:MoveOrder(group, animation, new_order)
local old_order = group.order
local old_order = animation.order

for i, anim in next, group.animations[old_order + 1] do
if anim == animation then
Expand Down
45 changes: 25 additions & 20 deletions alpha.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,45 @@ if AG.Alpha then return end
local Alpha = AG:New('Alpha', AG.Animation)

function Alpha:__Initialize()
self.alpha_change = nil
self.alpha_from = 1
self.alpha_change = 0
self.alpha_from = 0
self.alpha_to = 0
end

function Alpha:SetChange(change)
self.alpha_change = change
function Alpha:SaveProperties()
self.properties.alpha = self.target:GetAlpha()
end

function Alpha:GetChange()
return self.alpha_change
function Alpha:LoadProperties()
if self.properties.alpha then self.target:SetAlpha(self.properties.alpha) end
end

function Alpha:SetFromAlpha(alpha)
self.alpha_from = alpha
SetChange(self)
function Alpha:SetChange(change)
self.alpha_change = change
end

function Alpha:SetToAlpha(alpha)
self.alpha_to = alpha
SetChange(self)
function Alpha:GetChange()
return self.alpha_change
end

function Alpha:OnUpdate(elapsed)
local properties = self.group.properties
do
local function SetChange(self)
self.alpha_change = self.alpha_to - self.alpha_from
end

self.progress = self.smoothing_func(self.time / self.duration).y
function Alpha:SetFromAlpha(alpha)
self.alpha_from = alpha
SetChange(self)
end

local frame = self.group.parent

frame:SetAlpha(properties.alpha + self.progress * self.alpha_change)
function Alpha:SetToAlpha(alpha)
self.alpha_to = alpha
SetChange(self)
end
end

function Alpha:OnUpdate(elapsed)
local frame = self.target

local function SetChange()
self.alpha_change = self.alpha_from - self.alpha_to
frame:SetAlpha(self.alpha_from + self.smoothProgress * self.alpha_change)
end
30 changes: 20 additions & 10 deletions animation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function Animation:__SetScript(handler, func)
end
end


--[[
API
--]]
Expand All @@ -54,22 +53,20 @@ function Animation:Play()
AG:Fire(self.group, self, 'Play')
end


function Animation:Pause()
AG:Pause(self)

AG:Fire(self.group, self, 'Pause')
end


function Animation:Stop()
AG:Stop(self)

AG:Fire(self.group, self, 'Stop')
end

function Animation:IsDone()
return not self.playing
return self.finished
end

function Animation:IsPlaying()
Expand All @@ -85,25 +82,27 @@ function Animation:IsStopped()
end

function Animation:IsDelaying()
return not not self.delay
return self.delayed
end

function Animation:GetElapsed()
return self.time
return self.time < 0 and 0 or self.totalTime < self.time and self.totalTime or self.time
end

function Animation:SetStartDelay(delay_sec)
self.delay = delay_sec
self.startDelay = delay_sec
end

function Animation:GetStartDelay()
return self.delay
return self.startDelay
end

function Animation:SetEndDelay(delay_sec)
self.endDelay = delay_sec
end

function Animation:GetEndDelay()
return self.endDelay
end

function Animation:SetDuration(duration)
Expand All @@ -118,10 +117,12 @@ function Animation:GetProgress()
return self.progress
end

function Animation:GetSmoothProgress()
function Animation:SetSmoothProgress(smoothProgress)
self.smoothProgress = smoothProgress
end

function Animation:GetProgressWithDelay()
function Animation:GetSmoothProgress()
return self.smoothProgress
end

function Animation:SetMaxFramerate(framerate)
Expand Down Expand Up @@ -152,3 +153,12 @@ end
function Animation:GetRegionParent()
return self.group.parent
end

function Animation:SetTarget(region)
self.target = region
self:SaveProperties()
end

function Animation:GetTarget()
return self.target
end
Loading