[dali_1.0.25] Merge branch 'tizen'
[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) 2014 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/internal/common/owner-container.h>
23 #include <dali/internal/event/animation/key-frames-impl.h>
24 #include <dali/internal/event/animation/path-impl.h>
25 #include <dali/internal/update/nodes/node.h>
26 #include <dali/internal/update/common/property-base.h>
27 #include <dali/public-api/animation/alpha-functions.h>
28 #include <dali/public-api/animation/animation.h>
29 #include <dali/public-api/animation/time-period.h>
30 #include <dali/public-api/common/dali-common.h>
31 #include <dali/public-api/math/quaternion.h>
32 #include <dali/public-api/math/radian.h>
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 typedef Dali::Animation::Interpolation Interpolation;
41
42 struct AnimatorFunctionBase;
43
44 namespace SceneGraph
45 {
46
47 class AnimatorBase;
48
49 typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
50
51 typedef AnimatorContainer::Iterator AnimatorIter;
52 typedef AnimatorContainer::ConstIterator AnimatorConstIter;
53
54 /**
55  * An abstract base class for Animators, which can be added to scene graph animations.
56  * Each animator changes a single property of an object in the scene graph.
57  */
58 class AnimatorBase
59 {
60 public:
61
62   typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
63
64   /**
65    * Constructor.
66    */
67   AnimatorBase()
68   : mDurationSeconds(1.0f),
69     mInitialDelaySeconds(0.0f),
70     mAlphaFunc(AlphaFunctions::Linear),
71     mDisconnectAction(Dali::Animation::BakeFinal),
72     mActive(false),
73     mEnabled(true)
74   {
75   }
76
77   /**
78    * Virtual destructor.
79    */
80   virtual ~AnimatorBase()
81   {
82   }
83
84   /**
85    * Set the duration of the animator.
86    * @pre durationSeconds must be zero or greater; zero is useful when animating boolean values.
87    * @param [in] seconds Duration in seconds.
88    */
89   void SetDuration(float seconds)
90   {
91     DALI_ASSERT_DEBUG(seconds >= 0.0f);
92
93     mDurationSeconds = seconds;
94   }
95
96   /**
97    * Retrieve the duration of the animator.
98    * @return The duration in seconds.
99    */
100   float GetDuration()
101   {
102     return mDurationSeconds;
103   }
104
105   /**
106    * Set the delay before the animator should take effect.
107    * The default is zero i.e. no delay.
108    * @param [in] seconds The delay in seconds.
109    */
110   void SetInitialDelay(float seconds)
111   {
112     mInitialDelaySeconds = seconds;
113   }
114
115   /**
116    * Retrieve the initial delay of the animator.
117    * @return The delay in seconds.
118    */
119   float GetInitialDelay()
120   {
121     return mInitialDelaySeconds;
122   }
123
124   /**
125    * Set the alpha function for an animator.
126    * @param [in] alphaFunc The alpha function to apply to the animation progress.
127    */
128   void SetAlphaFunc(AlphaFunc alphaFunc)
129   {
130     mAlphaFunc = alphaFunc;
131   }
132
133   /**
134    * Retrieve the alpha function of an animator.
135    * @return The function.
136    */
137   AlphaFunc GetAlphaFunc() const
138   {
139     return mAlphaFunc;
140   }
141
142   /**
143    * Whether to bake the animation if attached property owner is disconnected.
144    * Property is only baked if the animator is active.
145    * @param [in] action The disconnect action.
146    */
147   void SetDisconnectAction( Dali::Animation::EndAction action )
148   {
149     mDisconnectAction = action;
150   }
151
152   /**
153    * Retrieve the disconnect action of an animator.
154    * @return The disconnect action.
155    */
156   Dali::Animation::EndAction GetDisconnectAction() const
157   {
158     return mDisconnectAction;
159   }
160
161   /**
162    * Whether the animator is active or not.
163    * @param [in] active The new active state.
164    * @post When the animator becomes active, it applies the disconnect-action if the property owner is then disconnected.
165    * @note When the property owner is disconnected, the active state is set to false.
166    */
167   void SetActive( bool active )
168   {
169     mActive = active;
170   }
171
172   /**
173    * Retrieve whether the animator has been set to active or not.
174    * @return The active state.
175    */
176   bool GetActive() const
177   {
178     return mActive;
179   }
180
181   /*
182    * Retrive wheter the animator's target object is valid and on the stage.
183    * @return The enabled state.
184    */
185   bool IsEnabled() const
186   {
187     return mEnabled;
188   }
189   /**
190    * Returns wheter the target object of the animator is still valid
191    * or has been destroyed.
192    * @return True if animator is orphan, false otherwise   *
193    * @note The SceneGraph::Animation will delete any orphan animator in its Update method.
194    */
195   virtual bool Orphan() = 0;
196
197   /**
198    * Update the scene object attached to the animator.
199    * @param[in] bufferIndex The buffer to animate.
200    * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
201    * @param[in] bake Bake.
202    */
203   virtual void Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
204
205 protected:
206
207   float mDurationSeconds;
208   float mInitialDelaySeconds;
209
210   AlphaFunc mAlphaFunc;
211
212   Dali::Animation::EndAction mDisconnectAction;     ///< EndAction to apply when target object gets disconnected from the stage.
213   bool mActive:1;                                   ///< Animator is "active" while it's running.
214   bool mEnabled:1;                                  ///< Animator is "enabled" while its target object is valid and on the stage.
215 };
216
217 /**
218  * An animator for a specific property type PropertyType.
219  */
220 template < typename PropertyType, typename PropertyAccessorType >
221 class Animator : public AnimatorBase, public PropertyOwner::Observer
222 {
223 public:
224
225   /**
226    * Construct a new property animator.
227    * @param[in] property The animatable property; only valid while the Animator is attached.
228    * @param[in] animatorFunction The function used to animate the property.
229    * @param[in] alphaFunction The alpha function to apply.
230    * @param[in] timePeriod The time period of this animation.
231    * @return A newly allocated animator.
232    */
233   static AnimatorBase* New( const PropertyOwner& propertyOwner,
234                             const PropertyBase& property,
235                             AnimatorFunctionBase* animatorFunction,
236                             AlphaFunction alphaFunction,
237                             const TimePeriod& timePeriod )
238   {
239     typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
240
241     // The property was const in the actor-thread, but animators are used in the scene-graph thread.
242     AnimatorType* animator = new AnimatorType( const_cast<PropertyOwner*>( &propertyOwner ),
243                                                const_cast<PropertyBase*>( &property ),
244                                                animatorFunction );
245
246     animator->SetAlphaFunc( alphaFunction );
247     animator->SetInitialDelay( timePeriod.delaySeconds );
248     animator->SetDuration( timePeriod.durationSeconds );
249
250     return animator;
251   }
252
253   /**
254    * Virtual destructor.
255    */
256   virtual ~Animator()
257   {
258     if (mPropertyOwner)
259     {
260       mPropertyOwner->RemoveObserver(*this);
261     }
262
263     if( mAnimatorFunction )
264     {
265       delete mAnimatorFunction;
266     }
267   }
268
269   /**
270    * Called when mPropertyOwner is connected to the scene graph.
271    */
272   virtual void PropertyOwnerConnected( PropertyOwner& owner )
273   {
274     mEnabled = true;
275   }
276
277   /**
278    * Called when mPropertyOwner is disconnected from the scene graph.
279    */
280   virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
281   {
282     // If we are active, then bake the value if required
283     if ( mActive && mDisconnectAction != Dali::Animation::Discard )
284     {
285       // Bake to target-value if BakeFinal, otherwise bake current value
286       Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
287     }
288
289     mActive = false;
290     mEnabled = false;
291   }
292
293   /**
294    * Called shortly before mPropertyOwner is destroyed
295    */
296   virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
297   {
298     mPropertyOwner = NULL;
299     mPropertyAccessor.Reset();
300     mEnabled = false;
301   }
302
303   /**
304    * From AnimatorBase.
305    */
306   virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
307   {
308     float alpha = mAlphaFunc( progress );
309     const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
310
311     const PropertyType result = (*mAnimatorFunction)( alpha, current );
312     if ( bake )
313     {
314       mPropertyAccessor.Bake( bufferIndex, result );
315     }
316     else
317     {
318       mPropertyAccessor.Set( bufferIndex, result );
319     }
320
321     mCurrentProgress = progress;
322   }
323
324   /**
325    * From AnimatorBase.
326    */
327   virtual bool Orphan()
328   {
329     return (mPropertyOwner == NULL);
330   }
331
332 private:
333
334   /**
335    * Private constructor; see also Animator::New().
336    */
337   Animator( PropertyOwner* propertyOwner,
338             PropertyBase* property,
339             AnimatorFunctionBase* animatorFunction )
340   : mPropertyOwner( propertyOwner ),
341     mPropertyAccessor( property ),
342     mAnimatorFunction( animatorFunction ),
343     mCurrentProgress( 0.0f )
344   {
345     mPropertyOwner->AddObserver(*this);
346   }
347
348   // Undefined
349   Animator( const Animator& );
350
351   // Undefined
352   Animator& operator=( const Animator& );
353
354 protected:
355
356   PropertyOwner* mPropertyOwner;
357   PropertyAccessorType mPropertyAccessor;
358
359   AnimatorFunctionBase* mAnimatorFunction;
360   float mCurrentProgress;
361 };
362
363 } // namespace SceneGraph
364
365 /*
366  * AnimatorFunction base class.
367  * All update functions must inherit from AnimatorFunctionBase and overload the appropiate "()" operator
368  */
369 struct AnimatorFunctionBase
370 {
371   /**
372    * Constructor
373    */
374   AnimatorFunctionBase(){}
375
376   /*
377    * Virtual destructor (Intended as base class)
378    */
379   virtual ~AnimatorFunctionBase(){}
380
381   ///Stub "()" operators.
382   virtual float operator()(float progress, const int& property)
383   {
384     return property;
385   }
386
387   virtual float operator()(float progress, const float& property)
388   {
389     return property;
390   }
391
392   virtual bool operator()(float progress, const bool& property)
393   {
394     return property;
395   }
396
397   virtual Vector2 operator()(float progress, const Vector2& property)
398   {
399     return property;
400   }
401
402   virtual Vector3 operator()(float progress, const Vector3& property)
403   {
404     return property;
405   }
406
407   virtual Vector4 operator()(float progress, const Vector4& property)
408   {
409     return property;
410   }
411
412   virtual Quaternion operator()(float progress, const Quaternion& property)
413   {
414     return property;
415   }
416 };
417
418 // Update functions
419
420 struct AnimateByFloat : public AnimatorFunctionBase
421 {
422   AnimateByFloat(const float& relativeValue)
423   : mRelative(relativeValue)
424   {
425   }
426
427   float operator()(float alpha, const float& property)
428   {
429     return float(property + mRelative * alpha);
430   }
431
432   float mRelative;
433 };
434
435 struct AnimateToFloat : public AnimatorFunctionBase
436 {
437   AnimateToFloat(const float& targetValue)
438   : mTarget(targetValue)
439   {
440   }
441
442   float operator()(float alpha, const float& property)
443   {
444     return float(property + ((mTarget - property) * alpha));
445   }
446
447   float mTarget;
448 };
449
450 struct AnimateByInteger : public AnimatorFunctionBase
451 {
452   AnimateByInteger(const int& relativeValue)
453   : mRelative(relativeValue)
454   {
455   }
456
457   float operator()(float alpha, const int& property)
458   {
459     return int(property + mRelative * alpha + 0.5f );
460   }
461
462   int mRelative;
463 };
464
465 struct AnimateToInteger : public AnimatorFunctionBase
466 {
467   AnimateToInteger(const int& targetValue)
468   : mTarget(targetValue)
469   {
470   }
471
472   float operator()(float alpha, const int& property)
473   {
474     return int(property + ((mTarget - property) * alpha) + 0.5f);
475   }
476
477   int mTarget;
478 };
479
480 struct AnimateByVector2 : public AnimatorFunctionBase
481 {
482   AnimateByVector2(const Vector2& relativeValue)
483   : mRelative(relativeValue)
484   {
485   }
486
487   Vector2 operator()(float alpha, const Vector2& property)
488   {
489     return Vector2(property + mRelative * alpha);
490   }
491
492   Vector2 mRelative;
493 };
494
495 struct AnimateToVector2 : public AnimatorFunctionBase
496 {
497   AnimateToVector2(const Vector2& targetValue)
498   : mTarget(targetValue)
499   {
500   }
501
502   Vector2 operator()(float alpha, const Vector2& property)
503   {
504     return Vector2(property + ((mTarget - property) * alpha));
505   }
506
507   Vector2 mTarget;
508 };
509
510 struct AnimateByVector3 : public AnimatorFunctionBase
511 {
512   AnimateByVector3(const Vector3& relativeValue)
513   : mRelative(relativeValue)
514   {
515   }
516
517   Vector3 operator()(float alpha, const Vector3& property)
518   {
519     return Vector3(property + mRelative * alpha);
520   }
521
522   Vector3 mRelative;
523 };
524
525 struct AnimateToVector3 : public AnimatorFunctionBase
526 {
527   AnimateToVector3(const Vector3& targetValue)
528   : mTarget(targetValue)
529   {
530   }
531
532   Vector3 operator()(float alpha, const Vector3& property)
533   {
534     return Vector3(property + ((mTarget - property) * alpha));
535   }
536
537   Vector3 mTarget;
538 };
539
540 struct AnimateByVector4 : public AnimatorFunctionBase
541 {
542   AnimateByVector4(const Vector4& relativeValue)
543   : mRelative(relativeValue)
544   {
545   }
546
547   Vector4 operator()(float alpha, const Vector4& property)
548   {
549     return Vector4(property + mRelative * alpha);
550   }
551
552   Vector4 mRelative;
553 };
554
555 struct AnimateToVector4 : public AnimatorFunctionBase
556 {
557   AnimateToVector4(const Vector4& targetValue)
558   : mTarget(targetValue)
559   {
560   }
561
562   Vector4 operator()(float alpha, const Vector4& property)
563   {
564     return Vector4(property + ((mTarget - property) * alpha));
565   }
566
567   Vector4 mTarget;
568 };
569
570 struct AnimateByOpacity : public AnimatorFunctionBase
571 {
572   AnimateByOpacity(const float& relativeValue)
573   : mRelative(relativeValue)
574   {
575   }
576
577   Vector4 operator()(float alpha, const Vector4& property)
578   {
579     Vector4 result(property);
580     result.a += mRelative * alpha;
581
582     return result;
583   }
584
585   float mRelative;
586 };
587
588 struct AnimateToOpacity : public AnimatorFunctionBase
589 {
590   AnimateToOpacity(const float& targetValue)
591   : mTarget(targetValue)
592   {
593   }
594
595   Vector4 operator()(float alpha, const Vector4& property)
596   {
597     Vector4 result(property);
598     result.a = property.a + ((mTarget - property.a) * alpha);
599
600     return result;
601   }
602
603   float mTarget;
604 };
605
606 struct AnimateByBoolean : public AnimatorFunctionBase
607 {
608   AnimateByBoolean(bool relativeValue)
609   : mRelative(relativeValue)
610   {
611   }
612
613   bool operator()(float alpha, const bool& property)
614   {
615     // Alpha is not useful here, just keeping to the same template as other update functors
616     return bool(alpha >= 1.0f ? (property || mRelative) : property);
617   }
618
619   bool mRelative;
620 };
621
622 struct AnimateToBoolean : public AnimatorFunctionBase
623 {
624   AnimateToBoolean(bool targetValue)
625   : mTarget(targetValue)
626   {
627   }
628
629   bool operator()(float alpha, const bool& property)
630   {
631     // Alpha is not useful here, just keeping to the same template as other update functors
632     return bool(alpha >= 1.0f ? mTarget : property);
633   }
634
635   bool mTarget;
636 };
637
638 struct RotateByAngleAxis : public AnimatorFunctionBase
639 {
640   RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
641   : mAngleRadians(angleRadians),
642     mAxis(axis.x, axis.y, axis.z, 0.0f)
643   {
644   }
645
646   Quaternion operator()(float alpha, const Quaternion& rotation)
647   {
648     if (alpha > 0.0f)
649     {
650       return rotation * Quaternion(mAngleRadians * alpha, mAxis);
651     }
652
653     return rotation;
654   }
655
656   float mAngleRadians;
657   Vector4 mAxis;
658 };
659
660 struct RotateToQuaternion : public AnimatorFunctionBase
661 {
662   RotateToQuaternion(const Quaternion& targetValue)
663   : mTarget(targetValue)
664   {
665   }
666
667   Quaternion operator()(float alpha, const Quaternion& rotation)
668   {
669     return Quaternion::Slerp(rotation, mTarget, alpha);
670   }
671
672   Quaternion mTarget;
673 };
674
675
676 struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
677 {
678   KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
679   : mKeyFrames(keyFrames)
680   {
681   }
682
683   bool operator()(float progress, const bool& property)
684   {
685     if(mKeyFrames->IsActive(progress))
686     {
687       return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
688     }
689     return property;
690   }
691
692   KeyFrameBooleanPtr mKeyFrames;
693 };
694
695 struct KeyFrameNumberFunctor : public AnimatorFunctionBase
696 {
697   KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
698   : mKeyFrames(keyFrames),mInterpolation(interpolation)
699   {
700   }
701
702   float operator()(float progress, const float& property)
703   {
704     if(mKeyFrames->IsActive(progress))
705     {
706       return mKeyFrames->GetValue(progress, mInterpolation);
707     }
708     return property;
709   }
710
711   KeyFrameNumberPtr mKeyFrames;
712   Interpolation mInterpolation;
713 };
714
715 struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
716 {
717   KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
718   : mKeyFrames(keyFrames),mInterpolation(interpolation)
719   {
720   }
721
722   float operator()(float progress, const int& property)
723   {
724     if(mKeyFrames->IsActive(progress))
725     {
726       return mKeyFrames->GetValue(progress, mInterpolation);
727     }
728     return property;
729   }
730
731   KeyFrameIntegerPtr mKeyFrames;
732   Interpolation mInterpolation;
733 };
734
735 struct KeyFrameVector2Functor : public AnimatorFunctionBase
736 {
737   KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
738   : mKeyFrames(keyFrames),mInterpolation(interpolation)
739   {
740   }
741
742   Vector2 operator()(float progress, const Vector2& property)
743   {
744     if(mKeyFrames->IsActive(progress))
745     {
746       return mKeyFrames->GetValue(progress, mInterpolation);
747     }
748     return property;
749   }
750
751   KeyFrameVector2Ptr mKeyFrames;
752   Interpolation mInterpolation;
753 };
754
755
756 struct KeyFrameVector3Functor : public AnimatorFunctionBase
757 {
758   KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
759   : mKeyFrames(keyFrames),mInterpolation(interpolation)
760   {
761   }
762
763   Vector3 operator()(float progress, const Vector3& property)
764   {
765     if(mKeyFrames->IsActive(progress))
766     {
767       return mKeyFrames->GetValue(progress, mInterpolation);
768     }
769     return property;
770   }
771
772   KeyFrameVector3Ptr mKeyFrames;
773   Interpolation mInterpolation;
774 };
775
776 struct KeyFrameVector4Functor : public AnimatorFunctionBase
777 {
778   KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
779   : mKeyFrames(keyFrames),mInterpolation(interpolation)
780   {
781   }
782
783   Vector4 operator()(float progress, const Vector4& property)
784   {
785     if(mKeyFrames->IsActive(progress))
786     {
787       return mKeyFrames->GetValue(progress, mInterpolation);
788     }
789     return property;
790   }
791
792   KeyFrameVector4Ptr mKeyFrames;
793   Interpolation mInterpolation;
794 };
795
796 struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase
797 {
798   KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
799   : mKeyFrames(keyFrames)
800   {
801   }
802
803   Quaternion operator()(float progress, const Quaternion& property)
804   {
805     if(mKeyFrames->IsActive(progress))
806     {
807       return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
808     }
809     return property;
810   }
811
812   KeyFrameQuaternionPtr mKeyFrames;
813 };
814
815 struct PathPositionFunctor : public AnimatorFunctionBase
816 {
817   PathPositionFunctor( PathPtr path )
818   : mPath(path)
819   {
820   }
821
822   Vector3 operator()(float progress, const Vector3& property)
823   {
824     return mPath->SamplePosition(progress );
825   }
826
827   PathPtr mPath;
828 };
829
830 struct PathRotationFunctor : public AnimatorFunctionBase
831 {
832   PathRotationFunctor( PathPtr path, const Vector3& forward )
833   : mPath(path),
834     mForward( forward )
835   {
836     mForward.Normalize();
837   }
838
839   Quaternion operator()(float progress, const Quaternion& property)
840   {
841     Vector3 tangent( mPath->SampleTangent(progress) );
842     return Quaternion( mForward, tangent );
843   }
844
845   PathPtr mPath;
846   Vector3 mForward;
847 };
848
849
850 } // namespace Internal
851
852 } // namespace Dali
853
854 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__