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