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.
100 class DALI_IMPORT_API Animation : public BaseHandle
104 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type @since_tizen 2.4
106 typedef Any AnyFunction; ///< Interpolation function @since_tizen 2.4
109 * @brief What to do when the animation ends, is stopped or is destroyed
114 Bake, ///< When the animation ends, the animated property values are saved. @since_tizen 2.4
115 Discard, ///< When the animation ends, the animated property values are forgotten. @since_tizen 2.4
116 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_tizen 2.4
120 * @brief What interpolation method to use on key-frame animations
125 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default) @since_tizen 2.4
126 Cubic ///< Values in between key frames are interpolated using a cubic polynomial. @since_tizen 2.4
130 * @brief Create an uninitialized Animation; this can be initialized with Animation::New().
132 * Calling member functions with an unintialized Animation handle is not allowed.
139 * @brief Create an initialized Animation.
141 * The animation will not loop.
142 * The default end action is "Bake".
143 * The default alpha function is linear.
145 * @param [in] durationSeconds The duration in seconds.
146 * @return A handle to a newly allocated Dali resource.
147 * @pre DurationSeconds must be greater than zero.
149 static Animation New(float durationSeconds);
152 * @brief Downcast a handle to Animation handle.
154 * If handle points to an Animation object the downcast produces
155 * valid handle. If not the returned handle is left uninitialized.
158 * @param[in] handle Handle to an object
159 * @return Handle to a Animation object or an uninitialized handle
161 static Animation DownCast( BaseHandle handle );
166 * This is non-virtual since derived Handle types must not contain data or virtual methods.
172 * @brief This copy constructor is required for (smart) pointer semantics.
175 * @param [in] handle A reference to the copied handle
177 Animation(const Animation& handle);
180 * @brief This assignment operator is required for (smart) pointer semantics.
183 * @param [in] rhs A reference to the copied handle
184 * @return A reference to this
186 Animation& operator=(const Animation& rhs);
189 * @brief Set the duration of an animation.
192 * @param[in] seconds The duration in seconds.
193 * @pre DurationSeconds must be greater than zero.
195 void SetDuration(float seconds);
198 * @brief Retrieve the duration of an animation.
201 * @return The duration in seconds.
203 float GetDuration() const;
206 * @brief Set whether the animation will loop.
209 * @param[in] looping True if the animation will loop.
211 void SetLooping(bool looping);
214 * @brief Query whether the animation will loop.
217 * @return True if the animation will loop.
219 bool IsLooping() const;
222 * @brief Set the end action of the animation.
224 * This action is performed when the animation ends or if it is stopped.
225 * Default end action is bake
227 * @param[in] action The end action.
229 void SetEndAction(EndAction action);
232 * @brief Returns the end action of the animation.
235 * @return The end action.
237 EndAction GetEndAction() const;
240 * @brief Set the disconnect action.
242 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
243 * Default action is to BakeFinal.
245 * @param[in] disconnectAction The disconnect action.
247 void SetDisconnectAction( EndAction disconnectAction );
250 * @brief Returns the disconnect action.
253 * @return The disconnect action.
255 EndAction GetDisconnectAction() const;
258 * @brief Set the default alpha function for an animation.
260 * This is applied to individual property animations, if no further alpha functions are supplied.
262 * @param[in] alpha The default alpha function.
264 void SetDefaultAlphaFunction(AlphaFunction alpha);
267 * @brief Retrieve the default alpha function for an animation.
270 * @return The default alpha function.
272 AlphaFunction GetDefaultAlphaFunction() const;
275 * @brief Sets the progress of the animation.
277 * The animation will play (or continue playing) from this point. The progress
278 * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
279 * otherwise, it will be ignored.
282 * @param[in] progress The new progress as a normalized value between [0,1] or between the
283 * play range if specified.
285 void SetCurrentProgress( float progress );
288 * @brief Retrieve the current progress of the animation.
291 * @return The current progress as a normalized value between [0,1].
293 float GetCurrentProgress();
296 * @brief Specifies an speed factor for the animation.
298 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
299 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
300 * to play the animation in reverse.
303 * @param[in] factor A value which will multiply the velocity.
305 void SetSpeedFactor( float factor );
308 * @brief Retrieve the speed factor of the animation
311 * @return Speed factor
313 float GetSpeedFactor() const;
316 * @brief Set the playing range.
318 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
319 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
322 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
323 * animation will play between those values.
325 void SetPlayRange( const Vector2& range );
328 * @brief Get the playing range
331 * @return The play range defined for the animation.
333 Vector2 GetPlayRange() const;
336 * @brief Play the animation.
342 * @brief Play the animation from a given point.
344 * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
345 * otherwise, it will be ignored.
348 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
350 void PlayFrom( float progress );
353 * @brief Pause the animation.
359 * @brief Stop the animation.
365 * @brief Clear the animation.
367 * This disconnects any objects that were being animated, effectively stopping the animation.
373 * @brief Connect to this signal to be notified when an Animation's animations have finished.
376 * @return A signal object to @ref Signal::Connect() with.
378 AnimationSignalType& FinishedSignal();
381 * @brief Animate a property value by a relative amount.
383 * The default alpha function will be used.
384 * The effect will start & end when the animation begins & ends.
386 * @param [in] target The target object/property to animate.
387 * @param [in] relativeValue The property value will change by this amount.
389 void AnimateBy(Property target, Property::Value relativeValue);
392 * @brief Animate a property value by a relative amount.
394 * The effect will start & end when the animation begins & ends.
396 * @param [in] target The target object/property to animate.
397 * @param [in] relativeValue The property value will change by this amount.
398 * @param [in] alpha The alpha function to apply.
400 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
403 * @brief Animate a property value by a relative amount.
405 * The default alpha function will be used.
407 * @param [in] target The target object/property to animate.
408 * @param [in] relativeValue The property value will increase/decrease by this amount.
409 * @param [in] period The effect will occur during this time period.
411 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
414 * @brief Animate a property value by a relative amount.
417 * @param [in] target The target object/property to animate.
418 * @param [in] relativeValue The property value will increase/decrease by this amount.
419 * @param [in] alpha The alpha function to apply.
420 * @param [in] period The effect will occur during this time period.
422 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
425 * @brief Animate a property to a destination value.
427 * The default alpha function will be used.
428 * The effect will start & end when the animation begins & ends.
430 * @param [in] target The target object/property to animate.
431 * @param [in] destinationValue The destination value.
433 void AnimateTo(Property target, Property::Value destinationValue);
436 * @brief Animate a property to a destination value.
438 * The effect will start & end when the animation begins & ends.
440 * @param [in] target The target object/property to animate.
441 * @param [in] destinationValue The destination value.
442 * @param [in] alpha The alpha function to apply.
444 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
447 * @brief Animate a property to a destination value.
449 * The default alpha function will be used.
451 * @param [in] target The target object/property to animate.
452 * @param [in] destinationValue The destination value.
453 * @param [in] period The effect will occur during this time period.
455 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
458 * @brief Animate a property to a destination value.
461 * @param [in] target The target object/property to animate.
462 * @param [in] destinationValue The destination value.
463 * @param [in] alpha The alpha function to apply.
464 * @param [in] period The effect will occur during this time period.
466 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
469 * @brief Animate a property between keyframes.
472 * @param [in] target The target object/property to animate.
473 * @param [in] keyFrames The key frames
475 void AnimateBetween(Property target, KeyFrames& keyFrames);
478 * @brief Animate a property between keyframes.
481 * @param [in] target The target object/property to animate
482 * @param [in] keyFrames The set of time/value pairs between which to animate.
483 * @param [in] interpolation The method used to interpolate between values.
485 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
488 * @brief Animate a property between keyframes.
491 * @param [in] target The target object/property to animate.
492 * @param [in] keyFrames The key frames
493 * @param [in] alpha The alpha function to apply.
495 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
498 * @brief Animate a property between keyframes.
501 * @param [in] target The target object/property to animate
502 * @param [in] keyFrames The set of time/value pairs between which to animate.
503 * @param [in] alpha The alpha function to apply.
504 * @param [in] interpolation The method used to interpolate between values.
506 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
509 * @brief Animate a property between keyframes.
512 * @param [in] target The target object/property to animate.
513 * @param [in] keyFrames The key frames
514 * @param [in] period The effect will occur during this time period.
516 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
519 * @brief Animate a property between keyframes.
522 * @param [in] target The target object/property to animate
523 * @param [in] keyFrames The set of time/value pairs between which to animate.
524 * @param [in] period The effect will occur duing this time period.
525 * @param [in] interpolation The method used to interpolate between values.
527 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
530 * @brief Animate a property between keyframes.
533 * @param [in] target The target object/property to animate.
534 * @param [in] keyFrames The key frames
535 * @param [in] alpha The alpha function to apply.
536 * @param [in] period The effect will occur during this time period.
538 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
541 * @brief Animate 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.
546 * @param [in] alpha The alpha function to apply to the overall progress.
547 * @param [in] period The effect will occur duing this time period.
548 * @param [in] interpolation The method used to interpolate between values.
550 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
553 // Actor-specific convenience methods
556 * @brief Animate an actor's position and orientation through a predefined path.
558 * The actor will rotate to orient the supplied
559 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
562 * @param[in] actor The actor to animate
563 * @param[in] path The path. It defines position and orientation
564 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
566 void Animate( Actor actor, Path path, const Vector3& forward );
569 * @brief Animate an actor's position and orientation through a predefined path.
571 * The actor will rotate to orient the supplied
572 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
575 * @param[in] actor The actor to animate
576 * @param[in] path The path. It defines position and orientation
577 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
578 * @param [in] alpha The alpha function to apply.
580 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
583 * @brief Animate an actor's position and orientation through a predefined path.
585 * The actor will rotate to orient the supplied
586 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
589 * @param[in] actor The actor to animate
590 * @param[in] path The path. It defines position and orientation
591 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
592 * @param [in] period The effect will occur during this time period.
594 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
597 * @brief Animate an actor's position and orientation through a predefined path.
599 * The actor will rotate to orient the supplied
600 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
603 * @param[in] actor The actor to animate
604 * @param[in] path The path. It defines position and orientation
605 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
606 * @param [in] alpha The alpha function to apply.
607 * @param [in] period The effect will occur during this time period.
609 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
612 * @brief Show an actor during the animation.
615 * @param [in] actor The actor to animate.
616 * @param [in] delaySeconds The initial delay from the start of the animation.
618 void Show(Actor actor, float delaySeconds);
621 * @brief Hide an actor during the animation.
624 * @param [in] actor The actor to animate.
625 * @param [in] delaySeconds The initial delay from the start of the animation.
627 void Hide(Actor actor, float delaySeconds);
629 public: // Not intended for use by Application developers
632 * @brief This constructor is used by Animation::New() methods
634 * @param [in] animation A pointer to a newly allocated Dali resource
636 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
645 #endif // __DALI_ANIMATION_H__