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>
39 namespace Internal DALI_INTERNAL
45 * @brief Dali::Animation can be used to animate the properties of any number of objects, typically Actors.
47 * An example animation setup is shown below:
53 * Actor mActor; // The object we wish to animate
54 * Animation mAnimation; // Keep this to control the animation
57 * // ...To play the animation
59 * mAnimation = Animation::New(3.0f); // duration 3 seconds
60 * mAnimation.AnimateTo(Property(mActor, Actor::Property::POSITION), Vector3(10.0f, 50.0f, 0.0f));
65 * Dali::Animation supports "fire and forget" behaviour i.e. an animation continues to play if the handle is discarded.
66 * Note that in the following example, the "Finish" signal will be emitted:
70 * void ExampleCallback( Animation& source )
72 * std::cout << "Animation has finished" << std::endl;
75 * void ExampleAnimation( Actor actor )
77 * Animation animation = Animation::New(2.0f); // duration 2 seconds
78 * animation.AnimateTo(Property(actor, Actor::Property::POSITION), 10.0f, 50.0f, 0.0f);
79 * animation.FinishedSignal().Connect( ExampleCallback );
81 * } // At this point the animation handle has gone out of scope
83 * Actor actor = Actor::New();
84 * Stage::GetCurrent().Add( actor );
86 * // Fire animation and forget about it
87 * ExampleAnimation( actor );
89 * // However the animation will continue, and "Animation has finished" will be printed after 2 seconds.
93 * If the "Finish" signal is connected to a member function of an object, it must be disconnected before the object is destroyed.
94 * This is typically done in the object destructor, and requires either the Dali::Connection object or Dali::Animation handle to be stored.
97 * | %Signal Name | Method |
98 * |--------------|--------------------------|
99 * | finished | @ref FinishedSignal() |
102 * | %Action Name | %Animation method called |
103 * |--------------|--------------------------|
106 * | pause | Pause() |
108 class DALI_IMPORT_API Animation : public BaseHandle
112 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type
114 typedef Any AnyFunction; ///< Interpolation function
117 * @brief What to do when the animation ends, is stopped or is destroyed
121 Bake, ///< When the animation ends, the animated property values are saved.
122 Discard, ///< When the animation ends, the animated property values are forgotten.
123 BakeFinal ///< If the animation is stopped, the animated property values are saved as if the animation had run to completion, otherwise behaves like Bake.
127 * @brief What interpolation method to use on key-frame animations
131 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default)
132 Cubic ///< Values in between key frames are interpolated using a cubic polynomial.
136 * @brief Create an uninitialized Animation; this can be initialized with Animation::New().
138 * Calling member functions with an uninitialized Dali::Object is not allowed.
143 * @brief Create an initialized Animation.
145 * The animation will not loop.
146 * The default end action is "Bake".
147 * The default alpha function is linear.
148 * @pre durationSeconds must be greater than zero.
149 * @param [in] durationSeconds The duration in seconds.
150 * @return A handle to a newly allocated Dali resource.
152 static Animation New(float durationSeconds);
155 * @brief Downcast an Object handle to Animation.
157 * If handle points to an Animation object the downcast produces
158 * valid handle. If not the returned handle is left uninitialized.
160 * @param[in] handle to An object
161 * @return handle to a Animation object or an uninitialized handle
163 static Animation DownCast( BaseHandle handle );
168 * This is non-virtual since derived Handle types must not contain data or virtual methods.
173 * @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.
182 * @param [in] rhs A reference to the copied handle
183 * @return A reference to this
185 Animation& operator=(const Animation& rhs);
188 * @brief Set the duration of an animation.
190 * @pre durationSeconds must be greater than zero.
191 * @param[in] seconds The duration in seconds.
193 void SetDuration(float seconds);
196 * @brief Retrieve the duration of an animation.
198 * @return The duration in seconds.
200 float GetDuration() const;
203 * @brief Set whether the animation will loop.
205 * @param[in] looping True if the animation will loop.
207 void SetLooping(bool looping);
210 * @brief Query whether the animation will loop.
212 * @return True if the animation will loop.
214 bool IsLooping() const;
217 * @brief Set the end action of the animation.
219 * This action is performed when the animation ends or if it is stopped.
220 * Default end action is bake
221 * @param[in] action The end action.
223 void SetEndAction(EndAction action);
226 * @brief Returns the end action of the animation.
228 * @return The end action.
230 EndAction GetEndAction() const;
233 * @brief Set the disconnect action.
235 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
236 * Default action is to BakeFinal.
237 * @param[in] disconnectAction The disconnect action.
239 void SetDisconnectAction( EndAction disconnectAction );
242 * @brief Returns the disconnect action.
244 * @return The disconnect action.
246 EndAction GetDisconnectAction() const;
249 * @brief Set the default alpha function for an animation.
251 * This is applied to individual property animations, if no further alpha functions are supplied.
252 * @param[in] alpha The default alpha function.
254 void SetDefaultAlphaFunction(AlphaFunction alpha);
257 * @brief Retrieve the default alpha function for an animation.
259 * @return The default alpha function.
261 AlphaFunction GetDefaultAlphaFunction() const;
264 * @brief Sets the progress of the animation.
266 * When played, the animation will start from this point.
267 * If playing, the animation will jump to, and continue playing from this point.
269 * The progress must be in the 0-1 interval or in the play range interval
270 * if defined ( See SetPlayRange ), otherwise, it will be ignored.
272 * @param[in] progress The new progress as a normalized value between [0,1]
273 * or between the play range if specified.
275 void SetCurrentProgress( float progress );
278 * @brief Retrieve the current progress of the animation.
280 * @return The current progress as a normalized value between [0,1].
282 float GetCurrentProgress();
285 * @brief Specifies an speed factor for the animation.
287 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
288 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
289 * to play the animation in reverse.
291 * @param[in] factor A value which will multiply the velocity.
293 void SetSpeedFactor( float factor );
296 * @brief Retrieve the speed factor of the animation
298 * @return speed factor
300 float GetSpeedFactor() const;
303 * @brief Set the playing range.
304 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
305 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
307 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
308 * animation will play between those values.
310 void SetPlayRange( const Vector2& range );
313 * @brief Get the playing range
315 * @return The play range defined for the animation.
317 Vector2 GetPlayRange() const;
320 * @brief Play the animation.
325 * @brief Play the animation from a given point.
326 * The progress must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
327 * otherwise, it will be ignored.
329 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
331 void PlayFrom( float progress );
334 * @brief Pause the animation.
339 * @brief Stop the animation.
344 * @brief Clear the animation.
346 * This disconnects any objects that were being animated, effectively stopping the animation.
351 * @brief Connect to this signal to be notified when an Animation's animations have finished.
353 * @return A signal object to Connect() with.
355 AnimationSignalType& FinishedSignal();
358 * @brief Animate a property value by a relative amount.
360 * The default alpha function will be used.
361 * The effect will start & end when the animation begins & ends.
362 * @param [in] target The target object/property to animate.
363 * @param [in] relativeValue The property value will change by this amount.
365 void AnimateBy(Property target, Property::Value relativeValue);
368 * @brief Animate a property value by a relative amount.
370 * The effect will start & end when the animation begins & ends.
371 * @param [in] target The target object/property to animate.
372 * @param [in] relativeValue The property value will change by this amount.
373 * @param [in] alpha The alpha function to apply.
375 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
378 * @brief Animate a property value by a relative amount.
380 * The default alpha function will be used.
381 * @param [in] target The target object/property to animate.
382 * @param [in] relativeValue The property value will increase/decrease by this amount.
383 * @param [in] period The effect will occur during this time period.
385 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
388 * @brief Animate a property value by a relative amount.
390 * @param [in] target The target object/property to animate.
391 * @param [in] relativeValue The property value will increase/decrease by this amount.
392 * @param [in] alpha The alpha function to apply.
393 * @param [in] period The effect will occur during this time period.
395 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
398 * @brief Animate a property to a destination value.
400 * The default alpha function will be used.
401 * The effect will start & end when the animation begins & ends.
402 * @param [in] target The target object/property to animate.
403 * @param [in] destinationValue The destination value.
405 void AnimateTo(Property target, Property::Value destinationValue);
408 * @brief Animate a property to a destination value.
410 * The effect will start & end when the animation begins & ends.
411 * @param [in] target The target object/property to animate.
412 * @param [in] destinationValue The destination value.
413 * @param [in] alpha The alpha function to apply.
415 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
418 * @brief Animate a property to a destination value.
420 * The default alpha function will be used.
421 * @param [in] target The target object/property to animate.
422 * @param [in] destinationValue The destination value.
423 * @param [in] period The effect will occur during this time period.
425 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
428 * @brief Animate a property to a destination value.
430 * @param [in] target The target object/property to animate.
431 * @param [in] destinationValue The destination value.
432 * @param [in] alpha The alpha function to apply.
433 * @param [in] period The effect will occur during this time period.
435 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
438 * @brief Animate a property between keyframes.
440 * @param [in] target The target object/property to animate.
441 * @param [in] keyFrames The key frames
443 void AnimateBetween(Property target, KeyFrames& keyFrames);
446 * @brief Animate a property between keyframes.
448 * @param [in] target The target object + property to animate
449 * @param [in] keyFrames The set of time / value pairs between which to animate.
450 * @param [in] interpolation The method used to interpolate between values.
452 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
455 * @brief Animate a property between keyframes.
457 * @param [in] target The target object/property to animate.
458 * @param [in] keyFrames The key frames
459 * @param [in] alpha The alpha function to apply.
461 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
464 * @brief Animate a property between keyframes.
466 * @param [in] target The target object + property to animate
467 * @param [in] keyFrames The set of time / value pairs between which to animate.
468 * @param [in] alpha The alpha function to apply.
469 * @param [in] interpolation The method used to interpolate between values.
471 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
474 * @brief Animate a property between keyframes.
476 * @param [in] target The target object/property to animate.
477 * @param [in] keyFrames The key frames
478 * @param [in] period The effect will occur during this time period.
480 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
483 * @brief Animate a property between keyframes.
485 * @param [in] target The target object + property to animate
486 * @param [in] keyFrames The set of time / value pairs between which to animate.
487 * @param [in] period The effect will occur duing this time period.
488 * @param [in] interpolation The method used to interpolate between values.
490 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
493 * @brief Animate a property between keyframes.
495 * @param [in] target The target object/property to animate.
496 * @param [in] keyFrames The key frames
497 * @param [in] alpha The alpha function to apply.
498 * @param [in] period The effect will occur during this time period.
500 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
503 * @brief Animate a property between keyframes.
505 * @param [in] target The target object + property to animate
506 * @param [in] keyFrames The set of time / value pairs between which to animate.
507 * @param [in] alpha The alpha function to apply to the overall progress.
508 * @param [in] period The effect will occur duing this time period.
509 * @param [in] interpolation The method used to interpolate between values.
511 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
514 // Actor-specific convenience methods
517 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
518 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
520 * @param[in] actor The actor to animate
521 * @param[in] path The path. It defines position and orientation
522 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
524 void Animate( Actor actor, Path path, const Vector3& forward );
527 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
528 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
530 * @param[in] actor The actor to animate
531 * @param[in] path The path. It defines position and orientation
532 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
533 * @param [in] alpha The alpha function to apply.
535 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
538 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
539 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
541 * @param[in] actor The actor to animate
542 * @param[in] path The path. It defines position and orientation
543 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
544 * @param [in] period The effect will occur during this time period.
546 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
549 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
550 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
552 * @param[in] actor The actor to animate
553 * @param[in] path The path. It defines position and orientation
554 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
555 * @param [in] alpha The alpha function to apply.
556 * @param [in] period The effect will occur during this time period.
558 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
561 * @brief Show an actor during the animation.
563 * @param [in] actor The actor to animate.
564 * @param [in] delaySeconds The initial delay from the start of the animation.
566 void Show(Actor actor, float delaySeconds);
569 * @brief Hide an actor during the animation.
571 * @param [in] actor The actor to animate.
572 * @param [in] delaySeconds The initial delay from the start of the animation.
574 void Hide(Actor actor, float delaySeconds);
576 public: // Not intended for use by Application developers
579 * @brief This constructor is used by Dali New() methods
580 * @param [in] animation A pointer to a newly allocated Dali resource
582 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
588 #endif // __DALI_ANIMATION_H__