[dali_1.2.39] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / animation / animation.h
1 #ifndef __DALI_ANIMATION_H__
2 #define __DALI_ANIMATION_H__
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // INTERNAL INCLUDES
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>
30
31 namespace Dali
32 {
33 /**
34  * @addtogroup dali_core_animation
35  * @{
36  */
37
38 class Actor;
39 struct Property;
40 struct Vector2;
41 struct Vector3;
42
43 namespace Internal DALI_INTERNAL
44 {
45 class Animation;
46 }
47
48 /**
49  * @brief Dali::Animation can be used to animate the properties of any number of objects, typically Actors.
50  *
51  * An example animation setup is shown below:
52  *
53  * @code
54  *
55  * struct MyProgram
56  * {
57  *   Actor mActor; // The object we wish to animate
58  *   Animation mAnimation; // Keep this to control the animation
59  * }
60  *
61  * // ...To play the animation
62  *
63  * mAnimation = Animation::New(3.0f); // duration 3 seconds
64  * mAnimation.AnimateTo(Property(mActor, Actor::Property::POSITION), Vector3(10.0f, 50.0f, 0.0f));
65  * mAnimation.Play();
66  *
67  * @endcode
68  *
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:
71  *
72  * @code
73  *
74  * void ExampleCallback( Animation& source )
75  * {
76  *   std::cout << "Animation has finished" << std::endl;
77  * }
78  *
79  * void ExampleAnimation( Actor actor )
80  * {
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 );
84  *   animation.Play();
85  * } // At this point the animation handle has gone out of scope
86  *
87  * Actor actor = Actor::New();
88  * Stage::GetCurrent().Add( actor );
89  *
90  * // Fire animation and forget about it
91  * ExampleAnimation( actor );
92  *
93  * // However the animation will continue, and "Animation has finished" will be printed after 2 seconds.
94  *
95  * @endcode
96  *
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.
99  *
100  * The overall animation time is superseded by the values given in the TimePeriod structure used when calling the AnimateTo(), AnimateBy(), AnimateBetween() and Animate() methods.
101  * If any of the individual calls to those functions exceeds the overall animation time, then the overall animation time is automatically extended.
102  *
103  * Signals
104  * | %Signal Name | Method                   |
105  * |--------------|--------------------------|
106  * | finished     | @ref FinishedSignal()    |
107  *
108  * Actions
109  * | %Action Name | %Animation method called |
110  * |--------------|--------------------------|
111  * | play         | Play()                   |
112  * | stop         | Stop()                   |
113  * | pause        | Pause()                  |
114  * @SINCE_1_0.0
115  */
116 class DALI_IMPORT_API Animation : public BaseHandle
117 {
118 public:
119
120   typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type @SINCE_1_0.0
121
122   typedef Any AnyFunction; ///< Interpolation function @SINCE_1_0.0
123
124   /**
125    * @brief Enumeration for what to do when the animation ends, is stopped, or is destroyed.
126    * @SINCE_1_0.0
127    */
128   enum EndAction
129   {
130     Bake,     ///< When the animation ends, the animated property values are saved. @SINCE_1_0.0
131     Discard,  ///< When the animation ends, the animated property values are forgotten. @SINCE_1_0.0
132     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   };
134
135   /**
136    * @brief Enumeration for what interpolation method to use on key-frame animations.
137    * @SINCE_1_0.0
138    */
139   enum Interpolation
140   {
141     Linear,   ///< Values in between key frames are interpolated using a linear polynomial. (Default) @SINCE_1_0.0
142     Cubic     ///< Values in between key frames are interpolated using a cubic polynomial. @SINCE_1_0.0
143   };
144
145   /**
146    * @brief Enumeration for what state the animation is in.
147    *
148    * Note: Calling Reset() on this class will NOT reset the animation. It will call BaseHandle::Reset() which drops the object handle.
149    *
150    * @SINCE_1_1.21
151    */
152   enum State
153   {
154     STOPPED,   ///< Animation has stopped @SINCE_1_1.21
155     PLAYING,   ///< The animation is playing @SINCE_1_1.21
156     PAUSED     ///< The animation is paused @SINCE_1_1.21
157   };
158
159   /**
160    * @brief Creates an uninitialized Animation; this can be initialized with Animation::New().
161    *
162    * Calling member functions with an uninitialized Animation handle is not allowed.
163    * @SINCE_1_0.0
164    */
165   Animation();
166
167   /**
168    * @brief Creates an initialized Animation.
169    *
170    * The animation will not loop.
171    * The default end action is "Bake".
172    * The default alpha function is linear.
173    * @SINCE_1_0.0
174    * @param[in] durationSeconds The duration in seconds
175    * @return A handle to a newly allocated Dali resource
176    * @note durationSeconds can not be negative.
177    */
178   static Animation New(float durationSeconds);
179
180   /**
181    * @brief Downcasts a handle to Animation handle.
182    *
183    * If handle points to an Animation object, the downcast produces valid handle.
184    * If not, the returned handle is left uninitialized.
185    *
186    * @SINCE_1_0.0
187    * @param[in] handle Handle to an object
188    * @return Handle to an Animation object or an uninitialized handle
189    */
190   static Animation DownCast( BaseHandle handle );
191
192   /**
193    * @brief Destructor.
194    *
195    * This is non-virtual since derived Handle types must not contain data or virtual methods.
196    * @SINCE_1_0.0
197    */
198   ~Animation();
199
200   /**
201    * @brief This copy constructor is required for (smart) pointer semantics.
202    *
203    * @SINCE_1_0.0
204    * @param[in] handle A reference to the copied handle
205    */
206   Animation(const Animation& handle);
207
208   /**
209    * @brief This assignment operator is required for (smart) pointer semantics.
210    *
211    * @SINCE_1_0.0
212    * @param[in] rhs A reference to the copied handle
213    * @return A reference to this
214    */
215   Animation& operator=(const Animation& rhs);
216
217   /**
218    * @brief Sets the duration of an animation.
219    *
220    * @SINCE_1_0.0
221    * @param[in] seconds The duration in seconds
222    * @pre DurationSeconds must be greater than zero.
223    */
224   void SetDuration(float seconds);
225
226   /**
227    * @brief Retrieves the duration of an animation.
228    *
229    * @SINCE_1_0.0
230    * @return The duration in seconds
231    */
232   float GetDuration() const;
233
234   /**
235    * @brief Sets whether the animation will loop.
236    *
237    * This function resets the loop count and should not be used with SetLoopCount(int).
238    * Setting this parameter does not cause the animation to Play().
239    *
240    * @SINCE_1_0.0
241    * @param[in] looping True if the animation will loop
242    */
243   void SetLooping(bool looping);
244
245   /**
246    * @brief Enables looping for 'count' repeats.
247    *
248    * A zero is the same as SetLooping(true) i.e. repeat forever.
249    * If Play() Stop() or 'count' loops is reached, the loop counter will reset.
250    * Setting this parameter does not cause the animation to Play().
251    *
252    * @SINCE_1_1.20
253    * @param[in] count The number of times to loop
254    */
255   void SetLoopCount(int count);
256
257   /**
258    * @brief Gets the loop count.
259    *
260    * A zero is the same as SetLooping(true) ie repeat forever.
261    * The loop count is initially 1 for play once.
262    *
263    * @SINCE_1_1.20
264    * @return The number of times to loop
265    */
266   int GetLoopCount();
267
268   /**
269    * @brief Gets the current loop count.
270    *
271    * A value 0 to GetLoopCount() indicating the current loop count when looping.
272    *
273    * @SINCE_1_1.20
274    * @return The current number of loops that have occured
275    */
276   int GetCurrentLoop();
277
278   /**
279    * @brief Queries whether the animation will loop.
280    *
281    * @SINCE_1_0.0
282    * @return True if the animation will loop
283    */
284   bool IsLooping() const;
285
286   /**
287    * @brief Sets the end action of the animation.
288    *
289    * This action is performed when the animation ends or if it is stopped.
290    * Default end action is bake.
291    * @SINCE_1_0.0
292    * @param[in] action The end action
293    */
294   void SetEndAction(EndAction action);
295
296   /**
297    * @brief Returns the end action of the animation.
298    *
299    * @SINCE_1_0.0
300    * @return The end action
301    */
302   EndAction GetEndAction() const;
303
304   /**
305    * @brief Sets the disconnect action.
306    *
307    * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
308    * Default action is to BakeFinal.
309    * @SINCE_1_0.0
310    * @param[in] disconnectAction The disconnect action
311    */
312   void SetDisconnectAction( EndAction disconnectAction );
313
314   /**
315    * @brief Returns the disconnect action.
316    *
317    * @SINCE_1_0.0
318    * @return The disconnect action
319    */
320   EndAction GetDisconnectAction() const;
321
322   /**
323    * @brief Sets the default alpha function for an animation.
324    *
325    * This is applied to individual property animations, if no further alpha functions are supplied.
326    * @SINCE_1_0.0
327    * @param[in] alpha The default alpha function
328    */
329   void SetDefaultAlphaFunction(AlphaFunction alpha);
330
331   /**
332    * @brief Retrieves the default alpha function for an animation.
333    *
334    * @SINCE_1_0.0
335    * @return The default alpha function
336    */
337   AlphaFunction GetDefaultAlphaFunction() const;
338
339   /*
340    * @brief Sets the progress of the animation.
341    *
342    * The animation will play (or continue playing) from this point. The progress
343    * must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
344    * otherwise, it will be ignored.
345    *
346    * @SINCE_1_0.0
347    * @param[in] progress The new progress as a normalized value between [0,1]
348    * or between the play range if specified
349    */
350   void SetCurrentProgress( float progress );
351
352   /**
353   * @brief Retrieves the current progress of the animation.
354   *
355   * @SINCE_1_0.0
356   * @return The current progress as a normalized value between [0,1]
357   */
358   float GetCurrentProgress();
359
360   /**
361    * @brief Specifies a speed factor for the animation.
362    *
363    * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
364    * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
365    * to play the animation in reverse.
366    *
367    * @SINCE_1_0.0
368    * @param[in] factor A value which will multiply the velocity
369    */
370   void SetSpeedFactor( float factor );
371
372   /**
373    * @brief Retrieves the speed factor of the animation.
374    *
375    * @SINCE_1_0.0
376    * @return Speed factor
377    */
378   float GetSpeedFactor() const;
379
380   /**
381    * @brief Sets the playing range.
382    *
383    * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
384    * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
385    *
386    * @SINCE_1_0.0
387    * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
388    * animation will play between those values
389    */
390   void SetPlayRange( const Vector2& range );
391
392   /**
393    * @brief Gets the playing range.
394    *
395    * @SINCE_1_0.0
396    * @return The play range defined for the animation
397    */
398   Vector2 GetPlayRange() const;
399
400   /**
401    * @brief Play the animation.
402    * @SINCE_1_0.0
403    */
404   void Play();
405
406   /**
407    * @brief Plays the animation from a given point.
408    *
409    * The progress must be in the 0-1 interval or in the play range interval if defined ( See @ref Animation::SetPlayRange ),
410    * otherwise, it will be ignored.
411    *
412    * @SINCE_1_0.0
413    * @param[in] progress A value between [0,1], or between the play range if specified, from where the animation should start playing
414    */
415   void PlayFrom( float progress );
416
417   /**
418    * @brief Pauses the animation.
419    * @SINCE_1_0.0
420    */
421   void Pause();
422
423   /**
424    * @brief Queries the state of the animation.
425    * @SINCE_1_1.21
426    * @return The Animation::State
427    */
428   State GetState() const;
429
430   /**
431    * @brief Stops the animation.
432    * @SINCE_1_0.0
433    */
434   void Stop();
435
436   /**
437    * @brief Clears the animation.
438    *
439    * This disconnects any objects that were being animated, effectively stopping the animation.
440    * @SINCE_1_0.0
441    */
442   void Clear();
443
444   /**
445    * @brief Connects to this signal to be notified when an Animation's animations have finished.
446    *
447    * @SINCE_1_0.0
448    * @return A signal object to connect with
449    */
450   AnimationSignalType& FinishedSignal();
451
452   /**
453    * @brief Animates a property value by a relative amount.
454    *
455    * The default alpha function will be used.
456    * The effect will start & end when the animation begins & ends.
457    * @SINCE_1_0.0
458    * @param[in] target The target object/property to animate
459    * @param[in] relativeValue The property value will change by this amount
460    */
461   void AnimateBy(Property target, Property::Value relativeValue);
462
463   /**
464    * @brief Animates a property value by a relative amount.
465    *
466    * The effect will start & end when the animation begins & ends.
467    * @SINCE_1_0.0
468    * @param[in] target The target object/property to animate
469    * @param[in] relativeValue The property value will change by this amount
470    * @param[in] alpha The alpha function to apply
471    */
472   void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
473
474   /**
475    * @brief Animates a property value by a relative amount.
476    *
477    * The default alpha function will be used.
478    * @SINCE_1_0.0
479    * @param[in] target The target object/property to animate
480    * @param[in] relativeValue The property value will increase/decrease by this amount
481    * @param[in] period The effect will occur during this time period
482    */
483   void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
484
485   /**
486    * @brief Animates a property value by a relative amount.
487    *
488    * @SINCE_1_0.0
489    * @param[in] target The target object/property to animate
490    * @param[in] relativeValue The property value will increase/decrease by this amount
491    * @param[in] alpha The alpha function to apply
492    * @param[in] period The effect will occur during this time period
493    */
494   void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
495
496   /**
497    * @brief Animates a property to a destination value.
498    *
499    * The default alpha function will be used.
500    * The effect will start & end when the animation begins & ends.
501    * @SINCE_1_0.0
502    * @param[in] target The target object/property to animate
503    * @param[in] destinationValue The destination value
504    */
505   void AnimateTo(Property target, Property::Value destinationValue);
506
507   /**
508    * @brief Animates a property to a destination value.
509    *
510    * The effect will start & end when the animation begins & ends.
511    * @SINCE_1_0.0
512    * @param[in] target The target object/property to animate
513    * @param[in] destinationValue The destination value
514    * @param[in] alpha The alpha function to apply
515    */
516   void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
517
518   /**
519    * @brief Animates a property to a destination value.
520    *
521    * The default alpha function will be used.
522    * @SINCE_1_0.0
523    * @param[in] target The target object/property to animate
524    * @param[in] destinationValue The destination value
525    * @param[in] period The effect will occur during this time period
526    */
527   void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
528
529   /**
530    * @brief Animates a property to a destination value.
531    *
532    * @SINCE_1_0.0
533    * @param[in] target The target object/property to animate
534    * @param[in] destinationValue The destination value
535    * @param[in] alpha The alpha function to apply
536    * @param[in] period The effect will occur during this time period
537    */
538   void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
539
540    /**
541    * @brief Animates a property between keyframes.
542    *
543    * @SINCE_1_0.0
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    */
547   void AnimateBetween(Property target, KeyFrames& keyFrames);
548
549   /**
550    * @brief Animates a property between keyframes.
551    *
552    * @SINCE_1_0.0
553    * @param[in] target The target object property to animate
554    * @param[in] keyFrames The set of time/value pairs between which to animate
555    * @param[in] interpolation The method used to interpolate between values
556    */
557   void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
558
559   /**
560    * @brief Animates a property between keyframes.
561    *
562    * @SINCE_1_0.0
563    * @param[in] target The target object property to animate
564    * @param[in] keyFrames The set of time/value pairs between which to animate
565    * @param[in] alpha The alpha function to apply
566    */
567   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
568
569   /**
570    * @brief Animates a property between keyframes.
571    *
572    * @SINCE_1_0.0
573    * @param[in] target The target object property to animate
574    * @param[in] keyFrames The set of time/value pairs between which to animate
575    * @param[in] alpha The alpha function to apply
576    * @param[in] interpolation The method used to interpolate between values
577    */
578   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
579
580   /**
581    * @brief Animates a property between keyframes.
582    *
583    * @SINCE_1_0.0
584    * @param[in] target The target object property to animate
585    * @param[in] keyFrames The set of time/value pairs between which to animate
586    * @param[in] period The effect will occur during this time period
587    */
588   void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
589
590   /**
591    * @brief Animates a property between keyframes.
592    *
593    * @SINCE_1_0.0
594    * @param[in] target The target object property to animate
595    * @param[in] keyFrames The set of time/value pairs between which to animate
596    * @param[in] period The effect will occur duing this time period
597    * @param[in] interpolation The method used to interpolate between values
598    */
599   void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
600
601   /**
602    * @brief Animates a property between keyframes.
603    *
604    * @SINCE_1_0.0
605    * @param[in] target The target object property to animate
606    * @param[in] keyFrames The set of time/value pairs between which to animate
607    * @param[in] alpha The alpha function to apply
608    * @param[in] period The effect will occur during this time period
609    */
610   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
611
612   /**
613    * @brief Animates a property between keyframes.
614    *
615    * @SINCE_1_0.0
616    * @param[in] target The target object property to animate
617    * @param[in] keyFrames The set of time/value pairs between which to animate
618    * @param[in] alpha The alpha function to apply to the overall progress
619    * @param[in] period The effect will occur duing this time period
620    * @param[in] interpolation The method used to interpolate between values
621    */
622   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
623
624
625   // Actor-specific convenience methods
626
627   /**
628    * @brief Animates an actor's position and orientation through a predefined path.
629    *
630    * The actor will rotate to orient the supplied
631    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
632    *
633    * @SINCE_1_0.0
634    * @param[in] actor The actor to animate
635    * @param[in] path The path. It defines position and orientation
636    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
637    */
638   void Animate( Actor actor, Path path, const Vector3& forward );
639
640   /**
641    * @brief Animates an actor's position and orientation through a predefined path.
642    *
643    * The actor will rotate to orient the supplied
644    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
645    *
646    * @SINCE_1_0.0
647    * @param[in] actor The actor to animate
648    * @param[in] path The path. It defines position and orientation
649    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
650    * @param[in] alpha The alpha function to apply
651    */
652   void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
653
654   /**
655    * @brief Animates an actor's position and orientation through a predefined path.
656    *
657    * The actor will rotate to orient the supplied
658    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
659    *
660    * @SINCE_1_0.0
661    * @param[in] actor The actor to animate
662    * @param[in] path The path. It defines position and orientation
663    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
664    * @param[in] period The effect will occur during this time period
665    */
666   void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
667
668   /**
669    * @brief Animates an actor's position and orientation through a predefined path.
670    *
671    * The actor will rotate to orient the supplied
672    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
673    *
674    * @SINCE_1_0.0
675    * @param[in] actor The actor to animate
676    * @param[in] path The path. It defines position and orientation
677    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
678    * @param[in] alpha The alpha function to apply
679    * @param[in] period The effect will occur during this time period
680    */
681   void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
682
683   /**
684    * @brief Shows an actor during the animation.
685    *
686    * @SINCE_1_0.0
687    * @param[in] actor The actor to animate
688    * @param[in] delaySeconds The initial delay from the start of the animation
689    */
690   void Show(Actor actor, float delaySeconds);
691
692   /**
693    * @brief Hides an actor during the animation.
694    *
695    * @SINCE_1_0.0
696    * @param[in] actor The actor to animate
697    * @param[in] delaySeconds The initial delay from the start of the animation
698    */
699   void Hide(Actor actor, float delaySeconds);
700
701 public: // Not intended for use by Application developers
702
703   /// @cond internal
704   /**
705    * @brief This constructor is used by Animation::New() methods.
706    * @SINCE_1_0.0
707    * @param[in] animation A pointer to a newly allocated Dali resource
708    */
709   explicit DALI_INTERNAL Animation(Internal::Animation* animation);
710   /// @endcond
711
712 };
713
714 /**
715  * @}
716  */
717 } // namespace Dali
718
719 #endif // __DALI_ANIMATION_H__