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/public-api/animation/alpha-functions.h>
30 #include <dali/public-api/animation/animation.h>
31 #include <dali/public-api/animation/time-period.h>
32 #include <dali/public-api/common/dali-common.h>
33 #include <dali/public-api/math/quaternion.h>
34 #include <dali/public-api/math/radian.h>
47 typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
49 typedef AnimatorContainer::Iterator AnimatorIter;
50 typedef AnimatorContainer::ConstIterator AnimatorConstIter;
53 * An abstract base class for Animators, which can be added to scene graph animations.
54 * Each animator changes a single property of an object in the scene graph.
60 typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
66 : mDurationSeconds(1.0f),
67 mInitialDelaySeconds(0.0f),
68 mAlphaFunc(AlphaFunctions::Linear),
69 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 * Property is only baked if the animator is active.
142 * @param [in] action The disconnect action.
144 void SetDisconnectAction( Dali::Animation::EndAction action )
146 mDisconnectAction = action;
150 * Retrieve the disconnect action of an animator.
151 * @return The disconnect action.
153 Dali::Animation::EndAction GetDisconnectAction() const
155 return mDisconnectAction;
159 * Whether the animator is active or not.
160 * @param [in] action The disconnect action.
161 * @post When the animator becomes active, it applies the disconnect-action if the property owner is then disconnected.
162 * @note When the property owner is disconnected, the active state is set to false.
164 void SetActive( bool active )
170 * Retrieve whether the animator has been set to active or not.
171 * @return The active state.
173 bool GetActive() const
179 * This must be called when the animator is attached to the scene-graph.
180 * @pre The animatable scene object must also be attached to the scene-graph.
181 * @param[in] propertyOwner The scene-object that owns the animatable property.
183 virtual void Attach( PropertyOwner* propertyOwner ) = 0;
186 * Update the scene object attached to the animator.
187 * @param[in] bufferIndex The buffer to animate.
188 * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
189 * @param[in] bake Bake.
190 * @return True if the update was applied, false if the animator has been orphaned.
192 virtual bool Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
195 * Query whether the animator is still attached to a scene object.
196 * The attachment will be automatically severed, when the object is destroyed.
197 * @return True if the animator is attached.
199 virtual bool IsAttached() const = 0;
203 float mDurationSeconds;
204 float mInitialDelaySeconds;
206 AlphaFunc mAlphaFunc;
208 Dali::Animation::EndAction mDisconnectAction;
213 * An animator for a specific property type PropertyType.
215 template < typename PropertyType, typename PropertyAccessorType >
216 class Animator : public AnimatorBase, public PropertyOwner::Observer
220 typedef boost::function< PropertyType (float, const PropertyType&) > AnimatorFunction;
223 * Construct a new property animator.
224 * @param[in] property The animatable property; only valid while the Animator is attached.
225 * @param[in] animatorFunction The function used to animate the property.
226 * @param[in] alphaFunction The alpha function to apply.
227 * @param[in] timePeriod The time period of this animation.
228 * @return A newly allocated animator.
230 static AnimatorBase* New( const PropertyBase& property,
231 AnimatorFunction animatorFunction,
232 AlphaFunction alphaFunction,
233 const TimePeriod& timePeriod )
235 typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
237 // The property was const in the actor-thread, but animators are used in the scene-graph thread.
238 AnimatorType* animator = new AnimatorType( const_cast<PropertyBase*>( &property ),
241 animator->SetAlphaFunc( alphaFunction );
242 animator->SetInitialDelay( timePeriod.delaySeconds );
243 animator->SetDuration( timePeriod.durationSeconds );
249 * Virtual destructor.
255 mPropertyOwner->RemoveObserver(*this);
261 * @param[in] propertyOwner The scene-object that owns the animatable property.
263 virtual void Attach( PropertyOwner* propertyOwner )
265 mPropertyOwner = propertyOwner;
269 mPropertyOwner->AddObserver(*this);
276 virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
278 // If the object dies, the animator has no effect
279 if ( mPropertyOwner )
281 float alpha = mAlphaFunc( progress );
283 const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
285 const PropertyType result = mAnimatorFunction( alpha, current );
289 mPropertyAccessor.Bake( bufferIndex, result );
293 mPropertyAccessor.Set( bufferIndex, result );
296 mCurrentProgress = progress;
299 return IsAttached(); // return false if orphaned
305 virtual bool IsAttached() const
307 return NULL != mPropertyOwner;
311 * Called when mPropertyOwner is disconnected from the scene graph.
313 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
315 // If we are active, then bake the value if required
316 if ( mActive && mDisconnectAction != Dali::Animation::Discard )
318 // Bake to target-value if BakeFinal, otherwise bake current value
319 Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
323 mPropertyOwner = NULL;
324 mPropertyAccessor.Reset();
328 * Called shortly before mPropertyOwner is destroyed, along with its property.
330 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
332 mPropertyOwner = NULL;
333 mPropertyAccessor.Reset();
339 * Private constructor; see also Animator::New().
341 Animator( PropertyBase* property,
342 AnimatorFunction animatorFunction )
343 : mPropertyOwner( NULL ),
344 mPropertyAccessor( property ),
345 mAnimatorFunction( animatorFunction ),
346 mCurrentProgress( 0.0f )
351 Animator( const Animator& );
354 Animator& operator=( const Animator& );
358 PropertyOwner* mPropertyOwner;
359 PropertyAccessorType mPropertyAccessor;
361 AnimatorFunction mAnimatorFunction;
362 float mCurrentProgress;
365 } // namespace SceneGraph
368 // Common Update functions
370 struct AnimateByFloat
372 AnimateByFloat(const float& relativeValue)
373 : mRelative(relativeValue)
377 float operator()(float alpha, const float& property)
379 return float(property + mRelative * alpha);
385 struct AnimateToFloat
387 AnimateToFloat(const float& targetValue)
388 : mTarget(targetValue)
392 float operator()(float alpha, const float& property)
394 return float(property + ((mTarget - property) * alpha));
400 struct AnimateByInteger
402 AnimateByInteger(const int& relativeValue)
403 : mRelative(relativeValue)
407 float operator()(float alpha, const int& property)
409 return int(property + mRelative * alpha + 0.5f );
415 struct AnimateToInteger
417 AnimateToInteger(const int& targetValue)
418 : mTarget(targetValue)
422 float operator()(float alpha, const int& property)
424 return int(property + ((mTarget - property) * alpha) + 0.5f);
430 struct AnimateByVector2
432 AnimateByVector2(const Vector2& relativeValue)
433 : mRelative(relativeValue)
437 Vector2 operator()(float alpha, const Vector2& property)
439 return Vector2(property + mRelative * alpha);
445 struct AnimateToVector2
447 AnimateToVector2(const Vector2& targetValue)
448 : mTarget(targetValue)
452 Vector2 operator()(float alpha, const Vector2& property)
454 return Vector2(property + ((mTarget - property) * alpha));
460 struct AnimateByVector3
462 AnimateByVector3(const Vector3& relativeValue)
463 : mRelative(relativeValue)
467 Vector3 operator()(float alpha, const Vector3& property)
469 return Vector3(property + mRelative * alpha);
475 struct AnimateToVector3
477 AnimateToVector3(const Vector3& targetValue)
478 : mTarget(targetValue)
482 Vector3 operator()(float alpha, const Vector3& property)
484 return Vector3(property + ((mTarget - property) * alpha));
490 struct AnimateByVector4
492 AnimateByVector4(const Vector4& relativeValue)
493 : mRelative(relativeValue)
497 Vector4 operator()(float alpha, const Vector4& property)
499 return Vector4(property + mRelative * alpha);
505 struct AnimateToVector4
507 AnimateToVector4(const Vector4& targetValue)
508 : mTarget(targetValue)
512 Vector4 operator()(float alpha, const Vector4& property)
514 return Vector4(property + ((mTarget - property) * alpha));
520 struct AnimateByOpacity
522 AnimateByOpacity(const float& relativeValue)
523 : mRelative(relativeValue)
527 Vector4 operator()(float alpha, const Vector4& property)
529 Vector4 result(property);
530 result.a += mRelative * alpha;
538 struct AnimateToOpacity
540 AnimateToOpacity(const float& targetValue)
541 : mTarget(targetValue)
545 Vector4 operator()(float alpha, const Vector4& property)
547 Vector4 result(property);
548 result.a = property.a + ((mTarget - property.a) * alpha);
556 struct AnimateByBoolean
558 AnimateByBoolean(bool relativeValue)
559 : mRelative(relativeValue)
563 bool operator()(float alpha, const bool& property)
565 // Alpha is not useful here, just keeping to the same template as other update functors
566 return bool(alpha >= 1.0f ? (property || mRelative) : property);
572 struct AnimateToBoolean
574 AnimateToBoolean(bool targetValue)
575 : mTarget(targetValue)
579 bool operator()(float alpha, const bool& property)
581 // Alpha is not useful here, just keeping to the same template as other update functors
582 return bool(alpha >= 1.0f ? mTarget : property);
588 struct RotateByAngleAxis
590 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
591 : mAngleRadians(angleRadians),
592 mAxis(axis.x, axis.y, axis.z, 0.0f)
596 Quaternion operator()(float alpha, const Quaternion& rotation)
600 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
610 struct RotateToQuaternion
612 RotateToQuaternion(const Quaternion& targetValue)
613 : mTarget(targetValue)
617 Quaternion operator()(float alpha, const Quaternion& rotation)
619 return Quaternion::Slerp(rotation, mTarget, alpha);
626 struct KeyFrameBooleanFunctor
628 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
629 : mKeyFrames(keyFrames)
633 bool operator()(float progress, const bool& property)
635 if(mKeyFrames->IsActive(progress))
637 return mKeyFrames->GetValue(progress);
642 KeyFrameBooleanPtr mKeyFrames;
645 struct KeyFrameNumberFunctor
647 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames)
648 : mKeyFrames(keyFrames)
652 float operator()(float progress, const float& property)
654 if(mKeyFrames->IsActive(progress))
656 return mKeyFrames->GetValue(progress);
661 KeyFrameNumberPtr mKeyFrames;
664 struct KeyFrameIntegerFunctor
666 KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames)
667 : mKeyFrames(keyFrames)
671 float operator()(float progress, const int& property)
673 if(mKeyFrames->IsActive(progress))
675 return mKeyFrames->GetValue(progress);
680 KeyFrameIntegerPtr mKeyFrames;
683 struct KeyFrameVector2Functor
685 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
686 : mKeyFrames(keyFrames)
690 Vector2 operator()(float progress, const Vector2& property)
692 if(mKeyFrames->IsActive(progress))
694 return mKeyFrames->GetValue(progress);
699 KeyFrameVector2Ptr mKeyFrames;
703 struct KeyFrameVector3Functor
705 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames)
706 : mKeyFrames(keyFrames)
710 Vector3 operator()(float progress, const Vector3& property)
712 if(mKeyFrames->IsActive(progress))
714 return mKeyFrames->GetValue(progress);
719 KeyFrameVector3Ptr mKeyFrames;
722 struct KeyFrameVector4Functor
724 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames)
725 : mKeyFrames(keyFrames)
729 Vector4 operator()(float progress, const Vector4& property)
731 if(mKeyFrames->IsActive(progress))
733 return mKeyFrames->GetValue(progress);
738 KeyFrameVector4Ptr mKeyFrames;
741 struct KeyFrameQuaternionFunctor
743 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
744 : mKeyFrames(keyFrames)
748 Quaternion operator()(float progress, const Quaternion& property)
750 if(mKeyFrames->IsActive(progress))
752 return mKeyFrames->GetValue(progress);
757 KeyFrameQuaternionPtr mKeyFrames;
763 } // namespace Internal
767 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__