2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/update/animation/scene-graph-animation.h>
21 #include <cmath> // fmod
24 #include <dali/internal/render/common/performance-monitor.h>
37 float DefaultAlphaFunc(float progress)
39 return progress; // linear
42 Animation::Animation(float durationSeconds, bool isLooping, Dali::Animation::EndAction endAction, Dali::Animation::EndAction destroyAction)
43 : mDurationSeconds(durationSeconds),
45 mEndAction(endAction),
46 mDestroyAction(destroyAction),
48 mElapsedSeconds(0.0f),
53 Animation::~Animation()
57 void Animation::SetDuration(float durationSeconds)
59 DALI_ASSERT_DEBUG(durationSeconds > 0.0f);
61 mDurationSeconds = durationSeconds;
64 void Animation::SetLooping(bool looping)
69 void Animation::SetEndAction(Dali::Animation::EndAction action)
74 void Animation::SetDestroyAction(Dali::Animation::EndAction action)
76 mDestroyAction = action;
79 void Animation::Play()
84 void Animation::Pause()
86 if (mState == Playing)
92 bool Animation::Stop(BufferIndex bufferIndex)
94 bool animationFinished(false);
96 if (mState == Playing || mState == Paused)
98 animationFinished = true; // The actor-thread should be notified of this
100 if (mEndAction == Dali::Animation::Bake)
102 UpdateAnimators(bufferIndex, true/*bake the final result*/);
105 // The animation has now been played to completion
109 mElapsedSeconds = 0.0f;
112 return animationFinished;
115 void Animation::OnDestroy(BufferIndex bufferIndex)
117 if (mState == Playing || mState == Paused)
119 if (mDestroyAction == Dali::Animation::Bake)
121 UpdateAnimators(bufferIndex, true/*bake the final result*/);
128 void Animation::AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner )
130 animator->Attach( propertyOwner );
132 mAnimators.PushBack( animator );
135 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
137 if (mState == Stopped || mState == Destroyed)
139 // Short circuit when animation isn't running
143 // The animation must still be applied when Paused/Stopping
144 if (mState == Playing)
146 mElapsedSeconds += elapsedSeconds;
151 if (mElapsedSeconds > mDurationSeconds)
153 mElapsedSeconds = fmod(mElapsedSeconds, mDurationSeconds);
157 const bool animationFinished(mState == Playing && mElapsedSeconds > mDurationSeconds);
159 UpdateAnimators(bufferIndex, animationFinished && (mEndAction == Dali::Animation::Bake));
161 if (animationFinished)
163 // The animation has now been played to completion
166 mElapsedSeconds = 0.0f;
170 return animationFinished;
173 void Animation::UpdateAnimators(BufferIndex bufferIndex, bool bake)
175 for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
177 // If an animator is not successfully applied, then it has been orphaned
180 AnimatorBase *animator = *iter;
181 const float initialDelay(animator->GetInitialDelay());
183 if (mElapsedSeconds >= initialDelay)
185 // Calculate a progress specific to each individual animator
186 float progress(1.0f);
187 const float animatorDuration = animator->GetDuration();
188 if (animatorDuration > 0.0f) // animators can be "immediate"
190 progress = min(1.0f, (mElapsedSeconds - initialDelay) / animatorDuration);
193 applied = animator->Update(bufferIndex, progress, bake);
196 // Animators are automatically removed, when orphaned from animatable scene objects.
199 iter = mAnimators.Erase(iter);
205 INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
210 } // namespace SceneGraph
212 } // namespace Internal