Remove current and future memory leaks with messages by forcing the use of OwnerPoint...
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / animation-impl.cpp
index c09c4b1..a57f936 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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
 
 // INTERNAL INCLUDES
-#include <dali/public-api/actors/actor.h>
 #include <dali/public-api/animation/alpha-function.h>
 #include <dali/public-api/animation/time-period.h>
 #include <dali/public-api/common/dali-common.h>
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/math/vector2.h>
 #include <dali/public-api/math/radian.h>
-#include <dali/internal/event/actors/actor-impl.h>
 #include <dali/internal/event/animation/animation-playlist.h>
 #include <dali/internal/event/animation/animator-connector.h>
+#include <dali/internal/event/animation/path-impl.h>
 #include <dali/internal/event/common/notification-manager.h>
 #include <dali/internal/event/common/property-helper.h>
 #include <dali/internal/event/common/stage-impl.h>
@@ -114,17 +113,18 @@ AnimationPtr Animation::New(float durationSeconds)
 }
 
 Animation::Animation( EventThreadServices& eventThreadServices, AnimationPlaylist& playlist, float durationSeconds, EndAction endAction, EndAction disconnectAction, AlphaFunction defaultAlpha )
-: mEventThreadServices( eventThreadServices ),
+: mAnimation( NULL ),
+  mEventThreadServices( eventThreadServices ),
   mPlaylist( playlist ),
-  mAnimation( NULL ),
-  mNotificationCount( 0 ),
-  mFinishedCallback( NULL ),
-  mFinishedCallbackObject( NULL ),
+  mFinishedSignal(),
+  mConnectors(),
+  mConnectorTargetValues(),
+  mPlayRange( Vector2(0.0f,1.0f)),
   mDurationSeconds( durationSeconds ),
   mSpeedFactor(1.0f),
+  mNotificationCount( 0 ),
   mLoopCount(1),
   mCurrentLoop(0),
-  mPlayRange( Vector2(0.0f,1.0f)),
   mEndAction( endAction ),
   mDisconnectAction( disconnectAction ),
   mDefaultAlpha( defaultAlpha ),
@@ -160,14 +160,10 @@ void Animation::CreateSceneObject()
 {
   DALI_ASSERT_DEBUG( mAnimation == NULL );
 
-  // Create a new animation, temporarily owned
-  SceneGraph::Animation* animation = SceneGraph::Animation::New( mDurationSeconds, mSpeedFactor, mPlayRange, mLoopCount, mEndAction, mDisconnectAction );
-
-  // Keep a const pointer to the animation.
-  mAnimation = animation;
-
-  // Transfer animation ownership to the update manager through a message
-  AddAnimationMessage( mEventThreadServices.GetUpdateManager(), animation );
+  // Create a new animation, Keep a const pointer to the animation.
+  mAnimation = SceneGraph::Animation::New( mDurationSeconds, mSpeedFactor, mPlayRange, mLoopCount, mEndAction, mDisconnectAction );
+  OwnerPointer< SceneGraph::Animation > transferOwnership( const_cast< SceneGraph::Animation* >( mAnimation ) );
+  AddAnimationMessage( mEventThreadServices.GetUpdateManager(), transferOwnership );
 }
 
 void Animation::DestroySceneObject()
@@ -267,76 +263,7 @@ void Animation::Play()
 
   mState = Dali::Animation::PLAYING;
 
-  unsigned int connectorTargetValuesIndex( 0 );
-  unsigned int numberOfConnectorTargetValues = mConnectorActorTargetValues.size();
-
-  /*
-   * Loop through all Animator connectors, if connector index matches the current index stored in mConnectorActorTargetValues container then
-   * should apply target values for this index to the Actor.
-   * Confirm object is an actor and it is a POSITION or SIZE Property Index before sending Notify message to Actor.
-   */
-  for ( unsigned int connectorIndex = 0; connectorIndex < mConnectors.Count(); connectorIndex ++)
-  {
-    // Use index to check if the current connector is next in the mConnectorActorTargetValues container, meaning targetValues have been pushed in AnimateXXFunction
-    if ( connectorTargetValuesIndex < numberOfConnectorTargetValues )
-    {
-      ConnectorTargetValues& connectorPair = mConnectorActorTargetValues[ connectorTargetValuesIndex ];
-
-      if ( connectorPair.connectorIndex == connectorIndex )
-      {
-        // Current connector index matches next in the stored connectors with target values so apply target value.
-        connectorTargetValuesIndex++; // Found a match for connector so increment index to next one
-
-        AnimatorConnectorBase* connector = mConnectors[ connectorIndex ];
-
-        Actor* maybeActor = static_cast<Actor*>( connector->GetObject() ); // Only Actors would be in mConnectorActorTargetValues container
-
-        if ( maybeActor )
-        {
-          // Get Stored Target Value and corresponding connector index
-          const Property::Type valueType = connectorPair.targetValue.GetType();
-          Property::Index propertyIndex = connector->GetPropertyIndex();
-
-          if ( valueType == Property::VECTOR3 )
-          {
-            Vector3 targetVector3 = connectorPair.targetValue.Get<Vector3>();
-
-            if ( propertyIndex == Dali::Actor::Property::POSITION )
-            {
-              maybeActor->NotifyPositionAnimation( *this, targetVector3 );
-            }
-            else if ( propertyIndex == Dali::Actor::Property::SIZE )
-            {
-              maybeActor->NotifySizeAnimation( *this, targetVector3 );
-            }
-          }
-          else if ( valueType == Property::FLOAT )
-          {
-            float targetFloat = connectorPair.targetValue.Get<float>();
-
-            if ( ( Dali::Actor::Property::POSITION_X == propertyIndex ) ||
-                 ( Dali::Actor::Property::POSITION_Y == propertyIndex ) ||
-                 ( Dali::Actor::Property::POSITION_Z == propertyIndex ) )
-            {
-              maybeActor->NotifyPositionAnimation( *this, targetFloat, propertyIndex );
-            }
-            else if ( ( Dali::Actor::Property::SIZE_WIDTH == propertyIndex ) ||
-                    ( Dali::Actor::Property::SIZE_HEIGHT == propertyIndex ) ||
-                    ( Dali::Actor::Property::SIZE_DEPTH == propertyIndex ) )
-
-            {
-              maybeActor->NotifySizeAnimation( *this, targetFloat, propertyIndex );
-            }
-          }
-          else
-          {
-            // Currently only FLOAT and VECTOR3 is supported for Target values in AnimateXXFunctions
-            DALI_LOG_WARNING("Animation::Play Unsupported Value Type provided as TargetValue\n");
-          }
-        }
-      }
-    }
-  }
+  NotifyObjects();
 
   // mAnimation is being used in a separate thread; queue a Play message
   PlayAnimationMessage( mEventThreadServices, *mAnimation );
@@ -351,6 +278,8 @@ void Animation::PlayFrom( float progress )
 
     mState = Dali::Animation::PLAYING;
 
+    NotifyObjects();
+
     // mAnimation is being used in a separate thread; queue a Play message
     PlayAnimationFromMessage( mEventThreadServices, *mAnimation, progress );
   }
@@ -384,6 +313,9 @@ void Animation::Clear()
   // Remove all the connectors
   mConnectors.Clear();
 
+  // Reset the connector target values
+  mConnectorTargetValues.clear();
+
   // Replace the old scene-object with a new one
   DestroySceneObject();
   CreateSceneObject();
@@ -412,12 +344,28 @@ void Animation::AnimateBy(Property& target, Property::Value& relativeValue, Time
 
 void Animation::AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha, TimePeriod period)
 {
-  Object& object = GetImplementation( target.object );
-  const Property::Type targetType = object.GetPropertyType( target.propertyIndex );
+  Object& object = GetImplementation(target.object);
+  const Property::Type targetType = object.GetPropertyType(target.propertyIndex);
   const Property::Type destinationType = relativeValue.GetType();
-  DALI_ASSERT_ALWAYS( targetType == destinationType && "Animated value and Property type don't match" );
 
-  ExtendDuration( period );
+  if ( object.GetPropertyComponentIndex( target.propertyIndex ) != Property::INVALID_COMPONENT_INDEX )
+  {
+    DALI_ASSERT_ALWAYS(Property::FLOAT == destinationType && "Animated value and Property type don't match");
+  }
+  else
+  {
+    DALI_ASSERT_ALWAYS(targetType == destinationType && "Animated value and Property type don't match");
+  }
+
+  ExtendDuration(period);
+
+  // Store data to later notify the object that its property is being animated
+  ConnectorTargetValues connectorPair;
+  connectorPair.targetValue = relativeValue;
+  connectorPair.connectorIndex = mConnectors.Count();
+  connectorPair.timePeriod = period;
+  connectorPair.animatorType = Animation::BY;
+  mConnectorTargetValues.push_back( connectorPair );
 
   switch ( targetType )
   {
@@ -546,6 +494,14 @@ void Animation::AnimateTo(Object& targetObject, Property::Index targetPropertyIn
 
   ExtendDuration( period );
 
+  // Store data to later notify the object that its property is being animated
+  ConnectorTargetValues connectorPair;
+  connectorPair.targetValue = destinationValue;
+  connectorPair.connectorIndex = mConnectors.Count();
+  connectorPair.timePeriod = period;
+  connectorPair.animatorType = Animation::TO;
+  mConnectorTargetValues.push_back( connectorPair );
+
   switch ( destinationType )
   {
     case Property::BOOLEAN:
@@ -572,26 +528,6 @@ void Animation::AnimateTo(Object& targetObject, Property::Index targetPropertyIn
 
     case Property::FLOAT:
     {
-      if ( ( Dali::Actor::Property::SIZE_WIDTH == targetPropertyIndex ) ||
-           ( Dali::Actor::Property::SIZE_HEIGHT == targetPropertyIndex ) ||
-           ( Dali::Actor::Property::SIZE_DEPTH == targetPropertyIndex )  ||
-           ( Dali::Actor::Property::POSITION_X == targetPropertyIndex ) ||
-           ( Dali::Actor::Property::POSITION_Y == targetPropertyIndex ) ||
-           ( Dali::Actor::Property::POSITION_Z == targetPropertyIndex ) )
-      {
-
-        Actor* maybeActor = dynamic_cast<Actor*>( &targetObject );
-        if ( maybeActor )
-        {
-          // Store data to later notify the actor that its size or position is being animated
-          ConnectorTargetValues connectorPair;
-          connectorPair.targetValue = destinationValue;
-          connectorPair.connectorIndex = mConnectors.Count();
-
-          mConnectorActorTargetValues.push_back( connectorPair );
-        }
-      }
-
       AddAnimatorConnector( AnimatorConnector<float>::New( targetObject,
                                                            targetPropertyIndex,
                                                            componentIndex,
@@ -614,21 +550,6 @@ void Animation::AnimateTo(Object& targetObject, Property::Index targetPropertyIn
 
     case Property::VECTOR3:
     {
-      if ( Dali::Actor::Property::SIZE == targetPropertyIndex || Dali::Actor::Property::POSITION == targetPropertyIndex )
-      {
-        // Test whether this is actually an Actor
-        Actor* maybeActor = dynamic_cast<Actor*>( &targetObject );
-        if ( maybeActor )
-        {
-          // Store data to later notify the actor that its size or position is being animated
-          ConnectorTargetValues connectorPair;
-          connectorPair.targetValue = destinationValue;
-          connectorPair.connectorIndex = mConnectors.Count();
-
-          mConnectorActorTargetValues.push_back( connectorPair );
-        }
-      }
-
       AddAnimatorConnector( AnimatorConnector<Vector3>::New( targetObject,
                                                              targetPropertyIndex,
                                                              componentIndex,
@@ -708,6 +629,14 @@ void Animation::AnimateBetween(Property target, const KeyFrames& keyFrames, Alph
 
   ExtendDuration( period );
 
+  // Store data to later notify the object that its property is being animated
+  ConnectorTargetValues connectorPair;
+  connectorPair.targetValue = keyFrames.GetLastKeyFrameValue();
+  connectorPair.connectorIndex = mConnectors.Count();
+  connectorPair.timePeriod = period;
+  connectorPair.animatorType = BETWEEN;
+  mConnectorTargetValues.push_back( connectorPair );
+
   switch(keyFrames.GetType())
   {
     case Dali::Property::BOOLEAN:
@@ -848,12 +777,6 @@ void Animation::EmitSignalFinish()
     Dali::Animation handle( this );
     mFinishedSignal.Emit( handle );
   }
-
-  // This callback is used internally, to avoid the overhead of using a signal.
-  if ( mFinishedCallback )
-  {
-    mFinishedCallback( mFinishedCallbackObject );
-  }
 }
 
 bool Animation::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
@@ -874,12 +797,6 @@ bool Animation::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface*
   return connected;
 }
 
-void Animation::SetFinishedCallback( FinishedCallback callback, Object* object )
-{
-  mFinishedCallback = callback;
-  mFinishedCallbackObject = object;
-}
-
 void Animation::AddAnimatorConnector( AnimatorConnectorBase* connector )
 {
   DALI_ASSERT_DEBUG( NULL != connector );
@@ -1055,6 +972,33 @@ Vector2 Animation::GetPlayRange() const
   return mPlayRange;
 }
 
+bool Animation::CompareConnectorEndTimes( const Animation::ConnectorTargetValues& lhs, const Animation::ConnectorTargetValues& rhs )
+{
+  return ( ( lhs.timePeriod.delaySeconds + lhs.timePeriod.durationSeconds ) < ( rhs.timePeriod.delaySeconds + rhs.timePeriod.durationSeconds ) );
+}
+
+void Animation::NotifyObjects()
+{
+  if( mEndAction != EndAction::Discard ) // If the animation is discarded, then we do not want to change the target values
+  {
+    // Sort according to end time with earlier end times coming first, if the end time is the same, then the connectors are not moved
+    std::stable_sort( mConnectorTargetValues.begin(), mConnectorTargetValues.end(), CompareConnectorEndTimes );
+
+    // Loop through all connector target values sorted by increasing end time
+    ConnectorTargetValuesContainer::const_iterator iter = mConnectorTargetValues.begin();
+    const ConnectorTargetValuesContainer::const_iterator endIter = mConnectorTargetValues.end();
+    for( ; iter != endIter; ++iter )
+    {
+      AnimatorConnectorBase* connector = mConnectors[ iter->connectorIndex ];
+
+      Object* object = connector->GetObject();
+      if( object )
+      {
+        object->NotifyPropertyAnimation( *this, connector->GetPropertyIndex(), iter->targetValue, iter->animatorType );
+      }
+    }
+  }
+}
 
 } // namespace Internal