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 Create an uninitialized Animation; this can be initialized with Animation::New().
145 * Calling member functions with an uninitialized Animation handle is not allowed.
151 * @brief Create an initialized Animation.
153 * The animation will not loop.
154 * The default end action is "Bake".
155 * The default alpha function is linear.
157 * @param [in] durationSeconds The duration in seconds.
158 * @return A handle to a newly allocated Dali resource.
159 * @pre DurationSeconds must be greater than zero.
161 static Animation New(float durationSeconds);
164 * @brief Downcast a handle to Animation handle.
166 * If handle points to an Animation object the downcast produces
167 * valid handle. If not the returned handle is left uninitialized.
170 * @param[in] handle Handle to an object
171 * @return Handle to a Animation object or an uninitialized handle
173 static Animation DownCast( BaseHandle handle );
178 * This is non-virtual since derived Handle types must not contain data or virtual methods.
184 * @brief This copy constructor is required for (smart) pointer semantics.
187 * @param [in] handle A reference to the copied handle
189 Animation(const Animation& handle);
192 * @brief This assignment operator is required for (smart) pointer semantics.
195 * @param [in] rhs A reference to the copied handle
196 * @return A reference to this
198 Animation& operator=(const Animation& rhs);
201 * @brief Set the duration of an animation.
204 * @param[in] seconds The duration in seconds.
205 * @pre DurationSeconds must be greater than zero.
207 void SetDuration(float seconds);
210 * @brief Retrieve the duration of an animation.
213 * @return The duration in seconds.
215 float GetDuration() const;
218 * @brief Set whether the animation will loop.
220 * This function resets the loop count and should not be used with SetLoopCount(int).
221 * Setting this parameter does not cause the animation to Play()
224 * @param[in] looping True if the animation will loop.
226 void SetLooping(bool looping);
229 * @brief Enable looping for 'count' repeats.
231 * A zero is the same as SetLooping(true) ie repeat forever.
232 * If Play() Stop() or 'count' loops is reached, the loop counter will reset.
233 * Setting this parameter does not cause the animation to Play()
236 * @param[in] count The number of times to loop.
238 void SetLoopCount(int count);
241 * @brief Get the loop count.
243 * A zero is the same as SetLooping(true) ie repeat forever.
244 * The loop count is initially 1 for play once.
247 * @return The number of times to loop.
252 * @brief Get the current loop count.
254 * A value 0 to GetLoopCount() indicating the current loop count when looping.
257 * @return The current number of loops that have occured.
259 int GetCurrentLoop();
262 * @brief Query whether the animation will loop.
265 * @return True if the animation will loop.
267 bool IsLooping() const;
270 * @brief Set the end action of the animation.
272 * This action is performed when the animation ends or if it is stopped.
273 * Default end action is bake
275 * @param[in] action The end action.
277 void SetEndAction(EndAction action);
280 * @brief Returns the end action of the animation.
283 * @return The end action.
285 EndAction GetEndAction() const;
288 * @brief Set the disconnect action.
290 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
291 * Default action is to BakeFinal.
293 * @param[in] disconnectAction The disconnect action.
295 void SetDisconnectAction( EndAction disconnectAction );
298 * @brief Returns the disconnect action.
301 * @return The disconnect action.
303 EndAction GetDisconnectAction() const;
306 * @brief Set the default alpha function for an animation.
308 * This is applied to individual property animations, if no further alpha functions are supplied.
310 * @param[in] alpha The default alpha function.
312 void SetDefaultAlphaFunction(AlphaFunction alpha);
315 * @brief Retrieve the default alpha function for an animation.
318 * @return The default alpha function.
320 AlphaFunction GetDefaultAlphaFunction() const;
323 * @brief Sets the progress of the animation.
325 * The animation will play (or continue playing) from this point. The progress
326 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
327 * otherwise, it will be ignored.
330 * @param[in] progress The new progress as a normalized value between [0,1]
331 * or between the play range if specified.
333 void SetCurrentProgress( float progress );
336 * @brief Retrieve the current progress of the animation.
339 * @return The current progress as a normalized value between [0,1].
341 float GetCurrentProgress();
344 * @brief Specifies an speed factor for the animation.
346 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
347 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
348 * to play the animation in reverse.
351 * @param[in] factor A value which will multiply the velocity.
353 void SetSpeedFactor( float factor );
356 * @brief Retrieve the speed factor of the animation
359 * @return Speed factor
361 float GetSpeedFactor() const;
364 * @brief Set the playing range.
366 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
367 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
370 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
371 * animation will play between those values.
373 void SetPlayRange( const Vector2& range );
376 * @brief Get the playing range
379 * @return The play range defined for the animation.
381 Vector2 GetPlayRange() const;
384 * @brief Play the animation.
390 * @brief Play the animation from a given point.
392 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
393 * otherwise, it will be ignored.
396 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
398 void PlayFrom( float progress );
401 * @brief Pause the animation.
407 * @brief Stop the animation.
413 * @brief Clear the animation.
415 * This disconnects any objects that were being animated, effectively stopping the animation.
421 * @brief Connect to this signal to be notified when an Animation's animations have finished.
424 * @return A signal object to @ref Signal::Connect() with.
426 AnimationSignalType& FinishedSignal();
429 * @brief Animate a property value by a relative amount.
431 * The default alpha function will be used.
432 * The effect will start & end when the animation begins & ends.
434 * @param [in] target The target object/property to animate.
435 * @param [in] relativeValue The property value will change by this amount.
437 void AnimateBy(Property target, Property::Value relativeValue);
440 * @brief Animate a property value by a relative amount.
442 * The effect will start & end when the animation begins & ends.
444 * @param [in] target The target object/property to animate.
445 * @param [in] relativeValue The property value will change by this amount.
446 * @param [in] alpha The alpha function to apply.
448 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
451 * @brief Animate a property value by a relative amount.
453 * The default alpha function will be used.
455 * @param [in] target The target object/property to animate.
456 * @param [in] relativeValue The property value will increase/decrease by this amount.
457 * @param [in] period The effect will occur during this time period.
459 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
462 * @brief Animate a property value by a relative amount.
465 * @param [in] target The target object/property to animate.
466 * @param [in] relativeValue The property value will increase/decrease by this amount.
467 * @param [in] alpha The alpha function to apply.
468 * @param [in] period The effect will occur during this time period.
470 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
473 * @brief Animate a property to a destination value.
475 * The default alpha function will be used.
476 * The effect will start & end when the animation begins & ends.
478 * @param [in] target The target object/property to animate.
479 * @param [in] destinationValue The destination value.
481 void AnimateTo(Property target, Property::Value destinationValue);
484 * @brief Animate a property to a destination value.
486 * The effect will start & end when the animation begins & ends.
488 * @param [in] target The target object/property to animate.
489 * @param [in] destinationValue The destination value.
490 * @param [in] alpha The alpha function to apply.
492 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
495 * @brief Animate a property to a destination value.
497 * The default alpha function will be used.
499 * @param [in] target The target object/property to animate.
500 * @param [in] destinationValue The destination value.
501 * @param [in] period The effect will occur during this time period.
503 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
506 * @brief Animate a property to a destination value.
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.
512 * @param [in] period The effect will occur during this time period.
514 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
517 * @brief Animate a property between keyframes.
520 * @param [in] target The target object property to animate.
521 * @param [in] keyFrames The set of time/value pairs between which to animate.
523 void AnimateBetween(Property target, KeyFrames& keyFrames);
526 * @brief Animate a property between keyframes.
529 * @param [in] target The target object property to animate
530 * @param [in] keyFrames The set of time/value pairs between which to animate.
531 * @param [in] interpolation The method used to interpolate between values.
533 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
536 * @brief Animate a property between keyframes.
539 * @param [in] target The target object property to animate.
540 * @param [in] keyFrames The set of time/value pairs between which to animate.
541 * @param [in] alpha The alpha function to apply.
543 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
546 * @brief Animate a property between keyframes.
549 * @param [in] target The target object property to animate
550 * @param [in] keyFrames The set of time/value pairs between which to animate.
551 * @param [in] alpha The alpha function to apply.
552 * @param [in] interpolation The method used to interpolate between values.
554 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, 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] period The effect will occur during this time period.
564 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
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] period The effect will occur duing this time period.
573 * @param [in] interpolation The method used to interpolate between values.
575 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, 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] alpha The alpha function to apply.
584 * @param [in] period The effect will occur during this time period.
586 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
589 * @brief Animate a property between keyframes.
592 * @param [in] target The target object property to animate
593 * @param [in] keyFrames The set of time/value pairs between which to animate.
594 * @param [in] alpha The alpha function to apply to the overall progress.
595 * @param [in] period The effect will occur duing this time period.
596 * @param [in] interpolation The method used to interpolate between values.
598 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
601 // Actor-specific convenience methods
604 * @brief Animate an actor's position and orientation through a predefined path.
606 * The actor will rotate to orient the supplied
607 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
610 * @param[in] actor The actor to animate
611 * @param[in] path The path. It defines position and orientation
612 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
614 void Animate( Actor actor, Path path, const Vector3& forward );
617 * @brief Animate an actor's position and orientation through a predefined path.
619 * The actor will rotate to orient the supplied
620 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
623 * @param[in] actor The actor to animate
624 * @param[in] path The path. It defines position and orientation
625 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
626 * @param [in] alpha The alpha function to apply.
628 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
631 * @brief Animate an actor's position and orientation through a predefined path.
633 * The actor will rotate to orient the supplied
634 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
637 * @param[in] actor The actor to animate
638 * @param[in] path The path. It defines position and orientation
639 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
640 * @param [in] period The effect will occur during this time period.
642 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
645 * @brief Animate 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.
655 * @param [in] period The effect will occur during this time period.
657 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
660 * @brief Show an actor during the animation.
663 * @param [in] actor The actor to animate.
664 * @param [in] delaySeconds The initial delay from the start of the animation.
666 void Show(Actor actor, float delaySeconds);
669 * @brief Hide an actor during the animation.
672 * @param [in] actor The actor to animate.
673 * @param [in] delaySeconds The initial delay from the start of the animation.
675 void Hide(Actor actor, float delaySeconds);
677 public: // Not intended for use by Application developers
680 * @brief This constructor is used by Animation::New() methods
682 * @param [in] animation A pointer to a newly allocated Dali resource
684 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
693 #endif // __DALI_ANIMATION_H__