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, const Vector2& playRange, bool isLooping, Dali::Animation::EndAction endAction, Dali::Animation::EndAction disconnectAction )
44 : mDurationSeconds(durationSeconds),
45 mSpeedFactor( speedFactor ),
47 mEndAction(endAction),
48 mDisconnectAction(disconnectAction),
50 mElapsedSeconds(playRange.x*mDurationSeconds),
52 mPlayRange( playRange )
56 Animation::~Animation()
60 void Animation::SetDuration(float durationSeconds)
62 DALI_ASSERT_DEBUG(durationSeconds > 0.0f);
64 mDurationSeconds = durationSeconds;
67 void Animation::SetLooping(bool looping)
72 void Animation::SetEndAction(Dali::Animation::EndAction action)
77 void Animation::SetDisconnectAction(Dali::Animation::EndAction action)
79 if ( mDisconnectAction != action )
81 mDisconnectAction = action;
83 for ( AnimatorIter iter = mAnimators.Begin(), endIter = mAnimators.End(); iter != endIter; ++iter )
85 (*iter)->SetDisconnectAction( action );
90 void Animation::SetPlayRange( const Vector2& range )
94 //Make sure mElapsedSeconds is within the new range
95 mElapsedSeconds = Dali::Clamp(mElapsedSeconds, mPlayRange.x*mDurationSeconds , mPlayRange.y*mDurationSeconds );
98 void Animation::Play()
102 if ( mSpeedFactor < 0.0f && mElapsedSeconds <= mPlayRange.x*mDurationSeconds )
104 mElapsedSeconds = mPlayRange.y * mDurationSeconds;
108 void Animation::PlayFrom( float progress )
110 //If the animation is already playing this has no effect
111 if( mState != Playing )
113 mElapsedSeconds = progress * mDurationSeconds;
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*/);
143 bool Animation::Stop(BufferIndex bufferIndex)
145 bool animationFinished(false);
147 if (mState == Playing || mState == Paused)
149 animationFinished = true; // The actor-thread should be notified of this
151 if( mEndAction != Dali::Animation::Discard )
153 Bake( bufferIndex, mEndAction );
156 // The animation has now been played to completion
160 mElapsedSeconds = mPlayRange.x*mDurationSeconds;
163 return animationFinished;
166 void Animation::OnDestroy(BufferIndex bufferIndex)
168 if (mState == Playing || mState == Paused)
170 if (mEndAction != Dali::Animation::Discard)
172 Bake( bufferIndex, mEndAction );
179 void Animation::AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner )
181 animator->Attach( propertyOwner );
182 animator->SetDisconnectAction( mDisconnectAction );
184 mAnimators.PushBack( animator );
187 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
189 if (mState == Stopped || mState == Destroyed)
191 // Short circuit when animation isn't running
195 // The animation must still be applied when Paused/Stopping
196 if (mState == Playing)
198 mElapsedSeconds += elapsedSeconds * mSpeedFactor;
201 Vector2 playRangeSeconds = mPlayRange * mDurationSeconds;
204 if (mElapsedSeconds > playRangeSeconds.y )
206 mElapsedSeconds = playRangeSeconds.x + fmod(mElapsedSeconds, playRangeSeconds.y);
208 else if( mElapsedSeconds < playRangeSeconds.x )
210 mElapsedSeconds = playRangeSeconds.y - fmod(mElapsedSeconds, playRangeSeconds.y);
214 const bool animationFinished(mState == Playing &&
215 (( mSpeedFactor > 0.0f && mElapsedSeconds > playRangeSeconds.y ) ||
216 ( mSpeedFactor < 0.0f && mElapsedSeconds < playRangeSeconds.x ))
219 UpdateAnimators(bufferIndex, animationFinished && (mEndAction != Dali::Animation::Discard));
221 if (animationFinished)
223 // The animation has now been played to completion
226 mElapsedSeconds = playRangeSeconds.x;
230 return animationFinished;
233 void Animation::UpdateAnimators(BufferIndex bufferIndex, bool bake)
235 float elapsedSecondsClamped = Clamp( mElapsedSeconds, mPlayRange.x * mDurationSeconds,mPlayRange.y * mDurationSeconds );
236 for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
238 // If an animator is not successfully applied, then it has been orphaned
241 AnimatorBase *animator = *iter;
242 const float initialDelay(animator->GetInitialDelay());
244 if (elapsedSecondsClamped >= initialDelay || mSpeedFactor < 0.0f )
246 // Calculate a progress specific to each individual animator
247 float progress(1.0f);
248 const float animatorDuration = animator->GetDuration();
249 if (animatorDuration > 0.0f) // animators can be "immediate"
251 progress = Clamp((elapsedSecondsClamped - initialDelay) / animatorDuration, 0.0f , 1.0f );
254 applied = animator->Update(bufferIndex, progress, bake);
257 // Animators are automatically removed, when orphaned from animatable scene objects.
260 iter = mAnimators.Erase(iter);
266 INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
271 } // namespace SceneGraph
273 } // namespace Internal