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