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 29fc882..b8d5f58 100644 (file)
@@ -1,32 +1,31 @@
 #ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
 #define __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
 
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-// EXTERNAL INCLUDES
-#include <boost/function.hpp>
+/*
+ * Copyright (c) 2014 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // 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/internal/common/observer-pointer.h>
 #include <dali/public-api/animation/alpha-functions.h>
+#include <dali/public-api/animation/animation.h>
 #include <dali/public-api/animation/time-period.h>
 #include <dali/public-api/common/dali-common.h>
 #include <dali/public-api/math/quaternion.h>
@@ -38,6 +37,10 @@ namespace Dali
 namespace Internal
 {
 
+typedef Dali::Animation::Interpolation Interpolation;
+
+struct AnimatorFunctionBase;
+
 namespace SceneGraph
 {
 
@@ -65,7 +68,9 @@ public:
   : mDurationSeconds(1.0f),
     mInitialDelaySeconds(0.0f),
     mAlphaFunc(AlphaFunctions::Linear),
-    mRelative(false)
+    mDisconnectAction(Dali::Animation::BakeFinal),
+    mActive(false),
+    mEnabled(true)
   {
   }
 
@@ -135,27 +140,67 @@ public:
   }
 
   /**
-   * 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.
+   * Whether to bake the animation if attached property owner is disconnected.
+   * Property is only baked if the animator is active.
+   * @param [in] action The disconnect action.
+   */
+  void SetDisconnectAction( Dali::Animation::EndAction action )
+  {
+    mDisconnectAction = action;
+  }
+
+  /**
+   * Retrieve the disconnect action of an animator.
+   * @return The disconnect action.
+   */
+  Dali::Animation::EndAction GetDisconnectAction() const
+  {
+    return mDisconnectAction;
+  }
+
+  /**
+   * Whether the animator is active or not.
+   * @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;
+  }
+
+  /**
+   * Retrieve whether the animator has been set to active or not.
+   * @return The active state.
+   */
+  bool GetActive() const
+  {
+    return mActive;
+  }
+
+  /*
+   * Retrive wheter the animator's target object is valid and on the stage.
+   * @return The enabled state.
+   */
+  bool IsEnabled() const
+  {
+    return mEnabled;
+  }
+  /**
+   * 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:
 
@@ -164,7 +209,9 @@ protected:
 
   AlphaFunc mAlphaFunc;
 
-  bool mRelative;
+  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.
 };
 
 /**
@@ -175,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.
@@ -185,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 );
@@ -212,64 +259,74 @@ public:
     {
       mPropertyOwner->RemoveObserver(*this);
     }
+
+    if( mAnimatorFunction )
+    {
+      delete mAnimatorFunction;
+    }
   }
 
   /**
-   * From AnimatorBase.
-   * @param[in] propertyOwner The scene-object that owns the animatable property.
+   * Called when mPropertyOwner is connected to the scene graph.
    */
-  virtual void Attach( PropertyOwner* propertyOwner )
+  virtual void PropertyOwnerConnected( PropertyOwner& owner )
   {
-    mPropertyOwner = propertyOwner;
-
-    if (mPropertyOwner)
-    {
-      mPropertyOwner->AddObserver(*this);
-    }
+    mEnabled = true;
   }
 
   /**
-   * From AnimatorBase.
+   * Called when mPropertyOwner is disconnected from the scene graph.
    */
-  virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
+  virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
   {
-    // If the object dies, the animator has no effect
-    if ( mPropertyOwner )
+    // If we are active, then bake the value if required
+    if ( mActive && mDisconnectAction != Dali::Animation::Discard )
     {
-      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 );
-      }
+      // Bake to target-value if BakeFinal, otherwise bake current value
+      Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
     }
 
-    return IsAttached(); // return false if orphaned
+    mActive = false;
+    mEnabled = false;
+  }
+
+  /**
+   * Called shortly before mPropertyOwner is destroyed
+   */
+  virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
+  {
+    mPropertyOwner = NULL;
+    mPropertyAccessor.Reset();
+    mEnabled = false;
   }
 
   /**
    * From AnimatorBase.
    */
-  virtual bool IsAttached() const
+  virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
   {
-    return NULL != 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;
   }
 
   /**
-   * Called shortly before mPropertyOwner is destroyed, along with its property.
+   * From AnimatorBase.
    */
-  virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
+  virtual bool Orphan()
   {
-    mPropertyOwner = NULL;
-    mPropertyAccessor.Reset();
+    return (mPropertyOwner == NULL);
   }
 
 private:
@@ -277,12 +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 )
+    mAnimatorFunction( animatorFunction ),
+    mCurrentProgress( 0.0f )
   {
+    mPropertyOwner->AddObserver(*this);
   }
 
   // Undefined
@@ -296,15 +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(){}
 
-// Common Update functions
+  /*
+   * Virtual destructor (Intended as base class)
+   */
+  virtual ~AnimatorFunctionBase(){}
 
-struct AnimateByFloat
+  ///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;
+  }
+
+  virtual Vector4 operator()(float progress, const Vector4& property)
+  {
+    return property;
+  }
+
+  virtual Quaternion operator()(float progress, const Quaternion& property)
+  {
+    return property;
+  }
+};
+
+// Update functions
+
+struct AnimateByFloat : public AnimatorFunctionBase
 {
   AnimateByFloat(const float& relativeValue)
   : mRelative(relativeValue)
@@ -319,7 +432,7 @@ struct AnimateByFloat
   float mRelative;
 };
 
-struct AnimateToFloat
+struct AnimateToFloat : public AnimatorFunctionBase
 {
   AnimateToFloat(const float& targetValue)
   : mTarget(targetValue)
@@ -334,7 +447,37 @@ struct AnimateToFloat
   float mTarget;
 };
 
-struct AnimateByVector2
+struct AnimateByInteger : public AnimatorFunctionBase
+{
+  AnimateByInteger(const int& relativeValue)
+  : mRelative(relativeValue)
+  {
+  }
+
+  float operator()(float alpha, const int& property)
+  {
+    return int(property + mRelative * alpha + 0.5f );
+  }
+
+  int mRelative;
+};
+
+struct AnimateToInteger : public AnimatorFunctionBase
+{
+  AnimateToInteger(const int& targetValue)
+  : mTarget(targetValue)
+  {
+  }
+
+  float operator()(float alpha, const int& property)
+  {
+    return int(property + ((mTarget - property) * alpha) + 0.5f);
+  }
+
+  int mTarget;
+};
+
+struct AnimateByVector2 : public AnimatorFunctionBase
 {
   AnimateByVector2(const Vector2& relativeValue)
   : mRelative(relativeValue)
@@ -349,7 +492,7 @@ struct AnimateByVector2
   Vector2 mRelative;
 };
 
-struct AnimateToVector2
+struct AnimateToVector2 : public AnimatorFunctionBase
 {
   AnimateToVector2(const Vector2& targetValue)
   : mTarget(targetValue)
@@ -364,7 +507,7 @@ struct AnimateToVector2
   Vector2 mTarget;
 };
 
-struct AnimateByVector3
+struct AnimateByVector3 : public AnimatorFunctionBase
 {
   AnimateByVector3(const Vector3& relativeValue)
   : mRelative(relativeValue)
@@ -379,7 +522,7 @@ struct AnimateByVector3
   Vector3 mRelative;
 };
 
-struct AnimateToVector3
+struct AnimateToVector3 : public AnimatorFunctionBase
 {
   AnimateToVector3(const Vector3& targetValue)
   : mTarget(targetValue)
@@ -394,7 +537,7 @@ struct AnimateToVector3
   Vector3 mTarget;
 };
 
-struct AnimateByVector4
+struct AnimateByVector4 : public AnimatorFunctionBase
 {
   AnimateByVector4(const Vector4& relativeValue)
   : mRelative(relativeValue)
@@ -409,7 +552,7 @@ struct AnimateByVector4
   Vector4 mRelative;
 };
 
-struct AnimateToVector4
+struct AnimateToVector4 : public AnimatorFunctionBase
 {
   AnimateToVector4(const Vector4& targetValue)
   : mTarget(targetValue)
@@ -424,7 +567,7 @@ struct AnimateToVector4
   Vector4 mTarget;
 };
 
-struct AnimateByOpacity
+struct AnimateByOpacity : public AnimatorFunctionBase
 {
   AnimateByOpacity(const float& relativeValue)
   : mRelative(relativeValue)
@@ -442,7 +585,7 @@ struct AnimateByOpacity
   float mRelative;
 };
 
-struct AnimateToOpacity
+struct AnimateToOpacity : public AnimatorFunctionBase
 {
   AnimateToOpacity(const float& targetValue)
   : mTarget(targetValue)
@@ -460,7 +603,7 @@ struct AnimateToOpacity
   float mTarget;
 };
 
-struct AnimateByBoolean
+struct AnimateByBoolean : public AnimatorFunctionBase
 {
   AnimateByBoolean(bool relativeValue)
   : mRelative(relativeValue)
@@ -476,7 +619,7 @@ struct AnimateByBoolean
   bool mRelative;
 };
 
-struct AnimateToBoolean
+struct AnimateToBoolean : public AnimatorFunctionBase
 {
   AnimateToBoolean(bool targetValue)
   : mTarget(targetValue)
@@ -492,7 +635,7 @@ struct AnimateToBoolean
   bool mTarget;
 };
 
-struct RotateByAngleAxis
+struct RotateByAngleAxis : public AnimatorFunctionBase
 {
   RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
   : mAngleRadians(angleRadians),
@@ -514,7 +657,7 @@ struct RotateByAngleAxis
   Vector4 mAxis;
 };
 
-struct RotateToQuaternion
+struct RotateToQuaternion : public AnimatorFunctionBase
 {
   RotateToQuaternion(const Quaternion& targetValue)
   : mTarget(targetValue)
@@ -530,7 +673,7 @@ struct RotateToQuaternion
 };
 
 
-struct KeyFrameBooleanFunctor
+struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
 {
   KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
   : mKeyFrames(keyFrames)
@@ -541,7 +684,7 @@ struct KeyFrameBooleanFunctor
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
     }
     return property;
   }
@@ -549,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)
   {
   }
 
@@ -560,18 +703,39 @@ struct KeyFrameNumberFunctor
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, mInterpolation);
     }
     return property;
   }
 
   KeyFrameNumberPtr mKeyFrames;
+  Interpolation mInterpolation;
 };
 
-struct KeyFrameVector2Functor
+struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
 {
-  KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
-  : mKeyFrames(keyFrames)
+  KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
+  {
+  }
+
+  float operator()(float progress, const int& property)
+  {
+    if(mKeyFrames->IsActive(progress))
+    {
+      return mKeyFrames->GetValue(progress, mInterpolation);
+    }
+    return property;
+  }
+
+  KeyFrameIntegerPtr mKeyFrames;
+  Interpolation mInterpolation;
+};
+
+struct KeyFrameVector2Functor : public AnimatorFunctionBase
+{
+  KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
+  : mKeyFrames(keyFrames),mInterpolation(interpolation)
   {
   }
 
@@ -579,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)
   {
   }
 
@@ -599,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)
   {
   }
 
@@ -618,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)
@@ -637,7 +804,7 @@ struct KeyFrameQuaternionFunctor
   {
     if(mKeyFrames->IsActive(progress))
     {
-      return mKeyFrames->GetValue(progress);
+      return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
     }
     return property;
   }
@@ -645,7 +812,39 @@ struct KeyFrameQuaternionFunctor
   KeyFrameQuaternionPtr mKeyFrames;
 };
 
+struct PathPositionFunctor : public AnimatorFunctionBase
+{
+  PathPositionFunctor( PathPtr path )
+  : mPath(path)
+  {
+  }
+
+  Vector3 operator()(float progress, const Vector3& property)
+  {
+    return mPath->SamplePosition(progress );
+  }
+
+  PathPtr mPath;
+};
 
+struct PathRotationFunctor : public AnimatorFunctionBase
+{
+  PathRotationFunctor( PathPtr path, const Vector3& forward )
+  : mPath(path),
+    mForward( forward )
+  {
+    mForward.Normalize();
+  }
+
+  Quaternion operator()(float progress, const Quaternion& property)
+  {
+    Vector3 tangent( mPath->SampleTangent(progress) );
+    return Quaternion( mForward, tangent );
+  }
+
+  PathPtr mPath;
+  Vector3 mForward;
+};
 
 
 } // namespace Internal