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 <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>
40 typedef Dali::Animation::Interpolation Interpolation;
42 struct AnimatorFunctionBase;
49 typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
51 typedef AnimatorContainer::Iterator AnimatorIter;
52 typedef AnimatorContainer::ConstIterator AnimatorConstIter;
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.
62 typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
68 : mDurationSeconds(1.0f),
69 mInitialDelaySeconds(0.0f),
70 mAlphaFunc(AlphaFunctions::Linear),
71 mDisconnectAction(Dali::Animation::BakeFinal),
80 virtual ~AnimatorBase()
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.
89 void SetDuration(float seconds)
91 DALI_ASSERT_DEBUG(seconds >= 0.0f);
93 mDurationSeconds = seconds;
97 * Retrieve the duration of the animator.
98 * @return The duration in seconds.
102 return mDurationSeconds;
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.
110 void SetInitialDelay(float seconds)
112 mInitialDelaySeconds = seconds;
116 * Retrieve the initial delay of the animator.
117 * @return The delay in seconds.
119 float GetInitialDelay()
121 return mInitialDelaySeconds;
125 * Set the alpha function for an animator.
126 * @param [in] alphaFunc The alpha function to apply to the animation progress.
128 void SetAlphaFunc(AlphaFunc alphaFunc)
130 mAlphaFunc = alphaFunc;
134 * Retrieve the alpha function of an animator.
135 * @return The function.
137 AlphaFunc GetAlphaFunc() const
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.
147 void SetDisconnectAction( Dali::Animation::EndAction action )
149 mDisconnectAction = action;
153 * Retrieve the disconnect action of an animator.
154 * @return The disconnect action.
156 Dali::Animation::EndAction GetDisconnectAction() const
158 return mDisconnectAction;
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.
167 void SetActive( bool active )
173 * Retrieve whether the animator has been set to active or not.
174 * @return The active state.
176 bool GetActive() const
182 * Retrive wheter the animator's target object is valid and on the stage.
183 * @return The enabled state.
185 bool IsEnabled() const
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.
195 virtual bool Orphan() = 0;
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.
203 virtual void Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
207 float mDurationSeconds;
208 float mInitialDelaySeconds;
210 AlphaFunc mAlphaFunc;
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.
218 * An animator for a specific property type PropertyType.
220 template < typename PropertyType, typename PropertyAccessorType >
221 class Animator : public AnimatorBase, public PropertyOwner::Observer
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.
233 static AnimatorBase* New( const PropertyOwner& propertyOwner,
234 const PropertyBase& property,
235 AnimatorFunctionBase* animatorFunction,
236 AlphaFunction alphaFunction,
237 const TimePeriod& timePeriod )
239 typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
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 ),
246 animator->SetAlphaFunc( alphaFunction );
247 animator->SetInitialDelay( timePeriod.delaySeconds );
248 animator->SetDuration( timePeriod.durationSeconds );
254 * Virtual destructor.
260 mPropertyOwner->RemoveObserver(*this);
263 if( mAnimatorFunction )
265 delete mAnimatorFunction;
270 * Called when mPropertyOwner is connected to the scene graph.
272 virtual void PropertyOwnerConnected( PropertyOwner& owner )
278 * Called when mPropertyOwner is disconnected from the scene graph.
280 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
282 // If we are active, then bake the value if required
283 if ( mActive && mDisconnectAction != Dali::Animation::Discard )
285 // Bake to target-value if BakeFinal, otherwise bake current value
286 Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
294 * Called shortly before mPropertyOwner is destroyed
296 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
298 mPropertyOwner = NULL;
299 mPropertyAccessor.Reset();
306 virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
308 float alpha = mAlphaFunc( progress );
309 const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
311 const PropertyType result = (*mAnimatorFunction)( alpha, current );
314 mPropertyAccessor.Bake( bufferIndex, result );
318 mPropertyAccessor.Set( bufferIndex, result );
321 mCurrentProgress = progress;
327 virtual bool Orphan()
329 return (mPropertyOwner == NULL);
335 * Private constructor; see also Animator::New().
337 Animator( PropertyOwner* propertyOwner,
338 PropertyBase* property,
339 AnimatorFunctionBase* animatorFunction )
340 : mPropertyOwner( propertyOwner ),
341 mPropertyAccessor( property ),
342 mAnimatorFunction( animatorFunction ),
343 mCurrentProgress( 0.0f )
345 mPropertyOwner->AddObserver(*this);
349 Animator( const Animator& );
352 Animator& operator=( const Animator& );
356 PropertyOwner* mPropertyOwner;
357 PropertyAccessorType mPropertyAccessor;
359 AnimatorFunctionBase* mAnimatorFunction;
360 float mCurrentProgress;
363 } // namespace SceneGraph
366 * AnimatorFunction base class.
367 * All update functions must inherit from AnimatorFunctionBase and overload the appropiate "()" operator
369 struct AnimatorFunctionBase
374 AnimatorFunctionBase(){}
377 * Virtual destructor (Intended as base class)
379 virtual ~AnimatorFunctionBase(){}
381 ///Stub "()" operators.
382 virtual bool operator()(float progress, const bool& property)
387 virtual float operator()(float progress, const int& property)
392 virtual float operator()(float progress, const unsigned int& property)
397 virtual float operator()(float progress, const float& property)
402 virtual Vector2 operator()(float progress, const Vector2& property)
407 virtual Vector3 operator()(float progress, const Vector3& property)
412 virtual Vector4 operator()(float progress, const Vector4& property)
417 virtual Quaternion operator()(float progress, const Quaternion& property)
425 struct AnimateByInteger : public AnimatorFunctionBase
427 AnimateByInteger(const int& relativeValue)
428 : mRelative(relativeValue)
432 float operator()(float alpha, const int& property)
434 return int(property + mRelative * alpha + 0.5f );
440 struct AnimateToInteger : public AnimatorFunctionBase
442 AnimateToInteger(const int& targetValue)
443 : mTarget(targetValue)
447 float operator()(float alpha, const int& property)
449 return int(property + ((mTarget - property) * alpha) + 0.5f);
455 struct AnimateByUnsignedInteger : public AnimatorFunctionBase
457 AnimateByUnsignedInteger(const unsigned int& relativeValue)
458 : mRelative(relativeValue)
462 float operator()(float alpha, const unsigned int& property)
464 return static_cast<unsigned int>(property + mRelative * alpha + 0.5f );
467 unsigned int mRelative;
470 struct AnimateToUnsignedInteger : public AnimatorFunctionBase
472 AnimateToUnsignedInteger(const unsigned int& targetValue)
473 : mTarget(targetValue)
477 float operator()(float alpha, const unsigned int& property)
479 return static_cast<unsigned int>(property + ((mTarget - property) * alpha) + 0.5f);
482 unsigned int mTarget;
485 struct AnimateByFloat : public AnimatorFunctionBase
487 AnimateByFloat(const float& relativeValue)
488 : mRelative(relativeValue)
492 float operator()(float alpha, const float& property)
494 return float(property + mRelative * alpha);
500 struct AnimateToFloat : public AnimatorFunctionBase
502 AnimateToFloat(const float& targetValue)
503 : mTarget(targetValue)
507 float operator()(float alpha, const float& property)
509 return float(property + ((mTarget - property) * alpha));
515 struct AnimateByVector2 : public AnimatorFunctionBase
517 AnimateByVector2(const Vector2& relativeValue)
518 : mRelative(relativeValue)
522 Vector2 operator()(float alpha, const Vector2& property)
524 return Vector2(property + mRelative * alpha);
530 struct AnimateToVector2 : public AnimatorFunctionBase
532 AnimateToVector2(const Vector2& targetValue)
533 : mTarget(targetValue)
537 Vector2 operator()(float alpha, const Vector2& property)
539 return Vector2(property + ((mTarget - property) * alpha));
545 struct AnimateByVector3 : public AnimatorFunctionBase
547 AnimateByVector3(const Vector3& relativeValue)
548 : mRelative(relativeValue)
552 Vector3 operator()(float alpha, const Vector3& property)
554 return Vector3(property + mRelative * alpha);
560 struct AnimateToVector3 : public AnimatorFunctionBase
562 AnimateToVector3(const Vector3& targetValue)
563 : mTarget(targetValue)
567 Vector3 operator()(float alpha, const Vector3& property)
569 return Vector3(property + ((mTarget - property) * alpha));
575 struct AnimateByVector4 : public AnimatorFunctionBase
577 AnimateByVector4(const Vector4& relativeValue)
578 : mRelative(relativeValue)
582 Vector4 operator()(float alpha, const Vector4& property)
584 return Vector4(property + mRelative * alpha);
590 struct AnimateToVector4 : public AnimatorFunctionBase
592 AnimateToVector4(const Vector4& targetValue)
593 : mTarget(targetValue)
597 Vector4 operator()(float alpha, const Vector4& property)
599 return Vector4(property + ((mTarget - property) * alpha));
605 struct AnimateByOpacity : public AnimatorFunctionBase
607 AnimateByOpacity(const float& relativeValue)
608 : mRelative(relativeValue)
612 Vector4 operator()(float alpha, const Vector4& property)
614 Vector4 result(property);
615 result.a += mRelative * alpha;
623 struct AnimateToOpacity : public AnimatorFunctionBase
625 AnimateToOpacity(const float& targetValue)
626 : mTarget(targetValue)
630 Vector4 operator()(float alpha, const Vector4& property)
632 Vector4 result(property);
633 result.a = property.a + ((mTarget - property.a) * alpha);
641 struct AnimateByBoolean : public AnimatorFunctionBase
643 AnimateByBoolean(bool relativeValue)
644 : mRelative(relativeValue)
648 bool operator()(float alpha, const bool& property)
650 // Alpha is not useful here, just keeping to the same template as other update functors
651 return bool(alpha >= 1.0f ? (property || mRelative) : property);
657 struct AnimateToBoolean : public AnimatorFunctionBase
659 AnimateToBoolean(bool targetValue)
660 : mTarget(targetValue)
664 bool operator()(float alpha, const bool& property)
666 // Alpha is not useful here, just keeping to the same template as other update functors
667 return bool(alpha >= 1.0f ? mTarget : property);
673 struct RotateByAngleAxis : public AnimatorFunctionBase
675 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
676 : mAngleRadians( angleRadians ),
677 mAxis(axis.x, axis.y, axis.z)
681 Quaternion operator()(float alpha, const Quaternion& rotation)
685 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
691 Radian mAngleRadians;
695 struct RotateToQuaternion : public AnimatorFunctionBase
697 RotateToQuaternion(const Quaternion& targetValue)
698 : mTarget(targetValue)
702 Quaternion operator()(float alpha, const Quaternion& rotation)
704 return Quaternion::Slerp(rotation, mTarget, alpha);
711 struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
713 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
714 : mKeyFrames(keyFrames)
718 bool operator()(float progress, const bool& property)
720 if(mKeyFrames->IsActive(progress))
722 return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
727 KeyFrameBooleanPtr mKeyFrames;
730 struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
732 KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
733 : mKeyFrames(keyFrames),mInterpolation(interpolation)
737 float operator()(float progress, const int& property)
739 if(mKeyFrames->IsActive(progress))
741 return mKeyFrames->GetValue(progress, mInterpolation);
746 KeyFrameIntegerPtr mKeyFrames;
747 Interpolation mInterpolation;
750 struct KeyFrameUnsignedIntegerFunctor : public AnimatorFunctionBase
752 KeyFrameUnsignedIntegerFunctor(KeyFrameUnsignedIntegerPtr keyFrames, Interpolation interpolation)
753 : mKeyFrames(keyFrames),mInterpolation(interpolation)
757 float operator()(float progress, const unsigned int& property)
759 if(mKeyFrames->IsActive(progress))
761 return mKeyFrames->GetValue(progress, mInterpolation);
766 KeyFrameUnsignedIntegerPtr mKeyFrames;
767 Interpolation mInterpolation;
770 struct KeyFrameNumberFunctor : public AnimatorFunctionBase
772 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
773 : mKeyFrames(keyFrames),mInterpolation(interpolation)
777 float operator()(float progress, const float& property)
779 if(mKeyFrames->IsActive(progress))
781 return mKeyFrames->GetValue(progress, mInterpolation);
786 KeyFrameNumberPtr mKeyFrames;
787 Interpolation mInterpolation;
790 struct KeyFrameVector2Functor : public AnimatorFunctionBase
792 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
793 : mKeyFrames(keyFrames),mInterpolation(interpolation)
797 Vector2 operator()(float progress, const Vector2& property)
799 if(mKeyFrames->IsActive(progress))
801 return mKeyFrames->GetValue(progress, mInterpolation);
806 KeyFrameVector2Ptr mKeyFrames;
807 Interpolation mInterpolation;
811 struct KeyFrameVector3Functor : public AnimatorFunctionBase
813 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
814 : mKeyFrames(keyFrames),mInterpolation(interpolation)
818 Vector3 operator()(float progress, const Vector3& property)
820 if(mKeyFrames->IsActive(progress))
822 return mKeyFrames->GetValue(progress, mInterpolation);
827 KeyFrameVector3Ptr mKeyFrames;
828 Interpolation mInterpolation;
831 struct KeyFrameVector4Functor : public AnimatorFunctionBase
833 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
834 : mKeyFrames(keyFrames),mInterpolation(interpolation)
838 Vector4 operator()(float progress, const Vector4& property)
840 if(mKeyFrames->IsActive(progress))
842 return mKeyFrames->GetValue(progress, mInterpolation);
847 KeyFrameVector4Ptr mKeyFrames;
848 Interpolation mInterpolation;
851 struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase
853 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
854 : mKeyFrames(keyFrames)
858 Quaternion operator()(float progress, const Quaternion& property)
860 if(mKeyFrames->IsActive(progress))
862 return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
867 KeyFrameQuaternionPtr mKeyFrames;
870 struct PathPositionFunctor : public AnimatorFunctionBase
872 PathPositionFunctor( PathPtr path )
877 Vector3 operator()(float progress, const Vector3& property)
879 return mPath->SamplePosition(progress );
885 struct PathRotationFunctor : public AnimatorFunctionBase
887 PathRotationFunctor( PathPtr path, const Vector3& forward )
891 mForward.Normalize();
894 Quaternion operator()(float progress, const Quaternion& property)
896 Vector3 tangent( mPath->SampleTangent(progress) );
897 return Quaternion( mForward, tangent );
905 } // namespace Internal
909 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__