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-functions.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.
265 * The animation will play (or continue playing) from this point. The progress
266 * must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
267 * otherwise, it will be ignored.
269 * @param[in] progress The new progress as a normalized value between [0,1] or between the
270 * play range if specified.
272 void SetCurrentProgress( float progress );
275 * @brief Retrieve the current progress of the animation.
277 * @return The current progress as a normalized value between [0,1].
279 float GetCurrentProgress();
282 * @brief Specifies an speed factor for the animation.
284 * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
285 * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
286 * to play the animation in reverse.
288 * @param[in] factor A value which will multiply the velocity.
290 void SetSpeedFactor( float factor );
293 * @brief Retrieve the speed factor of the animation
295 * @return speed factor
297 float GetSpeedFactor() const;
300 * @brief Set the playing range.
301 * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
302 * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
304 * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
305 * animation will play between those values.
307 void SetPlayRange( const Vector2& range );
310 * @brief Get the playing range
312 * @return The play range defined for the animation.
314 Vector2 GetPlayRange() const;
317 * @brief Play the animation.
322 * @brief Play the animation from a given point.
323 * The progress must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
324 * otherwise, it will be ignored.
326 * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
328 void PlayFrom( float progress );
331 * @brief Pause the animation.
336 * @brief Stop the animation.
341 * @brief Clear the animation.
343 * This disconnects any objects that were being animated, effectively stopping the animation.
348 * @brief Connect to this signal to be notified when an Animation's animations have finished.
350 * @return A signal object to Connect() with.
352 AnimationSignalType& FinishedSignal();
355 * @brief Animate a property value by a relative amount.
357 * The default alpha function will be used.
358 * The effect will start & end when the animation begins & ends.
359 * @param [in] target The target object/property to animate.
360 * @param [in] relativeValue The property value will change by this amount.
362 void AnimateBy(Property target, Property::Value relativeValue);
365 * @brief Animate a property value by a relative amount.
367 * The effect will start & end when the animation begins & ends.
368 * @param [in] target The target object/property to animate.
369 * @param [in] relativeValue The property value will change by this amount.
370 * @param [in] alpha The alpha function to apply.
372 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
375 * @brief Animate a property value by a relative amount.
377 * The default alpha function will be used.
378 * @param [in] target The target object/property to animate.
379 * @param [in] relativeValue The property value will increase/decrease by this amount.
380 * @param [in] period The effect will occur during this time period.
382 void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
385 * @brief Animate a property value by a relative amount.
387 * @param [in] target The target object/property to animate.
388 * @param [in] relativeValue The property value will increase/decrease by this amount.
389 * @param [in] alpha The alpha function to apply.
390 * @param [in] period The effect will occur during this time period.
392 void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
395 * @brief Animate a property to a destination value.
397 * The default alpha function will be used.
398 * The effect will start & end when the animation begins & ends.
399 * @param [in] target The target object/property to animate.
400 * @param [in] destinationValue The destination value.
402 void AnimateTo(Property target, Property::Value destinationValue);
405 * @brief Animate a property to a destination value.
407 * The effect will start & end when the animation begins & ends.
408 * @param [in] target The target object/property to animate.
409 * @param [in] destinationValue The destination value.
410 * @param [in] alpha The alpha function to apply.
412 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
415 * @brief Animate a property to a destination value.
417 * The default alpha function will be used.
418 * @param [in] target The target object/property to animate.
419 * @param [in] destinationValue The destination value.
420 * @param [in] period The effect will occur during this time period.
422 void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
425 * @brief Animate a property to a destination value.
427 * @param [in] target The target object/property to animate.
428 * @param [in] destinationValue The destination value.
429 * @param [in] alpha The alpha function to apply.
430 * @param [in] period The effect will occur during this time period.
432 void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
435 * @brief Animate a property between keyframes.
437 * @param [in] target The target object/property to animate.
438 * @param [in] keyFrames The key frames
440 void AnimateBetween(Property target, KeyFrames& keyFrames);
443 * @brief Animate a property between keyframes.
445 * @param [in] target The target object + property to animate
446 * @param [in] keyFrames The set of time / value pairs between which to animate.
447 * @param [in] interpolation The method used to interpolate between values.
449 void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
452 * @brief Animate a property between keyframes.
454 * @param [in] target The target object/property to animate.
455 * @param [in] keyFrames The key frames
456 * @param [in] alpha The alpha function to apply.
458 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
461 * @brief Animate a property between keyframes.
463 * @param [in] target The target object + property to animate
464 * @param [in] keyFrames The set of time / value pairs between which to animate.
465 * @param [in] alpha The alpha function to apply.
466 * @param [in] interpolation The method used to interpolate between values.
468 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
471 * @brief Animate a property between keyframes.
473 * @param [in] target The target object/property to animate.
474 * @param [in] keyFrames The key frames
475 * @param [in] period The effect will occur during this time period.
477 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
480 * @brief Animate a property between keyframes.
482 * @param [in] target The target object + property to animate
483 * @param [in] keyFrames The set of time / value pairs between which to animate.
484 * @param [in] period The effect will occur duing this time period.
485 * @param [in] interpolation The method used to interpolate between values.
487 void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
490 * @brief Animate a property between keyframes.
492 * @param [in] target The target object/property to animate.
493 * @param [in] keyFrames The key frames
494 * @param [in] alpha The alpha function to apply.
495 * @param [in] period The effect will occur during this time period.
497 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
500 * @brief Animate a property between keyframes.
502 * @param [in] target The target object + property to animate
503 * @param [in] keyFrames The set of time / value pairs between which to animate.
504 * @param [in] alpha The alpha function to apply to the overall progress.
505 * @param [in] period The effect will occur duing this time period.
506 * @param [in] interpolation The method used to interpolate between values.
508 void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
511 // Actor-specific convenience methods
514 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
515 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
517 * @param[in] actor The actor to animate
518 * @param[in] path The path. It defines position and orientation
519 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
521 void Animate( Actor actor, Path path, const Vector3& forward );
524 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
525 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
527 * @param[in] actor The actor to animate
528 * @param[in] path The path. It defines position and orientation
529 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
530 * @param [in] alpha The alpha function to apply.
532 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
535 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
536 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
538 * @param[in] actor The actor to animate
539 * @param[in] path The path. It defines position and orientation
540 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
541 * @param [in] period The effect will occur during this time period.
543 void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
546 * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
547 * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
549 * @param[in] actor The actor to animate
550 * @param[in] path The path. It defines position and orientation
551 * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
552 * @param [in] alpha The alpha function to apply.
553 * @param [in] period The effect will occur during this time period.
555 void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
558 * @brief Show an actor during the animation.
560 * @param [in] actor The actor to animate.
561 * @param [in] delaySeconds The initial delay from the start of the animation.
563 void Show(Actor actor, float delaySeconds);
566 * @brief Hide an actor during the animation.
568 * @param [in] actor The actor to animate.
569 * @param [in] delaySeconds The initial delay from the start of the animation.
571 void Hide(Actor actor, float delaySeconds);
573 public: // Not intended for use by Application developers
576 * @brief This constructor is used by Dali New() methods
577 * @param [in] animation A pointer to a newly allocated Dali resource
579 explicit DALI_INTERNAL Animation(Internal::Animation* animation);
585 #endif // __DALI_ANIMATION_H__