Implement Animation PlayAfter() API
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-animation.cpp
index 877010c..9ce8ef0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 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.
@@ -24,7 +24,7 @@
 // INTERNAL INCLUDES
 #include <dali/internal/common/memory-pool-object-allocator.h>
 #include <dali/internal/render/common/performance-monitor.h>
-
+#include <dali/public-api/math/math-utils.h>
 namespace //Unnamed namespace
 {
 //Memory pool used to allocate new animations. Memory used by this pool will be released when shutting down DALi
@@ -32,18 +32,24 @@ Dali::Internal::MemoryPoolObjectAllocator<Dali::Internal::SceneGraph::Animation>
 
 inline void WrapInPlayRange( float& elapsed, const Dali::Vector2& playRangeSeconds)
 {
-  if (elapsed > playRangeSeconds.y )
+  ifelapsed > playRangeSeconds.y )
   {
-    elapsed = playRangeSeconds.x + fmod(elapsed, playRangeSeconds.y);
+    elapsed = playRangeSeconds.x + fmodf((elapsed-playRangeSeconds.x), (playRangeSeconds.y-playRangeSeconds.x));
   }
   else if( elapsed < playRangeSeconds.x )
   {
-    elapsed = playRangeSeconds.y - fmod(elapsed, playRangeSeconds.y);
+    elapsed = playRangeSeconds.y - fmodf( (playRangeSeconds.x - elapsed), (playRangeSeconds.y-playRangeSeconds.x) );
   }
 }
 
+/// Compares the end times of the animators and if the end time is less, then it is moved earlier in the list. If end times are the same, then no change.
+bool CompareAnimatorEndTimes( const Dali::Internal::SceneGraph::AnimatorBase* lhs, const Dali::Internal::SceneGraph::AnimatorBase* rhs )
+{
+  return ( ( lhs->GetIntervalDelay() + lhs->GetDuration() ) < ( rhs->GetIntervalDelay() + rhs->GetDuration() ) );
 }
 
+} // unnamed namespace
+
 namespace Dali
 {
 
@@ -59,16 +65,19 @@ Animation* Animation::New( float durationSeconds, float speedFactor, const Vecto
 }
 
 Animation::Animation( float durationSeconds, float speedFactor, const Vector2& playRange, int loopCount, Dali::Animation::EndAction endAction, Dali::Animation::EndAction disconnectAction )
-: mDurationSeconds(durationSeconds),
+: mPlayRange( playRange ),
+  mDurationSeconds( durationSeconds ),
+  mDelaySeconds( 0.0f ),
+  mElapsedSeconds( playRange.x*mDurationSeconds ),
   mSpeedFactor( speedFactor ),
+  mProgressMarker( 0.0f ),
+  mPlayedCount( 0 ),
+  mLoopCount(loopCount),
+  mCurrentLoop(0),
   mEndAction(endAction),
   mDisconnectAction(disconnectAction),
   mState(Stopped),
-  mElapsedSeconds(playRange.x*mDurationSeconds),
-  mPlayedCount(0),
-  mLoopCount(loopCount),
-  mCurrentLoop(0),
-  mPlayRange( playRange )
+  mProgressReachedSignalRequired( false )
 {
 }
 
@@ -86,6 +95,15 @@ void Animation::SetDuration(float durationSeconds)
   mDurationSeconds = durationSeconds;
 }
 
+void Animation::SetProgressNotification( float progress )
+{
+  mProgressMarker = progress;
+  if ( mProgressMarker > 0.0f )
+  {
+    mProgressReachedSignalRequired = true;
+  }
+}
+
 void Animation::SetLoopCount(int loopCount)
 {
   mLoopCount = loopCount;
@@ -114,12 +132,27 @@ void Animation::SetPlayRange( const Vector2& range )
 {
   mPlayRange = range;
 
-  //Make sure mElapsedSeconds is within the new range
-  mElapsedSeconds = Dali::Clamp(mElapsedSeconds, mPlayRange.x*mDurationSeconds , mPlayRange.y*mDurationSeconds );
+  // Make sure mElapsedSeconds is within the new range
+
+  if( mState == Stopped )
+  {
+    // Ensure that the animation starts at the right place
+    mElapsedSeconds = mPlayRange.x * mDurationSeconds;
+  }
+  else
+  {
+    // If already past the end of the range, but before end of duration, then clamp will
+    // ensure that the animation stops on the next update.
+    // If not yet at the start of the range, clamping will jump to the start
+    mElapsedSeconds = Dali::Clamp(mElapsedSeconds, mPlayRange.x*mDurationSeconds , mPlayRange.y*mDurationSeconds );
+  }
 }
 
 void Animation::Play()
 {
+  // Sort according to end time with earlier end times coming first, if the end time is the same, then the animators are not moved
+  std::stable_sort( mAnimators.Begin(), mAnimators.End(), CompareAnimatorEndTimes );
+
   mState = Playing;
 
   if ( mSpeedFactor < 0.0f && mElapsedSeconds <= mPlayRange.x*mDurationSeconds )
@@ -134,7 +167,8 @@ void Animation::Play()
 
 void Animation::PlayFrom( float progress )
 {
-  //If the animation is already playing this has no effect
+  // If the animation is already playing this has no effect
+  // Progress is guaranteed to be in range.
   if( mState != Playing )
   {
     mElapsedSeconds = progress * mDurationSeconds;
@@ -144,6 +178,24 @@ void Animation::PlayFrom( float progress )
   }
 }
 
+void Animation::PlayAfter( float delaySeconds )
+{
+  if( mState != Playing )
+  {
+    mDelaySeconds = delaySeconds;
+    mState = Playing;
+
+    if ( mSpeedFactor < 0.0f && mElapsedSeconds <= mPlayRange.x*mDurationSeconds )
+    {
+      mElapsedSeconds = mPlayRange.y * mDurationSeconds;
+    }
+
+    SetAnimatorsActive( true );
+
+    mCurrentLoop = 0;
+  }
+}
+
 void Animation::Pause()
 {
   if (mState == Playing)
@@ -226,15 +278,15 @@ void Animation::OnDestroy(BufferIndex bufferIndex)
   mState = Destroyed;
 }
 
-void Animation::AddAnimator( AnimatorBase* animator )
+void Animation::AddAnimator( OwnerPointer<AnimatorBase>& animator )
 {
   animator->ConnectToSceneGraph();
   animator->SetDisconnectAction( mDisconnectAction );
 
-  mAnimators.PushBack( animator );
+  mAnimators.PushBack( animator.Release() );
 }
 
-void Animation::Update(BufferIndex bufferIndex, float elapsedSeconds, bool& looped, bool& finished )
+void Animation::Update(BufferIndex bufferIndex, float elapsedSeconds, bool& looped, bool& finished, bool& progressReached )
 {
   looped = false;
   finished = false;
@@ -248,7 +300,22 @@ void Animation::Update(BufferIndex bufferIndex, float elapsedSeconds, bool& loop
   // The animation must still be applied when Paused/Stopping
   if (mState == Playing)
   {
-    mElapsedSeconds += elapsedSeconds * mSpeedFactor;
+    // If there is delay time before Animation starts, wait the Animation until mDelaySeconds.
+    if( mDelaySeconds > 0)
+    {
+      mDelaySeconds = mDelaySeconds - ( elapsedSeconds * mSpeedFactor );
+    }
+    else
+    {
+      mElapsedSeconds += elapsedSeconds * mSpeedFactor;
+
+      if ( mProgressReachedSignalRequired && ( mElapsedSeconds >= mProgressMarker ) )
+      {
+        // The application should be notified by NotificationManager, in another thread
+        progressReached = true;
+        mProgressReachedSignalRequired = false;
+      }
+    }
   }
 
   Vector2 playRangeSeconds = mPlayRange * mDurationSeconds;
@@ -258,7 +325,7 @@ void Animation::Update(BufferIndex bufferIndex, float elapsedSeconds, bool& loop
     // loop forever
     WrapInPlayRange(mElapsedSeconds, playRangeSeconds);
 
-    UpdateAnimators(bufferIndex, false, false);
+    UpdateAnimators(bufferIndex, false, false );
 
     // don't increment mPlayedCount as event loop tracks this to indicate animation finished (end of all loops)
   }
@@ -269,13 +336,14 @@ void Animation::Update(BufferIndex bufferIndex, float elapsedSeconds, bool& loop
                (( mSpeedFactor > 0.0f && mElapsedSeconds > playRangeSeconds.y )  ||
                 ( mSpeedFactor < 0.0f && mElapsedSeconds < playRangeSeconds.x )) );
 
-    WrapInPlayRange(mElapsedSeconds, playRangeSeconds);
+    WrapInPlayRange( mElapsedSeconds, playRangeSeconds );
 
-    UpdateAnimators(bufferIndex, false, false);
+    UpdateAnimators(bufferIndex, false, false );
 
     if(looped)
     {
       ++mCurrentLoop;
+      mProgressReachedSignalRequired = mProgressMarker > 0.0f;
       // don't increment mPlayedCount until the finished final loop
     }
   }
@@ -287,7 +355,7 @@ void Animation::Update(BufferIndex bufferIndex, float elapsedSeconds, bool& loop
                  ( mSpeedFactor < 0.0f && mElapsedSeconds < playRangeSeconds.x )) );
 
     // update with bake if finished
-    UpdateAnimators(bufferIndex, finished && (mEndAction != Dali::Animation::Discard), finished);
+    UpdateAnimators(bufferIndex, finished && (mEndAction != Dali::Animation::Discard), finished );
 
     if(finished)
     {
@@ -301,16 +369,17 @@ void Animation::Update(BufferIndex bufferIndex, float elapsedSeconds, bool& loop
         DALI_ASSERT_DEBUG(mCurrentLoop == mLoopCount);
       }
 
+      mProgressReachedSignalRequired = mProgressMarker > 0.0f;
       mElapsedSeconds = playRangeSeconds.x;
       mState = Stopped;
     }
   }
-
 }
 
 void Animation::UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animationFinished )
 {
-  float elapsedSecondsClamped = Clamp( mElapsedSeconds, mPlayRange.x * mDurationSeconds,mPlayRange.y * mDurationSeconds );
+  const Vector2 playRange( mPlayRange * mDurationSeconds );
+  float elapsedSecondsClamped = Clamp( mElapsedSeconds, playRange.x, playRange.y );
 
   //Loop through all animators
   bool applied(true);
@@ -327,15 +396,16 @@ void Animation::UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animat
     {
       if( animator->IsEnabled() )
       {
-        const float initialDelay(animator->GetInitialDelay());
-        if (elapsedSecondsClamped >= initialDelay || mSpeedFactor < 0.0f )
+        const float intervalDelay( animator->GetIntervalDelay() );
+
+        if( elapsedSecondsClamped >= intervalDelay )
         {
           // Calculate a progress specific to each individual animator
           float progress(1.0f);
           const float animatorDuration = animator->GetDuration();
           if (animatorDuration > 0.0f) // animators can be "immediate"
           {
-            progress = Clamp((elapsedSecondsClamped - initialDelay) / animatorDuration, 0.0f , 1.0f );
+            progress = Clamp((elapsedSecondsClamped - intervalDelay) / animatorDuration, 0.0f , 1.0f );
           }
           animator->Update(bufferIndex, progress, bake);
         }