Merge "remove refobject based objects and dynamic casting from discard queue" into...
[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   /**
267    * Helper function to bake the result of the animation when it is stopped or
268    * destroyed.
269    * @param[in] bufferIndex The buffer to update.
270    * @param[in] action The end action specified.
271    */
272   void Bake(BufferIndex bufferIndex, EndAction action );
273
274   // Undefined
275   Animation(const Animation&);
276
277   // Undefined
278   Animation& operator=(const Animation& rhs);
279
280 protected:
281
282   float mDurationSeconds;
283   float mSpeedFactor;
284   bool mLooping;
285   EndAction mEndAction;
286   EndAction mDestroyAction;
287
288   State mState;
289   float mElapsedSeconds;
290   int mPlayCount;
291
292   AnimatorContainer mAnimators;
293 };
294
295 }; //namespace SceneGraph
296
297 // value types used by messages
298 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
299
300 namespace SceneGraph
301 {
302
303 // Messages for Animation
304
305 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
306 {
307   typedef MessageValue1< Animation, float > LocalType;
308
309   // Reserve some memory inside the message queue
310   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
311
312   // Construct message in the message queue memory; note that delete should not be called on the return value
313   new (slot) LocalType( &animation, &Animation::SetDuration, durationSeconds );
314 }
315
316 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
317 {
318   typedef MessageValue1< Animation, bool > LocalType;
319
320   // Reserve some memory inside the message queue
321   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
322
323   // Construct message in the message queue memory; note that delete should not be called on the return value
324   new (slot) LocalType( &animation, &Animation::SetLooping, looping );
325 }
326
327 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
328 {
329   typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
330
331   // Reserve some memory inside the message queue
332   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
333
334   // Construct message in the message queue memory; note that delete should not be called on the return value
335   new (slot) LocalType( &animation, &Animation::SetEndAction, action );
336 }
337
338 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
339 {
340   typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
341
342   // Reserve some memory inside the message queue
343   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
344
345   // Construct message in the message queue memory; note that delete should not be called on the return value
346   new (slot) LocalType( &animation, &Animation::SetDestroyAction, action );
347 }
348
349 inline void SetCurrentProgressMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
350 {
351   typedef MessageValue1< Animation, float > LocalType;
352
353   // Reserve some memory inside the message queue
354   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
355
356   // Construct message in the message queue memory; note that delete should not be called on the return value
357   new (slot) LocalType( &animation, &Animation::SetCurrentProgress, progress );
358 }
359
360 inline void SetSpeedFactorMessage( EventToUpdate& eventToUpdate, const Animation& animation, float factor )
361 {
362   typedef MessageValue1< Animation, float > LocalType;
363
364   // Reserve some memory inside the message queue
365   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
366
367   // Construct message in the message queue memory; note that delete should not be called on the return value
368   new (slot) LocalType( &animation, &Animation::SetSpeedFactor, factor );
369 }
370
371 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
372 {
373   typedef Message< Animation > LocalType;
374
375   // Reserve some memory inside the message queue
376   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
377
378   // Construct message in the message queue memory; note that delete should not be called on the return value
379   new (slot) LocalType( &animation, &Animation::Play );
380 }
381
382 inline void PlayAnimationFromMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
383 {
384   typedef MessageValue1< Animation,float > LocalType;
385
386   // Reserve some memory inside the message queue
387   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
388
389   // Construct message in the message queue memory; note that delete should not be called on the return value
390   new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
391 }
392
393 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
394 {
395   typedef Message< Animation > LocalType;
396
397   // Reserve some memory inside the message queue
398   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
399
400   // Construct message in the message queue memory; note that delete should not be called on the return value
401   new (slot) LocalType( &animation, &Animation::Pause );
402 }
403
404 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
405 {
406   typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
407
408   // Reserve some memory inside the message queue
409   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
410
411   // Construct message in the message queue memory; note that delete should not be called on the return value
412   new (slot) LocalType( &animation, &Animation::AddAnimator, &animator, const_cast<PropertyOwner*>( &owner ) );
413 }
414
415 } // namespace SceneGraph
416
417 } // namespace Internal
418
419 } // namespace Dali
420
421 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__