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] isLooping Whether the animation will loop.
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, bool isLooping, EndAction endAction, EndAction disconnectAction )
76 return new Animation( durationSeconds, speedFactor, playRange, isLooping, endAction, disconnectAction );
85 * Set the duration of an animation.
86 * @pre durationSeconds must be greater than zero.
87 * @param[in] durationSeconds The duration in seconds.
89 void SetDuration(float durationSeconds);
92 * Retrieve the duration of the animation.
93 * @return The duration in seconds.
95 float GetDuration() const
97 return mDurationSeconds;
101 * Retrieve the current progress of the animation.
102 * @return The current progress as a normalized value between [0,1].
104 float GetCurrentProgress() const
106 if( mDurationSeconds > 0.0f )
108 return mElapsedSeconds / mDurationSeconds;
115 * Sets the progress of the animation.
116 * @param[in] The new progress as a normalized value between [0,1]
118 void SetCurrentProgress( float progress )
120 mElapsedSeconds = mDurationSeconds * progress;
123 void SetSpeedFactor( float factor )
125 mSpeedFactor = factor;
129 * Set whether the animation will loop.
130 * @param[in] looping True if the animation will loop.
132 void SetLooping(bool looping);
135 * Query whether the animation will loop.
136 * @return True if the animation will loop.
138 bool IsLooping() const
144 * Set the end action of the animation.
145 * @param[in] action The end action.
147 void SetEndAction(EndAction action);
150 * Retrieve the action performed when the animation ends.
151 * @return The end action.
153 EndAction GetEndAction()
159 * Set the disconnect action of the animation when connected objects are disconnected.
160 * This action is performed during the next update when
161 * the connected object is disconnected.
162 * @param[in] action The disconnect action.
164 void SetDisconnectAction(EndAction action);
167 * Retrieve the action performed when the animation is destroyed.
168 * @return The destroy action.
170 EndAction GetDisconnectAction()
172 return mDisconnectAction;
176 * Set the playing range. The animation will only play between the minimum and maximum progress
179 * @param[in] range Two values between [0,1] to specify minimum and maximum progress.
181 void SetPlayRange( const Vector2& range );
184 * Play the animation.
189 * Play the animation from a given point
190 * @param[in] progress A value between [0,1] form where the animation should start playing
192 void PlayFrom( float progress );
195 * Pause the animation.
200 * Stop the animation.
201 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
202 * @return True if the animation has finished (otherwise it wasn't playing)
204 bool Stop(BufferIndex bufferIndex);
207 * Called shortly before the animation is destroyed.
208 * @param[in] bufferIndex The buffer to update when mEndAction == Bake.
210 void OnDestroy(BufferIndex bufferIndex);
213 * Query whether the animation is playing, paused or stopped.
214 * Note that even when paused, the Update() method should be called,
215 * since the current progress must be reapplied each frame.
217 State GetState() const
223 * Retrive a count of the number of times the animation has been played to completion.
224 * This can be used to emit "Finised" signals from the public-api
226 int GetPlayCount() const
232 * Add a newly created animator.
233 * Animators are automatically removed, when orphaned from an animatable scene object.
234 * @param[in] animator The animator to add.
235 * @param[in] propertyOwner The scene-object that owns the animatable property.
236 * @post The animator is owned by this animation.
238 void AddAnimator( AnimatorBase* animator );
241 * Retrieve the animators from an animation.
242 * @return The container of animators.
244 AnimatorContainer& GetAnimators()
250 * This causes the animators to change the properties of objects in the scene graph.
251 * @pre The animation is playing or paused.
252 * @param[in] bufferIndex The buffer to update.
253 * @param[in] elapsedSeconds The time elapsed since the previous frame.
254 * @return True if the animation has finished.
256 bool Update(BufferIndex bufferIndex, float elapsedSeconds);
262 * Protected constructor. See New()
264 Animation( float durationSeconds, float speedFactor, const Vector2& playRange, bool isLooping, EndAction endAction, EndAction disconnectAction );
270 * Helper for Update, also used to bake when the animation is stopped or destroyed.
271 * @param[in] bufferIndex The buffer to update.
272 * @param[in] bake True if the final result should be baked.
273 * @param[in] animationFinished True if the animation has finished.
275 void UpdateAnimators( BufferIndex bufferIndex, bool bake, bool animationFinished );
278 * Helper function to bake the result of the animation when it is stopped or
280 * @param[in] bufferIndex The buffer to update.
281 * @param[in] action The end action specified.
283 void Bake(BufferIndex bufferIndex, EndAction action );
286 * Helper function to set active state of animators.
287 * @param[in] active Every animator is set to this state
289 void SetAnimatorsActive( bool active );
292 Animation(const Animation&);
295 Animation& operator=(const Animation& rhs);
299 float mDurationSeconds;
302 EndAction mEndAction;
303 EndAction mDisconnectAction;
306 float mElapsedSeconds;
310 AnimatorContainer mAnimators;
313 }; //namespace SceneGraph
315 // value types used by messages
316 template <> struct ParameterType< Dali::Animation::EndAction > : public BasicType< Dali::Animation::EndAction > {};
321 // Messages for Animation
323 inline void SetDurationMessage( EventThreadServices& eventThreadServices, const Animation& animation, float durationSeconds )
325 typedef MessageValue1< Animation, float > LocalType;
327 // Reserve some memory inside the message queue
328 unsigned int* slot = eventThreadServices.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::SetDuration, durationSeconds );
334 inline void SetLoopingMessage( EventThreadServices& eventThreadServices, const Animation& animation, bool looping )
336 typedef MessageValue1< Animation, bool > LocalType;
338 // Reserve some memory inside the message queue
339 unsigned int* slot = eventThreadServices.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::SetLooping, looping );
345 inline void SetEndActionMessage( EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action )
347 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
349 // Reserve some memory inside the message queue
350 unsigned int* slot = eventThreadServices.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::SetEndAction, action );
356 inline void SetDisconnectActionMessage( EventThreadServices& eventThreadServices, const Animation& animation, Dali::Animation::EndAction action )
358 typedef MessageValue1< Animation, Dali::Animation::EndAction > LocalType;
360 // Reserve some memory inside the message queue
361 unsigned int* slot = eventThreadServices.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::SetDisconnectAction, action );
367 inline void SetCurrentProgressMessage( EventThreadServices& eventThreadServices, const Animation& animation, float progress )
369 typedef MessageValue1< Animation, float > LocalType;
371 // Reserve some memory inside the message queue
372 unsigned int* slot = eventThreadServices.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::SetCurrentProgress, progress );
378 inline void SetSpeedFactorMessage( EventThreadServices& eventThreadServices, const Animation& animation, float factor )
380 typedef MessageValue1< Animation, float > LocalType;
382 // Reserve some memory inside the message queue
383 unsigned int* slot = eventThreadServices.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::SetSpeedFactor, factor );
389 inline void SetPlayRangeMessage( EventThreadServices& eventThreadServices, const Animation& animation, const Vector2& range )
391 typedef MessageValue1< Animation, Vector2 > LocalType;
393 // Reserve some memory inside the message queue
394 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
396 // Construct message in the message queue memory; note that delete should not be called on the return value
397 new (slot) LocalType( &animation, &Animation::SetPlayRange, range );
400 inline void PlayAnimationMessage( EventThreadServices& eventThreadServices, const Animation& animation )
402 typedef Message< Animation > LocalType;
404 // Reserve some memory inside the message queue
405 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
407 // Construct message in the message queue memory; note that delete should not be called on the return value
408 new (slot) LocalType( &animation, &Animation::Play );
411 inline void PlayAnimationFromMessage( EventThreadServices& eventThreadServices, const Animation& animation, float progress )
413 typedef MessageValue1< Animation,float > LocalType;
415 // Reserve some memory inside the message queue
416 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
418 // Construct message in the message queue memory; note that delete should not be called on the return value
419 new (slot) LocalType( &animation, &Animation::PlayFrom, progress );
422 inline void PauseAnimationMessage( EventThreadServices& eventThreadServices, const Animation& animation )
424 typedef Message< Animation > LocalType;
426 // Reserve some memory inside the message queue
427 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
429 // Construct message in the message queue memory; note that delete should not be called on the return value
430 new (slot) LocalType( &animation, &Animation::Pause );
433 inline void AddAnimatorMessage( EventThreadServices& eventThreadServices, const Animation& animation, AnimatorBase& animator )
435 typedef MessageValue1< Animation, OwnerPointer<AnimatorBase> > LocalType;
437 // Reserve some memory inside the message queue
438 unsigned int* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
440 // Construct message in the message queue memory; note that delete should not be called on the return value
441 new (slot) LocalType( &animation, &Animation::AddAnimator, &animator );
445 } // namespace SceneGraph
447 } // namespace Internal
451 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATION_H__