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.
106 * | %Signal Name | Method |
107 * |--------------|--------------------------|
108 * | finished | @ref FinishedSignal() |
111 * | %Action Name | %Animation method called |
112 * |--------------|--------------------------|
115 * | pause | Pause() |
118 class DALI_IMPORT_API Animation : public BaseHandle
122 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type @SINCE_1_0.0
124 typedef Any AnyFunction; ///< Interpolation function @SINCE_1_0.0
127 * @brief Enumeration for what to do when the animation ends, is stopped, or is destroyed.
132 Bake, ///< When the animation ends, the animated property values are saved. @SINCE_1_0.0
133 Discard, ///< When the animation ends, the animated property values are forgotten. @SINCE_1_0.0
134 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
138 * @brief Enumeration for what interpolation method to use on key-frame animations.
143 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default) @SINCE_1_0.0
144 Cubic ///< Values in between key frames are interpolated using a cubic polynomial. @SINCE_1_0.0
148 * @brief Enumeration for what state the animation is in.
150 * Note: Calling Reset() on this class will NOT reset the animation. It will call BaseHandle::Reset() which drops the object handle.
156 STOPPED, ///< Animation has stopped @SINCE_1_1.21
157 PLAYING, ///< The animation is playing @SINCE_1_1.21
158 PAUSED ///< The animation is paused @SINCE_1_1.21
162 * @brief Creates an uninitialized Animation; this can be initialized with Animation::New().
164 * Calling member functions with an uninitialized Animation handle is not allowed.
170 * @brief Creates an initialized Animation.
172 * The animation will not loop.
173 * The default end action is "Bake".
174 * The default alpha function is linear.
176 * @param[in] durationSeconds The duration in seconds
177 * @return A handle to a newly allocated Dali resource
178 * @note durationSeconds can not be negative.
180 static Animation New(float durationSeconds);
183 * @brief Downcasts a handle to Animation handle.
185 * If handle points to an Animation object, the downcast produces valid handle.
186 * If not, the returned handle is left uninitialized.
189 * @param[in] handle Handle to an object
190 * @return Handle to an Animation object or an uninitialized handle
192 static Animation DownCast( BaseHandle handle );
197 * This is non-virtual since derived Handle types must not contain data or virtual methods.
203 * @brief This copy constructor is required for (smart) pointer semantics.
206 * @param[in] handle A reference to the copied handle
208 Animation(const Animation& handle);
211 * @brief This assignment operator is required for (smart) pointer semantics.
214 * @param[in] rhs A reference to the copied handle
215 * @return A reference to this
217 Animation& operator=(const Animation& rhs);
220 * @brief Sets the duration of an animation.
223 * @param[in] seconds The duration in seconds
224 * @pre DurationSeconds must be greater than zero.
226 void SetDuration(float seconds);
229 * @brief Retrieves the duration of an animation.
232 * @return The duration in seconds
234 float GetDuration() const;
237 * @brief Sets whether the animation will loop.
239 * This function resets the loop count and should not be used with SetLoopCount(int).
240 * Setting this parameter does not cause the animation to Play().
243 * @param[in] looping True if the animation will loop
245 void SetLooping(bool looping);
248 * @brief Enables looping for 'count' repeats.
250 * A zero is the same as SetLooping(true) i.e. repeat forever.
251 * If Play() Stop() or 'count' loops is reached, the loop counter will reset.
252 * Setting this parameter does not cause the animation to Play().
255 * @param[in] count The number of times to loop
257 void SetLoopCount(int count);
260 * @brief Gets the loop count.
262 * A zero is the same as SetLooping(true) ie repeat forever.
263 * The loop count is initially 1 for play once.
266 * @return The number of times to loop
271 * @brief Gets the current loop count.
273 * A value 0 to GetLoopCount() indicating the current loop count when looping.
276 * @return The current number of loops that have occured
278 int GetCurrentLoop();
281 * @brief Queries whether the animation will loop.
284 * @return True if the animation will loop
286 bool IsLooping() const;
289 * @brief Sets the end action of the animation.
291 * This action is performed when the animation ends or if it is stopped.
292 * Default end action is bake.
294 * @param[in] action The end action
296 void SetEndAction(EndAction action);
299 * @brief Returns the end action of the animation.
302 * @return The end action
304 EndAction GetEndAction() const;
307 * @brief Sets the disconnect action.
309 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
310 * Default action is to BakeFinal.
312 * @param[in] disconnectAction The disconnect action
314 void SetDisconnectAction( EndAction disconnectAction );
317 * @brief Returns the disconnect action.
320 * @return The disconnect action
322 EndAction GetDisconnectAction() const;
325 * @brief Sets the default alpha function for an animation.
327 * This is applied to individual property animations, if no further alpha functions are supplied.
329 * @param[in] alpha The default alpha function
331 void SetDefaultAlphaFunction(AlphaFunction alpha);
334 * @brief Retrieves the default alpha function for an animation.
337 * @return The default alpha function
339 AlphaFunction GetDefaultAlphaFunction() const;
342 * @brief Sets the progress of the animation.
344 * The animation will play (or continue playing) from this point. The progress
345 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
346 * otherwise, it will be ignored.
349 * @param[in] progress The new progress as a normalized value between [0,1]
350 * or between the play range if specified
352 void SetCurrentProgress( float progress );
355 * @brief Retrieves the current progress of the animation.
358 * @return The current progress as a normalized value between [0,1]
360 float GetCurrentProgress();
363 * @brief Specifies a speed factor for the animation.
365 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
366 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
367 * to play the animation in reverse.
370 * @param[in] factor A value which will multiply the velocity
372 void SetSpeedFactor( float factor );
375 * @brief Retrieves the speed factor of the animation.
378 * @return Speed factor
380 float GetSpeedFactor() const;
383 * @brief Sets the playing range.
385 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
386 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
389 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
390 * animation will play between those values
392 void SetPlayRange( const Vector2& range );
395 * @brief Gets the playing range.
398 * @return The play range defined for the animation
400 Vector2 GetPlayRange() const;
403 * @brief Play the animation.
409 * @brief Plays the animation from a given point.
411 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
412 * otherwise, it will be ignored.
415 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
417 void PlayFrom( float progress );
420 * @brief Pauses the animation.
426 * @brief Queries the state of the animation.
428 * @return The Animation::State
430 State GetState() const;
433 * @brief Stops the animation.
439 * @brief Clears the animation.
441 * This disconnects any objects that were being animated, effectively stopping the animation.
447 * @brief Connects to this signal to be notified when an Animation's animations have finished.
450 * @return A signal object to connect with
452 AnimationSignalType& FinishedSignal();
455 * @brief Animates a property value by a relative amount.
457 * The default alpha function will be used.
458 * The effect will start & end when the animation begins & ends.
460 * @param[in] target The target object/property to animate
461 * @param[in] relativeValue The property value will change by this amount
463 void AnimateBy(Property target, Property::Value relativeValue);
466 * @brief Animates a property value by a relative amount.
468 * The effect will start & end when the animation begins & ends.
470 * @param[in] target The target object/property to animate
471 * @param[in] relativeValue The property value will change by this amount
472 * @param[in] alpha The alpha function to apply
474 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
477 * @brief Animates a property value by a relative amount.
479 * The default alpha function will be used.
481 * @param[in] target The target object/property to animate
482 * @param[in] relativeValue The property value will increase/decrease by this amount
483 * @param[in] period The effect will occur during this time period
485 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
488 * @brief Animates a property value by a relative amount.
491 * @param[in] target The target object/property to animate
492 * @param[in] relativeValue The property value will increase/decrease by this amount
493 * @param[in] alpha The alpha function to apply
494 * @param[in] period The effect will occur during this time period
496 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
499 * @brief Animates a property to a destination value.
501 * The default alpha function will be used.
502 * The effect will start & end when the animation begins & ends.
504 * @param[in] target The target object/property to animate
505 * @param[in] destinationValue The destination value
507 void AnimateTo(Property target, Property::Value destinationValue);
510 * @brief Animates a property to a destination value.
512 * The effect will start & end when the animation begins & ends.
514 * @param[in] target The target object/property to animate
515 * @param[in] destinationValue The destination value
516 * @param[in] alpha The alpha function to apply
518 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
521 * @brief Animates a property to a destination value.
523 * The default alpha function will be used.
525 * @param[in] target The target object/property to animate
526 * @param[in] destinationValue The destination value
527 * @param[in] period The effect will occur during this time period
529 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
532 * @brief Animates a property to a destination value.
535 * @param[in] target The target object/property to animate
536 * @param[in] destinationValue The destination value
537 * @param[in] alpha The alpha function to apply
538 * @param[in] period The effect will occur during this time period
540 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
543 * @brief Animates a property between keyframes.
546 * @param[in] target The target object property to animate
547 * @param[in] keyFrames The set of time/value pairs between which to animate
549 void AnimateBetween(Property target, KeyFrames& keyFrames);
552 * @brief Animates a property between keyframes.
555 * @param[in] target The target object property to animate
556 * @param[in] keyFrames The set of time/value pairs between which to animate
557 * @param[in] interpolation The method used to interpolate between values
559 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
562 * @brief Animates a property between keyframes.
565 * @param[in] target The target object property to animate
566 * @param[in] keyFrames The set of time/value pairs between which to animate
567 * @param[in] alpha The alpha function to apply
569 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
572 * @brief Animates a property between keyframes.
575 * @param[in] target The target object property to animate
576 * @param[in] keyFrames The set of time/value pairs between which to animate
577 * @param[in] alpha The alpha function to apply
578 * @param[in] interpolation The method used to interpolate between values
580 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
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
588 * @param[in] period The effect will occur during this time period
590 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
593 * @brief Animates a property between keyframes.
596 * @param[in] target The target object property to animate
597 * @param[in] keyFrames The set of time/value pairs between which to animate
598 * @param[in] period The effect will occur duing this time period
599 * @param[in] interpolation The method used to interpolate between values
601 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
604 * @brief Animates a property between keyframes.
607 * @param[in] target The target object property to animate
608 * @param[in] keyFrames The set of time/value pairs between which to animate
609 * @param[in] alpha The alpha function to apply
610 * @param[in] period The effect will occur during this time period
612 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
615 * @brief Animates a property between keyframes.
618 * @param[in] target The target object property to animate
619 * @param[in] keyFrames The set of time/value pairs between which to animate
620 * @param[in] alpha The alpha function to apply to the overall progress
621 * @param[in] period The effect will occur duing this time period
622 * @param[in] interpolation The method used to interpolate between values
624 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
627 // Actor-specific convenience methods
630 * @brief Animates an actor's position and orientation through a predefined path.
632 * The actor will rotate to orient the supplied
633 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
636 * @param[in] actor The actor to animate
637 * @param[in] path The path. It defines position and orientation
638 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
640 void Animate( Actor actor, Path path, const Vector3& forward );
643 * @brief Animates an actor's position and orientation through a predefined path.
645 * The actor will rotate to orient the supplied
646 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
649 * @param[in] actor The actor to animate
650 * @param[in] path The path. It defines position and orientation
651 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
652 * @param[in] alpha The alpha function to apply
654 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
657 * @brief Animates an actor's position and orientation through a predefined path.
659 * The actor will rotate to orient the supplied
660 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
663 * @param[in] actor The actor to animate
664 * @param[in] path The path. It defines position and orientation
665 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
666 * @param[in] period The effect will occur during this time period
668 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
671 * @brief Animates an actor's position and orientation through a predefined path.
673 * The actor will rotate to orient the supplied
674 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
677 * @param[in] actor The actor to animate
678 * @param[in] path The path. It defines position and orientation
679 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
680 * @param[in] alpha The alpha function to apply
681 * @param[in] period The effect will occur during this time period
683 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
686 * @brief Shows an actor during the animation.
689 * @param[in] actor The actor to animate
690 * @param[in] delaySeconds The initial delay from the start of the animation
692 void Show(Actor actor, float delaySeconds);
695 * @brief Hides an actor during the animation.
698 * @param[in] actor The actor to animate
699 * @param[in] delaySeconds The initial delay from the start of the animation
701 void Hide(Actor actor, float delaySeconds);
703 public: // Not intended for use by Application developers
707 * @brief This constructor is used by Animation::New() methods.
709 * @param[in] animation A pointer to a newly allocated Dali resource
711 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
721 #endif // __DALI_ANIMATION_H__