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>
38 float DefaultAlphaFunc(float progress)
40 return progress; // linear
43 Animation::Animation(float durationSeconds, float speedFactor, bool isLooping, Dali::Animation::EndAction endAction, Dali::Animation::EndAction destroyAction)
44 : mDurationSeconds(durationSeconds),
45 mSpeedFactor( speedFactor ),
47 mEndAction(endAction),
48 mDestroyAction(destroyAction),
50 mElapsedSeconds(0.0f),
55 Animation::~Animation()
59 void Animation::SetDuration(float durationSeconds)
61 DALI_ASSERT_DEBUG(durationSeconds > 0.0f);
63 mDurationSeconds = durationSeconds;
66 void Animation::SetLooping(bool looping)
71 void Animation::SetEndAction(Dali::Animation::EndAction action)
76 void Animation::SetDestroyAction(Dali::Animation::EndAction action)
78 mDestroyAction = action;
81 void Animation::Play()
85 if ( mSpeedFactor < 0.0f && mElapsedSeconds <= 0.0f )
87 mElapsedSeconds = mDurationSeconds;
91 void Animation::PlayFrom( float progress )
93 //If the animation is already playing this has no effect
94 if( mState != Playing )
96 mElapsedSeconds = progress * mDurationSeconds;
101 void Animation::Pause()
103 if (mState == Playing)
109 void Animation::Bake(BufferIndex bufferIndex, EndAction action)
111 if( action == Dali::Animation::BakeFinal )
113 if( mSpeedFactor > 0.0f )
115 mElapsedSeconds = mDurationSeconds + Math::MACHINE_EPSILON_1; // Force animation to reach it's end
119 mElapsedSeconds = 0.0f - Math::MACHINE_EPSILON_1; //Force animation to reach it's beginning
123 UpdateAnimators(bufferIndex, true/*bake the final result*/);
126 bool Animation::Stop(BufferIndex bufferIndex)
128 bool animationFinished(false);
130 if (mState == Playing || mState == Paused)
132 animationFinished = true; // The actor-thread should be notified of this
134 if( mEndAction != Dali::Animation::Discard )
136 Bake( bufferIndex, mEndAction );
139 // The animation has now been played to completion
143 mElapsedSeconds = 0.0f;
146 return animationFinished;
149 void Animation::OnDestroy(BufferIndex bufferIndex)
151 if (mState == Playing || mState == Paused)
153 if (mDestroyAction != Dali::Animation::Discard)
155 Bake( bufferIndex, mDestroyAction );
162 void Animation::AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner )
164 animator->Attach( propertyOwner );
166 mAnimators.PushBack( animator );
169 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
171 if (mState == Stopped || mState == Destroyed)
173 // Short circuit when animation isn't running
177 // The animation must still be applied when Paused/Stopping
178 if (mState == Playing)
180 mElapsedSeconds += elapsedSeconds * mSpeedFactor;
185 if (mElapsedSeconds > mDurationSeconds )
187 mElapsedSeconds = fmod(mElapsedSeconds, mDurationSeconds);
189 else if( mElapsedSeconds < 0.0f )
191 mElapsedSeconds = mDurationSeconds - fmod(mElapsedSeconds, mDurationSeconds);
196 const bool animationFinished(mState == Playing &&
197 (( mSpeedFactor > 0.0f && mElapsedSeconds > mDurationSeconds ) ||
198 ( mSpeedFactor < 0.0f && mElapsedSeconds < 0.0f ))
201 UpdateAnimators(bufferIndex, animationFinished && (mEndAction != Dali::Animation::Discard));
203 if (animationFinished)
205 // The animation has now been played to completion
208 mElapsedSeconds = 0.0f;
212 return animationFinished;
215 void Animation::UpdateAnimators(BufferIndex bufferIndex, bool bake)
217 for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
219 // If an animator is not successfully applied, then it has been orphaned
222 AnimatorBase *animator = *iter;
223 const float initialDelay(animator->GetInitialDelay());
225 if (mElapsedSeconds >= initialDelay || mSpeedFactor < 0.0f )
227 // Calculate a progress specific to each individual animator
228 float progress(1.0f);
229 const float animatorDuration = animator->GetDuration();
230 if (animatorDuration > 0.0f) // animators can be "immediate"
232 progress = Clamp((mElapsedSeconds - initialDelay) / animatorDuration, 0.0f , 1.0f );
235 applied = animator->Update(bufferIndex, progress, bake);
238 // Animators are automatically removed, when orphaned from animatable scene objects.
241 iter = mAnimators.Erase(iter);
247 INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
252 } // namespace SceneGraph
254 } // namespace Internal