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>
36 float DefaultAlphaFunc(float progress)
38 return progress; // linear
41 Animation::Animation( float durationSeconds, float speedFactor, const Vector2& playRange, bool isLooping, Dali::Animation::EndAction endAction, Dali::Animation::EndAction disconnectAction )
42 : mDurationSeconds(durationSeconds),
43 mSpeedFactor( speedFactor ),
45 mEndAction(endAction),
46 mDisconnectAction(disconnectAction),
48 mElapsedSeconds(playRange.x*mDurationSeconds),
50 mPlayRange( playRange )
54 Animation::~Animation()
58 void Animation::SetDuration(float durationSeconds)
60 mDurationSeconds = durationSeconds;
63 void Animation::SetLooping(bool looping)
68 void Animation::SetEndAction(Dali::Animation::EndAction action)
73 void Animation::SetDisconnectAction(Dali::Animation::EndAction action)
75 if ( mDisconnectAction != action )
77 mDisconnectAction = action;
79 for ( AnimatorIter iter = mAnimators.Begin(), endIter = mAnimators.End(); iter != endIter; ++iter )
81 (*iter)->SetDisconnectAction( action );
86 void Animation::SetPlayRange( const Vector2& range )
90 //Make sure mElapsedSeconds is within the new range
91 mElapsedSeconds = Dali::Clamp(mElapsedSeconds, mPlayRange.x*mDurationSeconds , mPlayRange.y*mDurationSeconds );
94 void Animation::Play()
98 if ( mSpeedFactor < 0.0f && mElapsedSeconds <= mPlayRange.x*mDurationSeconds )
100 mElapsedSeconds = mPlayRange.y * mDurationSeconds;
103 SetAnimatorsActive( true );
106 void Animation::PlayFrom( float progress )
108 //If the animation is already playing this has no effect
109 if( mState != Playing )
111 mElapsedSeconds = progress * mDurationSeconds;
114 SetAnimatorsActive( true );
118 void Animation::Pause()
120 if (mState == Playing)
126 void Animation::Bake(BufferIndex bufferIndex, EndAction action)
128 if( action == Dali::Animation::BakeFinal )
130 if( mSpeedFactor > 0.0f )
132 mElapsedSeconds = mPlayRange.y*mDurationSeconds + Math::MACHINE_EPSILON_1; // Force animation to reach it's end
136 mElapsedSeconds = mPlayRange.x*mDurationSeconds - Math::MACHINE_EPSILON_1; //Force animation to reach it's beginning
140 UpdateAnimators( bufferIndex, true/*bake the final result*/, true /*animation finished*/ );
143 void Animation::SetAnimatorsActive( bool active )
145 for ( AnimatorIter iter = mAnimators.Begin(), endIter = mAnimators.End(); iter != endIter; ++iter )
147 (*iter)->SetActive( active );
151 bool Animation::Stop(BufferIndex bufferIndex)
153 bool animationFinished(false);
155 if (mState == Playing || mState == Paused)
157 animationFinished = true; // The actor-thread should be notified of this
159 if( mEndAction != Dali::Animation::Discard )
161 Bake( bufferIndex, mEndAction );
163 // Animators are automatically set to inactive in Bake
167 SetAnimatorsActive( false );
170 // The animation has now been played to completion
174 mElapsedSeconds = mPlayRange.x*mDurationSeconds;
177 return animationFinished;
180 void Animation::OnDestroy(BufferIndex bufferIndex)
182 if (mState == Playing || mState == Paused)
184 if (mEndAction != Dali::Animation::Discard)
186 Bake( bufferIndex, mEndAction );
188 // Animators are automatically set to inactive in Bake
192 SetAnimatorsActive( false );
199 void Animation::AddAnimator( AnimatorBase* animator )
201 animator->SetDisconnectAction( mDisconnectAction );
202 mAnimators.PushBack( animator );
205 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
207 if (mState == Stopped || mState == Destroyed)
209 // Short circuit when animation isn't running
213 // The animation must still be applied when Paused/Stopping
214 if (mState == Playing)
216 mElapsedSeconds += elapsedSeconds * mSpeedFactor;
219 Vector2 playRangeSeconds = mPlayRange * mDurationSeconds;
222 if (mElapsedSeconds > playRangeSeconds.y )
224 mElapsedSeconds = playRangeSeconds.x + fmod(mElapsedSeconds, playRangeSeconds.y);
226 else if( mElapsedSeconds < playRangeSeconds.x )
228 mElapsedSeconds = playRangeSeconds.y - fmod(mElapsedSeconds, playRangeSeconds.y);
232 const bool animationFinished(mState == Playing &&
233 (( mSpeedFactor > 0.0f && mElapsedSeconds > playRangeSeconds.y ) ||
234 ( mSpeedFactor < 0.0f && mElapsedSeconds < playRangeSeconds.x ))
237 UpdateAnimators(bufferIndex, animationFinished && (mEndAction != Dali::Animation::Discard), animationFinished);
239 if (animationFinished)
241 // The animation has now been played to completion
244 mElapsedSeconds = playRangeSeconds.x;
248 return animationFinished;
251 void Animation::UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animationFinished )
253 float elapsedSecondsClamped = Clamp( mElapsedSeconds, mPlayRange.x * mDurationSeconds,mPlayRange.y * mDurationSeconds );
255 //Loop through all animators
257 for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
259 AnimatorBase *animator = *iter;
261 if( animator->Orphan() )
263 //Remove animators whose PropertyOwner has been destroyed
264 iter = mAnimators.Erase(iter);
268 if( animator->IsEnabled() )
270 const float initialDelay(animator->GetInitialDelay());
271 if (elapsedSecondsClamped >= initialDelay || mSpeedFactor < 0.0f )
273 // Calculate a progress specific to each individual animator
274 float progress(1.0f);
275 const float animatorDuration = animator->GetDuration();
276 if (animatorDuration > 0.0f) // animators can be "immediate"
278 progress = Clamp((elapsedSecondsClamped - initialDelay) / animatorDuration, 0.0f , 1.0f );
280 animator->Update(bufferIndex, progress, bake);
289 if ( animationFinished )
291 animator->SetActive( false );
296 INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
305 } // namespace SceneGraph
307 } // namespace Internal