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 Dali::Object 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 an Object handle to Animation.
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 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 forever.
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 If true then the animation will loop forever, if false it will never 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 * When played, the animation will start from this point.
326 * If playing, the animation will jump to, and continue playing from this point.
328 * The progress must be in the 0-1 interval or in the play range interval
329 * if defined ( See SetPlayRange ), otherwise, it will be ignored.
331 * @param[in] progress The new progress as a normalized value between [0,1]
332 * or between the play range if specified.
334 void SetCurrentProgress( float progress );
337 * @brief Retrieve the current progress of the animation.
340 * @return The current progress as a normalized value between [0,1].
342 float GetCurrentProgress();
345 * @brief Specifies an speed factor for the animation.
347 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
348 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
349 * to play the animation in reverse.
352 * @param[in] factor A value which will multiply the velocity.
354 void SetSpeedFactor( float factor );
357 * @brief Retrieve the speed factor of the animation
360 * @return speed factor
362 float GetSpeedFactor() const;
365 * @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.
391 * The progress must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
392 * otherwise, it will be ignored.
395 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
397 void PlayFrom( float progress );
400 * @brief Pause the animation.
406 * @brief Stop the animation.
412 * @brief Clear the animation.
414 * This disconnects any objects that were being animated, effectively stopping the animation.
420 * @brief Connect to this signal to be notified when an Animation's animations have finished.
423 * @return A signal object to Connect() with.
425 AnimationSignalType& FinishedSignal();
428 * @brief Animate a property value by a relative amount.
430 * The default alpha function will be used.
431 * The effect will start & end when the animation begins & ends.
433 * @param [in] target The target object/property to animate.
434 * @param [in] relativeValue The property value will change by this amount.
436 void AnimateBy(Property target, Property::Value relativeValue);
439 * @brief Animate a property value by a relative amount.
441 * The effect will start & end when the animation begins & ends.
443 * @param [in] target The target object/property to animate.
444 * @param [in] relativeValue The property value will change by this amount.
445 * @param [in] alpha The alpha function to apply.
447 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
450 * @brief Animate a property value by a relative amount.
452 * The default alpha function will be used.
454 * @param [in] target The target object/property to animate.
455 * @param [in] relativeValue The property value will increase/decrease by this amount.
456 * @param [in] period The effect will occur during this time period.
458 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
461 * @brief Animate a property value by a relative amount.
464 * @param [in] target The target object/property to animate.
465 * @param [in] relativeValue The property value will increase/decrease by this amount.
466 * @param [in] alpha The alpha function to apply.
467 * @param [in] period The effect will occur during this time period.
469 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
472 * @brief Animate a property to a destination value.
474 * The default alpha function will be used.
475 * The effect will start & end when the animation begins & ends.
477 * @param [in] target The target object/property to animate.
478 * @param [in] destinationValue The destination value.
480 void AnimateTo(Property target, Property::Value destinationValue);
483 * @brief Animate a property to a destination value.
485 * The effect will start & end when the animation begins & ends.
487 * @param [in] target The target object/property to animate.
488 * @param [in] destinationValue The destination value.
489 * @param [in] alpha The alpha function to apply.
491 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
494 * @brief Animate a property to a destination value.
496 * The default alpha function will be used.
498 * @param [in] target The target object/property to animate.
499 * @param [in] destinationValue The destination value.
500 * @param [in] period The effect will occur during this time period.
502 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
505 * @brief Animate a property to a destination value.
508 * @param [in] target The target object/property to animate.
509 * @param [in] destinationValue The destination value.
510 * @param [in] alpha The alpha function to apply.
511 * @param [in] period The effect will occur during this time period.
513 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
516 * @brief Animate a property between keyframes.
519 * @param [in] target The target object/property to animate.
520 * @param [in] keyFrames The key frames
522 void AnimateBetween(Property target, KeyFrames& keyFrames);
525 * @brief Animate a property between keyframes.
528 * @param [in] target The target object + property to animate
529 * @param [in] keyFrames The set of time / value pairs between which to animate.
530 * @param [in] interpolation The method used to interpolate between values.
532 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
535 * @brief Animate a property between keyframes.
538 * @param [in] target The target object/property to animate.
539 * @param [in] keyFrames The key frames
540 * @param [in] alpha The alpha function to apply.
542 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
545 * @brief Animate 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.
550 * @param [in] alpha The alpha function to apply.
551 * @param [in] interpolation The method used to interpolate between values.
553 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
556 * @brief Animate a property between keyframes.
559 * @param [in] target The target object/property to animate.
560 * @param [in] keyFrames The key frames
561 * @param [in] period The effect will occur during this time period.
563 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
566 * @brief Animate a property between keyframes.
569 * @param [in] target The target object + property to animate
570 * @param [in] keyFrames The set of time / value pairs between which to animate.
571 * @param [in] period The effect will occur duing this time period.
572 * @param [in] interpolation The method used to interpolate between values.
574 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
577 * @brief Animate a property between keyframes.
580 * @param [in] target The target object/property to animate.
581 * @param [in] keyFrames The key frames
582 * @param [in] alpha The alpha function to apply.
583 * @param [in] period The effect will occur during this time period.
585 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, 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] alpha The alpha function to apply to the overall progress.
594 * @param [in] period The effect will occur duing this time period.
595 * @param [in] interpolation The method used to interpolate between values.
597 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
600 // Actor-specific convenience methods
603 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
604 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
607 * @param[in] actor The actor to animate
608 * @param[in] path The path. It defines position and orientation
609 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
611 void Animate( Actor actor, Path path, const Vector3& forward );
614 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
615 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
618 * @param[in] actor The actor to animate
619 * @param[in] path The path. It defines position and orientation
620 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
621 * @param [in] alpha The alpha function to apply.
623 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
626 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
627 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
630 * @param[in] actor The actor to animate
631 * @param[in] path The path. It defines position and orientation
632 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
633 * @param [in] period The effect will occur during this time period.
635 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
638 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
639 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
642 * @param[in] actor The actor to animate
643 * @param[in] path The path. It defines position and orientation
644 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
645 * @param [in] alpha The alpha function to apply.
646 * @param [in] period The effect will occur during this time period.
648 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
651 * @brief Show an actor during the animation.
654 * @param [in] actor The actor to animate.
655 * @param [in] delaySeconds The initial delay from the start of the animation.
657 void Show(Actor actor, float delaySeconds);
660 * @brief Hide 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 Hide(Actor actor, float delaySeconds);
668 public: // Not intended for use by Application developers
671 * @brief This constructor is used by Dali New() methods
673 * @param [in] animation A pointer to a newly allocated Dali resource
675 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
684 #endif // __DALI_ANIMATION_H__