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