2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/update/animation/scene-graph-animation.h>
22 #include <cmath> // fmod
25 #include <dali/internal/render/common/performance-monitor.h>
37 Animation::Animation( float durationSeconds, float speedFactor, const Vector2& playRange, bool isLooping, Dali::Animation::EndAction endAction, Dali::Animation::EndAction disconnectAction )
38 : mDurationSeconds(durationSeconds),
39 mSpeedFactor( speedFactor ),
41 mEndAction(endAction),
42 mDisconnectAction(disconnectAction),
44 mElapsedSeconds(playRange.x*mDurationSeconds),
46 mPlayRange( playRange )
50 Animation::~Animation()
54 void Animation::SetDuration(float durationSeconds)
56 mDurationSeconds = durationSeconds;
59 void Animation::SetLooping(bool looping)
64 void Animation::SetEndAction(Dali::Animation::EndAction action)
69 void Animation::SetDisconnectAction(Dali::Animation::EndAction action)
71 if ( mDisconnectAction != action )
73 mDisconnectAction = action;
75 for ( AnimatorIter iter = mAnimators.Begin(), endIter = mAnimators.End(); iter != endIter; ++iter )
77 (*iter)->SetDisconnectAction( action );
82 void Animation::SetPlayRange( const Vector2& range )
86 //Make sure mElapsedSeconds is within the new range
87 mElapsedSeconds = Dali::Clamp(mElapsedSeconds, mPlayRange.x*mDurationSeconds , mPlayRange.y*mDurationSeconds );
90 void Animation::Play()
94 if ( mSpeedFactor < 0.0f && mElapsedSeconds <= mPlayRange.x*mDurationSeconds )
96 mElapsedSeconds = mPlayRange.y * mDurationSeconds;
99 SetAnimatorsActive( true );
102 void Animation::PlayFrom( float progress )
104 //If the animation is already playing this has no effect
105 if( mState != Playing )
107 mElapsedSeconds = progress * mDurationSeconds;
110 SetAnimatorsActive( true );
114 void Animation::Pause()
116 if (mState == Playing)
122 void Animation::Bake(BufferIndex bufferIndex, EndAction action)
124 if( action == Dali::Animation::BakeFinal )
126 if( mSpeedFactor > 0.0f )
128 mElapsedSeconds = mPlayRange.y*mDurationSeconds + Math::MACHINE_EPSILON_1; // Force animation to reach it's end
132 mElapsedSeconds = mPlayRange.x*mDurationSeconds - Math::MACHINE_EPSILON_1; //Force animation to reach it's beginning
136 UpdateAnimators( bufferIndex, true/*bake the final result*/, true /*animation finished*/ );
139 void Animation::SetAnimatorsActive( bool active )
141 for ( AnimatorIter iter = mAnimators.Begin(), endIter = mAnimators.End(); iter != endIter; ++iter )
143 (*iter)->SetActive( active );
147 bool Animation::Stop(BufferIndex bufferIndex)
149 bool animationFinished(false);
151 if (mState == Playing || mState == Paused)
153 animationFinished = true; // The actor-thread should be notified of this
155 if( mEndAction != Dali::Animation::Discard )
157 Bake( bufferIndex, mEndAction );
159 // Animators are automatically set to inactive in Bake
163 SetAnimatorsActive( false );
166 // The animation has now been played to completion
170 mElapsedSeconds = mPlayRange.x*mDurationSeconds;
173 return animationFinished;
176 void Animation::OnDestroy(BufferIndex bufferIndex)
178 if (mState == Playing || mState == Paused)
180 if (mEndAction != Dali::Animation::Discard)
182 Bake( bufferIndex, mEndAction );
184 // Animators are automatically set to inactive in Bake
188 SetAnimatorsActive( false );
195 void Animation::AddAnimator( AnimatorBase* animator )
197 animator->ConnectToSceneGraph();
198 animator->SetDisconnectAction( mDisconnectAction );
200 mAnimators.PushBack( animator );
203 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
205 if (mState == Stopped || mState == Destroyed)
207 // Short circuit when animation isn't running
211 // The animation must still be applied when Paused/Stopping
212 if (mState == Playing)
214 mElapsedSeconds += elapsedSeconds * mSpeedFactor;
217 Vector2 playRangeSeconds = mPlayRange * mDurationSeconds;
220 if (mElapsedSeconds > playRangeSeconds.y )
222 mElapsedSeconds = playRangeSeconds.x + fmod(mElapsedSeconds, playRangeSeconds.y);
224 else if( mElapsedSeconds < playRangeSeconds.x )
226 mElapsedSeconds = playRangeSeconds.y - fmod(mElapsedSeconds, playRangeSeconds.y);
230 const bool animationFinished(mState == Playing &&
231 (( mSpeedFactor > 0.0f && mElapsedSeconds > playRangeSeconds.y ) ||
232 ( mSpeedFactor < 0.0f && mElapsedSeconds < playRangeSeconds.x ))
235 UpdateAnimators(bufferIndex, animationFinished && (mEndAction != Dali::Animation::Discard), animationFinished);
237 if (animationFinished)
239 // The animation has now been played to completion
242 mElapsedSeconds = playRangeSeconds.x;
246 return animationFinished;
249 void Animation::UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animationFinished )
251 float elapsedSecondsClamped = Clamp( mElapsedSeconds, mPlayRange.x * mDurationSeconds,mPlayRange.y * mDurationSeconds );
253 //Loop through all animators
255 for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
257 AnimatorBase *animator = *iter;
259 if( animator->Orphan() )
261 //Remove animators whose PropertyOwner has been destroyed
262 iter = mAnimators.Erase(iter);
266 if( animator->IsEnabled() )
268 const float initialDelay(animator->GetInitialDelay());
269 if (elapsedSecondsClamped >= initialDelay || mSpeedFactor < 0.0f )
271 // Calculate a progress specific to each individual animator
272 float progress(1.0f);
273 const float animatorDuration = animator->GetDuration();
274 if (animatorDuration > 0.0f) // animators can be "immediate"
276 progress = Clamp((elapsedSecondsClamped - initialDelay) / animatorDuration, 0.0f , 1.0f );
278 animator->Update(bufferIndex, progress, bake);
287 if ( animationFinished )
289 animator->SetActive( false );
294 INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
303 } // namespace SceneGraph
305 } // namespace Internal