Merge "Added api function to specify speed factor of an animation" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-animation.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/animation/scene-graph-animation.h>
20
21 // EXTERNAL INCLUDES
22 #include <cmath> // fmod
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/render/common/performance-monitor.h>
26
27 using namespace std;
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace SceneGraph
36 {
37
38 float DefaultAlphaFunc(float progress)
39 {
40   return progress; // linear
41 }
42
43 Animation::Animation(float durationSeconds, float speedFactor, bool isLooping, Dali::Animation::EndAction endAction, Dali::Animation::EndAction destroyAction)
44 : mDurationSeconds(durationSeconds),
45   mSpeedFactor( speedFactor ),
46   mLooping(isLooping),
47   mEndAction(endAction),
48   mDestroyAction(destroyAction),
49   mState(Stopped),
50   mElapsedSeconds(0.0f),
51   mPlayCount(0)
52 {
53 }
54
55 Animation::~Animation()
56 {
57 }
58
59 void Animation::SetDuration(float durationSeconds)
60 {
61   DALI_ASSERT_DEBUG(durationSeconds > 0.0f);
62
63   mDurationSeconds = durationSeconds;
64 }
65
66 void Animation::SetLooping(bool looping)
67 {
68   mLooping = looping;
69 }
70
71 void Animation::SetEndAction(Dali::Animation::EndAction action)
72 {
73   mEndAction = action;
74 }
75
76 void Animation::SetDestroyAction(Dali::Animation::EndAction action)
77 {
78   mDestroyAction = action;
79 }
80
81 void Animation::Play()
82 {
83   mState = Playing;
84
85   if ( mSpeedFactor < 0.0f && mElapsedSeconds <= 0.0f )
86   {
87     mElapsedSeconds = mDurationSeconds;
88   }
89 }
90
91 void Animation::PlayFrom( float progress )
92 {
93   //If the animation is already playing this has no effect
94   if( mState != Playing )
95   {
96     mElapsedSeconds = progress * mDurationSeconds;
97     mState = Playing;
98   }
99 }
100
101 void Animation::Pause()
102 {
103   if (mState == Playing)
104   {
105     mState = Paused;
106   }
107 }
108
109 bool Animation::Stop(BufferIndex bufferIndex)
110 {
111   bool animationFinished(false);
112
113   if (mState == Playing || mState == Paused)
114   {
115     animationFinished = true; // The actor-thread should be notified of this
116
117     if( mEndAction != Dali::Animation::Discard )
118     {
119       if( mEndAction == Dali::Animation::BakeFinal )
120       {
121         if( mSpeedFactor > 0.0f )
122         {
123           mElapsedSeconds = mDurationSeconds + Math::MACHINE_EPSILON_1; // Force animation to reach it's end
124         }
125         else
126         {
127           mElapsedSeconds = 0.0f - Math::MACHINE_EPSILON_1; //Force animation to reach it's beginning
128         }
129
130       }
131       UpdateAnimators(bufferIndex, true/*bake the final result*/);
132     }
133
134     // The animation has now been played to completion
135     ++mPlayCount;
136   }
137
138   mElapsedSeconds = 0.0f;
139   mState = Stopped;
140
141   return animationFinished;
142 }
143
144 void Animation::OnDestroy(BufferIndex bufferIndex)
145 {
146   if (mState == Playing || mState == Paused)
147   {
148     if (mDestroyAction != Dali::Animation::Discard)
149     {
150       UpdateAnimators(bufferIndex, true/*bake the final result*/);
151     }
152   }
153
154   mState = Destroyed;
155 }
156
157 void Animation::AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner )
158 {
159   animator->Attach( propertyOwner );
160
161   mAnimators.PushBack( animator );
162 }
163
164 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
165 {
166   if (mState == Stopped || mState == Destroyed)
167   {
168     // Short circuit when animation isn't running
169     return false;
170   }
171
172   // The animation must still be applied when Paused/Stopping
173   if (mState == Playing)
174   {
175     mElapsedSeconds += elapsedSeconds * mSpeedFactor;
176   }
177
178   if (mLooping)
179   {
180     if (mElapsedSeconds > mDurationSeconds )
181     {
182       mElapsedSeconds = fmod(mElapsedSeconds, mDurationSeconds);
183     }
184     else if( mElapsedSeconds < 0.0f )
185     {
186       mElapsedSeconds = mDurationSeconds - fmod(mElapsedSeconds, mDurationSeconds);
187     }
188   }
189
190
191   const bool animationFinished(mState == Playing                                              &&
192                               (( mSpeedFactor > 0.0f && mElapsedSeconds > mDurationSeconds )  ||
193                                ( mSpeedFactor < 0.0f && mElapsedSeconds < 0.0f ))
194                               );
195
196   UpdateAnimators(bufferIndex, animationFinished && (mEndAction != Dali::Animation::Discard));
197
198   if (animationFinished)
199   {
200     // The animation has now been played to completion
201     ++mPlayCount;
202
203     mElapsedSeconds = 0.0f;
204     mState = Stopped;
205   }
206
207   return animationFinished;
208 }
209
210 void Animation::UpdateAnimators(BufferIndex bufferIndex, bool bake)
211 {
212   for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
213   {
214     // If an animator is not successfully applied, then it has been orphaned
215     bool applied(true);
216
217     AnimatorBase *animator = *iter;
218     const float initialDelay(animator->GetInitialDelay());
219
220     if (mElapsedSeconds >= initialDelay || mSpeedFactor < 0.0f )
221     {
222       // Calculate a progress specific to each individual animator
223       float progress(1.0f);
224       const float animatorDuration = animator->GetDuration();
225       if (animatorDuration > 0.0f) // animators can be "immediate"
226       {
227         progress = Clamp((mElapsedSeconds - initialDelay) / animatorDuration, 0.0f , 1.0f );
228       }
229
230       applied = animator->Update(bufferIndex, progress, bake);
231     }
232
233     // Animators are automatically removed, when orphaned from animatable scene objects.
234     if (!applied)
235     {
236       iter = mAnimators.Erase(iter);
237     }
238     else
239     {
240       ++iter;
241
242       INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
243     }
244   }
245 }
246
247 } // namespace SceneGraph
248
249 } // namespace Internal
250
251 } // namespace Dali