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 * Retrieve the current progress of the animation.
99 * @return The current progress as a normalized value between [0,1].
101 float GetCurrentProgress() const
103 if( mDurationSeconds > 0.0f )
105 return mElapsedSeconds / mDurationSeconds;
112 * Sets the progress of the animation.
113 * @param[in] The new progress as a normalized value between [0,1]
115 void SetCurrentProgress( float progress )
117 mElapsedSeconds = mDurationSeconds * progress;
121 * Set whether the animation will loop.
122 * @param[in] looping True if the animation will loop.
124 void SetLooping(bool looping);
127 * Query whether the animation will loop.
128 * @return True if the animation will loop.
130 bool IsLooping() const
136 * Set the end action of the animation.
137 * @param[in] action The end action.
139 void SetEndAction(EndAction action);
142 * Retrieve the action performed when the animation ends.
143 * @return The end action.
145 EndAction GetEndAction()
151 * Set the destroy action of the animation.
152 * This action is performed during the next update when
153 * the animation is destroyed.
154 * @param[in] action The destroy action.
156 void SetDestroyAction(EndAction action);
159 * Retrieve the action performed when the animation is destroyed.
160 * @return The destroy action.
162 EndAction GetDestroyAction()
164 return mDestroyAction;
168 * Play the animation.
173 * Play the animation from a given point
174 * @param[in] progress A value between [0,1] form where the animation should start playing
176 void PlayFrom( float progress );
179 * Pause the animation.
184 * Stop the animation.
185 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
186 * @return True if the animation has finished (otherwise it wasn't playing)
188 bool Stop(BufferIndex bufferIndex);
191 * Called shortly before the animation is destroyed.
192 * @param[in] bufferIndex The buffer to update when mDestroyAction == Bake.
194 void OnDestroy(BufferIndex bufferIndex);
197 * Query whether the animation is playing, paused or stopped.
198 * Note that even when paused, the Update() method should be called,
199 * since the current progress must be reapplied each frame.
201 State GetState() const
207 * Retrive a count of the number of times the animation has been played to completion.
208 * This can be used to emit "Finised" signals from the public-api
210 int GetPlayCount() const
216 * Add a newly created animator.
217 * Animators are automatically removed, when orphaned from an animatable scene object.
218 * @param[in] animator The animator to add.
219 * @param[in] propertyOwner The scene-object that owns the animatable property.
220 * @post The animator is owned by this animation.
222 void AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner );
225 * Retrieve the animators from an animation.
226 * @return The container of animators.
228 AnimatorContainer& GetAnimators()
234 * This causes the animators to change the properties of objects in the scene graph.
235 * @pre The animation is playing or paused.
236 * @param[in] bufferIndex The buffer to update.
237 * @param[in] elapsedSeconds The time elapsed since the previous frame.
238 * @return True if the animation has finished.
240 bool Update(BufferIndex bufferIndex, float elapsedSeconds);
246 * Protected constructor. See New()
248 Animation( float durationSeconds, bool isLooping, EndAction endAction, EndAction destroyAction );
254 * Helper for Update, also used to bake when the animation is stopped or destroyed.
255 * @param[in] bufferIndex The buffer to update.
256 * @param[in] bake True if the final result should be baked.
258 void UpdateAnimators(BufferIndex bufferIndex, bool bake);
261 Animation(const Animation&);
264 Animation& operator=(const Animation& rhs);
268 float mDurationSeconds;
270 EndAction mEndAction;
271 EndAction mDestroyAction;
274 float mElapsedSeconds;
277 AnimatorContainer mAnimators;
280 }; //namespace SceneGraph
282 // value types used by messages
283 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
288 // Messages for Animation
290 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
292 typedef MessageValue1< Animation, float > LocalType;
294 // Reserve some memory inside the message queue
295 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
297 // Construct message in the message queue memory; note that delete should not be called on the return value
298 new (slot) LocalType( &animation, &Animation::SetDuration, durationSeconds );
301 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
303 typedef MessageValue1< Animation, bool > LocalType;
305 // Reserve some memory inside the message queue
306 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
308 // Construct message in the message queue memory; note that delete should not be called on the return value
309 new (slot) LocalType( &animation, &Animation::SetLooping, looping );
312 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
314 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
316 // Reserve some memory inside the message queue
317 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
319 // Construct message in the message queue memory; note that delete should not be called on the return value
320 new (slot) LocalType( &animation, &Animation::SetEndAction, action );
323 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
325 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
327 // Reserve some memory inside the message queue
328 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
330 // Construct message in the message queue memory; note that delete should not be called on the return value
331 new (slot) LocalType( &animation, &Animation::SetDestroyAction, action );
334 inline void SetCurrentProgressMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
336 typedef MessageValue1< Animation, float > LocalType;
338 // Reserve some memory inside the message queue
339 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
341 // Construct message in the message queue memory; note that delete should not be called on the return value
342 new (slot) LocalType( &animation, &Animation::SetCurrentProgress, progress );
345 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
347 typedef Message< Animation > LocalType;
349 // Reserve some memory inside the message queue
350 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
352 // Construct message in the message queue memory; note that delete should not be called on the return value
353 new (slot) LocalType( &animation, &Animation::Play );
356 inline void PlayAnimationFromMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
358 typedef MessageValue1< Animation,float > LocalType;
360 // Reserve some memory inside the message queue
361 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
363 // Construct message in the message queue memory; note that delete should not be called on the return value
364 new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
367 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
369 typedef Message< Animation > LocalType;
371 // Reserve some memory inside the message queue
372 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
374 // Construct message in the message queue memory; note that delete should not be called on the return value
375 new (slot) LocalType( &animation, &Animation::Pause );
378 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
380 typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
382 // Reserve some memory inside the message queue
383 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
385 // Construct message in the message queue memory; note that delete should not be called on the return value
386 new (slot) LocalType( &animation, &Animation::AddAnimator, &animator, const_cast<PropertyOwner*>( &owner ) );
389 } // namespace SceneGraph
391 } // namespace Internal
395 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__