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() |
112 class DALI_IMPORT_API Animation : public BaseHandle
116 typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type
118 typedef Any AnyFunction; ///< Interpolation function
121 * @brief What to do when the animation ends, is stopped or is destroyed
125 Bake, ///< When the animation ends, the animated property values are saved.
126 Discard, ///< When the animation ends, the animated property values are forgotten.
127 BakeFinal ///< If the animation is stopped, the animated property values are saved as if the animation had run to completion, otherwise behaves like Bake.
131 * @brief What interpolation method to use on key-frame animations
135 Linear, ///< Values in between key frames are interpolated using a linear polynomial. (Default)
136 Cubic ///< Values in between key frames are interpolated using a cubic polynomial.
140 * @brief Create an uninitialized Animation; this can be initialized with Animation::New().
142 * Calling member functions with an uninitialized Dali::Object is not allowed.
147 * @brief Create an initialized Animation.
149 * The animation will not loop.
150 * The default end action is "Bake".
151 * The default alpha function is linear.
152 * @pre durationSeconds must be greater than zero.
153 * @param [in] durationSeconds The duration in seconds.
154 * @return A handle to a newly allocated Dali resource.
156 static Animation New(float durationSeconds);
159 * @brief Downcast an Object handle to Animation.
161 * If handle points to an Animation object the downcast produces
162 * valid handle. If not the returned handle is left uninitialized.
164 * @param[in] handle to An object
165 * @return handle to a Animation object or an uninitialized handle
167 static Animation DownCast( BaseHandle handle );
172 * This is non-virtual since derived Handle types must not contain data or virtual methods.
177 * @brief This copy constructor is required for (smart) pointer semantics.
179 * @param [in] handle A reference to the copied handle
181 Animation(const Animation& handle);
184 * @brief This assignment operator is required for (smart) pointer semantics.
186 * @param [in] rhs A reference to the copied handle
187 * @return A reference to this
189 Animation& operator=(const Animation& rhs);
192 * @brief Set the duration of an animation.
194 * @pre durationSeconds must be greater than zero.
195 * @param[in] seconds The duration in seconds.
197 void SetDuration(float seconds);
200 * @brief Retrieve the duration of an animation.
202 * @return The duration in seconds.
204 float GetDuration() const;
207 * @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.
216 * @return True if the animation will loop.
218 bool IsLooping() const;
221 * @brief Set the end action of the animation.
223 * This action is performed when the animation ends or if it is stopped.
224 * Default end action is bake
225 * @param[in] action The end action.
227 void SetEndAction(EndAction action);
230 * @brief Returns the end action of the animation.
232 * @return The end action.
234 EndAction GetEndAction() const;
237 * @brief Set the disconnect action.
239 * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
240 * Default action is to BakeFinal.
241 * @param[in] disconnectAction The disconnect action.
243 void SetDisconnectAction( EndAction disconnectAction );
246 * @brief Returns the disconnect action.
248 * @return The disconnect action.
250 EndAction GetDisconnectAction() const;
253 * @brief Set the default alpha function for an animation.
255 * This is applied to individual property animations, if no further alpha functions are supplied.
256 * @param[in] alpha The default alpha function.
258 void SetDefaultAlphaFunction(AlphaFunction alpha);
261 * @brief Retrieve the default alpha function for an animation.
263 * @return The default alpha function.
265 AlphaFunction GetDefaultAlphaFunction() const;
268 * @brief Sets the progress of the animation.
270 * When played, the animation will start from this point.
271 * If playing, the animation will jump to, and continue playing from this point.
273 * The progress must be in the 0-1 interval or in the play range interval
274 * if defined ( See SetPlayRange ), otherwise, it will be ignored.
276 * @param[in] progress The new progress as a normalized value between [0,1]
277 * or between the play range if specified.
279 void SetCurrentProgress( float progress );
282 * @brief Retrieve the current progress of the animation.
284 * @return The current progress as a normalized value between [0,1].
286 float GetCurrentProgress();
289 * @brief Specifies an speed factor for the animation.
291 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
292 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
293 * to play the animation in reverse.
295 * @param[in] factor A value which will multiply the velocity.
297 void SetSpeedFactor( float factor );
300 * @brief Retrieve the speed factor of the animation
302 * @return speed factor
304 float GetSpeedFactor() const;
307 * @brief Set the playing range.
308 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
309 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
311 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
312 * animation will play between those values.
314 void SetPlayRange( const Vector2& range );
317 * @brief Get the playing range
319 * @return The play range defined for the animation.
321 Vector2 GetPlayRange() const;
324 * @brief Play the animation.
329 * @brief Play the animation from a given point.
330 * The progress must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
331 * otherwise, it will be ignored.
333 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
335 void PlayFrom( float progress );
338 * @brief Pause the animation.
343 * @brief Stop the animation.
348 * @brief Clear the animation.
350 * This disconnects any objects that were being animated, effectively stopping the animation.
355 * @brief Connect to this signal to be notified when an Animation's animations have finished.
357 * @return A signal object to Connect() with.
359 AnimationSignalType& FinishedSignal();
362 * @brief Animate a property value by a relative amount.
364 * The default alpha function will be used.
365 * The effect will start & end when the animation begins & ends.
366 * @param [in] target The target object/property to animate.
367 * @param [in] relativeValue The property value will change by this amount.
369 void AnimateBy(Property target, Property::Value relativeValue);
372 * @brief Animate a property value by a relative amount.
374 * The effect will start & end when the animation begins & ends.
375 * @param [in] target The target object/property to animate.
376 * @param [in] relativeValue The property value will change by this amount.
377 * @param [in] alpha The alpha function to apply.
379 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
382 * @brief Animate a property value by a relative amount.
384 * The default alpha function will be used.
385 * @param [in] target The target object/property to animate.
386 * @param [in] relativeValue The property value will increase/decrease by this amount.
387 * @param [in] period The effect will occur during this time period.
389 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
392 * @brief Animate a property value by a relative amount.
394 * @param [in] target The target object/property to animate.
395 * @param [in] relativeValue The property value will increase/decrease by this amount.
396 * @param [in] alpha The alpha function to apply.
397 * @param [in] period The effect will occur during this time period.
399 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
402 * @brief Animate a property to a destination value.
404 * The default alpha function will be used.
405 * The effect will start & end when the animation begins & ends.
406 * @param [in] target The target object/property to animate.
407 * @param [in] destinationValue The destination value.
409 void AnimateTo(Property target, Property::Value destinationValue);
412 * @brief Animate a property to a destination value.
414 * The effect will start & end when the animation begins & ends.
415 * @param [in] target The target object/property to animate.
416 * @param [in] destinationValue The destination value.
417 * @param [in] alpha The alpha function to apply.
419 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
422 * @brief Animate a property to a destination value.
424 * The default alpha function will be used.
425 * @param [in] target The target object/property to animate.
426 * @param [in] destinationValue The destination value.
427 * @param [in] period The effect will occur during this time period.
429 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
432 * @brief Animate a property to a destination value.
434 * @param [in] target The target object/property to animate.
435 * @param [in] destinationValue The destination value.
436 * @param [in] alpha The alpha function to apply.
437 * @param [in] period The effect will occur during this time period.
439 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
442 * @brief Animate a property between keyframes.
444 * @param [in] target The target object/property to animate.
445 * @param [in] keyFrames The key frames
447 void AnimateBetween(Property target, KeyFrames& keyFrames);
450 * @brief Animate a property between keyframes.
452 * @param [in] target The target object + property to animate
453 * @param [in] keyFrames The set of time / value pairs between which to animate.
454 * @param [in] interpolation The method used to interpolate between values.
456 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
459 * @brief Animate a property between keyframes.
461 * @param [in] target The target object/property to animate.
462 * @param [in] keyFrames The key frames
463 * @param [in] alpha The alpha function to apply.
465 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
468 * @brief Animate a property between keyframes.
470 * @param [in] target The target object + property to animate
471 * @param [in] keyFrames The set of time / value pairs between which to animate.
472 * @param [in] alpha The alpha function to apply.
473 * @param [in] interpolation The method used to interpolate between values.
475 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
478 * @brief Animate a property between keyframes.
480 * @param [in] target The target object/property to animate.
481 * @param [in] keyFrames The key frames
482 * @param [in] period The effect will occur during this time period.
484 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
487 * @brief Animate a property between keyframes.
489 * @param [in] target The target object + property to animate
490 * @param [in] keyFrames The set of time / value pairs between which to animate.
491 * @param [in] period The effect will occur duing this time period.
492 * @param [in] interpolation The method used to interpolate between values.
494 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
497 * @brief Animate a property between keyframes.
499 * @param [in] target The target object/property to animate.
500 * @param [in] keyFrames The key frames
501 * @param [in] alpha The alpha function to apply.
502 * @param [in] period The effect will occur during this time period.
504 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
507 * @brief Animate a property between keyframes.
509 * @param [in] target The target object + property to animate
510 * @param [in] keyFrames The set of time / value pairs between which to animate.
511 * @param [in] alpha The alpha function to apply to the overall progress.
512 * @param [in] period The effect will occur duing this time period.
513 * @param [in] interpolation The method used to interpolate between values.
515 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
518 // Actor-specific convenience methods
521 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
522 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
524 * @param[in] actor The actor to animate
525 * @param[in] path The path. It defines position and orientation
526 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
528 void Animate( Actor actor, Path path, const Vector3& forward );
531 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
532 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
534 * @param[in] actor The actor to animate
535 * @param[in] path The path. It defines position and orientation
536 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
537 * @param [in] alpha The alpha function to apply.
539 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
542 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
543 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
545 * @param[in] actor The actor to animate
546 * @param[in] path The path. It defines position and orientation
547 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
548 * @param [in] period The effect will occur during this time period.
550 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
553 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
554 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
556 * @param[in] actor The actor to animate
557 * @param[in] path The path. It defines position and orientation
558 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
559 * @param [in] alpha The alpha function to apply.
560 * @param [in] period The effect will occur during this time period.
562 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
565 * @brief Show an actor during the animation.
567 * @param [in] actor The actor to animate.
568 * @param [in] delaySeconds The initial delay from the start of the animation.
570 void Show(Actor actor, float delaySeconds);
573 * @brief Hide an actor during the animation.
575 * @param [in] actor The actor to animate.
576 * @param [in] delaySeconds The initial delay from the start of the animation.
578 void Hide(Actor actor, float delaySeconds);
580 public: // Not intended for use by Application developers
583 * @brief This constructor is used by Dali New() methods
584 * @param [in] animation A pointer to a newly allocated Dali resource
586 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
595 #endif // __DALI_ANIMATION_H__