1 #ifndef DALI_ANIMATION_H
2 #define DALI_ANIMATION_H
5 * Copyright (c) 2020 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);
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);
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 Play the animation.
441 * @brief Plays the animation from a given point.
443 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
444 * otherwise, it will be ignored.
447 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
449 void PlayFrom(float progress);
452 * @brief Play the animation after a given delay time.
454 * The delay time is not included in the looping time.
455 * When the delay time is negative value, it would treat as play immediately.
457 * @param[in] delaySeconds The delay time
459 void PlayAfter(float delaySeconds);
462 * @brief Pauses the animation.
468 * @brief Queries the state of the animation.
470 * @return The Animation::State
472 State GetState() const;
475 * @brief Stops the animation.
481 * @brief Clears the animation.
483 * This disconnects any objects that were being animated, effectively stopping the animation.
489 * @brief Sets the looping mode.
491 * Animation plays forwards and then restarts from the beginning or runs backwards again.
493 * @param[in] loopingMode The looping mode is one of RESTART and AUTO_REVERSE
495 void SetLoopingMode(LoopingMode loopingMode);
498 * @brief Gets one of the current looping mode.
501 * @return The current looping mode
503 LoopingMode GetLoopingMode() const;
506 * @brief Connects to this signal to be notified when an Animation's animations have finished.
509 * @return A signal object to connect with
511 AnimationSignalType& FinishedSignal();
514 * @brief Animates a property value by a relative amount.
516 * The default alpha function will be used.
517 * The effect will start & end when the animation begins & ends.
519 * @param[in] target The target object/property to animate
520 * @param[in] relativeValue The property value will change by this amount
522 void AnimateBy(Property target, Property::Value relativeValue);
525 * @brief Animates a property value by a relative amount.
527 * The effect will start & end when the animation begins & ends.
529 * @param[in] target The target object/property to animate
530 * @param[in] relativeValue The property value will change by this amount
531 * @param[in] alpha The alpha function to apply
533 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
536 * @brief Animates a property value by a relative amount.
538 * The default alpha function will be used.
540 * @param[in] target The target object/property to animate
541 * @param[in] relativeValue The property value will increase/decrease by this amount
542 * @param[in] period The effect will occur during this time period
544 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
547 * @brief Animates a property value by a relative amount.
550 * @param[in] target The target object/property to animate
551 * @param[in] relativeValue The property value will increase/decrease by this amount
552 * @param[in] alpha The alpha function to apply
553 * @param[in] period The effect will occur during this time period
555 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
558 * @brief Animates a property to a destination value.
560 * The default alpha function will be used.
561 * The effect will start & end when the animation begins & ends.
563 * @param[in] target The target object/property to animate
564 * @param[in] destinationValue The destination value
566 void AnimateTo(Property target, Property::Value destinationValue);
569 * @brief Animates a property to a destination value.
571 * The effect will start & end when the animation begins & ends.
573 * @param[in] target The target object/property to animate
574 * @param[in] destinationValue The destination value
575 * @param[in] alpha The alpha function to apply
577 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
580 * @brief Animates a property to a destination value.
582 * The default alpha function will be used.
584 * @param[in] target The target object/property to animate
585 * @param[in] destinationValue The destination value
586 * @param[in] period The effect will occur during this time period
588 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
591 * @brief Animates a property to a destination value.
594 * @param[in] target The target object/property to animate
595 * @param[in] destinationValue The destination value
596 * @param[in] alpha The alpha function to apply
597 * @param[in] period The effect will occur during this time period
599 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
602 * @brief Animates a property between keyframes.
605 * @param[in] target The target object property to animate
606 * @param[in] keyFrames The set of time/value pairs between which to animate
608 void AnimateBetween(Property target, KeyFrames& keyFrames);
611 * @brief Animates a property between keyframes.
614 * @param[in] target The target object property to animate
615 * @param[in] keyFrames The set of time/value pairs between which to animate
616 * @param[in] interpolation The method used to interpolate between values
618 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
621 * @brief Animates a property between keyframes.
624 * @param[in] target The target object property to animate
625 * @param[in] keyFrames The set of time/value pairs between which to animate
626 * @param[in] alpha The alpha function to apply
628 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
631 * @brief Animates a property between keyframes.
634 * @param[in] target The target object property to animate
635 * @param[in] keyFrames The set of time/value pairs between which to animate
636 * @param[in] alpha The alpha function to apply
637 * @param[in] interpolation The method used to interpolate between values
639 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
642 * @brief Animates a property between keyframes.
645 * @param[in] target The target object property to animate
646 * @param[in] keyFrames The set of time/value pairs between which to animate
647 * @param[in] period The effect will occur during this time period
649 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
652 * @brief Animates a property between keyframes.
655 * @param[in] target The target object property to animate
656 * @param[in] keyFrames The set of time/value pairs between which to animate
657 * @param[in] period The effect will occur during this time period
658 * @param[in] interpolation The method used to interpolate between values
660 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
663 * @brief Animates a property between keyframes.
666 * @param[in] target The target object property to animate
667 * @param[in] keyFrames The set of time/value pairs between which to animate
668 * @param[in] alpha The alpha function to apply
669 * @param[in] period The effect will occur during this time period
671 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
674 * @brief Animates a property between keyframes.
677 * @param[in] target The target object property to animate
678 * @param[in] keyFrames The set of time/value pairs between which to animate
679 * @param[in] alpha The alpha function to apply to the overall progress
680 * @param[in] period The effect will occur during this time period
681 * @param[in] interpolation The method used to interpolate between values
683 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
685 // Actor-specific convenience methods
688 * @brief Animates an actor's position and orientation through a predefined path.
690 * The actor will rotate to orient the supplied
691 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
694 * @param[in] actor The actor to animate
695 * @param[in] path The path. It defines position and orientation
696 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
698 void Animate(Actor actor, Path path, const Vector3& forward);
701 * @brief Animates an actor's position and orientation through a predefined path.
703 * The actor will rotate to orient the supplied
704 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
707 * @param[in] actor The actor to animate
708 * @param[in] path The path. It defines position and orientation
709 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
710 * @param[in] alpha The alpha function to apply
712 void Animate(Actor actor, Path path, const Vector3& forward, AlphaFunction alpha);
715 * @brief Animates an actor's position and orientation through a predefined path.
717 * The actor will rotate to orient the supplied
718 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
721 * @param[in] actor The actor to animate
722 * @param[in] path The path. It defines position and orientation
723 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
724 * @param[in] period The effect will occur during this time period
726 void Animate(Actor actor, Path path, const Vector3& forward, TimePeriod period);
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
739 * @param[in] period The effect will occur during this time period
741 void Animate(Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
744 * @brief Shows an actor during the animation.
747 * @param[in] actor The actor to animate
748 * @param[in] delaySeconds The initial delay from the start of the animation
750 void Show(Actor actor, float delaySeconds);
753 * @brief Hides an actor during the animation.
756 * @param[in] actor The actor to animate
757 * @param[in] delaySeconds The initial delay from the start of the animation
759 void Hide(Actor actor, float delaySeconds);
761 public: // Not intended for use by Application developers
764 * @brief This constructor is used by Animation::New() methods.
766 * @param[in] animation A pointer to a newly allocated Dali resource
768 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
777 #endif // DALI_ANIMATION_H