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