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
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 Move constructor.
239 * @param[in] rhs A reference to the moved handle
241 Animation( Animation&& rhs );
244 * @brief Move assignment operator.
247 * @param[in] rhs A reference to the moved handle
248 * @return A reference to this handle
250 Animation& operator=( Animation&& rhs );
253 * @brief Sets the duration of an animation.
256 * @param[in] seconds The duration in seconds
257 * @pre DurationSeconds must be greater than zero.
259 void SetDuration(float seconds);
262 * @brief Retrieves the duration of an animation.
265 * @return The duration in seconds
267 float GetDuration() const;
270 * @brief Sets whether the animation will loop.
272 * This function resets the loop count and should not be used with SetLoopCount(int).
273 * Setting this parameter does not cause the animation to Play().
276 * @param[in] looping True if the animation will loop
278 void SetLooping(bool looping);
281 * @brief Enables looping for 'count' repeats.
283 * A zero is the same as SetLooping(true) i.e. repeat forever.
284 * This function resets the looping value and should not be used with SetLooping(bool).
285 * Setting this parameter does not cause the animation to Play().
288 * @param[in] count The number of times to loop
290 void SetLoopCount(int32_t count);
293 * @brief Gets the loop count.
295 * A zero is the same as SetLooping(true) ie repeat forever.
296 * The loop count is initially 1 for play once.
299 * @return The number of times to loop
301 int32_t GetLoopCount();
304 * @brief Gets the current loop count.
306 * A value 0 to GetLoopCount() indicating the current loop count when looping.
309 * @return The current number of loops that have occured
311 int32_t GetCurrentLoop();
314 * @brief Queries whether the animation will loop.
317 * @return True if the animation will loop
319 bool IsLooping() const;
322 * @brief Sets the end action of the animation.
324 * This action is performed when the animation ends or if it is stopped.
325 * Default end action is bake.
327 * @param[in] action The end action
329 void SetEndAction(EndAction action);
332 * @brief Returns the end action of the animation.
335 * @return The end action
337 EndAction GetEndAction() const;
340 * @brief Sets the disconnect action.
342 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
343 * Default action is to BakeFinal.
345 * @param[in] disconnectAction The disconnect action
347 void SetDisconnectAction( EndAction disconnectAction );
350 * @brief Returns the disconnect action.
353 * @return The disconnect action
355 EndAction GetDisconnectAction() const;
358 * @brief Sets the default alpha function for an animation.
360 * This is applied to individual property animations, if no further alpha functions are supplied.
362 * @param[in] alpha The default alpha function
364 void SetDefaultAlphaFunction(AlphaFunction alpha);
367 * @brief Retrieves the default alpha function for an animation.
370 * @return The default alpha function
372 AlphaFunction GetDefaultAlphaFunction() const;
375 * @brief Sets the progress of the animation.
377 * The animation will play (or continue playing) from this point. The progress
378 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
379 * otherwise, it will be ignored.
382 * @param[in] progress The new progress as a normalized value between [0,1]
383 * or between the play range if specified
385 void SetCurrentProgress( float progress );
388 * @brief Retrieves the current progress of the animation.
391 * @return The current progress as a normalized value between [0,1]
393 float GetCurrentProgress();
396 * @brief Specifies a speed factor for the animation.
398 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
399 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
400 * to play the animation in reverse.
403 * @param[in] factor A value which will multiply the velocity
405 void SetSpeedFactor( float factor );
408 * @brief Retrieves the speed factor of the animation.
411 * @return Speed factor
413 float GetSpeedFactor() const;
416 * @brief Sets the playing range.
418 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
419 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
422 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
423 * animation will play between those values
425 void SetPlayRange( const Vector2& range );
428 * @brief Gets the playing range.
431 * @return The play range defined for the animation
433 Vector2 GetPlayRange() const;
436 * @brief Play the animation.
442 * @brief Plays the animation from a given point.
444 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
445 * otherwise, it will be ignored.
448 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
450 void PlayFrom( float progress );
453 * @brief Play the animation after a given delay time.
455 * The delay time is not included in the looping time.
456 * When the delay time is negative value, it would treat as play immediately.
458 * @param[in] delaySeconds The delay time
460 void PlayAfter( float delaySeconds );
463 * @brief Pauses the animation.
469 * @brief Queries the state of the animation.
471 * @return The Animation::State
473 State GetState() const;
476 * @brief Stops the animation.
482 * @brief Clears the animation.
484 * This disconnects any objects that were being animated, effectively stopping the animation.
490 * @brief Sets the looping mode.
492 * Animation plays forwards and then restarts from the beginning or runs backwards again.
494 * @param[in] loopingMode The looping mode is one of RESTART and AUTO_REVERSE
496 void SetLoopingMode( LoopingMode loopingMode );
499 * @brief Gets one of the current looping mode.
502 * @return The current looping mode
504 LoopingMode GetLoopingMode() const;
507 * @brief Connects to this signal to be notified when an Animation's animations have finished.
510 * @return A signal object to connect with
512 AnimationSignalType& FinishedSignal();
515 * @brief Animates a property value by a relative amount.
517 * The default alpha function will be used.
518 * The effect will start & end when the animation begins & ends.
520 * @param[in] target The target object/property to animate
521 * @param[in] relativeValue The property value will change by this amount
523 void AnimateBy(Property target, Property::Value relativeValue);
526 * @brief Animates a property value by a relative amount.
528 * The effect will start & end when the animation begins & ends.
530 * @param[in] target The target object/property to animate
531 * @param[in] relativeValue The property value will change by this amount
532 * @param[in] alpha The alpha function to apply
534 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
537 * @brief Animates a property value by a relative amount.
539 * The default alpha function will be used.
541 * @param[in] target The target object/property to animate
542 * @param[in] relativeValue The property value will increase/decrease by this amount
543 * @param[in] period The effect will occur during this time period
545 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
548 * @brief Animates a property value by a relative amount.
551 * @param[in] target The target object/property to animate
552 * @param[in] relativeValue The property value will increase/decrease by this amount
553 * @param[in] alpha The alpha function to apply
554 * @param[in] period The effect will occur during this time period
556 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
559 * @brief Animates a property to a destination value.
561 * The default alpha function will be used.
562 * The effect will start & end when the animation begins & ends.
564 * @param[in] target The target object/property to animate
565 * @param[in] destinationValue The destination value
567 void AnimateTo(Property target, Property::Value destinationValue);
570 * @brief Animates a property to a destination value.
572 * The effect will start & end when the animation begins & ends.
574 * @param[in] target The target object/property to animate
575 * @param[in] destinationValue The destination value
576 * @param[in] alpha The alpha function to apply
578 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
581 * @brief Animates a property to a destination value.
583 * The default alpha function will be used.
585 * @param[in] target The target object/property to animate
586 * @param[in] destinationValue The destination value
587 * @param[in] period The effect will occur during this time period
589 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
592 * @brief Animates a property to a destination value.
595 * @param[in] target The target object/property to animate
596 * @param[in] destinationValue The destination value
597 * @param[in] alpha The alpha function to apply
598 * @param[in] period The effect will occur during this time period
600 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
603 * @brief Animates a property between keyframes.
606 * @param[in] target The target object property to animate
607 * @param[in] keyFrames The set of time/value pairs between which to animate
609 void AnimateBetween(Property target, KeyFrames& keyFrames);
612 * @brief Animates a property between keyframes.
615 * @param[in] target The target object property to animate
616 * @param[in] keyFrames The set of time/value pairs between which to animate
617 * @param[in] interpolation The method used to interpolate between values
619 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
622 * @brief Animates a property between keyframes.
625 * @param[in] target The target object property to animate
626 * @param[in] keyFrames The set of time/value pairs between which to animate
627 * @param[in] alpha The alpha function to apply
629 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
632 * @brief Animates a property between keyframes.
635 * @param[in] target The target object property to animate
636 * @param[in] keyFrames The set of time/value pairs between which to animate
637 * @param[in] alpha The alpha function to apply
638 * @param[in] interpolation The method used to interpolate between values
640 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
643 * @brief Animates a property between keyframes.
646 * @param[in] target The target object property to animate
647 * @param[in] keyFrames The set of time/value pairs between which to animate
648 * @param[in] period The effect will occur during this time period
650 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
653 * @brief Animates a property between keyframes.
656 * @param[in] target The target object property to animate
657 * @param[in] keyFrames The set of time/value pairs between which to animate
658 * @param[in] period The effect will occur during this time period
659 * @param[in] interpolation The method used to interpolate between values
661 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
664 * @brief Animates a property between keyframes.
667 * @param[in] target The target object property to animate
668 * @param[in] keyFrames The set of time/value pairs between which to animate
669 * @param[in] alpha The alpha function to apply
670 * @param[in] period The effect will occur during this time period
672 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
675 * @brief Animates a property between keyframes.
678 * @param[in] target The target object property to animate
679 * @param[in] keyFrames The set of time/value pairs between which to animate
680 * @param[in] alpha The alpha function to apply to the overall progress
681 * @param[in] period The effect will occur during this time period
682 * @param[in] interpolation The method used to interpolate between values
684 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
687 // Actor-specific convenience methods
690 * @brief Animates an actor's position and orientation through a predefined path.
692 * The actor will rotate to orient the supplied
693 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
696 * @param[in] actor The actor to animate
697 * @param[in] path The path. It defines position and orientation
698 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
700 void Animate( Actor actor, Path path, const Vector3& forward );
703 * @brief Animates an actor's position and orientation through a predefined path.
705 * The actor will rotate to orient the supplied
706 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
709 * @param[in] actor The actor to animate
710 * @param[in] path The path. It defines position and orientation
711 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
712 * @param[in] alpha The alpha function to apply
714 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
717 * @brief Animates an actor's position and orientation through a predefined path.
719 * The actor will rotate to orient the supplied
720 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
723 * @param[in] actor The actor to animate
724 * @param[in] path The path. It defines position and orientation
725 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
726 * @param[in] period The effect will occur during this time period
728 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
731 * @brief Animates an actor's position and orientation through a predefined path.
733 * The actor will rotate to orient the supplied
734 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
737 * @param[in] actor The actor to animate
738 * @param[in] path The path. It defines position and orientation
739 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
740 * @param[in] alpha The alpha function to apply
741 * @param[in] period The effect will occur during this time period
743 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
746 * @brief Shows an actor during the animation.
749 * @param[in] actor The actor to animate
750 * @param[in] delaySeconds The initial delay from the start of the animation
752 void Show(Actor actor, float delaySeconds);
755 * @brief Hides an actor during the animation.
758 * @param[in] actor The actor to animate
759 * @param[in] delaySeconds The initial delay from the start of the animation
761 void Hide(Actor actor, float delaySeconds);
763 public: // Not intended for use by Application developers
767 * @brief This constructor is used by Animation::New() methods.
769 * @param[in] animation A pointer to a newly allocated Dali resource
771 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
781 #endif // DALI_ANIMATION_H