1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__
5 * Copyright (c) 2017 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 * Set the progress marker to trigger notification
96 * @param[in] progress percent of progress to trigger notification, 0.0f < progress <= 1.0f
98 void SetProgressNotification( float progress );
101 * Retrieve the duration of the animation.
102 * @return The duration in seconds.
104 float GetDuration() const
106 return mDurationSeconds;
110 * Retrieve the current progress of the animation.
111 * @return The current progress as a normalized value between [0,1].
113 float GetCurrentProgress() const
115 if( mDurationSeconds > 0.0f )
117 return mElapsedSeconds / mDurationSeconds;
124 * Sets the progress of the animation.
125 * @param[in] The new progress as a normalized value between [0,1]
127 void SetCurrentProgress( float progress )
129 mElapsedSeconds = mDurationSeconds * progress;
132 void SetSpeedFactor( float factor )
134 mSpeedFactor = factor;
138 * Set the animation loop count.
139 * 0 is loop forever, N loop play N times
140 * @param[in] loopCount The loop count
142 void SetLoopCount(int loopCount);
145 * Query whether the animation will loop.
146 * @return True if the animation will loop.
148 bool IsLooping() const
150 return mLoopCount != 1;
155 * @return the loop count
157 int GetLoopCount() const
163 * Set the end action of the animation.
164 * @param[in] action The end action.
166 void SetEndAction(EndAction action);
169 * Retrieve the action performed when the animation ends.
170 * @return The end action.
172 EndAction GetEndAction()
178 * Set the disconnect action of the animation when connected objects are disconnected.
179 * This action is performed during the next update when
180 * the connected object is disconnected.
181 * @param[in] action The disconnect action.
183 void SetDisconnectAction(EndAction action);
186 * Retrieve the action performed when the animation is destroyed.
187 * @return The destroy action.
189 EndAction GetDisconnectAction()
191 return mDisconnectAction;
195 * Set the playing range. The animation will only play between the minimum and maximum progress
198 * @param[in] range Two values between [0,1] to specify minimum and maximum progress.
200 void SetPlayRange( const Vector2& range );
203 * Play the animation.
208 * Play the animation from a given point
209 * @param[in] progress A value between [0,1] form where the animation should start playing
211 void PlayFrom( float progress );
214 * Pause the animation.
219 * Stop the animation.
220 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
221 * @return True if the animation has finished (otherwise it wasn't playing)
223 bool Stop(BufferIndex bufferIndex);
226 * Called shortly before the animation is destroyed.
227 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
229 void OnDestroy(BufferIndex bufferIndex);
232 * Query whether the animation is playing, paused or stopped.
233 * Note that even when paused, the Update() method should be called,
234 * since the current progress must be reapplied each frame.
236 State GetState() const
242 * Retrive a count of the number of times the animation has been played to completion.
243 * This can be used to emit "Finised" signals from the public-api
245 int GetPlayedCount() const
251 * Get the current loop count from zero to GetLoopCount().
253 int GetCurrentLoop() const
259 * Add a newly created animator.
260 * Animators are automatically removed, when orphaned from an animatable scene object.
261 * @param[in] animator The animator to add.
262 * @param[in] propertyOwner The scene-object that owns the animatable property.
263 * @post The animator is owned by this animation.
265 void AddAnimator( OwnerPointer<AnimatorBase>& animator );
268 * Retrieve the animators from an animation.
269 * @return The container of animators.
271 AnimatorContainer& GetAnimators()
277 * This causes the animators to change the properties of objects in the scene graph.
278 * @pre The animation is playing or paused.
279 * @param[in] bufferIndex The buffer to update.
280 * @param[in] elapsedSeconds The time elapsed since the previous frame.
281 * @param[out] looped True if the animation looped
282 * @param[out] finished True if the animation has finished.
283 * @param[out] progressReached True if progress marker reached
285 void Update(BufferIndex bufferIndex, float elapsedSeconds, bool& looped, bool& finished, bool& progressReached );
291 * Protected constructor. See New()
293 Animation( float durationSeconds, float speedFactor, const Vector2& playRange, int loopCount, EndAction endAction, EndAction disconnectAction );
299 * Helper for Update, also used to bake when the animation is stopped or destroyed.
300 * @param[in] bufferIndex The buffer to update.
301 * @param[in] bake True if the final result should be baked.
302 * @param[in] animationFinished True if the animation has finished.
304 void UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animationFinished );
307 * Helper function to bake the result of the animation when it is stopped or
309 * @param[in] bufferIndex The buffer to update.
310 * @param[in] action The end action specified.
312 void Bake(BufferIndex bufferIndex, EndAction action );
315 * Helper function to set active state of animators.
316 * @param[in] active Every animator is set to this state
318 void SetAnimatorsActive( bool active );
321 Animation(const Animation&);
324 Animation& operator=(const Animation& rhs);
328 float mDurationSeconds;
330 EndAction mEndAction;
331 EndAction mDisconnectAction;
334 float mElapsedSeconds;
335 int mPlayedCount; // Incremented at end of animation or completion of all loops
336 // Never incremented when looping forever. Event thread tracks to signal end.
337 int mLoopCount; // N loop setting
338 int mCurrentLoop; // Current loop number
342 float mProgressMarker; // Progress marker to trigger a notification
343 bool mProgressReachedSignalRequired; // Flag to indicate the progress marker was hit
345 AnimatorContainer mAnimators;
348 }; //namespace SceneGraph
350 // value types used by messages
351 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
356 // Messages for Animation
358 inline void SetDurationMessage( EventThreadServices& eventThreadServices, const Animation& animation, float durationSeconds )
360 typedef MessageValue1< Animation, float > 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::SetDuration, durationSeconds );
369 inline void SetProgressNotificationMessage( EventThreadServices& eventThreadServices, const Animation& animation, float progress )
371 typedef MessageValue1< Animation, float > 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::SetProgressNotification, progress );
381 inline void SetLoopingMessage( EventThreadServices& eventThreadServices, const Animation& animation, int loopCount )
383 typedef MessageValue1< Animation, int > LocalType;
385 // Reserve some memory inside the message queue
386 unsigned int* slot = eventThreadServices.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::SetLoopCount, loopCount );
392 inline void SetEndActionMessage( EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action )
394 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
396 // Reserve some memory inside the message queue
397 unsigned int* slot = eventThreadServices.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::SetEndAction, action );
403 inline void SetDisconnectActionMessage( EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action )
405 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
407 // Reserve some memory inside the message queue
408 unsigned int* slot = eventThreadServices.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::SetDisconnectAction, action );
414 inline void SetCurrentProgressMessage( EventThreadServices& eventThreadServices, const Animation& animation, float progress )
416 typedef MessageValue1< Animation, float > LocalType;
418 // Reserve some memory inside the message queue
419 unsigned int* slot = eventThreadServices.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::SetCurrentProgress, progress );
425 inline void SetSpeedFactorMessage( EventThreadServices& eventThreadServices, const Animation& animation, float factor )
427 typedef MessageValue1< Animation, float > LocalType;
429 // Reserve some memory inside the message queue
430 unsigned int* slot = eventThreadServices.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::SetSpeedFactor, factor );
436 inline void SetPlayRangeMessage( EventThreadServices& eventThreadServices, const Animation& animation, const Vector2& range )
438 typedef MessageValue1< Animation, Vector2 > LocalType;
440 // Reserve some memory inside the message queue
441 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
443 // Construct message in the message queue memory; note that delete should not be called on the return value
444 new (slot) LocalType( &animation, &Animation::SetPlayRange, range );
447 inline void PlayAnimationMessage( EventThreadServices& eventThreadServices, const Animation& animation )
449 typedef Message< Animation > LocalType;
451 // Reserve some memory inside the message queue
452 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
454 // Construct message in the message queue memory; note that delete should not be called on the return value
455 new (slot) LocalType( &animation, &Animation::Play );
458 inline void PlayAnimationFromMessage( EventThreadServices& eventThreadServices, const Animation& animation, float progress )
460 typedef MessageValue1< Animation,float > LocalType;
462 // Reserve some memory inside the message queue
463 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
465 // Construct message in the message queue memory; note that delete should not be called on the return value
466 new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
469 inline void PauseAnimationMessage( EventThreadServices& eventThreadServices, const Animation& animation )
471 typedef Message< Animation > LocalType;
473 // Reserve some memory inside the message queue
474 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
476 // Construct message in the message queue memory; note that delete should not be called on the return value
477 new (slot) LocalType( &animation, &Animation::Pause );
480 inline void AddAnimatorMessage( EventThreadServices& eventThreadServices, const Animation& animation, AnimatorBase& animator )
482 typedef MessageValue1< Animation, OwnerPointer<AnimatorBase> > LocalType;
484 // Reserve some memory inside the message queue
485 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
487 // Construct message in the message queue memory; note that delete should not be called on the return value
488 OwnerPointer<AnimatorBase> parameter( &animator );
489 new (slot) LocalType( &animation, &Animation::AddAnimator, parameter );
493 } // namespace SceneGraph
495 } // namespace Internal
499 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__