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] playRange Minimum and maximum progress between which the animation will play.
68 * @param[in] isLooping Whether the animation will loop.
69 * @param[in] endAction The action to perform when the animation ends.
70 * @param[in] destroyAction The action to perform when the animation is destroyed.
71 * @return A new Animation
73 static Animation* New( float durationSeconds, float speedFactor, const Vector2& playRange, bool isLooping, EndAction endAction, EndAction destroyAction )
75 return new Animation( durationSeconds, speedFactor, playRange, isLooping, endAction, destroyAction );
84 * Set the duration of an animation.
85 * @pre durationSeconds must be greater than zero.
86 * @param[in] durationSeconds The duration in seconds.
88 void SetDuration(float durationSeconds);
91 * Retrieve the duration of the animation.
92 * @return The duration in seconds.
94 float GetDuration() const
96 return mDurationSeconds;
100 * Retrieve the current progress of the animation.
101 * @return The current progress as a normalized value between [0,1].
103 float GetCurrentProgress() const
105 if( mDurationSeconds > 0.0f )
107 return mElapsedSeconds / mDurationSeconds;
114 * Sets the progress of the animation.
115 * @param[in] The new progress as a normalized value between [0,1]
117 void SetCurrentProgress( float progress )
119 mElapsedSeconds = mDurationSeconds * progress;
122 void SetSpeedFactor( float factor )
124 mSpeedFactor = factor;
128 * Set whether the animation will loop.
129 * @param[in] looping True if the animation will loop.
131 void SetLooping(bool looping);
134 * Query whether the animation will loop.
135 * @return True if the animation will loop.
137 bool IsLooping() const
143 * Set the end action of the animation.
144 * @param[in] action The end action.
146 void SetEndAction(EndAction action);
149 * Retrieve the action performed when the animation ends.
150 * @return The end action.
152 EndAction GetEndAction()
158 * Set the destroy action of the animation.
159 * This action is performed during the next update when
160 * the animation is destroyed.
161 * @param[in] action The destroy action.
163 void SetDestroyAction(EndAction action);
166 * Retrieve the action performed when the animation is destroyed.
167 * @return The destroy action.
169 EndAction GetDestroyAction()
171 return mDestroyAction;
175 * Set the playing range. The animation will only play between the minimum and maximum progress
178 * @param[in] range Two values between [0,1] to specify minimum and maximum progress.
180 void SetPlayRange( const Vector2& range );
183 * Play the animation.
188 * Play the animation from a given point
189 * @param[in] progress A value between [0,1] form where the animation should start playing
191 void PlayFrom( float progress );
194 * Pause the animation.
199 * Stop the animation.
200 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
201 * @return True if the animation has finished (otherwise it wasn't playing)
203 bool Stop(BufferIndex bufferIndex);
206 * Called shortly before the animation is destroyed.
207 * @param[in] bufferIndex The buffer to update when mDestroyAction == Bake.
209 void OnDestroy(BufferIndex bufferIndex);
212 * Query whether the animation is playing, paused or stopped.
213 * Note that even when paused, the Update() method should be called,
214 * since the current progress must be reapplied each frame.
216 State GetState() const
222 * Retrive a count of the number of times the animation has been played to completion.
223 * This can be used to emit "Finised" signals from the public-api
225 int GetPlayCount() const
231 * Add a newly created animator.
232 * Animators are automatically removed, when orphaned from an animatable scene object.
233 * @param[in] animator The animator to add.
234 * @param[in] propertyOwner The scene-object that owns the animatable property.
235 * @post The animator is owned by this animation.
237 void AddAnimator( AnimatorBase* animator, PropertyOwner* propertyOwner );
240 * Retrieve the animators from an animation.
241 * @return The container of animators.
243 AnimatorContainer& GetAnimators()
249 * This causes the animators to change the properties of objects in the scene graph.
250 * @pre The animation is playing or paused.
251 * @param[in] bufferIndex The buffer to update.
252 * @param[in] elapsedSeconds The time elapsed since the previous frame.
253 * @return True if the animation has finished.
255 bool Update(BufferIndex bufferIndex, float elapsedSeconds);
261 * Protected constructor. See New()
263 Animation( float durationSeconds, float speedFactor, const Vector2& playRange, bool isLooping, EndAction endAction, EndAction destroyAction );
269 * Helper for Update, also used to bake when the animation is stopped or destroyed.
270 * @param[in] bufferIndex The buffer to update.
271 * @param[in] bake True if the final result should be baked.
273 void UpdateAnimators(BufferIndex bufferIndex, bool bake);
276 * Helper function to bake the result of the animation when it is stopped or
278 * @param[in] bufferIndex The buffer to update.
279 * @param[in] action The end action specified.
281 void Bake(BufferIndex bufferIndex, EndAction action );
284 Animation(const Animation&);
287 Animation& operator=(const Animation& rhs);
291 float mDurationSeconds;
294 EndAction mEndAction;
295 EndAction mDestroyAction;
298 float mElapsedSeconds;
302 AnimatorContainer mAnimators;
305 }; //namespace SceneGraph
307 // value types used by messages
308 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
313 // Messages for Animation
315 inline void SetDurationMessage( EventToUpdate& eventToUpdate, const Animation& animation, float durationSeconds )
317 typedef MessageValue1< Animation, float > 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::SetDuration, durationSeconds );
326 inline void SetLoopingMessage( EventToUpdate& eventToUpdate, const Animation& animation, bool looping )
328 typedef MessageValue1< Animation, bool > 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::SetLooping, looping );
337 inline void SetEndActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
339 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
341 // Reserve some memory inside the message queue
342 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
344 // Construct message in the message queue memory; note that delete should not be called on the return value
345 new (slot) LocalType( &animation, &Animation::SetEndAction, action );
348 inline void SetDestroyActionMessage( EventToUpdate& eventToUpdate, const Animation& animation, Dali::Animation::EndAction action )
350 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
352 // Reserve some memory inside the message queue
353 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
355 // Construct message in the message queue memory; note that delete should not be called on the return value
356 new (slot) LocalType( &animation, &Animation::SetDestroyAction, action );
359 inline void SetCurrentProgressMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
361 typedef MessageValue1< Animation, float > LocalType;
363 // Reserve some memory inside the message queue
364 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
366 // Construct message in the message queue memory; note that delete should not be called on the return value
367 new (slot) LocalType( &animation, &Animation::SetCurrentProgress, progress );
370 inline void SetSpeedFactorMessage( EventToUpdate& eventToUpdate, const Animation& animation, float factor )
372 typedef MessageValue1< Animation, float > LocalType;
374 // Reserve some memory inside the message queue
375 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
377 // Construct message in the message queue memory; note that delete should not be called on the return value
378 new (slot) LocalType( &animation, &Animation::SetSpeedFactor, factor );
381 inline void SetPlayRangeMessage( EventToUpdate& eventToUpdate, const Animation& animation, const Vector2& range )
383 typedef MessageValue1< Animation, Vector2 > LocalType;
385 // Reserve some memory inside the message queue
386 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
388 // Construct message in the message queue memory; note that delete should not be called on the return value
389 new (slot) LocalType( &animation, &Animation::SetPlayRange, range );
392 inline void PlayAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
394 typedef Message< Animation > LocalType;
396 // Reserve some memory inside the message queue
397 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
399 // Construct message in the message queue memory; note that delete should not be called on the return value
400 new (slot) LocalType( &animation, &Animation::Play );
403 inline void PlayAnimationFromMessage( EventToUpdate& eventToUpdate, const Animation& animation, float progress )
405 typedef MessageValue1< Animation,float > LocalType;
407 // Reserve some memory inside the message queue
408 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
410 // Construct message in the message queue memory; note that delete should not be called on the return value
411 new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
414 inline void PauseAnimationMessage( EventToUpdate& eventToUpdate, const Animation& animation )
416 typedef Message< Animation > LocalType;
418 // Reserve some memory inside the message queue
419 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
421 // Construct message in the message queue memory; note that delete should not be called on the return value
422 new (slot) LocalType( &animation, &Animation::Pause );
425 inline void AddAnimatorMessage( EventToUpdate& eventToUpdate, const Animation& animation, AnimatorBase& animator, const PropertyOwner& owner )
427 typedef MessageValue2< Animation, OwnerPointer<AnimatorBase>, PropertyOwner* > LocalType;
429 // Reserve some memory inside the message queue
430 unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
432 // Construct message in the message queue memory; note that delete should not be called on the return value
433 new (slot) LocalType( &animation, &Animation::AddAnimator, &animator, const_cast<PropertyOwner*>( &owner ) );
436 } // namespace SceneGraph
438 } // namespace Internal
442 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__