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 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
39 typedef OwnerContainer< Animation* > AnimationContainer;
41 typedef AnimationContainer::Iterator AnimationIter;
42 typedef AnimationContainer::ConstIterator AnimationConstIter;
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.
53 typedef Dali::Animation::EndAction EndAction;
64 * Construct a new Animation.
65 * @param[in] durationSeconds The duration of the animation in seconds.
66 * @param[in] isLooping Whether the animation will loop.
67 * @param[in] endAction The action to perform when the animation ends.
68 * @param[in] destroyAction The action to perform when the animation is destroyed.
69 * @return A new Animation
71 static Animation* New( float durationSeconds, bool isLooping, EndAction endAction, EndAction destroyAction )
73 return new Animation( durationSeconds, isLooping, endAction, destroyAction );
82 * Set the duration of an animation.
83 * @pre durationSeconds must be greater than zero.
84 * @param[in] durationSeconds The duration in seconds.
86 void SetDuration(float durationSeconds);
89 * Retrieve the duration of the animation.
90 * @return The duration in seconds.
92 float GetDuration() const
94 return mDurationSeconds;
98 * Set whether the animation will loop.
99 * @param[in] looping True if the animation will loop.
101 void SetLooping(bool looping);
104 * Query whether the animation will loop.
105 * @return True if the animation will loop.
107 bool IsLooping() const
113 * Set the end action of the animation.
114 * @param[in] action The end action.
116 void SetEndAction(EndAction action);
119 * Retrieve the action performed when the animation ends.
120 * @return The end action.
122 EndAction GetEndAction()
128 * Set the destroy action of the animation.
129 * This action is performed during the next update when
130 * the animation is destroyed.
131 * @param[in] action The destroy action.
133 void SetDestroyAction(EndAction action);
136 * Retrieve the action performed when the animation is destroyed.
137 * @return The destroy action.
139 EndAction GetDestroyAction()
141 return mDestroyAction;
145 * Play the animation.
150 * Pause the animation.
155 * Stop the animation.
156 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
157 * @return True if the animation has finished (otherwise it wasn't playing)
159 bool Stop(BufferIndex bufferIndex);
162 * Called shortly before the animation is destroyed.
163 * @param[in] bufferIndex The buffer to update when mDestroyAction == Bake.
165 void OnDestroy(BufferIndex bufferIndex);
168 * Query whether the animation is playing, paused or stopped.
169 * Note that even when paused, the Update() method should be called,
170 * since the current progress must be reapplied each frame.
172 State GetState() const
178 * Retrive a count of the number of times the animation has been played to completion.
179 * This can be used to emit "Finised" signals from the public-api
181 int GetPlayCount() const
187 * Add a newly created animator.
188 * Animators are automatically removed, when orphaned from an animatable scene object.
189 * @param[in] animator The animator to add.
190 * @param[in] propertyOwner The scene-object that owns the animatable property.
191 * @post The animator is owned by this animation.
193 void AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner );
196 * Retrieve the animators from an animation.
197 * @return The container of animators.
199 AnimatorContainer& GetAnimators()
205 * This causes the animators to change the properties of objects in the scene graph.
206 * @pre The animation is playing or paused.
207 * @param[in] bufferIndex The buffer to update.
208 * @param[in] elapsedSeconds The time elapsed since the previous frame.
209 * @return True if the animation has finished.
211 bool Update(BufferIndex bufferIndex, float elapsedSeconds);
217 * Protected constructor. See New()
219 Animation( float durationSeconds, bool isLooping, EndAction endAction, EndAction destroyAction );
225 * Helper for Update, also used to bake when the animation is stopped or destroyed.
226 * @param[in] bufferIndex The buffer to update.
227 * @param[in] bake True if the final result should be baked.
229 void UpdateAnimators(BufferIndex bufferIndex, bool bake);
232 Animation(const Animation&);
235 Animation& operator=(const Animation& rhs);
239 float mDurationSeconds;
241 EndAction mEndAction;
242 EndAction mDestroyAction;
245 float mElapsedSeconds;
248 AnimatorContainer mAnimators;
251 }; //namespace SceneGraph
253 // value types used by messages
254 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
259 // Messages for Animation
261 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
263 typedef MessageValue1< Animation, float > LocalType;
265 // Reserve some memory inside the message queue
266 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
268 // Construct message in the message queue memory; note that delete should not be called on the return value
269 new (slot) LocalType( &animation, &Animation::SetDuration, durationSeconds );
272 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
274 typedef MessageValue1< Animation, bool > LocalType;
276 // Reserve some memory inside the message queue
277 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
279 // Construct message in the message queue memory; note that delete should not be called on the return value
280 new (slot) LocalType( &animation, &Animation::SetLooping, looping );
283 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
285 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
287 // Reserve some memory inside the message queue
288 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
290 // Construct message in the message queue memory; note that delete should not be called on the return value
291 new (slot) LocalType( &animation, &Animation::SetEndAction, action );
294 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
296 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
298 // Reserve some memory inside the message queue
299 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
301 // Construct message in the message queue memory; note that delete should not be called on the return value
302 new (slot) LocalType( &animation, &Animation::SetDestroyAction, action );
305 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
307 typedef Message< Animation > LocalType;
309 // Reserve some memory inside the message queue
310 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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::Play );
316 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
318 typedef Message< Animation > LocalType;
320 // Reserve some memory inside the message queue
321 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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::Pause );
327 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
329 typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
331 // Reserve some memory inside the message queue
332 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
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::AddAnimator, &animator, const_cast<PropertyOwner*>( &owner ) );
338 } // namespace SceneGraph
340 } // namespace Internal
344 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__