2.1 update

Pawkette [03-26-14 - 15:28]
2.1 update
Filename
LibAnimation.lua
diff --git a/LibAnimation.lua b/LibAnimation.lua
index d4de42b..16323dd 100644
--- a/LibAnimation.lua
+++ b/LibAnimation.lua
@@ -30,15 +30,21 @@ THE SOFTWARE.
 ----------------------------------------------------
 if ( not LibStub ) then return end

-local kName, kVersion = 'LibAnimation-1.0', 1.2
-local LibAnimation = LibStub:NewLibrary( kName, kVersion )
+local kName, kVersion   = 'LibAnimation-1.0', 2.1
+local LibAnimation      = LibStub:NewLibrary( kName, kVersion )
 if ( not LibAnimation ) then return end

-local AnimationMgr = ANIMATION_MANAGER
-local defaultEase = ZO_LinearEase
+local AnimationMgr          = ANIMATION_MANAGER
+local defaultEase           = ZO_LinearEase
+
+local ANIMATION_SIZE        = ANIMATION_SIZE
+local ANIMATION_TRANSLATE   = ANIMATION_TRANSLATE
+local ANIMATION_SCALE       = ANIMATION_SCALE
+local ANIMATION_ALPHA       = ANIMATION_ALPHA
+local _

 --- Create a new animation for control
--- @tparam table control the gui element to animate
+-- @tparam table control (optional)
 -- @tparam number playbackType (optional)
 -- @tparam number loopCount (optional)
 -- @treturn LibAnimation object
@@ -60,7 +66,7 @@ function LibAnimation:New( control, playbackType, loopCount )
 end

 --- Animation Constructor
--- @tparam table control
+-- @tparam table control (optional)
 -- @tparam number playbackType (optional)
 -- @tparam number loopCount (optional)
 function LibAnimation:Initialize( control, playbackType, loopCount )
@@ -69,6 +75,14 @@ function LibAnimation:Initialize( control, playbackType, loopCount )
     self.timeline:SetPlaybackType( playbackType, loopCount )
 end

+function LibAnimation:Apply( control )
+    self.timeline:ApplyAllAnimationsToControl( control )
+end
+
+function LibAnimation:SetHandler( ... )
+    self.timeline:SetHandler( ... )
+end
+
 --- Allows you to add a callback at a certain point in the timeline
 -- @tparam function fn
 -- @tparam number delay how long to wait before calling
@@ -103,34 +117,73 @@ function LibAnimation:Backward()
     self.timeline:PlayBackward()
 end

+function LibAnimation:SetUserData( data )
+    self._udata = data
+end
+
+function LibAnimation:GetUserData()
+    return self._udata
+end
+
 --- Get's the existing animation or creates a new one
 -- @tparam number animType
 -- @tparam number delay (optional)
 -- @tresult animation
-function LibAnimation:Insert( animType, delay )
-    return self.timeline:InsertAnimation( animType, self.control, delay or 0 )
+function LibAnimation:Insert( animType, duration, delay, anchorIndex, fn )
+    local anim = self.timeline:InsertAnimation( animType, self.control, delay or 0 )
+
+    anim:SetDuration( duration or 1 )
+    anim:SetEasingFunction( fn or defaultEase )
+
+    if ( animType == ANIMATION_TRANSLATE ) then
+        anim:SetAnchorIndex( anchorIndex or 0 )
+    end
+    return anim
 end

 --- Create new translate animation
+-- @tparam number xorigin
+-- @tparam number yorigin
 -- @tparam number xoffset
 -- @tparam number yoffset
 -- @tparam number duration
 -- @tparam number delay (optional)
 -- @tparam number anchorIndex (optional)
 -- @tparam function fn easing function (optional)
-function LibAnimation:TranslateTo( xoffset, yoffset, duration, delay, anchorIndex, fn )
+function LibAnimation:TranslateToFrom( xorigin, yorigin, xoffset, yoffset, duration, delay, anchorIndex, fn )
     self:Stop()
+    local anim = self:Insert( ANIMATION_TRANSLATE, duration, delay, anchorIndex, fn )
+    anim:SetStartOffsetX( xorigin )
+    anim:SetStartOffsetY( yorigin )
+    anim:SetEndOffsetX( xoffset )
+    anim:SetEndOffsetY( yoffset )
+end

-    local anim = self:Insert( ANIMATION_TRANSLATE, delay )
+--- Create new translate animation
+-- @tparam number xoffset
+-- @tparam number yoffset
+-- @tparam number duration
+-- @tparam number delay (optional)
+-- @tparam number anchorIndex (optional)
+-- @tparam function fn easing function (optional)
+function LibAnimation:TranslateTo( xoffset, yoffset, duration, delay, anchorIndex, fn )
     local _, _, _, _, offsX, offsY = self.control:GetAnchor( anchorIndex or 0 )
+    self:TranslateToFrom( offsX, offsY, xoffset, yoffset, duration, delay, anchorIndex, fn )
+end

-    anim:SetDuration( duration or 1 )
-    anim:SetEasingFunction( fn or defaultEase )
-    anim:SetStartOffsetX( offsX )
-    anim:SetStartOffsetY( offsY )
-    anim:SetEndOffsetX( xoffset )
-    anim:SetEndOffsetY( yoffset  )
-    anim:SetAnchorIndex( anchorIndex or 0 )
+--- Create a new size animation
+-- @tparam number startWidth
+-- @tparam number startHeight
+-- @tparam number width target width
+-- @tparam number height target height
+-- @tparam number duration
+-- @tparam number delay (optional)
+-- @tparam function fn easing function (optional)
+function LibAnimation:ResizeToFrom( startWidth, startHeight, width, height, duration, delay, fn )
+    self:Stop()
+    local anim = self:Insert( ANIMATION_SIZE, duration, delay, nil, fn )
+    anim:SetHeightStartAndEnd( startHeight, height )
+    anim:SetWidthStartAndEnd( startWidth, width )
 end

 --- Create a new size animation
@@ -140,14 +193,20 @@ end
 -- @tparam number delay (optional)
 -- @tparam function fn easing function (optional)
 function LibAnimation:ResizeTo( width, height, duration, delay, fn )
-    self:Stop()
+    self:ResizeToFrom( self.control:GetWidth(), self.control:GetHeight(), width, height, duration, delay, fn )
+end

-    local anim = self:Insert( ANIMATION_SIZE, delay )

-    anim:SetDuration( duration or 1 )
-    anim:SetEasingFunction( fn or defaultEase )
-    anim:SetHeightStartAndEnd( self.control:GetHeight(), height )
-    anim:SetWidthStartAndEnd( self.control:GetWidth(), width )
+--- Create a new scale animation
+-- @tparam number startScale
+-- @tparam number scale
+-- @tparam number duration
+-- @tparam number delay (optional)
+-- @tparam function fn easing function (optional)
+function LibAnimation:ScaleToFrom( startScale, scale, duration, delay, fn )
+    self:Stop()
+    local anim = self:Insert( ANIMATION_SCALE, duration, delay, nil, fn )
+    anim:SetScaleValues( startScale, scale )
 end

 --- Create a new scale animation
@@ -156,13 +215,14 @@ end
 -- @tparam number delay (optional)
 -- @tparam function fn easing function (optional)
 function LibAnimation:ScaleTo( scale, duration, delay, fn )
-    self:Stop()
+    self:ScaleToFrom( self.control:GetScale(), scale, duration, delay, fn )
+end

-    local anim = self:Insert( ANIMATION_SCALE, delay )

-    anim:SetDuration( duration or 1 )
-    anim:SetEasingFunction( fn or defaultEase )
-    anim:SetScaleValues( self.control:GetScale(), scale )
+function LibAnimation:AlphaToFrom( startAlpha, alpha, duration, delay, fn )
+    self:Stop()
+    local anim = self:Insert( ANIMATION_ALPHA, duration, delay, nil, fn )
+    anim:SetAlphaValues( startAlpha, alpha )
 end

 --- Create a new alpha animation
@@ -171,13 +231,7 @@ end
 -- @tparam number delay (optional)
 -- @tparam function fn easing function (optional)
 function LibAnimation:AlphaTo( alpha, duration, delay, fn )
-    self:Stop()
-
-    local anim = self:Insert( ANIMATION_ALPHA, delay )
-
-    anim:SetDuration( duration or 1 )
-    anim:SetEasingFunction( fn or defaultEase )
-    anim:SetAlphaValues( self.control:GetAlpha(), alpha )
+    self:AlphaToFrom( self.control:GetAlpha(), alpha, duration, delay, fn )
 end

 --- Create a new scroll animation