Refactor SceneGraphProperty handling code in event side to make RegisterProperty...
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-animator.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_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/animation/alpha-function.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/animation/time-period.h>
25 #include <dali/public-api/common/constants.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/math/quaternion.h>
28 #include <dali/public-api/math/radian.h>
29 #include <dali/devel-api/common/owner-container.h>
30 #include <dali/internal/event/animation/key-frames-impl.h>
31 #include <dali/internal/event/animation/path-impl.h>
32 #include <dali/internal/update/nodes/node.h>
33 #include <dali/internal/update/common/property-base.h>
34 #include <dali/internal/update/animation/property-accessor.h>
35 #include <dali/integration-api/debug.h>
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 typedef Dali::Animation::Interpolation Interpolation;
44
45 struct AnimatorFunctionBase;
46
47 namespace SceneGraph
48 {
49
50 class AnimatorBase;
51
52 typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
53
54 typedef AnimatorContainer::Iterator AnimatorIter;
55 typedef AnimatorContainer::ConstIterator AnimatorConstIter;
56
57 /**
58  * An abstract base class for Animators, which can be added to scene graph animations.
59  * Each animator changes a single property of an object in the scene graph.
60  */
61 class AnimatorBase
62 {
63 public:
64
65   typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
66
67   /**
68    * Observer to determine when the animator is no longer present
69    */
70   class LifecycleObserver
71   {
72   public:
73     /**
74      * Called shortly before the animator is destroyed.
75      */
76     virtual void ObjectDestroyed() = 0;
77
78   protected:
79     /**
80      * Virtual destructor, no deletion through this interface
81      */
82     virtual ~LifecycleObserver() = default;
83   };
84
85
86   /**
87    * Constructor.
88    */
89   AnimatorBase()
90   : mLifecycleObserver(nullptr),
91     mDurationSeconds(1.0f),
92     mIntervalDelaySeconds(0.0f),
93     mSpeedFactor(1.0f),
94     mLoopCount(1),
95     mAlphaFunction(AlphaFunction::DEFAULT),
96     mDisconnectAction(Dali::Animation::BakeFinal),
97     mAnimationPlaying(false),
98     mEnabled(true),
99     mConnectedToSceneGraph(false),
100     mAutoReverseEnabled( false )
101   {
102   }
103
104   /**
105    * Virtual destructor.
106    */
107   virtual ~AnimatorBase()
108   {
109     if( mLifecycleObserver != nullptr )
110     {
111       mLifecycleObserver->ObjectDestroyed();
112     }
113   }
114
115   void AddLifecycleObserver( LifecycleObserver& observer )
116   {
117     mLifecycleObserver = &observer;
118   }
119
120   void RemoveLifecycleObserver( LifecycleObserver& observer )
121   {
122     mLifecycleObserver = nullptr;
123   }
124
125   /**
126    * Called when Animator is added to the scene-graph in update-thread.
127    */
128   virtual void ConnectToSceneGraph() = 0;
129
130   /**
131    * Set the duration of the animator.
132    * @pre durationSeconds must be zero or greater; zero is useful when animating boolean values.
133    * @param [in] seconds Duration in seconds.
134    */
135   void SetDuration(float seconds)
136   {
137     DALI_ASSERT_DEBUG(seconds >= 0.0f);
138
139     mDurationSeconds = seconds;
140   }
141
142   /**
143    * Retrieve the duration of the animator.
144    * @return The duration in seconds.
145    */
146   float GetDuration() const
147   {
148     return mDurationSeconds;
149   }
150
151   void SetSpeedFactor( float factor )
152   {
153     mSpeedFactor = factor;
154   }
155
156   void SetLoopCount(int32_t loopCount)
157   {
158     mLoopCount = loopCount;
159   }
160
161   float SetProgress( float progress )
162   {
163     float value = 0.0f;
164
165     if( mAutoReverseEnabled )
166     {
167       if( mSpeedFactor > 0.0f )
168       {
169         value = 1.0f - 2.0f * std::abs( progress - 0.5f );
170       }
171       // Reverse mode
172       else if( mSpeedFactor < 0.0f )
173       {
174         value = 2.0f * std::abs( progress - 0.5f );
175       }
176     }
177     else
178     {
179       value = progress;
180     }
181
182     return value;
183   }
184
185   /**
186    * Set the delay before the animator should take effect.
187    * The default is zero i.e. no delay.
188    * @param [in] seconds The delay in seconds.
189    */
190   void SetIntervalDelay(float seconds)
191   {
192     mIntervalDelaySeconds = seconds;
193   }
194
195   /**
196    * Retrieve the delay before the animator should take effect.
197    * @return The delay in seconds.
198    */
199   float GetIntervalDelay() const
200   {
201     return mIntervalDelaySeconds;
202   }
203
204   /**
205    * Set the alpha function for an animator.
206    * @param [in] alphaFunc The alpha function to apply to the animation progress.
207    */
208   void SetAlphaFunction(const AlphaFunction& alphaFunction)
209   {
210     mAlphaFunction = alphaFunction;
211   }
212
213   /**
214    * Retrieve the alpha function of an animator.
215    * @return The function.
216    */
217   AlphaFunction GetAlphaFunction() const
218   {
219     return mAlphaFunction;
220   }
221
222   /**
223    * Applies the alpha function to the specified progress
224    * @param[in] Current progress
225    * @return The progress after the alpha function has been aplied
226    */
227   float ApplyAlphaFunction( float progress ) const
228   {
229     float result = progress;
230
231     AlphaFunction::Mode alphaFunctionMode( mAlphaFunction.GetMode() );
232     if( alphaFunctionMode == AlphaFunction::BUILTIN_FUNCTION )
233     {
234       switch(mAlphaFunction.GetBuiltinFunction())
235       {
236         case AlphaFunction::DEFAULT:
237         case AlphaFunction::LINEAR:
238         {
239           break;
240         }
241         case AlphaFunction::REVERSE:
242         {
243           result = 1.0f-progress;
244           break;
245         }
246         case AlphaFunction::EASE_IN_SQUARE:
247         {
248           result = progress * progress;
249           break;
250         }
251         case AlphaFunction::EASE_OUT_SQUARE:
252         {
253           result = 1.0f - (1.0f-progress) * (1.0f-progress);
254           break;
255         }
256         case AlphaFunction::EASE_IN:
257         {
258           result = progress * progress * progress;
259           break;
260         }
261         case AlphaFunction::EASE_OUT:
262         {
263           result = (progress-1.0f) * (progress-1.0f) * (progress-1.0f) + 1.0f;
264           break;
265         }
266         case AlphaFunction::EASE_IN_OUT:
267         {
268           result = progress*progress*(3.0f-2.0f*progress);
269           break;
270         }
271         case AlphaFunction::EASE_IN_SINE:
272         {
273           result = -1.0f * cosf(progress * Math::PI_2) + 1.0f;
274           break;
275         }
276         case AlphaFunction::EASE_OUT_SINE:
277         {
278           result = sinf(progress * Math::PI_2);
279           break;
280         }
281         case AlphaFunction::EASE_IN_OUT_SINE:
282         {
283           result = -0.5f * (cosf(Math::PI * progress) - 1.0f);
284           break;
285         }
286         case AlphaFunction::BOUNCE:
287         {
288           result = sinf(progress * Math::PI);
289           break;
290         }
291         case AlphaFunction::SIN:
292         {
293           result = 0.5f - cosf(progress * 2.0f * Math::PI) * 0.5f;
294           break;
295         }
296         case AlphaFunction::EASE_OUT_BACK:
297         {
298           const float sqrt2 = 1.70158f;
299           progress -= 1.0f;
300           result = 1.0f + progress * progress * ( ( sqrt2 + 1.0f ) * progress + sqrt2 );
301           break;
302         }
303         case AlphaFunction::COUNT:
304         {
305           break;
306         }
307       }
308     }
309     else if(  alphaFunctionMode == AlphaFunction::CUSTOM_FUNCTION )
310     {
311       AlphaFunctionPrototype customFunction = mAlphaFunction.GetCustomFunction();
312       if( customFunction )
313       {
314         result = customFunction(progress);
315       }
316     }
317     else
318     {
319       //If progress is very close to 0 or very close to 1 we don't need to evaluate the curve as the result will
320       //be almost 0 or almost 1 respectively
321       if( ( progress > Math::MACHINE_EPSILON_1 ) && ((1.0f - progress) > Math::MACHINE_EPSILON_1) )
322       {
323         Dali::Vector4 controlPoints = mAlphaFunction.GetBezierControlPoints();
324
325         static const float tolerance = 0.001f;  //10 iteration max
326
327         //Perform a binary search on the curve
328         float lowerBound(0.0f);
329         float upperBound(1.0f);
330         float currentT(0.5f);
331         float currentX = EvaluateCubicBezier( controlPoints.x, controlPoints.z, currentT);
332         while( fabsf( progress - currentX ) > tolerance )
333         {
334           if( progress > currentX )
335           {
336             lowerBound = currentT;
337           }
338           else
339           {
340             upperBound = currentT;
341           }
342           currentT = (upperBound+lowerBound)*0.5f;
343           currentX = EvaluateCubicBezier( controlPoints.x, controlPoints.z, currentT);
344         }
345         result = EvaluateCubicBezier( controlPoints.y, controlPoints.w, currentT);
346       }
347     }
348
349     return result;
350   }
351
352   /**
353    * Whether to bake the animation if attached property owner is disconnected.
354    * Property is only baked if the animator is active.
355    * @param [in] action The disconnect action.
356    */
357   void SetDisconnectAction( Dali::Animation::EndAction action )
358   {
359     mDisconnectAction = action;
360   }
361
362   /**
363    * Retrieve the disconnect action of an animator.
364    * @return The disconnect action.
365    */
366   Dali::Animation::EndAction GetDisconnectAction() const
367   {
368     return mDisconnectAction;
369   }
370
371   /**
372    * Whether the animator is active or not.
373    * @param [in] active The new active state.
374    * @post When the animator becomes active, it applies the disconnect-action if the property owner is then disconnected.
375    * @note When the property owner is disconnected, the active state is set to false.
376    */
377   void SetActive( bool active )
378   {
379     mAnimationPlaying = active;
380   }
381
382   /**
383    * Retrive wheter the animator's target object is valid and on the stage.
384    * @return The enabled state.
385    */
386   bool IsEnabled() const
387   {
388     return mEnabled;
389   }
390
391   /**
392    * @brief Sets the looping mode.
393    * @param[in] loopingMode True when the looping mode is AUTO_REVERSE
394    */
395   void SetLoopingMode( bool loopingMode )
396   {
397     mAutoReverseEnabled = loopingMode;
398   }
399
400   /**
401    * Returns wheter the target object of the animator is still valid
402    * or has been destroyed.
403    * @return True if animator is orphan, false otherwise   *
404    * @note The SceneGraph::Animation will delete any orphan animator in its Update method.
405    */
406   virtual bool Orphan() = 0;
407
408   /**
409    * Update the scene object attached to the animator.
410    * @param[in] bufferIndex The buffer to animate.
411    * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
412    * @param[in] bake Bake.
413    */
414   virtual void Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
415
416 protected:
417
418   /**
419    * Helper function to evaluate a cubic bezier curve assuming first point is at 0.0 and last point is at 1.0
420    * @param[in] p0 First control point of the bezier curve
421    * @param[in] p1 Second control point of the bezier curve
422    * @param[in] t A floating point value between 0.0 and 1.0
423    * @return Value of the curve at progress t
424    */
425   inline float EvaluateCubicBezier( float p0, float p1, float t ) const
426   {
427     float tSquare = t*t;
428     return 3.0f*(1.0f-t)*(1.0f-t)*t*p0 + 3.0f*(1.0f-t)*tSquare*p1 + tSquare*t;
429   }
430
431   LifecycleObserver* mLifecycleObserver;
432   float mDurationSeconds;
433   float mIntervalDelaySeconds;
434   float mSpeedFactor;
435
436   int32_t mLoopCount;
437
438   AlphaFunction mAlphaFunction;
439
440   Dali::Animation::EndAction mDisconnectAction;     ///< EndAction to apply when target object gets disconnected from the stage.
441   bool mAnimationPlaying:1;                         ///< whether disconnect has been applied while it's running.
442   bool mEnabled:1;                                  ///< Animator is "enabled" while its target object is valid and on the stage.
443   bool mConnectedToSceneGraph:1;                    ///< True if ConnectToSceneGraph() has been called in update-thread.
444   bool mAutoReverseEnabled:1;
445 };
446
447 /**
448  * An animator for a specific property type PropertyType.
449  */
450 template < typename PropertyType, typename PropertyAccessorType >
451 class Animator : public AnimatorBase, public PropertyOwner::Observer
452 {
453 public:
454
455   /**
456    * Construct a new property animator.
457    * @param[in] property The animatable property; only valid while the Animator is attached.
458    * @param[in] animatorFunction The function used to animate the property.
459    * @param[in] alphaFunction The alpha function to apply.
460    * @param[in] timePeriod The time period of this animation.
461    * @return A newly allocated animator.
462    */
463   static AnimatorBase* New( const PropertyOwner& propertyOwner,
464                             const PropertyBase& property,
465                             AnimatorFunctionBase* animatorFunction,
466                             AlphaFunction alphaFunction,
467                             const TimePeriod& timePeriod )
468   {
469     typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
470
471     // The property was const in the actor-thread, but animators are used in the scene-graph thread.
472     AnimatorType* animator = new AnimatorType( const_cast<PropertyOwner*>( &propertyOwner ),
473                                                const_cast<PropertyBase*>( &property ),
474                                                animatorFunction );
475
476     animator->SetAlphaFunction( alphaFunction );
477     animator->SetIntervalDelay( timePeriod.delaySeconds );
478     animator->SetDuration( timePeriod.durationSeconds );
479
480     return animator;
481   }
482
483   /**
484    * Virtual destructor.
485    */
486   virtual ~Animator()
487   {
488     if (mPropertyOwner && mConnectedToSceneGraph)
489     {
490       mPropertyOwner->RemoveObserver(*this);
491     }
492
493     delete mAnimatorFunction;
494   }
495
496   /**
497    * Called when Animator is added to the scene-graph in update-thread.
498    */
499   virtual void ConnectToSceneGraph()
500   {
501     mConnectedToSceneGraph = true;
502     mPropertyOwner->AddObserver(*this);
503   }
504
505   /**
506    * Called when mPropertyOwner is connected to the scene graph.
507    */
508   virtual void PropertyOwnerConnected( PropertyOwner& owner )
509   {
510     mEnabled = true;
511   }
512
513   /**
514    * Called when mPropertyOwner is disconnected from the scene graph.
515    */
516   virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
517   {
518     // If we are active, then bake the value if required
519     if ( mAnimationPlaying && mDisconnectAction != Dali::Animation::Discard )
520     {
521       // Bake to target-value if BakeFinal, otherwise bake current value
522       Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
523     }
524
525     mEnabled = false;
526   }
527
528   /**
529    * Called shortly before mPropertyOwner is destroyed
530    */
531   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
532   {
533     mPropertyOwner = NULL;
534   }
535
536   /**
537    * From AnimatorBase.
538    */
539   virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
540   {
541     if( mLoopCount >= 0 )
542     {
543       // Update the progress value
544       progress = SetProgress( progress );
545     }
546
547     float alpha = ApplyAlphaFunction( progress );
548
549     const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
550
551     // need to cast the return value in case property is integer
552     const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
553     if ( bake )
554     {
555       mPropertyAccessor.Bake( bufferIndex, result );
556     }
557     else
558     {
559       mPropertyAccessor.Set( bufferIndex, result );
560     }
561
562     mCurrentProgress = progress;
563   }
564
565   /**
566    * From AnimatorBase.
567    */
568   virtual bool Orphan()
569   {
570     return (mPropertyOwner == NULL);
571   }
572
573 private:
574
575   /**
576    * Private constructor; see also Animator::New().
577    */
578   Animator( PropertyOwner* propertyOwner,
579             PropertyBase* property,
580             AnimatorFunctionBase* animatorFunction )
581   : mPropertyOwner( propertyOwner ),
582     mPropertyAccessor( property ),
583     mAnimatorFunction( animatorFunction ),
584     mCurrentProgress( 0.0f )
585   {
586     // WARNING - this object is created in the event-thread
587     // The scene-graph mPropertyOwner object cannot be observed here
588   }
589
590   // Undefined
591   Animator( const Animator& );
592
593   // Undefined
594   Animator& operator=( const Animator& );
595
596 protected:
597
598   PropertyOwner* mPropertyOwner;
599   PropertyAccessorType mPropertyAccessor;
600
601   AnimatorFunctionBase* mAnimatorFunction;
602   float mCurrentProgress;
603 };
604
605
606
607 /**
608  * An animator for a specific property type PropertyType.
609  */
610 template <typename T, typename PropertyAccessorType>
611 class AnimatorTransformProperty : public AnimatorBase, public PropertyOwner::Observer
612 {
613 public:
614
615   /**
616    * Construct a new property animator.
617    * @param[in] property The animatable property; only valid while the Animator is attached.
618    * @param[in] animatorFunction The function used to animate the property.
619    * @param[in] alphaFunction The alpha function to apply.
620    * @param[in] timePeriod The time period of this animation.
621    * @return A newly allocated animator.
622    */
623   static AnimatorBase* New( const PropertyOwner& propertyOwner,
624                             const PropertyBase& property,
625                             AnimatorFunctionBase* animatorFunction,
626                             AlphaFunction alphaFunction,
627                             const TimePeriod& timePeriod )
628   {
629
630     // The property was const in the actor-thread, but animators are used in the scene-graph thread.
631     AnimatorTransformProperty* animator = new AnimatorTransformProperty( const_cast<PropertyOwner*>( &propertyOwner ),
632                                                const_cast<PropertyBase*>( &property ),
633                                                animatorFunction );
634
635     animator->SetAlphaFunction( alphaFunction );
636     animator->SetIntervalDelay( timePeriod.delaySeconds );
637     animator->SetDuration( timePeriod.durationSeconds );
638
639     return animator;
640   }
641
642   /**
643    * Virtual destructor.
644    */
645   virtual ~AnimatorTransformProperty()
646   {
647     if (mPropertyOwner && mConnectedToSceneGraph)
648     {
649       mPropertyOwner->RemoveObserver(*this);
650     }
651
652     delete mAnimatorFunction;
653   }
654
655   /**
656    * Called when Animator is added to the scene-graph in update-thread.
657    */
658   virtual void ConnectToSceneGraph()
659   {
660     mConnectedToSceneGraph = true;
661     mPropertyOwner->AddObserver(*this);
662   }
663
664   /**
665    * Called when mPropertyOwner is connected to the scene graph.
666    */
667   virtual void PropertyOwnerConnected( PropertyOwner& owner )
668   {
669     mEnabled = true;
670   }
671
672   /**
673    * Called when mPropertyOwner is disconnected from the scene graph.
674    */
675   virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
676   {
677     // If we are active, then bake the value if required
678     if ( mAnimationPlaying && mDisconnectAction != Dali::Animation::Discard )
679     {
680       // Bake to target-value if BakeFinal, otherwise bake current value
681       Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
682     }
683
684     mEnabled = false;
685   }
686
687   /**
688    * Called shortly before mPropertyOwner is destroyed
689    */
690   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
691   {
692     mPropertyOwner = NULL;
693   }
694
695   /**
696    * From AnimatorBase.
697    */
698   virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
699   {
700     if( mLoopCount >= 0 )
701     {
702       // Update the progress value
703       progress = SetProgress( progress );
704     }
705
706     float alpha = ApplyAlphaFunction( progress );
707
708     const T& current = mPropertyAccessor.Get( bufferIndex );
709
710     // need to cast the return value in case property is integer
711     T result = static_cast<T>( (*mAnimatorFunction)( alpha, current ) );
712
713     if ( bake )
714     {
715       mPropertyAccessor.Bake( bufferIndex, result );
716     }
717     else
718     {
719       mPropertyAccessor.Set( bufferIndex, result );
720     }
721
722     mCurrentProgress = progress;
723   }
724
725   /**
726    * From AnimatorBase.
727    */
728   virtual bool Orphan()
729   {
730     return (mPropertyOwner == NULL);
731   }
732
733 private:
734
735   /**
736    * Private constructor; see also Animator::New().
737    */
738   AnimatorTransformProperty( PropertyOwner* propertyOwner,
739             PropertyBase* property,
740             AnimatorFunctionBase* animatorFunction )
741   : mPropertyOwner( propertyOwner ),
742     mPropertyAccessor( property ),
743     mAnimatorFunction( animatorFunction ),
744     mCurrentProgress( 0.0f )
745   {
746     // WARNING - this object is created in the event-thread
747     // The scene-graph mPropertyOwner object cannot be observed here
748   }
749
750   // Undefined
751   AnimatorTransformProperty( const AnimatorTransformProperty& );
752
753   // Undefined
754   AnimatorTransformProperty& operator=( const AnimatorTransformProperty& );
755
756 protected:
757
758   PropertyOwner* mPropertyOwner;
759   PropertyAccessorType mPropertyAccessor;
760
761   AnimatorFunctionBase* mAnimatorFunction;
762   float mCurrentProgress;
763 };
764
765 } // namespace SceneGraph
766
767 /*
768  * AnimatorFunction base class.
769  * All update functions must inherit from AnimatorFunctionBase and overload the appropiate "()" operator
770  */
771 struct AnimatorFunctionBase
772 {
773   /**
774    * Constructor
775    */
776   AnimatorFunctionBase(){}
777
778   /*
779    * Virtual destructor (Intended as base class)
780    */
781   virtual ~AnimatorFunctionBase(){}
782
783   ///Stub "()" operators.
784   virtual bool operator()(float progress, const bool& property)
785   {
786     return property;
787   }
788
789   virtual float operator()(float progress, const int32_t& property)
790   {
791     return static_cast<float>( property );
792   }
793
794   virtual float operator()(float progress, const float& property)
795   {
796     return property;
797   }
798
799   virtual Vector2 operator()(float progress, const Vector2& property)
800   {
801     return property;
802   }
803
804   virtual Vector3 operator()(float progress, const Vector3& property)
805   {
806     return property;
807   }
808
809   virtual Vector4 operator()(float progress, const Vector4& property)
810   {
811     return property;
812   }
813
814   virtual Quaternion operator()(float progress, const Quaternion& property)
815   {
816     return property;
817   }
818 };
819
820 // Update functions
821
822 struct AnimateByInteger : public AnimatorFunctionBase
823 {
824   AnimateByInteger(const int& relativeValue)
825   : mRelative(relativeValue)
826   {
827   }
828
829   using AnimatorFunctionBase::operator();
830   float operator()(float alpha, const int32_t& property)
831   {
832     // integers need to be correctly rounded
833     return roundf(static_cast<float>( property ) + static_cast<float>( mRelative ) * alpha );
834   }
835
836   int32_t mRelative;
837 };
838
839 struct AnimateToInteger : public AnimatorFunctionBase
840 {
841   AnimateToInteger(const int& targetValue)
842   : mTarget(targetValue)
843   {
844   }
845
846   using AnimatorFunctionBase::operator();
847   float operator()(float alpha, const int32_t& property)
848   {
849     // integers need to be correctly rounded
850     return roundf(static_cast<float>( property ) + (static_cast<float>(mTarget - property) * alpha) );
851   }
852
853   int32_t mTarget;
854 };
855
856 struct AnimateByFloat : public AnimatorFunctionBase
857 {
858   AnimateByFloat(const float& relativeValue)
859   : mRelative(relativeValue)
860   {
861   }
862
863   using AnimatorFunctionBase::operator();
864   float operator()(float alpha, const float& property)
865   {
866     return float(property + mRelative * alpha);
867   }
868
869   float mRelative;
870 };
871
872 struct AnimateToFloat : public AnimatorFunctionBase
873 {
874   AnimateToFloat(const float& targetValue)
875   : mTarget(targetValue)
876   {
877   }
878
879   using AnimatorFunctionBase::operator();
880   float operator()(float alpha, const float& property)
881   {
882     return float(property + ((mTarget - property) * alpha));
883   }
884
885   float mTarget;
886 };
887
888 struct AnimateByVector2 : public AnimatorFunctionBase
889 {
890   AnimateByVector2(const Vector2& relativeValue)
891   : mRelative(relativeValue)
892   {
893   }
894
895   using AnimatorFunctionBase::operator();
896   Vector2 operator()(float alpha, const Vector2& property)
897   {
898     return Vector2(property + mRelative * alpha);
899   }
900
901   Vector2 mRelative;
902 };
903
904 struct AnimateToVector2 : public AnimatorFunctionBase
905 {
906   AnimateToVector2(const Vector2& targetValue)
907   : mTarget(targetValue)
908   {
909   }
910
911   using AnimatorFunctionBase::operator();
912   Vector2 operator()(float alpha, const Vector2& property)
913   {
914     return Vector2(property + ((mTarget - property) * alpha));
915   }
916
917   Vector2 mTarget;
918 };
919
920 struct AnimateByVector3 : public AnimatorFunctionBase
921 {
922   AnimateByVector3(const Vector3& relativeValue)
923   : mRelative(relativeValue)
924   {
925   }
926
927   using AnimatorFunctionBase::operator();
928   Vector3 operator()(float alpha, const Vector3& property)
929   {
930     return Vector3(property + mRelative * alpha);
931   }
932
933   Vector3 mRelative;
934 };
935
936 struct AnimateToVector3 : public AnimatorFunctionBase
937 {
938   AnimateToVector3(const Vector3& targetValue)
939   : mTarget(targetValue)
940   {
941   }
942
943   using AnimatorFunctionBase::operator();
944   Vector3 operator()(float alpha, const Vector3& property)
945   {
946     return Vector3(property + ((mTarget - property) * alpha));
947   }
948
949   Vector3 mTarget;
950 };
951
952 struct AnimateByVector4 : public AnimatorFunctionBase
953 {
954   AnimateByVector4(const Vector4& relativeValue)
955   : mRelative(relativeValue)
956   {
957   }
958
959   using AnimatorFunctionBase::operator();
960   Vector4 operator()(float alpha, const Vector4& property)
961   {
962     return Vector4(property + mRelative * alpha);
963   }
964
965   Vector4 mRelative;
966 };
967
968 struct AnimateToVector4 : public AnimatorFunctionBase
969 {
970   AnimateToVector4(const Vector4& targetValue)
971   : mTarget(targetValue)
972   {
973   }
974
975   using AnimatorFunctionBase::operator();
976   Vector4 operator()(float alpha, const Vector4& property)
977   {
978     return Vector4(property + ((mTarget - property) * alpha));
979   }
980
981   Vector4 mTarget;
982 };
983
984 struct AnimateByOpacity : public AnimatorFunctionBase
985 {
986   AnimateByOpacity(const float& relativeValue)
987   : mRelative(relativeValue)
988   {
989   }
990
991   using AnimatorFunctionBase::operator();
992   Vector4 operator()(float alpha, const Vector4& property)
993   {
994     Vector4 result(property);
995     result.a += mRelative * alpha;
996
997     return result;
998   }
999
1000   float mRelative;
1001 };
1002
1003 struct AnimateToOpacity : public AnimatorFunctionBase
1004 {
1005   AnimateToOpacity(const float& targetValue)
1006   : mTarget(targetValue)
1007   {
1008   }
1009
1010   using AnimatorFunctionBase::operator();
1011   Vector4 operator()(float alpha, const Vector4& property)
1012   {
1013     Vector4 result(property);
1014     result.a = property.a + ((mTarget - property.a) * alpha);
1015
1016     return result;
1017   }
1018
1019   float mTarget;
1020 };
1021
1022 struct AnimateByBoolean : public AnimatorFunctionBase
1023 {
1024   AnimateByBoolean(bool relativeValue)
1025   : mRelative(relativeValue)
1026   {
1027   }
1028
1029   using AnimatorFunctionBase::operator();
1030   bool operator()(float alpha, const bool& property)
1031   {
1032     // Alpha is not useful here, just keeping to the same template as other update functors
1033     return bool(alpha >= 1.0f ? (property || mRelative) : property);
1034   }
1035
1036   bool mRelative;
1037 };
1038
1039 struct AnimateToBoolean : public AnimatorFunctionBase
1040 {
1041   AnimateToBoolean(bool targetValue)
1042   : mTarget(targetValue)
1043   {
1044   }
1045
1046   using AnimatorFunctionBase::operator();
1047   bool operator()(float alpha, const bool& property)
1048   {
1049     // Alpha is not useful here, just keeping to the same template as other update functors
1050     return bool(alpha >= 1.0f ? mTarget : property);
1051   }
1052
1053   bool mTarget;
1054 };
1055
1056 struct RotateByAngleAxis : public AnimatorFunctionBase
1057 {
1058   RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
1059   : mAngleRadians( angleRadians ),
1060     mAxis(axis.x, axis.y, axis.z)
1061   {
1062   }
1063
1064   using AnimatorFunctionBase::operator();
1065   Quaternion operator()(float alpha, const Quaternion& rotation)
1066   {
1067     if (alpha > 0.0f)
1068     {
1069       return rotation * Quaternion(mAngleRadians * alpha, mAxis);
1070     }
1071
1072     return rotation;
1073   }
1074
1075   Radian mAngleRadians;
1076   Vector3 mAxis;
1077 };
1078
1079 struct RotateToQuaternion : public AnimatorFunctionBase
1080 {
1081   RotateToQuaternion(const Quaternion& targetValue)
1082   : mTarget(targetValue)
1083   {
1084   }
1085
1086   using AnimatorFunctionBase::operator();
1087   Quaternion operator()(float alpha, const Quaternion& rotation)
1088   {
1089     return Quaternion::Slerp(rotation, mTarget, alpha);
1090   }
1091
1092   Quaternion mTarget;
1093 };
1094
1095
1096 struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
1097 {
1098   KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
1099   : mKeyFrames(keyFrames)
1100   {
1101   }
1102
1103   using AnimatorFunctionBase::operator();
1104   bool operator()(float progress, const bool& property)
1105   {
1106     if(mKeyFrames->IsActive(progress))
1107     {
1108       return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
1109     }
1110     return property;
1111   }
1112
1113   KeyFrameBooleanPtr mKeyFrames;
1114 };
1115
1116 struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
1117 {
1118   KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
1119   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1120   {
1121   }
1122
1123   using AnimatorFunctionBase::operator();
1124   float operator()(float progress, const int32_t& property)
1125   {
1126     if(mKeyFrames->IsActive(progress))
1127     {
1128       return static_cast<float>( mKeyFrames->GetValue(progress, mInterpolation) );
1129     }
1130     return static_cast<float>( property );
1131   }
1132
1133   KeyFrameIntegerPtr mKeyFrames;
1134   Interpolation mInterpolation;
1135 };
1136
1137 struct KeyFrameNumberFunctor : public AnimatorFunctionBase
1138 {
1139   KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
1140   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1141   {
1142   }
1143
1144   using AnimatorFunctionBase::operator();
1145   float operator()(float progress, const float& property)
1146   {
1147     if(mKeyFrames->IsActive(progress))
1148     {
1149       return mKeyFrames->GetValue(progress, mInterpolation);
1150     }
1151     return property;
1152   }
1153
1154   KeyFrameNumberPtr mKeyFrames;
1155   Interpolation mInterpolation;
1156 };
1157
1158 struct KeyFrameVector2Functor : public AnimatorFunctionBase
1159 {
1160   KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
1161   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1162   {
1163   }
1164
1165   using AnimatorFunctionBase::operator();
1166   Vector2 operator()(float progress, const Vector2& property)
1167   {
1168     if(mKeyFrames->IsActive(progress))
1169     {
1170       return mKeyFrames->GetValue(progress, mInterpolation);
1171     }
1172     return property;
1173   }
1174
1175   KeyFrameVector2Ptr mKeyFrames;
1176   Interpolation mInterpolation;
1177 };
1178
1179
1180 struct KeyFrameVector3Functor : public AnimatorFunctionBase
1181 {
1182   KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
1183   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1184   {
1185   }
1186
1187   using AnimatorFunctionBase::operator();
1188   Vector3 operator()(float progress, const Vector3& property)
1189   {
1190     if(mKeyFrames->IsActive(progress))
1191     {
1192       return mKeyFrames->GetValue(progress, mInterpolation);
1193     }
1194     return property;
1195   }
1196
1197   KeyFrameVector3Ptr mKeyFrames;
1198   Interpolation mInterpolation;
1199 };
1200
1201 struct KeyFrameVector4Functor : public AnimatorFunctionBase
1202 {
1203   KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
1204   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1205   {
1206   }
1207
1208   using AnimatorFunctionBase::operator();
1209   Vector4 operator()(float progress, const Vector4& property)
1210   {
1211     if(mKeyFrames->IsActive(progress))
1212     {
1213       return mKeyFrames->GetValue(progress, mInterpolation);
1214     }
1215     return property;
1216   }
1217
1218   KeyFrameVector4Ptr mKeyFrames;
1219   Interpolation mInterpolation;
1220 };
1221
1222 struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase
1223 {
1224   KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
1225   : mKeyFrames(keyFrames)
1226   {
1227   }
1228
1229   using AnimatorFunctionBase::operator();
1230   Quaternion operator()(float progress, const Quaternion& property)
1231   {
1232     if(mKeyFrames->IsActive(progress))
1233     {
1234       return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
1235     }
1236     return property;
1237   }
1238
1239   KeyFrameQuaternionPtr mKeyFrames;
1240 };
1241
1242 struct PathPositionFunctor : public AnimatorFunctionBase
1243 {
1244   PathPositionFunctor( PathPtr path )
1245   : mPath(path)
1246   {
1247   }
1248
1249   using AnimatorFunctionBase::operator();
1250   Vector3 operator()(float progress, const Vector3& property)
1251   {
1252     Vector3 position(property);
1253     static_cast<void>( mPath->SamplePosition(progress, position) );
1254     return position;
1255   }
1256
1257   PathPtr mPath;
1258 };
1259
1260 struct PathRotationFunctor : public AnimatorFunctionBase
1261 {
1262   PathRotationFunctor( PathPtr path, const Vector3& forward )
1263   : mPath(path),
1264     mForward( forward )
1265   {
1266     mForward.Normalize();
1267   }
1268
1269   using AnimatorFunctionBase::operator();
1270   Quaternion operator()(float progress, const Quaternion& property)
1271   {
1272     Vector3 tangent;
1273     if( mPath->SampleTangent(progress, tangent) )
1274     {
1275       return Quaternion( mForward, tangent );
1276     }
1277     else
1278     {
1279       return property;
1280     }
1281   }
1282
1283   PathPtr mPath;
1284   Vector3 mForward;
1285 };
1286
1287 } // namespace Internal
1288
1289 } // namespace Dali
1290
1291 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__