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