New Popup implementation
[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 class Actor;
35 struct Property;
36 struct Vector2;
37 struct Vector3;
38
39 namespace Internal DALI_INTERNAL
40 {
41 class Animation;
42 }
43
44 /**
45  * @brief Dali::Animation can be used to animate the properties of any number of objects, typically Actors.
46  *
47  * An example animation setup is shown below:
48  *
49  * @code
50  *
51  * struct MyProgram
52  * {
53  *   Actor mActor; // The object we wish to animate
54  *   Animation mAnimation; // Keep this to control the animation
55  * }
56  *
57  * // ...To play the animation
58  *
59  * mAnimation = Animation::New(3.0f); // duration 3 seconds
60  * mAnimation.AnimateTo(Property(mActor, Actor::Property::POSITION), Vector3(10.0f, 50.0f, 0.0f));
61  * mAnimation.Play();
62  *
63  * @endcode
64  *
65  * Dali::Animation supports "fire and forget" behaviour i.e. an animation continues to play if the handle is discarded.
66  * Note that in the following example, the "Finish" signal will be emitted:
67  *
68  * @code
69  *
70  * void ExampleCallback( Animation& source )
71  * {
72  *   std::cout << "Animation has finished" << std::endl;
73  * }
74  *
75  * void ExampleAnimation( Actor actor )
76  * {
77  *   Animation animation = Animation::New(2.0f); // duration 2 seconds
78  *   animation.AnimateTo(Property(actor, Actor::Property::POSITION), 10.0f, 50.0f, 0.0f);
79  *   animation.FinishedSignal().Connect( ExampleCallback );
80  *   animation.Play();
81  * } // At this point the animation handle has gone out of scope
82  *
83  * Actor actor = Actor::New();
84  * Stage::GetCurrent().Add( actor );
85  *
86  * // Fire animation and forget about it
87  * ExampleAnimation( actor );
88  *
89  * // However the animation will continue, and "Animation has finished" will be printed after 2 seconds.
90  *
91  * @endcode
92  *
93  * If the "Finish" signal is connected to a member function of an object, it must be disconnected before the object is destroyed.
94  * This is typically done in the object destructor, and requires either the Dali::Connection object or Dali::Animation handle to be stored.
95  *
96  * Signals
97  * | %Signal Name | Method                   |
98  * |--------------|--------------------------|
99  * | finished     | @ref FinishedSignal()    |
100  *
101  * Actions
102  * | %Action Name | %Animation method called |
103  * |--------------|--------------------------|
104  * | play         | Play()                   |
105  * | stop         | Stop()                   |
106  * | pause        | Pause()                  |
107  */
108 class DALI_IMPORT_API Animation : public BaseHandle
109 {
110 public:
111
112   typedef Signal< void (Animation&) > AnimationSignalType; ///< Animation finished signal type
113
114   typedef Any AnyFunction; ///< Interpolation function
115
116   /**
117    * @brief What to do when the animation ends, is stopped or is destroyed
118    */
119   enum EndAction
120   {
121     Bake,     ///< When the animation ends, the animated property values are saved.
122     Discard,  ///< When the animation ends, the animated property values are forgotten.
123     BakeFinal ///< If the animation is stopped, the animated property values are saved as if the animation had run to completion, otherwise behaves like Bake.
124   };
125
126   /**
127    * @brief What interpolation method to use on key-frame animations
128    */
129   enum Interpolation
130   {
131     Linear,   ///< Values in between key frames are interpolated using a linear polynomial. (Default)
132     Cubic     ///< Values in between key frames are interpolated using a cubic polynomial.
133   };
134
135   /**
136    * @brief Create an uninitialized Animation; this can be initialized with Animation::New().
137    *
138    * Calling member functions with an uninitialized Dali::Object is not allowed.
139    */
140   Animation();
141
142   /**
143    * @brief Create an initialized Animation.
144    *
145    * The animation will not loop.
146    * The default end action is "Bake".
147    * The default alpha function is linear.
148    * @pre durationSeconds must be greater than zero.
149    * @param [in] durationSeconds The duration in seconds.
150    * @return A handle to a newly allocated Dali resource.
151    */
152   static Animation New(float durationSeconds);
153
154   /**
155    * @brief Downcast an Object handle to Animation.
156    *
157    * If handle points to an Animation object the downcast produces
158    * valid handle. If not the returned handle is left uninitialized.
159    *
160    * @param[in] handle to An object
161    * @return handle to a Animation object or an uninitialized handle
162    */
163   static Animation DownCast( BaseHandle handle );
164
165   /**
166    * @brief Destructor
167    *
168    * This is non-virtual since derived Handle types must not contain data or virtual methods.
169    */
170   ~Animation();
171
172   /**
173    * @brief This copy constructor is required for (smart) pointer semantics.
174    *
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    * @param [in] rhs  A reference to the copied handle
183    * @return A reference to this
184    */
185   Animation& operator=(const Animation& rhs);
186
187   /**
188    * @brief Set the duration of an animation.
189    *
190    * @pre durationSeconds must be greater than zero.
191    * @param[in] seconds The duration in seconds.
192    */
193   void SetDuration(float seconds);
194
195   /**
196    * @brief Retrieve the duration of an animation.
197    *
198    * @return The duration in seconds.
199    */
200   float GetDuration() const;
201
202   /**
203    * @brief Set whether the animation will loop.
204    *
205    * @param[in] looping True if the animation will loop.
206    */
207   void SetLooping(bool looping);
208
209   /**
210    * @brief Query whether the animation will loop.
211    *
212    * @return True if the animation will loop.
213    */
214   bool IsLooping() const;
215
216   /**
217    * @brief Set the end action of the animation.
218    *
219    * This action is performed when the animation ends or if it is stopped.
220    * Default end action is bake
221    * @param[in] action The end action.
222    */
223   void SetEndAction(EndAction action);
224
225   /**
226    * @brief Returns the end action of the animation.
227    *
228    * @return The end action.
229    */
230   EndAction GetEndAction() const;
231
232   /**
233    * @brief Set the disconnect action.
234    *
235    * If any of the animated property owners are disconnected from the stage while the animation is being played, then this action is performed.
236    * Default action is to BakeFinal.
237    * @param[in] disconnectAction The disconnect action.
238    */
239   void SetDisconnectAction( EndAction disconnectAction );
240
241   /**
242    * @brief Returns the disconnect action.
243    *
244    * @return The disconnect action.
245    */
246   EndAction GetDisconnectAction() const;
247
248   /**
249    * @brief Set the default alpha function for an animation.
250    *
251    * This is applied to individual property animations, if no further alpha functions are supplied.
252    * @param[in] alpha The default alpha function.
253    */
254   void SetDefaultAlphaFunction(AlphaFunction alpha);
255
256   /**
257    * @brief Retrieve the default alpha function for an animation.
258    *
259    * @return The default alpha function.
260    */
261   AlphaFunction GetDefaultAlphaFunction() const;
262
263   /*
264    * @brief Sets the progress of the animation.
265    *
266    * When played, the animation will start from this point.
267    * If playing, the animation will jump to, and continue playing from this point.
268    *
269    * The progress must be in the 0-1 interval or in the play range interval
270    * if defined ( See SetPlayRange ), otherwise, it will be ignored.
271    *
272    * @param[in] progress The new progress as a normalized value between [0,1]
273    * or between the play range if specified.
274    */
275   void SetCurrentProgress( float progress );
276
277   /**
278   * @brief Retrieve the current progress of the animation.
279   *
280   * @return The current progress as a normalized value between [0,1].
281   */
282   float GetCurrentProgress();
283
284   /**
285    * @brief Specifies an speed factor for the animation.
286    *
287    * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
288    * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
289    * to play the animation in reverse.
290    *
291    * @param[in] factor A value which will multiply the velocity.
292    */
293   void SetSpeedFactor( float factor );
294
295   /**
296    * @brief Retrieve the speed factor of the animation
297    *
298    * @return speed factor
299    */
300   float GetSpeedFactor() const;
301
302   /**
303    * @brief Set the playing range.
304    * Animation will play between the values specified. Both values ( range.x and range.y ) should be between 0-1,
305    * otherwise they will be ignored. If the range provided is not in proper order ( minimum,maximum ), it will be reordered.
306    *
307    * @param[in] range Two values between [0,1] to specify minimum and maximum progress. The
308    * animation will play between those values.
309    */
310   void SetPlayRange( const Vector2& range );
311
312   /**
313    * @brief Get the playing range
314    *
315    * @return The play range defined for the animation.
316    */
317   Vector2 GetPlayRange() const;
318
319   /**
320    * @brief Play the animation.
321    */
322   void Play();
323
324   /**
325    * @brief Play the animation from a given point.
326    * The progress must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
327    * otherwise, it will be ignored.
328    *
329    * @param[in] progress A value between [0,1], or between the play range if specified, form where the animation should start playing
330    */
331   void PlayFrom( float progress );
332
333   /**
334    * @brief Pause the animation.
335    */
336   void Pause();
337
338   /**
339    * @brief Stop the animation.
340    */
341   void Stop();
342
343   /**
344    * @brief Clear the animation.
345    *
346    * This disconnects any objects that were being animated, effectively stopping the animation.
347    */
348   void Clear();
349
350   /**
351    * @brief Connect to this signal to be notified when an Animation's animations have finished.
352    *
353    * @return A signal object to Connect() with.
354    */
355   AnimationSignalType& FinishedSignal();
356
357   /**
358    * @brief Animate a property value by a relative amount.
359    *
360    * The default alpha function will be used.
361    * The effect will start & end when the animation begins & ends.
362    * @param [in] target The target object/property to animate.
363    * @param [in] relativeValue The property value will change by this amount.
364    */
365   void AnimateBy(Property target, Property::Value relativeValue);
366
367   /**
368    * @brief Animate a property value by a relative amount.
369    *
370    * The effect will start & end when the animation begins & ends.
371    * @param [in] target The target object/property to animate.
372    * @param [in] relativeValue The property value will change by this amount.
373    * @param [in] alpha The alpha function to apply.
374    */
375   void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha);
376
377   /**
378    * @brief Animate a property value by a relative amount.
379    *
380    * The default alpha function will be used.
381    * @param [in] target The target object/property to animate.
382    * @param [in] relativeValue The property value will increase/decrease by this amount.
383    * @param [in] period The effect will occur during this time period.
384    */
385   void AnimateBy(Property target, Property::Value relativeValue, TimePeriod period);
386
387   /**
388    * @brief Animate a property value by a relative amount.
389    *
390    * @param [in] target The target object/property to animate.
391    * @param [in] relativeValue The property value will increase/decrease by this amount.
392    * @param [in] alpha The alpha function to apply.
393    * @param [in] period The effect will occur during this time period.
394    */
395   void AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period);
396
397   /**
398    * @brief Animate a property to a destination value.
399    *
400    * The default alpha function will be used.
401    * The effect will start & end when the animation begins & ends.
402    * @param [in] target The target object/property to animate.
403    * @param [in] destinationValue The destination value.
404    */
405   void AnimateTo(Property target, Property::Value destinationValue);
406
407   /**
408    * @brief Animate a property to a destination value.
409    *
410    * The effect will start & end when the animation begins & ends.
411    * @param [in] target The target object/property to animate.
412    * @param [in] destinationValue The destination value.
413    * @param [in] alpha The alpha function to apply.
414    */
415   void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha);
416
417   /**
418    * @brief Animate a property to a destination value.
419    *
420    * The default alpha function will be used.
421    * @param [in] target The target object/property to animate.
422    * @param [in] destinationValue The destination value.
423    * @param [in] period The effect will occur during this time period.
424    */
425   void AnimateTo(Property target, Property::Value destinationValue, TimePeriod period);
426
427   /**
428    * @brief Animate a property to a destination value.
429    *
430    * @param [in] target The target object/property to animate.
431    * @param [in] destinationValue The destination value.
432    * @param [in] alpha The alpha function to apply.
433    * @param [in] period The effect will occur during this time period.
434    */
435   void AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period);
436
437    /**
438    * @brief Animate a property between keyframes.
439    *
440    * @param [in] target The target object/property to animate.
441    * @param [in] keyFrames The key frames
442    */
443   void AnimateBetween(Property target, KeyFrames& keyFrames);
444
445   /**
446    * @brief Animate a property between keyframes.
447    *
448    * @param [in] target The target object + property to animate
449    * @param [in] keyFrames The set of time / value pairs between which to animate.
450    * @param [in] interpolation The method used to interpolate between values.
451    */
452   void AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation);
453
454   /**
455    * @brief Animate a property between keyframes.
456    *
457    * @param [in] target The target object/property to animate.
458    * @param [in] keyFrames The key frames
459    * @param [in] alpha The alpha function to apply.
460    */
461   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha);
462
463   /**
464    * @brief Animate a property between keyframes.
465    *
466    * @param [in] target The target object + property to animate
467    * @param [in] keyFrames The set of time / value pairs between which to animate.
468    * @param [in] alpha The alpha function to apply.
469    * @param [in] interpolation The method used to interpolate between values.
470    */
471   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
472
473   /**
474    * @brief Animate a property between keyframes.
475    *
476    * @param [in] target The target object/property to animate.
477    * @param [in] keyFrames The key frames
478    * @param [in] period The effect will occur during this time period.
479    */
480   void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period);
481
482   /**
483    * @brief Animate a property between keyframes.
484    *
485    * @param [in] target The target object + property to animate
486    * @param [in] keyFrames The set of time / value pairs between which to animate.
487    * @param [in] period The effect will occur duing this time period.
488    * @param [in] interpolation The method used to interpolate between values.
489    */
490   void AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
491
492   /**
493    * @brief Animate a property between keyframes.
494    *
495    * @param [in] target The target object/property to animate.
496    * @param [in] keyFrames The key frames
497    * @param [in] alpha The alpha function to apply.
498    * @param [in] period The effect will occur during this time period.
499    */
500   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
501
502   /**
503    * @brief Animate a property between keyframes.
504    *
505    * @param [in] target The target object + property to animate
506    * @param [in] keyFrames The set of time / value pairs between which to animate.
507    * @param [in] alpha The alpha function to apply to the overall progress.
508    * @param [in] period The effect will occur duing this time period.
509    * @param [in] interpolation The method used to interpolate between values.
510    */
511   void AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation);
512
513
514   // Actor-specific convenience methods
515
516   /**
517    * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
518    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
519    *
520    * @param[in] actor The actor to animate
521    * @param[in] path The path. It defines position and orientation
522    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
523    */
524   void Animate( Actor actor, Path path, const Vector3& forward );
525
526   /**
527    * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
528    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
529    *
530    * @param[in] actor The actor to animate
531    * @param[in] path The path. It defines position and orientation
532    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
533    * @param [in] alpha The alpha function to apply.
534    */
535   void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha );
536
537   /**
538    * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
539    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
540    *
541    * @param[in] actor The actor to animate
542    * @param[in] path The path. It defines position and orientation
543    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
544    * @param [in] period The effect will occur during this time period.
545    */
546   void Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period );
547
548   /**
549    * @brief Animate an actor's position and orientation through a predefined path. The actor will rotate to orient the supplied
550    * forward vector with the path's tangent. If forward is the zero vector then no rotation will happen.
551    *
552    * @param[in] actor The actor to animate
553    * @param[in] path The path. It defines position and orientation
554    * @param[in] forward The vector (in local space coordinate system) that will be oriented with the path's tangent direction
555    * @param [in] alpha The alpha function to apply.
556    * @param [in] period The effect will occur during this time period.
557    */
558   void Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
559
560   /**
561    * @brief Show an actor during the animation.
562    *
563    * @param [in] actor The actor to animate.
564    * @param [in] delaySeconds The initial delay from the start of the animation.
565    */
566   void Show(Actor actor, float delaySeconds);
567
568   /**
569    * @brief Hide an actor during the animation.
570    *
571    * @param [in] actor The actor to animate.
572    * @param [in] delaySeconds The initial delay from the start of the animation.
573    */
574   void Hide(Actor actor, float delaySeconds);
575
576 public: // Not intended for use by Application developers
577
578   /**
579    * @brief This constructor is used by Dali New() methods
580    * @param [in] animation A pointer to a newly allocated Dali resource
581    */
582   explicit DALI_INTERNAL Animation(Internal::Animation* animation);
583
584 };
585
586 } // namespace Dali
587
588 #endif // __DALI_ANIMATION_H__