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>
24 #include <dali/internal/common/buffer-index.h>
25 #include <dali/internal/common/message.h>
26 #include <dali/internal/event/common/event-thread-services.h>
27 #include <dali/internal/update/animation/scene-graph-animator.h>
40 typedef OwnerContainer< Animation* > AnimationContainer;
42 typedef AnimationContainer::Iterator AnimationIter;
43 typedef AnimationContainer::ConstIterator AnimationConstIter;
46 * Animations are used to change the properties of scene graph objects, as part of a scene
47 * managers "update" phase. An animation is a container of Animator objects; the actual setting
48 * of object values is done by the animators.
54 typedef Dali::Animation::EndAction EndAction;
65 * Construct a new Animation.
66 * @param[in] durationSeconds The duration of the animation in seconds.
67 * @param[in] speedFactor Multiplier to the animation velocity.
68 * @param[in] playRange Minimum and maximum progress between which the animation will play.
69 * @param[in] loopCount The number of times the animation will loop. ( See SetLoopCount() )
70 * @param[in] endAction The action to perform when the animation ends.
71 * @param[in] disconnectAction The action to perform when the property owner of an animator is disconnected.
72 * @return A new Animation
74 static Animation* New( float durationSeconds, float speedFactor, const Vector2& playRange, int loopCount, EndAction endAction, EndAction disconnectAction );
82 * Overriden delete operator
83 * Deletes the animation from its global memory pool
85 void operator delete( void* ptr );
88 * Set the duration of an animation.
89 * @pre durationSeconds must be greater than zero.
90 * @param[in] durationSeconds The duration in seconds.
92 void SetDuration(float durationSeconds);
95 * Retrieve the duration of the animation.
96 * @return The duration in seconds.
98 float GetDuration() const
100 return mDurationSeconds;
104 * Retrieve the current progress of the animation.
105 * @return The current progress as a normalized value between [0,1].
107 float GetCurrentProgress() const
109 if( mDurationSeconds > 0.0f )
111 return mElapsedSeconds / mDurationSeconds;
118 * Sets the progress of the animation.
119 * @param[in] The new progress as a normalized value between [0,1]
121 void SetCurrentProgress( float progress )
123 mElapsedSeconds = mDurationSeconds * progress;
126 void SetSpeedFactor( float factor )
128 mSpeedFactor = factor;
132 * Set the animation loop count.
133 * 0 is loop forever, N loop play N times
134 * @param[in] loopCount The loop count
136 void SetLoopCount(int loopCount);
139 * Query whether the animation will loop.
140 * @return True if the animation will loop.
142 bool IsLooping() const
144 return mLoopCount != 1;
149 * @return the loop count
151 int GetLoopCount() const
157 * Set the end action of the animation.
158 * @param[in] action The end action.
160 void SetEndAction(EndAction action);
163 * Retrieve the action performed when the animation ends.
164 * @return The end action.
166 EndAction GetEndAction()
172 * Set the disconnect action of the animation when connected objects are disconnected.
173 * This action is performed during the next update when
174 * the connected object is disconnected.
175 * @param[in] action The disconnect action.
177 void SetDisconnectAction(EndAction action);
180 * Retrieve the action performed when the animation is destroyed.
181 * @return The destroy action.
183 EndAction GetDisconnectAction()
185 return mDisconnectAction;
189 * Set the playing range. The animation will only play between the minimum and maximum progress
192 * @param[in] range Two values between [0,1] to specify minimum and maximum progress.
194 void SetPlayRange( const Vector2& range );
197 * Play the animation.
202 * Play the animation from a given point
203 * @param[in] progress A value between [0,1] form where the animation should start playing
205 void PlayFrom( float progress );
208 * Pause the animation.
213 * Stop the animation.
214 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
215 * @return True if the animation has finished (otherwise it wasn't playing)
217 bool Stop(BufferIndex bufferIndex);
220 * Called shortly before the animation is destroyed.
221 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
223 void OnDestroy(BufferIndex bufferIndex);
226 * Query whether the animation is playing, paused or stopped.
227 * Note that even when paused, the Update() method should be called,
228 * since the current progress must be reapplied each frame.
230 State GetState() const
236 * Retrive a count of the number of times the animation has been played to completion.
237 * This can be used to emit "Finised" signals from the public-api
239 int GetPlayedCount() const
245 * Get the current loop count from zero to GetLoopCount().
247 int GetCurrentLoop() const
253 * Add a newly created animator.
254 * Animators are automatically removed, when orphaned from an animatable scene object.
255 * @param[in] animator The animator to add.
256 * @param[in] propertyOwner The scene-object that owns the animatable property.
257 * @post The animator is owned by this animation.
259 void AddAnimator( AnimatorBase* animator );
262 * Retrieve the animators from an animation.
263 * @return The container of animators.
265 AnimatorContainer& GetAnimators()
271 * This causes the animators to change the properties of objects in the scene graph.
272 * @pre The animation is playing or paused.
273 * @param[in] bufferIndex The buffer to update.
274 * @param[in] elapsedSeconds The time elapsed since the previous frame.
275 * @param[out] looped True if the animation looped
276 * @param[out] finished True if the animation has finished.
278 void Update(BufferIndex bufferIndex, float elapsedSeconds, bool& looped, bool& finished );
284 * Protected constructor. See New()
286 Animation( float durationSeconds, float speedFactor, const Vector2& playRange, int loopCount, EndAction endAction, EndAction disconnectAction );
292 * Helper for Update, also used to bake when the animation is stopped or destroyed.
293 * @param[in] bufferIndex The buffer to update.
294 * @param[in] bake True if the final result should be baked.
295 * @param[in] animationFinished True if the animation has finished.
297 void UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animationFinished );
300 * Helper function to bake the result of the animation when it is stopped or
302 * @param[in] bufferIndex The buffer to update.
303 * @param[in] action The end action specified.
305 void Bake(BufferIndex bufferIndex, EndAction action );
308 * Helper function to set active state of animators.
309 * @param[in] active Every animator is set to this state
311 void SetAnimatorsActive( bool active );
314 Animation(const Animation&);
317 Animation& operator=(const Animation& rhs);
321 float mDurationSeconds;
323 EndAction mEndAction;
324 EndAction mDisconnectAction;
327 float mElapsedSeconds;
328 int mPlayedCount; // Incremented at end of animation or completion of all loops
329 // Never incremented when looping forever. Event thread tracks to signal end.
330 int mLoopCount; // N loop setting
331 int mCurrentLoop; // Current loop number
334 AnimatorContainer mAnimators;
337 }; //namespace SceneGraph
339 // value types used by messages
340 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
345 // Messages for Animation
347 inline void SetDurationMessage( EventThreadServices& eventThreadServices, const Animation& animation, float durationSeconds )
349 typedef MessageValue1< Animation, float > LocalType;
351 // Reserve some memory inside the message queue
352 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
354 // Construct message in the message queue memory; note that delete should not be called on the return value
355 new (slot) LocalType( &animation, &Animation::SetDuration, durationSeconds );
358 inline void SetLoopingMessage( EventThreadServices& eventThreadServices, const Animation& animation, int loopCount )
360 typedef MessageValue1< Animation, int > LocalType;
362 // Reserve some memory inside the message queue
363 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
365 // Construct message in the message queue memory; note that delete should not be called on the return value
366 new (slot) LocalType( &animation, &Animation::SetLoopCount, loopCount );
369 inline void SetEndActionMessage( EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action )
371 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
373 // Reserve some memory inside the message queue
374 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
376 // Construct message in the message queue memory; note that delete should not be called on the return value
377 new (slot) LocalType( &animation, &Animation::SetEndAction, action );
380 inline void SetDisconnectActionMessage( EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action )
382 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
384 // Reserve some memory inside the message queue
385 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
387 // Construct message in the message queue memory; note that delete should not be called on the return value
388 new (slot) LocalType( &animation, &Animation::SetDisconnectAction, action );
391 inline void SetCurrentProgressMessage( EventThreadServices& eventThreadServices, const Animation& animation, float progress )
393 typedef MessageValue1< Animation, float > LocalType;
395 // Reserve some memory inside the message queue
396 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
398 // Construct message in the message queue memory; note that delete should not be called on the return value
399 new (slot) LocalType( &animation, &Animation::SetCurrentProgress, progress );
402 inline void SetSpeedFactorMessage( EventThreadServices& eventThreadServices, const Animation& animation, float factor )
404 typedef MessageValue1< Animation, float > LocalType;
406 // Reserve some memory inside the message queue
407 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
409 // Construct message in the message queue memory; note that delete should not be called on the return value
410 new (slot) LocalType( &animation, &Animation::SetSpeedFactor, factor );
413 inline void SetPlayRangeMessage( EventThreadServices& eventThreadServices, const Animation& animation, const Vector2& range )
415 typedef MessageValue1< Animation, Vector2 > LocalType;
417 // Reserve some memory inside the message queue
418 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
420 // Construct message in the message queue memory; note that delete should not be called on the return value
421 new (slot) LocalType( &animation, &Animation::SetPlayRange, range );
424 inline void PlayAnimationMessage( EventThreadServices& eventThreadServices, const Animation& animation )
426 typedef Message< Animation > LocalType;
428 // Reserve some memory inside the message queue
429 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
431 // Construct message in the message queue memory; note that delete should not be called on the return value
432 new (slot) LocalType( &animation, &Animation::Play );
435 inline void PlayAnimationFromMessage( EventThreadServices& eventThreadServices, const Animation& animation, float progress )
437 typedef MessageValue1< Animation,float > LocalType;
439 // Reserve some memory inside the message queue
440 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
442 // Construct message in the message queue memory; note that delete should not be called on the return value
443 new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
446 inline void PauseAnimationMessage( EventThreadServices& eventThreadServices, const Animation& animation )
448 typedef Message< Animation > LocalType;
450 // Reserve some memory inside the message queue
451 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
453 // Construct message in the message queue memory; note that delete should not be called on the return value
454 new (slot) LocalType( &animation, &Animation::Pause );
457 inline void AddAnimatorMessage( EventThreadServices& eventThreadServices, const Animation& animation, AnimatorBase& animator )
459 typedef MessageValue1< Animation, OwnerPointer<AnimatorBase> > LocalType;
461 // Reserve some memory inside the message queue
462 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
464 // Construct message in the message queue memory; note that delete should not be called on the return value
465 new (slot) LocalType( &animation, &Animation::AddAnimator, &animator );
469 } // namespace SceneGraph
471 } // namespace Internal
475 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__