1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
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>
38 typedef OwnerContainer< Animation* > AnimationContainer;
40 typedef AnimationContainer::Iterator AnimationIter;
41 typedef AnimationContainer::ConstIterator AnimationConstIter;
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.
52 typedef Dali::Animation::EndAction EndAction;
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
70 static Animation* New( float durationSeconds, bool isLooping, EndAction endAction, EndAction destroyAction )
72 return new Animation( durationSeconds, isLooping, endAction, destroyAction );
81 * Set the duration of an animation.
82 * @pre durationSeconds must be greater than zero.
83 * @param[in] durationSeconds The duration in seconds.
85 void SetDuration(float durationSeconds);
88 * Retrieve the duration of the animation.
89 * @return The duration in seconds.
91 float GetDuration() const
93 return mDurationSeconds;
97 * Set whether the animation will loop.
98 * @param[in] looping True if the animation will loop.
100 void SetLooping(bool looping);
103 * Query whether the animation will loop.
104 * @return True if the animation will loop.
106 bool IsLooping() const
112 * Set the end action of the animation.
113 * @param[in] action The end action.
115 void SetEndAction(EndAction action);
118 * Retrieve the action performed when the animation ends.
119 * @return The end action.
121 EndAction GetEndAction()
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.
132 void SetDestroyAction(EndAction action);
135 * Retrieve the action performed when the animation is destroyed.
136 * @return The destroy action.
138 EndAction GetDestroyAction()
140 return mDestroyAction;
144 * Play the animation.
149 * Pause the animation.
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)
158 bool Stop(BufferIndex bufferIndex);
161 * Called shortly before the animation is destroyed.
162 * @param[in] bufferIndex The buffer to update when mDestroyAction == Bake.
164 void OnDestroy(BufferIndex bufferIndex);
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.
171 State GetState() const
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
180 int GetPlayCount() const
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.
192 void AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner );
195 * Retrieve the animators from an animation.
196 * @return The container of animators.
198 AnimatorContainer& GetAnimators()
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.
210 bool Update(BufferIndex bufferIndex, float elapsedSeconds);
216 * Protected constructor. See New()
218 Animation( float durationSeconds, bool isLooping, EndAction endAction, EndAction destroyAction );
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.
228 void UpdateAnimators(BufferIndex bufferIndex, bool bake);
231 Animation(const Animation&);
234 Animation& operator=(const Animation& rhs);
238 float mDurationSeconds;
240 EndAction mEndAction;
241 EndAction mDestroyAction;
244 float mElapsedSeconds;
247 AnimatorContainer mAnimators;
250 }; //namespace SceneGraph
252 // value types used by messages
253 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
258 // Messages for Animation
260 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
262 typedef MessageValue1< Animation, float > LocalType;
264 // Reserve some memory inside the message queue
265 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
271 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
273 typedef MessageValue1< Animation, bool > LocalType;
275 // Reserve some memory inside the message queue
276 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
282 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
284 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
286 // Reserve some memory inside the message queue
287 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
293 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
295 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
297 // Reserve some memory inside the message queue
298 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
304 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
306 typedef Message< Animation > LocalType;
308 // Reserve some memory inside the message queue
309 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
315 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
317 typedef Message< Animation > LocalType;
319 // Reserve some memory inside the message queue
320 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 );
326 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
328 typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
330 // Reserve some memory inside the message queue
331 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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 ) );
337 } // namespace SceneGraph
339 } // namespace Internal
343 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__