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 Creates an uninitialized Animation; this can be initialized with Animation::New().
166 * Calling member functions with an uninitialized Animation handle is not allowed.
172 * @brief Creates an initialized Animation.
174 * The animation will not loop.
175 * The default end action is "Bake".
176 * The default alpha function is linear.
178 * @param[in] durationSeconds The duration in seconds
179 * @return A handle to a newly allocated Dali resource
180 * @note durationSeconds can not be negative.
182 static Animation New(float durationSeconds);
185 * @brief Downcasts a handle to Animation handle.
187 * If handle points to an Animation object, the downcast produces valid handle.
188 * If not, the returned handle is left uninitialized.
191 * @param[in] handle Handle to an object
192 * @return Handle to an Animation object or an uninitialized handle
194 static Animation DownCast( BaseHandle handle );
199 * This is non-virtual since derived Handle types must not contain data or virtual methods.
205 * @brief This copy constructor is required for (smart) pointer semantics.
208 * @param[in] handle A reference to the copied handle
210 Animation(const Animation& handle);
213 * @brief This assignment operator is required for (smart) pointer semantics.
216 * @param[in] rhs A reference to the copied handle
217 * @return A reference to this
219 Animation& operator=(const Animation& rhs);
222 * @brief Sets the duration of an animation.
225 * @param[in] seconds The duration in seconds
226 * @pre DurationSeconds must be greater than zero.
228 void SetDuration(float seconds);
231 * @brief Retrieves the duration of an animation.
234 * @return The duration in seconds
236 float GetDuration() const;
239 * @brief Sets whether the animation will loop.
241 * This function resets the loop count and should not be used with SetLoopCount(int).
242 * Setting this parameter does not cause the animation to Play().
245 * @param[in] looping True if the animation will loop
247 void SetLooping(bool looping);
250 * @brief Enables looping for 'count' repeats.
252 * A zero is the same as SetLooping(true) i.e. repeat forever.
253 * If Play() Stop() or 'count' loops is reached, the loop counter will reset.
254 * Setting this parameter does not cause the animation to Play().
257 * @param[in] count The number of times to loop
259 void SetLoopCount(int count);
262 * @brief Gets the loop count.
264 * A zero is the same as SetLooping(true) ie repeat forever.
265 * The loop count is initially 1 for play once.
268 * @return The number of times to loop
273 * @brief Gets the current loop count.
275 * A value 0 to GetLoopCount() indicating the current loop count when looping.
278 * @return The current number of loops that have occured
280 int GetCurrentLoop();
283 * @brief Queries whether the animation will loop.
286 * @return True if the animation will loop
288 bool IsLooping() const;
291 * @brief Sets the end action of the animation.
293 * This action is performed when the animation ends or if it is stopped.
294 * Default end action is bake.
296 * @param[in] action The end action
298 void SetEndAction(EndAction action);
301 * @brief Returns the end action of the animation.
304 * @return The end action
306 EndAction GetEndAction() const;
309 * @brief Sets the disconnect action.
311 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
312 * Default action is to BakeFinal.
314 * @param[in] disconnectAction The disconnect action
316 void SetDisconnectAction( EndAction disconnectAction );
319 * @brief Returns the disconnect action.
322 * @return The disconnect action
324 EndAction GetDisconnectAction() const;
327 * @brief Sets the default alpha function for an animation.
329 * This is applied to individual property animations, if no further alpha functions are supplied.
331 * @param[in] alpha The default alpha function
333 void SetDefaultAlphaFunction(AlphaFunction alpha);
336 * @brief Retrieves the default alpha function for an animation.
339 * @return The default alpha function
341 AlphaFunction GetDefaultAlphaFunction() const;
344 * @brief Sets the progress of the animation.
346 * The animation will play (or continue playing) from this point. The progress
347 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
348 * otherwise, it will be ignored.
351 * @param[in] progress The new progress as a normalized value between [0,1]
352 * or between the play range if specified
354 void SetCurrentProgress( float progress );
357 * @brief Retrieves the current progress of the animation.
360 * @return The current progress as a normalized value between [0,1]
362 float GetCurrentProgress();
365 * @brief Specifies a speed factor for the animation.
367 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
368 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
369 * to play the animation in reverse.
372 * @param[in] factor A value which will multiply the velocity
374 void SetSpeedFactor( float factor );
377 * @brief Retrieves the speed factor of the animation.
380 * @return Speed factor
382 float GetSpeedFactor() const;
385 * @brief Sets the playing range.
387 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
388 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
391 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
392 * animation will play between those values
394 void SetPlayRange( const Vector2& range );
397 * @brief Gets the playing range.
400 * @return The play range defined for the animation
402 Vector2 GetPlayRange() const;
405 * @brief Play the animation.
411 * @brief Plays the animation from a given point.
413 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
414 * otherwise, it will be ignored.
417 * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
419 void PlayFrom( float progress );
422 * @brief Pauses the animation.
428 * @brief Queries the state of the animation.
430 * @return The Animation::State
432 State GetState() const;
435 * @brief Stops the animation.
441 * @brief Clears the animation.
443 * This disconnects any objects that were being animated, effectively stopping the animation.
449 * @brief Connects to this signal to be notified when an Animation's animations have finished.
452 * @return A signal object to connect with
454 AnimationSignalType& FinishedSignal();
457 * @brief Animates a property value by a relative amount.
459 * The default alpha function will be used.
460 * The effect will start & end when the animation begins & ends.
462 * @param[in] target The target object/property to animate
463 * @param[in] relativeValue The property value will change by this amount
465 void AnimateBy(Property target, Property::Value relativeValue);
468 * @brief Animates a property value by a relative amount.
470 * The effect will start & end when the animation begins & ends.
472 * @param[in] target The target object/property to animate
473 * @param[in] relativeValue The property value will change by this amount
474 * @param[in] alpha The alpha function to apply
476 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
479 * @brief Animates a property value by a relative amount.
481 * The default alpha function will be used.
483 * @param[in] target The target object/property to animate
484 * @param[in] relativeValue The property value will increase/decrease by this amount
485 * @param[in] period The effect will occur during this time period
487 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
490 * @brief Animates a property value by a relative amount.
493 * @param[in] target The target object/property to animate
494 * @param[in] relativeValue The property value will increase/decrease by this amount
495 * @param[in] alpha The alpha function to apply
496 * @param[in] period The effect will occur during this time period
498 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
501 * @brief Animates a property to a destination value.
503 * The default alpha function will be used.
504 * The effect will start & end when the animation begins & ends.
506 * @param[in] target The target object/property to animate
507 * @param[in] destinationValue The destination value
509 void AnimateTo(Property target, Property::Value destinationValue);
512 * @brief Animates a property to a destination value.
514 * The effect will start & end when the animation begins & ends.
516 * @param[in] target The target object/property to animate
517 * @param[in] destinationValue The destination value
518 * @param[in] alpha The alpha function to apply
520 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
523 * @brief Animates a property to a destination value.
525 * The default alpha function will be used.
527 * @param[in] target The target object/property to animate
528 * @param[in] destinationValue The destination value
529 * @param[in] period The effect will occur during this time period
531 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
534 * @brief Animates a property to a destination value.
537 * @param[in] target The target object/property to animate
538 * @param[in] destinationValue The destination value
539 * @param[in] alpha The alpha function to apply
540 * @param[in] period The effect will occur during this time period
542 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
545 * @brief Animates a property between keyframes.
548 * @param[in] target The target object property to animate
549 * @param[in] keyFrames The set of time/value pairs between which to animate
551 void AnimateBetween(Property target, KeyFrames& keyFrames);
554 * @brief Animates a property between keyframes.
557 * @param[in] target The target object property to animate
558 * @param[in] keyFrames The set of time/value pairs between which to animate
559 * @param[in] interpolation The method used to interpolate between values
561 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
564 * @brief Animates a property between keyframes.
567 * @param[in] target The target object property to animate
568 * @param[in] keyFrames The set of time/value pairs between which to animate
569 * @param[in] alpha The alpha function to apply
571 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
574 * @brief Animates a property between keyframes.
577 * @param[in] target The target object property to animate
578 * @param[in] keyFrames The set of time/value pairs between which to animate
579 * @param[in] alpha The alpha function to apply
580 * @param[in] interpolation The method used to interpolate between values
582 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
585 * @brief Animates a property between keyframes.
588 * @param[in] target The target object property to animate
589 * @param[in] keyFrames The set of time/value pairs between which to animate
590 * @param[in] period The effect will occur during this time period
592 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
595 * @brief Animates a property between keyframes.
598 * @param[in] target The target object property to animate
599 * @param[in] keyFrames The set of time/value pairs between which to animate
600 * @param[in] period The effect will occur duing this time period
601 * @param[in] interpolation The method used to interpolate between values
603 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
606 * @brief Animates a property between keyframes.
609 * @param[in] target The target object property to animate
610 * @param[in] keyFrames The set of time/value pairs between which to animate
611 * @param[in] alpha The alpha function to apply
612 * @param[in] period The effect will occur during this time period
614 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
617 * @brief Animates a property between keyframes.
620 * @param[in] target The target object property to animate
621 * @param[in] keyFrames The set of time/value pairs between which to animate
622 * @param[in] alpha The alpha function to apply to the overall progress
623 * @param[in] period The effect will occur duing this time period
624 * @param[in] interpolation The method used to interpolate between values
626 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
629 // Actor-specific convenience methods
632 * @brief Animates an actor's position and orientation through a predefined path.
634 * The actor will rotate to orient the supplied
635 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
638 * @param[in] actor The actor to animate
639 * @param[in] path The path. It defines position and orientation
640 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
642 void Animate( Actor actor, Path path, const Vector3& forward );
645 * @brief Animates an actor's position and orientation through a predefined path.
647 * The actor will rotate to orient the supplied
648 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
651 * @param[in] actor The actor to animate
652 * @param[in] path The path. It defines position and orientation
653 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
654 * @param[in] alpha The alpha function to apply
656 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
659 * @brief Animates an actor's position and orientation through a predefined path.
661 * The actor will rotate to orient the supplied
662 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
665 * @param[in] actor The actor to animate
666 * @param[in] path The path. It defines position and orientation
667 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
668 * @param[in] period The effect will occur during this time period
670 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
673 * @brief Animates an actor's position and orientation through a predefined path.
675 * The actor will rotate to orient the supplied
676 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
679 * @param[in] actor The actor to animate
680 * @param[in] path The path. It defines position and orientation
681 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
682 * @param[in] alpha The alpha function to apply
683 * @param[in] period The effect will occur during this time period
685 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
688 * @brief Shows an actor during the animation.
691 * @param[in] actor The actor to animate
692 * @param[in] delaySeconds The initial delay from the start of the animation
694 void Show(Actor actor, float delaySeconds);
697 * @brief Hides an actor during the animation.
700 * @param[in] actor The actor to animate
701 * @param[in] delaySeconds The initial delay from the start of the animation
703 void Hide(Actor actor, float delaySeconds);
705 public: // Not intended for use by Application developers
709 * @brief This constructor is used by Animation::New() methods.
711 * @param[in] animation A pointer to a newly allocated Dali resource
713 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
723 #endif // __DALI_ANIMATION_H__