Merge "Make the core code a bit more generic to be able to compile without glibc...
[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     float alpha = ApplyAlphaFunction( progress );
523
524     // PropertyType specific part
525     DoUpdate( bufferIndex, bake, alpha );
526
527     mCurrentProgress = progress;
528   }
529
530   /**
531    * Type specific part of the animator
532    * @param bufferIndex index to use
533    * @param bake whether to bake or not
534    * @param alpha value from alpha based on progress
535    */
536   virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) = 0;
537
538 protected:
539
540   /**
541    * Helper function to evaluate a cubic bezier curve assuming first point is at 0.0 and last point is at 1.0
542    * @param[in] p0 First control point of the bezier curve
543    * @param[in] p1 Second control point of the bezier curve
544    * @param[in] t A floating point value between 0.0 and 1.0
545    * @return Value of the curve at progress t
546    */
547   inline float EvaluateCubicBezier( float p0, float p1, float t ) const
548   {
549     float tSquare = t*t;
550     return 3.0f*(1.0f-t)*(1.0f-t)*t*p0 + 3.0f*(1.0f-t)*tSquare*p1 + tSquare*t;
551   }
552
553   LifecycleObserver* mLifecycleObserver;
554   PropertyOwner* mPropertyOwner;
555   AnimatorFunctionBase* mAnimatorFunction;
556   float mDurationSeconds;
557   float mIntervalDelaySeconds;
558   float mSpeedFactor;
559   float mCurrentProgress;
560
561   int32_t mLoopCount;
562
563   AlphaFunction mAlphaFunction;
564
565   Dali::Animation::EndAction mDisconnectAction;     ///< EndAction to apply when target object gets disconnected from the stage.
566   bool mAnimationPlaying:1;                         ///< whether disconnect has been applied while it's running.
567   bool mEnabled:1;                                  ///< Animator is "enabled" while its target object is valid and on the stage.
568   bool mConnectedToSceneGraph:1;                    ///< True if ConnectToSceneGraph() has been called in update-thread.
569   bool mAutoReverseEnabled:1;
570 };
571
572 /**
573  * An animator for a specific property type PropertyType.
574  */
575 template < typename PropertyType, typename PropertyAccessorType >
576 class Animator : public AnimatorBase
577 {
578 public:
579
580   /**
581    * Construct a new property animator.
582    * @param[in] property The animatable property; only valid while the Animator is attached.
583    * @param[in] animatorFunction The function used to animate the property.
584    * @param[in] alphaFunction The alpha function to apply.
585    * @param[in] timePeriod The time period of this animation.
586    * @return A newly allocated animator.
587    */
588   static AnimatorBase* New( const PropertyOwner& propertyOwner,
589                             const PropertyBase& property,
590                             AnimatorFunctionBase* animatorFunction,
591                             AlphaFunction alphaFunction,
592                             const TimePeriod& timePeriod )
593   {
594     // The property was const in the actor-thread, but animators are used in the scene-graph thread.
595     return new Animator( const_cast<PropertyOwner*>( &propertyOwner ),
596                                   const_cast<PropertyBase*>( &property ),
597                                   animatorFunction,
598                                   alphaFunction,
599                                   timePeriod );
600   }
601
602   /**
603    * Virtual destructor.
604    */
605   virtual ~Animator()
606   {
607   }
608
609   /**
610    * @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
611    */
612   virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
613   {
614     const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
615
616     // need to cast the return value in case property is integer
617     const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
618
619     if ( bake )
620     {
621       mPropertyAccessor.Bake( bufferIndex, result );
622     }
623     else
624     {
625       mPropertyAccessor.Set( bufferIndex, result );
626     }
627   }
628
629 private:
630
631   /**
632    * Private constructor; see also Animator::New().
633    */
634   Animator( PropertyOwner* propertyOwner,
635             PropertyBase* property,
636             AnimatorFunctionBase* animatorFunction,
637             AlphaFunction alphaFunction,
638             const TimePeriod& timePeriod )
639   : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
640     mPropertyAccessor( property )
641   {
642     // WARNING - this object is created in the event-thread
643     // The scene-graph mPropertyOwner object cannot be observed here
644   }
645
646   // Undefined
647   Animator( const Animator& );
648
649   // Undefined
650   Animator& operator=( const Animator& );
651
652 protected:
653
654   PropertyAccessorType mPropertyAccessor;
655
656 };
657
658
659
660 /**
661  * An animator for a specific property type PropertyType.
662  */
663 template <typename PropertyType, typename PropertyAccessorType>
664 class AnimatorTransformProperty : public AnimatorBase
665 {
666 public:
667
668   /**
669    * Construct a new property animator.
670    * @param[in] property The animatable property; only valid while the Animator is attached.
671    * @param[in] animatorFunction The function used to animate the property.
672    * @param[in] alphaFunction The alpha function to apply.
673    * @param[in] timePeriod The time period of this animation.
674    * @return A newly allocated animator.
675    */
676   static AnimatorBase* New( const PropertyOwner& propertyOwner,
677                             const PropertyBase& property,
678                             AnimatorFunctionBase* animatorFunction,
679                             AlphaFunction alphaFunction,
680                             const TimePeriod& timePeriod )
681   {
682
683     // The property was const in the actor-thread, but animators are used in the scene-graph thread.
684     return new AnimatorTransformProperty( const_cast<PropertyOwner*>( &propertyOwner ),
685                                                                          const_cast<PropertyBase*>( &property ),
686                                                                          animatorFunction,
687                                                                          alphaFunction,
688                                                                          timePeriod );
689   }
690
691   /**
692    * Virtual destructor.
693    */
694   virtual ~AnimatorTransformProperty()
695   {
696   }
697
698   /**
699    * @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
700    */
701   virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
702   {
703     const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
704
705     // need to cast the return value in case property is integer
706     const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
707
708     if ( bake )
709     {
710       mPropertyAccessor.Bake( bufferIndex, result );
711     }
712     else
713     {
714       mPropertyAccessor.Set( bufferIndex, result );
715     }
716   }
717
718 private:
719
720   /**
721    * Private constructor; see also Animator::New().
722    */
723   AnimatorTransformProperty( PropertyOwner* propertyOwner,
724             PropertyBase* property,
725             AnimatorFunctionBase* animatorFunction,
726             AlphaFunction alphaFunction,
727             const TimePeriod& timePeriod )
728   : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
729     mPropertyAccessor( property )
730   {
731     // WARNING - this object is created in the event-thread
732     // The scene-graph mPropertyOwner object cannot be observed here
733   }
734
735   // Undefined
736   AnimatorTransformProperty() = delete;
737   AnimatorTransformProperty( const AnimatorTransformProperty& ) = delete;
738   AnimatorTransformProperty& operator=( const AnimatorTransformProperty& ) = delete;
739
740 protected:
741
742   PropertyAccessorType mPropertyAccessor;
743
744 };
745
746 } // namespace SceneGraph
747
748 // Update functions
749
750 struct AnimateByInteger : public AnimatorFunctionBase
751 {
752   AnimateByInteger(const int& relativeValue)
753   : mRelative(relativeValue)
754   {
755   }
756
757   using AnimatorFunctionBase::operator();
758   float operator()(float alpha, const int32_t& property)
759   {
760     // integers need to be correctly rounded
761     return roundf(static_cast<float>( property ) + static_cast<float>( mRelative ) * alpha );
762   }
763
764   int32_t mRelative;
765 };
766
767 struct AnimateToInteger : public AnimatorFunctionBase
768 {
769   AnimateToInteger(const int& targetValue)
770   : mTarget(targetValue)
771   {
772   }
773
774   using AnimatorFunctionBase::operator();
775   float operator()(float alpha, const int32_t& property)
776   {
777     // integers need to be correctly rounded
778     return roundf(static_cast<float>( property ) + (static_cast<float>(mTarget - property) * alpha) );
779   }
780
781   int32_t mTarget;
782 };
783
784 struct AnimateByFloat : public AnimatorFunctionBase
785 {
786   AnimateByFloat(const float& relativeValue)
787   : mRelative(relativeValue)
788   {
789   }
790
791   using AnimatorFunctionBase::operator();
792   float operator()(float alpha, const float& property)
793   {
794     return float(property + mRelative * alpha);
795   }
796
797   float mRelative;
798 };
799
800 struct AnimateToFloat : public AnimatorFunctionBase
801 {
802   AnimateToFloat(const float& targetValue)
803   : mTarget(targetValue)
804   {
805   }
806
807   using AnimatorFunctionBase::operator();
808   float operator()(float alpha, const float& property)
809   {
810     return float(property + ((mTarget - property) * alpha));
811   }
812
813   float mTarget;
814 };
815
816 struct AnimateByVector2 : public AnimatorFunctionBase
817 {
818   AnimateByVector2(const Vector2& relativeValue)
819   : mRelative(relativeValue)
820   {
821   }
822
823   using AnimatorFunctionBase::operator();
824   Vector2 operator()(float alpha, const Vector2& property)
825   {
826     return Vector2(property + mRelative * alpha);
827   }
828
829   Vector2 mRelative;
830 };
831
832 struct AnimateToVector2 : public AnimatorFunctionBase
833 {
834   AnimateToVector2(const Vector2& targetValue)
835   : mTarget(targetValue)
836   {
837   }
838
839   using AnimatorFunctionBase::operator();
840   Vector2 operator()(float alpha, const Vector2& property)
841   {
842     return Vector2(property + ((mTarget - property) * alpha));
843   }
844
845   Vector2 mTarget;
846 };
847
848 struct AnimateByVector3 : public AnimatorFunctionBase
849 {
850   AnimateByVector3(const Vector3& relativeValue)
851   : mRelative(relativeValue)
852   {
853   }
854
855   using AnimatorFunctionBase::operator();
856   Vector3 operator()(float alpha, const Vector3& property)
857   {
858     return Vector3(property + mRelative * alpha);
859   }
860
861   Vector3 mRelative;
862 };
863
864 struct AnimateToVector3 : public AnimatorFunctionBase
865 {
866   AnimateToVector3(const Vector3& targetValue)
867   : mTarget(targetValue)
868   {
869   }
870
871   using AnimatorFunctionBase::operator();
872   Vector3 operator()(float alpha, const Vector3& property)
873   {
874     return Vector3(property + ((mTarget - property) * alpha));
875   }
876
877   Vector3 mTarget;
878 };
879
880 struct AnimateByVector4 : public AnimatorFunctionBase
881 {
882   AnimateByVector4(const Vector4& relativeValue)
883   : mRelative(relativeValue)
884   {
885   }
886
887   using AnimatorFunctionBase::operator();
888   Vector4 operator()(float alpha, const Vector4& property)
889   {
890     return Vector4(property + mRelative * alpha);
891   }
892
893   Vector4 mRelative;
894 };
895
896 struct AnimateToVector4 : public AnimatorFunctionBase
897 {
898   AnimateToVector4(const Vector4& targetValue)
899   : mTarget(targetValue)
900   {
901   }
902
903   using AnimatorFunctionBase::operator();
904   Vector4 operator()(float alpha, const Vector4& property)
905   {
906     return Vector4(property + ((mTarget - property) * alpha));
907   }
908
909   Vector4 mTarget;
910 };
911
912 struct AnimateByOpacity : public AnimatorFunctionBase
913 {
914   AnimateByOpacity(const float& relativeValue)
915   : mRelative(relativeValue)
916   {
917   }
918
919   using AnimatorFunctionBase::operator();
920   Vector4 operator()(float alpha, const Vector4& property)
921   {
922     Vector4 result(property);
923     result.a += mRelative * alpha;
924
925     return result;
926   }
927
928   float mRelative;
929 };
930
931 struct AnimateToOpacity : public AnimatorFunctionBase
932 {
933   AnimateToOpacity(const float& targetValue)
934   : mTarget(targetValue)
935   {
936   }
937
938   using AnimatorFunctionBase::operator();
939   Vector4 operator()(float alpha, const Vector4& property)
940   {
941     Vector4 result(property);
942     result.a = property.a + ((mTarget - property.a) * alpha);
943
944     return result;
945   }
946
947   float mTarget;
948 };
949
950 struct AnimateByBoolean : public AnimatorFunctionBase
951 {
952   AnimateByBoolean(bool relativeValue)
953   : mRelative(relativeValue)
954   {
955   }
956
957   using AnimatorFunctionBase::operator();
958   bool operator()(float alpha, const bool& property)
959   {
960     // Alpha is not useful here, just keeping to the same template as other update functors
961     return bool(alpha >= 1.0f ? (property || mRelative) : property);
962   }
963
964   bool mRelative;
965 };
966
967 struct AnimateToBoolean : public AnimatorFunctionBase
968 {
969   AnimateToBoolean(bool targetValue)
970   : mTarget(targetValue)
971   {
972   }
973
974   using AnimatorFunctionBase::operator();
975   bool operator()(float alpha, const bool& property)
976   {
977     // Alpha is not useful here, just keeping to the same template as other update functors
978     return bool(alpha >= 1.0f ? mTarget : property);
979   }
980
981   bool mTarget;
982 };
983
984 struct RotateByAngleAxis : public AnimatorFunctionBase
985 {
986   RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
987   : mAngleRadians( angleRadians ),
988     mAxis(axis.x, axis.y, axis.z)
989   {
990   }
991
992   using AnimatorFunctionBase::operator();
993   Quaternion operator()(float alpha, const Quaternion& rotation)
994   {
995     if (alpha > 0.0f)
996     {
997       return rotation * Quaternion(mAngleRadians * alpha, mAxis);
998     }
999
1000     return rotation;
1001   }
1002
1003   Radian mAngleRadians;
1004   Vector3 mAxis;
1005 };
1006
1007 struct RotateToQuaternion : public AnimatorFunctionBase
1008 {
1009   RotateToQuaternion(const Quaternion& targetValue)
1010   : mTarget(targetValue)
1011   {
1012   }
1013
1014   using AnimatorFunctionBase::operator();
1015   Quaternion operator()(float alpha, const Quaternion& rotation)
1016   {
1017     return Quaternion::Slerp(rotation, mTarget, alpha);
1018   }
1019
1020   Quaternion mTarget;
1021 };
1022
1023
1024 struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
1025 {
1026   KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
1027   : mKeyFrames(keyFrames)
1028   {
1029   }
1030
1031   using AnimatorFunctionBase::operator();
1032   bool operator()(float progress, const bool& property)
1033   {
1034     if(mKeyFrames->IsActive(progress))
1035     {
1036       return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
1037     }
1038     return property;
1039   }
1040
1041   KeyFrameBooleanPtr mKeyFrames;
1042 };
1043
1044 struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
1045 {
1046   KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
1047   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1048   {
1049   }
1050
1051   using AnimatorFunctionBase::operator();
1052   float operator()(float progress, const int32_t& property)
1053   {
1054     if(mKeyFrames->IsActive(progress))
1055     {
1056       return static_cast<float>( mKeyFrames->GetValue(progress, mInterpolation) );
1057     }
1058     return static_cast<float>( property );
1059   }
1060
1061   KeyFrameIntegerPtr mKeyFrames;
1062   Interpolation mInterpolation;
1063 };
1064
1065 struct KeyFrameNumberFunctor : public AnimatorFunctionBase
1066 {
1067   KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
1068   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1069   {
1070   }
1071
1072   using AnimatorFunctionBase::operator();
1073   float operator()(float progress, const float& property)
1074   {
1075     if(mKeyFrames->IsActive(progress))
1076     {
1077       return mKeyFrames->GetValue(progress, mInterpolation);
1078     }
1079     return property;
1080   }
1081
1082   KeyFrameNumberPtr mKeyFrames;
1083   Interpolation mInterpolation;
1084 };
1085
1086 struct KeyFrameVector2Functor : public AnimatorFunctionBase
1087 {
1088   KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
1089   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1090   {
1091   }
1092
1093   using AnimatorFunctionBase::operator();
1094   Vector2 operator()(float progress, const Vector2& property)
1095   {
1096     if(mKeyFrames->IsActive(progress))
1097     {
1098       return mKeyFrames->GetValue(progress, mInterpolation);
1099     }
1100     return property;
1101   }
1102
1103   KeyFrameVector2Ptr mKeyFrames;
1104   Interpolation mInterpolation;
1105 };
1106
1107
1108 struct KeyFrameVector3Functor : public AnimatorFunctionBase
1109 {
1110   KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
1111   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1112   {
1113   }
1114
1115   using AnimatorFunctionBase::operator();
1116   Vector3 operator()(float progress, const Vector3& property)
1117   {
1118     if(mKeyFrames->IsActive(progress))
1119     {
1120       return mKeyFrames->GetValue(progress, mInterpolation);
1121     }
1122     return property;
1123   }
1124
1125   KeyFrameVector3Ptr mKeyFrames;
1126   Interpolation mInterpolation;
1127 };
1128
1129 struct KeyFrameVector4Functor : public AnimatorFunctionBase
1130 {
1131   KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
1132   : mKeyFrames(keyFrames),mInterpolation(interpolation)
1133   {
1134   }
1135
1136   using AnimatorFunctionBase::operator();
1137   Vector4 operator()(float progress, const Vector4& property)
1138   {
1139     if(mKeyFrames->IsActive(progress))
1140     {
1141       return mKeyFrames->GetValue(progress, mInterpolation);
1142     }
1143     return property;
1144   }
1145
1146   KeyFrameVector4Ptr mKeyFrames;
1147   Interpolation mInterpolation;
1148 };
1149
1150 struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase
1151 {
1152   KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
1153   : mKeyFrames(keyFrames)
1154   {
1155   }
1156
1157   using AnimatorFunctionBase::operator();
1158   Quaternion operator()(float progress, const Quaternion& property)
1159   {
1160     if(mKeyFrames->IsActive(progress))
1161     {
1162       return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
1163     }
1164     return property;
1165   }
1166
1167   KeyFrameQuaternionPtr mKeyFrames;
1168 };
1169
1170 struct PathPositionFunctor : public AnimatorFunctionBase
1171 {
1172   PathPositionFunctor( PathPtr path )
1173   : mPath(path)
1174   {
1175   }
1176
1177   using AnimatorFunctionBase::operator();
1178   Vector3 operator()(float progress, const Vector3& property)
1179   {
1180     Vector3 position(property);
1181     static_cast<void>( mPath->SamplePosition(progress, position) );
1182     return position;
1183   }
1184
1185   PathPtr mPath;
1186 };
1187
1188 struct PathRotationFunctor : public AnimatorFunctionBase
1189 {
1190   PathRotationFunctor( PathPtr path, const Vector3& forward )
1191   : mPath(path),
1192     mForward( forward )
1193   {
1194     mForward.Normalize();
1195   }
1196
1197   using AnimatorFunctionBase::operator();
1198   Quaternion operator()(float progress, const Quaternion& property)
1199   {
1200     Vector3 tangent;
1201     if( mPath->SampleTangent(progress, tangent) )
1202     {
1203       return Quaternion( mForward, tangent );
1204     }
1205     else
1206     {
1207       return property;
1208     }
1209   }
1210
1211   PathPtr mPath;
1212   Vector3 mForward;
1213 };
1214
1215 } // namespace Internal
1216
1217 } // namespace Dali
1218
1219 #endif // DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H