From 29a8c4eba8a9dc6b3b1c7f4a01eadc992bd22afc Mon Sep 17 00:00:00 2001 From: LaYt Date: Sun, 18 Sep 2022 23:55:09 +0700 Subject: [PATCH 01/22] Fixed errors and added some missed options --- AnimationGroup.lua | 11 +++++++---- alpha.lua | 11 ++++++----- group.lua | 6 ++++-- scale.lua | 21 +++++++++++++++++++-- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index f6e1e45..5aeef5a 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -128,8 +128,11 @@ function AG:LoadProperties(group) group.parent:SetHeight(group.properties.height) local point = group.properties.point - if point and point[1] then - group.parent:SetPoint(unpack(point)) + -- old implementation some times fires error =) + if point and type(point) == "table" then + local point, region ,relativePoint, offsetX, offsetY = unpack(point) + region = type(region) ~= "function" and region or nil + group.parent:SetPoint(point, region ,relativePoint, offsetX, offsetY) end end @@ -194,7 +197,7 @@ 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] @@ -202,7 +205,7 @@ function AG:Fire(group, animation, signal) table.insert(func, { anim, handler_func }) end end - else + elseif animation then func = animation.handlers['On' .. signal] end end diff --git a/alpha.lua b/alpha.lua index 216ea6a..880585e 100644 --- a/alpha.lua +++ b/alpha.lua @@ -43,6 +43,10 @@ end function Alpha:GetChange() return self.alpha_change end +do + local function SetChange(self) + self.alpha_change = self.alpha_to - self.alpha_from + end function Alpha:SetFromAlpha(alpha) self.alpha_from = alpha @@ -53,6 +57,7 @@ function Alpha:SetToAlpha(alpha) self.alpha_to = alpha SetChange(self) end +end function Alpha:OnUpdate(elapsed) local properties = self.group.properties @@ -61,10 +66,6 @@ function Alpha:OnUpdate(elapsed) local frame = self.group.parent - frame:SetAlpha(properties.alpha + self.progress * self.alpha_change) + frame:SetAlpha(self.alpha_from + self.progress * self.alpha_change) end - -local function SetChange() - self.alpha_change = self.alpha_from - self.alpha_to -end diff --git a/group.lua b/group.lua index 097cab0..410295f 100644 --- a/group.lua +++ b/group.lua @@ -141,7 +141,8 @@ function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) ['OnPlay'] = true, ['OnPaused'] = true, ['OnStop'] = true, - ['OnFinished'] = true + ['OnFinished'] = true, + ['OnLoop'] = true } local default_smoothing = 'LINEAR' @@ -186,7 +187,8 @@ function AnimationGroup:__Initialize(parent) ['OnPlay'] = true, ['OnPaused'] = true, ['OnStop'] = true, - ['OnFinished'] = true + ['OnFinished'] = true, + ['OnLoop'] = true } -- The original implementation claims to support up to 100 orders... yuck! diff --git a/scale.lua b/scale.lua index 91c069c..51fe616 100644 --- a/scale.lua +++ b/scale.lua @@ -38,6 +38,9 @@ function Scale:__Initialize() self.scale = {} self.scale.x = nil self.scale.y = nil + self.from = {} + self.from.x = nil + self.from.y = nil end function Scale:SetOrigin(point, offset_x, offset_y) @@ -63,12 +66,26 @@ function Scale:GetScale() return scale.x, scale.y end +function Scale:SetFromScale(x, y) + self.from.x = x + self.from.y = y +end + +function Scale:GetFromScale() + local scale = self.from + + return scale.x, scale.y +end + function Scale:OnUpdate(elapsed) local properties = self.group.properties + local fromx = self.from.x ~= nil and self.from.x or 1 + local fromy = self.from.y ~= nil and self.from.y or 1 + self.progress = self.smoothing_func(self.time / self.duration).y local frame = self.group.parent - frame:SetWidth(properties.width + self.progress * (properties.width * self.scale.x)) - frame:SetHeight(properties.height + self.progress * (properties.height * self.scale.y)) + frame:SetWidth(properties.width * (fromx + self.progress * (self.scale.x - fromx))) + frame:SetHeight(properties.height * (fromy + self.progress * (self.scale.y - fromy))) end From 9b97da1bf981a77938a9c7715ddc209f7a600de2 Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 19 Sep 2022 11:22:53 +0700 Subject: [PATCH 02/22] Update scale.lua Co-authored-by: Martin Jesper Low Madsen --- scale.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scale.lua b/scale.lua index 51fe616..0b44bb6 100644 --- a/scale.lua +++ b/scale.lua @@ -38,9 +38,9 @@ function Scale:__Initialize() self.scale = {} self.scale.x = nil self.scale.y = nil - self.from = {} - self.from.x = nil - self.from.y = nil + self.from = {} + self.from.x = 1 + self.from.y = 1 end function Scale:SetOrigin(point, offset_x, offset_y) From 8241e910625c56dfef75274983022c88b8d75e4e Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 19 Sep 2022 11:23:13 +0700 Subject: [PATCH 03/22] Update scale.lua Co-authored-by: Martin Jesper Low Madsen --- scale.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scale.lua b/scale.lua index 0b44bb6..8e014f0 100644 --- a/scale.lua +++ b/scale.lua @@ -80,8 +80,8 @@ end function Scale:OnUpdate(elapsed) local properties = self.group.properties - local fromx = self.from.x ~= nil and self.from.x or 1 - local fromy = self.from.y ~= nil and self.from.y or 1 + local from_x = self.from.x + local from_y = self.from.y self.progress = self.smoothing_func(self.time / self.duration).y From c78fabf24bbf5bf702c6f224f068a32dad4708c2 Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 19 Sep 2022 11:23:31 +0700 Subject: [PATCH 04/22] Update scale.lua Co-authored-by: Martin Jesper Low Madsen --- scale.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scale.lua b/scale.lua index 8e014f0..3b08a86 100644 --- a/scale.lua +++ b/scale.lua @@ -86,6 +86,6 @@ function Scale:OnUpdate(elapsed) self.progress = self.smoothing_func(self.time / self.duration).y local frame = self.group.parent - frame:SetWidth(properties.width * (fromx + self.progress * (self.scale.x - fromx))) - frame:SetHeight(properties.height * (fromy + self.progress * (self.scale.y - fromy))) + frame:SetWidth(properties.width * (from_x + self.progress * (self.scale.x - from_x))) + frame:SetHeight(properties.height * (from_y + self.progress * (self.scale.y - from_y))) end From 4437701a82953d0df5dcb4b9ecb57d892c5e942a Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 19 Sep 2022 11:31:59 +0700 Subject: [PATCH 05/22] Update group.lua Co-authored-by: Martin Jesper Low Madsen --- group.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group.lua b/group.lua index 410295f..5d70ed3 100644 --- a/group.lua +++ b/group.lua @@ -188,7 +188,7 @@ function AnimationGroup:__Initialize(parent) ['OnPaused'] = true, ['OnStop'] = true, ['OnFinished'] = true, - ['OnLoop'] = true + ['OnLoop'] = true } -- The original implementation claims to support up to 100 orders... yuck! From 826d875cb4495f8abe72225853443d7b017fc20a Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 19 Sep 2022 11:34:31 +0700 Subject: [PATCH 06/22] Update AnimationGroup.lua Co-authored-by: Martin Jesper Low Madsen --- AnimationGroup.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 5aeef5a..7570497 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -129,10 +129,10 @@ function AG:LoadProperties(group) local point = group.properties.point -- old implementation some times fires error =) - if point and type(point) == "table" then - local point, region ,relativePoint, offsetX, offsetY = unpack(point) - region = type(region) ~= "function" and region or nil - group.parent:SetPoint(point, region ,relativePoint, offsetX, offsetY) + if point and type(point) == "table" then + local point, relative_to, relative_point, offset_x, offset_y = unpack(point) + relative_to = type(relative_to) ~= "function" and relative_to or nil + group.parent:SetPoint(point, relative_to, relative_point, offset_x, offset_y) end end From e162190284aefed32c0c4c587e04bfcb1275296e Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 19 Sep 2022 12:00:56 +0700 Subject: [PATCH 07/22] Fix for error --- AnimationGroup.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 7570497..9ed8f41 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -129,7 +129,7 @@ function AG:LoadProperties(group) local point = group.properties.point -- old implementation some times fires error =) - if point and type(point) == "table" then + if point and type(point) == "table" and table.getn(point) > 0 then local point, relative_to, relative_point, offset_x, offset_y = unpack(point) relative_to = type(relative_to) ~= "function" and relative_to or nil group.parent:SetPoint(point, relative_to, relative_point, offset_x, offset_y) From 4f6e26a81add217a9f30a01e394d873fadd4c17f Mon Sep 17 00:00:00 2001 From: LaYt Date: Fri, 30 Sep 2022 09:40:08 +0700 Subject: [PATCH 08/22] Update AnimationGroup.lua Co-authored-by: Martin Jesper Low Madsen --- AnimationGroup.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 9ed8f41..58badef 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -128,9 +128,9 @@ function AG:LoadProperties(group) group.parent:SetHeight(group.properties.height) local point = group.properties.point - -- old implementation some times fires error =) if point and type(point) == "table" and table.getn(point) > 0 then local point, relative_to, relative_point, offset_x, offset_y = unpack(point) + -- NOTE: Errors may occur if relative_to is a function. This may require more debugging to why it would be that in the first place relative_to = type(relative_to) ~= "function" and relative_to or nil group.parent:SetPoint(point, relative_to, relative_point, offset_x, offset_y) end From 1e9373bc90ae4a0f1cd01a0c0ae0ee18a78931b1 Mon Sep 17 00:00:00 2001 From: LaYt Date: Fri, 30 Sep 2022 09:41:23 +0700 Subject: [PATCH 09/22] Update group.lua Co-authored-by: Martin Jesper Low Madsen --- group.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group.lua b/group.lua index 5d70ed3..08c04dd 100644 --- a/group.lua +++ b/group.lua @@ -142,7 +142,7 @@ function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) ['OnPaused'] = true, ['OnStop'] = true, ['OnFinished'] = true, - ['OnLoop'] = true + ['OnLoop'] = true } local default_smoothing = 'LINEAR' From 6ffeecdde34b5e36f85ea9a10909d69387ac5007 Mon Sep 17 00:00:00 2001 From: LaYt Date: Sun, 2 Oct 2022 23:50:38 +0700 Subject: [PATCH 10/22] Huge Update Introduced animation:SetTarget() - Sets the region affected by this animation Reworked saved attributes per animation from per group Added: - startdelay & enddelay functionality - Get/SetSmoothProgress - to use custom function of smothprogress - 'OnStep' (temporary name) callback, fires every OnUpdate of animation - 'REPEAT' type of looping Fixed many mistakes. Fixed 'OnLoop' callback --- AnimationGroup.lua | 107 ++++++++++++++++++++++++++------------------- alpha.lua | 48 +++++++++++--------- animation.lua | 32 +++++++++++--- group.lua | 49 ++++++++++++++++----- path.lua | 4 +- rotation.lua | 61 ++++++++++++++++++-------- scale.lua | 47 +++++++++++++------- translation.lua | 28 +++++++++--- 8 files changed, 249 insertions(+), 127 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 58badef..232a859 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -24,7 +24,7 @@ if not LibStub then return end -local MAJOR_VERSION, MINOR_VERSION = 'AnimationGroup-1.0', '$Format:%ct-%h$' +local MAJOR_VERSION, MINOR_VERSION = 'AnimationGroup-1.0', '1520787712-0b498e1' -- Probably not a release if not string.find(MINOR_VERSION, '%d+') then MINOR_VERSION = 0 end @@ -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 @@ -60,28 +60,49 @@ function CreateFrame(...) return frame end - local function OnUpdate(self, elapsed) if self.paused then return end - - self.time = self.time + (self.group.reverse and -elapsed or elapsed) + self.delaying = true + + if not self.group.reverse and self.startdelay and self.startdelayTime < self.startdelay then + self.startdelayTime = self.startdelayTime + elapsed + elseif self.group.reverse and self.enddelay and self.enddelayTime < self.enddelay then + self.enddelayTime = self.enddelayTime + elapsed + elseif (not self.group.reverse and self.time < self.duration) or (self.group.reverse and self.time > 0) then + self.time = self.time + (self.group.reverse and -elapsed or elapsed) + self.delaying = false + self.progress = self.time / self.duration + self.progress = self.progress < 0 and 0 or self.progress + self.progress = self.progress > 1 and 1 or self.progress + self.smoothProgress = self.smoothing_func(self.progress).y + end if self.time > self.duration or (self.group.reverse and self.time < 0) then - AG:Stop(self) - AG:Fire(self.group, self, 'Finished') + if self.group.reverse and self.startdelay and self.startdelayTime < self.startdelay then + self.startdelayTime = self.startdelayTime + elapsed + self.delaying = true + elseif not self.group.reverse and self.enddelay and self.enddelayTime < self.enddelay then + self.enddelayTime = self.enddelayTime + elapsed + self.delaying = true + else + AG:Stop(self) + AG:Fire(self.group, self, 'Finished') + return + end - return + end + if type(self.handlers["OnStep"]) == 'function' then + self.handlers["OnStep"](self, elapsed) end -- Temporary until all animation types are implemented - if self.OnUpdate then + if not self.delaying and self.OnUpdate then self:OnUpdate(elapsed) end end - --[[ Global library routines --]] @@ -110,32 +131,9 @@ function AG:StopGroup(group) 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() } -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 type(point) == "table" and table.getn(point) > 0 then - local point, relative_to, relative_point, offset_x, offset_y = unpack(point) - -- NOTE: Errors may occur if relative_to is a function. This may require more debugging to why it would be that in the first place - relative_to = type(relative_to) ~= "function" and relative_to or nil - group.parent:SetPoint(point, relative_to, relative_point, offset_x, offset_y) - end -end - function AG:Stop(animation) animation.time = 0 @@ -147,8 +145,13 @@ function AG:Stop(animation) end function AG:Play(animation) - if not animation.playing and animation.group.parent:IsVisible() then + if not animation.playing and animation.target:IsVisible() then + -- printT({"AG:PLAY", animation:GetName()}) animation.time = animation.group.reverse and animation.duration or 0 + animation.startdelayTime = 0 + animation.enddelayTime = 0 + animation.progress = 0 + animation.smoothProgress = 0 animation.playing = true animation:SetScript('OnUpdate', function() OnUpdate(this, arg1) end) end @@ -156,7 +159,6 @@ function AG:Play(animation) animation.paused = false end - function AG:Pause(animation) animation.paused = true end @@ -179,6 +181,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 @@ -225,8 +228,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) or false AG:PlayGroup(group) + group_func = group.handlers['OnLoop'] + table.insert(args, group.reverse and 'REVERSE' or 'FORWARD') end group.shifted = false else @@ -256,15 +261,14 @@ 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) - + for k, v in pairs(group:GetAnimations()) do + v:LoadProperties() + end + --printT({"CallBack", 'OnFinished'}) 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 @@ -274,15 +278,26 @@ 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 + --group.shifted = true + if repeating then + group.order = -1 + else + --group.order = group.order - (group.reverse and -1 or 1) + 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 diff --git a/alpha.lua b/alpha.lua index 880585e..e099f48 100644 --- a/alpha.lua +++ b/alpha.lua @@ -31,11 +31,19 @@ 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:SaveProperties() + self.properties.alpha = self.target:GetAlpha() +end + +function Alpha:LoadProperties() + if self.properties.alpha then self.target:SetAlpha(self.properties.alpha) end +end + function Alpha:SetChange(change) self.alpha_change = change end @@ -43,29 +51,27 @@ end function Alpha:GetChange() return self.alpha_change end -do - local function SetChange(self) - self.alpha_change = self.alpha_to - self.alpha_from - end - -function Alpha:SetFromAlpha(alpha) - self.alpha_from = alpha - SetChange(self) -end -function Alpha:SetToAlpha(alpha) - self.alpha_to = alpha - SetChange(self) -end +do + local function SetChange(self) + self.alpha_change = self.alpha_to - self.alpha_from + end + + function Alpha:SetFromAlpha(alpha) + self.alpha_from = alpha + SetChange(self) + end + + function Alpha:SetToAlpha(alpha) + self.alpha_to = alpha + SetChange(self) + end end function Alpha:OnUpdate(elapsed) - local properties = self.group.properties + --self.progress = self.smoothing_func(self.time / self.duration).y - self.progress = self.smoothing_func(self.time / self.duration).y + local frame = self.target - local frame = self.group.parent - - frame:SetAlpha(self.alpha_from + self.progress * self.alpha_change) + frame:SetAlpha(self.alpha_from + self.smoothProgress * self.alpha_change) end - diff --git a/animation.lua b/animation.lua index 83ee9b1..ee4a28f 100644 --- a/animation.lua +++ b/animation.lua @@ -69,7 +69,7 @@ function Animation:Stop() end function Animation:IsDone() - return not self.playing + return self.finished end function Animation:IsPlaying() @@ -85,25 +85,32 @@ function Animation:IsStopped() end function Animation:IsDelaying() - return not not self.delay + return self.delaying end function Animation:GetElapsed() - return self.time + local durationTime = self.group.reverse and (self.duration - self.time) or self.time + local elapsed = (self.startdelayTime + durationTime + self.enddelayTime) + if self.group.reverse then + elapsed = self.startdelay + self.enddelay + self.duration - elapsed + end + return elapsed 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) @@ -118,10 +125,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) @@ -152,3 +161,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 \ No newline at end of file diff --git a/group.lua b/group.lua index 08c04dd..163dd18 100644 --- a/group.lua +++ b/group.lua @@ -43,18 +43,22 @@ local function SetScript(self, handler, func) end end - --[[ API --]] function AnimationGroup:Play() - AG:SaveProperties(self) - self.reverse = false self.finishing = false self.order = 0 + for k, v in pairs(self:GetAnimations()) do + v:SaveProperties() + end + + + + repeat AG:PlayGroup(self) @@ -128,7 +132,20 @@ function AnimationGroup:GetLoopState() return self.loop_state end +function AnimationGroup:GetAnimations() + local animations = {} + for i = 0, AG.ORDER_LIMIT - 1, 1 do + for _, animation in next, self.animations[i + 1] or {} do + if animation then + tinsert(animations, animation) + end + end + end + return animations +end + function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) + animation_type = string.upper(string.sub(animation_type, 1, 1)) .. string.lower(string.sub(animation_type, 2)) local animation = AG[animation_type]:Bind(CreateFrame('Frame', name)) animation.group = self @@ -136,13 +153,17 @@ function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) animation.type = animation_type animation.duration = nil animation.progress = nil + animation.smoothProgress = nil + animation.target = animation.group.parent + animation.handlers = { ['OnLoad'] = true, ['OnPlay'] = true, ['OnPaused'] = true, ['OnStop'] = true, ['OnFinished'] = true, - ['OnLoop'] = true + ['OnLoop'] = true, + ['OnStep'] = true, } local default_smoothing = 'LINEAR' @@ -157,13 +178,21 @@ function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) animation._SetScript = animation.SetScript animation.SetScript = animation.__SetScript + animation.properties = { + alpha = nil, + width = nil, + height = nil, + point = {} + } + + animation:SaveProperties() + animation.order = 0 table.insert(self.animations[animation.order + 1], animation) return animation end - --[[ Private --]] @@ -188,19 +217,15 @@ function AnimationGroup:__Initialize(parent) ['OnPaused'] = true, ['OnStop'] = true, ['OnFinished'] = true, - ['OnLoop'] = true + ['OnLoop'] = true, + ['OnStep'] = true, } -- The original implementation claims to support up to 100 orders... yuck! -- Lets keep it at 10 for sanity. self.animations = { {} } - self.properties = { - alpha = nil, - width = nil, - height = nil, - point = {} - } + self._SetScript = self.SetScript self.SetScript = SetScript diff --git a/path.lua b/path.lua index 865ecf8..0881ba9 100644 --- a/path.lua +++ b/path.lua @@ -39,8 +39,8 @@ end -- Adds a new path control point. function Path:CreateControlPoint(name, template, order) local control_point = { name = name, - template = template, - order = order } + template = template, + order = order } table.insert(self.control_points, control_points) end diff --git a/rotation.lua b/rotation.lua index 646c11b..2e0bbfe 100644 --- a/rotation.lua +++ b/rotation.lua @@ -42,26 +42,48 @@ function Rotation:__Initialize() self.origin.y = 0 end +function Rotation:SaveProperties() + self.properties.width = self.target:GetWidth() + self.properties.height = self.target:GetHeight() + local point, relativeRegion, relativePoint, offsetX, offsetY = self.target:GetPoint() + self.properties.point = { point = point or "CENTER", + relativeRegion = relativeRegion or UIParent, + relativePoint = relativePoint or "CENTER", + offsetX = offsetX or 0, + offsetY = offsetY or 0 } +end + +function Rotation:LoadProperties() + if self.properties.width then self.target:SetWidth(self.properties.width) end + if self.properties.height then self.target:SetHeight(self.properties.height) end + + local point = self.properties.point + + if point and type(point) == "table" and table.getn(point) > 1 then + self.target:SetPoint(point.point, point.relativeRegion, point.relativePoint, point.offsetX, point.offsetY) + end +end + local GetRegions = function(self) - return { self.group.parent:GetRegions() } + return { self.target:GetRegions() } end local anchor_coords = { - ['TOP'] = { x = 0, y = 1 }, - ['LEFT'] = { x = -1, y = 0 }, - ['BOTTOM'] = { x = 0, y = -1 }, - ['RIGHT'] = { x = 1, y = 0 }, - ['TOPLEFT'] = { x = -1, y = 1 }, - ['TOPRIGHT'] = { x = 1, y = 1 }, + ['TOP'] = { x = 0, y = 1 }, + ['LEFT'] = { x = -1, y = 0 }, + ['BOTTOM'] = { x = 0, y = -1 }, + ['RIGHT'] = { x = 1, y = 0 }, + ['TOPLEFT'] = { x = -1, y = 1 }, + ['TOPRIGHT'] = { x = 1, y = 1 }, ['BOTTOMLEFT'] = { x = -1, y = -1 }, - ['BOTTOMRIGHT'] = { x = 1, y = -1 }, - ['CENTER'] = { x = 0, y = 0 } + ['BOTTOMRIGHT'] = { x = 1, y = -1 }, + ['CENTER'] = { x = 0, y = 0 } } local frame_corners = { - [1] = { x = -1, y = 1 }, -- upper left - [2] = { x = -1, y = -1 }, -- lower left - [3] = { x = 1, y = 1 }, -- upper right - [4] = { x = 1, y = -1 } -- lower right + [1] = { x = -1, y = 1 }, -- upper left + [2] = { x = -1, y = -1 }, -- lower left + [3] = { x = 1, y = 1 }, -- upper right + [4] = { x = 1, y = -1 } -- lower right } local corners = { 0, 0, 0, 0, 0, 0, 0, 0 } local function GetCoords(self, progress) @@ -69,7 +91,7 @@ local function GetCoords(self, progress) local _cos = cos(rad) local _sin = sin(rad) - local properties = self.group.properties + local properties = self.properties local origin = { x = anchor_coords[self.origin.point].x + @@ -95,10 +117,10 @@ local function GetCoords(self, progress) end function Rotation:OnUpdate(elapsed) - self.progress = self.smoothing_func(self.time / self.duration).y + --self.progress = self.smoothing_func(self.time / self.duration).y local regions = GetRegions(self) - local coords = { GetCoords(self, self.progress) } + local coords = { GetCoords(self, self.smoothProgress) } for _, region in next, regions do if region.GetTexture then region:SetTexCoord(unpack(coords)) @@ -109,20 +131,23 @@ end function Rotation:SetDegrees(degrees) self.radians = (degrees / 360) * 2 * pi end + function Rotation:GetDegrees() return (self.radians / (2 * pi)) * 360 end + function Rotation:SetRadians(radians) self.radians = radians end + function Rotation:GetRadians() return self.radians end function Rotation:SetOrigin(point, offsetX, offsetY) self.origin.point = point - self.origin.x = x - self.origin.y = y + self.origin.x = offsetX + self.origin.y = offsetY end function Rotation:GetOrigin() diff --git a/scale.lua b/scale.lua index 3b08a86..6b618bf 100644 --- a/scale.lua +++ b/scale.lua @@ -32,21 +32,33 @@ local Scale = AG:New('Scale', AG.Animation) function Scale:__Initialize() self.origin = {} - self.origin.point = nil - self.origin.x = nil - self.origin.y = nil + self.origin.point = "CENTER" + self.origin.x = 0 + self.origin.y = 0 self.scale = {} - self.scale.x = nil - self.scale.y = nil + self.scale.x = 0 + self.scale.y = 0 self.from = {} - self.from.x = 1 - self.from.y = 1 + self.from.x = 0 + self.from.y = 0 +end + +function Scale:SaveProperties() + self.properties.width = self.target:GetWidth() + self.properties.height = self.target:GetHeight() + --printT({"SAVE", self.properties }) +end + +function Scale:LoadProperties() + if self.properties.width then self.target:SetWidth(self.properties.width) end + if self.properties.height then self.target:SetHeight(self.properties.height) end + --printT({"LOAD",self.properties}) end function Scale:SetOrigin(point, offset_x, offset_y) self.origin.point = point - self.origin.x = x - self.origin.y = y + self.origin.x = offset_x + self.origin.y = offset_y end function Scale:GetOrigin() @@ -60,6 +72,11 @@ function Scale:SetScale(x, y) self.scale.y = y end +function Scale:SetToScale(x, y) + self.scale.x = x + self.scale.y = y +end + function Scale:GetScale() local scale = self.scale @@ -78,14 +95,14 @@ function Scale:GetFromScale() end function Scale:OnUpdate(elapsed) - local properties = self.group.properties + local properties = self.properties local from_x = self.from.x local from_y = self.from.y - - self.progress = self.smoothing_func(self.time / self.duration).y - local frame = self.group.parent - frame:SetWidth(properties.width * (from_x + self.progress * (self.scale.x - from_x))) - frame:SetHeight(properties.height * (from_y + self.progress * (self.scale.y - from_y))) + -- self.progress = self.smoothing_func(self.time / self.duration).y + + local frame = self.target + frame:SetWidth(properties.width * (from_x + self.smoothProgress * (self.scale.x - from_x))) + frame:SetHeight(properties.height * (from_y + self.smoothProgress * (self.scale.y - from_y))) end diff --git a/translation.lua b/translation.lua index f8f972f..388584d 100644 --- a/translation.lua +++ b/translation.lua @@ -36,6 +36,22 @@ function Translation:__Initialize() self.offset.y = nil end +function Translation:SaveProperties() + local point, relativeRegion, relativePoint, offsetX, offsetY = self.target:GetPoint() + self.properties.point = { point = point or "CENTER", + relativeRegion = relativeRegion or UIParent, + relativePoint = relativePoint or "CENTER", + offsetX = offsetX or 0, + offsetY = offsetY or 0 } +end + +function Translation:LoadProperties() + local point = self.properties.point + if point and type(point) == "table" and table.getn(point) > 1 then + self.target:SetPoint(point.point, point.relativeRegion, point.relativePoint, point.offsetX, point.offsetY) + end +end + function Translation:SetOffset(x, y) self.offset.x = x self.offset.y = y @@ -46,14 +62,14 @@ function Translation:GetOffset() end function Translation:OnUpdate(elapsed) - self.progress = self.smoothing_func(self.time / self.duration).y + --self.progress = self.smoothing_func(self.time / self.duration).y - local frame = self.group.parent + local frame = self.target - local point = self.group.properties.point + local point = self.properties.point frame:ClearAllPoints() - frame:SetPoint(point[1], point[2], point[3], - point[4] + self.progress * self.offset.x, - point[5] + self.progress * self.offset.y) + frame:SetPoint(point.point, point.relativeRegion, point.relativePoint, + point.offsetX + self.smoothProgress * self.offset.x, + point.offsetY + self.smoothProgress * self.offset.y) end From e9096334aec8a5034a009fa24146477291411ae9 Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 3 Oct 2022 10:19:20 +0700 Subject: [PATCH 11/22] some cleanup --- AnimationGroup.lua | 19 +++++++++++++------ group.lua | 13 ++----------- scale.lua | 2 -- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 232a859..5b31cf6 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -130,10 +130,22 @@ function AG:StopGroup(group) AG:Stop(animation) end end - + AG:LoadProperties(group) group.playing = false end +function AG:SaveProperties(group) + for k, v in pairs(group:GetAnimations()) do + v:SaveProperties() + end +end + +function AG:LoadProperties(group) + for k, v in pairs(group:GetAnimations()) do + v:LoadProperties() + end +end + function AG:Stop(animation) animation.time = 0 @@ -146,7 +158,6 @@ end function AG:Play(animation) if not animation.playing and animation.target:IsVisible() then - -- printT({"AG:PLAY", animation:GetName()}) animation.time = animation.group.reverse and animation.duration or 0 animation.startdelayTime = 0 animation.enddelayTime = 0 @@ -263,10 +274,6 @@ function AG:Fire(group, animation, signal) if (signal == 'Finished' and all_finished and shift) then if (group.finishing and (bouncing or repeating)) or not (bouncing or repeating) then AG:StopGroup(group) - for k, v in pairs(group:GetAnimations()) do - v:LoadProperties() - end - --printT({"CallBack", 'OnFinished'}) group_func = group.handlers['OnFinished'] table.insert(args, group.finishing) end diff --git a/group.lua b/group.lua index 163dd18..b879e00 100644 --- a/group.lua +++ b/group.lua @@ -48,17 +48,12 @@ end --]] function AnimationGroup:Play() + AG:SaveProperties(self) + self.reverse = false self.finishing = false self.order = 0 - for k, v in pairs(self:GetAnimations()) do - v:SaveProperties() - end - - - - repeat AG:PlayGroup(self) @@ -162,7 +157,6 @@ function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) ['OnPaused'] = true, ['OnStop'] = true, ['OnFinished'] = true, - ['OnLoop'] = true, ['OnStep'] = true, } @@ -218,15 +212,12 @@ function AnimationGroup:__Initialize(parent) ['OnStop'] = true, ['OnFinished'] = true, ['OnLoop'] = true, - ['OnStep'] = true, } -- The original implementation claims to support up to 100 orders... yuck! -- Lets keep it at 10 for sanity. self.animations = { {} } - - self._SetScript = self.SetScript self.SetScript = SetScript end diff --git a/scale.lua b/scale.lua index 6b618bf..ab2eed0 100644 --- a/scale.lua +++ b/scale.lua @@ -46,13 +46,11 @@ end function Scale:SaveProperties() self.properties.width = self.target:GetWidth() self.properties.height = self.target:GetHeight() - --printT({"SAVE", self.properties }) end function Scale:LoadProperties() if self.properties.width then self.target:SetWidth(self.properties.width) end if self.properties.height then self.target:SetHeight(self.properties.height) end - --printT({"LOAD",self.properties}) end function Scale:SetOrigin(point, offset_x, offset_y) From d94a9534107aa7c71a8f9d3fb326595ac2a12ce4 Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 3 Oct 2022 12:34:05 +0700 Subject: [PATCH 12/22] fix some bugs --- rotation.lua | 5 +---- translation.lua | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/rotation.lua b/rotation.lua index 2e0bbfe..167da19 100644 --- a/rotation.lua +++ b/rotation.lua @@ -58,11 +58,8 @@ function Rotation:LoadProperties() if self.properties.height then self.target:SetHeight(self.properties.height) end local point = self.properties.point - - if point and type(point) == "table" and table.getn(point) > 1 then - self.target:SetPoint(point.point, point.relativeRegion, point.relativePoint, point.offsetX, point.offsetY) + self.target:SetPoint(point.point, point.relativeRegion, point.relativePoint, point.offsetX, point.offsetY) end -end local GetRegions = function(self) return { self.target:GetRegions() } diff --git a/translation.lua b/translation.lua index 388584d..8d697d6 100644 --- a/translation.lua +++ b/translation.lua @@ -47,9 +47,7 @@ end function Translation:LoadProperties() local point = self.properties.point - if point and type(point) == "table" and table.getn(point) > 1 then - self.target:SetPoint(point.point, point.relativeRegion, point.relativePoint, point.offsetX, point.offsetY) - end + self.target:SetPoint(point.point, point.relativeRegion, point.relativePoint, point.offsetX, point.offsetY) end function Translation:SetOffset(x, y) From 63e39e385816c813ce14136c1b552ea779eea550 Mon Sep 17 00:00:00 2001 From: LaYt Date: Mon, 3 Oct 2022 12:52:50 +0700 Subject: [PATCH 13/22] fix logic bug --- AnimationGroup.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 5b31cf6..c4ab778 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -130,7 +130,7 @@ function AG:StopGroup(group) AG:Stop(animation) end end - AG:LoadProperties(group) + self:LoadProperties(group) group.playing = false end From 8c727bcfcd5d92c3b39e353d1a5d2f57c24ef887 Mon Sep 17 00:00:00 2001 From: LaYt Date: Tue, 4 Oct 2022 18:26:59 +0700 Subject: [PATCH 14/22] Found solution for 'OnUpdate' script --- AnimationGroup.lua | 10 +++++----- group.lua | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index c4ab778..da29816 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -24,7 +24,7 @@ if not LibStub then return end -local MAJOR_VERSION, MINOR_VERSION = 'AnimationGroup-1.0', '1520787712-0b498e1' +local MAJOR_VERSION, MINOR_VERSION = 'AnimationGroup-1.0', '1520787716-0b498e1' -- Probably not a release if not string.find(MINOR_VERSION, '%d+') then MINOR_VERSION = 0 end @@ -93,8 +93,8 @@ local function OnUpdate(self, elapsed) end end - if type(self.handlers["OnStep"]) == 'function' then - self.handlers["OnStep"](self, elapsed) + if type(self.handlers["OnUpdate"]) == 'function' then + self.handlers["OnUpdate"](self, elapsed) end -- Temporary until all animation types are implemented @@ -150,7 +150,7 @@ function AG:Stop(animation) animation.time = 0 if animation.OnUpdate then - animation:SetScript('OnUpdate', nil) + animation:_SetScript('OnUpdate', nil) end animation.playing = false @@ -164,7 +164,7 @@ function AG:Play(animation) 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) end animation.paused = false diff --git a/group.lua b/group.lua index b879e00..3d6e182 100644 --- a/group.lua +++ b/group.lua @@ -157,7 +157,7 @@ function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) ['OnPaused'] = true, ['OnStop'] = true, ['OnFinished'] = true, - ['OnStep'] = true, + ['OnUpdate'] = true, } local default_smoothing = 'LINEAR' From 1fe90f0cd147ff603afe208541ffdc62ed056fcf Mon Sep 17 00:00:00 2001 From: LaYt Date: Wed, 5 Oct 2022 22:47:03 +0700 Subject: [PATCH 15/22] some clean up --- animation.lua | 7 ++----- curves.lua | 15 +++++++-------- rotation.lua | 2 +- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/animation.lua b/animation.lua index ee4a28f..543ecc9 100644 --- a/animation.lua +++ b/animation.lua @@ -43,7 +43,6 @@ function Animation:__SetScript(handler, func) end end - --[[ API --]] @@ -54,14 +53,12 @@ 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) @@ -90,7 +87,7 @@ end function Animation:GetElapsed() local durationTime = self.group.reverse and (self.duration - self.time) or self.time - local elapsed = (self.startdelayTime + durationTime + self.enddelayTime) + local elapsed = (self.startdelayTime + durationTime + self.enddelayTime) if self.group.reverse then elapsed = self.startdelay + self.enddelay + self.duration - elapsed end @@ -169,4 +166,4 @@ end function Animation:GetTarget() return self.target -end \ No newline at end of file +end diff --git a/curves.lua b/curves.lua index c948fea..07c1850 100644 --- a/curves.lua +++ b/curves.lua @@ -42,24 +42,23 @@ end local mt = { __add = Point.Add, - __mul = Point.Multiply } + __mul = Point.Multiply +} function Point:New(x, y) - return setmetatable({x = x, y = y}, mt) + return setmetatable({ x = x, y = y }, mt) end local function QuadraticBezier(p_0, p_1, p_2) return function(t) - return - (1 - t) * ((1 - t) * p_0 + t * p_1) + + return (1 - t) * ((1 - t) * p_0 + t * p_1) + t * ((1 - t) * p_1 + t * p_2) end end local function CubicBezier(p_0, p_1, p_2, p_3) return function(t) - return - (1 - t) * QuadraticBezier(p_0, p_1, p_2)(t) + + return (1 - t) * QuadraticBezier(p_0, p_1, p_2)(t) + t * QuadraticBezier(p_1, p_2, p_3)(t) end end @@ -92,7 +91,7 @@ local CubicBezierEaseInOut = function() end local Linear = function(t) - return {x = 0, y = t} + return { x = 0, y = t } end Curves.curves = { @@ -103,4 +102,4 @@ Curves.curves = { ['LINEAR'] = Linear } -setmetatable(Curves, {__index = Curves.curves }) +setmetatable(Curves, { __index = Curves.curves }) diff --git a/rotation.lua b/rotation.lua index 167da19..c42205c 100644 --- a/rotation.lua +++ b/rotation.lua @@ -59,7 +59,7 @@ function Rotation:LoadProperties() local point = self.properties.point self.target:SetPoint(point.point, point.relativeRegion, point.relativePoint, point.offsetX, point.offsetY) - end +end local GetRegions = function(self) return { self.target:GetRegions() } From 4f8b9152570c6acbc78ec2b5fe9021067702edeb Mon Sep 17 00:00:00 2001 From: LaYt Date: Tue, 11 Oct 2022 18:04:28 +0700 Subject: [PATCH 16/22] Update AnimationGroup.lua Co-authored-by: Martin Jesper Low Madsen --- AnimationGroup.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index da29816..b1909e3 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -24,7 +24,7 @@ if not LibStub then return end -local MAJOR_VERSION, MINOR_VERSION = 'AnimationGroup-1.0', '1520787716-0b498e1' +local MAJOR_VERSION, MINOR_VERSION = 'AnimationGroup-1.0', '$Format:%ct-%h$' -- Probably not a release if not string.find(MINOR_VERSION, '%d+') then MINOR_VERSION = 0 end From 94fda5061e01c2e91072435d6b32bb0e0cac6f60 Mon Sep 17 00:00:00 2001 From: LaYt Date: Tue, 11 Oct 2022 18:31:47 +0700 Subject: [PATCH 17/22] Update AnimationGroup.lua Co-authored-by: Martin Jesper Low Madsen --- AnimationGroup.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index b1909e3..0276099 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -239,7 +239,7 @@ 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 = bouncing and (not group.reverse) or false + group.reverse = bouncing and (not group.reverse) AG:PlayGroup(group) group_func = group.handlers['OnLoop'] table.insert(args, group.reverse and 'REVERSE' or 'FORWARD') From 2b586199b9eca4b971d23d8d953b5c57d050fa87 Mon Sep 17 00:00:00 2001 From: LaYt Date: Tue, 11 Oct 2022 18:55:12 +0700 Subject: [PATCH 18/22] Naming and more --- AnimationGroup.lua | 20 ++++++++++---------- animation.lua | 12 ++++++------ group.lua | 8 +++++--- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 0276099..3bd01d0 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -66,10 +66,10 @@ local function OnUpdate(self, elapsed) end self.delaying = true - if not self.group.reverse and self.startdelay and self.startdelayTime < self.startdelay then - self.startdelayTime = self.startdelayTime + elapsed - elseif self.group.reverse and self.enddelay and self.enddelayTime < self.enddelay then - self.enddelayTime = self.enddelayTime + elapsed + if not self.group.reverse and self.startDelay and self.startDelayTime < self.startDelay then + self.startDelayTime = self.startDelayTime + elapsed + elseif self.group.reverse and self.endDelay and self.endDelayTime < self.endDelay then + self.endDelayTime = self.endDelayTime + elapsed elseif (not self.group.reverse and self.time < self.duration) or (self.group.reverse and self.time > 0) then self.time = self.time + (self.group.reverse and -elapsed or elapsed) self.delaying = false @@ -80,11 +80,11 @@ local function OnUpdate(self, elapsed) end if self.time > self.duration or (self.group.reverse and self.time < 0) then - if self.group.reverse and self.startdelay and self.startdelayTime < self.startdelay then - self.startdelayTime = self.startdelayTime + elapsed + if self.group.reverse and self.startDelay and self.startDelayTime < self.startDelay then + self.startDelayTime = self.startDelayTime + elapsed self.delaying = true - elseif not self.group.reverse and self.enddelay and self.enddelayTime < self.enddelay then - self.enddelayTime = self.enddelayTime + elapsed + elseif not self.group.reverse and self.endDelay and self.endDelayTime < self.endDelay then + self.endDelayTime = self.endDelayTime + elapsed self.delaying = true else AG:Stop(self) @@ -159,8 +159,8 @@ end function AG:Play(animation) if not animation.playing and animation.target:IsVisible() then animation.time = animation.group.reverse and animation.duration or 0 - animation.startdelayTime = 0 - animation.enddelayTime = 0 + animation.startDelayTime = 0 + animation.endDelayTime = 0 animation.progress = 0 animation.smoothProgress = 0 animation.playing = true diff --git a/animation.lua b/animation.lua index 543ecc9..725a2fc 100644 --- a/animation.lua +++ b/animation.lua @@ -87,27 +87,27 @@ end function Animation:GetElapsed() local durationTime = self.group.reverse and (self.duration - self.time) or self.time - local elapsed = (self.startdelayTime + durationTime + self.enddelayTime) + local elapsed = (self.startDelayTime + durationTime + self.endDelayTime) if self.group.reverse then - elapsed = self.startdelay + self.enddelay + self.duration - elapsed + elapsed = self.startDelay + self.endDelay + self.duration - elapsed end return elapsed end function Animation:SetStartDelay(delay_sec) - self.startdelay = delay_sec + self.startDelay = delay_sec end function Animation:GetStartDelay() - return self.startdelay + return self.startDelay end function Animation:SetEndDelay(delay_sec) - self.enddelay = delay_sec + self.endDelay = delay_sec end function Animation:GetEndDelay() - return self.enddelay + return self.endDelay end function Animation:SetDuration(duration) diff --git a/group.lua b/group.lua index 3d6e182..b62ee53 100644 --- a/group.lua +++ b/group.lua @@ -130,9 +130,11 @@ end function AnimationGroup:GetAnimations() local animations = {} for i = 0, AG.ORDER_LIMIT - 1, 1 do - for _, animation in next, self.animations[i + 1] or {} do - if animation then - tinsert(animations, animation) + if self.animations[i + 1] then + for _, animation in next, self.animations[i + 1] do + if animation then + tinsert(animations, animation) + end end end end From b8f296a0e3adbee2a61094e7a5425b80ee8eb4c7 Mon Sep 17 00:00:00 2001 From: LaYt Date: Tue, 11 Oct 2022 19:14:42 +0700 Subject: [PATCH 19/22] condition's clearing --- AnimationGroup.lua | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 3bd01d0..1e7d1a6 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -64,13 +64,18 @@ local function OnUpdate(self, elapsed) if self.paused then return end + local reverse = self.group.reverse + local in_start_delay = self.startDelay and self.startDelayTime < self.startDelay + local in_end_delay = self.endDelay and self.endDelayTime < self.endDelay + local in_progress = not reverse and self.time < self.duration + local in_progress_revers = reverse and 0 < self.time self.delaying = true - if not self.group.reverse and self.startDelay and self.startDelayTime < self.startDelay then + if not reverse and in_start_delay then self.startDelayTime = self.startDelayTime + elapsed - elseif self.group.reverse and self.endDelay and self.endDelayTime < self.endDelay then + elseif reverse and in_end_delay then self.endDelayTime = self.endDelayTime + elapsed - elseif (not self.group.reverse and self.time < self.duration) or (self.group.reverse and self.time > 0) then + elseif (in_progress) or (in_progress_revers) then self.time = self.time + (self.group.reverse and -elapsed or elapsed) self.delaying = false self.progress = self.time / self.duration @@ -80,10 +85,10 @@ local function OnUpdate(self, elapsed) end if self.time > self.duration or (self.group.reverse and self.time < 0) then - if self.group.reverse and self.startDelay and self.startDelayTime < self.startDelay then + if reverse and in_start_delay then self.startDelayTime = self.startDelayTime + elapsed self.delaying = true - elseif not self.group.reverse and self.endDelay and self.endDelayTime < self.endDelay then + elseif not reverse and in_end_delay then self.endDelayTime = self.endDelayTime + elapsed self.delaying = true else @@ -93,11 +98,12 @@ local function OnUpdate(self, elapsed) end end + -- 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 not self.delaying and self.OnUpdate then self:OnUpdate(elapsed) end From d635a1fff557fdf20bb6da27065c269621444857 Mon Sep 17 00:00:00 2001 From: LaYt Date: Wed, 12 Oct 2022 23:32:15 +0700 Subject: [PATCH 20/22] Apply suggestions from code review Co-authored-by: Martin Jesper Low Madsen --- AnimationGroup.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 1e7d1a6..dc6dcce 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -64,18 +64,18 @@ local function OnUpdate(self, elapsed) if self.paused then return end - local reverse = self.group.reverse - local in_start_delay = self.startDelay and self.startDelayTime < self.startDelay - local in_end_delay = self.endDelay and self.endDelayTime < self.endDelay + local in_reverse = self.group.reverse + local is_start_delayed = self.startDelay and self.startDelayTime < self.startDelay + local is_end_delayed = self.endDelay and self.endDelayTime < self.endDelay local in_progress = not reverse and self.time < self.duration - local in_progress_revers = reverse and 0 < self.time + local in_progress_reverse = reverse and 0 < self.time self.delaying = true if not reverse and in_start_delay then self.startDelayTime = self.startDelayTime + elapsed elseif reverse and in_end_delay then self.endDelayTime = self.endDelayTime + elapsed - elseif (in_progress) or (in_progress_revers) then + elseif in_progress or in_progress_reverse then self.time = self.time + (self.group.reverse and -elapsed or elapsed) self.delaying = false self.progress = self.time / self.duration From 43264f826f3fc07da094b8e41d2e25498444194f Mon Sep 17 00:00:00 2001 From: LaYt Date: Thu, 13 Oct 2022 00:53:29 +0700 Subject: [PATCH 21/22] Reworked OnUpdate and removed work comments some naming --- AnimationGroup.lua | 31 ++++++++++++------------------- alpha.lua | 2 -- animation.lua | 2 +- rotation.lua | 2 -- scale.lua | 2 -- translation.lua | 2 -- 6 files changed, 13 insertions(+), 28 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index dc6dcce..9dd7d80 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -67,36 +67,31 @@ local function OnUpdate(self, elapsed) local in_reverse = self.group.reverse local is_start_delayed = self.startDelay and self.startDelayTime < self.startDelay local is_end_delayed = self.endDelay and self.endDelayTime < self.endDelay - local in_progress = not reverse and self.time < self.duration - local in_progress_reverse = reverse and 0 < self.time - self.delaying = true + local in_progress = not in_reverse and self.time < self.duration + local in_progress_reverse = in_reverse and 0 < self.time + local is_animation_complete = self.duration < self.time or (in_reverse and self.time < 0) - if not reverse and in_start_delay then + self.delayed = true + + if (not in_reverse and is_start_delayed) or (is_animation_complete and in_reverse and is_start_delayed) then self.startDelayTime = self.startDelayTime + elapsed - elseif reverse and in_end_delay then + elseif (in_reverse and is_end_delayed) or (is_animation_complete and not in_reverse and is_end_delayed) then self.endDelayTime = self.endDelayTime + elapsed elseif in_progress or in_progress_reverse then self.time = self.time + (self.group.reverse and -elapsed or elapsed) - self.delaying = false self.progress = self.time / self.duration self.progress = self.progress < 0 and 0 or self.progress self.progress = self.progress > 1 and 1 or self.progress self.smoothProgress = self.smoothing_func(self.progress).y + self.delayed = false end - if self.time > self.duration or (self.group.reverse and self.time < 0) then - if reverse and in_start_delay then - self.startDelayTime = self.startDelayTime + elapsed - self.delaying = true - elseif not reverse and in_end_delay then - self.endDelayTime = self.endDelayTime + elapsed - self.delaying = true - else + is_animation_complete = self.duration < self.time or (in_reverse and self.time < 0) + if is_animation_complete and not( in_reverse and is_start_delayed) + and not (not in_reverse and is_end_delayed) then AG:Stop(self) AG:Fire(self.group, self, 'Finished') return - end - end -- Calling user's callback_handlers if type(self.handlers["OnUpdate"]) == 'function' then @@ -104,7 +99,7 @@ local function OnUpdate(self, elapsed) end -- Calling animations OnUpdate - if not self.delaying and self.OnUpdate then + if not self.delayed and self.OnUpdate then self:OnUpdate(elapsed) end end @@ -293,11 +288,9 @@ function AG:Fire(group, animation, signal) -- We `bounce' if the boundary orders have no animations if (signal == 'Finished' and not group.playing and shift and (not group.finishing) and (bouncing or repeating)) then - --group.shifted = true if repeating then group.order = -1 else - --group.order = group.order - (group.reverse and -1 or 1) group.reverse = not group.reverse end group_func = group.handlers['OnLoop'] diff --git a/alpha.lua b/alpha.lua index e099f48..14f4ea3 100644 --- a/alpha.lua +++ b/alpha.lua @@ -69,8 +69,6 @@ do end function Alpha:OnUpdate(elapsed) - --self.progress = self.smoothing_func(self.time / self.duration).y - local frame = self.target frame:SetAlpha(self.alpha_from + self.smoothProgress * self.alpha_change) diff --git a/animation.lua b/animation.lua index 725a2fc..c312575 100644 --- a/animation.lua +++ b/animation.lua @@ -82,7 +82,7 @@ function Animation:IsStopped() end function Animation:IsDelaying() - return self.delaying + return self.delayed end function Animation:GetElapsed() diff --git a/rotation.lua b/rotation.lua index c42205c..58e7503 100644 --- a/rotation.lua +++ b/rotation.lua @@ -114,8 +114,6 @@ local function GetCoords(self, progress) end function Rotation:OnUpdate(elapsed) - --self.progress = self.smoothing_func(self.time / self.duration).y - local regions = GetRegions(self) local coords = { GetCoords(self, self.smoothProgress) } for _, region in next, regions do diff --git a/scale.lua b/scale.lua index ab2eed0..773570d 100644 --- a/scale.lua +++ b/scale.lua @@ -98,8 +98,6 @@ function Scale:OnUpdate(elapsed) local from_x = self.from.x local from_y = self.from.y - -- self.progress = self.smoothing_func(self.time / self.duration).y - local frame = self.target frame:SetWidth(properties.width * (from_x + self.smoothProgress * (self.scale.x - from_x))) frame:SetHeight(properties.height * (from_y + self.smoothProgress * (self.scale.y - from_y))) diff --git a/translation.lua b/translation.lua index 8d697d6..60e186e 100644 --- a/translation.lua +++ b/translation.lua @@ -60,8 +60,6 @@ function Translation:GetOffset() end function Translation:OnUpdate(elapsed) - --self.progress = self.smoothing_func(self.time / self.duration).y - local frame = self.target local point = self.properties.point From 4385f7d410be664e49eed2f199beace4d5b179e8 Mon Sep 17 00:00:00 2001 From: LaYt Date: Thu, 13 Oct 2022 12:25:24 +0700 Subject: [PATCH 22/22] 1 more rework of OnUpdate --- AnimationGroup.lua | 54 ++++++++++++++++++++++------------------------ animation.lua | 7 +----- group.lua | 9 +++++--- 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/AnimationGroup.lua b/AnimationGroup.lua index 9dd7d80..90c55e6 100644 --- a/AnimationGroup.lua +++ b/AnimationGroup.lua @@ -65,43 +65,42 @@ local function OnUpdate(self, elapsed) return end local in_reverse = self.group.reverse - local is_start_delayed = self.startDelay and self.startDelayTime < self.startDelay - local is_end_delayed = self.endDelay and self.endDelayTime < self.endDelay - local in_progress = not in_reverse and self.time < self.duration - local in_progress_reverse = in_reverse and 0 < self.time - local is_animation_complete = self.duration < self.time or (in_reverse and self.time < 0) - - self.delayed = true - - if (not in_reverse and is_start_delayed) or (is_animation_complete and in_reverse and is_start_delayed) then - self.startDelayTime = self.startDelayTime + elapsed - elseif (in_reverse and is_end_delayed) or (is_animation_complete and not in_reverse and is_end_delayed) then - self.endDelayTime = self.endDelayTime + elapsed - elseif in_progress or in_progress_reverse then - self.time = self.time + (self.group.reverse and -elapsed or elapsed) - self.progress = self.time / self.duration + + self.time = self.time + (in_reverse and -elapsed or elapsed) + local animation_time = self.time - self.startDelay + + 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 + + -- 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 - self.delayed = false - end - is_animation_complete = self.duration < self.time or (in_reverse and self.time < 0) - if is_animation_complete and not( in_reverse and is_start_delayed) - and not (not in_reverse and is_end_delayed) then - AG:Stop(self) - AG:Fire(self.group, self, 'Finished') - return - end -- Calling user's callback_handlers if type(self.handlers["OnUpdate"]) == 'function' then self.handlers["OnUpdate"](self, elapsed) end -- Calling animations OnUpdate - if not self.delayed and self.OnUpdate then + if self.OnUpdate then self:OnUpdate(elapsed) end + + -- Stop animation after last update + if is_animation_complete then + AG:Stop(self) + AG:Fire(self.group, self, 'Finished') + return + end + end --[[ @@ -159,9 +158,8 @@ end function AG:Play(animation) if not animation.playing and animation.target:IsVisible() then - animation.time = animation.group.reverse and animation.duration or 0 - animation.startDelayTime = 0 - animation.endDelayTime = 0 + 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 diff --git a/animation.lua b/animation.lua index c312575..c17f471 100644 --- a/animation.lua +++ b/animation.lua @@ -86,12 +86,7 @@ function Animation:IsDelaying() end function Animation:GetElapsed() - local durationTime = self.group.reverse and (self.duration - self.time) or self.time - local elapsed = (self.startDelayTime + durationTime + self.endDelayTime) - if self.group.reverse then - elapsed = self.startDelay + self.endDelay + self.duration - elapsed - end - return elapsed + return self.time < 0 and 0 or self.totalTime < self.time and self.totalTime or self.time end function Animation:SetStartDelay(delay_sec) diff --git a/group.lua b/group.lua index b62ee53..cf4218c 100644 --- a/group.lua +++ b/group.lua @@ -148,9 +148,12 @@ function AnimationGroup:CreateAnimation(animation_type, name, inherits_from) animation.group = self animation:SetParent(self) animation.type = animation_type - animation.duration = nil - animation.progress = nil - animation.smoothProgress = nil + animation.duration = 0 + animation.progress = 0 + animation.smoothProgress = 0 + animation.startDelay = 0 + animation.endDelay = 0 + animation.totalTime = 0 animation.target = animation.group.parent animation.handlers = {