1 #ifndef __DALI_ANIMATION_H__
2 #define __DALI_ANIMATION_H__
5 * Copyright (c) 2016 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.
104 * | %Signal Name | Method |
105 * |--------------|--------------------------|
106 * | finished | @ref FinishedSignal() |
109 * | %Action Name | %Animation method called |
110 * |--------------|--------------------------|
113 * | pause | Pause() |
116 class DALI_IMPORT_API Animation : public BaseHandle
120 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type @SINCE_1_0.0
122 typedef Any AnyFunction; ///< Interpolation function @SINCE_1_0.0
125 * @brief Enumeration for what to do when the animation ends, is stopped, or is destroyed.
130 Bake, ///< When the animation ends, the animated property values are saved. @SINCE_1_0.0
131 Discard, ///< When the animation ends, the animated property values are forgotten. @SINCE_1_0.0
132 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
136 * @brief Enumeration for what interpolation method to use on key-frame animations.
141 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default) @SINCE_1_0.0
142 Cubic ///< Values in between key frames are interpolated using a cubic polynomial. @SINCE_1_0.0
146 * @brief Enumeration for what state the animation is in.
148 * Note: Calling Reset() on this class will NOT reset the animation. It will call BaseHandle::Reset() which drops the object handle.
154 STOPPED, ///< Animation has stopped @SINCE_1_1.21
155 PLAYING, ///< The animation is playing @SINCE_1_1.21
156 PAUSED ///< The animation is paused @SINCE_1_1.21
160 * @brief Creates an uninitialized Animation; this can be initialized with Animation::New().
162 * Calling member functions with an uninitialized Animation handle is not allowed.
168 * @brief Creates an initialized Animation.
170 * The animation will not loop.
171 * The default end action is "Bake".
172 * The default alpha function is linear.
174 * @param[in] durationSeconds The duration in seconds
175 * @return A handle to a newly allocated Dali resource
176 * @note durationSeconds can not be negative.
178 static Animation New(float durationSeconds);
181 * @brief Downcasts a handle to Animation handle.
183 * If handle points to an Animation object, the downcast produces valid handle.
184 * If not, the returned handle is left uninitialized.
187 * @param[in] handle Handle to an object
188 * @return Handle to an Animation object or an uninitialized handle
190 static Animation DownCast( BaseHandle handle );
195 * This is non-virtual since derived Handle types must not contain data or virtual methods.
201 * @brief This copy constructor is required for (smart) pointer semantics.
204 * @param[in] handle A reference to the copied handle
206 Animation(const Animation& handle);
209 * @brief This assignment operator is required for (smart) pointer semantics.
212 * @param[in] rhs A reference to the copied handle
213 * @return A reference to this
215 Animation& operator=(const Animation& rhs);
218 * @brief Sets the duration of an animation.
221 * @param[in] seconds The duration in seconds
222 * @pre DurationSeconds must be greater than zero.
224 void SetDuration(float seconds);
227 * @brief Retrieves the duration of an animation.
230 * @return The duration in seconds
232 float GetDuration() const;
235 * @brief Sets whether the animation will loop.
237 * This function resets the loop count and should not be used with SetLoopCount(int).
238 * Setting this parameter does not cause the animation to Play().
241 * @param[in] looping True if the animation will loop
243 void SetLooping(bool looping);
246 * @brief Enables looping for 'count' repeats.
248 * A zero is the same as SetLooping(true) i.e. repeat forever.
249 * If Play() Stop() or 'count' loops is reached, the loop counter will reset.
250 * Setting this parameter does not cause the animation to Play().
253 * @param[in] count The number of times to loop
255 void SetLoopCount(int count);
258 * @brief Gets the loop count.
260 * A zero is the same as SetLooping(true) ie repeat forever.
261 * The loop count is initially 1 for play once.
264 * @return The number of times to loop
269 * @brief Gets the current loop count.
271 * A value 0 to GetLoopCount() indicating the current loop count when looping.
274 * @return The current number of loops that have occured
276 int GetCurrentLoop();
279 * @brief Queries whether the animation will loop.
282 * @return True if the animation will loop
284 bool IsLooping() const;
287 * @brief Sets the end action of the animation.
289 * This action is performed when the animation ends or if it is stopped.
290 * Default end action is bake.
292 * @param[in] action The end action
294 void SetEndAction(EndAction action);
297 * @brief Returns the end action of the animation.
300 * @return The end action
302 EndAction GetEndAction() const;
305 * @brief Sets the disconnect action.
307 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
308 * Default action is to BakeFinal.
310 * @param[in] disconnectAction The disconnect action
312 void SetDisconnectAction( EndAction disconnectAction );
315 * @brief Returns the disconnect action.
318 * @return The disconnect action
320 EndAction GetDisconnectAction() const;
323 * @brief Sets the default alpha function for an animation.
325 * This is applied to individual property animations, if no further alpha functions are supplied.
327 * @param[in] alpha The default alpha function
329 void SetDefaultAlphaFunction(AlphaFunction alpha);
332 * @brief Retrieves the default alpha function for an animation.
335 * @return The default alpha function
337 AlphaFunction GetDefaultAlphaFunction() const;
340 * @brief Sets the progress of the animation.
342 * The animation will play (or continue playing) from this point. The progress
343 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
344 * otherwise, it will be ignored.
347 * @param[in] progress The new progress as a normalized value between [0,1]
348 * or between the play range if specified
350 void SetCurrentProgress( float progress );
353 * @brief Retrieves the current progress of the animation.
356 * @return The current progress as a normalized value between [0,1]
358 float GetCurrentProgress();
361 * @brief Specifies a speed factor for the animation.
363 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
364 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
365 * to play the animation in reverse.
368 * @param[in] factor A value which will multiply the velocity
370 void SetSpeedFactor( float factor );
373 * @brief Retrieves the speed factor of the animation.
376 * @return Speed factor
378 float GetSpeedFactor() const;
381 * @brief Sets the playing range.
383 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
384 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
387 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
388 * animation will play between those values
390 void SetPlayRange( const Vector2& range );
393 * @brief Gets the playing range.
396 * @return The play range defined for the animation
398 Vector2 GetPlayRange() const;
401 * @brief Play the animation.
407 * @brief Plays the animation from a given point.
409 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
410 * otherwise, it will be ignored.
413 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
415 void PlayFrom( float progress );
418 * @brief Pauses the animation.
424 * @brief Queries the state of the animation.
426 * @return The Animation::State
428 State GetState() const;
431 * @brief Stops the animation.
437 * @brief Clears the animation.
439 * This disconnects any objects that were being animated, effectively stopping the animation.
445 * @brief Connects to this signal to be notified when an Animation's animations have finished.
448 * @return A signal object to connect with
450 AnimationSignalType& FinishedSignal();
453 * @brief Animates a property value by a relative amount.
455 * The default alpha function will be used.
456 * The effect will start & end when the animation begins & ends.
458 * @param[in] target The target object/property to animate
459 * @param[in] relativeValue The property value will change by this amount
461 void AnimateBy(Property target, Property::Value relativeValue);
464 * @brief Animates a property value by a relative amount.
466 * The effect will start & end when the animation begins & ends.
468 * @param[in] target The target object/property to animate
469 * @param[in] relativeValue The property value will change by this amount
470 * @param[in] alpha The alpha function to apply
472 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
475 * @brief Animates a property value by a relative amount.
477 * The default alpha function will be used.
479 * @param[in] target The target object/property to animate
480 * @param[in] relativeValue The property value will increase/decrease by this amount
481 * @param[in] period The effect will occur during this time period
483 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
486 * @brief Animates a property value by a relative amount.
489 * @param[in] target The target object/property to animate
490 * @param[in] relativeValue The property value will increase/decrease by this amount
491 * @param[in] alpha The alpha function to apply
492 * @param[in] period The effect will occur during this time period
494 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
497 * @brief Animates a property to a destination value.
499 * The default alpha function will be used.
500 * The effect will start & end when the animation begins & ends.
502 * @param[in] target The target object/property to animate
503 * @param[in] destinationValue The destination value
505 void AnimateTo(Property target, Property::Value destinationValue);
508 * @brief Animates a property to a destination value.
510 * The effect will start & end when the animation begins & ends.
512 * @param[in] target The target object/property to animate
513 * @param[in] destinationValue The destination value
514 * @param[in] alpha The alpha function to apply
516 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
519 * @brief Animates a property to a destination value.
521 * The default alpha function will be used.
523 * @param[in] target The target object/property to animate
524 * @param[in] destinationValue The destination value
525 * @param[in] period The effect will occur during this time period
527 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
530 * @brief Animates a property to a destination value.
533 * @param[in] target The target object/property to animate
534 * @param[in] destinationValue The destination value
535 * @param[in] alpha The alpha function to apply
536 * @param[in] period The effect will occur during this time period
538 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
541 * @brief Animates a property between keyframes.
544 * @param[in] target The target object property to animate
545 * @param[in] keyFrames The set of time/value pairs between which to animate
547 void AnimateBetween(Property target, KeyFrames& keyFrames);
550 * @brief Animates a property between keyframes.
553 * @param[in] target The target object property to animate
554 * @param[in] keyFrames The set of time/value pairs between which to animate
555 * @param[in] interpolation The method used to interpolate between values
557 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
560 * @brief Animates a property between keyframes.
563 * @param[in] target The target object property to animate
564 * @param[in] keyFrames The set of time/value pairs between which to animate
565 * @param[in] alpha The alpha function to apply
567 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
570 * @brief Animates a property between keyframes.
573 * @param[in] target The target object property to animate
574 * @param[in] keyFrames The set of time/value pairs between which to animate
575 * @param[in] alpha The alpha function to apply
576 * @param[in] interpolation The method used to interpolate between values
578 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
581 * @brief Animates a property between keyframes.
584 * @param[in] target The target object property to animate
585 * @param[in] keyFrames The set of time/value pairs between which to animate
586 * @param[in] period The effect will occur during this time period
588 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
591 * @brief Animates a property between keyframes.
594 * @param[in] target The target object property to animate
595 * @param[in] keyFrames The set of time/value pairs between which to animate
596 * @param[in] period The effect will occur duing this time period
597 * @param[in] interpolation The method used to interpolate between values
599 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, 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
608 * @param[in] period The effect will occur during this time period
610 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
613 * @brief Animates a property between keyframes.
616 * @param[in] target The target object property to animate
617 * @param[in] keyFrames The set of time/value pairs between which to animate
618 * @param[in] alpha The alpha function to apply to the overall progress
619 * @param[in] period The effect will occur duing this time period
620 * @param[in] interpolation The method used to interpolate between values
622 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
625 // Actor-specific convenience methods
628 * @brief Animates an actor's position and orientation through a predefined path.
630 * The actor will rotate to orient the supplied
631 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
634 * @param[in] actor The actor to animate
635 * @param[in] path The path. It defines position and orientation
636 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
638 void Animate( Actor actor, Path path, const Vector3& forward );
641 * @brief Animates an actor's position and orientation through a predefined path.
643 * The actor will rotate to orient the supplied
644 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
647 * @param[in] actor The actor to animate
648 * @param[in] path The path. It defines position and orientation
649 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
650 * @param[in] alpha The alpha function to apply
652 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
655 * @brief Animates an actor's position and orientation through a predefined path.
657 * The actor will rotate to orient the supplied
658 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
661 * @param[in] actor The actor to animate
662 * @param[in] path The path. It defines position and orientation
663 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
664 * @param[in] period The effect will occur during this time period
666 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
669 * @brief Animates an actor's position and orientation through a predefined path.
671 * The actor will rotate to orient the supplied
672 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
675 * @param[in] actor The actor to animate
676 * @param[in] path The path. It defines position and orientation
677 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
678 * @param[in] alpha The alpha function to apply
679 * @param[in] period The effect will occur during this time period
681 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
684 * @brief Shows an actor during the animation.
687 * @param[in] actor The actor to animate
688 * @param[in] delaySeconds The initial delay from the start of the animation
690 void Show(Actor actor, float delaySeconds);
693 * @brief Hides an actor during the animation.
696 * @param[in] actor The actor to animate
697 * @param[in] delaySeconds The initial delay from the start of the animation
699 void Hide(Actor actor, float delaySeconds);
701 public: // Not intended for use by Application developers
705 * @brief This constructor is used by Animation::New() methods.
707 * @param[in] animation A pointer to a newly allocated Dali resource
709 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
719 #endif // __DALI_ANIMATION_H__