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