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