Merge "Updated dali_env script to ensure no build telemetry is sent." into devel...
[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::DevelAnimation::SetProgressNotification()
88    */
89   void SetProgressNotification( float progress );
90
91   /**
92    * @copydoc Dali::DevelAnimation::GetProgressNotification()
93    */
94   float GetProgressNotification();
95
96   /**
97    * @copydoc Dali::Animation::GetDuration()
98    */
99   float GetDuration() const;
100
101   /**
102    * @copydoc Dali::Animation::SetLooping()
103    */
104   void SetLooping(bool on);
105
106   /**
107    * @copydoc Dali::Animation::SetLoopCount()
108    */
109   void SetLoopCount(int count);
110
111   /**
112    * @copydoc Dali::Animation::GetLoopCount()
113    */
114   int GetLoopCount();
115
116   /**
117    * @copydoc Dali::Animation::GetCurrentLoop()
118    */
119   int GetCurrentLoop();
120
121   /**
122    * @copydoc Dali::Animation::IsLooping()
123    */
124   bool IsLooping() const;
125
126   /**
127    * @copydoc Dali::Animation::SetEndAction()
128    */
129   void SetEndAction(EndAction action);
130
131   /**
132    * @copydoc Dali::Animation::GetEndAction()
133    */
134   EndAction GetEndAction() const;
135
136   /**
137    * @copydoc Dali::Animation::SetDisconnectAction()
138    */
139   void SetDisconnectAction(EndAction action);
140
141   /**
142    * @copydoc Dali::Animation::GetDisconnectAction()
143    */
144   EndAction GetDisconnectAction() const;
145
146   /**
147    * @copydoc Dali::Animation::SetDefaultAlphaFunction()
148    */
149   void SetDefaultAlphaFunction(AlphaFunction alpha)
150   {
151     mDefaultAlpha = alpha;
152   }
153
154   /**
155    * @copydoc Dali::Animation::GetDefaultAlphaFunction()
156    */
157   AlphaFunction GetDefaultAlphaFunction() const
158   {
159     return mDefaultAlpha;
160   }
161
162   /**
163    * @copydoc Dali::Animation::Play()
164    */
165   void Play();
166
167   /**
168    * @copydoc Dali::Animation::PlayFrom()
169    */
170   void PlayFrom( float progress );
171
172   /**
173    * @copydoc Dali::Animation::PlayAfter()
174    */
175   void PlayAfter( float delaySeconds );
176
177   /**
178    * @copydoc Dali::Animation::Pause()
179    */
180   void Pause();
181
182   /**
183    * @copydoc Dali::Animation::GetState()
184    */
185   Dali::Animation::State GetState() const;
186
187   /**
188    * @copydoc Dali::Animation::Stop()
189    */
190   void Stop();
191
192   /**
193    * @copydoc Dali::Animation::Clear()
194    */
195   void Clear();
196
197   /**
198    * Query whether a Finished signal should be emitted for this animation.
199    * This should only be called by NotificationManager, before signals are emitted.
200    * @post HasFinished() will return false on subsequent calls, until the animation is replayed to completion.
201    */
202   bool HasFinished();
203
204   /**
205    * @copydoc Dali::Animation::FinishedSignal()
206    */
207   Dali::Animation::AnimationSignalType& FinishedSignal();
208
209   /**
210    * @copydoc Dali::DevelAnimation::ProgressHasBeenReachedSignal()
211    */
212   Dali::Animation::AnimationSignalType& ProgressReachedSignal();
213
214   /**
215    * Emit the Finished signal
216    */
217   void EmitSignalFinish();
218
219   /**
220    * Emit the ProgressReached signal
221    */
222   void EmitSignalProgressReached();
223
224   /**
225    * Connects a callback function with the object's signals.
226    * @param[in] object The object providing the signal.
227    * @param[in] tracker Used to disconnect the signal.
228    * @param[in] signalName The signal to connect to.
229    * @param[in] functor A newly allocated FunctorDelegate.
230    * @return True if the signal was connected.
231    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
232    */
233   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
234
235   /**
236    * Performs actions as requested using the action name.
237    * @param[in] object The object on which to perform the action.
238    * @param[in] actionName The action to perform.
239    * @param[in] attributes The attributes with which to perfrom this action.
240    * @return true if action was done
241    */
242   static bool DoAction(BaseObject* object, const std::string& actionName, const Property::Map& attributes);
243
244   /**
245    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue)
246    */
247   void AnimateBy(Property& target, Property::Value& relativeValue);
248
249   /**
250    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha)
251    */
252   void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha);
253
254   /**
255    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, TimePeriod period)
256    */
257   void AnimateBy(Property& target, Property::Value& relativeValue, TimePeriod period);
258
259   /**
260    * @copydoc Dali::Animation::AnimateBy(Property target, Property::Value relativeValue, AlphaFunction alpha, TimePeriod period)
261    */
262   void AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha, TimePeriod period);
263
264   /**
265    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue)
266    */
267   void AnimateTo(Property& target, Property::Value& destinationValue);
268
269   /**
270    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha)
271    */
272   void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha);
273
274   /**
275    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, TimePeriod period)
276    */
277   void AnimateTo(Property& target, Property::Value& destinationValue, TimePeriod period);
278
279   /**
280    * @copydoc Dali::Animation::AnimateTo(Property target, Property::Value destinationValue, AlphaFunction alpha, TimePeriod period)
281    */
282   void AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period);
283
284   /**
285    * Animate a property to a destination value.
286    * @param [in] targetObject The target object to animate.
287    * @param [in] targetPropertyIndex The index of the target property.
288    * @param [in] componentIndex Index to a sub component of a property, for use with Vector2, Vector3 and Vector4
289    * @param [in] destinationValue The destination value.
290    * @param [in] alpha The alpha function to apply.
291    * @param [in] period The effect will occur during this time period.
292    */
293   void AnimateTo(Object& targetObject, Property::Index targetPropertyIndex, int componentIndex, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period);
294
295   /**
296    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames)
297    */
298   void AnimateBetween(Property target, const KeyFrames& keyFrames);
299
300   /**
301    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, Interpolation interpolation)
302    */
303   void AnimateBetween(Property target, const KeyFrames& keyFrames, Interpolation interpolation );
304
305   /**
306    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period)
307    */
308   void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period);
309
310   /**
311    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation)
312    */
313   void AnimateBetween(Property target, const KeyFrames& keyFrames, TimePeriod period, Interpolation interpolation);
314
315   /**
316    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha)
317    */
318   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha);
319
320   /**
321    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation)
322    */
323   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, Interpolation interpolation);
324
325   /**
326    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period)
327    */
328   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period);
329
330   /**
331    * @copydoc Dali::Animation::AnimateBetween(Property target, KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation )
332    */
333   void AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation );
334
335   // Actor-specific convenience functions
336
337   /**
338    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward )
339    */
340   void Animate( Actor& actor, const Path& path, const Vector3& forward );
341
342   /**
343    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha )
344    */
345   void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha );
346
347   /**
348    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, TimePeriod period )
349    */
350   void Animate( Actor& actor, const Path& path, const Vector3& forward, TimePeriod period );
351
352   /**
353    * @copydoc Dali::Animation::Animate( Actor actor, Path path, const Vector3& forward, AlphaFunction alpha, TimePeriod period)
354    */
355   void Animate( Actor& actor, const Path& path, const Vector3& forward, AlphaFunction alpha, TimePeriod period);
356
357   /**
358    * @copydoc Dali::Animation::Show()
359    */
360   void Show(Actor& actor, float delaySeconds);
361
362   /**
363    * @copydoc Dali::Animation::Hide()
364    */
365   void Hide(Actor& actor, float delaySeconds);
366
367   /*
368    * @copydoc Dali::Animation::SetCurrentProgress()
369    */
370   void SetCurrentProgress(float progress);
371
372   /*
373    * @copydoc Dali::Animation::GetCurrentProgress()
374    */
375   float GetCurrentProgress();
376
377   /*
378    * @copydoc Dali::Animation::SetSpeedFactor()
379    */
380   void SetSpeedFactor( float factor );
381
382   /*
383    * @copydoc Dali::Animation::GetSpeedFactor()
384    */
385   float GetSpeedFactor() const;
386
387   /*
388    * @copydoc Dali::Animation::SetPlayRange()
389    */
390   void SetPlayRange( const Vector2& range );
391
392   /*
393    * @copydoc Dali::Animation::GetPlayRange
394    */
395   Vector2 GetPlayRange() 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     unsigned int 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   int mNotificationCount; ///< Keep track of how many Finished signals have been emitted.
535   int mLoopCount;
536   int mCurrentLoop;
537   EndAction mEndAction;
538   EndAction mDisconnectAction;
539   AlphaFunction mDefaultAlpha;
540   Dali::Animation::State mState;
541   float mProgressReachedMarker;
542   float mDelaySeconds;
543 };
544
545 } // namespace Internal
546
547 // Helpers for public-api forwarding methods
548
549 inline Internal::Animation& GetImplementation(Dali::Animation& animation)
550 {
551   DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
552
553   BaseObject& handle = animation.GetBaseObject();
554
555   return static_cast<Internal::Animation&>(handle);
556 }
557
558 inline const Internal::Animation& GetImplementation(const Dali::Animation& animation)
559 {
560   DALI_ASSERT_ALWAYS( animation && "Animation handle is empty" );
561
562   const BaseObject& handle = animation.GetBaseObject();
563
564   return static_cast<const Internal::Animation&>(handle);
565 }
566
567 } // namespace Dali
568
569 #endif // __DALI_INTERNAL_ANIMATION_H__