[Tizen] Implement partial update
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-animator.h
old mode 100644 (file)
new mode 100755 (executable)
index 13c245f..cbf0842
@@ -1,8 +1,8 @@
-#ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
-#define __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
+#ifndef DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H
+#define DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H
 
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 
 // EXTERNAL INCLUDES
-#include <boost/function.hpp>
+#include <cmath>
 
 // INTERNAL INCLUDES
-#include <dali/internal/common/owner-container.h>
-#include <dali/internal/event/animation/key-frames-impl.h>
-#include <dali/internal/event/animation/path-impl.h>
-#include <dali/internal/update/nodes/node.h>
-#include <dali/internal/update/common/property-base.h>
-#include <dali/public-api/animation/alpha-functions.h>
+#include <dali/public-api/animation/alpha-function.h>
 #include <dali/public-api/animation/animation.h>
 #include <dali/public-api/animation/time-period.h>
+#include <dali/public-api/common/constants.h>
 #include <dali/public-api/common/dali-common.h>
 #include <dali/public-api/math/quaternion.h>
 #include <dali/public-api/math/radian.h>
+#include <dali/devel-api/common/owner-container.h>
+#include <dali/internal/event/animation/key-frames-impl.h>
+#include <dali/internal/event/animation/path-impl.h>
+#include <dali/internal/update/nodes/node.h>
+#include <dali/internal/update/common/property-base.h>
+#include <dali/internal/update/animation/property-accessor.h>
+#include <dali/integration-api/debug.h>
 
 namespace Dali
 {
@@ -40,35 +43,115 @@ namespace Dali
 namespace Internal
 {
 
-namespace SceneGraph
+using Interpolation = Dali::Animation::Interpolation;
+
+/**
+ * AnimatorFunction base class.
+ * Needs to be declared first so AnimatorBase knows about it's destructor
+ * All update functions must inherit from AnimatorFunctionBase and overload the appropiate "()" operator
+ */
+struct AnimatorFunctionBase
 {
+  /**
+   * Constructor
+   */
+  AnimatorFunctionBase() {}
 
-class AnimatorBase;
+  /*
+   * Virtual destructor (Intended as base class)
+   */
+  virtual ~AnimatorFunctionBase() {}
 
-typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
+  ///Stub "()" operators.
+  virtual bool operator()(float progress, const bool& property)
+  {
+    return property;
+  }
+
+  virtual float operator()(float progress, const int32_t& property)
+  {
+    return static_cast<float>( property );
+  }
+
+  virtual float operator()(float progress, const float& property)
+  {
+    return property;
+  }
 
-typedef AnimatorContainer::Iterator AnimatorIter;
-typedef AnimatorContainer::ConstIterator AnimatorConstIter;
+  virtual Vector2 operator()(float progress, const Vector2& property)
+  {
+    return property;
+  }
+
+  virtual Vector3 operator()(float progress, const Vector3& property)
+  {
+    return property;
+  }
+
+  virtual Vector4 operator()(float progress, const Vector4& property)
+  {
+    return property;
+  }
+
+  virtual Quaternion operator()(float progress, const Quaternion& property)
+  {
+    return property;
+  }
+};
+
+namespace SceneGraph
+{
 
 /**
  * An abstract base class for Animators, which can be added to scene graph animations.
  * Each animator changes a single property of an object in the scene graph.
  */
-class AnimatorBase
+class AnimatorBase : public PropertyOwner::Observer
 {
 public:
 
-  typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
+  using AlphaFunc = float (*)(float progress); ///< Definition of an alpha function
+
+  /**
+   * Observer to determine when the animator is no longer present
+   */
+  class LifecycleObserver
+  {
+  public:
+    /**
+     * Called shortly before the animator is destroyed.
+     */
+    virtual void ObjectDestroyed() = 0;
+
+  protected:
+    /**
+     * Virtual destructor, no deletion through this interface
+     */
+    virtual ~LifecycleObserver() = default;
+  };
+
 
   /**
    * Constructor.
    */
-  AnimatorBase()
-  : mDurationSeconds(1.0f),
-    mInitialDelaySeconds(0.0f),
-    mAlphaFunc(AlphaFunctions::Linear),
-    mDisconnectAction(Dali::Animation::BakeFinal),
-    mActive(false)
+  AnimatorBase( PropertyOwner* propertyOwner,
+                AnimatorFunctionBase* animatorFunction,
+                AlphaFunction alphaFunction,
+                const TimePeriod& timePeriod )
+  : mLifecycleObserver( nullptr ),
+    mPropertyOwner( propertyOwner ),
+    mAnimatorFunction( animatorFunction ),
+    mDurationSeconds( timePeriod.durationSeconds ),
+    mIntervalDelaySeconds( timePeriod.delaySeconds ),
+    mSpeedFactor( 1.0f ),
+    mCurrentProgress( 0.f ),
+    mLoopCount( 1 ),
+    mAlphaFunction( alphaFunction ),
+    mDisconnectAction( Dali::Animation::BakeFinal ),
+    mAnimationPlaying( false ),
+    mEnabled( true ),
+    mConnectedToSceneGraph( false ),
+    mAutoReverseEnabled( false )
   {
   }
 
@@ -77,6 +160,68 @@ public:
    */
   virtual ~AnimatorBase()
   {
+    delete mAnimatorFunction;
+    if (mPropertyOwner && mConnectedToSceneGraph)
+    {
+      mPropertyOwner->RemoveObserver(*this);
+    }
+    if( mLifecycleObserver != nullptr )
+    {
+      mLifecycleObserver->ObjectDestroyed();
+    }
+  }
+
+  void AddLifecycleObserver( LifecycleObserver& observer )
+  {
+    mLifecycleObserver = &observer;
+  }
+
+  void RemoveLifecycleObserver( LifecycleObserver& observer )
+  {
+    mLifecycleObserver = nullptr;
+  }
+
+private: // From PropertyOwner::Observer
+
+  /**
+   * @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
+   */
+  void PropertyOwnerConnected( PropertyOwner& owner ) override final
+  {
+    mEnabled = true;
+  }
+
+  /**
+   * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
+   */
+  void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner ) override final
+  {
+    // If we are active, then bake the value if required
+    if ( mAnimationPlaying && mDisconnectAction != Dali::Animation::Discard )
+    {
+      // Bake to target-value if BakeFinal, otherwise bake current value
+      Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
+    }
+
+    mEnabled = false;
+  }
+
+  /**
+   * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
+   */
+  void PropertyOwnerDestroyed( PropertyOwner& owner ) override final
+  {
+    mPropertyOwner = nullptr;
+  }
+
+public:
+  /**
+   * Called when Animator is added to the scene-graph in update-thread.
+   */
+  void ConnectToSceneGraph()
+  {
+    mConnectedToSceneGraph = true;
+    mPropertyOwner->AddObserver(*this);
   }
 
   /**
@@ -95,46 +240,210 @@ public:
    * Retrieve the duration of the animator.
    * @return The duration in seconds.
    */
-  float GetDuration()
+  float GetDuration() const
   {
     return mDurationSeconds;
   }
 
+  void SetSpeedFactor( float factor )
+  {
+    mSpeedFactor = factor;
+  }
+
+  void SetLoopCount(int32_t loopCount)
+  {
+    mLoopCount = loopCount;
+  }
+
+  float SetProgress( float progress )
+  {
+    float value = 0.0f;
+
+    if( mAutoReverseEnabled )
+    {
+      if( mSpeedFactor > 0.0f )
+      {
+        value = 1.0f - 2.0f * std::abs( progress - 0.5f );
+      }
+      // Reverse mode
+      else if( mSpeedFactor < 0.0f )
+      {
+        value = 2.0f * std::abs( progress - 0.5f );
+      }
+    }
+    else
+    {
+      value = progress;
+    }
+
+    return value;
+  }
+
   /**
    * Set the delay before the animator should take effect.
    * The default is zero i.e. no delay.
    * @param [in] seconds The delay in seconds.
    */
-  void SetInitialDelay(float seconds)
+  void SetIntervalDelay(float seconds)
   {
-    mInitialDelaySeconds = seconds;
+    mIntervalDelaySeconds = seconds;
   }
 
   /**
-   * Retrieve the initial delay of the animator.
+   * Retrieve the delay before the animator should take effect.
    * @return The delay in seconds.
    */
-  float GetInitialDelay()
+  float GetIntervalDelay() const
   {
-    return mInitialDelaySeconds;
+    return mIntervalDelaySeconds;
   }
 
   /**
    * Set the alpha function for an animator.
    * @param [in] alphaFunc The alpha function to apply to the animation progress.
    */
-  void SetAlphaFunc(AlphaFunc alphaFunc)
+  void SetAlphaFunction(const AlphaFunction& alphaFunction)
   {
-    mAlphaFunc = alphaFunc;
+    mAlphaFunction = alphaFunction;
   }
 
   /**
    * Retrieve the alpha function of an animator.
    * @return The function.
    */
-  AlphaFunc GetAlphaFunc() const
+  AlphaFunction GetAlphaFunction() const
+  {
+    return mAlphaFunction;
+  }
+
+  /**
+   * Applies the alpha function to the specified progress
+   * @param[in] Current progress
+   * @return The progress after the alpha function has been aplied
+   */
+  float ApplyAlphaFunction( float progress ) const
   {
-    return mAlphaFunc;
+    float result = progress;
+
+    AlphaFunction::Mode alphaFunctionMode( mAlphaFunction.GetMode() );
+    if( alphaFunctionMode == AlphaFunction::BUILTIN_FUNCTION )
+    {
+      switch(mAlphaFunction.GetBuiltinFunction())
+      {
+        case AlphaFunction::DEFAULT:
+        case AlphaFunction::LINEAR:
+        {
+          break;
+        }
+        case AlphaFunction::REVERSE:
+        {
+          result = 1.0f-progress;
+          break;
+        }
+        case AlphaFunction::EASE_IN_SQUARE:
+        {
+          result = progress * progress;
+          break;
+        }
+        case AlphaFunction::EASE_OUT_SQUARE:
+        {
+          result = 1.0f - (1.0f-progress) * (1.0f-progress);
+          break;
+        }
+        case AlphaFunction::EASE_IN:
+        {
+          result = progress * progress * progress;
+          break;
+        }
+        case AlphaFunction::EASE_OUT:
+        {
+          result = (progress-1.0f) * (progress-1.0f) * (progress-1.0f) + 1.0f;
+          break;
+        }
+        case AlphaFunction::EASE_IN_OUT:
+        {
+          result = progress*progress*(3.0f-2.0f*progress);
+          break;
+        }
+        case AlphaFunction::EASE_IN_SINE:
+        {
+          result = -1.0f * cosf(progress * Math::PI_2) + 1.0f;
+          break;
+        }
+        case AlphaFunction::EASE_OUT_SINE:
+        {
+          result = sinf(progress * Math::PI_2);
+          break;
+        }
+        case AlphaFunction::EASE_IN_OUT_SINE:
+        {
+          result = -0.5f * (cosf(Math::PI * progress) - 1.0f);
+          break;
+        }
+        case AlphaFunction::BOUNCE:
+        {
+          result = sinf(progress * Math::PI);
+          break;
+        }
+        case AlphaFunction::SIN:
+        {
+          result = 0.5f - cosf(progress * 2.0f * Math::PI) * 0.5f;
+          break;
+        }
+        case AlphaFunction::EASE_OUT_BACK:
+        {
+          const float sqrt2 = 1.70158f;
+          progress -= 1.0f;
+          result = 1.0f + progress * progress * ( ( sqrt2 + 1.0f ) * progress + sqrt2 );
+          break;
+        }
+        case AlphaFunction::COUNT:
+        {
+          break;
+        }
+      }
+    }
+    else if(  alphaFunctionMode == AlphaFunction::CUSTOM_FUNCTION )
+    {
+      AlphaFunctionPrototype customFunction = mAlphaFunction.GetCustomFunction();
+      if( customFunction )
+      {
+        result = customFunction(progress);
+      }
+    }
+    else
+    {
+      //If progress is very close to 0 or very close to 1 we don't need to evaluate the curve as the result will
+      //be almost 0 or almost 1 respectively
+      if( ( progress > Math::MACHINE_EPSILON_1 ) && ((1.0f - progress) > Math::MACHINE_EPSILON_1) )
+      {
+        Dali::Vector4 controlPoints = mAlphaFunction.GetBezierControlPoints();
+
+        static const float tolerance = 0.001f;  //10 iteration max
+
+        //Perform a binary search on the curve
+        float lowerBound(0.0f);
+        float upperBound(1.0f);
+        float currentT(0.5f);
+        float currentX = EvaluateCubicBezier( controlPoints.x, controlPoints.z, currentT);
+        while( fabsf( progress - currentX ) > tolerance )
+        {
+          if( progress > currentX )
+          {
+            lowerBound = currentT;
+          }
+          else
+          {
+            upperBound = currentT;
+          }
+          currentT = (upperBound+lowerBound)*0.5f;
+          currentX = EvaluateCubicBezier( controlPoints.x, controlPoints.z, currentT);
+        }
+        result = EvaluateCubicBezier( controlPoints.y, controlPoints.w, currentT);
+      }
+    }
+
+    return result;
   }
 
   /**
@@ -158,68 +467,121 @@ public:
 
   /**
    * Whether the animator is active or not.
-   * @param [in] action The disconnect action.
+   * @param [in] active The new active state.
    * @post When the animator becomes active, it applies the disconnect-action if the property owner is then disconnected.
    * @note When the property owner is disconnected, the active state is set to false.
    */
   void SetActive( bool active )
   {
-    mActive = active;
+    mAnimationPlaying = active;
   }
 
   /**
-   * Retrieve whether the animator has been set to active or not.
-   * @return The active state.
+   * Whether the animator's target object is valid and on the stage.
+   * @return The enabled state.
    */
-  bool GetActive() const
+  bool IsEnabled() const
   {
-    return mActive;
+    return mEnabled;
   }
 
   /**
-   * This must be called when the animator is attached to the scene-graph.
-   * @pre The animatable scene object must also be attached to the scene-graph.
-   * @param[in] propertyOwner The scene-object that owns the animatable property.
+   * @brief Sets the looping mode.
+   * @param[in] loopingMode True when the looping mode is AUTO_REVERSE
    */
-  virtual void Attach( PropertyOwner* propertyOwner ) = 0;
+  void SetLoopingMode( bool loopingMode )
+  {
+    mAutoReverseEnabled = loopingMode;
+  }
+
+  /**
+   * Returns wheter the target object of the animator is still valid
+   * or has been destroyed.
+   * @return True if animator is orphan, false otherwise   *
+   * @note The SceneGraph::Animation will delete any orphan animator in its Update method.
+   */
+  bool Orphan()
+  {
+    return (mPropertyOwner == nullptr);
+  }
 
   /**
    * Update the scene object attached to the animator.
    * @param[in] bufferIndex The buffer to animate.
    * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
    * @param[in] bake Bake.
-   * @return True if the update was applied, false if the animator has been orphaned.
    */
-  virtual bool Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
+  void Update( BufferIndex bufferIndex, float progress, bool bake )
+  {
+    if( mLoopCount >= 0 )
+    {
+      // Update the progress value
+      progress = SetProgress( progress );
+    }
+
+    if( mPropertyOwner )
+    {
+      mPropertyOwner->SetPropertyDirty( true );
+    }
+
+    float alpha = ApplyAlphaFunction( progress );
+
+    // PropertyType specific part
+    DoUpdate( bufferIndex, bake, alpha );
+
+    mCurrentProgress = progress;
+  }
 
   /**
-   * Query whether the animator is still attached to a scene object.
-   * The attachment will be automatically severed, when the object is destroyed.
-   * @return True if the animator is attached.
+   * Type specific part of the animator
+   * @param bufferIndex index to use
+   * @param bake whether to bake or not
+   * @param alpha value from alpha based on progress
    */
-  virtual bool IsAttached() const = 0;
+  virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) = 0;
 
 protected:
 
+  /**
+   * Helper function to evaluate a cubic bezier curve assuming first point is at 0.0 and last point is at 1.0
+   * @param[in] p0 First control point of the bezier curve
+   * @param[in] p1 Second control point of the bezier curve
+   * @param[in] t A floating point value between 0.0 and 1.0
+   * @return Value of the curve at progress t
+   */
+  inline float EvaluateCubicBezier( float p0, float p1, float t ) const
+  {
+    float tSquare = t*t;
+    return 3.0f*(1.0f-t)*(1.0f-t)*t*p0 + 3.0f*(1.0f-t)*tSquare*p1 + tSquare*t;
+  }
+
+  LifecycleObserver* mLifecycleObserver;
+  PropertyOwner* mPropertyOwner;
+  AnimatorFunctionBase* mAnimatorFunction;
   float mDurationSeconds;
-  float mInitialDelaySeconds;
+  float mIntervalDelaySeconds;
+  float mSpeedFactor;
+  float mCurrentProgress;
+
+  int32_t mLoopCount;
 
-  AlphaFunc mAlphaFunc;
+  AlphaFunction mAlphaFunction;
 
-  Dali::Animation::EndAction mDisconnectAction;
-  bool mActive:1;
+  Dali::Animation::EndAction mDisconnectAction;     ///< EndAction to apply when target object gets disconnected from the stage.
+  bool mAnimationPlaying:1;                         ///< whether disconnect has been applied while it's running.
+  bool mEnabled:1;                                  ///< Animator is "enabled" while its target object is valid and on the stage.
+  bool mConnectedToSceneGraph:1;                    ///< True if ConnectToSceneGraph() has been called in update-thread.
+  bool mAutoReverseEnabled:1;
 };
 
 /**
  * An animator for a specific property type PropertyType.
  */
 template < typename PropertyType, typename PropertyAccessorType >
-class Animator : public AnimatorBase, public PropertyOwner::Observer
+class Animator : public AnimatorBase
 {
 public:
 
-  typedef boost::function< PropertyType (float, const PropertyType&) > AnimatorFunction;
-
   /**
    * Construct a new property animator.
    * @param[in] property The animatable property; only valid while the Animator is attached.
@@ -228,22 +590,18 @@ public:
    * @param[in] timePeriod The time period of this animation.
    * @return A newly allocated animator.
    */
-  static AnimatorBase* New( const PropertyBase& property,
-                            AnimatorFunction animatorFunction,
+  static AnimatorBase* New( const PropertyOwner& propertyOwner,
+                            const PropertyBase& property,
+                            AnimatorFunctionBase* animatorFunction,
                             AlphaFunction alphaFunction,
                             const TimePeriod& timePeriod )
   {
-    typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
-
     // The property was const in the actor-thread, but animators are used in the scene-graph thread.
-    AnimatorType* animator = new AnimatorType( const_cast<PropertyBase*>( &property ),
-                                               animatorFunction );
-
-    animator->SetAlphaFunc( alphaFunction );
-    animator->SetInitialDelay( timePeriod.delaySeconds );
-    animator->SetDuration( timePeriod.durationSeconds );
-
-    return animator;
+    return new Animator( const_cast<PropertyOwner*>( &propertyOwner ),
+                                  const_cast<PropertyBase*>( &property ),
+                                  animatorFunction,
+                                  alphaFunction,
+                                  timePeriod );
   }
 
   /**
@@ -251,87 +609,115 @@ public:
    */
   virtual ~Animator()
   {
-    if (mPropertyOwner)
-    {
-      mPropertyOwner->RemoveObserver(*this);
-    }
   }
 
   /**
-   * From AnimatorBase.
-   * @param[in] propertyOwner The scene-object that owns the animatable property.
+   * @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
    */
-  virtual void Attach( PropertyOwner* propertyOwner )
+  virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
   {
-    mPropertyOwner = propertyOwner;
+    const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
+
+    // need to cast the return value in case property is integer
+    const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
 
-    if (mPropertyOwner)
+    if ( bake )
     {
-      mPropertyOwner->AddObserver(*this);
+      mPropertyAccessor.Bake( bufferIndex, result );
+    }
+    else
+    {
+      mPropertyAccessor.Set( bufferIndex, result );
     }
   }
 
+private:
+
   /**
-   * From AnimatorBase.
+   * Private constructor; see also Animator::New().
    */
-  virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
+  Animator( PropertyOwner* propertyOwner,
+            PropertyBase* property,
+            AnimatorFunctionBase* animatorFunction,
+            AlphaFunction alphaFunction,
+            const TimePeriod& timePeriod )
+  : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
+    mPropertyAccessor( property )
   {
-    // If the object dies, the animator has no effect
-    if ( mPropertyOwner )
-    {
-      float alpha = mAlphaFunc( progress );
+    // WARNING - this object is created in the event-thread
+    // The scene-graph mPropertyOwner object cannot be observed here
+  }
 
-      const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
+  // Undefined
+  Animator( const Animator& );
 
-      const PropertyType result = mAnimatorFunction( alpha, current );
+  // Undefined
+  Animator& operator=( const Animator& );
 
-      if ( bake )
-      {
-        mPropertyAccessor.Bake( bufferIndex, result );
-      }
-      else
-      {
-        mPropertyAccessor.Set( bufferIndex, result );
-      }
+protected:
 
-      mCurrentProgress = progress;
-    }
+  PropertyAccessorType mPropertyAccessor;
 
-    return IsAttached(); // return false if orphaned
-  }
+};
+
+
+
+/**
+ * An animator for a specific property type PropertyType.
+ */
+template <typename PropertyType, typename PropertyAccessorType>
+class AnimatorTransformProperty : public AnimatorBase
+{
+public:
 
   /**
-   * From AnimatorBase.
+   * Construct a new property animator.
+   * @param[in] property The animatable property; only valid while the Animator is attached.
+   * @param[in] animatorFunction The function used to animate the property.
+   * @param[in] alphaFunction The alpha function to apply.
+   * @param[in] timePeriod The time period of this animation.
+   * @return A newly allocated animator.
    */
-  virtual bool IsAttached() const
+  static AnimatorBase* New( const PropertyOwner& propertyOwner,
+                            const PropertyBase& property,
+                            AnimatorFunctionBase* animatorFunction,
+                            AlphaFunction alphaFunction,
+                            const TimePeriod& timePeriod )
   {
-    return NULL != mPropertyOwner;
+
+    // The property was const in the actor-thread, but animators are used in the scene-graph thread.
+    return new AnimatorTransformProperty( const_cast<PropertyOwner*>( &propertyOwner ),
+                                                                         const_cast<PropertyBase*>( &property ),
+                                                                         animatorFunction,
+                                                                         alphaFunction,
+                                                                         timePeriod );
   }
 
   /**
-   * Called when mPropertyOwner is disconnected from the scene graph.
+   * Virtual destructor.
    */
-  virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
+  virtual ~AnimatorTransformProperty()
   {
-    // If we are active, then bake the value if required
-    if ( mActive && mDisconnectAction != Dali::Animation::Discard )
-    {
-      // Bake to target-value if BakeFinal, otherwise bake current value
-      Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
-    }
-
-    mActive = false;
-    mPropertyOwner = NULL;
-    mPropertyAccessor.Reset();
   }
 
   /**
-   * Called shortly before mPropertyOwner is destroyed, along with its property.
+   * @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
    */
-  virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
+  virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
   {
-    mPropertyOwner = NULL;
-    mPropertyAccessor.Reset();
+    const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
+
+    // need to cast the return value in case property is integer
+    const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
+
+    if ( bake )
+    {
+      mPropertyAccessor.Bake( bufferIndex, result );
+    }
+    else
+    {
+      mPropertyAccessor.Set( bufferIndex, result );
+    }
   }
 
 private:
@@ -339,102 +725,107 @@ private:
   /**
    * Private constructor; see also Animator::New().
    */
-  Animator( PropertyBase* property,
-            AnimatorFunction animatorFunction )
-  : mPropertyOwner( NULL ),
-    mPropertyAccessor( property ),
-    mAnimatorFunction( animatorFunction ),
-    mCurrentProgress( 0.0f )
+  AnimatorTransformProperty( PropertyOwner* propertyOwner,
+            PropertyBase* property,
+            AnimatorFunctionBase* animatorFunction,
+            AlphaFunction alphaFunction,
+            const TimePeriod& timePeriod )
+  : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
+    mPropertyAccessor( property )
   {
+    // WARNING - this object is created in the event-thread
+    // The scene-graph mPropertyOwner object cannot be observed here
   }
 
   // Undefined
-  Animator( const Animator& );
-
-  // Undefined
-  Animator& operator=( const Animator& );
+  AnimatorTransformProperty() = delete;
+  AnimatorTransformProperty( const AnimatorTransformProperty& ) = delete;
+  AnimatorTransformProperty& operator=( const AnimatorTransformProperty& ) = delete;
 
 protected:
 
-  PropertyOwner* mPropertyOwner;
   PropertyAccessorType mPropertyAccessor;
 
-  AnimatorFunction mAnimatorFunction;
-  float mCurrentProgress;
 };
 
 } // namespace SceneGraph
 
+// Update functions
 
-// Common Update functions
-
-struct AnimateByFloat
+struct AnimateByInteger : public AnimatorFunctionBase
 {
-  AnimateByFloat(const float& relativeValue)
+  AnimateByInteger(const int& relativeValue)
   : mRelative(relativeValue)
   {
   }
 
-  float operator()(float alpha, const float& property)
+  using AnimatorFunctionBase::operator();
+  float operator()(float alpha, const int32_t& property)
   {
-    return float(property + mRelative * alpha);
+    // integers need to be correctly rounded
+    return roundf(static_cast<float>( property ) + static_cast<float>( mRelative ) * alpha );
   }
 
-  float mRelative;
+  int32_t mRelative;
 };
 
-struct AnimateToFloat
+struct AnimateToInteger : public AnimatorFunctionBase
 {
-  AnimateToFloat(const float& targetValue)
+  AnimateToInteger(const int& targetValue)
   : mTarget(targetValue)
   {
   }
 
-  float operator()(float alpha, const float& property)
+  using AnimatorFunctionBase::operator();
+  float operator()(float alpha, const int32_t& property)
   {
-    return float(property + ((mTarget - property) * alpha));
+    // integers need to be correctly rounded
+    return roundf(static_cast<float>( property ) + (static_cast<float>(mTarget - property) * alpha) );
   }
 
-  float mTarget;
+  int32_t mTarget;
 };
 
-struct AnimateByInteger
+struct AnimateByFloat : public AnimatorFunctionBase
 {
-  AnimateByInteger(const int& relativeValue)
+  AnimateByFloat(const float& relativeValue)
   : mRelative(relativeValue)
   {
   }
 
-  float operator()(float alpha, const int& property)
+  using AnimatorFunctionBase::operator();
+  float operator()(float alpha, const float& property)
   {
-    return int(property + mRelative * alpha + 0.5f );
+    return float(property + mRelative * alpha);
   }
 
-  int mRelative;
+  float mRelative;
 };
 
-struct AnimateToInteger
+struct AnimateToFloat : public AnimatorFunctionBase
 {
-  AnimateToInteger(const int& targetValue)
+  AnimateToFloat(const float& targetValue)
   : mTarget(targetValue)
   {
   }
 
-  float operator()(float alpha, const int& property)
+  using AnimatorFunctionBase::operator();
+  float operator()(float alpha, const float& property)
   {
-    return int(property + ((mTarget - property) * alpha) + 0.5f);
+    return float(property + ((mTarget - property) * alpha));
   }
 
-  int mTarget;
+  float mTarget;
 };
 
-struct AnimateByVector2
+struct AnimateByVector2 : public AnimatorFunctionBase
 {
   AnimateByVector2(const Vector2& relativeValue)
   : mRelative(relativeValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector2 operator()(float alpha, const Vector2& property)
   {
     return Vector2(property + mRelative * alpha);
@@ -443,13 +834,14 @@ struct AnimateByVector2
   Vector2 mRelative;
 };
 
-struct AnimateToVector2
+struct AnimateToVector2 : public AnimatorFunctionBase
 {
   AnimateToVector2(const Vector2& targetValue)
   : mTarget(targetValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector2 operator()(float alpha, const Vector2& property)
   {
     return Vector2(property + ((mTarget - property) * alpha));
@@ -458,13 +850,14 @@ struct AnimateToVector2
   Vector2 mTarget;
 };
 
-struct AnimateByVector3
+struct AnimateByVector3 : public AnimatorFunctionBase
 {
   AnimateByVector3(const Vector3& relativeValue)
   : mRelative(relativeValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector3 operator()(float alpha, const Vector3& property)
   {
     return Vector3(property + mRelative * alpha);
@@ -473,13 +866,14 @@ struct AnimateByVector3
   Vector3 mRelative;
 };
 
-struct AnimateToVector3
+struct AnimateToVector3 : public AnimatorFunctionBase
 {
   AnimateToVector3(const Vector3& targetValue)
   : mTarget(targetValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector3 operator()(float alpha, const Vector3& property)
   {
     return Vector3(property + ((mTarget - property) * alpha));
@@ -488,13 +882,14 @@ struct AnimateToVector3
   Vector3 mTarget;
 };
 
-struct AnimateByVector4
+struct AnimateByVector4 : public AnimatorFunctionBase
 {
   AnimateByVector4(const Vector4& relativeValue)
   : mRelative(relativeValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector4 operator()(float alpha, const Vector4& property)
   {
     return Vector4(property + mRelative * alpha);
@@ -503,13 +898,14 @@ struct AnimateByVector4
   Vector4 mRelative;
 };
 
-struct AnimateToVector4
+struct AnimateToVector4 : public AnimatorFunctionBase
 {
   AnimateToVector4(const Vector4& targetValue)
   : mTarget(targetValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector4 operator()(float alpha, const Vector4& property)
   {
     return Vector4(property + ((mTarget - property) * alpha));
@@ -518,13 +914,14 @@ struct AnimateToVector4
   Vector4 mTarget;
 };
 
-struct AnimateByOpacity
+struct AnimateByOpacity : public AnimatorFunctionBase
 {
   AnimateByOpacity(const float& relativeValue)
   : mRelative(relativeValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector4 operator()(float alpha, const Vector4& property)
   {
     Vector4 result(property);
@@ -536,13 +933,14 @@ struct AnimateByOpacity
   float mRelative;
 };
 
-struct AnimateToOpacity
+struct AnimateToOpacity : public AnimatorFunctionBase
 {
   AnimateToOpacity(const float& targetValue)
   : mTarget(targetValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector4 operator()(float alpha, const Vector4& property)
   {
     Vector4 result(property);
@@ -554,13 +952,14 @@ struct AnimateToOpacity
   float mTarget;
 };
 
-struct AnimateByBoolean
+struct AnimateByBoolean : public AnimatorFunctionBase
 {
   AnimateByBoolean(bool relativeValue)
   : mRelative(relativeValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   bool operator()(float alpha, const bool& property)
   {
     // Alpha is not useful here, just keeping to the same template as other update functors
@@ -570,13 +969,14 @@ struct AnimateByBoolean
   bool mRelative;
 };
 
-struct AnimateToBoolean
+struct AnimateToBoolean : public AnimatorFunctionBase
 {
   AnimateToBoolean(bool targetValue)
   : mTarget(targetValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   bool operator()(float alpha, const bool& property)
   {
     // Alpha is not useful here, just keeping to the same template as other update functors
@@ -586,14 +986,15 @@ struct AnimateToBoolean
   bool mTarget;
 };
 
-struct RotateByAngleAxis
+struct RotateByAngleAxis : public AnimatorFunctionBase
 {
   RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
-  : mAngleRadians(angleRadians),
-    mAxis(axis.x, axis.y, axis.z, 0.0f)
+  : mAngleRadians( angleRadians ),
+    mAxis(axis.x, axis.y, axis.z)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Quaternion operator()(float alpha, const Quaternion& rotation)
   {
     if (alpha > 0.0f)
@@ -604,17 +1005,18 @@ struct RotateByAngleAxis
     return rotation;
   }
 
-  float mAngleRadians;
-  Vector4 mAxis;
+  Radian mAngleRadians;
+  Vector3 mAxis;
 };
 
-struct RotateToQuaternion
+struct RotateToQuaternion : public AnimatorFunctionBase
 {
   RotateToQuaternion(const Quaternion& targetValue)
   : mTarget(targetValue)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Quaternion operator()(float alpha, const Quaternion& rotation)
   {
     return Quaternion::Slerp(rotation, mTarget, alpha);
@@ -624,18 +1026,19 @@ struct RotateToQuaternion
 };
 
 
-struct KeyFrameBooleanFunctor
+struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
 {
   KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
   : mKeyFrames(keyFrames)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   bool operator()(float progress, const bool& property)
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
     }
     return property;
   }
@@ -643,114 +1046,125 @@ struct KeyFrameBooleanFunctor
   KeyFrameBooleanPtr mKeyFrames;
 };
 
-struct KeyFrameNumberFunctor
+struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
 {
-  KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
-  float operator()(float progress, const float& property)
+  using AnimatorFunctionBase::operator();
+  float operator()(float progress, const int32_t& property)
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return static_cast<float>( mKeyFrames->GetValue(progress, mInterpolation) );
     }
-    return property;
+    return static_cast<float>( property );
   }
 
-  KeyFrameNumberPtr mKeyFrames;
+  KeyFrameIntegerPtr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
-struct KeyFrameIntegerFunctor
+struct KeyFrameNumberFunctor : public AnimatorFunctionBase
 {
-  KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
-  float operator()(float progress, const int& property)
+  using AnimatorFunctionBase::operator();
+  float operator()(float progress, const float& property)
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, mInterpolation);
     }
     return property;
   }
 
-  KeyFrameIntegerPtr mKeyFrames;
+  KeyFrameNumberPtr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
-struct KeyFrameVector2Functor
+struct KeyFrameVector2Functor : public AnimatorFunctionBase
 {
-  KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector2 operator()(float progress, const Vector2& property)
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, mInterpolation);
     }
     return property;
   }
 
   KeyFrameVector2Ptr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
 
-struct KeyFrameVector3Functor
+struct KeyFrameVector3Functor : public AnimatorFunctionBase
 {
-  KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector3 operator()(float progress, const Vector3& property)
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, mInterpolation);
     }
     return property;
   }
 
   KeyFrameVector3Ptr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
-struct KeyFrameVector4Functor
+struct KeyFrameVector4Functor : public AnimatorFunctionBase
 {
-  KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector4 operator()(float progress, const Vector4& property)
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, mInterpolation);
     }
     return property;
   }
 
   KeyFrameVector4Ptr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
-struct KeyFrameQuaternionFunctor
+struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase
 {
   KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
   : mKeyFrames(keyFrames)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Quaternion operator()(float progress, const Quaternion& property)
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
     }
     return property;
   }
@@ -758,22 +1172,25 @@ struct KeyFrameQuaternionFunctor
   KeyFrameQuaternionPtr mKeyFrames;
 };
 
-struct PathPositionFunctor
+struct PathPositionFunctor : public AnimatorFunctionBase
 {
   PathPositionFunctor( PathPtr path )
   : mPath(path)
   {
   }
 
+  using AnimatorFunctionBase::operator();
   Vector3 operator()(float progress, const Vector3& property)
   {
-    return mPath->SamplePosition(progress );
+    Vector3 position(property);
+    static_cast<void>( mPath->SamplePosition(progress, position) );
+    return position;
   }
 
   PathPtr mPath;
 };
 
-struct PathRotationFunctor
+struct PathRotationFunctor : public AnimatorFunctionBase
 {
   PathRotationFunctor( PathPtr path, const Vector3& forward )
   : mPath(path),
@@ -782,19 +1199,26 @@ struct PathRotationFunctor
     mForward.Normalize();
   }
 
+  using AnimatorFunctionBase::operator();
   Quaternion operator()(float progress, const Quaternion& property)
   {
-    Vector3 tangent( mPath->SampleTangent(progress) );
-    return Quaternion( mForward, tangent );
+    Vector3 tangent;
+    if( mPath->SampleTangent(progress, tangent) )
+    {
+      return Quaternion( mForward, tangent );
+    }
+    else
+    {
+      return property;
+    }
   }
 
   PathPtr mPath;
   Vector3 mForward;
 };
 
-
 } // namespace Internal
 
 } // namespace Dali
 
-#endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
+#endif // DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H