[SRUK] Initial copy from Tizen 2.2 version
[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 Flora License, Version 1.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://floralicense.org/license/
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 // INTERNAL INCLUDES
21 #include <dali/public-api/animation/animation.h>
22 #include <dali/internal/update/animation/scene-graph-animator.h>
23 #include <dali/internal/common/buffer-index.h>
24 #include <dali/internal/common/message.h>
25 #include <dali/internal/common/event-to-update.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace SceneGraph
34 {
35
36 class Animation;
37
38 typedef OwnerContainer< Animation* > AnimationContainer;
39
40 typedef AnimationContainer::Iterator AnimationIter;
41 typedef AnimationContainer::ConstIterator AnimationConstIter;
42
43 /**
44  * Animations are used to change the properties of scene graph objects, as part of a scene
45  * managers "update" phase. An animation is a container of Animator objects; the actual setting
46  * of object values is done by the animators.
47  */
48 class Animation
49 {
50 public:
51
52   typedef Dali::Animation::EndAction EndAction;
53
54   enum State
55   {
56     Stopped,
57     Playing,
58     Paused,
59     Destroyed
60   };
61
62   /**
63    * Construct a new Animation.
64    * @param[in] durationSeconds The duration of the animation in seconds.
65    * @param[in] isLooping Whether the animation will loop.
66    * @param[in] endAction The action to perform when the animation ends.
67    * @param[in] destroyAction The action to perform when the animation is destroyed.
68    * @return A new Animation
69    */
70   static Animation* New( float durationSeconds, bool isLooping, EndAction endAction, EndAction destroyAction )
71   {
72     return new Animation( durationSeconds, isLooping, endAction, destroyAction );
73   }
74
75   /**
76    * Virtual destructor
77    */
78   virtual ~Animation();
79
80   /**
81    * Set the duration of an animation.
82    * @pre durationSeconds must be greater than zero.
83    * @param[in] durationSeconds The duration in seconds.
84    */
85   void SetDuration(float durationSeconds);
86
87   /**
88    * Retrieve the duration of the animation.
89    * @return The duration in seconds.
90    */
91   float GetDuration() const
92   {
93     return mDurationSeconds;
94   }
95
96   /**
97    * Set whether the animation will loop.
98    * @param[in] looping True if the animation will loop.
99    */
100   void SetLooping(bool looping);
101
102   /**
103    * Query whether the animation will loop.
104    * @return True if the animation will loop.
105    */
106   bool IsLooping() const
107   {
108     return mLooping;
109   }
110
111   /**
112    * Set the end action of the animation.
113    * @param[in] action The end action.
114    */
115   void SetEndAction(EndAction action);
116
117   /**
118    * Retrieve the action performed when the animation ends.
119    * @return The end action.
120    */
121   EndAction GetEndAction()
122   {
123     return mEndAction;
124   }
125
126   /**
127    * Set the destroy action of the animation.
128    * This action is performed during the next update when
129    * the animation is destroyed.
130    * @param[in] action The destroy action.
131    */
132   void SetDestroyAction(EndAction action);
133
134   /**
135    * Retrieve the action performed when the animation is destroyed.
136    * @return The destroy action.
137    */
138   EndAction GetDestroyAction()
139   {
140     return mDestroyAction;
141   }
142
143   /**
144    * Play the animation.
145    */
146   void Play();
147
148   /**
149    * Pause the animation.
150    */
151   void Pause();
152
153   /**
154    * Stop the animation.
155    * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
156    * @return True if the animation has finished (otherwise it wasn't playing)
157    */
158   bool Stop(BufferIndex bufferIndex);
159
160   /**
161    * Called shortly before the animation is destroyed.
162    * @param[in] bufferIndex The buffer to update when mDestroyAction == Bake.
163    */
164   void OnDestroy(BufferIndex bufferIndex);
165
166   /**
167    * Query whether the animation is playing, paused or stopped.
168    * Note that even when paused, the Update() method should be called,
169    * since the current progress must be reapplied each frame.
170    */
171   State GetState() const
172   {
173     return mState;
174   }
175
176   /**
177    * Retrive a count of the number of times the animation has been played to completion.
178    * This can be used to emit "Finised" signals from the public-api
179    */
180   int GetPlayCount() const
181   {
182     return mPlayCount;
183   }
184
185   /**
186    * Add a newly created animator.
187    * Animators are automatically removed, when orphaned from an animatable scene object.
188    * @param[in] animator The animator to add.
189    * @param[in] propertyOwner The scene-object that owns the animatable property.
190    * @post The animator is owned by this animation.
191    */
192   void AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner );
193
194   /**
195    * Retrieve the animators from an animation.
196    * @return The container of animators.
197    */
198   AnimatorContainer& GetAnimators()
199   {
200     return mAnimators;
201   }
202
203   /**
204    * This causes the animators to change the properties of objects in the scene graph.
205    * @pre The animation is playing or paused.
206    * @param[in] bufferIndex The buffer to update.
207    * @param[in] elapsedSeconds The time elapsed since the previous frame.
208    * @return True if the animation has finished.
209    */
210   bool Update(BufferIndex bufferIndex, float elapsedSeconds);
211
212
213 protected:
214
215   /**
216    * Protected constructor. See New()
217    */
218   Animation( float durationSeconds, bool isLooping, EndAction endAction, EndAction destroyAction );
219
220
221 private:
222
223   /**
224    * Helper for Update, also used to bake when the animation is stopped or destroyed.
225    * @param[in] bufferIndex The buffer to update.
226    * @param[in] bake True if the final result should be baked.
227    */
228   void UpdateAnimators(BufferIndex bufferIndex, bool bake);
229
230   // Undefined
231   Animation(const Animation&);
232
233   // Undefined
234   Animation& operator=(const Animation& rhs);
235
236 protected:
237
238   float mDurationSeconds;
239   bool mLooping;
240   EndAction mEndAction;
241   EndAction mDestroyAction;
242
243   State mState;
244   float mElapsedSeconds;
245   int mPlayCount;
246
247   AnimatorContainer mAnimators;
248 };
249
250 }; //namespace SceneGraph
251
252 // value types used by messages
253 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
254
255 namespace SceneGraph
256 {
257
258 // Messages for Animation
259
260 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
261 {
262   typedef MessageValue1< Animation, float > LocalType;
263
264   // Reserve some memory inside the message queue
265   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
266
267   // Construct message in the message queue memory; note that delete should not be called on the return value
268   new (slot) LocalType( &animation, &Animation::SetDuration, durationSeconds );
269 }
270
271 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
272 {
273   typedef MessageValue1< Animation, bool > LocalType;
274
275   // Reserve some memory inside the message queue
276   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
277
278   // Construct message in the message queue memory; note that delete should not be called on the return value
279   new (slot) LocalType( &animation, &Animation::SetLooping, looping );
280 }
281
282 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
283 {
284   typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
285
286   // Reserve some memory inside the message queue
287   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
288
289   // Construct message in the message queue memory; note that delete should not be called on the return value
290   new (slot) LocalType( &animation, &Animation::SetEndAction, action );
291 }
292
293 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
294 {
295   typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
296
297   // Reserve some memory inside the message queue
298   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
299
300   // Construct message in the message queue memory; note that delete should not be called on the return value
301   new (slot) LocalType( &animation, &Animation::SetDestroyAction, action );
302 }
303
304 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
305 {
306   typedef Message< Animation > LocalType;
307
308   // Reserve some memory inside the message queue
309   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
310
311   // Construct message in the message queue memory; note that delete should not be called on the return value
312   new (slot) LocalType( &animation, &Animation::Play );
313 }
314
315 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
316 {
317   typedef Message< Animation > LocalType;
318
319   // Reserve some memory inside the message queue
320   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
321
322   // Construct message in the message queue memory; note that delete should not be called on the return value
323   new (slot) LocalType( &animation, &Animation::Pause );
324 }
325
326 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
327 {
328   typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
329
330   // Reserve some memory inside the message queue
331   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
332
333   // Construct message in the message queue memory; note that delete should not be called on the return value
334   new (slot) LocalType( &animation, &Animation::AddAnimator, &animator, const_cast<PropertyOwner*>( &owner ) );
335 }
336
337 } // namespace SceneGraph
338
339 } // namespace Internal
340
341 } // namespace Dali
342
343 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__