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 float operator()(float progress, const int& property)
387 virtual float operator()(float progress, const float& property)
392 virtual bool operator()(float progress, const bool& property)
397 virtual Vector2 operator()(float progress, const Vector2& property)
402 virtual Vector3 operator()(float progress, const Vector3& property)
407 virtual Vector4 operator()(float progress, const Vector4& property)
412 virtual Quaternion operator()(float progress, const Quaternion& property)
420 struct AnimateByFloat : public AnimatorFunctionBase
422 AnimateByFloat(const float& relativeValue)
423 : mRelative(relativeValue)
427 float operator()(float alpha, const float& property)
429 return float(property + mRelative * alpha);
435 struct AnimateToFloat : public AnimatorFunctionBase
437 AnimateToFloat(const float& targetValue)
438 : mTarget(targetValue)
442 float operator()(float alpha, const float& property)
444 return float(property + ((mTarget - property) * alpha));
450 struct AnimateByInteger : public AnimatorFunctionBase
452 AnimateByInteger(const int& relativeValue)
453 : mRelative(relativeValue)
457 float operator()(float alpha, const int& property)
459 return int(property + mRelative * alpha + 0.5f );
465 struct AnimateToInteger : public AnimatorFunctionBase
467 AnimateToInteger(const int& targetValue)
468 : mTarget(targetValue)
472 float operator()(float alpha, const int& property)
474 return int(property + ((mTarget - property) * alpha) + 0.5f);
480 struct AnimateByVector2 : public AnimatorFunctionBase
482 AnimateByVector2(const Vector2& relativeValue)
483 : mRelative(relativeValue)
487 Vector2 operator()(float alpha, const Vector2& property)
489 return Vector2(property + mRelative * alpha);
495 struct AnimateToVector2 : public AnimatorFunctionBase
497 AnimateToVector2(const Vector2& targetValue)
498 : mTarget(targetValue)
502 Vector2 operator()(float alpha, const Vector2& property)
504 return Vector2(property + ((mTarget - property) * alpha));
510 struct AnimateByVector3 : public AnimatorFunctionBase
512 AnimateByVector3(const Vector3& relativeValue)
513 : mRelative(relativeValue)
517 Vector3 operator()(float alpha, const Vector3& property)
519 return Vector3(property + mRelative * alpha);
525 struct AnimateToVector3 : public AnimatorFunctionBase
527 AnimateToVector3(const Vector3& targetValue)
528 : mTarget(targetValue)
532 Vector3 operator()(float alpha, const Vector3& property)
534 return Vector3(property + ((mTarget - property) * alpha));
540 struct AnimateByVector4 : public AnimatorFunctionBase
542 AnimateByVector4(const Vector4& relativeValue)
543 : mRelative(relativeValue)
547 Vector4 operator()(float alpha, const Vector4& property)
549 return Vector4(property + mRelative * alpha);
555 struct AnimateToVector4 : public AnimatorFunctionBase
557 AnimateToVector4(const Vector4& targetValue)
558 : mTarget(targetValue)
562 Vector4 operator()(float alpha, const Vector4& property)
564 return Vector4(property + ((mTarget - property) * alpha));
570 struct AnimateByOpacity : public AnimatorFunctionBase
572 AnimateByOpacity(const float& relativeValue)
573 : mRelative(relativeValue)
577 Vector4 operator()(float alpha, const Vector4& property)
579 Vector4 result(property);
580 result.a += mRelative * alpha;
588 struct AnimateToOpacity : public AnimatorFunctionBase
590 AnimateToOpacity(const float& targetValue)
591 : mTarget(targetValue)
595 Vector4 operator()(float alpha, const Vector4& property)
597 Vector4 result(property);
598 result.a = property.a + ((mTarget - property.a) * alpha);
606 struct AnimateByBoolean : public AnimatorFunctionBase
608 AnimateByBoolean(bool relativeValue)
609 : mRelative(relativeValue)
613 bool operator()(float alpha, const bool& property)
615 // Alpha is not useful here, just keeping to the same template as other update functors
616 return bool(alpha >= 1.0f ? (property || mRelative) : property);
622 struct AnimateToBoolean : public AnimatorFunctionBase
624 AnimateToBoolean(bool targetValue)
625 : mTarget(targetValue)
629 bool operator()(float alpha, const bool& property)
631 // Alpha is not useful here, just keeping to the same template as other update functors
632 return bool(alpha >= 1.0f ? mTarget : property);
638 struct RotateByAngleAxis : public AnimatorFunctionBase
640 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
641 : mAngleRadians( angleRadians ),
642 mAxis(axis.x, axis.y, axis.z)
646 Quaternion operator()(float alpha, const Quaternion& rotation)
650 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
656 Radian mAngleRadians;
660 struct RotateToQuaternion : public AnimatorFunctionBase
662 RotateToQuaternion(const Quaternion& targetValue)
663 : mTarget(targetValue)
667 Quaternion operator()(float alpha, const Quaternion& rotation)
669 return Quaternion::Slerp(rotation, mTarget, alpha);
676 struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
678 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
679 : mKeyFrames(keyFrames)
683 bool operator()(float progress, const bool& property)
685 if(mKeyFrames->IsActive(progress))
687 return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
692 KeyFrameBooleanPtr mKeyFrames;
695 struct KeyFrameNumberFunctor : public AnimatorFunctionBase
697 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
698 : mKeyFrames(keyFrames),mInterpolation(interpolation)
702 float operator()(float progress, const float& property)
704 if(mKeyFrames->IsActive(progress))
706 return mKeyFrames->GetValue(progress, mInterpolation);
711 KeyFrameNumberPtr mKeyFrames;
712 Interpolation mInterpolation;
715 struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
717 KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
718 : mKeyFrames(keyFrames),mInterpolation(interpolation)
722 float operator()(float progress, const int& property)
724 if(mKeyFrames->IsActive(progress))
726 return mKeyFrames->GetValue(progress, mInterpolation);
731 KeyFrameIntegerPtr mKeyFrames;
732 Interpolation mInterpolation;
735 struct KeyFrameVector2Functor : public AnimatorFunctionBase
737 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
738 : mKeyFrames(keyFrames),mInterpolation(interpolation)
742 Vector2 operator()(float progress, const Vector2& property)
744 if(mKeyFrames->IsActive(progress))
746 return mKeyFrames->GetValue(progress, mInterpolation);
751 KeyFrameVector2Ptr mKeyFrames;
752 Interpolation mInterpolation;
756 struct KeyFrameVector3Functor : public AnimatorFunctionBase
758 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
759 : mKeyFrames(keyFrames),mInterpolation(interpolation)
763 Vector3 operator()(float progress, const Vector3& property)
765 if(mKeyFrames->IsActive(progress))
767 return mKeyFrames->GetValue(progress, mInterpolation);
772 KeyFrameVector3Ptr mKeyFrames;
773 Interpolation mInterpolation;
776 struct KeyFrameVector4Functor : public AnimatorFunctionBase
778 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
779 : mKeyFrames(keyFrames),mInterpolation(interpolation)
783 Vector4 operator()(float progress, const Vector4& property)
785 if(mKeyFrames->IsActive(progress))
787 return mKeyFrames->GetValue(progress, mInterpolation);
792 KeyFrameVector4Ptr mKeyFrames;
793 Interpolation mInterpolation;
796 struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase
798 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
799 : mKeyFrames(keyFrames)
803 Quaternion operator()(float progress, const Quaternion& property)
805 if(mKeyFrames->IsActive(progress))
807 return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
812 KeyFrameQuaternionPtr mKeyFrames;
815 struct PathPositionFunctor : public AnimatorFunctionBase
817 PathPositionFunctor( PathPtr path )
822 Vector3 operator()(float progress, const Vector3& property)
824 return mPath->SamplePosition(progress );
830 struct PathRotationFunctor : public AnimatorFunctionBase
832 PathRotationFunctor( PathPtr path, const Vector3& forward )
836 mForward.Normalize();
839 Quaternion operator()(float progress, const Quaternion& property)
841 Vector3 tangent( mPath->SampleTangent(progress) );
842 return Quaternion( mForward, tangent );
850 } // namespace Internal
854 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__