Doxygen grouping
[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  */
112 class DALI_IMPORT_API Animation : public BaseHandle
113 {
114 public:
115
116   typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type
117
118   typedef Any AnyFunction; ///< Interpolation function
119
120   /**
121    * @brief What to do when the animation ends, is stopped or is destroyed
122    */
123   enum EndAction
124   {
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.
128   };
129
130   /**
131    * @brief What interpolation method to use on key-frame animations
132    */
133   enum Interpolation
134   {
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.
137   };
138
139   /**
140    * @brief Create an uninitialized Animation; this can be initialized with Animation::New().
141    *
142    * Calling member functions with an uninitialized Dali::Object is not allowed.
143    */
144   Animation();
145
146   /**
147    * @brief Create an initialized Animation.
148    *
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.
155    */
156   static Animation New(float durationSeconds);
157
158   /**
159    * @brief Downcast an Object handle to Animation.
160    *
161    * If handle points to an Animation object the downcast produces
162    * valid handle. If not the returned handle is left uninitialized.
163    *
164    * @param[in] handle to An object
165    * @return handle to a Animation object or an uninitialized handle
166    */
167   static Animation DownCast( BaseHandle handle );
168
169   /**
170    * @brief Destructor
171    *
172    * This is non-virtual since derived Handle types must not contain data or virtual methods.
173    */
174   ~Animation();
175
176   /**
177    * @brief This copy constructor is required for (smart) pointer semantics.
178    *
179    * @param [in] handle A reference to the copied handle
180    */
181   Animation(const Animation& handle);
182
183   /**
184    * @brief This assignment operator is required for (smart) pointer semantics.
185    *
186    * @param [in] rhs  A reference to the copied handle
187    * @return A reference to this
188    */
189   Animation& operator=(const Animation& rhs);
190
191   /**
192    * @brief Set the duration of an animation.
193    *
194    * @pre durationSeconds must be greater than zero.
195    * @param[in] seconds The duration in seconds.
196    */
197   void SetDuration(float seconds);
198
199   /**
200    * @brief Retrieve the duration of an animation.
201    *
202    * @return The duration in seconds.
203    */
204   float GetDuration() const;
205
206   /**
207    * @brief Set whether the animation will loop.
208    *
209    * @param[in] looping True if the animation will loop.
210    */
211   void SetLooping(bool looping);
212
213   /**
214    * @brief Query whether the animation will loop.
215    *
216    * @return True if the animation will loop.
217    */
218   bool IsLooping() const;
219
220   /**
221    * @brief Set the end action of the animation.
222    *
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.
226    */
227   void SetEndAction(EndAction action);
228
229   /**
230    * @brief Returns the end action of the animation.
231    *
232    * @return The end action.
233    */
234   EndAction GetEndAction() const;
235
236   /**
237    * @brief Set the disconnect action.
238    *
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.
242    */
243   void SetDisconnectAction( EndAction disconnectAction );
244
245   /**
246    * @brief Returns the disconnect action.
247    *
248    * @return The disconnect action.
249    */
250   EndAction GetDisconnectAction() const;
251
252   /**
253    * @brief Set the default alpha function for an animation.
254    *
255    * This is applied to individual property animations, if no further alpha functions are supplied.
256    * @param[in] alpha The default alpha function.
257    */
258   void SetDefaultAlphaFunction(AlphaFunction alpha);
259
260   /**
261    * @brief Retrieve the default alpha function for an animation.
262    *
263    * @return The default alpha function.
264    */
265   AlphaFunction GetDefaultAlphaFunction() const;
266
267   /*
268    * @brief Sets the progress of the animation.
269    *
270    * When played, the animation will start from this point.
271    * If playing, the animation will jump to, and continue playing from this point.
272    *
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.
275    *
276    * @param[in] progress The new progress as a normalized value between [0,1]
277    * or between the play range if specified.
278    */
279   void SetCurrentProgress( float progress );
280
281   /**
282   * @brief Retrieve the current progress of the animation.
283   *
284   * @return The current progress as a normalized value between [0,1].
285   */
286   float GetCurrentProgress();
287
288   /**
289    * @brief Specifies an speed factor for the animation.
290    *
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.
294    *
295    * @param[in] factor A value which will multiply the velocity.
296    */
297   void SetSpeedFactor( float factor );
298
299   /**
300    * @brief Retrieve the speed factor of the animation
301    *
302    * @return speed factor
303    */
304   float GetSpeedFactor() const;
305
306   /**
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.
310    *
311    * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
312    * animation will play between those values.
313    */
314   void SetPlayRange( const Vector2& range );
315
316   /**
317    * @brief Get the playing range
318    *
319    * @return The play range defined for the animation.
320    */
321   Vector2 GetPlayRange() const;
322
323   /**
324    * @brief Play the animation.
325    */
326   void Play();
327
328   /**
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.
332    *
333    * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
334    */
335   void PlayFrom( float progress );
336
337   /**
338    * @brief Pause the animation.
339    */
340   void Pause();
341
342   /**
343    * @brief Stop the animation.
344    */
345   void Stop();
346
347   /**
348    * @brief Clear the animation.
349    *
350    * This disconnects any objects that were being animated, effectively stopping the animation.
351    */
352   void Clear();
353
354   /**
355    * @brief Connect to this signal to be notified when an Animation's animations have finished.
356    *
357    * @return A signal object to Connect() with.
358    */
359   AnimationSignalType& FinishedSignal();
360
361   /**
362    * @brief Animate a property value by a relative amount.
363    *
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.
368    */
369   void AnimateBy(Property target, Property::Value relativeValue);
370
371   /**
372    * @brief Animate a property value by a relative amount.
373    *
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.
378    */
379   void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
380
381   /**
382    * @brief Animate a property value by a relative amount.
383    *
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.
388    */
389   void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
390
391   /**
392    * @brief Animate a property value by a relative amount.
393    *
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.
398    */
399   void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
400
401   /**
402    * @brief Animate a property to a destination value.
403    *
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.
408    */
409   void AnimateTo(Property target, Property::Value destinationValue);
410
411   /**
412    * @brief Animate a property to a destination value.
413    *
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.
418    */
419   void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
420
421   /**
422    * @brief Animate a property to a destination value.
423    *
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.
428    */
429   void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
430
431   /**
432    * @brief Animate a property to a destination value.
433    *
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.
438    */
439   void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
440
441    /**
442    * @brief Animate a property between keyframes.
443    *
444    * @param [in] target The target object/property to animate.
445    * @param [in] keyFrames The key frames
446    */
447   void AnimateBetween(Property target, KeyFrames& keyFrames);
448
449   /**
450    * @brief Animate a property between keyframes.
451    *
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.
455    */
456   void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
457
458   /**
459    * @brief Animate a property between keyframes.
460    *
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.
464    */
465   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
466
467   /**
468    * @brief Animate a property between keyframes.
469    *
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.
474    */
475   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
476
477   /**
478    * @brief Animate a property between keyframes.
479    *
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.
483    */
484   void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
485
486   /**
487    * @brief Animate a property between keyframes.
488    *
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.
493    */
494   void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
495
496   /**
497    * @brief Animate a property between keyframes.
498    *
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.
503    */
504   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
505
506   /**
507    * @brief Animate a property between keyframes.
508    *
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.
514    */
515   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
516
517
518   // Actor-specific convenience methods
519
520   /**
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.
523    *
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
527    */
528   void Animate( Actor actor, Path path, const Vector3& forward );
529
530   /**
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.
533    *
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.
538    */
539   void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
540
541   /**
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.
544    *
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.
549    */
550   void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
551
552   /**
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.
555    *
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.
561    */
562   void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
563
564   /**
565    * @brief Show an actor during the animation.
566    *
567    * @param [in] actor The actor to animate.
568    * @param [in] delaySeconds The initial delay from the start of the animation.
569    */
570   void Show(Actor actor, float delaySeconds);
571
572   /**
573    * @brief Hide an actor during the animation.
574    *
575    * @param [in] actor The actor to animate.
576    * @param [in] delaySeconds The initial delay from the start of the animation.
577    */
578   void Hide(Actor actor, float delaySeconds);
579
580 public: // Not intended for use by Application developers
581
582   /**
583    * @brief This constructor is used by Dali New() methods
584    * @param [in] animation A pointer to a newly allocated Dali resource
585    */
586   explicit DALI_INTERNAL Animation(Internal::Animation* animation);
587
588 };
589
590 /**
591  * @}
592  */
593 } // namespace Dali
594
595 #endif // __DALI_ANIMATION_H__