Merge "Guard against invalid PanGesture timestamps" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-animation.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/animation/animation.h>
23 #include <dali/internal/update/animation/scene-graph-animator.h>
24 #include <dali/internal/common/buffer-index.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/common/event-to-update.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace SceneGraph
35 {
36
37 class Animation;
38
39 typedef OwnerContainer< Animation* > AnimationContainer;
40
41 typedef AnimationContainer::Iterator AnimationIter;
42 typedef AnimationContainer::ConstIterator AnimationConstIter;
43
44 /**
45  * Animations are used to change the properties of scene graph objects, as part of a scene
46  * managers "update" phase. An animation is a container of Animator objects; the actual setting
47  * of object values is done by the animators.
48  */
49 class Animation
50 {
51 public:
52
53   typedef Dali::Animation::EndAction EndAction;
54
55   enum State
56   {
57     Stopped,
58     Playing,
59     Paused,
60     Destroyed
61   };
62
63   /**
64    * Construct a new Animation.
65    * @param[in] durationSeconds The duration of the animation in seconds.
66    * @param[in] speedFactor Multiplier to the animation velocity.
67    * @param[in] isLooping Whether the animation will loop.
68    * @param[in] endAction The action to perform when the animation ends.
69    * @param[in] destroyAction The action to perform when the animation is destroyed.
70    * @return A new Animation
71    */
72   static Animation* New( float durationSeconds, float speedFactor, bool isLooping, EndAction endAction, EndAction destroyAction )
73   {
74     return new Animation( durationSeconds, speedFactor, isLooping, endAction, destroyAction );
75   }
76
77   /**
78    * Virtual destructor
79    */
80   virtual ~Animation();
81
82   /**
83    * Set the duration of an animation.
84    * @pre durationSeconds must be greater than zero.
85    * @param[in] durationSeconds The duration in seconds.
86    */
87   void SetDuration(float durationSeconds);
88
89   /**
90    * Retrieve the duration of the animation.
91    * @return The duration in seconds.
92    */
93   float GetDuration() const
94   {
95     return mDurationSeconds;
96   }
97
98   /*
99    * Retrieve the current progress of the animation.
100    * @return The current progress as a normalized value between [0,1].
101    */
102   float GetCurrentProgress() const
103   {
104     if( mDurationSeconds > 0.0f )
105     {
106       return mElapsedSeconds / mDurationSeconds;
107     }
108
109     return 0.0f;
110   }
111
112   /*
113    * Sets the progress of the animation.
114    * @param[in] The new progress as a normalized value between [0,1]
115    */
116   void SetCurrentProgress( float progress )
117   {
118     mElapsedSeconds = mDurationSeconds * progress;
119   }
120
121   void SetSpeedFactor( float factor )
122   {
123     mSpeedFactor = factor;
124   }
125
126   /**
127    * Set whether the animation will loop.
128    * @param[in] looping True if the animation will loop.
129    */
130   void SetLooping(bool looping);
131
132   /**
133    * Query whether the animation will loop.
134    * @return True if the animation will loop.
135    */
136   bool IsLooping() const
137   {
138     return mLooping;
139   }
140
141   /**
142    * Set the end action of the animation.
143    * @param[in] action The end action.
144    */
145   void SetEndAction(EndAction action);
146
147   /**
148    * Retrieve the action performed when the animation ends.
149    * @return The end action.
150    */
151   EndAction GetEndAction()
152   {
153     return mEndAction;
154   }
155
156   /**
157    * Set the destroy action of the animation.
158    * This action is performed during the next update when
159    * the animation is destroyed.
160    * @param[in] action The destroy action.
161    */
162   void SetDestroyAction(EndAction action);
163
164   /**
165    * Retrieve the action performed when the animation is destroyed.
166    * @return The destroy action.
167    */
168   EndAction GetDestroyAction()
169   {
170     return mDestroyAction;
171   }
172
173   /**
174    * Play the animation.
175    */
176   void Play();
177
178   /*
179    * Play the animation from a given point
180    * @param[in] progress A value between [0,1] form where the animation should start playing
181    */
182   void PlayFrom( float progress );
183
184   /**
185    * Pause the animation.
186    */
187   void Pause();
188
189   /**
190    * Stop the animation.
191    * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
192    * @return True if the animation has finished (otherwise it wasn't playing)
193    */
194   bool Stop(BufferIndex bufferIndex);
195
196   /**
197    * Called shortly before the animation is destroyed.
198    * @param[in] bufferIndex The buffer to update when mDestroyAction == Bake.
199    */
200   void OnDestroy(BufferIndex bufferIndex);
201
202   /**
203    * Query whether the animation is playing, paused or stopped.
204    * Note that even when paused, the Update() method should be called,
205    * since the current progress must be reapplied each frame.
206    */
207   State GetState() const
208   {
209     return mState;
210   }
211
212   /**
213    * Retrive a count of the number of times the animation has been played to completion.
214    * This can be used to emit "Finised" signals from the public-api
215    */
216   int GetPlayCount() const
217   {
218     return mPlayCount;
219   }
220
221   /**
222    * Add a newly created animator.
223    * Animators are automatically removed, when orphaned from an animatable scene object.
224    * @param[in] animator The animator to add.
225    * @param[in] propertyOwner The scene-object that owns the animatable property.
226    * @post The animator is owned by this animation.
227    */
228   void AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner );
229
230   /**
231    * Retrieve the animators from an animation.
232    * @return The container of animators.
233    */
234   AnimatorContainer& GetAnimators()
235   {
236     return mAnimators;
237   }
238
239   /**
240    * This causes the animators to change the properties of objects in the scene graph.
241    * @pre The animation is playing or paused.
242    * @param[in] bufferIndex The buffer to update.
243    * @param[in] elapsedSeconds The time elapsed since the previous frame.
244    * @return True if the animation has finished.
245    */
246   bool Update(BufferIndex bufferIndex, float elapsedSeconds);
247
248
249 protected:
250
251   /**
252    * Protected constructor. See New()
253    */
254   Animation( float durationSeconds, float speedFactor, bool isLooping, EndAction endAction, EndAction destroyAction );
255
256
257 private:
258
259   /**
260    * Helper for Update, also used to bake when the animation is stopped or destroyed.
261    * @param[in] bufferIndex The buffer to update.
262    * @param[in] bake True if the final result should be baked.
263    */
264   void UpdateAnimators(BufferIndex bufferIndex, bool bake);
265
266   // Undefined
267   Animation(const Animation&);
268
269   // Undefined
270   Animation& operator=(const Animation& rhs);
271
272 protected:
273
274   float mDurationSeconds;
275   float mSpeedFactor;
276   bool mLooping;
277   EndAction mEndAction;
278   EndAction mDestroyAction;
279
280   State mState;
281   float mElapsedSeconds;
282   int mPlayCount;
283
284   AnimatorContainer mAnimators;
285 };
286
287 }; //namespace SceneGraph
288
289 // value types used by messages
290 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
291
292 namespace SceneGraph
293 {
294
295 // Messages for Animation
296
297 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
298 {
299   typedef MessageValue1< Animation, float > LocalType;
300
301   // Reserve some memory inside the message queue
302   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
303
304   // Construct message in the message queue memory; note that delete should not be called on the return value
305   new (slot) LocalType( &animation, &Animation::SetDuration, durationSeconds );
306 }
307
308 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
309 {
310   typedef MessageValue1< Animation, bool > LocalType;
311
312   // Reserve some memory inside the message queue
313   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
314
315   // Construct message in the message queue memory; note that delete should not be called on the return value
316   new (slot) LocalType( &animation, &Animation::SetLooping, looping );
317 }
318
319 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
320 {
321   typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
322
323   // Reserve some memory inside the message queue
324   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
325
326   // Construct message in the message queue memory; note that delete should not be called on the return value
327   new (slot) LocalType( &animation, &Animation::SetEndAction, action );
328 }
329
330 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
331 {
332   typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
333
334   // Reserve some memory inside the message queue
335   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
336
337   // Construct message in the message queue memory; note that delete should not be called on the return value
338   new (slot) LocalType( &animation, &Animation::SetDestroyAction, action );
339 }
340
341 inline void SetCurrentProgressMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
342 {
343   typedef MessageValue1< Animation, float > LocalType;
344
345   // Reserve some memory inside the message queue
346   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
347
348   // Construct message in the message queue memory; note that delete should not be called on the return value
349   new (slot) LocalType( &animation, &Animation::SetCurrentProgress, progress );
350 }
351
352 inline void SetSpeedFactorMessage( EventToUpdate& eventToUpdate, const Animation& animation, float factor )
353 {
354   typedef MessageValue1< Animation, float > LocalType;
355
356   // Reserve some memory inside the message queue
357   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
358
359   // Construct message in the message queue memory; note that delete should not be called on the return value
360   new (slot) LocalType( &animation, &Animation::SetSpeedFactor, factor );
361 }
362
363 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
364 {
365   typedef Message< Animation > LocalType;
366
367   // Reserve some memory inside the message queue
368   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
369
370   // Construct message in the message queue memory; note that delete should not be called on the return value
371   new (slot) LocalType( &animation, &Animation::Play );
372 }
373
374 inline void PlayAnimationFromMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
375 {
376   typedef MessageValue1< Animation,float > LocalType;
377
378   // Reserve some memory inside the message queue
379   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
380
381   // Construct message in the message queue memory; note that delete should not be called on the return value
382   new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
383 }
384
385 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
386 {
387   typedef Message< Animation > LocalType;
388
389   // Reserve some memory inside the message queue
390   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
391
392   // Construct message in the message queue memory; note that delete should not be called on the return value
393   new (slot) LocalType( &animation, &Animation::Pause );
394 }
395
396 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
397 {
398   typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
399
400   // Reserve some memory inside the message queue
401   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
402
403   // Construct message in the message queue memory; note that delete should not be called on the return value
404   new (slot) LocalType( &animation, &Animation::AddAnimator, &animator, const_cast<PropertyOwner*>( &owner ) );
405 }
406
407 } // namespace SceneGraph
408
409 } // namespace Internal
410
411 } // namespace Dali
412
413 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__