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] 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
72 static Animation* New( float durationSeconds, float speedFactor, bool isLooping, EndAction endAction, EndAction destroyAction )
74 return new Animation( durationSeconds, speedFactor, isLooping, endAction, destroyAction );
83 * Set the duration of an animation.
84 * @pre durationSeconds must be greater than zero.
85 * @param[in] durationSeconds The duration in seconds.
87 void SetDuration(float durationSeconds);
90 * Retrieve the duration of the animation.
91 * @return The duration in seconds.
93 float GetDuration() const
95 return mDurationSeconds;
99 * Retrieve the current progress of the animation.
100 * @return The current progress as a normalized value between [0,1].
102 float GetCurrentProgress() const
104 if( mDurationSeconds > 0.0f )
106 return mElapsedSeconds / mDurationSeconds;
113 * Sets the progress of the animation.
114 * @param[in] The new progress as a normalized value between [0,1]
116 void SetCurrentProgress( float progress )
118 mElapsedSeconds = mDurationSeconds * progress;
121 void SetSpeedFactor( float factor )
123 mSpeedFactor = factor;
127 * Set whether the animation will loop.
128 * @param[in] looping True if the animation will loop.
130 void SetLooping(bool looping);
133 * Query whether the animation will loop.
134 * @return True if the animation will loop.
136 bool IsLooping() const
142 * Set the end action of the animation.
143 * @param[in] action The end action.
145 void SetEndAction(EndAction action);
148 * Retrieve the action performed when the animation ends.
149 * @return The end action.
151 EndAction GetEndAction()
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.
162 void SetDestroyAction(EndAction action);
165 * Retrieve the action performed when the animation is destroyed.
166 * @return The destroy action.
168 EndAction GetDestroyAction()
170 return mDestroyAction;
174 * Play the animation.
179 * Play the animation from a given point
180 * @param[in] progress A value between [0,1] form where the animation should start playing
182 void PlayFrom( float progress );
185 * Pause the animation.
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)
194 bool Stop(BufferIndex bufferIndex);
197 * Called shortly before the animation is destroyed.
198 * @param[in] bufferIndex The buffer to update when mDestroyAction == Bake.
200 void OnDestroy(BufferIndex bufferIndex);
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.
207 State GetState() const
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
216 int GetPlayCount() const
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.
228 void AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner );
231 * Retrieve the animators from an animation.
232 * @return The container of animators.
234 AnimatorContainer& GetAnimators()
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.
246 bool Update(BufferIndex bufferIndex, float elapsedSeconds);
252 * Protected constructor. See New()
254 Animation( float durationSeconds, float speedFactor, bool isLooping, EndAction endAction, EndAction destroyAction );
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.
264 void UpdateAnimators(BufferIndex bufferIndex, bool bake);
267 Animation(const Animation&);
270 Animation& operator=(const Animation& rhs);
274 float mDurationSeconds;
277 EndAction mEndAction;
278 EndAction mDestroyAction;
281 float mElapsedSeconds;
284 AnimatorContainer mAnimators;
287 }; //namespace SceneGraph
289 // value types used by messages
290 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
295 // Messages for Animation
297 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
299 typedef MessageValue1< Animation, float > LocalType;
301 // Reserve some memory inside the message queue
302 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
304 // Construct message in the message queue memory; note that delete should not be called on the return value
305 new (slot) LocalType( &animation, &Animation::SetDuration, durationSeconds );
308 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
310 typedef MessageValue1< Animation, bool > LocalType;
312 // Reserve some memory inside the message queue
313 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
315 // Construct message in the message queue memory; note that delete should not be called on the return value
316 new (slot) LocalType( &animation, &Animation::SetLooping, looping );
319 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
321 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
323 // Reserve some memory inside the message queue
324 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
326 // Construct message in the message queue memory; note that delete should not be called on the return value
327 new (slot) LocalType( &animation, &Animation::SetEndAction, action );
330 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
332 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
334 // Reserve some memory inside the message queue
335 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
337 // Construct message in the message queue memory; note that delete should not be called on the return value
338 new (slot) LocalType( &animation, &Animation::SetDestroyAction, action );
341 inline void SetCurrentProgressMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
343 typedef MessageValue1< Animation, float > LocalType;
345 // Reserve some memory inside the message queue
346 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
348 // Construct message in the message queue memory; note that delete should not be called on the return value
349 new (slot) LocalType( &animation, &Animation::SetCurrentProgress, progress );
352 inline void SetSpeedFactorMessage( EventToUpdate& eventToUpdate, const Animation& animation, float factor )
354 typedef MessageValue1< Animation, float > LocalType;
356 // Reserve some memory inside the message queue
357 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
359 // Construct message in the message queue memory; note that delete should not be called on the return value
360 new (slot) LocalType( &animation, &Animation::SetSpeedFactor, factor );
363 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
365 typedef Message< Animation > LocalType;
367 // Reserve some memory inside the message queue
368 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
370 // Construct message in the message queue memory; note that delete should not be called on the return value
371 new (slot) LocalType( &animation, &Animation::Play );
374 inline void PlayAnimationFromMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
376 typedef MessageValue1< Animation,float > LocalType;
378 // Reserve some memory inside the message queue
379 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
381 // Construct message in the message queue memory; note that delete should not be called on the return value
382 new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
385 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
387 typedef Message< Animation > LocalType;
389 // Reserve some memory inside the message queue
390 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
392 // Construct message in the message queue memory; note that delete should not be called on the return value
393 new (slot) LocalType( &animation, &Animation::Pause );
396 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
398 typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
400 // Reserve some memory inside the message queue
401 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
403 // Construct message in the message queue memory; note that delete should not be called on the return value
404 new (slot) LocalType( &animation, &Animation::AddAnimator, &animator, const_cast<PropertyOwner*>( &owner ) );
407 } // namespace SceneGraph
409 } // namespace Internal
413 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__