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