-#ifndef __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
-#define __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
+#ifndef DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H
+#define DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H
/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
*/
+// EXTERNAL INCLUDES
+#include <cmath>
+
// INTERNAL INCLUDES
#include <dali/public-api/animation/alpha-function.h>
#include <dali/public-api/animation/animation.h>
namespace Internal
{
-typedef Dali::Animation::Interpolation Interpolation;
-
-struct AnimatorFunctionBase;
+using Interpolation = Dali::Animation::Interpolation;
-namespace SceneGraph
+/**
+ * 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;
+ }
-class AnimatorBase;
+ virtual Vector3 operator()(float progress, const Vector3& property)
+ {
+ return property;
+ }
+
+ virtual Vector4 operator()(float progress, const Vector4& property)
+ {
+ return property;
+ }
-typedef OwnerContainer< AnimatorBase* > AnimatorContainer;
+ virtual Quaternion operator()(float progress, const Quaternion& property)
+ {
+ return property;
+ }
+};
-typedef AnimatorContainer::Iterator AnimatorIter;
-typedef AnimatorContainer::ConstIterator AnimatorConstIter;
+namespace SceneGraph
+{
/**
* An abstract base class for Animators, which can be added to scene graph animations.
* Each animator changes a single property of an object in the scene graph.
*/
-class AnimatorBase
+class AnimatorBase : public PropertyOwner::Observer
{
public:
- typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function
+ using AlphaFunc = float (*)(float progress); ///< Definition of an alpha function
/**
* Observer to determine when the animator is no longer present
/**
* Constructor.
*/
- AnimatorBase()
- : mLifecycleObserver(nullptr),
- mDurationSeconds(1.0f),
- mIntervalDelaySeconds(0.0f),
- mSpeedFactor(1.0f),
- mLoopCount(1),
- mAlphaFunction(AlphaFunction::DEFAULT),
- mDisconnectAction(Dali::Animation::BakeFinal),
- mAnimationPlaying(false),
- mEnabled(true),
- mConnectedToSceneGraph(false),
+ 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::BakeFinal ),
+ mAnimationPlaying( false ),
+ mEnabled( true ),
+ mConnectedToSceneGraph( false ),
mAutoReverseEnabled( false )
{
}
*/
virtual ~AnimatorBase()
{
+ delete mAnimatorFunction;
+ if (mPropertyOwner && mConnectedToSceneGraph)
+ {
+ mPropertyOwner->RemoveObserver(*this);
+ }
if( mLifecycleObserver != nullptr )
{
mLifecycleObserver->ObjectDestroyed();
mLifecycleObserver = nullptr;
}
+private: // From PropertyOwner::Observer
+
+ /**
+ * @copydoc PropertyOwner::Observer::PropertyOwnerConnected( PropertyOwner& owner )
+ */
+ void PropertyOwnerConnected( PropertyOwner& owner ) override final
+ {
+ mEnabled = true;
+ }
+
+ /**
+ * @copydoc PropertyOwner::Observer::PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
+ */
+ void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner ) override final
+ {
+ // If we are active, then bake the value if required
+ if ( mAnimationPlaying && mDisconnectAction != Dali::Animation::Discard )
+ {
+ // Bake to target-value if BakeFinal, otherwise bake current value
+ Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
+ }
+
+ mEnabled = false;
+ }
+
+ /**
+ * @copydoc PropertyOwner::Observer::PropertyOwnerDestroyed( PropertyOwner& owner )
+ */
+ void PropertyOwnerDestroyed( PropertyOwner& owner ) override final
+ {
+ mPropertyOwner = nullptr;
+ }
+
+public:
/**
* Called when Animator is added to the scene-graph in update-thread.
*/
- virtual void ConnectToSceneGraph() = 0;
+ void ConnectToSceneGraph()
+ {
+ mConnectedToSceneGraph = true;
+ mPropertyOwner->AddObserver(*this);
+ }
/**
* Set the duration of the animator.
}
/**
- * Retrive wheter the animator's target object is valid and on the stage.
+ * Whether the animator's target object is valid and on the stage.
* @return The enabled state.
*/
bool IsEnabled() const
* @return True if animator is orphan, false otherwise *
* @note The SceneGraph::Animation will delete any orphan animator in its Update method.
*/
- virtual bool Orphan() = 0;
+ bool Orphan()
+ {
+ return (mPropertyOwner == nullptr);
+ }
/**
* Update the scene object attached to the animator.
* @param[in] progress A value from 0 to 1, where 0 is the start of the animation, and 1 is the end point.
* @param[in] bake Bake.
*/
- virtual void Update(BufferIndex bufferIndex, float progress, bool bake) = 0;
+ void Update( BufferIndex bufferIndex, float progress, bool bake )
+ {
+ if( mLoopCount >= 0 )
+ {
+ // Update the progress value
+ progress = SetProgress( progress );
+ }
+
+ if( mPropertyOwner )
+ {
+ mPropertyOwner->SetPropertyDirty( true );
+ }
+
+ float alpha = ApplyAlphaFunction( progress );
+
+ // PropertyType specific part
+ DoUpdate( bufferIndex, bake, alpha );
+
+ mCurrentProgress = progress;
+ }
+
+ /**
+ * Type specific part of the animator
+ * @param bufferIndex index to use
+ * @param bake whether to bake or not
+ * @param alpha value from alpha based on progress
+ */
+ virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) = 0;
protected:
}
LifecycleObserver* mLifecycleObserver;
+ PropertyOwner* mPropertyOwner;
+ AnimatorFunctionBase* mAnimatorFunction;
float mDurationSeconds;
float mIntervalDelaySeconds;
float mSpeedFactor;
+ float mCurrentProgress;
int32_t mLoopCount;
* An animator for a specific property type PropertyType.
*/
template < typename PropertyType, typename PropertyAccessorType >
-class Animator : public AnimatorBase, public PropertyOwner::Observer
+class Animator : public AnimatorBase
{
public:
AlphaFunction alphaFunction,
const TimePeriod& timePeriod )
{
- typedef Animator< PropertyType, PropertyAccessorType > AnimatorType;
-
// The property was const in the actor-thread, but animators are used in the scene-graph thread.
- AnimatorType* animator = new AnimatorType( const_cast<PropertyOwner*>( &propertyOwner ),
- const_cast<PropertyBase*>( &property ),
- animatorFunction );
-
- animator->SetAlphaFunction( alphaFunction );
- animator->SetIntervalDelay( timePeriod.delaySeconds );
- animator->SetDuration( timePeriod.durationSeconds );
-
- return animator;
+ return new Animator( const_cast<PropertyOwner*>( &propertyOwner ),
+ const_cast<PropertyBase*>( &property ),
+ animatorFunction,
+ alphaFunction,
+ timePeriod );
}
/**
*/
virtual ~Animator()
{
- if (mPropertyOwner && mConnectedToSceneGraph)
- {
- mPropertyOwner->RemoveObserver(*this);
- }
-
- delete mAnimatorFunction;
- }
-
- /**
- * Called when Animator is added to the scene-graph in update-thread.
- */
- virtual void ConnectToSceneGraph()
- {
- mConnectedToSceneGraph = true;
- mPropertyOwner->AddObserver(*this);
- }
-
- /**
- * Called when mPropertyOwner is connected to the scene graph.
- */
- virtual void PropertyOwnerConnected( PropertyOwner& owner )
- {
- mEnabled = true;
- }
-
- /**
- * Called when mPropertyOwner is disconnected from the scene graph.
- */
- virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
- {
- // If we are active, then bake the value if required
- if ( mAnimationPlaying && mDisconnectAction != Dali::Animation::Discard )
- {
- // Bake to target-value if BakeFinal, otherwise bake current value
- Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
- }
-
- mEnabled = false;
- }
-
- /**
- * Called shortly before mPropertyOwner is destroyed
- */
- virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
- {
- mPropertyOwner = NULL;
}
/**
- * From AnimatorBase.
+ * @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
*/
- virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
+ virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
{
- if( mLoopCount >= 0 )
- {
- // Update the progress value
- progress = SetProgress( progress );
- }
-
- float alpha = ApplyAlphaFunction( progress );
-
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 ) );
+
if ( bake )
{
mPropertyAccessor.Bake( bufferIndex, result );
{
mPropertyAccessor.Set( bufferIndex, result );
}
-
- mCurrentProgress = progress;
- }
-
- /**
- * From AnimatorBase.
- */
- virtual bool Orphan()
- {
- return (mPropertyOwner == NULL);
}
private:
*/
Animator( PropertyOwner* propertyOwner,
PropertyBase* property,
- AnimatorFunctionBase* animatorFunction )
- : mPropertyOwner( propertyOwner ),
- mPropertyAccessor( property ),
- mAnimatorFunction( animatorFunction ),
- mCurrentProgress( 0.0f )
+ AnimatorFunctionBase* animatorFunction,
+ AlphaFunction alphaFunction,
+ const TimePeriod& timePeriod )
+ : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
+ mPropertyAccessor( property )
{
// WARNING - this object is created in the event-thread
// The scene-graph mPropertyOwner object cannot be observed here
protected:
- PropertyOwner* mPropertyOwner;
PropertyAccessorType mPropertyAccessor;
- AnimatorFunctionBase* mAnimatorFunction;
- float mCurrentProgress;
};
/**
* An animator for a specific property type PropertyType.
*/
-template <typename T, typename PropertyAccessorType>
-class AnimatorTransformProperty : public AnimatorBase, public PropertyOwner::Observer
+template <typename PropertyType, typename PropertyAccessorType>
+class AnimatorTransformProperty : public AnimatorBase
{
public:
{
// The property was const in the actor-thread, but animators are used in the scene-graph thread.
- AnimatorTransformProperty* animator = new AnimatorTransformProperty( const_cast<PropertyOwner*>( &propertyOwner ),
- const_cast<PropertyBase*>( &property ),
- animatorFunction );
-
- animator->SetAlphaFunction( alphaFunction );
- animator->SetIntervalDelay( timePeriod.delaySeconds );
- animator->SetDuration( timePeriod.durationSeconds );
-
- return animator;
+ return new AnimatorTransformProperty( const_cast<PropertyOwner*>( &propertyOwner ),
+ const_cast<PropertyBase*>( &property ),
+ animatorFunction,
+ alphaFunction,
+ timePeriod );
}
/**
*/
virtual ~AnimatorTransformProperty()
{
- if (mPropertyOwner && mConnectedToSceneGraph)
- {
- mPropertyOwner->RemoveObserver(*this);
- }
-
- delete mAnimatorFunction;
- }
-
- /**
- * Called when Animator is added to the scene-graph in update-thread.
- */
- virtual void ConnectToSceneGraph()
- {
- mConnectedToSceneGraph = true;
- mPropertyOwner->AddObserver(*this);
- }
-
- /**
- * Called when mPropertyOwner is connected to the scene graph.
- */
- virtual void PropertyOwnerConnected( PropertyOwner& owner )
- {
- mEnabled = true;
- }
-
- /**
- * Called when mPropertyOwner is disconnected from the scene graph.
- */
- virtual void PropertyOwnerDisconnected( BufferIndex bufferIndex, PropertyOwner& owner )
- {
- // If we are active, then bake the value if required
- if ( mAnimationPlaying && mDisconnectAction != Dali::Animation::Discard )
- {
- // Bake to target-value if BakeFinal, otherwise bake current value
- Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
- }
-
- mEnabled = false;
- }
-
- /**
- * Called shortly before mPropertyOwner is destroyed
- */
- virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
- {
- mPropertyOwner = NULL;
}
/**
- * From AnimatorBase.
+ * @copydoc AnimatorBase::DoUpdate( BufferIndex bufferIndex, bool bake, float alpha )
*/
- virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
+ virtual void DoUpdate( BufferIndex bufferIndex, bool bake, float alpha ) override final
{
- if( mLoopCount >= 0 )
- {
- // Update the progress value
- progress = SetProgress( progress );
- }
-
- float alpha = ApplyAlphaFunction( progress );
-
- const T& current = mPropertyAccessor.Get( bufferIndex );
+ const PropertyType& current = mPropertyAccessor.Get( bufferIndex );
// need to cast the return value in case property is integer
- T result = static_cast<T>( (*mAnimatorFunction)( alpha, current ) );
+ const PropertyType result = static_cast<PropertyType>( (*mAnimatorFunction)( alpha, current ) );
if ( bake )
{
{
mPropertyAccessor.Set( bufferIndex, result );
}
-
- mCurrentProgress = progress;
- }
-
- /**
- * From AnimatorBase.
- */
- virtual bool Orphan()
- {
- return (mPropertyOwner == NULL);
}
private:
*/
AnimatorTransformProperty( PropertyOwner* propertyOwner,
PropertyBase* property,
- AnimatorFunctionBase* animatorFunction )
- : mPropertyOwner( propertyOwner ),
- mPropertyAccessor( property ),
- mAnimatorFunction( animatorFunction ),
- mCurrentProgress( 0.0f )
+ AnimatorFunctionBase* animatorFunction,
+ AlphaFunction alphaFunction,
+ const TimePeriod& timePeriod )
+ : AnimatorBase( propertyOwner, animatorFunction, alphaFunction, timePeriod ),
+ mPropertyAccessor( property )
{
// WARNING - this object is created in the event-thread
// The scene-graph mPropertyOwner object cannot be observed here
}
// Undefined
- AnimatorTransformProperty( const AnimatorTransformProperty& );
-
- // Undefined
- AnimatorTransformProperty& operator=( const AnimatorTransformProperty& );
+ AnimatorTransformProperty() = delete;
+ AnimatorTransformProperty( const AnimatorTransformProperty& ) = delete;
+ AnimatorTransformProperty& operator=( const AnimatorTransformProperty& ) = delete;
protected:
- PropertyOwner* mPropertyOwner;
PropertyAccessorType mPropertyAccessor;
- AnimatorFunctionBase* mAnimatorFunction;
- float mCurrentProgress;
};
} // namespace SceneGraph
-/*
- * AnimatorFunction base class.
- * 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;
- }
-};
-
// Update functions
struct AnimateByInteger : public AnimatorFunctionBase
} // namespace Dali
-#endif // __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
+#endif // DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H