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.
221 * @param[in] looping True if the animation will loop.
223 void SetLooping(bool looping);
226 * @brief Query whether the animation will loop.
229 * @return True if the animation will loop.
231 bool IsLooping() const;
234 * @brief Set the end action of the animation.
236 * This action is performed when the animation ends or if it is stopped.
237 * Default end action is bake
239 * @param[in] action The end action.
241 void SetEndAction(EndAction action);
244 * @brief Returns the end action of the animation.
247 * @return The end action.
249 EndAction GetEndAction() const;
252 * @brief Set the disconnect action.
254 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
255 * Default action is to BakeFinal.
257 * @param[in] disconnectAction The disconnect action.
259 void SetDisconnectAction( EndAction disconnectAction );
262 * @brief Returns the disconnect action.
265 * @return The disconnect action.
267 EndAction GetDisconnectAction() const;
270 * @brief Set the default alpha function for an animation.
272 * This is applied to individual property animations, if no further alpha functions are supplied.
274 * @param[in] alpha The default alpha function.
276 void SetDefaultAlphaFunction(AlphaFunction alpha);
279 * @brief Retrieve the default alpha function for an animation.
282 * @return The default alpha function.
284 AlphaFunction GetDefaultAlphaFunction() const;
287 * @brief Sets the progress of the animation.
289 * When played, the animation will start from this point.
290 * If playing, the animation will jump to, and continue playing from this point.
292 * The progress must be in the 0-1 interval or in the play range interval
293 * if defined ( See SetPlayRange ), otherwise, it will be ignored.
295 * @param[in] progress The new progress as a normalized value between [0,1]
296 * or between the play range if specified.
298 void SetCurrentProgress( float progress );
301 * @brief Retrieve the current progress of the animation.
304 * @return The current progress as a normalized value between [0,1].
306 float GetCurrentProgress();
309 * @brief Specifies an speed factor for the animation.
311 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
312 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
313 * to play the animation in reverse.
316 * @param[in] factor A value which will multiply the velocity.
318 void SetSpeedFactor( float factor );
321 * @brief Retrieve the speed factor of the animation
324 * @return speed factor
326 float GetSpeedFactor() const;
329 * @brief Set the playing range.
330 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
331 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
334 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
335 * animation will play between those values.
337 void SetPlayRange( const Vector2& range );
340 * @brief Get the playing range
343 * @return The play range defined for the animation.
345 Vector2 GetPlayRange() const;
348 * @brief Play the animation.
354 * @brief Play the animation from a given point.
355 * The progress must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
356 * otherwise, it will be ignored.
359 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
361 void PlayFrom( float progress );
364 * @brief Pause the animation.
370 * @brief Stop the animation.
376 * @brief Clear the animation.
378 * This disconnects any objects that were being animated, effectively stopping the animation.
384 * @brief Connect to this signal to be notified when an Animation's animations have finished.
387 * @return A signal object to Connect() with.
389 AnimationSignalType& FinishedSignal();
392 * @brief Animate a property value by a relative amount.
394 * The default alpha function will be used.
395 * The effect will start & end when the animation begins & ends.
397 * @param [in] target The target object/property to animate.
398 * @param [in] relativeValue The property value will change by this amount.
400 void AnimateBy(Property target, Property::Value relativeValue);
403 * @brief Animate a property value by a relative amount.
405 * The effect will start & end when the animation begins & ends.
407 * @param [in] target The target object/property to animate.
408 * @param [in] relativeValue The property value will change by this amount.
409 * @param [in] alpha The alpha function to apply.
411 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
414 * @brief Animate a property value by a relative amount.
416 * The default alpha function will be used.
418 * @param [in] target The target object/property to animate.
419 * @param [in] relativeValue The property value will increase/decrease by this amount.
420 * @param [in] period The effect will occur during this time period.
422 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
425 * @brief Animate a property value by a relative amount.
428 * @param [in] target The target object/property to animate.
429 * @param [in] relativeValue The property value will increase/decrease by this amount.
430 * @param [in] alpha The alpha function to apply.
431 * @param [in] period The effect will occur during this time period.
433 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
436 * @brief Animate a property to a destination value.
438 * The default alpha function will be used.
439 * The effect will start & end when the animation begins & ends.
441 * @param [in] target The target object/property to animate.
442 * @param [in] destinationValue The destination value.
444 void AnimateTo(Property target, Property::Value destinationValue);
447 * @brief Animate a property to a destination value.
449 * The effect will start & end when the animation begins & ends.
451 * @param [in] target The target object/property to animate.
452 * @param [in] destinationValue The destination value.
453 * @param [in] alpha The alpha function to apply.
455 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
458 * @brief Animate a property to a destination value.
460 * The default alpha function will be used.
462 * @param [in] target The target object/property to animate.
463 * @param [in] destinationValue The destination value.
464 * @param [in] period The effect will occur during this time period.
466 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
469 * @brief Animate a property to a destination value.
472 * @param [in] target The target object/property to animate.
473 * @param [in] destinationValue The destination value.
474 * @param [in] alpha The alpha function to apply.
475 * @param [in] period The effect will occur during this time period.
477 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
480 * @brief Animate a property between keyframes.
483 * @param [in] target The target object/property to animate.
484 * @param [in] keyFrames The key frames
486 void AnimateBetween(Property target, KeyFrames& keyFrames);
489 * @brief Animate a property between keyframes.
492 * @param [in] target The target object + property to animate
493 * @param [in] keyFrames The set of time / value pairs between which to animate.
494 * @param [in] interpolation The method used to interpolate between values.
496 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
499 * @brief Animate a property between keyframes.
502 * @param [in] target The target object/property to animate.
503 * @param [in] keyFrames The key frames
504 * @param [in] alpha The alpha function to apply.
506 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
509 * @brief Animate a property between keyframes.
512 * @param [in] target The target object + property to animate
513 * @param [in] keyFrames The set of time / value pairs between which to animate.
514 * @param [in] alpha The alpha function to apply.
515 * @param [in] interpolation The method used to interpolate between values.
517 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
520 * @brief Animate a property between keyframes.
523 * @param [in] target The target object/property to animate.
524 * @param [in] keyFrames The key frames
525 * @param [in] period The effect will occur during this time period.
527 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
530 * @brief Animate a property between keyframes.
533 * @param [in] target The target object + property to animate
534 * @param [in] keyFrames The set of time / value pairs between which to animate.
535 * @param [in] period The effect will occur duing this time period.
536 * @param [in] interpolation The method used to interpolate between values.
538 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
541 * @brief Animate a property between keyframes.
544 * @param [in] target The target object/property to animate.
545 * @param [in] keyFrames The key frames
546 * @param [in] alpha The alpha function to apply.
547 * @param [in] period The effect will occur during this time period.
549 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
552 * @brief Animate a property between keyframes.
555 * @param [in] target The target object + property to animate
556 * @param [in] keyFrames The set of time / value pairs between which to animate.
557 * @param [in] alpha The alpha function to apply to the overall progress.
558 * @param [in] period The effect will occur duing this time period.
559 * @param [in] interpolation The method used to interpolate between values.
561 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
564 // Actor-specific convenience methods
567 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
568 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
571 * @param[in] actor The actor to animate
572 * @param[in] path The path. It defines position and orientation
573 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
575 void Animate( Actor actor, Path path, const Vector3& forward );
578 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
579 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
582 * @param[in] actor The actor to animate
583 * @param[in] path The path. It defines position and orientation
584 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
585 * @param [in] alpha The alpha function to apply.
587 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
590 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
591 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
594 * @param[in] actor The actor to animate
595 * @param[in] path The path. It defines position and orientation
596 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
597 * @param [in] period The effect will occur during this time period.
599 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
602 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
603 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
606 * @param[in] actor The actor to animate
607 * @param[in] path The path. It defines position and orientation
608 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
609 * @param [in] alpha The alpha function to apply.
610 * @param [in] period The effect will occur during this time period.
612 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
615 * @brief Show an actor during the animation.
618 * @param [in] actor The actor to animate.
619 * @param [in] delaySeconds The initial delay from the start of the animation.
621 void Show(Actor actor, float delaySeconds);
624 * @brief Hide an actor during the animation.
627 * @param [in] actor The actor to animate.
628 * @param [in] delaySeconds The initial delay from the start of the animation.
630 void Hide(Actor actor, float delaySeconds);
632 public: // Not intended for use by Application developers
635 * @brief This constructor is used by Dali New() methods
637 * @param [in] animation A pointer to a newly allocated Dali resource
639 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
648 #endif // __DALI_ANIMATION_H__