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