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 Flora License, Version 1.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://floralicense.org/license/
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.
21 #include <boost/function.hpp>
24 #include <dali/internal/common/owner-container.h>
25 #include <dali/internal/event/animation/key-frames-impl.h>
26 #include <dali/internal/update/nodes/node.h>
27 #include <dali/internal/update/common/property-base.h>
28 #include <dali/internal/common/observer-pointer.h>
29 #include <dali/public-api/animation/alpha-functions.h>
30 #include <dali/public-api/animation/time-period.h>
31 #include <dali/public-api/common/dali-common.h>
32 #include <dali/public-api/math/quaternion.h>
33 #include <dali/public-api/math/radian.h>
46 typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
48 typedef AnimatorContainer::Iterator AnimatorIter;
49 typedef AnimatorContainer::ConstIterator AnimatorConstIter;
52 * An abstract base class for Animators, which can be added to scene graph animations.
53 * Each animator changes a single property of an object in the scene graph.
59 typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
65 : mDurationSeconds(1.0f),
66 mInitialDelaySeconds(0.0f),
67 mAlphaFunc(AlphaFunctions::Linear),
75 virtual ~AnimatorBase()
80 * Set the duration of the animator.
81 * @pre durationSeconds must be zero or greater; zero is useful when animating boolean values.
82 * @param [in] seconds Duration in seconds.
84 void SetDuration(float seconds)
86 DALI_ASSERT_DEBUG(seconds >= 0.0f);
88 mDurationSeconds = seconds;
92 * Retrieve the duration of the animator.
93 * @return The duration in seconds.
97 return mDurationSeconds;
101 * Set the delay before the animator should take effect.
102 * The default is zero i.e. no delay.
103 * @param [in] seconds The delay in seconds.
105 void SetInitialDelay(float seconds)
107 mInitialDelaySeconds = seconds;
111 * Retrieve the initial delay of the animator.
112 * @return The delay in seconds.
114 float GetInitialDelay()
116 return mInitialDelaySeconds;
120 * Set the alpha function for an animator.
121 * @param [in] alphaFunc The alpha function to apply to the animation progress.
123 void SetAlphaFunc(AlphaFunc alphaFunc)
125 mAlphaFunc = alphaFunc;
129 * Retrieve the alpha function of an animator.
130 * @return The function.
132 AlphaFunc GetAlphaFunc() const
138 * This must be called when the animator is attached to the scene-graph.
139 * @pre The animatable scene object must also be attached to the scene-graph.
140 * @param[in] propertyOwner The scene-object that owns the animatable property.
142 virtual void Attach( PropertyOwner* propertyOwner ) = 0;
145 * Update the scene object attached to the animator.
146 * @param[in] bufferIndex The buffer to animate.
147 * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
148 * @param[in] bake Bake.
149 * @return True if the update was applied, false if the animator has been orphaned.
151 virtual bool Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
154 * Query whether the animator is still attached to a scene object.
155 * The attachment will be automatically severed, when the object is destroyed.
156 * @return True if the animator is attached.
158 virtual bool IsAttached() const = 0;
162 float mDurationSeconds;
163 float mInitialDelaySeconds;
165 AlphaFunc mAlphaFunc;
171 * An animator for a specific property type PropertyType.
173 template < typename PropertyType, typename PropertyAccessorType >
174 class Animator : public AnimatorBase, public PropertyOwner::Observer
178 typedef boost::function< PropertyType (float, const PropertyType&) > AnimatorFunction;
181 * Construct a new property animator.
182 * @param[in] property The animatable property; only valid while the Animator is attached.
183 * @param[in] animatorFunction The function used to animate the property.
184 * @param[in] alphaFunction The alpha function to apply.
185 * @param[in] timePeriod The time period of this animation.
186 * @return A newly allocated animator.
188 static AnimatorBase* New( const PropertyBase& property,
189 AnimatorFunction animatorFunction,
190 AlphaFunction alphaFunction,
191 const TimePeriod& timePeriod )
193 typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
195 // The property was const in the actor-thread, but animators are used in the scene-graph thread.
196 AnimatorType* animator = new AnimatorType( const_cast<PropertyBase*>( &property ),
199 animator->SetAlphaFunc( alphaFunction );
200 animator->SetInitialDelay( timePeriod.delaySeconds );
201 animator->SetDuration( timePeriod.durationSeconds );
207 * Virtual destructor.
213 mPropertyOwner->RemoveObserver(*this);
219 * @param[in] propertyOwner The scene-object that owns the animatable property.
221 virtual void Attach( PropertyOwner* propertyOwner )
223 mPropertyOwner = propertyOwner;
227 mPropertyOwner->AddObserver(*this);
234 virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
236 // If the object dies, the animator has no effect
237 if ( mPropertyOwner )
239 float alpha = mAlphaFunc( progress );
241 const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
243 const PropertyType result = mAnimatorFunction( alpha, current );
247 mPropertyAccessor.Bake( bufferIndex, result );
251 mPropertyAccessor.Set( bufferIndex, result );
255 return IsAttached(); // return false if orphaned
261 virtual bool IsAttached() const
263 return NULL != mPropertyOwner;
267 * Called shortly before mPropertyOwner is destroyed, along with its property.
269 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
271 mPropertyOwner = NULL;
272 mPropertyAccessor.Reset();
278 * Private constructor; see also Animator::New().
280 Animator( PropertyBase* property,
281 AnimatorFunction animatorFunction )
282 : mPropertyOwner( NULL ),
283 mPropertyAccessor( property ),
284 mAnimatorFunction( animatorFunction )
289 Animator( const Animator& );
292 Animator& operator=( const Animator& );
296 PropertyOwner* mPropertyOwner;
297 PropertyAccessorType mPropertyAccessor;
299 AnimatorFunction mAnimatorFunction;
302 } // namespace SceneGraph
305 // Common Update functions
307 struct AnimateByFloat
309 AnimateByFloat(const float& relativeValue)
310 : mRelative(relativeValue)
314 float operator()(float alpha, const float& property)
316 return float(property + mRelative * alpha);
322 struct AnimateToFloat
324 AnimateToFloat(const float& targetValue)
325 : mTarget(targetValue)
329 float operator()(float alpha, const float& property)
331 return float(property + ((mTarget - property) * alpha));
337 struct AnimateByVector2
339 AnimateByVector2(const Vector2& relativeValue)
340 : mRelative(relativeValue)
344 Vector2 operator()(float alpha, const Vector2& property)
346 return Vector2(property + mRelative * alpha);
352 struct AnimateToVector2
354 AnimateToVector2(const Vector2& targetValue)
355 : mTarget(targetValue)
359 Vector2 operator()(float alpha, const Vector2& property)
361 return Vector2(property + ((mTarget - property) * alpha));
367 struct AnimateByVector3
369 AnimateByVector3(const Vector3& relativeValue)
370 : mRelative(relativeValue)
374 Vector3 operator()(float alpha, const Vector3& property)
376 return Vector3(property + mRelative * alpha);
382 struct AnimateToVector3
384 AnimateToVector3(const Vector3& targetValue)
385 : mTarget(targetValue)
389 Vector3 operator()(float alpha, const Vector3& property)
391 return Vector3(property + ((mTarget - property) * alpha));
397 struct AnimateByVector4
399 AnimateByVector4(const Vector4& relativeValue)
400 : mRelative(relativeValue)
404 Vector4 operator()(float alpha, const Vector4& property)
406 return Vector4(property + mRelative * alpha);
412 struct AnimateToVector4
414 AnimateToVector4(const Vector4& targetValue)
415 : mTarget(targetValue)
419 Vector4 operator()(float alpha, const Vector4& property)
421 return Vector4(property + ((mTarget - property) * alpha));
427 struct AnimateByOpacity
429 AnimateByOpacity(const float& relativeValue)
430 : mRelative(relativeValue)
434 Vector4 operator()(float alpha, const Vector4& property)
436 Vector4 result(property);
437 result.a += mRelative * alpha;
445 struct AnimateToOpacity
447 AnimateToOpacity(const float& targetValue)
448 : mTarget(targetValue)
452 Vector4 operator()(float alpha, const Vector4& property)
454 Vector4 result(property);
455 result.a = property.a + ((mTarget - property.a) * alpha);
463 struct AnimateByBoolean
465 AnimateByBoolean(bool relativeValue)
466 : mRelative(relativeValue)
470 bool operator()(float alpha, const bool& property)
472 // Alpha is not useful here, just keeping to the same template as other update functors
473 return bool(alpha >= 1.0f ? (property || mRelative) : property);
479 struct AnimateToBoolean
481 AnimateToBoolean(bool targetValue)
482 : mTarget(targetValue)
486 bool operator()(float alpha, const bool& property)
488 // Alpha is not useful here, just keeping to the same template as other update functors
489 return bool(alpha >= 1.0f ? mTarget : property);
495 struct RotateByAngleAxis
497 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
498 : mAngleRadians(angleRadians),
499 mAxis(axis.x, axis.y, axis.z, 0.0f)
503 Quaternion operator()(float alpha, const Quaternion& rotation)
507 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
517 struct RotateToQuaternion
519 RotateToQuaternion(const Quaternion& targetValue)
520 : mTarget(targetValue)
524 Quaternion operator()(float alpha, const Quaternion& rotation)
526 return Quaternion::Slerp(rotation, mTarget, alpha);
533 struct KeyFrameBooleanFunctor
535 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
536 : mKeyFrames(keyFrames)
540 bool operator()(float progress, const bool& property)
542 if(mKeyFrames->IsActive(progress))
544 return mKeyFrames->GetValue(progress);
549 KeyFrameBooleanPtr mKeyFrames;
552 struct KeyFrameNumberFunctor
554 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames)
555 : mKeyFrames(keyFrames)
559 float operator()(float progress, const float& property)
561 if(mKeyFrames->IsActive(progress))
563 return mKeyFrames->GetValue(progress);
568 KeyFrameNumberPtr mKeyFrames;
571 struct KeyFrameVector2Functor
573 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
574 : mKeyFrames(keyFrames)
578 Vector2 operator()(float progress, const Vector2& property)
580 if(mKeyFrames->IsActive(progress))
582 return mKeyFrames->GetValue(progress);
587 KeyFrameVector2Ptr mKeyFrames;
591 struct KeyFrameVector3Functor
593 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames)
594 : mKeyFrames(keyFrames)
598 Vector3 operator()(float progress, const Vector3& property)
600 if(mKeyFrames->IsActive(progress))
602 return mKeyFrames->GetValue(progress);
607 KeyFrameVector3Ptr mKeyFrames;
610 struct KeyFrameVector4Functor
612 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames)
613 : mKeyFrames(keyFrames)
617 Vector4 operator()(float progress, const Vector4& property)
619 if(mKeyFrames->IsActive(progress))
621 return mKeyFrames->GetValue(progress);
626 KeyFrameVector4Ptr mKeyFrames;
629 struct KeyFrameQuaternionFunctor
631 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
632 : mKeyFrames(keyFrames)
636 Quaternion operator()(float progress, const Quaternion& property)
638 if(mKeyFrames->IsActive(progress))
640 return mKeyFrames->GetValue(progress);
645 KeyFrameQuaternionPtr mKeyFrames;
651 } // namespace Internal
655 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__