Merge "Ensured that messages are aligned to 8 byte words on 64bit ARM" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-animator.h
index 13c245f..b8d5f58 100644 (file)
@@ -18,9 +18,6 @@
  *
  */
 
-// EXTERNAL INCLUDES
-#include <boost/function.hpp>
-
 // INTERNAL INCLUDES
 #include <dali/internal/common/owner-container.h>
 #include <dali/internal/event/animation/key-frames-impl.h>
@@ -40,6 +37,10 @@ namespace Dali
 namespace Internal
 {
 
+typedef Dali::Animation::Interpolation Interpolation;
+
+struct AnimatorFunctionBase;
+
 namespace SceneGraph
 {
 
@@ -68,7 +69,8 @@ public:
     mInitialDelaySeconds(0.0f),
     mAlphaFunc(AlphaFunctions::Linear),
     mDisconnectAction(Dali::Animation::BakeFinal),
-    mActive(false)
+    mActive(false),
+    mEnabled(true)
   {
   }
 
@@ -158,7 +160,7 @@ 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.
    */
@@ -176,28 +178,29 @@ public:
     return mActive;
   }
 
+  /*
+   * Retrive wheter the animator's target object is valid and on the stage.
+   * @return The enabled state.
+   */
+  bool IsEnabled() const
+  {
+    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.
+   * 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.
    */
-  virtual void Attach( PropertyOwner* propertyOwner ) = 0;
+  virtual bool Orphan() = 0;
 
   /**
    * 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;
-
-  /**
-   * 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.
    */
-  virtual bool IsAttached() const = 0;
+  virtual void Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
 
 protected:
 
@@ -206,8 +209,9 @@ protected:
 
   AlphaFunc mAlphaFunc;
 
-  Dali::Animation::EndAction mDisconnectAction;
-  bool mActive:1;
+  Dali::Animation::EndAction mDisconnectAction;     ///< EndAction to apply when target object gets disconnected from the stage.
+  bool mActive:1;                                   ///< Animator is "active" while it's running.
+  bool mEnabled:1;                                  ///< Animator is "enabled" while its target object is valid and on the stage.
 };
 
 /**
@@ -218,8 +222,6 @@ class Animator : public AnimatorBase, public PropertyOwner::Observer
 {
 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,15 +230,17 @@ 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 ),
+    AnimatorType* animator = new AnimatorType( const_cast<PropertyOwner*>( &propertyOwner ),
+                                               const_cast<PropertyBase*>( &property ),
                                                animatorFunction );
 
     animator->SetAlphaFunc( alphaFunction );
@@ -255,57 +259,19 @@ public:
     {
       mPropertyOwner->RemoveObserver(*this);
     }
-  }
 
-  /**
-   * From AnimatorBase.
-   * @param[in] propertyOwner The scene-object that owns the animatable property.
-   */
-  virtual void Attach( PropertyOwner* propertyOwner )
-  {
-    mPropertyOwner = propertyOwner;
-
-    if (mPropertyOwner)
+    if( mAnimatorFunction )
     {
-      mPropertyOwner->AddObserver(*this);
+      delete mAnimatorFunction;
     }
   }
 
   /**
-   * From AnimatorBase.
+   * Called when mPropertyOwner is connected to the scene graph.
    */
-  virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
+  virtual void PropertyOwnerConnected( PropertyOwner& owner )
   {
-    // If the object dies, the animator has no effect
-    if ( mPropertyOwner )
-    {
-      float alpha = mAlphaFunc( progress );
-
-      const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
-
-      const PropertyType result = mAnimatorFunction( alpha, current );
-
-      if ( bake )
-      {
-        mPropertyAccessor.Bake( bufferIndex, result );
-      }
-      else
-      {
-        mPropertyAccessor.Set( bufferIndex, result );
-      }
-
-      mCurrentProgress = progress;
-    }
-
-    return IsAttached(); // return false if orphaned
-  }
-
-  /**
-   * From AnimatorBase.
-   */
-  virtual bool IsAttached() const
-  {
-    return NULL != mPropertyOwner;
+    mEnabled = true;
   }
 
   /**
@@ -321,17 +287,46 @@ public:
     }
 
     mActive = false;
-    mPropertyOwner = NULL;
-    mPropertyAccessor.Reset();
+    mEnabled = false;
   }
 
   /**
-   * Called shortly before mPropertyOwner is destroyed, along with its property.
+   * Called shortly before mPropertyOwner is destroyed
    */
   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
   {
     mPropertyOwner = NULL;
     mPropertyAccessor.Reset();
+    mEnabled = false;
+  }
+
+  /**
+   * From AnimatorBase.
+   */
+  virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
+  {
+    float alpha = mAlphaFunc( progress );
+    const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
+
+    const PropertyType result = (*mAnimatorFunction)( alpha, current );
+    if ( bake )
+    {
+      mPropertyAccessor.Bake( bufferIndex, result );
+    }
+    else
+    {
+      mPropertyAccessor.Set( bufferIndex, result );
+    }
+
+    mCurrentProgress = progress;
+  }
+
+  /**
+   * From AnimatorBase.
+   */
+  virtual bool Orphan()
+  {
+    return (mPropertyOwner == NULL);
   }
 
 private:
@@ -339,13 +334,15 @@ private:
   /**
    * Private constructor; see also Animator::New().
    */
-  Animator( PropertyBase* property,
-            AnimatorFunction animatorFunction )
-  : mPropertyOwner( NULL ),
+  Animator( PropertyOwner* propertyOwner,
+            PropertyBase* property,
+            AnimatorFunctionBase* animatorFunction )
+  : mPropertyOwner( propertyOwner ),
     mPropertyAccessor( property ),
     mAnimatorFunction( animatorFunction ),
     mCurrentProgress( 0.0f )
   {
+    mPropertyOwner->AddObserver(*this);
   }
 
   // Undefined
@@ -359,16 +356,68 @@ protected:
   PropertyOwner* mPropertyOwner;
   PropertyAccessorType mPropertyAccessor;
 
-  AnimatorFunction mAnimatorFunction;
+  AnimatorFunctionBase* mAnimatorFunction;
   float mCurrentProgress;
 };
 
 } // namespace SceneGraph
 
+/*
+ * AnimatorFunction base class.
+ * All update functions must inherit from AnimatorFunctionBase and overload the appropiate "()" operator
+ */
+struct AnimatorFunctionBase
+{
+  /**
+   * Constructor
+   */
+  AnimatorFunctionBase(){}
+
+  /*
+   * Virtual destructor (Intended as base class)
+   */
+  virtual ~AnimatorFunctionBase(){}
+
+  ///Stub "()" operators.
+  virtual float operator()(float progress, const int& property)
+  {
+    return property;
+  }
+
+  virtual float operator()(float progress, const float& property)
+  {
+    return property;
+  }
+
+  virtual bool operator()(float progress, const bool& property)
+  {
+    return property;
+  }
+
+  virtual Vector2 operator()(float progress, const Vector2& property)
+  {
+    return property;
+  }
+
+  virtual Vector3 operator()(float progress, const Vector3& property)
+  {
+    return property;
+  }
 
-// Common Update functions
+  virtual Vector4 operator()(float progress, const Vector4& property)
+  {
+    return property;
+  }
 
-struct AnimateByFloat
+  virtual Quaternion operator()(float progress, const Quaternion& property)
+  {
+    return property;
+  }
+};
+
+// Update functions
+
+struct AnimateByFloat : public AnimatorFunctionBase
 {
   AnimateByFloat(const float& relativeValue)
   : mRelative(relativeValue)
@@ -383,7 +432,7 @@ struct AnimateByFloat
   float mRelative;
 };
 
-struct AnimateToFloat
+struct AnimateToFloat : public AnimatorFunctionBase
 {
   AnimateToFloat(const float& targetValue)
   : mTarget(targetValue)
@@ -398,7 +447,7 @@ struct AnimateToFloat
   float mTarget;
 };
 
-struct AnimateByInteger
+struct AnimateByInteger : public AnimatorFunctionBase
 {
   AnimateByInteger(const int& relativeValue)
   : mRelative(relativeValue)
@@ -413,7 +462,7 @@ struct AnimateByInteger
   int mRelative;
 };
 
-struct AnimateToInteger
+struct AnimateToInteger : public AnimatorFunctionBase
 {
   AnimateToInteger(const int& targetValue)
   : mTarget(targetValue)
@@ -428,7 +477,7 @@ struct AnimateToInteger
   int mTarget;
 };
 
-struct AnimateByVector2
+struct AnimateByVector2 : public AnimatorFunctionBase
 {
   AnimateByVector2(const Vector2& relativeValue)
   : mRelative(relativeValue)
@@ -443,7 +492,7 @@ struct AnimateByVector2
   Vector2 mRelative;
 };
 
-struct AnimateToVector2
+struct AnimateToVector2 : public AnimatorFunctionBase
 {
   AnimateToVector2(const Vector2& targetValue)
   : mTarget(targetValue)
@@ -458,7 +507,7 @@ struct AnimateToVector2
   Vector2 mTarget;
 };
 
-struct AnimateByVector3
+struct AnimateByVector3 : public AnimatorFunctionBase
 {
   AnimateByVector3(const Vector3& relativeValue)
   : mRelative(relativeValue)
@@ -473,7 +522,7 @@ struct AnimateByVector3
   Vector3 mRelative;
 };
 
-struct AnimateToVector3
+struct AnimateToVector3 : public AnimatorFunctionBase
 {
   AnimateToVector3(const Vector3& targetValue)
   : mTarget(targetValue)
@@ -488,7 +537,7 @@ struct AnimateToVector3
   Vector3 mTarget;
 };
 
-struct AnimateByVector4
+struct AnimateByVector4 : public AnimatorFunctionBase
 {
   AnimateByVector4(const Vector4& relativeValue)
   : mRelative(relativeValue)
@@ -503,7 +552,7 @@ struct AnimateByVector4
   Vector4 mRelative;
 };
 
-struct AnimateToVector4
+struct AnimateToVector4 : public AnimatorFunctionBase
 {
   AnimateToVector4(const Vector4& targetValue)
   : mTarget(targetValue)
@@ -518,7 +567,7 @@ struct AnimateToVector4
   Vector4 mTarget;
 };
 
-struct AnimateByOpacity
+struct AnimateByOpacity : public AnimatorFunctionBase
 {
   AnimateByOpacity(const float& relativeValue)
   : mRelative(relativeValue)
@@ -536,7 +585,7 @@ struct AnimateByOpacity
   float mRelative;
 };
 
-struct AnimateToOpacity
+struct AnimateToOpacity : public AnimatorFunctionBase
 {
   AnimateToOpacity(const float& targetValue)
   : mTarget(targetValue)
@@ -554,7 +603,7 @@ struct AnimateToOpacity
   float mTarget;
 };
 
-struct AnimateByBoolean
+struct AnimateByBoolean : public AnimatorFunctionBase
 {
   AnimateByBoolean(bool relativeValue)
   : mRelative(relativeValue)
@@ -570,7 +619,7 @@ struct AnimateByBoolean
   bool mRelative;
 };
 
-struct AnimateToBoolean
+struct AnimateToBoolean : public AnimatorFunctionBase
 {
   AnimateToBoolean(bool targetValue)
   : mTarget(targetValue)
@@ -586,7 +635,7 @@ struct AnimateToBoolean
   bool mTarget;
 };
 
-struct RotateByAngleAxis
+struct RotateByAngleAxis : public AnimatorFunctionBase
 {
   RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
   : mAngleRadians(angleRadians),
@@ -608,7 +657,7 @@ struct RotateByAngleAxis
   Vector4 mAxis;
 };
 
-struct RotateToQuaternion
+struct RotateToQuaternion : public AnimatorFunctionBase
 {
   RotateToQuaternion(const Quaternion& targetValue)
   : mTarget(targetValue)
@@ -624,7 +673,7 @@ struct RotateToQuaternion
 };
 
 
-struct KeyFrameBooleanFunctor
+struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
 {
   KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
   : mKeyFrames(keyFrames)
@@ -635,7 +684,7 @@ struct KeyFrameBooleanFunctor
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
     }
     return property;
   }
@@ -643,10 +692,10 @@ struct KeyFrameBooleanFunctor
   KeyFrameBooleanPtr mKeyFrames;
 };
 
-struct KeyFrameNumberFunctor
+struct KeyFrameNumberFunctor : public AnimatorFunctionBase
 {
-  KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
@@ -654,18 +703,19 @@ struct KeyFrameNumberFunctor
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, mInterpolation);
     }
     return property;
   }
 
   KeyFrameNumberPtr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
-struct KeyFrameIntegerFunctor
+struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
 {
-  KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
@@ -673,18 +723,19 @@ struct KeyFrameIntegerFunctor
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, mInterpolation);
     }
     return property;
   }
 
   KeyFrameIntegerPtr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
-struct KeyFrameVector2Functor
+struct KeyFrameVector2Functor : public AnimatorFunctionBase
 {
-  KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
@@ -692,19 +743,20 @@ struct KeyFrameVector2Functor
   {
     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)
   {
   }
 
@@ -712,18 +764,19 @@ struct KeyFrameVector3Functor
   {
     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)
   {
   }
 
@@ -731,15 +784,16 @@ struct KeyFrameVector4Functor
   {
     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)
@@ -750,7 +804,7 @@ struct KeyFrameQuaternionFunctor
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
     }
     return property;
   }
@@ -758,7 +812,7 @@ struct KeyFrameQuaternionFunctor
   KeyFrameQuaternionPtr mKeyFrames;
 };
 
-struct PathPositionFunctor
+struct PathPositionFunctor : public AnimatorFunctionBase
 {
   PathPositionFunctor( PathPtr path )
   : mPath(path)
@@ -773,7 +827,7 @@ struct PathPositionFunctor
   PathPtr mPath;
 };
 
-struct PathRotationFunctor
+struct PathRotationFunctor : public AnimatorFunctionBase
 {
   PathRotationFunctor( PathPtr path, const Vector3& forward )
   : mPath(path),