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),
78 virtual ~AnimatorBase()
83 * Set the duration of the animator.
84 * @pre durationSeconds must be zero or greater; zero is useful when animating boolean values.
85 * @param [in] seconds Duration in seconds.
87 void SetDuration(float seconds)
89 DALI_ASSERT_DEBUG(seconds >= 0.0f);
91 mDurationSeconds = seconds;
95 * Retrieve the duration of the animator.
96 * @return The duration in seconds.
100 return mDurationSeconds;
104 * Set the delay before the animator should take effect.
105 * The default is zero i.e. no delay.
106 * @param [in] seconds The delay in seconds.
108 void SetInitialDelay(float seconds)
110 mInitialDelaySeconds = seconds;
114 * Retrieve the initial delay of the animator.
115 * @return The delay in seconds.
117 float GetInitialDelay()
119 return mInitialDelaySeconds;
123 * Set the alpha function for an animator.
124 * @param [in] alphaFunc The alpha function to apply to the animation progress.
126 void SetAlphaFunc(AlphaFunc alphaFunc)
128 mAlphaFunc = alphaFunc;
132 * Retrieve the alpha function of an animator.
133 * @return The function.
135 AlphaFunc GetAlphaFunc() const
141 * Whether to bake the animation if attached property owner is disconnected.
142 * Property is only baked if the animator is active.
143 * @param [in] action The disconnect action.
145 void SetDisconnectAction( Dali::Animation::EndAction action )
147 mDisconnectAction = action;
151 * Retrieve the disconnect action of an animator.
152 * @return The disconnect action.
154 Dali::Animation::EndAction GetDisconnectAction() const
156 return mDisconnectAction;
160 * Whether the animator is active or not.
161 * @param [in] action The disconnect action.
162 * @post When the animator becomes active, it applies the disconnect-action if the property owner is then disconnected.
163 * @note When the property owner is disconnected, the active state is set to false.
165 void SetActive( bool active )
171 * Retrieve whether the animator has been set to active or not.
172 * @return The active state.
174 bool GetActive() const
180 * This must be called when the animator is attached to the scene-graph.
181 * @pre The animatable scene object must also be attached to the scene-graph.
182 * @param[in] propertyOwner The scene-object that owns the animatable property.
184 virtual void Attach( PropertyOwner* propertyOwner ) = 0;
187 * Update the scene object attached to the animator.
188 * @param[in] bufferIndex The buffer to animate.
189 * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
190 * @param[in] bake Bake.
191 * @return True if the update was applied, false if the animator has been orphaned.
193 virtual bool Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
196 * Query whether the animator is still attached to a scene object.
197 * The attachment will be automatically severed, when the object is destroyed.
198 * @return True if the animator is attached.
200 virtual bool IsAttached() const = 0;
204 float mDurationSeconds;
205 float mInitialDelaySeconds;
207 AlphaFunc mAlphaFunc;
209 Dali::Animation::EndAction mDisconnectAction;
214 * An animator for a specific property type PropertyType.
216 template < typename PropertyType, typename PropertyAccessorType >
217 class Animator : public AnimatorBase, public PropertyOwner::Observer
221 typedef boost::function< PropertyType (float, const PropertyType&) > AnimatorFunction;
224 * Construct a new property animator.
225 * @param[in] property The animatable property; only valid while the Animator is attached.
226 * @param[in] animatorFunction The function used to animate the property.
227 * @param[in] alphaFunction The alpha function to apply.
228 * @param[in] timePeriod The time period of this animation.
229 * @return A newly allocated animator.
231 static AnimatorBase* New( const PropertyBase& property,
232 AnimatorFunction animatorFunction,
233 AlphaFunction alphaFunction,
234 const TimePeriod& timePeriod )
236 typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
238 // The property was const in the actor-thread, but animators are used in the scene-graph thread.
239 AnimatorType* animator = new AnimatorType( const_cast<PropertyBase*>( &property ),
242 animator->SetAlphaFunc( alphaFunction );
243 animator->SetInitialDelay( timePeriod.delaySeconds );
244 animator->SetDuration( timePeriod.durationSeconds );
250 * Virtual destructor.
256 mPropertyOwner->RemoveObserver(*this);
262 * @param[in] propertyOwner The scene-object that owns the animatable property.
264 virtual void Attach( PropertyOwner* propertyOwner )
266 mPropertyOwner = propertyOwner;
270 mPropertyOwner->AddObserver(*this);
277 virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
279 // If the object dies, the animator has no effect
280 if ( mPropertyOwner )
282 float alpha = mAlphaFunc( progress );
284 const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
286 const PropertyType result = mAnimatorFunction( alpha, current );
290 mPropertyAccessor.Bake( bufferIndex, result );
294 mPropertyAccessor.Set( bufferIndex, result );
297 mCurrentProgress = progress;
300 return IsAttached(); // return false if orphaned
306 virtual bool IsAttached() const
308 return NULL != mPropertyOwner;
312 * Called when mPropertyOwner is disconnected from the scene graph.
314 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
316 // If we are active, then bake the value if required
317 if ( mActive && mDisconnectAction != Dali::Animation::Discard )
319 // Bake to target-value if BakeFinal, otherwise bake current value
320 Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
324 mPropertyOwner = NULL;
325 mPropertyAccessor.Reset();
329 * Called shortly before mPropertyOwner is destroyed, along with its property.
331 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
333 mPropertyOwner = NULL;
334 mPropertyAccessor.Reset();
340 * Private constructor; see also Animator::New().
342 Animator( PropertyBase* property,
343 AnimatorFunction animatorFunction )
344 : mPropertyOwner( NULL ),
345 mPropertyAccessor( property ),
346 mAnimatorFunction( animatorFunction ),
347 mCurrentProgress( 0.0f )
352 Animator( const Animator& );
355 Animator& operator=( const Animator& );
359 PropertyOwner* mPropertyOwner;
360 PropertyAccessorType mPropertyAccessor;
362 AnimatorFunction mAnimatorFunction;
363 float mCurrentProgress;
366 } // namespace SceneGraph
369 // Common Update functions
371 struct AnimateByFloat
373 AnimateByFloat(const float& relativeValue)
374 : mRelative(relativeValue)
378 float operator()(float alpha, const float& property)
380 return float(property + mRelative * alpha);
386 struct AnimateToFloat
388 AnimateToFloat(const float& targetValue)
389 : mTarget(targetValue)
393 float operator()(float alpha, const float& property)
395 return float(property + ((mTarget - property) * alpha));
401 struct AnimateByInteger
403 AnimateByInteger(const int& relativeValue)
404 : mRelative(relativeValue)
408 float operator()(float alpha, const int& property)
410 return int(property + mRelative * alpha + 0.5f );
416 struct AnimateToInteger
418 AnimateToInteger(const int& targetValue)
419 : mTarget(targetValue)
423 float operator()(float alpha, const int& property)
425 return int(property + ((mTarget - property) * alpha) + 0.5f);
431 struct AnimateByVector2
433 AnimateByVector2(const Vector2& relativeValue)
434 : mRelative(relativeValue)
438 Vector2 operator()(float alpha, const Vector2& property)
440 return Vector2(property + mRelative * alpha);
446 struct AnimateToVector2
448 AnimateToVector2(const Vector2& targetValue)
449 : mTarget(targetValue)
453 Vector2 operator()(float alpha, const Vector2& property)
455 return Vector2(property + ((mTarget - property) * alpha));
461 struct AnimateByVector3
463 AnimateByVector3(const Vector3& relativeValue)
464 : mRelative(relativeValue)
468 Vector3 operator()(float alpha, const Vector3& property)
470 return Vector3(property + mRelative * alpha);
476 struct AnimateToVector3
478 AnimateToVector3(const Vector3& targetValue)
479 : mTarget(targetValue)
483 Vector3 operator()(float alpha, const Vector3& property)
485 return Vector3(property + ((mTarget - property) * alpha));
491 struct AnimateByVector4
493 AnimateByVector4(const Vector4& relativeValue)
494 : mRelative(relativeValue)
498 Vector4 operator()(float alpha, const Vector4& property)
500 return Vector4(property + mRelative * alpha);
506 struct AnimateToVector4
508 AnimateToVector4(const Vector4& targetValue)
509 : mTarget(targetValue)
513 Vector4 operator()(float alpha, const Vector4& property)
515 return Vector4(property + ((mTarget - property) * alpha));
521 struct AnimateByOpacity
523 AnimateByOpacity(const float& relativeValue)
524 : mRelative(relativeValue)
528 Vector4 operator()(float alpha, const Vector4& property)
530 Vector4 result(property);
531 result.a += mRelative * alpha;
539 struct AnimateToOpacity
541 AnimateToOpacity(const float& targetValue)
542 : mTarget(targetValue)
546 Vector4 operator()(float alpha, const Vector4& property)
548 Vector4 result(property);
549 result.a = property.a + ((mTarget - property.a) * alpha);
557 struct AnimateByBoolean
559 AnimateByBoolean(bool relativeValue)
560 : mRelative(relativeValue)
564 bool operator()(float alpha, const bool& property)
566 // Alpha is not useful here, just keeping to the same template as other update functors
567 return bool(alpha >= 1.0f ? (property || mRelative) : property);
573 struct AnimateToBoolean
575 AnimateToBoolean(bool targetValue)
576 : mTarget(targetValue)
580 bool operator()(float alpha, const bool& property)
582 // Alpha is not useful here, just keeping to the same template as other update functors
583 return bool(alpha >= 1.0f ? mTarget : property);
589 struct RotateByAngleAxis
591 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
592 : mAngleRadians(angleRadians),
593 mAxis(axis.x, axis.y, axis.z, 0.0f)
597 Quaternion operator()(float alpha, const Quaternion& rotation)
601 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
611 struct RotateToQuaternion
613 RotateToQuaternion(const Quaternion& targetValue)
614 : mTarget(targetValue)
618 Quaternion operator()(float alpha, const Quaternion& rotation)
620 return Quaternion::Slerp(rotation, mTarget, alpha);
627 struct KeyFrameBooleanFunctor
629 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
630 : mKeyFrames(keyFrames)
634 bool operator()(float progress, const bool& property)
636 if(mKeyFrames->IsActive(progress))
638 return mKeyFrames->GetValue(progress);
643 KeyFrameBooleanPtr mKeyFrames;
646 struct KeyFrameNumberFunctor
648 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames)
649 : mKeyFrames(keyFrames)
653 float operator()(float progress, const float& property)
655 if(mKeyFrames->IsActive(progress))
657 return mKeyFrames->GetValue(progress);
662 KeyFrameNumberPtr mKeyFrames;
665 struct KeyFrameIntegerFunctor
667 KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames)
668 : mKeyFrames(keyFrames)
672 float operator()(float progress, const int& property)
674 if(mKeyFrames->IsActive(progress))
676 return mKeyFrames->GetValue(progress);
681 KeyFrameIntegerPtr mKeyFrames;
684 struct KeyFrameVector2Functor
686 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
687 : mKeyFrames(keyFrames)
691 Vector2 operator()(float progress, const Vector2& property)
693 if(mKeyFrames->IsActive(progress))
695 return mKeyFrames->GetValue(progress);
700 KeyFrameVector2Ptr mKeyFrames;
704 struct KeyFrameVector3Functor
706 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames)
707 : mKeyFrames(keyFrames)
711 Vector3 operator()(float progress, const Vector3& property)
713 if(mKeyFrames->IsActive(progress))
715 return mKeyFrames->GetValue(progress);
720 KeyFrameVector3Ptr mKeyFrames;
723 struct KeyFrameVector4Functor
725 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames)
726 : mKeyFrames(keyFrames)
730 Vector4 operator()(float progress, const Vector4& property)
732 if(mKeyFrames->IsActive(progress))
734 return mKeyFrames->GetValue(progress);
739 KeyFrameVector4Ptr mKeyFrames;
742 struct KeyFrameQuaternionFunctor
744 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
745 : mKeyFrames(keyFrames)
749 Quaternion operator()(float progress, const Quaternion& property)
751 if(mKeyFrames->IsActive(progress))
753 return mKeyFrames->GetValue(progress);
758 KeyFrameQuaternionPtr mKeyFrames;
764 } // namespace Internal
768 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__