// EXTERNAL INCLUDES
#include <cmath>
+#include <functional>
// INTERNAL INCLUDES
#include <dali/public-api/animation/alpha-function.h>
using Interpolation = Dali::Animation::Interpolation;
-/**
- * AnimatorFunction base class.
- * Needs to be declared first so AnimatorBase knows about it's destructor
- * All update functions must inherit from AnimatorFunctionBase and overload the appropiate "()" operator
- */
-struct AnimatorFunctionBase
-{
- /**
- * Constructor
- */
- AnimatorFunctionBase() {}
-
- /*
- * Virtual destructor (Intended as base class)
- */
- virtual ~AnimatorFunctionBase() {}
-
- ///Stub "()" operators.
- virtual bool operator()(float progress, const bool& property)
- {
- return property;
- }
-
- virtual float operator()(float progress, const int32_t& property)
- {
- return static_cast<float>( property );
- }
-
- virtual float operator()(float progress, const float& property)
- {
- return property;
- }
-
- virtual Vector2 operator()(float progress, const Vector2& property)
- {
- return property;
- }
-
- virtual Vector3 operator()(float progress, const Vector3& property)
- {
- return property;
- }
-
- virtual Vector4 operator()(float progress, const Vector4& property)
- {
- return property;
- }
-
- virtual Quaternion operator()(float progress, const Quaternion& property)
- {
- return property;
- }
-};
namespace SceneGraph
{
* Constructor.
*/
AnimatorBase( PropertyOwner* propertyOwner,
- AnimatorFunctionBase* animatorFunction,
AlphaFunction alphaFunction,
const TimePeriod& timePeriod )
: mLifecycleObserver( nullptr ),
mPropertyOwner( propertyOwner ),
- mAnimatorFunction( animatorFunction ),
mDurationSeconds( timePeriod.durationSeconds ),
mIntervalDelaySeconds( timePeriod.delaySeconds ),
mSpeedFactor( 1.0f ),
mCurrentProgress( 0.f ),
- mLoopCount( 1 ),
mAlphaFunction( alphaFunction ),
mDisconnectAction( Dali::Animation::BAKE_FINAL ),
mAnimationPlaying( false ),
/**
* Virtual destructor.
*/
- virtual ~AnimatorBase()
+ ~AnimatorBase() override
{
- delete mAnimatorFunction;
if (mPropertyOwner && mConnectedToSceneGraph)
{
mPropertyOwner->RemoveObserver(*this);
/**
* @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
*/
- void PropertyOwnerConnected( PropertyOwner& owner ) override final
+ void PropertyOwnerConnected( PropertyOwner& owner ) final
{
mEnabled = true;
}
/**
* @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
*/
- void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner ) override final
+ void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner ) final
{
// If we are active, then bake the value if required
if ( mAnimationPlaying && mDisconnectAction != Dali::Animation::DISCARD )
/**
* @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
*/
- void PropertyOwnerDestroyed( PropertyOwner& owner ) override final
+ void PropertyOwnerDestroyed( PropertyOwner& owner ) final
{
mPropertyOwner = nullptr;
}
{
mConnectedToSceneGraph = true;
mPropertyOwner->AddObserver(*this);
+
+ // Enable if the target object is valid and connected to the scene graph.
+ mEnabled = mPropertyOwner->IsAnimationPossible();
}
/**
LifecycleObserver* mLifecycleObserver;
PropertyOwner* mPropertyOwner;
- AnimatorFunctionBase* mAnimatorFunction;
+
float mDurationSeconds;
float mIntervalDelaySeconds;
float mSpeedFactor;
float mCurrentProgress;
- int32_t mLoopCount;
-
AlphaFunction mAlphaFunction;
+ int32_t mLoopCount{1};
Dali::Animation::EndAction mDisconnectAction; ///< EndAction to apply when target object gets disconnected from the stage.
bool mAnimationPlaying:1; ///< whether disconnect has been applied while it's running.
bool mEnabled:1; ///< Animator is "enabled" while its target object is valid and on the stage.
/**
* An animator for a specific property type PropertyType.
*/
-template < typename PropertyType, typename PropertyAccessorType >
-class Animator : public AnimatorBase
+template<typename PropertyType, typename PropertyAccessorType>
+class Animator final : public AnimatorBase
{
+ using AnimatorFunction = std::function<PropertyType(float, const PropertyType&)>;
+
+ AnimatorFunction mAnimatorFunction;
+
public:
/**
* @param[in] timePeriod The time period of this animation.
* @return A newly allocated animator.
*/
- static AnimatorBase* New( const PropertyOwner& propertyOwner,
- const PropertyBase& property,
- AnimatorFunctionBase* animatorFunction,
- AlphaFunction alphaFunction,
- const TimePeriod& timePeriod )
+ static AnimatorBase* New(const PropertyOwner& propertyOwner,
+ const PropertyBase& property,
+ AnimatorFunction animatorFunction,
+ AlphaFunction alphaFunction,
+ const TimePeriod& timePeriod)
{
// The property was const in the actor-thread, but animators are used in the scene-graph thread.
- return new Animator( const_cast<PropertyOwner*>( &propertyOwner ),
- const_cast<PropertyBase*>( &property ),
- animatorFunction,
- alphaFunction,
- timePeriod );
- }
-
- /**
- * Virtual destructor.
- */
- virtual ~Animator()
- {
+ return new Animator(const_cast<PropertyOwner*>(&propertyOwner),
+ const_cast<PropertyBase*>(&property),
+ std::move(animatorFunction),
+ alphaFunction,
+ timePeriod);
}
/**
* @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
*/
- virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
+ void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) final
{
const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
// need to cast the return value in case property is integer
- const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
+ const PropertyType result = static_cast<PropertyType>(mAnimatorFunction(alpha, current));
if ( bake )
{
/**
* Private constructor; see also Animator::New().
*/
- Animator( PropertyOwner* propertyOwner,
- PropertyBase* property,
- AnimatorFunctionBase* animatorFunction,
- AlphaFunction alphaFunction,
- const TimePeriod& timePeriod )
- : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
- mPropertyAccessor( property )
+ Animator(PropertyOwner* propertyOwner,
+ PropertyBase* property,
+ AnimatorFunction animatorFunction,
+ AlphaFunction alphaFunction,
+ const TimePeriod& timePeriod)
+ : AnimatorBase(propertyOwner, alphaFunction, timePeriod),
+ mAnimatorFunction(std::move(animatorFunction)),
+ mPropertyAccessor(property)
{
// WARNING - this object is created in the event-thread
// The scene-graph mPropertyOwner object cannot be observed here
/**
* An animator for a specific property type PropertyType.
*/
-template <typename PropertyType, typename PropertyAccessorType>
-class AnimatorTransformProperty : public AnimatorBase
+template<typename PropertyType, typename PropertyAccessorType>
+class AnimatorTransformProperty final : public AnimatorBase
{
+ using AnimatorFunction = std::function<PropertyType(float, const PropertyType&)>;
+
+ AnimatorFunction mAnimatorFunction;
+
public:
/**
* @param[in] timePeriod The time period of this animation.
* @return A newly allocated animator.
*/
- static AnimatorBase* New( const PropertyOwner& propertyOwner,
- const PropertyBase& property,
- AnimatorFunctionBase* animatorFunction,
- AlphaFunction alphaFunction,
- const TimePeriod& timePeriod )
+ static AnimatorBase* New(const PropertyOwner& propertyOwner,
+ const PropertyBase& property,
+ AnimatorFunction animatorFunction,
+ AlphaFunction alphaFunction,
+ const TimePeriod& timePeriod)
{
// The property was const in the actor-thread, but animators are used in the scene-graph thread.
- return new AnimatorTransformProperty( const_cast<PropertyOwner*>( &propertyOwner ),
- const_cast<PropertyBase*>( &property ),
- animatorFunction,
- alphaFunction,
- timePeriod );
- }
-
- /**
- * Virtual destructor.
- */
- virtual ~AnimatorTransformProperty()
- {
+ return new AnimatorTransformProperty(const_cast<PropertyOwner*>(&propertyOwner),
+ const_cast<PropertyBase*>(&property),
+ std::move(animatorFunction),
+ alphaFunction,
+ timePeriod);
}
/**
* @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
*/
- virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
+ void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) final
{
const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
// need to cast the return value in case property is integer
- const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
+ const PropertyType result = static_cast<PropertyType>(mAnimatorFunction(alpha, current));
if ( bake )
{
/**
* Private constructor; see also Animator::New().
*/
- AnimatorTransformProperty( PropertyOwner* propertyOwner,
- PropertyBase* property,
- AnimatorFunctionBase* animatorFunction,
- AlphaFunction alphaFunction,
- const TimePeriod& timePeriod )
- : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
- mPropertyAccessor( property )
+ AnimatorTransformProperty(PropertyOwner* propertyOwner,
+ PropertyBase* property,
+ AnimatorFunction animatorFunction,
+ AlphaFunction alphaFunction,
+ const TimePeriod& timePeriod)
+ : AnimatorBase(propertyOwner, alphaFunction, timePeriod),
+ mAnimatorFunction(std::move(animatorFunction)),
+ mPropertyAccessor(property)
{
// WARNING - this object is created in the event-thread
// The scene-graph mPropertyOwner object cannot be observed here
// Update functions
-struct AnimateByInteger : public AnimatorFunctionBase
+struct AnimateByInteger
{
AnimateByInteger(const int& relativeValue)
: mRelative(relativeValue)
{
}
- using AnimatorFunctionBase::operator();
float operator()(float alpha, const int32_t& property)
{
// integers need to be correctly rounded
int32_t mRelative;
};
-struct AnimateToInteger : public AnimatorFunctionBase
+struct AnimateToInteger
{
AnimateToInteger(const int& targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
float operator()(float alpha, const int32_t& property)
{
// integers need to be correctly rounded
int32_t mTarget;
};
-struct AnimateByFloat : public AnimatorFunctionBase
+struct AnimateByFloat
{
AnimateByFloat(const float& relativeValue)
: mRelative(relativeValue)
{
}
- using AnimatorFunctionBase::operator();
float operator()(float alpha, const float& property)
{
return float(property + mRelative * alpha);
float mRelative;
};
-struct AnimateToFloat : public AnimatorFunctionBase
+struct AnimateToFloat
{
AnimateToFloat(const float& targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
float operator()(float alpha, const float& property)
{
return float(property + ((mTarget - property) * alpha));
float mTarget;
};
-struct AnimateByVector2 : public AnimatorFunctionBase
+struct AnimateByVector2
{
AnimateByVector2(const Vector2& relativeValue)
: mRelative(relativeValue)
{
}
- using AnimatorFunctionBase::operator();
Vector2 operator()(float alpha, const Vector2& property)
{
return Vector2(property + mRelative * alpha);
Vector2 mRelative;
};
-struct AnimateToVector2 : public AnimatorFunctionBase
+struct AnimateToVector2
{
AnimateToVector2(const Vector2& targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
Vector2 operator()(float alpha, const Vector2& property)
{
return Vector2(property + ((mTarget - property) * alpha));
Vector2 mTarget;
};
-struct AnimateByVector3 : public AnimatorFunctionBase
+struct AnimateByVector3
{
AnimateByVector3(const Vector3& relativeValue)
: mRelative(relativeValue)
{
}
- using AnimatorFunctionBase::operator();
Vector3 operator()(float alpha, const Vector3& property)
{
return Vector3(property + mRelative * alpha);
Vector3 mRelative;
};
-struct AnimateToVector3 : public AnimatorFunctionBase
+struct AnimateToVector3
{
AnimateToVector3(const Vector3& targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
Vector3 operator()(float alpha, const Vector3& property)
{
return Vector3(property + ((mTarget - property) * alpha));
Vector3 mTarget;
};
-struct AnimateByVector4 : public AnimatorFunctionBase
+struct AnimateByVector4
{
AnimateByVector4(const Vector4& relativeValue)
: mRelative(relativeValue)
{
}
- using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
return Vector4(property + mRelative * alpha);
Vector4 mRelative;
};
-struct AnimateToVector4 : public AnimatorFunctionBase
+struct AnimateToVector4
{
AnimateToVector4(const Vector4& targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
return Vector4(property + ((mTarget - property) * alpha));
Vector4 mTarget;
};
-struct AnimateByOpacity : public AnimatorFunctionBase
+struct AnimateByOpacity
{
AnimateByOpacity(const float& relativeValue)
: mRelative(relativeValue)
{
}
- using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
Vector4 result(property);
float mRelative;
};
-struct AnimateToOpacity : public AnimatorFunctionBase
+struct AnimateToOpacity
{
AnimateToOpacity(const float& targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
Vector4 result(property);
float mTarget;
};
-struct AnimateByBoolean : public AnimatorFunctionBase
+struct AnimateByBoolean
{
AnimateByBoolean(bool relativeValue)
: mRelative(relativeValue)
{
}
- using AnimatorFunctionBase::operator();
bool operator()(float alpha, const bool& property)
{
// Alpha is not useful here, just keeping to the same template as other update functors
bool mRelative;
};
-struct AnimateToBoolean : public AnimatorFunctionBase
+struct AnimateToBoolean
{
AnimateToBoolean(bool targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
bool operator()(float alpha, const bool& property)
{
// Alpha is not useful here, just keeping to the same template as other update functors
bool mTarget;
};
-struct RotateByAngleAxis : public AnimatorFunctionBase
+struct RotateByAngleAxis
{
RotateByAngleAxis(const Radian& angleRadians, const Vector3& axis)
: mAngleRadians( angleRadians ),
{
}
- using AnimatorFunctionBase::operator();
Quaternion operator()(float alpha, const Quaternion& rotation)
{
if (alpha > 0.0f)
Vector3 mAxis;
};
-struct RotateToQuaternion : public AnimatorFunctionBase
+struct RotateToQuaternion
{
RotateToQuaternion(const Quaternion& targetValue)
: mTarget(targetValue)
{
}
- using AnimatorFunctionBase::operator();
Quaternion operator()(float alpha, const Quaternion& rotation)
{
return Quaternion::Slerp(rotation, mTarget, alpha);
Quaternion mTarget;
};
-
-struct KeyFrameBooleanFunctor : public AnimatorFunctionBase
+struct KeyFrameBooleanFunctor
{
- KeyFrameBooleanFunctor(KeyFrameBooleanPtr keyFrames)
- : mKeyFrames(keyFrames)
+ KeyFrameBooleanFunctor(KeyFrameBoolean keyFrames)
+ : mKeyFrames(std::move(keyFrames))
{
}
- using AnimatorFunctionBase::operator();
bool operator()(float progress, const bool& property)
{
- if(mKeyFrames->IsActive(progress))
+ if(mKeyFrames.IsActive(progress))
{
- return mKeyFrames->GetValue(progress, Dali::Animation::LINEAR);
+ return mKeyFrames.GetValue(progress, Dali::Animation::LINEAR);
}
return property;
}
- KeyFrameBooleanPtr mKeyFrames;
+ KeyFrameBoolean mKeyFrames;
};
-struct KeyFrameIntegerFunctor : public AnimatorFunctionBase
+struct KeyFrameIntegerFunctor
{
- KeyFrameIntegerFunctor(KeyFrameIntegerPtr keyFrames, Interpolation interpolation)
- : mKeyFrames(keyFrames),mInterpolation(interpolation)
+ KeyFrameIntegerFunctor(KeyFrameInteger keyFrames, Interpolation interpolation)
+ : mKeyFrames(std::move(keyFrames)),
+ mInterpolation(interpolation)
{
}
- using AnimatorFunctionBase::operator();
float operator()(float progress, const int32_t& property)
{
- if(mKeyFrames->IsActive(progress))
+ if(mKeyFrames.IsActive(progress))
{
- return static_cast<float>( mKeyFrames->GetValue(progress, mInterpolation) );
+ return static_cast<float>(mKeyFrames.GetValue(progress, mInterpolation));
}
return static_cast<float>( property );
}
- KeyFrameIntegerPtr mKeyFrames;
+ KeyFrameInteger mKeyFrames;
Interpolation mInterpolation;
};
-struct KeyFrameNumberFunctor : public AnimatorFunctionBase
+struct KeyFrameNumberFunctor
{
- KeyFrameNumberFunctor(KeyFrameNumberPtr keyFrames, Interpolation interpolation)
- : mKeyFrames(keyFrames),mInterpolation(interpolation)
+ KeyFrameNumberFunctor(KeyFrameNumber keyFrames, Interpolation interpolation)
+ : mKeyFrames(std::move(keyFrames)),
+ mInterpolation(interpolation)
{
}
- using AnimatorFunctionBase::operator();
float operator()(float progress, const float& property)
{
- if(mKeyFrames->IsActive(progress))
+ if(mKeyFrames.IsActive(progress))
{
- return mKeyFrames->GetValue(progress, mInterpolation);
+ return mKeyFrames.GetValue(progress, mInterpolation);
}
return property;
}
- KeyFrameNumberPtr mKeyFrames;
+ KeyFrameNumber mKeyFrames;
Interpolation mInterpolation;
};
-struct KeyFrameVector2Functor : public AnimatorFunctionBase
+struct KeyFrameVector2Functor
{
- KeyFrameVector2Functor(KeyFrameVector2Ptr keyFrames, Interpolation interpolation)
- : mKeyFrames(keyFrames),mInterpolation(interpolation)
+ KeyFrameVector2Functor(KeyFrameVector2 keyFrames, Interpolation interpolation)
+ : mKeyFrames(std::move(keyFrames)),
+ mInterpolation(interpolation)
{
}
- using AnimatorFunctionBase::operator();
Vector2 operator()(float progress, const Vector2& property)
{
- if(mKeyFrames->IsActive(progress))
+ if(mKeyFrames.IsActive(progress))
{
- return mKeyFrames->GetValue(progress, mInterpolation);
+ return mKeyFrames.GetValue(progress, mInterpolation);
}
return property;
}
- KeyFrameVector2Ptr mKeyFrames;
+ KeyFrameVector2 mKeyFrames;
Interpolation mInterpolation;
};
-
-struct KeyFrameVector3Functor : public AnimatorFunctionBase
+struct KeyFrameVector3Functor
{
- KeyFrameVector3Functor(KeyFrameVector3Ptr keyFrames, Interpolation interpolation)
- : mKeyFrames(keyFrames),mInterpolation(interpolation)
+ KeyFrameVector3Functor(KeyFrameVector3 keyFrames, Interpolation interpolation)
+ : mKeyFrames(std::move(keyFrames)),
+ mInterpolation(interpolation)
{
}
- using AnimatorFunctionBase::operator();
Vector3 operator()(float progress, const Vector3& property)
{
- if(mKeyFrames->IsActive(progress))
+ if(mKeyFrames.IsActive(progress))
{
- return mKeyFrames->GetValue(progress, mInterpolation);
+ return mKeyFrames.GetValue(progress, mInterpolation);
}
return property;
}
- KeyFrameVector3Ptr mKeyFrames;
+ KeyFrameVector3 mKeyFrames;
Interpolation mInterpolation;
};
-struct KeyFrameVector4Functor : public AnimatorFunctionBase
+struct KeyFrameVector4Functor
{
- KeyFrameVector4Functor(KeyFrameVector4Ptr keyFrames, Interpolation interpolation)
- : mKeyFrames(keyFrames),mInterpolation(interpolation)
+ KeyFrameVector4Functor(KeyFrameVector4 keyFrames, Interpolation interpolation)
+ : mKeyFrames(std::move(keyFrames)),
+ mInterpolation(interpolation)
{
}
- using AnimatorFunctionBase::operator();
Vector4 operator()(float progress, const Vector4& property)
{
- if(mKeyFrames->IsActive(progress))
+ if(mKeyFrames.IsActive(progress))
{
- return mKeyFrames->GetValue(progress, mInterpolation);
+ return mKeyFrames.GetValue(progress, mInterpolation);
}
return property;
}
- KeyFrameVector4Ptr mKeyFrames;
+ KeyFrameVector4 mKeyFrames;
Interpolation mInterpolation;
};
-struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase
+struct KeyFrameQuaternionFunctor
{
- KeyFrameQuaternionFunctor(KeyFrameQuaternionPtr keyFrames)
- : mKeyFrames(keyFrames)
+ KeyFrameQuaternionFunctor(KeyFrameQuaternion keyFrames)
+ : mKeyFrames(std::move(keyFrames))
{
}
- using AnimatorFunctionBase::operator();
Quaternion operator()(float progress, const Quaternion& property)
{
- if(mKeyFrames->IsActive(progress))
+ if(mKeyFrames.IsActive(progress))
{
- return mKeyFrames->GetValue(progress, Dali::Animation::LINEAR);
+ return mKeyFrames.GetValue(progress, Dali::Animation::LINEAR);
}
return property;
}
- KeyFrameQuaternionPtr mKeyFrames;
+ KeyFrameQuaternion mKeyFrames;
};
-struct PathPositionFunctor : public AnimatorFunctionBase
+struct PathPositionFunctor
{
PathPositionFunctor( PathPtr path )
: mPath(path)
{
}
- using AnimatorFunctionBase::operator();
Vector3 operator()(float progress, const Vector3& property)
{
Vector3 position(property);
PathPtr mPath;
};
-struct PathRotationFunctor : public AnimatorFunctionBase
+struct PathRotationFunctor
{
PathRotationFunctor( PathPtr path, const Vector3& forward )
: mPath(path),
mForward.Normalize();
}
- using AnimatorFunctionBase::operator();
Quaternion operator()(float progress, const Quaternion& property)
{
Vector3 tangent;