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