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, bool isLooping, Dali::Animation::EndAction endAction, Dali::Animation::EndAction destroyAction)
44 : mDurationSeconds(durationSeconds),
46 mEndAction(endAction),
47 mDestroyAction(destroyAction),
49 mElapsedSeconds(0.0f),
54 Animation::~Animation()
58 void Animation::SetDuration(float durationSeconds)
60 DALI_ASSERT_DEBUG(durationSeconds > 0.0f);
62 mDurationSeconds = durationSeconds;
65 void Animation::SetLooping(bool looping)
70 void Animation::SetEndAction(Dali::Animation::EndAction action)
75 void Animation::SetDestroyAction(Dali::Animation::EndAction action)
77 mDestroyAction = action;
80 void Animation::Play()
85 void Animation::Pause()
87 if (mState == Playing)
93 bool Animation::Stop(BufferIndex bufferIndex)
95 bool animationFinished(false);
97 if (mState == Playing || mState == Paused)
99 animationFinished = true; // The actor-thread should be notified of this
101 if( mEndAction != Dali::Animation::Discard )
103 if( mEndAction == Dali::Animation::BakeFinal )
105 mElapsedSeconds = mDurationSeconds + Math::MACHINE_EPSILON_1; // Force animation to reach it's end
107 UpdateAnimators(bufferIndex, true/*bake the final result*/);
110 // The animation has now been played to completion
114 mElapsedSeconds = 0.0f;
117 return animationFinished;
120 void Animation::OnDestroy(BufferIndex bufferIndex)
122 if (mState == Playing || mState == Paused)
124 if (mDestroyAction != Dali::Animation::Discard)
126 UpdateAnimators(bufferIndex, true/*bake the final result*/);
133 void Animation::AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner )
135 animator->Attach( propertyOwner );
137 mAnimators.PushBack( animator );
140 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
142 if (mState == Stopped || mState == Destroyed)
144 // Short circuit when animation isn't running
148 // The animation must still be applied when Paused/Stopping
149 if (mState == Playing)
151 mElapsedSeconds += elapsedSeconds;
156 if (mElapsedSeconds > mDurationSeconds)
158 mElapsedSeconds = fmod(mElapsedSeconds, mDurationSeconds);
162 const bool animationFinished(mState == Playing && mElapsedSeconds > mDurationSeconds);
164 UpdateAnimators(bufferIndex, animationFinished && (mEndAction != Dali::Animation::Discard));
166 if (animationFinished)
168 // The animation has now been played to completion
171 mElapsedSeconds = 0.0f;
175 return animationFinished;
178 void Animation::UpdateAnimators(BufferIndex bufferIndex, bool bake)
180 for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
182 // If an animator is not successfully applied, then it has been orphaned
185 AnimatorBase *animator = *iter;
186 const float initialDelay(animator->GetInitialDelay());
188 if (mElapsedSeconds >= initialDelay)
190 // Calculate a progress specific to each individual animator
191 float progress(1.0f);
192 const float animatorDuration = animator->GetDuration();
193 if (animatorDuration > 0.0f) // animators can be "immediate"
195 progress = min(1.0f, (mElapsedSeconds - initialDelay) / animatorDuration);
198 applied = animator->Update(bufferIndex, progress, bake);
201 // Animators are automatically removed, when orphaned from animatable scene objects.
204 iter = mAnimators.Erase(iter);
210 INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
215 } // namespace SceneGraph
217 } // namespace Internal