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