Merge "(Properties) Specify which properties can be registered as animatable" into...
[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, const Vector2& playRange, 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(playRange.x*mDurationSeconds),
51   mPlayCount(0),
52   mPlayRange( playRange )
53 {
54 }
55
56 Animation::~Animation()
57 {
58 }
59
60 void Animation::SetDuration(float durationSeconds)
61 {
62   DALI_ASSERT_DEBUG(durationSeconds > 0.0f);
63
64   mDurationSeconds = durationSeconds;
65 }
66
67 void Animation::SetLooping(bool looping)
68 {
69   mLooping = looping;
70 }
71
72 void Animation::SetEndAction(Dali::Animation::EndAction action)
73 {
74   mEndAction = action;
75 }
76
77 void Animation::SetDestroyAction(Dali::Animation::EndAction action)
78 {
79   mDestroyAction = action;
80 }
81
82 void Animation::SetPlayRange( const Vector2& range )
83 {
84   mPlayRange = range;
85
86   //Make sure mElapsedSeconds is within the new range
87   mElapsedSeconds = Dali::Clamp(mElapsedSeconds, mPlayRange.x*mDurationSeconds , mPlayRange.y*mDurationSeconds );
88 }
89
90 void Animation::Play()
91 {
92   mState = Playing;
93
94   if ( mSpeedFactor < 0.0f && mElapsedSeconds <= mPlayRange.x*mDurationSeconds )
95   {
96     mElapsedSeconds = mPlayRange.y * mDurationSeconds;
97   }
98 }
99
100 void Animation::PlayFrom( float progress )
101 {
102   //If the animation is already playing this has no effect
103   if( mState != Playing )
104   {
105     mElapsedSeconds = progress * mDurationSeconds;
106     mState = Playing;
107   }
108 }
109
110 void Animation::Pause()
111 {
112   if (mState == Playing)
113   {
114     mState = Paused;
115   }
116 }
117
118 void Animation::Bake(BufferIndex bufferIndex, EndAction action)
119 {
120   if( action == Dali::Animation::BakeFinal )
121   {
122     if( mSpeedFactor > 0.0f )
123     {
124       mElapsedSeconds = mPlayRange.y*mDurationSeconds + Math::MACHINE_EPSILON_1; // Force animation to reach it's end
125     }
126     else
127     {
128       mElapsedSeconds = mPlayRange.x*mDurationSeconds - Math::MACHINE_EPSILON_1; //Force animation to reach it's beginning
129     }
130   }
131
132   UpdateAnimators(bufferIndex, true/*bake the final result*/);
133 }
134
135 bool Animation::Stop(BufferIndex bufferIndex)
136 {
137   bool animationFinished(false);
138
139   if (mState == Playing || mState == Paused)
140   {
141     animationFinished = true; // The actor-thread should be notified of this
142
143     if( mEndAction != Dali::Animation::Discard )
144     {
145       Bake( bufferIndex, mEndAction );
146     }
147
148     // The animation has now been played to completion
149     ++mPlayCount;
150   }
151
152   mElapsedSeconds = mPlayRange.x*mDurationSeconds;
153   mState = Stopped;
154
155   return animationFinished;
156 }
157
158 void Animation::OnDestroy(BufferIndex bufferIndex)
159 {
160   if (mState == Playing || mState == Paused)
161   {
162     if (mDestroyAction != Dali::Animation::Discard)
163     {
164       Bake( bufferIndex, mDestroyAction );
165     }
166   }
167
168   mState = Destroyed;
169 }
170
171 void Animation::AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner )
172 {
173   animator->Attach( propertyOwner );
174
175   mAnimators.PushBack( animator );
176 }
177
178 bool Animation::Update(BufferIndex bufferIndex, float elapsedSeconds)
179 {
180   if (mState == Stopped || mState == Destroyed)
181   {
182     // Short circuit when animation isn't running
183     return false;
184   }
185
186   // The animation must still be applied when Paused/Stopping
187   if (mState == Playing)
188   {
189     mElapsedSeconds += elapsedSeconds * mSpeedFactor;
190   }
191
192   Vector2 playRangeSeconds = mPlayRange * mDurationSeconds;
193   if (mLooping)
194   {
195     if (mElapsedSeconds > playRangeSeconds.y )
196     {
197       mElapsedSeconds = playRangeSeconds.x + fmod(mElapsedSeconds, playRangeSeconds.y);
198     }
199     else if( mElapsedSeconds < playRangeSeconds.x )
200     {
201       mElapsedSeconds = playRangeSeconds.y - fmod(mElapsedSeconds, playRangeSeconds.y);
202     }
203   }
204
205   const bool animationFinished(mState == Playing                                                &&
206                               (( mSpeedFactor > 0.0f && mElapsedSeconds > playRangeSeconds.y )  ||
207                                ( mSpeedFactor < 0.0f && mElapsedSeconds < playRangeSeconds.x ))
208                               );
209
210   UpdateAnimators(bufferIndex, animationFinished && (mEndAction != Dali::Animation::Discard));
211
212   if (animationFinished)
213   {
214     // The animation has now been played to completion
215     ++mPlayCount;
216
217     mElapsedSeconds = playRangeSeconds.x;
218     mState = Stopped;
219   }
220
221   return animationFinished;
222 }
223
224 void Animation::UpdateAnimators(BufferIndex bufferIndex, bool bake)
225 {
226   float elapsedSecondsClamped = Clamp( mElapsedSeconds, mPlayRange.x * mDurationSeconds,mPlayRange.y * mDurationSeconds );
227   for ( AnimatorIter iter = mAnimators.Begin(); iter != mAnimators.End(); )
228   {
229     // If an animator is not successfully applied, then it has been orphaned
230     bool applied(true);
231
232     AnimatorBase *animator = *iter;
233     const float initialDelay(animator->GetInitialDelay());
234
235     if (elapsedSecondsClamped >= initialDelay || mSpeedFactor < 0.0f )
236     {
237       // Calculate a progress specific to each individual animator
238       float progress(1.0f);
239       const float animatorDuration = animator->GetDuration();
240       if (animatorDuration > 0.0f) // animators can be "immediate"
241       {
242         progress = Clamp((elapsedSecondsClamped - initialDelay) / animatorDuration, 0.0f , 1.0f );
243       }
244
245       applied = animator->Update(bufferIndex, progress, bake);
246     }
247
248     // Animators are automatically removed, when orphaned from animatable scene objects.
249     if (!applied)
250     {
251       iter = mAnimators.Erase(iter);
252     }
253     else
254     {
255       ++iter;
256
257       INCREASE_COUNTER(PerformanceMonitor::ANIMATORS_APPLIED);
258     }
259   }
260 }
261
262 } // namespace SceneGraph
263
264 } // namespace Internal
265
266 } // namespace Dali