1 #ifndef DALI_ANIMATION_H
2 #define DALI_ANIMATION_H
5 * Copyright (c) 2018 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 <cstdint> // uint32_t
25 #include <dali/public-api/animation/alpha-function.h>
26 #include <dali/public-api/animation/key-frames.h>
27 #include <dali/public-api/animation/path.h>
28 #include <dali/public-api/animation/time-period.h>
29 #include <dali/public-api/object/any.h>
30 #include <dali/public-api/object/handle.h>
31 #include <dali/public-api/object/property.h>
32 #include <dali/public-api/signals/dali-signal.h>
37 * @addtogroup dali_core_animation
46 namespace Internal DALI_INTERNAL
52 * @brief Dali::Animation can be used to animate the properties of any number of objects, typically Actors.
54 * An example animation setup is shown below:
60 * Actor mActor; // The object we wish to animate
61 * Animation mAnimation; // Keep this to control the animation
64 * // ...To play the animation
66 * mAnimation = Animation::New(3.0f); // duration 3 seconds
67 * mAnimation.AnimateTo(Property(mActor, Actor::Property::POSITION), Vector3(10.0f, 50.0f, 0.0f));
72 * Dali::Animation supports "fire and forget" behaviour i.e. an animation continues to play if the handle is discarded.
73 * Note that in the following example, the "Finish" signal will be emitted:
77 * void ExampleCallback( Animation& source )
79 * std::cout << "Animation has finished" << std::endl;
82 * void ExampleAnimation( Actor actor )
84 * Animation animation = Animation::New(2.0f); // duration 2 seconds
85 * animation.AnimateTo(Property(actor, Actor::Property::POSITION), 10.0f, 50.0f, 0.0f);
86 * animation.FinishedSignal().Connect( ExampleCallback );
88 * } // At this point the animation handle has gone out of scope
90 * Actor actor = Actor::New();
91 * Stage::GetCurrent().Add( actor );
93 * // Fire animation and forget about it
94 * ExampleAnimation( actor );
96 * // However the animation will continue, and "Animation has finished" will be printed after 2 seconds.
100 * If the "Finish" signal is connected to a member function of an object, it must be disconnected before the object is destroyed.
101 * This is typically done in the object destructor, and requires either the Dali::Connection object or Dali::Animation handle to be stored.
103 * The overall animation time is superseded by the values given in the TimePeriod structure used when calling the AnimateTo(), AnimateBy(), AnimateBetween() and Animate() methods.
104 * If any of the individual calls to those functions exceeds the overall animation time, then the overall animation time is automatically extended.
106 * Using AnimateTo and AnimateBy for the same property of the same Actor will yield undefined behaviour especially if the TimePeriod overlaps.
108 * After calling Animation::Play(), Handle::GetProperty will return the target value of the animated property.
111 * | %Signal Name | Method |
112 * |--------------|--------------------------|
113 * | finished | @ref FinishedSignal() |
116 * | %Action Name | %Animation method called |
117 * |--------------|--------------------------|
120 * | pause | Pause() |
123 class DALI_CORE_API Animation : public BaseHandle
127 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type @SINCE_1_0.0
129 typedef Any AnyFunction; ///< Interpolation function @SINCE_1_0.0
132 * @brief Enumeration for what to do when the animation ends, is stopped, or is destroyed.
137 Bake, ///< When the animation ends, the animated property values are saved. @SINCE_1_0.0
138 Discard, ///< When the animation ends, the animated property values are forgotten. @SINCE_1_0.0
139 BakeFinal ///< If the animation is stopped, the animated property values are saved as if the animation had run to completion, otherwise behaves like Bake. @SINCE_1_0.0
143 * @brief Enumeration for what interpolation method to use on key-frame animations.
148 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default) @SINCE_1_0.0
149 Cubic ///< Values in between key frames are interpolated using a cubic polynomial. @SINCE_1_0.0
153 * @brief Enumeration for what state the animation is in.
155 * Note: Calling Reset() on this class will NOT reset the animation. It will call BaseHandle::Reset() which drops the object handle.
161 STOPPED, ///< Animation has stopped @SINCE_1_1.21
162 PLAYING, ///< The animation is playing @SINCE_1_1.21
163 PAUSED ///< The animation is paused @SINCE_1_1.21
167 * @brief Enumeration for what looping mode is in.
173 RESTART, ///< When the animation arrives at the end in looping mode, the animation restarts from the beginning. @SINCE_1_2.60
174 AUTO_REVERSE ///< When the animation arrives at the end in looping mode, the animation reverses direction and runs backwards again. @SINCE_1_2.60
178 * @brief Creates an uninitialized Animation; this can be initialized with Animation::New().
180 * Calling member functions with an uninitialized Animation handle is not allowed.
186 * @brief Creates an initialized Animation.
188 * The animation will not loop.
189 * The default end action is "Bake".
190 * The default alpha function is linear.
192 * @param[in] durationSeconds The duration in seconds
193 * @return A handle to a newly allocated Dali resource
194 * @note durationSeconds can not be negative.
196 static Animation New(float durationSeconds);
199 * @brief Downcasts a handle to Animation handle.
201 * If handle points to an Animation object, the downcast produces valid handle.
202 * If not, the returned handle is left uninitialized.
205 * @param[in] handle Handle to an object
206 * @return Handle to an Animation object or an uninitialized handle
208 static Animation DownCast( BaseHandle handle );
213 * This is non-virtual since derived Handle types must not contain data or virtual methods.
219 * @brief This copy constructor is required for (smart) pointer semantics.
222 * @param[in] handle A reference to the copied handle
224 Animation(const Animation& handle);
227 * @brief This assignment operator is required for (smart) pointer semantics.
230 * @param[in] rhs A reference to the copied handle
231 * @return A reference to this
233 Animation& operator=(const Animation& rhs);
236 * @brief Sets the duration of an animation.
239 * @param[in] seconds The duration in seconds
240 * @pre DurationSeconds must be greater than zero.
242 void SetDuration(float seconds);
245 * @brief Retrieves the duration of an animation.
248 * @return The duration in seconds
250 float GetDuration() const;
253 * @brief Sets whether the animation will loop.
255 * This function resets the loop count and should not be used with SetLoopCount(int).
256 * Setting this parameter does not cause the animation to Play().
259 * @param[in] looping True if the animation will loop
261 void SetLooping(bool looping);
264 * @brief Enables looping for 'count' repeats.
266 * A zero is the same as SetLooping(true) i.e. repeat forever.
267 * This function resets the looping value and should not be used with SetLooping(bool).
268 * Setting this parameter does not cause the animation to Play().
271 * @param[in] count The number of times to loop
273 void SetLoopCount(int32_t count);
276 * @brief Gets the loop count.
278 * A zero is the same as SetLooping(true) ie repeat forever.
279 * The loop count is initially 1 for play once.
282 * @return The number of times to loop
284 int32_t GetLoopCount();
287 * @brief Gets the current loop count.
289 * A value 0 to GetLoopCount() indicating the current loop count when looping.
292 * @return The current number of loops that have occured
294 int32_t GetCurrentLoop();
297 * @brief Queries whether the animation will loop.
300 * @return True if the animation will loop
302 bool IsLooping() const;
305 * @brief Sets the end action of the animation.
307 * This action is performed when the animation ends or if it is stopped.
308 * Default end action is bake.
310 * @param[in] action The end action
312 void SetEndAction(EndAction action);
315 * @brief Returns the end action of the animation.
318 * @return The end action
320 EndAction GetEndAction() const;
323 * @brief Sets the disconnect action.
325 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
326 * Default action is to BakeFinal.
328 * @param[in] disconnectAction The disconnect action
330 void SetDisconnectAction( EndAction disconnectAction );
333 * @brief Returns the disconnect action.
336 * @return The disconnect action
338 EndAction GetDisconnectAction() const;
341 * @brief Sets the default alpha function for an animation.
343 * This is applied to individual property animations, if no further alpha functions are supplied.
345 * @param[in] alpha The default alpha function
347 void SetDefaultAlphaFunction(AlphaFunction alpha);
350 * @brief Retrieves the default alpha function for an animation.
353 * @return The default alpha function
355 AlphaFunction GetDefaultAlphaFunction() const;
358 * @brief Sets the progress of the animation.
360 * The animation will play (or continue playing) from this point. The progress
361 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
362 * otherwise, it will be ignored.
365 * @param[in] progress The new progress as a normalized value between [0,1]
366 * or between the play range if specified
368 void SetCurrentProgress( float progress );
371 * @brief Retrieves the current progress of the animation.
374 * @return The current progress as a normalized value between [0,1]
376 float GetCurrentProgress();
379 * @brief Specifies a speed factor for the animation.
381 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
382 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
383 * to play the animation in reverse.
386 * @param[in] factor A value which will multiply the velocity
388 void SetSpeedFactor( float factor );
391 * @brief Retrieves the speed factor of the animation.
394 * @return Speed factor
396 float GetSpeedFactor() const;
399 * @brief Sets the playing range.
401 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
402 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
405 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
406 * animation will play between those values
408 void SetPlayRange( const Vector2& range );
411 * @brief Gets the playing range.
414 * @return The play range defined for the animation
416 Vector2 GetPlayRange() const;
419 * @brief Play the animation.
425 * @brief Plays the animation from a given point.
427 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
428 * otherwise, it will be ignored.
431 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
433 void PlayFrom( float progress );
436 * @brief Play the animation after a given delay time.
438 * The delay time is not included in the looping time.
439 * When the delay time is negative value, it would treat as play immediately.
441 * @param[in] delaySeconds The delay time
443 void PlayAfter( float delaySeconds );
446 * @brief Pauses the animation.
452 * @brief Queries the state of the animation.
454 * @return The Animation::State
456 State GetState() const;
459 * @brief Stops the animation.
465 * @brief Clears the animation.
467 * This disconnects any objects that were being animated, effectively stopping the animation.
473 * @brief Sets the looping mode.
475 * Animation plays forwards and then restarts from the beginning or runs backwards again.
477 * @param[in] loopingMode The looping mode is one of RESTART and AUTO_REVERSE
479 void SetLoopingMode( LoopingMode loopingMode );
482 * @brief Gets one of the current looping mode.
485 * @return The current looping mode
487 LoopingMode GetLoopingMode() const;
490 * @brief Connects to this signal to be notified when an Animation's animations have finished.
493 * @return A signal object to connect with
495 AnimationSignalType& FinishedSignal();
498 * @brief Animates a property value by a relative amount.
500 * The default alpha function will be used.
501 * The effect will start & end when the animation begins & ends.
503 * @param[in] target The target object/property to animate
504 * @param[in] relativeValue The property value will change by this amount
506 void AnimateBy(Property target, Property::Value relativeValue);
509 * @brief Animates a property value by a relative amount.
511 * The effect will start & end when the animation begins & ends.
513 * @param[in] target The target object/property to animate
514 * @param[in] relativeValue The property value will change by this amount
515 * @param[in] alpha The alpha function to apply
517 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
520 * @brief Animates a property value by a relative amount.
522 * The default alpha function will be used.
524 * @param[in] target The target object/property to animate
525 * @param[in] relativeValue The property value will increase/decrease by this amount
526 * @param[in] period The effect will occur during this time period
528 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
531 * @brief Animates a property value by a relative amount.
534 * @param[in] target The target object/property to animate
535 * @param[in] relativeValue The property value will increase/decrease by this amount
536 * @param[in] alpha The alpha function to apply
537 * @param[in] period The effect will occur during this time period
539 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
542 * @brief Animates a property to a destination value.
544 * The default alpha function will be used.
545 * The effect will start & end when the animation begins & ends.
547 * @param[in] target The target object/property to animate
548 * @param[in] destinationValue The destination value
550 void AnimateTo(Property target, Property::Value destinationValue);
553 * @brief Animates a property to a destination value.
555 * The effect will start & end when the animation begins & ends.
557 * @param[in] target The target object/property to animate
558 * @param[in] destinationValue The destination value
559 * @param[in] alpha The alpha function to apply
561 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
564 * @brief Animates a property to a destination value.
566 * The default alpha function will be used.
568 * @param[in] target The target object/property to animate
569 * @param[in] destinationValue The destination value
570 * @param[in] period The effect will occur during this time period
572 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
575 * @brief Animates a property to a destination value.
578 * @param[in] target The target object/property to animate
579 * @param[in] destinationValue The destination value
580 * @param[in] alpha The alpha function to apply
581 * @param[in] period The effect will occur during this time period
583 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
586 * @brief Animates a property between keyframes.
589 * @param[in] target The target object property to animate
590 * @param[in] keyFrames The set of time/value pairs between which to animate
592 void AnimateBetween(Property target, KeyFrames& keyFrames);
595 * @brief Animates a property between keyframes.
598 * @param[in] target The target object property to animate
599 * @param[in] keyFrames The set of time/value pairs between which to animate
600 * @param[in] interpolation The method used to interpolate between values
602 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
605 * @brief Animates a property between keyframes.
608 * @param[in] target The target object property to animate
609 * @param[in] keyFrames The set of time/value pairs between which to animate
610 * @param[in] alpha The alpha function to apply
612 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
615 * @brief Animates a property between keyframes.
618 * @param[in] target The target object property to animate
619 * @param[in] keyFrames The set of time/value pairs between which to animate
620 * @param[in] alpha The alpha function to apply
621 * @param[in] interpolation The method used to interpolate between values
623 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
626 * @brief Animates a property between keyframes.
629 * @param[in] target The target object property to animate
630 * @param[in] keyFrames The set of time/value pairs between which to animate
631 * @param[in] period The effect will occur during this time period
633 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
636 * @brief Animates a property between keyframes.
639 * @param[in] target The target object property to animate
640 * @param[in] keyFrames The set of time/value pairs between which to animate
641 * @param[in] period The effect will occur during this time period
642 * @param[in] interpolation The method used to interpolate between values
644 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
647 * @brief Animates a property between keyframes.
650 * @param[in] target The target object property to animate
651 * @param[in] keyFrames The set of time/value pairs between which to animate
652 * @param[in] alpha The alpha function to apply
653 * @param[in] period The effect will occur during this time period
655 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
658 * @brief Animates a property between keyframes.
661 * @param[in] target The target object property to animate
662 * @param[in] keyFrames The set of time/value pairs between which to animate
663 * @param[in] alpha The alpha function to apply to the overall progress
664 * @param[in] period The effect will occur during this time period
665 * @param[in] interpolation The method used to interpolate between values
667 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
670 // Actor-specific convenience methods
673 * @brief Animates an actor's position and orientation through a predefined path.
675 * The actor will rotate to orient the supplied
676 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
679 * @param[in] actor The actor to animate
680 * @param[in] path The path. It defines position and orientation
681 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
683 void Animate( Actor actor, Path path, const Vector3& forward );
686 * @brief Animates an actor's position and orientation through a predefined path.
688 * The actor will rotate to orient the supplied
689 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
692 * @param[in] actor The actor to animate
693 * @param[in] path The path. It defines position and orientation
694 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
695 * @param[in] alpha The alpha function to apply
697 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
700 * @brief Animates an actor's position and orientation through a predefined path.
702 * The actor will rotate to orient the supplied
703 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
706 * @param[in] actor The actor to animate
707 * @param[in] path The path. It defines position and orientation
708 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
709 * @param[in] period The effect will occur during this time period
711 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
714 * @brief Animates an actor's position and orientation through a predefined path.
716 * The actor will rotate to orient the supplied
717 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
720 * @param[in] actor The actor to animate
721 * @param[in] path The path. It defines position and orientation
722 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
723 * @param[in] alpha The alpha function to apply
724 * @param[in] period The effect will occur during this time period
726 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
729 * @brief Shows an actor during the animation.
732 * @param[in] actor The actor to animate
733 * @param[in] delaySeconds The initial delay from the start of the animation
735 void Show(Actor actor, float delaySeconds);
738 * @brief Hides an actor during the animation.
741 * @param[in] actor The actor to animate
742 * @param[in] delaySeconds The initial delay from the start of the animation
744 void Hide(Actor actor, float delaySeconds);
746 public: // Not intended for use by Application developers
750 * @brief This constructor is used by Animation::New() methods.
752 * @param[in] animation A pointer to a newly allocated Dali resource
754 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
764 #endif // DALI_ANIMATION_H