1 #ifndef __DALI_ANIMATION_H__
2 #define __DALI_ANIMATION_H__
5 * Copyright (c) 2015 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.
101 * | %Signal Name | Method |
102 * |--------------|--------------------------|
103 * | finished | @ref FinishedSignal() |
106 * | %Action Name | %Animation method called |
107 * |--------------|--------------------------|
110 * | pause | Pause() |
113 class DALI_IMPORT_API Animation : public BaseHandle
117 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type @SINCE_1_0.0
119 typedef Any AnyFunction; ///< Interpolation function @SINCE_1_0.0
122 * @brief What to do when the animation ends, is stopped or is destroyed
127 Bake, ///< When the animation ends, the animated property values are saved. @SINCE_1_0.0
128 Discard, ///< When the animation ends, the animated property values are forgotten. @SINCE_1_0.0
129 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
133 * @brief What interpolation method to use on key-frame animations
138 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default) @SINCE_1_0.0
139 Cubic ///< Values in between key frames are interpolated using a cubic polynomial. @SINCE_1_0.0
143 * @brief What state the animation is in
145 * Note: Calling Reset() on this class will NOT reset the animation. It will call BaseHandle::Reset() which drops the object handle.
151 STOPPED, ///< Animation has stopped @SINCE_1_1.21
152 PLAYING, ///< The animation is playing @SINCE_1_1.21
153 PAUSED ///< The animation is paused @SINCE_1_1.21
157 * @brief Create an uninitialized Animation; this can be initialized with Animation::New().
159 * Calling member functions with an uninitialized Animation handle is not allowed.
165 * @brief Create an initialized Animation.
167 * The animation will not loop.
168 * The default end action is "Bake".
169 * The default alpha function is linear.
171 * @param [in] durationSeconds The duration in seconds.
172 * @return A handle to a newly allocated Dali resource.
173 * @pre DurationSeconds must be greater than zero.
175 static Animation New(float durationSeconds);
178 * @brief Downcast a handle to Animation handle.
180 * If handle points to an Animation object the downcast produces
181 * valid handle. If not the returned handle is left uninitialized.
184 * @param[in] handle Handle to an object
185 * @return Handle to a Animation object or an uninitialized handle
187 static Animation DownCast( BaseHandle handle );
192 * This is non-virtual since derived Handle types must not contain data or virtual methods.
198 * @brief This copy constructor is required for (smart) pointer semantics.
201 * @param [in] handle A reference to the copied handle
203 Animation(const Animation& handle);
206 * @brief This assignment operator is required for (smart) pointer semantics.
209 * @param [in] rhs A reference to the copied handle
210 * @return A reference to this
212 Animation& operator=(const Animation& rhs);
215 * @brief Set the duration of an animation.
218 * @param[in] seconds The duration in seconds.
219 * @pre DurationSeconds must be greater than zero.
221 void SetDuration(float seconds);
224 * @brief Retrieve the duration of an animation.
227 * @return The duration in seconds.
229 float GetDuration() const;
232 * @brief Set whether the animation will loop.
234 * This function resets the loop count and should not be used with SetLoopCount(int).
235 * Setting this parameter does not cause the animation to Play()
238 * @param[in] looping True if the animation will loop.
240 void SetLooping(bool looping);
243 * @brief Enable looping for 'count' repeats.
245 * A zero is the same as SetLooping(true) ie repeat forever.
246 * If Play() Stop() or 'count' loops is reached, the loop counter will reset.
247 * Setting this parameter does not cause the animation to Play()
250 * @param[in] count The number of times to loop.
252 void SetLoopCount(int count);
255 * @brief Get the loop count.
257 * A zero is the same as SetLooping(true) ie repeat forever.
258 * The loop count is initially 1 for play once.
261 * @return The number of times to loop.
266 * @brief Get the current loop count.
268 * A value 0 to GetLoopCount() indicating the current loop count when looping.
271 * @return The current number of loops that have occured.
273 int GetCurrentLoop();
276 * @brief Query whether the animation will loop.
279 * @return True if the animation will loop.
281 bool IsLooping() const;
284 * @brief Set the end action of the animation.
286 * This action is performed when the animation ends or if it is stopped.
287 * Default end action is bake
289 * @param[in] action The end action.
291 void SetEndAction(EndAction action);
294 * @brief Returns the end action of the animation.
297 * @return The end action.
299 EndAction GetEndAction() const;
302 * @brief Set the disconnect action.
304 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
305 * Default action is to BakeFinal.
307 * @param[in] disconnectAction The disconnect action.
309 void SetDisconnectAction( EndAction disconnectAction );
312 * @brief Returns the disconnect action.
315 * @return The disconnect action.
317 EndAction GetDisconnectAction() const;
320 * @brief Set the default alpha function for an animation.
322 * This is applied to individual property animations, if no further alpha functions are supplied.
324 * @param[in] alpha The default alpha function.
326 void SetDefaultAlphaFunction(AlphaFunction alpha);
329 * @brief Retrieve the default alpha function for an animation.
332 * @return The default alpha function.
334 AlphaFunction GetDefaultAlphaFunction() const;
337 * @brief Sets the progress of the animation.
339 * The animation will play (or continue playing) from this point. The progress
340 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
341 * otherwise, it will be ignored.
344 * @param[in] progress The new progress as a normalized value between [0,1]
345 * or between the play range if specified.
347 void SetCurrentProgress( float progress );
350 * @brief Retrieve the current progress of the animation.
353 * @return The current progress as a normalized value between [0,1].
355 float GetCurrentProgress();
358 * @brief Specifies an speed factor for the animation.
360 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
361 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
362 * to play the animation in reverse.
365 * @param[in] factor A value which will multiply the velocity.
367 void SetSpeedFactor( float factor );
370 * @brief Retrieve the speed factor of the animation
373 * @return Speed factor
375 float GetSpeedFactor() const;
378 * @brief Set the playing range.
380 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
381 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
384 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
385 * animation will play between those values.
387 void SetPlayRange( const Vector2& range );
390 * @brief Get the playing range
393 * @return The play range defined for the animation.
395 Vector2 GetPlayRange() const;
398 * @brief Play the animation.
404 * @brief Play the animation from a given point.
406 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
407 * otherwise, it will be ignored.
410 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
412 void PlayFrom( float progress );
415 * @brief Pause the animation.
421 * @brief Query the state of the animation.
423 * @return The Animation::State
425 State GetState() const;
428 * @brief Stop the animation.
434 * @brief Clear the animation.
436 * This disconnects any objects that were being animated, effectively stopping the animation.
442 * @brief Connect to this signal to be notified when an Animation's animations have finished.
445 * @return A signal object to connect with.
447 AnimationSignalType& FinishedSignal();
450 * @brief Animate a property value by a relative amount.
452 * The default alpha function will be used.
453 * The effect will start & end when the animation begins & ends.
455 * @param [in] target The target object/property to animate.
456 * @param [in] relativeValue The property value will change by this amount.
458 void AnimateBy(Property target, Property::Value relativeValue);
461 * @brief Animate a property value by a relative amount.
463 * The effect will start & end when the animation begins & ends.
465 * @param [in] target The target object/property to animate.
466 * @param [in] relativeValue The property value will change by this amount.
467 * @param [in] alpha The alpha function to apply.
469 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
472 * @brief Animate a property value by a relative amount.
474 * The default alpha function will be used.
476 * @param [in] target The target object/property to animate.
477 * @param [in] relativeValue The property value will increase/decrease by this amount.
478 * @param [in] period The effect will occur during this time period.
480 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
483 * @brief Animate a property value by a relative amount.
486 * @param [in] target The target object/property to animate.
487 * @param [in] relativeValue The property value will increase/decrease by this amount.
488 * @param [in] alpha The alpha function to apply.
489 * @param [in] period The effect will occur during this time period.
491 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
494 * @brief Animate a property to a destination value.
496 * The default alpha function will be used.
497 * The effect will start & end when the animation begins & ends.
499 * @param [in] target The target object/property to animate.
500 * @param [in] destinationValue The destination value.
502 void AnimateTo(Property target, Property::Value destinationValue);
505 * @brief Animate a property to a destination value.
507 * The effect will start & end when the animation begins & ends.
509 * @param [in] target The target object/property to animate.
510 * @param [in] destinationValue The destination value.
511 * @param [in] alpha The alpha function to apply.
513 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
516 * @brief Animate a property to a destination value.
518 * The default alpha function will be used.
520 * @param [in] target The target object/property to animate.
521 * @param [in] destinationValue The destination value.
522 * @param [in] period The effect will occur during this time period.
524 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
527 * @brief Animate a property to a destination value.
530 * @param [in] target The target object/property to animate.
531 * @param [in] destinationValue The destination value.
532 * @param [in] alpha The alpha function to apply.
533 * @param [in] period The effect will occur during this time period.
535 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
538 * @brief Animate a property between keyframes.
541 * @param [in] target The target object property to animate.
542 * @param [in] keyFrames The set of time/value pairs between which to animate.
544 void AnimateBetween(Property target, KeyFrames& keyFrames);
547 * @brief Animate a property between keyframes.
550 * @param [in] target The target object property to animate
551 * @param [in] keyFrames The set of time/value pairs between which to animate.
552 * @param [in] interpolation The method used to interpolate between values.
554 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
557 * @brief Animate a property between keyframes.
560 * @param [in] target The target object property to animate.
561 * @param [in] keyFrames The set of time/value pairs between which to animate.
562 * @param [in] alpha The alpha function to apply.
564 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
567 * @brief Animate a property between keyframes.
570 * @param [in] target The target object property to animate
571 * @param [in] keyFrames The set of time/value pairs between which to animate.
572 * @param [in] alpha The alpha function to apply.
573 * @param [in] interpolation The method used to interpolate between values.
575 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
578 * @brief Animate a property between keyframes.
581 * @param [in] target The target object property to animate.
582 * @param [in] keyFrames The set of time/value pairs between which to animate.
583 * @param [in] period The effect will occur during this time period.
585 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
588 * @brief Animate a property between keyframes.
591 * @param [in] target The target object property to animate
592 * @param [in] keyFrames The set of time/value pairs between which to animate.
593 * @param [in] period The effect will occur duing this time period.
594 * @param [in] interpolation The method used to interpolate between values.
596 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
599 * @brief Animate a property between keyframes.
602 * @param [in] target The target object property to animate.
603 * @param [in] keyFrames The set of time/value pairs between which to animate.
604 * @param [in] alpha The alpha function to apply.
605 * @param [in] period The effect will occur during this time period.
607 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
610 * @brief Animate a property between keyframes.
613 * @param [in] target The target object property to animate
614 * @param [in] keyFrames The set of time/value pairs between which to animate.
615 * @param [in] alpha The alpha function to apply to the overall progress.
616 * @param [in] period The effect will occur duing this time period.
617 * @param [in] interpolation The method used to interpolate between values.
619 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
622 // Actor-specific convenience methods
625 * @brief Animate an actor's position and orientation through a predefined path.
627 * The actor will rotate to orient the supplied
628 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
631 * @param[in] actor The actor to animate
632 * @param[in] path The path. It defines position and orientation
633 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
635 void Animate( Actor actor, Path path, const Vector3& forward );
638 * @brief Animate an actor's position and orientation through a predefined path.
640 * The actor will rotate to orient the supplied
641 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
644 * @param[in] actor The actor to animate
645 * @param[in] path The path. It defines position and orientation
646 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
647 * @param [in] alpha The alpha function to apply.
649 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
652 * @brief Animate an actor's position and orientation through a predefined path.
654 * The actor will rotate to orient the supplied
655 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
658 * @param[in] actor The actor to animate
659 * @param[in] path The path. It defines position and orientation
660 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
661 * @param [in] period The effect will occur during this time period.
663 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
666 * @brief Animate an actor's position and orientation through a predefined path.
668 * The actor will rotate to orient the supplied
669 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
672 * @param[in] actor The actor to animate
673 * @param[in] path The path. It defines position and orientation
674 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
675 * @param [in] alpha The alpha function to apply.
676 * @param [in] period The effect will occur during this time period.
678 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
681 * @brief Show an actor during the animation.
684 * @param [in] actor The actor to animate.
685 * @param [in] delaySeconds The initial delay from the start of the animation.
687 void Show(Actor actor, float delaySeconds);
690 * @brief Hide an actor during the animation.
693 * @param [in] actor The actor to animate.
694 * @param [in] delaySeconds The initial delay from the start of the animation.
696 void Hide(Actor actor, float delaySeconds);
698 public: // Not intended for use by Application developers
701 * @brief This constructor is used by Animation::New() methods
703 * @param [in] animation A pointer to a newly allocated Dali resource
705 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
714 #endif // __DALI_ANIMATION_H__