1 #ifndef DALI_ANIMATION_H
2 #define DALI_ANIMATION_H
5 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/animation/alpha-function.h>
23 #include <dali/public-api/animation/key-frames.h>
24 #include <dali/public-api/animation/path.h>
25 #include <dali/public-api/animation/time-period.h>
26 #include <dali/public-api/object/any.h>
27 #include <dali/public-api/object/handle.h>
28 #include <dali/public-api/object/property.h>
29 #include <dali/public-api/signals/dali-signal.h>
34 * @addtogroup dali_core_animation
43 namespace Internal DALI_INTERNAL
49 * @brief Dali::Animation can be used to animate the properties of any number of objects, typically Actors.
51 * An example animation setup is shown below:
57 * Actor mActor; // The object we wish to animate
58 * Animation mAnimation; // Keep this to control the animation
61 * // ...To play the animation
63 * mAnimation = Animation::New(3.0f); // duration 3 seconds
64 * mAnimation.AnimateTo(Property(mActor, Actor::Property::POSITION), Vector3(10.0f, 50.0f, 0.0f));
69 * Dali::Animation supports "fire and forget" behaviour i.e. an animation continues to play if the handle is discarded.
70 * Note that in the following example, the "Finish" signal will be emitted:
74 * void ExampleCallback( Animation& source )
76 * std::cout << "Animation has finished" << std::endl;
79 * void ExampleAnimation( Actor actor )
81 * Animation animation = Animation::New(2.0f); // duration 2 seconds
82 * animation.AnimateTo(Property(actor, Actor::Property::POSITION), 10.0f, 50.0f, 0.0f);
83 * animation.FinishedSignal().Connect( ExampleCallback );
85 * } // At this point the animation handle has gone out of scope
87 * Actor actor = Actor::New();
88 * Stage::GetCurrent().Add( actor );
90 * // Fire animation and forget about it
91 * ExampleAnimation( actor );
93 * // However the animation will continue, and "Animation has finished" will be printed after 2 seconds.
97 * If the "Finish" signal is connected to a member function of an object, it must be disconnected before the object is destroyed.
98 * This is typically done in the object destructor, and requires either the Dali::Connection object or Dali::Animation handle to be stored.
100 * The overall animation time is superseded by the values given in the TimePeriod structure used when calling the AnimateTo(), AnimateBy(), AnimateBetween() and Animate() methods.
101 * If any of the individual calls to those functions exceeds the overall animation time, then the overall animation time is automatically extended.
103 * Using AnimateTo and AnimateBy for the same property of the same Actor will yield undefined behaviour especially if the TimePeriod overlaps.
105 * After calling Animation::Play(), Handle::GetProperty will return the target value of the animated property.
108 * | %Signal Name | Method |
109 * |--------------|--------------------------|
110 * | finished | @ref FinishedSignal() |
113 * | %Action Name | %Animation method called |
114 * |--------------|--------------------------|
117 * | pause | Pause() |
120 class DALI_IMPORT_API Animation : public BaseHandle
124 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type @SINCE_1_0.0
126 typedef Any AnyFunction; ///< Interpolation function @SINCE_1_0.0
129 * @brief Enumeration for what to do when the animation ends, is stopped, or is destroyed.
134 Bake, ///< When the animation ends, the animated property values are saved. @SINCE_1_0.0
135 Discard, ///< When the animation ends, the animated property values are forgotten. @SINCE_1_0.0
136 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
140 * @brief Enumeration for what interpolation method to use on key-frame animations.
145 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default) @SINCE_1_0.0
146 Cubic ///< Values in between key frames are interpolated using a cubic polynomial. @SINCE_1_0.0
150 * @brief Enumeration for what state the animation is in.
152 * Note: Calling Reset() on this class will NOT reset the animation. It will call BaseHandle::Reset() which drops the object handle.
158 STOPPED, ///< Animation has stopped @SINCE_1_1.21
159 PLAYING, ///< The animation is playing @SINCE_1_1.21
160 PAUSED ///< The animation is paused @SINCE_1_1.21
164 * @brief Enumeration for what looping mode is in.
170 RESTART, ///< When the animation arrives at the end in looping mode, the animation restarts from the beginning. @SINCE_1_2.60
171 AUTO_REVERSE ///< When the animation arrives at the end in looping mode, the animation reverses direction and runs backwards again. @SINCE_1_2.60
175 * @brief Creates an uninitialized Animation; this can be initialized with Animation::New().
177 * Calling member functions with an uninitialized Animation handle is not allowed.
183 * @brief Creates an initialized Animation.
185 * The animation will not loop.
186 * The default end action is "Bake".
187 * The default alpha function is linear.
189 * @param[in] durationSeconds The duration in seconds
190 * @return A handle to a newly allocated Dali resource
191 * @note durationSeconds can not be negative.
193 static Animation New(float durationSeconds);
196 * @brief Downcasts a handle to Animation handle.
198 * If handle points to an Animation object, the downcast produces valid handle.
199 * If not, the returned handle is left uninitialized.
202 * @param[in] handle Handle to an object
203 * @return Handle to an Animation object or an uninitialized handle
205 static Animation DownCast( BaseHandle handle );
210 * This is non-virtual since derived Handle types must not contain data or virtual methods.
216 * @brief This copy constructor is required for (smart) pointer semantics.
219 * @param[in] handle A reference to the copied handle
221 Animation(const Animation& handle);
224 * @brief This assignment operator is required for (smart) pointer semantics.
227 * @param[in] rhs A reference to the copied handle
228 * @return A reference to this
230 Animation& operator=(const Animation& rhs);
233 * @brief Sets the duration of an animation.
236 * @param[in] seconds The duration in seconds
237 * @pre DurationSeconds must be greater than zero.
239 void SetDuration(float seconds);
242 * @brief Retrieves the duration of an animation.
245 * @return The duration in seconds
247 float GetDuration() const;
250 * @brief Sets whether the animation will loop.
252 * This function resets the loop count and should not be used with SetLoopCount(int).
253 * Setting this parameter does not cause the animation to Play().
256 * @param[in] looping True if the animation will loop
258 void SetLooping(bool looping);
261 * @brief Enables looping for 'count' repeats.
263 * A zero is the same as SetLooping(true) i.e. repeat forever.
264 * If Play() Stop() or 'count' loops is reached, the loop counter will reset.
265 * Setting this parameter does not cause the animation to Play().
268 * @param[in] count The number of times to loop
270 void SetLoopCount(int count);
273 * @brief Gets the loop count.
275 * A zero is the same as SetLooping(true) ie repeat forever.
276 * The loop count is initially 1 for play once.
279 * @return The number of times to loop
284 * @brief Gets the current loop count.
286 * A value 0 to GetLoopCount() indicating the current loop count when looping.
289 * @return The current number of loops that have occured
291 int GetCurrentLoop();
294 * @brief Queries whether the animation will loop.
297 * @return True if the animation will loop
299 bool IsLooping() const;
302 * @brief Sets the end action of the animation.
304 * This action is performed when the animation ends or if it is stopped.
305 * Default end action is bake.
307 * @param[in] action The end action
309 void SetEndAction(EndAction action);
312 * @brief Returns the end action of the animation.
315 * @return The end action
317 EndAction GetEndAction() const;
320 * @brief Sets the disconnect action.
322 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
323 * Default action is to BakeFinal.
325 * @param[in] disconnectAction The disconnect action
327 void SetDisconnectAction( EndAction disconnectAction );
330 * @brief Returns the disconnect action.
333 * @return The disconnect action
335 EndAction GetDisconnectAction() const;
338 * @brief Sets the default alpha function for an animation.
340 * This is applied to individual property animations, if no further alpha functions are supplied.
342 * @param[in] alpha The default alpha function
344 void SetDefaultAlphaFunction(AlphaFunction alpha);
347 * @brief Retrieves the default alpha function for an animation.
350 * @return The default alpha function
352 AlphaFunction GetDefaultAlphaFunction() const;
355 * @brief Sets the progress of the animation.
357 * The animation will play (or continue playing) from this point. The progress
358 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
359 * otherwise, it will be ignored.
362 * @param[in] progress The new progress as a normalized value between [0,1]
363 * or between the play range if specified
365 void SetCurrentProgress( float progress );
368 * @brief Retrieves the current progress of the animation.
371 * @return The current progress as a normalized value between [0,1]
373 float GetCurrentProgress();
376 * @brief Specifies a speed factor for the animation.
378 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
379 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
380 * to play the animation in reverse.
383 * @param[in] factor A value which will multiply the velocity
385 void SetSpeedFactor( float factor );
388 * @brief Retrieves the speed factor of the animation.
391 * @return Speed factor
393 float GetSpeedFactor() const;
396 * @brief Sets the playing range.
398 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
399 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
402 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
403 * animation will play between those values
405 void SetPlayRange( const Vector2& range );
408 * @brief Gets the playing range.
411 * @return The play range defined for the animation
413 Vector2 GetPlayRange() const;
416 * @brief Play the animation.
422 * @brief Plays the animation from a given point.
424 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
425 * otherwise, it will be ignored.
428 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
430 void PlayFrom( float progress );
433 * @brief Play the animation after a given delay time.
435 * The delay time is not included in the looping time.
436 * When the delay time is negative value, it would treat as play immediately.
438 * @param[in] delaySeconds The delay time
440 void PlayAfter( float delaySeconds );
443 * @brief Pauses the animation.
449 * @brief Queries the state of the animation.
451 * @return The Animation::State
453 State GetState() const;
456 * @brief Stops the animation.
462 * @brief Clears the animation.
464 * This disconnects any objects that were being animated, effectively stopping the animation.
470 * @brief Sets the looping mode.
472 * Animation plays forwards and then restarts from the beginning or runs backwards again.
474 * @param[in] loopingMode The looping mode is one of RESTART and AUTO_REVERSE
476 void SetLoopingMode( LoopingMode loopingMode );
479 * @brief Gets one of the current looping mode.
482 * @return The current looping mode
484 LoopingMode GetLoopingMode() const;
487 * @brief Connects to this signal to be notified when an Animation's animations have finished.
490 * @return A signal object to connect with
492 AnimationSignalType& FinishedSignal();
495 * @brief Animates a property value by a relative amount.
497 * The default alpha function will be used.
498 * The effect will start & end when the animation begins & ends.
500 * @param[in] target The target object/property to animate
501 * @param[in] relativeValue The property value will change by this amount
503 void AnimateBy(Property target, Property::Value relativeValue);
506 * @brief Animates a property value by a relative amount.
508 * The effect will start & end when the animation begins & ends.
510 * @param[in] target The target object/property to animate
511 * @param[in] relativeValue The property value will change by this amount
512 * @param[in] alpha The alpha function to apply
514 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
517 * @brief Animates a property value by a relative amount.
519 * The default alpha function will be used.
521 * @param[in] target The target object/property to animate
522 * @param[in] relativeValue The property value will increase/decrease by this amount
523 * @param[in] period The effect will occur during this time period
525 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
528 * @brief Animates a property value by a relative amount.
531 * @param[in] target The target object/property to animate
532 * @param[in] relativeValue The property value will increase/decrease by this amount
533 * @param[in] alpha The alpha function to apply
534 * @param[in] period The effect will occur during this time period
536 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
539 * @brief Animates a property to a destination value.
541 * The default alpha function will be used.
542 * The effect will start & end when the animation begins & ends.
544 * @param[in] target The target object/property to animate
545 * @param[in] destinationValue The destination value
547 void AnimateTo(Property target, Property::Value destinationValue);
550 * @brief Animates a property to a destination value.
552 * The effect will start & end when the animation begins & ends.
554 * @param[in] target The target object/property to animate
555 * @param[in] destinationValue The destination value
556 * @param[in] alpha The alpha function to apply
558 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
561 * @brief Animates a property to a destination value.
563 * The default alpha function will be used.
565 * @param[in] target The target object/property to animate
566 * @param[in] destinationValue The destination value
567 * @param[in] period The effect will occur during this time period
569 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
572 * @brief Animates a property to a destination value.
575 * @param[in] target The target object/property to animate
576 * @param[in] destinationValue The destination value
577 * @param[in] alpha The alpha function to apply
578 * @param[in] period The effect will occur during this time period
580 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
583 * @brief Animates a property between keyframes.
586 * @param[in] target The target object property to animate
587 * @param[in] keyFrames The set of time/value pairs between which to animate
589 void AnimateBetween(Property target, KeyFrames& keyFrames);
592 * @brief Animates a property between keyframes.
595 * @param[in] target The target object property to animate
596 * @param[in] keyFrames The set of time/value pairs between which to animate
597 * @param[in] interpolation The method used to interpolate between values
599 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
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
607 * @param[in] alpha The alpha function to apply
609 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
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] alpha The alpha function to apply
618 * @param[in] interpolation The method used to interpolate between values
620 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
623 * @brief Animates a property between keyframes.
626 * @param[in] target The target object property to animate
627 * @param[in] keyFrames The set of time/value pairs between which to animate
628 * @param[in] period The effect will occur during this time period
630 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
633 * @brief Animates a property between keyframes.
636 * @param[in] target The target object property to animate
637 * @param[in] keyFrames The set of time/value pairs between which to animate
638 * @param[in] period The effect will occur duing this time period
639 * @param[in] interpolation The method used to interpolate between values
641 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
644 * @brief Animates a property between keyframes.
647 * @param[in] target The target object property to animate
648 * @param[in] keyFrames The set of time/value pairs between which to animate
649 * @param[in] alpha The alpha function to apply
650 * @param[in] period The effect will occur during this time period
652 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
655 * @brief Animates a property between keyframes.
658 * @param[in] target The target object property to animate
659 * @param[in] keyFrames The set of time/value pairs between which to animate
660 * @param[in] alpha The alpha function to apply to the overall progress
661 * @param[in] period The effect will occur duing this time period
662 * @param[in] interpolation The method used to interpolate between values
664 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
667 // Actor-specific convenience methods
670 * @brief Animates an actor's position and orientation through a predefined path.
672 * The actor will rotate to orient the supplied
673 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
676 * @param[in] actor The actor to animate
677 * @param[in] path The path. It defines position and orientation
678 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
680 void Animate( Actor actor, Path path, const Vector3& forward );
683 * @brief Animates an actor's position and orientation through a predefined path.
685 * The actor will rotate to orient the supplied
686 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
689 * @param[in] actor The actor to animate
690 * @param[in] path The path. It defines position and orientation
691 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
692 * @param[in] alpha The alpha function to apply
694 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
697 * @brief Animates an actor's position and orientation through a predefined path.
699 * The actor will rotate to orient the supplied
700 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
703 * @param[in] actor The actor to animate
704 * @param[in] path The path. It defines position and orientation
705 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
706 * @param[in] period The effect will occur during this time period
708 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
711 * @brief Animates an actor's position and orientation through a predefined path.
713 * The actor will rotate to orient the supplied
714 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
717 * @param[in] actor The actor to animate
718 * @param[in] path The path. It defines position and orientation
719 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
720 * @param[in] alpha The alpha function to apply
721 * @param[in] period The effect will occur during this time period
723 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
726 * @brief Shows an actor during the animation.
729 * @param[in] actor The actor to animate
730 * @param[in] delaySeconds The initial delay from the start of the animation
732 void Show(Actor actor, float delaySeconds);
735 * @brief Hides an actor during the animation.
738 * @param[in] actor The actor to animate
739 * @param[in] delaySeconds The initial delay from the start of the animation
741 void Hide(Actor actor, float delaySeconds);
743 public: // Not intended for use by Application developers
747 * @brief This constructor is used by Animation::New() methods.
749 * @param[in] animation A pointer to a newly allocated Dali resource
751 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
761 #endif // DALI_ANIMATION_H