Harmonize Animation API parameter checking and add test cases for them
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / animation-impl.h
1 #ifndef __DALI_INTERNAL_ANIMATION_H__
2 #define __DALI_INTERNAL_ANIMATION_H__
3
4 /*
5  * Copyright (c) 2018 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/common/vector-wrapper.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/animation/animation.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/devel-api/animation/animation-devel.h>
27 #include <dali/devel-api/common/owner-container.h>
28 #include <dali/internal/event/animation/key-frames-impl.h>
29 #include <dali/internal/event/common/event-thread-services.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace SceneGraph
38 {
39 class Animation;
40 class UpdateManager;
41 }
42
43 class Actor;
44 class Animation;
45 class AnimationPlaylist;
46 class AnimatorConnectorBase;
47 class Object;
48 class Path;
49
50 typedef IntrusivePtr<Animation> AnimationPtr;
51 typedef std::vector<AnimationPtr> AnimationContainer;
52
53 typedef AnimationContainer::iterator AnimationIter;
54 typedef AnimationContainer::const_iterator AnimationConstIter;
55
56 /**
57  * Animation is a proxy for a SceneGraph::Animation object.
58  * The UpdateManager owns the Animation object, but the lifetime of the animation is
59  * indirectly controlled by the Animation.
60  */
61 class Animation : public BaseObject
62 {
63 public:
64
65   enum Type
66   {
67     TO,      ///< Animating TO the given value
68     BY,      ///< Animating BY the given value
69     BETWEEN  ///< Animating BETWEEN key-frames
70   };
71
72   typedef Dali::Animation::EndAction EndAction;
73   typedef Dali::Animation::Interpolation Interpolation;
74
75   /**
76    * Create a new Animation object.
77    * @param[in] durationSeconds The duration of the animation.
78    * @return A smart-pointer to the newly allocated Animation.
79    */
80   static AnimationPtr New(float durationSeconds);
81
82   /**
83    * @copydoc Dali::Animation::SetDuration()
84    */
85   void SetDuration(float seconds);
86
87   /**
88    * @copydoc Dali::DevelAnimation::SetProgressNotification()
89    */
90   void SetProgressNotification( float progress );
91
92   /**
93    * @copydoc Dali::DevelAnimation::GetProgressNotification()
94    */
95   float GetProgressNotification();
96
97   /**
98    * @copydoc Dali::Animation::GetDuration()
99    */
100   float GetDuration() const;
101
102   /**
103    * @copydoc Dali::Animation::SetLooping()
104    */
105   void SetLooping(bool on);
106
107   /**
108    * @copydoc Dali::Animation::SetLoopCount()
109    */
110   void SetLoopCount(int32_t count);
111
112   /**
113    * @copydoc Dali::Animation::GetLoopCount()
114    */
115   int32_t GetLoopCount();
116
117   /**
118    * @copydoc Dali::Animation::GetCurrentLoop()
119    */
120   int32_t GetCurrentLoop();
121
122   /**
123    * @copydoc Dali::Animation::IsLooping()
124    */
125   bool IsLooping() const;
126
127   /**
128    * @copydoc Dali::Animation::SetEndAction()
129    */
130   void SetEndAction(EndAction action);
131
132   /**
133    * @copydoc Dali::Animation::GetEndAction()
134    */
135   EndAction GetEndAction() const;
136
137   /**
138    * @copydoc Dali::Animation::SetDisconnectAction()
139    */
140   void SetDisconnectAction(EndAction action);
141
142   /**
143    * @copydoc Dali::Animation::GetDisconnectAction()
144    */
145   EndAction GetDisconnectAction() const;
146
147   /**
148    * @copydoc Dali::Animation::SetDefaultAlphaFunction()
149    */
150   void SetDefaultAlphaFunction(AlphaFunction alpha)
151   {
152     mDefaultAlpha = alpha;
153   }
154
155   /**
156    * @copydoc Dali::Animation::GetDefaultAlphaFunction()
157    */
158   AlphaFunction GetDefaultAlphaFunction() const
159   {
160     return mDefaultAlpha;
161   }
162
163   /**
164    * @copydoc Dali::Animation::Play()
165    */
166   void Play();
167
168   /**
169    * @copydoc Dali::Animation::PlayFrom()
170    */
171   void PlayFrom( float progress );
172
173   /**
174    * @copydoc Dali::Animation::PlayAfter()
175    */
176   void PlayAfter( float delaySeconds );
177
178   /**
179    * @copydoc Dali::Animation::Pause()
180    */
181   void Pause();
182
183   /**
184    * @copydoc Dali::Animation::GetState()
185    */
186   Dali::Animation::State GetState() const;
187
188   /**
189    * @copydoc Dali::Animation::Stop()
190    */
191   void Stop();
192
193   /**
194    * @copydoc Dali::Animation::Clear()
195    */
196   void Clear();
197
198   /**
199    * Query whether a Finished signal should be emitted for this animation.
200    * This should only be called by NotificationManager, before signals are emitted.
201    * @post HasFinished() will return false on subsequent calls, until the animation is replayed to completion.
202    */
203   bool HasFinished();
204
205   /**
206    * @copydoc Dali::Animation::FinishedSignal()
207    */
208   Dali::Animation::AnimationSignalType& FinishedSignal();
209
210   /**
211    * @copydoc Dali::DevelAnimation::ProgressHasBeenReachedSignal()
212    */
213   Dali::Animation::AnimationSignalType& ProgressReachedSignal();
214
215   /**
216    * Emit the Finished signal
217    */
218   void EmitSignalFinish();
219
220   /**
221    * Emit the ProgressReached signal
222    */
223   void EmitSignalProgressReached();
224
225   /**
226    * Connects a callback function with the object's signals.
227    * @param[in] object The object providing the signal.
228    * @param[in] tracker Used to disconnect the signal.
229    * @param[in] signalName The signal to connect to.
230    * @param[in] functor A newly allocated FunctorDelegate.
231    * @return True if the signal was connected.
232    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
233    */
234   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
235
236   /**
237    * Performs actions as requested using the action name.
238    * @param[in] object The object on which to perform the action.
239    * @param[in] actionName The action to perform.
240    * @param[in] attributes The attributes with which to perfrom this action.
241    * @return true if action was done
242    */
243   static bool DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes);
244
245   /**
246    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue)
247    */
248   void AnimateBy(Property& target, Property::Value& relativeValue);
249
250   /**
251    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha)
252    */
253   void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha);
254
255   /**
256    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, TimePeriod period)
257    */
258   void AnimateBy(Property& target, Property::Value& relativeValue, TimePeriod period);
259
260   /**
261    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period)
262    */
263   void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha, TimePeriod period);
264
265   /**
266    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue)
267    */
268   void AnimateTo(Property& target, Property::Value& destinationValue);
269
270   /**
271    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha)
272    */
273   void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha);
274
275   /**
276    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, TimePeriod period)
277    */
278   void AnimateTo(Property& target, Property::Value& destinationValue, TimePeriod period);
279
280   /**
281    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period)
282    */
283   void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period);
284
285   /**
286    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames)
287    */
288   void AnimateBetween(Property target, const KeyFrames& keyFrames);
289
290   /**
291    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation)
292    */
293   void AnimateBetween(Property target, const KeyFrames& keyFrames, Interpolation interpolation );
294
295   /**
296    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period)
297    */
298   void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period);
299
300   /**
301    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation)
302    */
303   void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
304
305   /**
306    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha)
307    */
308   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha);
309
310   /**
311    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation)
312    */
313   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
314
315   /**
316    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period)
317    */
318   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
319
320   /**
321    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation )
322    */
323   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation );
324
325   // Actor-specific convenience functions
326
327   /**
328    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward )
329    */
330   void Animate( Actor& actor, const Path& path, const Vector3& forward );
331
332   /**
333    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha )
334    */
335   void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha );
336
337   /**
338    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period )
339    */
340   void Animate( Actor& actor, const Path& path, const Vector3& forward, TimePeriod period );
341
342   /**
343    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period)
344    */
345   void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
346
347   /**
348    * @copydoc Dali::Animation::Show()
349    */
350   void Show(Actor& actor, float delaySeconds);
351
352   /**
353    * @copydoc Dali::Animation::Hide()
354    */
355   void Hide(Actor& actor, float delaySeconds);
356
357   /**
358    * @copydoc Dali::Animation::SetCurrentProgress()
359    */
360   void SetCurrentProgress(float progress);
361
362   /**
363    * @copydoc Dali::Animation::GetCurrentProgress()
364    */
365   float GetCurrentProgress();
366
367   /**
368    * @copydoc Dali::Animation::SetSpeedFactor()
369    */
370   void SetSpeedFactor( float factor );
371
372   /**
373    * @copydoc Dali::Animation::GetSpeedFactor()
374    */
375   float GetSpeedFactor() const;
376
377   /**
378    * @copydoc Dali::Animation::SetPlayRange()
379    */
380   void SetPlayRange( const Vector2& range );
381
382   /**
383    * @copydoc Dali::Animation::GetPlayRange()
384    */
385   Vector2 GetPlayRange() const;
386
387   /**
388    * @copydoc Dali::Animation::SetLoopingMode()
389    */
390   void SetLoopingMode( Dali::Animation::LoopingMode loopingMode );
391
392   /**
393    * @copydoc Dali::Animation::GetLoopingMode()
394    */
395   Dali::Animation::LoopingMode GetLoopingMode() const;
396
397 public: // For connecting animators to animations
398
399   /**
400    * Add an animator connector.
401    * @param[in] connector The animator connector.
402    */
403   void AddAnimatorConnector( AnimatorConnectorBase* connector );
404
405   /**
406    * Retrieve the SceneGraph::Animation object.
407    * @return The animation.
408    */
409   const SceneGraph::Animation* GetSceneObject()
410   {
411     return mAnimation;
412   }
413
414   /**
415    * Retrieve the event thread services object
416    * @return The interface for sending messages to the scene graph
417    */
418   EventThreadServices& GetEventThreadServices()
419   {
420     return mEventThreadServices;
421   }
422
423 protected:
424
425   /**
426    * Construct a new Animation.
427    * @param[in] eventThreadServices The interface for sending messages to the scene graph
428    * @param[in] playlist The list of currently playing animations.
429    * @param[in] durationSeconds The duration of the animation in seconds.
430    * @param[in] endAction The action to perform when the animation ends.
431    * @param[in] disconnectAction The action to perform when the property owner of an animator is disconnected.
432    * @param[in] defaultAlpha The default alpha function to apply to animators.
433    */
434   Animation( EventThreadServices& eventThreadServices,
435              AnimationPlaylist& playlist,
436              float durationSeconds,
437              EndAction endAction,
438              EndAction disconnectAction,
439              AlphaFunction defaultAlpha);
440
441   /**
442    * Second-phase constructor.
443    */
444   void Initialize();
445
446   /**
447    * Helper to create a scene-graph animation
448    */
449   void CreateSceneObject();
450
451   /**
452    * Helper to create a scene-graph animation
453    */
454   void DestroySceneObject();
455
456   /**
457    * A reference counted object may only be deleted by calling Unreference()
458    */
459   virtual ~Animation();
460
461 private:
462
463   /**
464    * Extends the duration when an animator is added with TimePeriod that exceeds current duration.
465    * @param[in] timePeriod The time period for an animator.
466    */
467   void ExtendDuration( const TimePeriod& timePeriod );
468
469   // Undefined
470   Animation(const Animation&);
471
472   // Undefined
473   Animation& operator=(const Animation& rhs);
474
475 private:
476
477   struct ConnectorTargetValues
478   {
479     ConnectorTargetValues()
480     : targetValue(),
481       timePeriod( 0.0f ),
482       connectorIndex( 0 ),
483       animatorType( TO )
484     {
485     }
486
487     Property::Value targetValue;
488     TimePeriod timePeriod;
489     std::size_t connectorIndex;
490     Animation::Type animatorType;
491   };
492
493 private:
494
495   /**
496    * Compares the end times of the animators returning true if lhs end time is less than rhs end time.
497    * @param[in] lhs The first comparator
498    * @param[in] rhs The second comparator
499    * @return True if end time of lhs is less, false otherwise.
500    */
501   static bool CompareConnectorEndTimes( const ConnectorTargetValues& lhs, const ConnectorTargetValues& rhs );
502
503   /**
504    * Notifies all the objects whose properties are being animated.
505    */
506   void NotifyObjects();
507
508   /**
509    * Sends message to SceneGraph with final progress value
510    */
511   void SendFinalProgressNotificationMessage();
512
513 private:
514
515   const SceneGraph::Animation* mAnimation;
516
517   EventThreadServices& mEventThreadServices;
518   AnimationPlaylist& mPlaylist;
519
520   Dali::Animation::AnimationSignalType mFinishedSignal;
521
522   Dali::Animation::AnimationSignalType mProgressReachedSignal;
523
524   typedef OwnerContainer< AnimatorConnectorBase* > AnimatorConnectorContainer;
525   AnimatorConnectorContainer mConnectors; ///< Owned by the Animation
526
527   typedef std::vector< ConnectorTargetValues > ConnectorTargetValuesContainer;
528   ConnectorTargetValuesContainer mConnectorTargetValues; //< Used to store animating property target value information
529
530   Vector2 mPlayRange;
531
532   float mDurationSeconds;
533   float mSpeedFactor;
534   int32_t mNotificationCount; ///< Keep track of how many Finished signals have been emitted.
535   int32_t mLoopCount;
536   int32_t mCurrentLoop;
537   EndAction mEndAction;
538   EndAction mDisconnectAction;
539   AlphaFunction mDefaultAlpha;
540   Dali::Animation::State mState;
541   float mProgressReachedMarker;
542   float mDelaySeconds;
543   bool mAutoReverseEnabled;  ///< Flag to identify that the looping mode is auto reverse.
544 };
545
546 } // namespace Internal
547
548 // Helpers for public-api forwarding methods
549
550 inline Internal::Animation& GetImplementation(Dali::Animation& animation)
551 {
552   DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
553
554   BaseObject& handle = animation.GetBaseObject();
555
556   return static_cast<Internal::Animation&>(handle);
557 }
558
559 inline const Internal::Animation& GetImplementation(const Dali::Animation& animation)
560 {
561   DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
562
563   const BaseObject& handle = animation.GetBaseObject();
564
565   return static_cast<const Internal::Animation&>(handle);
566 }
567
568 } // namespace Dali
569
570 #endif // __DALI_INTERNAL_ANIMATION_H__