[4.0] Changed Update to reset only target properties each frame
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / animator-connector.h
index 74f7510..8119063 100644 (file)
@@ -2,7 +2,7 @@
 #define __DALI_INTERNAL_ANIMATOR_CONNECTOR_H__
 
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
  */
 
 // INTERNAL INCLUDES
-#include <dali/internal/event/common/object-impl.h>
 #include <dali/internal/event/animation/animator-connector-base.h>
 #include <dali/internal/event/animation/animation-impl.h>
 #include <dali/internal/update/common/property-owner.h>
 #include <dali/internal/update/animation/property-accessor.h>
 #include <dali/internal/update/animation/property-component-accessor.h>
-#include <dali/internal/update/animation/scene-graph-animator.h>
+#include <dali/internal/update/common/property-resetter.h>
 #include <dali/internal/update/manager/update-manager.h>
 
 namespace Dali
@@ -35,16 +34,14 @@ namespace Internal
 {
 
 /**
- * AnimatorConnector is used to connect SceneGraph::Animators for newly created scene-graph objects.
- *
- * The scene-graph objects are created by a Object e.g. Actor is a proxy for SceneGraph::Node.
- * AnimatorConnector observes the proxy object, in order to detect when a scene-graph object is created.
+ * AnimatorConnector is used to connect SceneGraph::Animators and
+ * PropertyResetters for newly created scene-graph objects.
  *
  * SceneGraph::Animators weakly reference scene objects, and are automatically deleted when orphaned.
  * Therefore the AnimatorConnector is NOT responsible for disconnecting animators.
  */
 template < typename PropertyType >
-class AnimatorConnector : public AnimatorConnectorBase, public Object::Observer
+class AnimatorConnector : public AnimatorConnectorBase
 {
 public:
 
@@ -86,8 +83,8 @@ public:
       mObject->RemoveObserver( *this );
     }
 
-    //If there is not a SceneGraph::Animator, the AnimatorConnector is responsible for deleting the mAnimatorFunction
-    //otherwise, the animator function ownership is transferred to the SceneGraph::Animator
+    // If there is not a SceneGraph::Animator, the AnimatorConnector is responsible for deleting the mAnimatorFunction
+    // otherwise, the animator function ownership is transferred to the SceneGraph::Animator
     if( !mAnimator )
     {
       delete mAnimatorFunction;
@@ -121,14 +118,10 @@ private:
                      Internal::AnimatorFunctionBase* animatorFunction,
                      AlphaFunction alpha,
                      const TimePeriod& period )
-  : AnimatorConnectorBase( alpha, period ),
-    mObject( &object ),
+  : AnimatorConnectorBase( object, propertyIndex, componentIndex, alpha, period ),
     mAnimator(0),
-    mPropertyIndex( propertyIndex ),
-    mComponentIndex( componentIndex ),
     mAnimatorFunction( animatorFunction )
   {
-    object.AddObserver( *this );
   }
 
   // Undefined
@@ -143,29 +136,16 @@ private:
   virtual void SceneObjectAdded( Object& object )
   {
     //If the animator has not been created yet, create it now.
-    if( !mAnimator )
+    if( !mAnimator && mObject )
     {
       CreateAnimator();
     }
   }
 
-  /**
-   * From Object::Observer
-   */
-  virtual void SceneObjectRemoved( Object& object )
-  {
-  }
-
-  /**
-   * From Object::Observer
-   */
-  virtual void ObjectDestroyed( Object& object )
-  {
-    mObject = NULL;
-  }
-
    /**
-   * Helper function to create a Scenegraph::Animator and add it to its correspondent SceneGraph::Animation.
+   * Helper function to create a Scenegraph::Animator and PropertyResetter and add it to its correspondent
+   * SceneGraph::Animation.
+   *
    * @note This function will only be called the first time the object is added to the scene or at creation time if
    * the object was already in the scene
    */
@@ -178,10 +158,12 @@ private:
     //Get the PropertyOwner the animator is going to animate
     const SceneGraph::PropertyOwner* propertyOwner = mObject->GetSceneObject();
 
-    //Get SceneGraph::BaseProperty
+    // Get SceneGraph::BaseProperty
     const SceneGraph::PropertyBase* baseProperty = mObject->GetSceneObjectAnimatableProperty( mPropertyIndex );
 
-    //Check if property is a component of another property
+    OwnerPointer<SceneGraph::PropertyResetterBase> resetter;
+
+    // Check if property is a component of another property
     const int componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
     if( componentIndex != Property::INVALID_COMPONENT_INDEX )
     {
@@ -190,118 +172,208 @@ private:
 
     if( mComponentIndex == Property::INVALID_COMPONENT_INDEX )
     {
-      ///Animating the whole property
+      // Animating the whole property
 
-      //Cast to AnimatableProperty
+      // Cast to AnimatableProperty
       const PropertyInterfaceType* animatableProperty = dynamic_cast< const PropertyInterfaceType* >( baseProperty );
 
-      //Dynamic cast will fail if BaseProperty is not an AnimatableProperty
-      DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
-
-      //Create the animator
-      mAnimator = AnimatorType::New( *propertyOwner, *animatableProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
+      if( animatableProperty == NULL )
+      {
+        if( baseProperty->IsTransformManagerProperty() )
+        {
+          mAnimator = SceneGraph::AnimatorTransformProperty< PropertyType,TransformManagerPropertyAccessor<PropertyType> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
+          // Don't reset transform manager properties - TransformManager will do it more efficiently
+        }
+        else
+        {
+          DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
+        }
 
+      }
+      else
+      {
+        // Create the animator and resetter
+        mAnimator = AnimatorType::New( *propertyOwner, *animatableProperty, mAnimatorFunction,
+                                       mAlphaFunction, mTimePeriod );
+        resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
+      }
     }
     else
     {
-      ///Animating a component of the property
-
-      //Vector3
-      if ( PropertyTypes::Get< Vector3 >() == baseProperty->GetType() )
       {
-        // Cast to AnimatableProperty of type Vector3
-        const SceneGraph::AnimatableProperty<Vector3>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( baseProperty );
+        // Animating a component of the property
+        if ( PropertyTypes::Get< Vector2 >() == baseProperty->GetType() )
+        {
+          // Animate float component of Vector2 property
 
-        //Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
-        DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
+          // Cast to AnimatableProperty of type Vector2
+          const SceneGraph::AnimatableProperty<Vector2>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector2>* >( baseProperty );
+          DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
 
-        switch( mComponentIndex )
-        {
-          case 0:
+          switch( mComponentIndex )
           {
-            mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( *propertyOwner,
-                                                                                                 *animatableProperty,
-                                                                                                 mAnimatorFunction,
-                                                                                                 mAlphaFunction,
-                                                                                                 mTimePeriod );
-            break;
+            case 0:
+            {
+              mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector2> >::New( *propertyOwner,
+                                                                                                   *animatableProperty,
+                                                                                                   mAnimatorFunction,
+                                                                                                   mAlphaFunction,
+                                                                                                   mTimePeriod );
+              break;
+            }
+            case 1:
+            {
+              mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector2> >::New( *propertyOwner,
+                                                                                                   *animatableProperty,
+                                                                                                   mAnimatorFunction,
+                                                                                                   mAlphaFunction,
+                                                                                                   mTimePeriod );
+              break;
+            }
+            default:
+            {
+              break;
+            }
           }
-          case 1:
+
+          if( mAnimator != nullptr )
           {
-            mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( *propertyOwner,
-                                                                                                 *animatableProperty,
-                                                                                                 mAnimatorFunction,
-                                                                                                 mAlphaFunction,
-                                                                                                 mTimePeriod );
-            break;
+            resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
           }
-          case 2:
+        }
+
+        else if ( PropertyTypes::Get< Vector3 >() == baseProperty->GetType() )
+        {
+          // Animate float component of Vector3 property
+          // Cast to AnimatableProperty of type Vector3
+          const SceneGraph::AnimatableProperty<Vector3>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector3>* >( baseProperty );
+
+          if( animatableProperty == NULL )
           {
-            mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( *propertyOwner,
-                                                                                                 *animatableProperty,
-                                                                                                 mAnimatorFunction,
-                                                                                                 mAlphaFunction,
-                                                                                                 mTimePeriod );
-            break;
+            if( baseProperty->IsTransformManagerProperty() )
+            {
+              if( mComponentIndex == 0 )
+              {
+                mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,0> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
+              }
+              else if( mComponentIndex == 1 )
+              {
+                mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,1> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
+              }
+              else if( mComponentIndex == 2 )
+              {
+                mAnimator = SceneGraph::AnimatorTransformProperty< float,TransformManagerPropertyComponentAccessor<Vector3,2> >::New( *propertyOwner, *baseProperty, mAnimatorFunction, mAlphaFunction, mTimePeriod );
+              }
+            }
+            else
+            {
+              DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
+            }
+            // Don't manually reset transform property - TransformManager will do it more efficiently
           }
-          default:
+          else
           {
-            break;
+            // Dynamic cast will fail if BaseProperty is not a Vector3 AnimatableProperty
+            DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
+
+            switch( mComponentIndex )
+            {
+              case 0:
+              {
+                mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector3> >::New( *propertyOwner,
+                                                                                                     *animatableProperty,
+                                                                                                     mAnimatorFunction,
+                                                                                                     mAlphaFunction,
+                                                                                                     mTimePeriod );
+                break;
+              }
+              case 1:
+              {
+                mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector3> >::New( *propertyOwner,
+                                                                                                     *animatableProperty,
+                                                                                                     mAnimatorFunction,
+                                                                                                     mAlphaFunction,
+                                                                                                     mTimePeriod );
+                break;
+              }
+              case 2:
+              {
+                mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector3> >::New( *propertyOwner,
+                                                                                                     *animatableProperty,
+                                                                                                     mAnimatorFunction,
+                                                                                                     mAlphaFunction,
+                                                                                                     mTimePeriod );
+                break;
+              }
+              default:
+              {
+                break;
+              }
+            }
+
+            if( mAnimator != nullptr )
+            {
+              resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
+            }
           }
         }
-      }
-      else if ( PropertyTypes::Get< Vector4 >() == baseProperty->GetType() )
-      {
-        // Animate float component of Vector4 property
+        else if ( PropertyTypes::Get< Vector4 >() == baseProperty->GetType() )
+        {
+          // Animate float component of Vector4 property
 
-        // Cast to AnimatableProperty of type Vector4
-        const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( baseProperty );
+          // Cast to AnimatableProperty of type Vector4
+          const SceneGraph::AnimatableProperty<Vector4>* animatableProperty = dynamic_cast< const SceneGraph::AnimatableProperty<Vector4>* >( baseProperty );
 
-        //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
-        DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
+          //Dynamic cast will fail if BaseProperty is not a Vector4 AnimatableProperty
+          DALI_ASSERT_DEBUG( animatableProperty != NULL && "Animating non-animatable property" );
 
-        switch( mComponentIndex )
-        {
-          case 0:
+          switch( mComponentIndex )
           {
-            mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( *propertyOwner,
-                                                                                                 *animatableProperty,
-                                                                                                 mAnimatorFunction,
-                                                                                                 mAlphaFunction,
-                                                                                                 mTimePeriod );
-            break;
+            case 0:
+            {
+              mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorX<Vector4> >::New( *propertyOwner,
+                                                                                                   *animatableProperty,
+                                                                                                   mAnimatorFunction,
+                                                                                                   mAlphaFunction,
+                                                                                                   mTimePeriod );
+              break;
+            }
+            case 1:
+            {
+              mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( *propertyOwner,
+                                                                                                   *animatableProperty,
+                                                                                                   mAnimatorFunction,
+                                                                                                   mAlphaFunction,
+                                                                                                   mTimePeriod );
+              break;
+            }
+            case 2:
+            {
+              mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( *propertyOwner,
+                                                                                                   *animatableProperty,
+                                                                                                   mAnimatorFunction,
+                                                                                                   mAlphaFunction,
+                                                                                                   mTimePeriod );
+              break;
+            }
+            case 3:
+            {
+              mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( *propertyOwner,
+                                                                                                   *animatableProperty,
+                                                                                                   mAnimatorFunction,
+                                                                                                   mAlphaFunction,
+                                                                                                   mTimePeriod );
+              break;
+            }
+
+            default:
+            {
+              break;
+            }
           }
-          case 1:
+          if( mAnimator != nullptr )
           {
-            mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorY<Vector4> >::New( *propertyOwner,
-                                                                                                 *animatableProperty,
-                                                                                                 mAnimatorFunction,
-                                                                                                 mAlphaFunction,
-                                                                                                 mTimePeriod );
-            break;
-          }
-          case 2:
-          {
-            mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorZ<Vector4> >::New( *propertyOwner,
-                                                                                                 *animatableProperty,
-                                                                                                 mAnimatorFunction,
-                                                                                                 mAlphaFunction,
-                                                                                                 mTimePeriod );
-            break;
-          }
-          case 3:
-          {
-            mAnimator = SceneGraph::Animator< float, PropertyComponentAccessorW<Vector4> >::New( *propertyOwner,
-                                                                                                 *animatableProperty,
-                                                                                                 mAnimatorFunction,
-                                                                                                 mAlphaFunction,
-                                                                                                 mTimePeriod );
-            break;
-          }
-
-          default:
-          {
-            break;
+            resetter = SceneGraph::AnimatorResetter::New( *propertyOwner, *baseProperty, *mAnimator );
           }
         }
       }
@@ -313,16 +385,18 @@ private:
     const SceneGraph::Animation* animation = mParent->GetSceneObject();
     DALI_ASSERT_DEBUG( NULL != animation );
     AddAnimatorMessage( mParent->GetEventThreadServices(), *animation, *mAnimator );
+
+    // Add the new SceneGraph::PropertyResetter to the update manager via message
+    if( resetter != nullptr )
+    {
+      AddResetterMessage( mParent->GetEventThreadServices().GetUpdateManager(), resetter );
+    }
   }
 
 protected:
 
-  Object* mObject; ///< Not owned by the animator connector. Valid until ObjectDestroyed() is called.
   SceneGraph::AnimatorBase* mAnimator;
 
-  Property::Index mPropertyIndex;
-  int mComponentIndex;
-
   Internal::AnimatorFunctionBase* mAnimatorFunction;  ///< Owned by the animator connector until an Scenegraph::Animator is created
 };