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