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