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/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),
76 virtual ~AnimatorBase()
81 * Set the duration of the animator.
82 * @pre durationSeconds must be zero or greater; zero is useful when animating boolean values.
83 * @param [in] seconds Duration in seconds.
85 void SetDuration(float seconds)
87 DALI_ASSERT_DEBUG(seconds >= 0.0f);
89 mDurationSeconds = seconds;
93 * Retrieve the duration of the animator.
94 * @return The duration in seconds.
98 return mDurationSeconds;
102 * Set the delay before the animator should take effect.
103 * The default is zero i.e. no delay.
104 * @param [in] seconds The delay in seconds.
106 void SetInitialDelay(float seconds)
108 mInitialDelaySeconds = seconds;
112 * Retrieve the initial delay of the animator.
113 * @return The delay in seconds.
115 float GetInitialDelay()
117 return mInitialDelaySeconds;
121 * Set the alpha function for an animator.
122 * @param [in] alphaFunc The alpha function to apply to the animation progress.
124 void SetAlphaFunc(AlphaFunc alphaFunc)
126 mAlphaFunc = alphaFunc;
130 * Retrieve the alpha function of an animator.
131 * @return The function.
133 AlphaFunc GetAlphaFunc() const
139 * This must be called when the animator is attached to the scene-graph.
140 * @pre The animatable scene object must also be attached to the scene-graph.
141 * @param[in] propertyOwner The scene-object that owns the animatable property.
143 virtual void Attach( PropertyOwner* propertyOwner ) = 0;
146 * Update the scene object attached to the animator.
147 * @param[in] bufferIndex The buffer to animate.
148 * @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
149 * @param[in] bake Bake.
150 * @return True if the update was applied, false if the animator has been orphaned.
152 virtual bool Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
155 * Query whether the animator is still attached to a scene object.
156 * The attachment will be automatically severed, when the object is destroyed.
157 * @return True if the animator is attached.
159 virtual bool IsAttached() const = 0;
163 float mDurationSeconds;
164 float mInitialDelaySeconds;
166 AlphaFunc mAlphaFunc;
172 * An animator for a specific property type PropertyType.
174 template < typename PropertyType, typename PropertyAccessorType >
175 class Animator : public AnimatorBase, public PropertyOwner::Observer
179 typedef boost::function< PropertyType (float, const PropertyType&) > AnimatorFunction;
182 * Construct a new property animator.
183 * @param[in] property The animatable property; only valid while the Animator is attached.
184 * @param[in] animatorFunction The function used to animate the property.
185 * @param[in] alphaFunction The alpha function to apply.
186 * @param[in] timePeriod The time period of this animation.
187 * @return A newly allocated animator.
189 static AnimatorBase* New( const PropertyBase& property,
190 AnimatorFunction animatorFunction,
191 AlphaFunction alphaFunction,
192 const TimePeriod& timePeriod )
194 typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
196 // The property was const in the actor-thread, but animators are used in the scene-graph thread.
197 AnimatorType* animator = new AnimatorType( const_cast<PropertyBase*>( &property ),
200 animator->SetAlphaFunc( alphaFunction );
201 animator->SetInitialDelay( timePeriod.delaySeconds );
202 animator->SetDuration( timePeriod.durationSeconds );
208 * Virtual destructor.
214 mPropertyOwner->RemoveObserver(*this);
220 * @param[in] propertyOwner The scene-object that owns the animatable property.
222 virtual void Attach( PropertyOwner* propertyOwner )
224 mPropertyOwner = propertyOwner;
228 mPropertyOwner->AddObserver(*this);
235 virtual bool Update( BufferIndex bufferIndex, float progress, bool bake )
237 // If the object dies, the animator has no effect
238 if ( mPropertyOwner )
240 float alpha = mAlphaFunc( progress );
242 const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
244 const PropertyType result = mAnimatorFunction( alpha, current );
248 mPropertyAccessor.Bake( bufferIndex, result );
252 mPropertyAccessor.Set( bufferIndex, result );
256 return IsAttached(); // return false if orphaned
262 virtual bool IsAttached() const
264 return NULL != mPropertyOwner;
268 * Called when mPropertyOwner is disconnected from the scene graph.
270 virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
272 mPropertyOwner = NULL;
273 mPropertyAccessor.Reset();
277 * Called shortly before mPropertyOwner is destroyed, along with its property.
279 virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
281 mPropertyOwner = NULL;
282 mPropertyAccessor.Reset();
288 * Private constructor; see also Animator::New().
290 Animator( PropertyBase* property,
291 AnimatorFunction animatorFunction )
292 : mPropertyOwner( NULL ),
293 mPropertyAccessor( property ),
294 mAnimatorFunction( animatorFunction )
299 Animator( const Animator& );
302 Animator& operator=( const Animator& );
306 PropertyOwner* mPropertyOwner;
307 PropertyAccessorType mPropertyAccessor;
309 AnimatorFunction mAnimatorFunction;
312 } // namespace SceneGraph
315 // Common Update functions
317 struct AnimateByFloat
319 AnimateByFloat(const float& relativeValue)
320 : mRelative(relativeValue)
324 float operator()(float alpha, const float& property)
326 return float(property + mRelative * alpha);
332 struct AnimateToFloat
334 AnimateToFloat(const float& targetValue)
335 : mTarget(targetValue)
339 float operator()(float alpha, const float& property)
341 return float(property + ((mTarget - property) * alpha));
347 struct AnimateByInteger
349 AnimateByInteger(const int& relativeValue)
350 : mRelative(relativeValue)
354 float operator()(float alpha, const int& property)
356 return int(property + mRelative * alpha + 0.5f );
362 struct AnimateToInteger
364 AnimateToInteger(const int& targetValue)
365 : mTarget(targetValue)
369 float operator()(float alpha, const int& property)
371 return int(property + ((mTarget - property) * alpha) + 0.5f);
377 struct AnimateByVector2
379 AnimateByVector2(const Vector2& relativeValue)
380 : mRelative(relativeValue)
384 Vector2 operator()(float alpha, const Vector2& property)
386 return Vector2(property + mRelative * alpha);
392 struct AnimateToVector2
394 AnimateToVector2(const Vector2& targetValue)
395 : mTarget(targetValue)
399 Vector2 operator()(float alpha, const Vector2& property)
401 return Vector2(property + ((mTarget - property) * alpha));
407 struct AnimateByVector3
409 AnimateByVector3(const Vector3& relativeValue)
410 : mRelative(relativeValue)
414 Vector3 operator()(float alpha, const Vector3& property)
416 return Vector3(property + mRelative * alpha);
422 struct AnimateToVector3
424 AnimateToVector3(const Vector3& targetValue)
425 : mTarget(targetValue)
429 Vector3 operator()(float alpha, const Vector3& property)
431 return Vector3(property + ((mTarget - property) * alpha));
437 struct AnimateByVector4
439 AnimateByVector4(const Vector4& relativeValue)
440 : mRelative(relativeValue)
444 Vector4 operator()(float alpha, const Vector4& property)
446 return Vector4(property + mRelative * alpha);
452 struct AnimateToVector4
454 AnimateToVector4(const Vector4& targetValue)
455 : mTarget(targetValue)
459 Vector4 operator()(float alpha, const Vector4& property)
461 return Vector4(property + ((mTarget - property) * alpha));
467 struct AnimateByOpacity
469 AnimateByOpacity(const float& relativeValue)
470 : mRelative(relativeValue)
474 Vector4 operator()(float alpha, const Vector4& property)
476 Vector4 result(property);
477 result.a += mRelative * alpha;
485 struct AnimateToOpacity
487 AnimateToOpacity(const float& targetValue)
488 : mTarget(targetValue)
492 Vector4 operator()(float alpha, const Vector4& property)
494 Vector4 result(property);
495 result.a = property.a + ((mTarget - property.a) * alpha);
503 struct AnimateByBoolean
505 AnimateByBoolean(bool relativeValue)
506 : mRelative(relativeValue)
510 bool operator()(float alpha, const bool& property)
512 // Alpha is not useful here, just keeping to the same template as other update functors
513 return bool(alpha >= 1.0f ? (property || mRelative) : property);
519 struct AnimateToBoolean
521 AnimateToBoolean(bool targetValue)
522 : mTarget(targetValue)
526 bool operator()(float alpha, const bool& property)
528 // Alpha is not useful here, just keeping to the same template as other update functors
529 return bool(alpha >= 1.0f ? mTarget : property);
535 struct RotateByAngleAxis
537 RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
538 : mAngleRadians(angleRadians),
539 mAxis(axis.x, axis.y, axis.z, 0.0f)
543 Quaternion operator()(float alpha, const Quaternion& rotation)
547 return rotation * Quaternion(mAngleRadians * alpha, mAxis);
557 struct RotateToQuaternion
559 RotateToQuaternion(const Quaternion& targetValue)
560 : mTarget(targetValue)
564 Quaternion operator()(float alpha, const Quaternion& rotation)
566 return Quaternion::Slerp(rotation, mTarget, alpha);
573 struct KeyFrameBooleanFunctor
575 KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
576 : mKeyFrames(keyFrames)
580 bool operator()(float progress, const bool& property)
582 if(mKeyFrames->IsActive(progress))
584 return mKeyFrames->GetValue(progress);
589 KeyFrameBooleanPtr mKeyFrames;
592 struct KeyFrameNumberFunctor
594 KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames)
595 : mKeyFrames(keyFrames)
599 float operator()(float progress, const float& property)
601 if(mKeyFrames->IsActive(progress))
603 return mKeyFrames->GetValue(progress);
608 KeyFrameNumberPtr mKeyFrames;
611 struct KeyFrameIntegerFunctor
613 KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames)
614 : mKeyFrames(keyFrames)
618 float operator()(float progress, const int& property)
620 if(mKeyFrames->IsActive(progress))
622 return mKeyFrames->GetValue(progress);
627 KeyFrameIntegerPtr mKeyFrames;
630 struct KeyFrameVector2Functor
632 KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames)
633 : mKeyFrames(keyFrames)
637 Vector2 operator()(float progress, const Vector2& property)
639 if(mKeyFrames->IsActive(progress))
641 return mKeyFrames->GetValue(progress);
646 KeyFrameVector2Ptr mKeyFrames;
650 struct KeyFrameVector3Functor
652 KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames)
653 : mKeyFrames(keyFrames)
657 Vector3 operator()(float progress, const Vector3& property)
659 if(mKeyFrames->IsActive(progress))
661 return mKeyFrames->GetValue(progress);
666 KeyFrameVector3Ptr mKeyFrames;
669 struct KeyFrameVector4Functor
671 KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames)
672 : mKeyFrames(keyFrames)
676 Vector4 operator()(float progress, const Vector4& property)
678 if(mKeyFrames->IsActive(progress))
680 return mKeyFrames->GetValue(progress);
685 KeyFrameVector4Ptr mKeyFrames;
688 struct KeyFrameQuaternionFunctor
690 KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
691 : mKeyFrames(keyFrames)
695 Quaternion operator()(float progress, const Quaternion& property)
697 if(mKeyFrames->IsActive(progress))
699 return mKeyFrames->GetValue(progress);
704 KeyFrameQuaternionPtr mKeyFrames;
710 } // namespace Internal
714 #endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__