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/event/animation/path-impl.h>
28 #include <dali/internal/update/nodes/node.h>
29 #include <dali/internal/update/common/property-base.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>
43 typedef Dali::Animation::Interpolation Interpolation;
50 typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
52 typedef AnimatorContainer::Iterator AnimatorIter;
53 typedef AnimatorContainer::ConstIterator AnimatorConstIter;
56 * An abstract base class for Animators, which can be added to scene graph animations.
57 * Each animator changes a single property of an object in the scene graph.
63 typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
69 : mDurationSeconds(1.0f),
70 mInitialDelaySeconds(0.0f),
71 mAlphaFunc(AlphaFunctions::Linear),
72 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] action The disconnect action.
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 * This must be called when the animator is attached to the scene-graph.
183 * @pre The animatable scene object must also be attached to the scene-graph.
184 * @param[in] propertyOwner The scene-object that owns the animatable property.
186 virtual void Attach( PropertyOwner* propertyOwner ) = 0;
189 * Update the scene object attached to the animator.
190 * @param[in] bufferIndex The buffer to animate.
191 * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
192 * @param[in] bake Bake.
193 * @return True if the update was applied, false if the animator has been orphaned.
195 virtual bool Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
198 * Query whether the animator is still attached to a scene object.
199 * The attachment will be automatically severed, when the object is destroyed.
200 * @return True if the animator is attached.
202 virtual bool IsAttached() const = 0;
206 float mDurationSeconds;
207 float mInitialDelaySeconds;
209 AlphaFunc mAlphaFunc;
211 Dali::Animation::EndAction mDisconnectAction;
216 * An animator for a specific property type PropertyType.
218 template < typename PropertyType, typename PropertyAccessorType >
219 class Animator : public AnimatorBase, public PropertyOwner::Observer
223 typedef boost::function< PropertyType (float, const PropertyType&) > AnimatorFunction;
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 PropertyBase& property,
234 AnimatorFunction animatorFunction,
235 AlphaFunction alphaFunction,
236 const TimePeriod& timePeriod )
238 typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
240 // The property was const in the actor-thread, but animators are used in the scene-graph thread.
241 AnimatorType* animator = new AnimatorType( const_cast<PropertyBase*>( &property ),
244 animator->SetAlphaFunc( alphaFunction );
245 animator->SetInitialDelay( timePeriod.delaySeconds );
246 animator->SetDuration( timePeriod.durationSeconds );
252 * Virtual destructor.
258 mPropertyOwner->RemoveObserver(*this);
264 * @param[in] propertyOwner The scene-object that owns the animatable property.
266 virtual void Attach( PropertyOwner* propertyOwner )
268 mPropertyOwner = propertyOwner;
272 mPropertyOwner->AddObserver(*this);
279 virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
281 // If the object dies, the animator has no effect
282 if ( mPropertyOwner )
284 float alpha = mAlphaFunc( progress );
286 const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
288 const PropertyType result = mAnimatorFunction( alpha, current );
292 mPropertyAccessor.Bake( bufferIndex, result );
296 mPropertyAccessor.Set( bufferIndex, result );
299 mCurrentProgress = progress;
302 return IsAttached(); // return false if orphaned
308 virtual bool IsAttached() const
310 return NULL != mPropertyOwner;
314 * Called when mPropertyOwner is disconnected from the scene graph.
316 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
318 // If we are active, then bake the value if required
319 if ( mActive && mDisconnectAction != Dali::Animation::Discard )
321 // Bake to target-value if BakeFinal, otherwise bake current value
322 Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
326 mPropertyOwner = NULL;
327 mPropertyAccessor.Reset();
331 * Called shortly before mPropertyOwner is destroyed, along with its property.
333 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
335 mPropertyOwner = NULL;
336 mPropertyAccessor.Reset();
342 * Private constructor; see also Animator::New().
344 Animator( PropertyBase* property,
345 AnimatorFunction animatorFunction )
346 : mPropertyOwner( NULL ),
347 mPropertyAccessor( property ),
348 mAnimatorFunction( animatorFunction ),
349 mCurrentProgress( 0.0f )
354 Animator( const Animator& );
357 Animator& operator=( const Animator& );
361 PropertyOwner* mPropertyOwner;
362 PropertyAccessorType mPropertyAccessor;
364 AnimatorFunction mAnimatorFunction;
365 float mCurrentProgress;
368 } // namespace SceneGraph
371 // Common Update functions
373 struct AnimateByFloat
375 AnimateByFloat(const float& relativeValue)
376 : mRelative(relativeValue)
380 float operator()(float alpha, const float& property)
382 return float(property + mRelative * alpha);
388 struct AnimateToFloat
390 AnimateToFloat(const float& targetValue)
391 : mTarget(targetValue)
395 float operator()(float alpha, const float& property)
397 return float(property + ((mTarget - property) * alpha));
403 struct AnimateByInteger
405 AnimateByInteger(const int& relativeValue)
406 : mRelative(relativeValue)
410 float operator()(float alpha, const int& property)
412 return int(property + mRelative * alpha + 0.5f );
418 struct AnimateToInteger
420 AnimateToInteger(const int& targetValue)
421 : mTarget(targetValue)
425 float operator()(float alpha, const int& property)
427 return int(property + ((mTarget - property) * alpha) + 0.5f);
433 struct AnimateByVector2
435 AnimateByVector2(const Vector2& relativeValue)
436 : mRelative(relativeValue)
440 Vector2 operator()(float alpha, const Vector2& property)
442 return Vector2(property + mRelative * alpha);
448 struct AnimateToVector2
450 AnimateToVector2(const Vector2& targetValue)
451 : mTarget(targetValue)
455 Vector2 operator()(float alpha, const Vector2& property)
457 return Vector2(property + ((mTarget - property) * alpha));
463 struct AnimateByVector3
465 AnimateByVector3(const Vector3& relativeValue)
466 : mRelative(relativeValue)
470 Vector3 operator()(float alpha, const Vector3& property)
472 return Vector3(property + mRelative * alpha);
478 struct AnimateToVector3
480 AnimateToVector3(const Vector3& targetValue)
481 : mTarget(targetValue)
485 Vector3 operator()(float alpha, const Vector3& property)
487 return Vector3(property + ((mTarget - property) * alpha));
493 struct AnimateByVector4
495 AnimateByVector4(const Vector4& relativeValue)
496 : mRelative(relativeValue)
500 Vector4 operator()(float alpha, const Vector4& property)
502 return Vector4(property + mRelative * alpha);
508 struct AnimateToVector4
510 AnimateToVector4(const Vector4& targetValue)
511 : mTarget(targetValue)
515 Vector4 operator()(float alpha, const Vector4& property)
517 return Vector4(property + ((mTarget - property) * alpha));
523 struct AnimateByOpacity
525 AnimateByOpacity(const float& relativeValue)
526 : mRelative(relativeValue)
530 Vector4 operator()(float alpha, const Vector4& property)
532 Vector4 result(property);
533 result.a += mRelative * alpha;
541 struct AnimateToOpacity
543 AnimateToOpacity(const float& targetValue)
544 : mTarget(targetValue)
548 Vector4 operator()(float alpha, const Vector4& property)
550 Vector4 result(property);
551 result.a = property.a + ((mTarget - property.a) * alpha);
559 struct AnimateByBoolean
561 AnimateByBoolean(bool relativeValue)
562 : mRelative(relativeValue)
566 bool operator()(float alpha, const bool& property)
568 // Alpha is not useful here, just keeping to the same template as other update functors
569 return bool(alpha >= 1.0f ? (property || mRelative) : property);
575 struct AnimateToBoolean
577 AnimateToBoolean(bool targetValue)
578 : mTarget(targetValue)
582 bool operator()(float alpha, const bool& property)
584 // Alpha is not useful here, just keeping to the same template as other update functors
585 return bool(alpha >= 1.0f ? mTarget : property);
591 struct RotateByAngleAxis
593 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
594 : mAngleRadians(angleRadians),
595 mAxis(axis.x, axis.y, axis.z, 0.0f)
599 Quaternion operator()(float alpha, const Quaternion& rotation)
603 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
613 struct RotateToQuaternion
615 RotateToQuaternion(const Quaternion& targetValue)
616 : mTarget(targetValue)
620 Quaternion operator()(float alpha, const Quaternion& rotation)
622 return Quaternion::Slerp(rotation, mTarget, alpha);
629 struct KeyFrameBooleanFunctor
631 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
632 : mKeyFrames(keyFrames)
636 bool operator()(float progress, const bool& property)
638 if(mKeyFrames->IsActive(progress))
640 return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
645 KeyFrameBooleanPtr mKeyFrames;
648 struct KeyFrameNumberFunctor
650 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
651 : mKeyFrames(keyFrames),mInterpolation(interpolation)
655 float operator()(float progress, const float& property)
657 if(mKeyFrames->IsActive(progress))
659 return mKeyFrames->GetValue(progress, mInterpolation);
664 KeyFrameNumberPtr mKeyFrames;
665 Interpolation mInterpolation;
668 struct KeyFrameIntegerFunctor
670 KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
671 : mKeyFrames(keyFrames),mInterpolation(interpolation)
675 float operator()(float progress, const int& property)
677 if(mKeyFrames->IsActive(progress))
679 return mKeyFrames->GetValue(progress, mInterpolation);
684 KeyFrameIntegerPtr mKeyFrames;
685 Interpolation mInterpolation;
688 struct KeyFrameVector2Functor
690 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
691 : mKeyFrames(keyFrames),mInterpolation(interpolation)
695 Vector2 operator()(float progress, const Vector2& property)
697 if(mKeyFrames->IsActive(progress))
699 return mKeyFrames->GetValue(progress, mInterpolation);
704 KeyFrameVector2Ptr mKeyFrames;
705 Interpolation mInterpolation;
709 struct KeyFrameVector3Functor
711 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
712 : mKeyFrames(keyFrames),mInterpolation(interpolation)
716 Vector3 operator()(float progress, const Vector3& property)
718 if(mKeyFrames->IsActive(progress))
720 return mKeyFrames->GetValue(progress, mInterpolation);
725 KeyFrameVector3Ptr mKeyFrames;
726 Interpolation mInterpolation;
729 struct KeyFrameVector4Functor
731 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
732 : mKeyFrames(keyFrames),mInterpolation(interpolation)
736 Vector4 operator()(float progress, const Vector4& property)
738 if(mKeyFrames->IsActive(progress))
740 return mKeyFrames->GetValue(progress, mInterpolation);
745 KeyFrameVector4Ptr mKeyFrames;
746 Interpolation mInterpolation;
749 struct KeyFrameQuaternionFunctor
751 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
752 : mKeyFrames(keyFrames)
756 Quaternion operator()(float progress, const Quaternion& property)
758 if(mKeyFrames->IsActive(progress))
760 return mKeyFrames->GetValue(progress, Dali::Animation::Linear);
765 KeyFrameQuaternionPtr mKeyFrames;
768 struct PathPositionFunctor
770 PathPositionFunctor( PathPtr path )
775 Vector3 operator()(float progress, const Vector3& property)
777 return mPath->SamplePosition(progress );
783 struct PathRotationFunctor
785 PathRotationFunctor( PathPtr path, const Vector3& forward )
789 mForward.Normalize();
792 Quaternion operator()(float progress, const Quaternion& property)
794 Vector3 tangent( mPath->SampleTangent(progress) );
795 return Quaternion( mForward, tangent );
803 } // namespace Internal
807 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__