1 #ifndef DALI_ANIMATION_H
2 #define DALI_ANIMATION_H
5 * Copyright (c) 2024 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, uint8_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
126 using AnimationSignalType = Signal<void(Animation&)>; ///< Animation finished signal type @SINCE_1_0.0
128 using AnyFunction = Any; ///< Interpolation function @SINCE_1_0.0
131 * @brief Enumeration for what to do when the animation ends, is stopped, or is destroyed.
134 enum EndAction : uint8_t
136 BAKE, ///< When the animation ends, the animated property values are saved. @SINCE_1_0.0
137 DISCARD, ///< When the animation ends, the animated property values are forgotten. @SINCE_1_0.0
138 BAKE_FINAL ///< 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
142 * @brief Enumeration for what interpolation method to use on key-frame animations.
145 enum Interpolation : uint8_t
147 LINEAR, ///< Values in between key frames are interpolated using a linear polynomial. (Default) @SINCE_1_0.0
148 CUBIC ///< Values in between key frames are interpolated using a cubic polynomial. @SINCE_1_0.0
152 * @brief Enumeration for what state the animation is in.
154 * Note: Calling Reset() on this class will NOT reset the animation. It will call BaseHandle::Reset() which drops the object handle.
160 STOPPED, ///< Animation has stopped @SINCE_1_1.21
161 PLAYING, ///< The animation is playing @SINCE_1_1.21
162 PAUSED ///< The animation is paused @SINCE_1_1.21
166 * @brief Enumeration for what looping mode is in.
170 enum LoopingMode : uint8_t
172 RESTART, ///< When the animation arrives at the end in looping mode, the animation restarts from the beginning. @SINCE_1_2.60
173 AUTO_REVERSE ///< When the animation arrives at the end in looping mode, the animation reverses direction and runs backwards again. @SINCE_1_2.60
177 * @brief Creates an uninitialized Animation; this can be initialized with Animation::New().
179 * Calling member functions with an uninitialized Animation handle is not allowed.
185 * @brief Creates an initialized Animation.
187 * The animation will not loop.
188 * The default end action is "Bake".
189 * The default alpha function is linear.
191 * @param[in] durationSeconds The duration in seconds
192 * @return A handle to a newly allocated Dali resource
193 * @note durationSeconds can not be negative.
195 static Animation New(float durationSeconds);
198 * @brief Downcasts a handle to Animation handle.
200 * If handle points to an Animation object, the downcast produces valid handle.
201 * If not, the returned handle is left uninitialized.
204 * @param[in] handle Handle to an object
205 * @return Handle to an Animation object or an uninitialized handle
207 static Animation DownCast(BaseHandle handle);
212 * This is non-virtual since derived Handle types must not contain data or virtual methods.
218 * @brief This copy constructor is required for (smart) pointer semantics.
221 * @param[in] handle A reference to the copied handle
223 Animation(const Animation& handle);
226 * @brief This assignment operator is required for (smart) pointer semantics.
229 * @param[in] rhs A reference to the copied handle
230 * @return A reference to this
232 Animation& operator=(const Animation& rhs);
235 * @brief Move constructor.
238 * @param[in] rhs A reference to the moved handle
240 Animation(Animation&& rhs) noexcept;
243 * @brief Move assignment operator.
246 * @param[in] rhs A reference to the moved handle
247 * @return A reference to this handle
249 Animation& operator=(Animation&& rhs) noexcept;
252 * @brief Sets the duration of an animation.
255 * @param[in] seconds The duration in seconds
256 * @pre DurationSeconds must be greater than zero.
258 void SetDuration(float seconds);
261 * @brief Retrieves the duration of an animation.
264 * @return The duration in seconds
266 float GetDuration() const;
269 * @brief Sets whether the animation will loop.
271 * This function resets the loop count and should not be used with SetLoopCount(int).
272 * Setting this parameter does not cause the animation to Play().
275 * @param[in] looping True if the animation will loop
277 void SetLooping(bool looping);
280 * @brief Enables looping for 'count' repeats.
282 * A zero is the same as SetLooping(true) i.e. repeat forever.
283 * This function resets the looping value and should not be used with SetLooping(bool).
284 * Setting this parameter does not cause the animation to Play().
287 * @param[in] count The number of times to loop
289 void SetLoopCount(int32_t count);
292 * @brief Gets the loop count.
294 * A zero is the same as SetLooping(true) ie repeat forever.
295 * The loop count is initially 1 for play once.
298 * @return The number of times to loop
300 int32_t GetLoopCount();
303 * @brief Gets the current loop count.
305 * A value 0 to GetLoopCount() indicating the current loop count when looping.
308 * @return The current number of loops that have occured
310 int32_t GetCurrentLoop();
313 * @brief Queries whether the animation will loop.
316 * @return True if the animation will loop
318 bool IsLooping() const;
321 * @brief Sets the end action of the animation.
323 * This action is performed when the animation ends or if it is stopped.
324 * Default end action is bake.
326 * @param[in] action The end action
328 void SetEndAction(EndAction action);
331 * @brief Returns the end action of the animation.
334 * @return The end action
336 EndAction GetEndAction() const;
339 * @brief Sets the disconnect action.
341 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
342 * Default action is to BakeFinal.
344 * @param[in] disconnectAction The disconnect action
346 void SetDisconnectAction(EndAction disconnectAction);
349 * @brief Returns the disconnect action.
352 * @return The disconnect action
354 EndAction GetDisconnectAction() const;
357 * @brief Sets the default alpha function for an animation.
359 * This is applied to individual property animations, if no further alpha functions are supplied.
361 * @param[in] alpha The default alpha function
363 void SetDefaultAlphaFunction(AlphaFunction alpha);
366 * @brief Retrieves the default alpha function for an animation.
369 * @return The default alpha function
371 AlphaFunction GetDefaultAlphaFunction() const;
374 * @brief Sets the progress of the animation.
376 * The animation will play (or continue playing) from this point. The progress
377 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
378 * otherwise, it will be ignored.
381 * @param[in] progress The new progress as a normalized value between [0,1]
382 * or between the play range if specified
384 void SetCurrentProgress(float progress);
387 * @brief Retrieves the current progress of the animation.
390 * @return The current progress as a normalized value between [0,1]
392 float GetCurrentProgress();
395 * @brief Specifies a speed factor for the animation.
397 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
398 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
399 * to play the animation in reverse.
402 * @param[in] factor A value which will multiply the velocity
404 void SetSpeedFactor(float factor);
407 * @brief Retrieves the speed factor of the animation.
410 * @return Speed factor
412 float GetSpeedFactor() const;
415 * @brief Sets the playing range.
417 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
418 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
421 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
422 * animation will play between those values
424 void SetPlayRange(const Vector2& range);
427 * @brief Gets the playing range.
430 * @return The play range defined for the animation
432 Vector2 GetPlayRange() const;
435 * @brief Sets the blend point to interpolate animate property
438 * @param[in] blendPoint A value between [0,1], If the value of the keyframe whose progress is 0 is different from the current value,
439 * the property is animated as it smoothly blends until the progress reaches the blendPoint.
440 * @note The blend point only affects animation registered with AnimateBetween. Other animations operate the same as when Play() is called.
441 * And the blend point needs to be set before this animation plays. If the blend point changes after playback, animation continuity cannot be guaranteed.
442 * @note In the case of a looping animation, the animation is blended only in the first loop.
444 void SetBlendPoint(float blendPoint);
447 * @brief Gets the Blend Point
450 * @return The blend point to interpolate animate property
452 float GetBlendPoint() const;
455 * @brief Play the animation.
461 * @brief Plays the animation from a given point.
463 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
464 * otherwise, it will be ignored.
467 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
469 void PlayFrom(float progress);
472 * @brief Play the animation after a given delay time.
474 * The delay time is not included in the looping time.
475 * When the delay time is negative value, it would treat as play immediately.
477 * @param[in] delaySeconds The delay time
479 void PlayAfter(float delaySeconds);
482 * @brief Pauses the animation.
488 * @brief Queries the state of the animation.
490 * @return The Animation::State
492 State GetState() const;
495 * @brief Stops the animation.
501 * @brief Clears the animation.
503 * This disconnects any objects that were being animated, effectively stopping the animation.
509 * @brief Sets the looping mode.
511 * Animation plays forwards and then restarts from the beginning or runs backwards again.
513 * @param[in] loopingMode The looping mode is one of RESTART and AUTO_REVERSE
515 void SetLoopingMode(LoopingMode loopingMode);
518 * @brief Gets one of the current looping mode.
521 * @return The current looping mode
523 LoopingMode GetLoopingMode() const;
526 * @brief Get the unique id of Animation. It could be 0 given animation is invalid.
529 * @return The unique id of Animation, or 0 if invalid.
531 uint32_t GetAnimationId() const;
534 * @brief Connects to this signal to be notified when an Animation's animations have finished.
537 * @return A signal object to connect with
539 AnimationSignalType& FinishedSignal();
542 * @brief Animates a property value by a relative amount.
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] relativeValue The property value will change by this amount
550 void AnimateBy(Property target, Property::Value relativeValue);
553 * @brief Animates a property value by a relative amount.
555 * The effect will start & end when the animation begins & ends.
557 * @param[in] target The target object/property to animate
558 * @param[in] relativeValue The property value will change by this amount
559 * @param[in] alpha The alpha function to apply
561 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
564 * @brief Animates a property value by a relative amount.
566 * The default alpha function will be used.
568 * @param[in] target The target object/property to animate
569 * @param[in] relativeValue The property value will increase/decrease by this amount
570 * @param[in] period The effect will occur during this time period
572 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
575 * @brief Animates a property value by a relative amount.
578 * @param[in] target The target object/property to animate
579 * @param[in] relativeValue The property value will increase/decrease by this amount
580 * @param[in] alpha The alpha function to apply
581 * @param[in] period The effect will occur during this time period
583 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
586 * @brief Animates a property to a destination value.
588 * The default alpha function will be used.
589 * The effect will start & end when the animation begins & ends.
591 * @param[in] target The target object/property to animate
592 * @param[in] destinationValue The destination value
594 void AnimateTo(Property target, Property::Value destinationValue);
597 * @brief Animates a property to a destination value.
599 * The effect will start & end when the animation begins & ends.
601 * @param[in] target The target object/property to animate
602 * @param[in] destinationValue The destination value
603 * @param[in] alpha The alpha function to apply
605 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
608 * @brief Animates a property to a destination value.
610 * The default alpha function will be used.
612 * @param[in] target The target object/property to animate
613 * @param[in] destinationValue The destination value
614 * @param[in] period The effect will occur during this time period
616 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
619 * @brief Animates a property to a destination value.
622 * @param[in] target The target object/property to animate
623 * @param[in] destinationValue The destination value
624 * @param[in] alpha The alpha function to apply
625 * @param[in] period The effect will occur during this time period
627 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
630 * @brief Animates a property between keyframes.
633 * @param[in] target The target object property to animate
634 * @param[in] keyFrames The set of time/value pairs between which to animate
636 void AnimateBetween(Property target, KeyFrames keyFrames);
639 * @brief Animates a property between keyframes.
642 * @param[in] target The target object property to animate
643 * @param[in] keyFrames The set of time/value pairs between which to animate
644 * @param[in] interpolation The method used to interpolate between values
646 void AnimateBetween(Property target, KeyFrames keyFrames, Interpolation interpolation);
649 * @brief Animates a property between keyframes.
652 * @param[in] target The target object property to animate
653 * @param[in] keyFrames The set of time/value pairs between which to animate
654 * @param[in] alpha The alpha function to apply
656 void AnimateBetween(Property target, KeyFrames keyFrames, AlphaFunction alpha);
659 * @brief Animates a property between keyframes.
662 * @param[in] target The target object property to animate
663 * @param[in] keyFrames The set of time/value pairs between which to animate
664 * @param[in] alpha The alpha function to apply
665 * @param[in] interpolation The method used to interpolate between values
667 void AnimateBetween(Property target, KeyFrames keyFrames, AlphaFunction alpha, Interpolation interpolation);
670 * @brief Animates a property between keyframes.
673 * @param[in] target The target object property to animate
674 * @param[in] keyFrames The set of time/value pairs between which to animate
675 * @param[in] period The effect will occur during this time period
677 void AnimateBetween(Property target, KeyFrames keyFrames, TimePeriod period);
680 * @brief Animates a property between keyframes.
683 * @param[in] target The target object property to animate
684 * @param[in] keyFrames The set of time/value pairs between which to animate
685 * @param[in] period The effect will occur during this time period
686 * @param[in] interpolation The method used to interpolate between values
688 void AnimateBetween(Property target, KeyFrames keyFrames, TimePeriod period, Interpolation interpolation);
691 * @brief Animates a property between keyframes.
694 * @param[in] target The target object property to animate
695 * @param[in] keyFrames The set of time/value pairs between which to animate
696 * @param[in] alpha The alpha function to apply
697 * @param[in] period The effect will occur during this time period
699 void AnimateBetween(Property target, KeyFrames keyFrames, AlphaFunction alpha, TimePeriod period);
702 * @brief Animates a property between keyframes.
705 * @param[in] target The target object property to animate
706 * @param[in] keyFrames The set of time/value pairs between which to animate
707 * @param[in] alpha The alpha function to apply to the overall progress
708 * @param[in] period The effect will occur during this time period
709 * @param[in] interpolation The method used to interpolate between values
711 void AnimateBetween(Property target, KeyFrames keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
713 // Actor-specific convenience methods
716 * @brief Animates an actor's position and orientation through a predefined path.
718 * The actor will rotate to orient the supplied
719 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
722 * @param[in] actor The actor to animate
723 * @param[in] path The path. It defines position and orientation
724 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
726 void Animate(Actor actor, Path path, const Vector3& forward);
729 * @brief Animates an actor's position and orientation through a predefined path.
731 * The actor will rotate to orient the supplied
732 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
735 * @param[in] actor The actor to animate
736 * @param[in] path The path. It defines position and orientation
737 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
738 * @param[in] alpha The alpha function to apply
740 void Animate(Actor actor, Path path, const Vector3& forward, AlphaFunction alpha);
743 * @brief Animates an actor's position and orientation through a predefined path.
745 * The actor will rotate to orient the supplied
746 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
749 * @param[in] actor The actor to animate
750 * @param[in] path The path. It defines position and orientation
751 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
752 * @param[in] period The effect will occur during this time period
754 void Animate(Actor actor, Path path, const Vector3& forward, TimePeriod period);
757 * @brief Animates an actor's position and orientation through a predefined path.
759 * The actor will rotate to orient the supplied
760 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
763 * @param[in] actor The actor to animate
764 * @param[in] path The path. It defines position and orientation
765 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
766 * @param[in] alpha The alpha function to apply
767 * @param[in] period The effect will occur during this time period
769 void Animate(Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
772 * @brief Shows an actor during the animation.
775 * @param[in] actor The actor to animate
776 * @param[in] delaySeconds The initial delay from the start of the animation
778 void Show(Actor actor, float delaySeconds);
781 * @brief Hides an actor during the animation.
784 * @param[in] actor The actor to animate
785 * @param[in] delaySeconds The initial delay from the start of the animation
787 void Hide(Actor actor, float delaySeconds);
789 public: // Not intended for use by Application developers
792 * @brief This constructor is used by Animation::New() methods.
794 * @param[in] animation A pointer to a newly allocated Dali resource
796 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
805 #endif // DALI_ANIMATION_H