1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <boost/function.hpp>
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/animation.h>
32 #include <dali/public-api/animation/time-period.h>
33 #include <dali/public-api/common/dali-common.h>
34 #include <dali/public-api/math/quaternion.h>
35 #include <dali/public-api/math/radian.h>
48 typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
50 typedef AnimatorContainer::Iterator AnimatorIter;
51 typedef AnimatorContainer::ConstIterator AnimatorConstIter;
54 * An abstract base class for Animators, which can be added to scene graph animations.
55 * Each animator changes a single property of an object in the scene graph.
61 typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
67 : mDurationSeconds(1.0f),
68 mInitialDelaySeconds(0.0f),
69 mAlphaFunc(AlphaFunctions::Linear),
70 mDisconnectAction(Dali::Animation::BakeFinal)
77 virtual ~AnimatorBase()
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.
86 void SetDuration(float seconds)
88 DALI_ASSERT_DEBUG(seconds >= 0.0f);
90 mDurationSeconds = seconds;
94 * Retrieve the duration of the animator.
95 * @return The duration in seconds.
99 return mDurationSeconds;
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.
107 void SetInitialDelay(float seconds)
109 mInitialDelaySeconds = seconds;
113 * Retrieve the initial delay of the animator.
114 * @return The delay in seconds.
116 float GetInitialDelay()
118 return mInitialDelaySeconds;
122 * Set the alpha function for an animator.
123 * @param [in] alphaFunc The alpha function to apply to the animation progress.
125 void SetAlphaFunc(AlphaFunc alphaFunc)
127 mAlphaFunc = alphaFunc;
131 * Retrieve the alpha function of an animator.
132 * @return The function.
134 AlphaFunc GetAlphaFunc() const
140 * Whether to bake the animation if attached property owner is disconnected.
141 * @param [in] action The disconnect action.
143 void SetDisconnectAction( Dali::Animation::EndAction action )
145 mDisconnectAction = action;
149 * This must be called when the animator is attached to the scene-graph.
150 * @pre The animatable scene object must also be attached to the scene-graph.
151 * @param[in] propertyOwner The scene-object that owns the animatable property.
153 virtual void Attach( PropertyOwner* propertyOwner ) = 0;
156 * Update the scene object attached to the animator.
157 * @param[in] bufferIndex The buffer to animate.
158 * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
159 * @param[in] bake Bake.
160 * @return True if the update was applied, false if the animator has been orphaned.
162 virtual bool Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
165 * Query whether the animator is still attached to a scene object.
166 * The attachment will be automatically severed, when the object is destroyed.
167 * @return True if the animator is attached.
169 virtual bool IsAttached() const = 0;
173 float mDurationSeconds;
174 float mInitialDelaySeconds;
176 AlphaFunc mAlphaFunc;
178 Dali::Animation::EndAction mDisconnectAction;
182 * An animator for a specific property type PropertyType.
184 template < typename PropertyType, typename PropertyAccessorType >
185 class Animator : public AnimatorBase, public PropertyOwner::Observer
189 typedef boost::function< PropertyType (float, const PropertyType&) > AnimatorFunction;
192 * Construct a new property animator.
193 * @param[in] property The animatable property; only valid while the Animator is attached.
194 * @param[in] animatorFunction The function used to animate the property.
195 * @param[in] alphaFunction The alpha function to apply.
196 * @param[in] timePeriod The time period of this animation.
197 * @return A newly allocated animator.
199 static AnimatorBase* New( const PropertyBase& property,
200 AnimatorFunction animatorFunction,
201 AlphaFunction alphaFunction,
202 const TimePeriod& timePeriod )
204 typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
206 // The property was const in the actor-thread, but animators are used in the scene-graph thread.
207 AnimatorType* animator = new AnimatorType( const_cast<PropertyBase*>( &property ),
210 animator->SetAlphaFunc( alphaFunction );
211 animator->SetInitialDelay( timePeriod.delaySeconds );
212 animator->SetDuration( timePeriod.durationSeconds );
218 * Virtual destructor.
224 mPropertyOwner->RemoveObserver(*this);
230 * @param[in] propertyOwner The scene-object that owns the animatable property.
232 virtual void Attach( PropertyOwner* propertyOwner )
234 mPropertyOwner = propertyOwner;
238 mPropertyOwner->AddObserver(*this);
245 virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
247 // If the object dies, the animator has no effect
248 if ( mPropertyOwner )
250 float alpha = mAlphaFunc( progress );
252 const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
254 const PropertyType result = mAnimatorFunction( alpha, current );
258 mPropertyAccessor.Bake( bufferIndex, result );
262 mPropertyAccessor.Set( bufferIndex, result );
265 mCurrentProgress = progress;
268 return IsAttached(); // return false if orphaned
274 virtual bool IsAttached() const
276 return NULL != mPropertyOwner;
280 * Called when mPropertyOwner is disconnected from the scene graph.
282 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
284 // Bake the value if required
285 if ( mDisconnectAction != Dali::Animation::Discard )
287 // Bake to target-value if BakeFinal, otherwise bake current value
288 Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
291 mPropertyOwner = NULL;
292 mPropertyAccessor.Reset();
296 * Called shortly before mPropertyOwner is destroyed, along with its property.
298 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
300 mPropertyOwner = NULL;
301 mPropertyAccessor.Reset();
307 * Private constructor; see also Animator::New().
309 Animator( PropertyBase* property,
310 AnimatorFunction animatorFunction )
311 : mPropertyOwner( NULL ),
312 mPropertyAccessor( property ),
313 mAnimatorFunction( animatorFunction ),
314 mCurrentProgress( 0.0f )
319 Animator( const Animator& );
322 Animator& operator=( const Animator& );
326 PropertyOwner* mPropertyOwner;
327 PropertyAccessorType mPropertyAccessor;
329 AnimatorFunction mAnimatorFunction;
330 float mCurrentProgress;
333 } // namespace SceneGraph
336 // Common Update functions
338 struct AnimateByFloat
340 AnimateByFloat(const float& relativeValue)
341 : mRelative(relativeValue)
345 float operator()(float alpha, const float& property)
347 return float(property + mRelative * alpha);
353 struct AnimateToFloat
355 AnimateToFloat(const float& targetValue)
356 : mTarget(targetValue)
360 float operator()(float alpha, const float& property)
362 return float(property + ((mTarget - property) * alpha));
368 struct AnimateByInteger
370 AnimateByInteger(const int& relativeValue)
371 : mRelative(relativeValue)
375 float operator()(float alpha, const int& property)
377 return int(property + mRelative * alpha + 0.5f );
383 struct AnimateToInteger
385 AnimateToInteger(const int& targetValue)
386 : mTarget(targetValue)
390 float operator()(float alpha, const int& property)
392 return int(property + ((mTarget - property) * alpha) + 0.5f);
398 struct AnimateByVector2
400 AnimateByVector2(const Vector2& relativeValue)
401 : mRelative(relativeValue)
405 Vector2 operator()(float alpha, const Vector2& property)
407 return Vector2(property + mRelative * alpha);
413 struct AnimateToVector2
415 AnimateToVector2(const Vector2& targetValue)
416 : mTarget(targetValue)
420 Vector2 operator()(float alpha, const Vector2& property)
422 return Vector2(property + ((mTarget - property) * alpha));
428 struct AnimateByVector3
430 AnimateByVector3(const Vector3& relativeValue)
431 : mRelative(relativeValue)
435 Vector3 operator()(float alpha, const Vector3& property)
437 return Vector3(property + mRelative * alpha);
443 struct AnimateToVector3
445 AnimateToVector3(const Vector3& targetValue)
446 : mTarget(targetValue)
450 Vector3 operator()(float alpha, const Vector3& property)
452 return Vector3(property + ((mTarget - property) * alpha));
458 struct AnimateByVector4
460 AnimateByVector4(const Vector4& relativeValue)
461 : mRelative(relativeValue)
465 Vector4 operator()(float alpha, const Vector4& property)
467 return Vector4(property + mRelative * alpha);
473 struct AnimateToVector4
475 AnimateToVector4(const Vector4& targetValue)
476 : mTarget(targetValue)
480 Vector4 operator()(float alpha, const Vector4& property)
482 return Vector4(property + ((mTarget - property) * alpha));
488 struct AnimateByOpacity
490 AnimateByOpacity(const float& relativeValue)
491 : mRelative(relativeValue)
495 Vector4 operator()(float alpha, const Vector4& property)
497 Vector4 result(property);
498 result.a += mRelative * alpha;
506 struct AnimateToOpacity
508 AnimateToOpacity(const float& targetValue)
509 : mTarget(targetValue)
513 Vector4 operator()(float alpha, const Vector4& property)
515 Vector4 result(property);
516 result.a = property.a + ((mTarget - property.a) * alpha);
524 struct AnimateByBoolean
526 AnimateByBoolean(bool relativeValue)
527 : mRelative(relativeValue)
531 bool operator()(float alpha, const bool& property)
533 // Alpha is not useful here, just keeping to the same template as other update functors
534 return bool(alpha >= 1.0f ? (property || mRelative) : property);
540 struct AnimateToBoolean
542 AnimateToBoolean(bool targetValue)
543 : mTarget(targetValue)
547 bool operator()(float alpha, const bool& property)
549 // Alpha is not useful here, just keeping to the same template as other update functors
550 return bool(alpha >= 1.0f ? mTarget : property);
556 struct RotateByAngleAxis
558 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
559 : mAngleRadians(angleRadians),
560 mAxis(axis.x, axis.y, axis.z, 0.0f)
564 Quaternion operator()(float alpha, const Quaternion& rotation)
568 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
578 struct RotateToQuaternion
580 RotateToQuaternion(const Quaternion& targetValue)
581 : mTarget(targetValue)
585 Quaternion operator()(float alpha, const Quaternion& rotation)
587 return Quaternion::Slerp(rotation, mTarget, alpha);
594 struct KeyFrameBooleanFunctor
596 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
597 : mKeyFrames(keyFrames)
601 bool operator()(float progress, const bool& property)
603 if(mKeyFrames->IsActive(progress))
605 return mKeyFrames->GetValue(progress);
610 KeyFrameBooleanPtr mKeyFrames;
613 struct KeyFrameNumberFunctor
615 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames)
616 : mKeyFrames(keyFrames)
620 float operator()(float progress, const float& property)
622 if(mKeyFrames->IsActive(progress))
624 return mKeyFrames->GetValue(progress);
629 KeyFrameNumberPtr mKeyFrames;
632 struct KeyFrameIntegerFunctor
634 KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames)
635 : mKeyFrames(keyFrames)
639 float operator()(float progress, const int& property)
641 if(mKeyFrames->IsActive(progress))
643 return mKeyFrames->GetValue(progress);
648 KeyFrameIntegerPtr mKeyFrames;
651 struct KeyFrameVector2Functor
653 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
654 : mKeyFrames(keyFrames)
658 Vector2 operator()(float progress, const Vector2& property)
660 if(mKeyFrames->IsActive(progress))
662 return mKeyFrames->GetValue(progress);
667 KeyFrameVector2Ptr mKeyFrames;
671 struct KeyFrameVector3Functor
673 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames)
674 : mKeyFrames(keyFrames)
678 Vector3 operator()(float progress, const Vector3& property)
680 if(mKeyFrames->IsActive(progress))
682 return mKeyFrames->GetValue(progress);
687 KeyFrameVector3Ptr mKeyFrames;
690 struct KeyFrameVector4Functor
692 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames)
693 : mKeyFrames(keyFrames)
697 Vector4 operator()(float progress, const Vector4& property)
699 if(mKeyFrames->IsActive(progress))
701 return mKeyFrames->GetValue(progress);
706 KeyFrameVector4Ptr mKeyFrames;
709 struct KeyFrameQuaternionFunctor
711 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
712 : mKeyFrames(keyFrames)
716 Quaternion operator()(float progress, const Quaternion& property)
718 if(mKeyFrames->IsActive(progress))
720 return mKeyFrames->GetValue(progress);
725 KeyFrameQuaternionPtr mKeyFrames;
731 } // namespace Internal
735 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__