#define __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__
/*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 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.
*/
// INTERNAL INCLUDES
-#include <dali/internal/common/owner-container.h>
+#include <dali/devel-api/common/owner-container.h>
#include <dali/internal/event/animation/key-frames-impl.h>
#include <dali/internal/event/animation/path-impl.h>
#include <dali/internal/update/nodes/node.h>
#include <dali/public-api/common/dali-common.h>
#include <dali/public-api/math/quaternion.h>
#include <dali/public-api/math/radian.h>
+#include <dali/internal/update/animation/property-accessor.h>
+
namespace Dali
{
*/
AnimatorBase()
: mDurationSeconds(1.0f),
- mInitialDelaySeconds(0.0f),
+ mIntervalDelaySeconds(0.0f),
mAlphaFunction(AlphaFunction::DEFAULT),
mDisconnectAction(Dali::Animation::BakeFinal),
mActive(false),
- mEnabled(true)
+ mEnabled(true),
+ mConnectedToSceneGraph(false)
{
}
}
/**
+ * Called when Animator is added to the scene-graph in update-thread.
+ */
+ virtual void ConnectToSceneGraph() = 0;
+
+ /**
* Set the duration of the animator.
* @pre durationSeconds must be zero or greater; zero is useful when animating boolean values.
* @param [in] seconds Duration in seconds.
* Retrieve the duration of the animator.
* @return The duration in seconds.
*/
- float GetDuration()
+ float GetDuration() const
{
return mDurationSeconds;
}
* The default is zero i.e. no delay.
* @param [in] seconds The delay in seconds.
*/
- void SetInitialDelay(float seconds)
+ void SetIntervalDelay(float seconds)
{
- mInitialDelaySeconds = seconds;
+ mIntervalDelaySeconds = seconds;
}
/**
- * Retrieve the initial delay of the animator.
+ * Retrieve the delay before the animator should take effect.
* @return The delay in seconds.
*/
- float GetInitialDelay()
+ float GetIntervalDelay() const
{
- return mInitialDelaySeconds;
+ return mIntervalDelaySeconds;
}
/**
result = 1.0f + progress * progress * ( ( sqrt2 + 1.0f ) * progress + sqrt2 );
break;
}
- default:
+ case AlphaFunction::COUNT:
+ {
break;
+ }
}
}
else if( alphaFunctionMode == AlphaFunction::CUSTOM_FUNCTION )
}
float mDurationSeconds;
- float mInitialDelaySeconds;
+ float mIntervalDelaySeconds;
AlphaFunction mAlphaFunction;
Dali::Animation::EndAction mDisconnectAction; ///< EndAction to apply when target object gets disconnected from the stage.
bool mActive:1; ///< Animator is "active" while it's running.
bool mEnabled:1; ///< Animator is "enabled" while its target object is valid and on the stage.
+ bool mConnectedToSceneGraph:1; ///< True if ConnectToSceneGraph() has been called in update-thread.
};
/**
animatorFunction );
animator->SetAlphaFunction( alphaFunction );
- animator->SetInitialDelay( timePeriod.delaySeconds );
+ animator->SetIntervalDelay( timePeriod.delaySeconds );
animator->SetDuration( timePeriod.durationSeconds );
return animator;
*/
virtual ~Animator()
{
- if (mPropertyOwner)
+ if (mPropertyOwner && mConnectedToSceneGraph)
{
mPropertyOwner->RemoveObserver(*this);
}
- if( mAnimatorFunction )
- {
- delete mAnimatorFunction;
- }
+ delete mAnimatorFunction;
+ }
+
+ /**
+ * Called when Animator is added to the scene-graph in update-thread.
+ */
+ virtual void ConnectToSceneGraph()
+ {
+ mConnectedToSceneGraph = true;
+ mPropertyOwner->AddObserver(*this);
}
/**
virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
{
mPropertyOwner = NULL;
- mPropertyAccessor.Reset();
- mEnabled = false;
}
/**
mAnimatorFunction( animatorFunction ),
mCurrentProgress( 0.0f )
{
- mPropertyOwner->AddObserver(*this);
+ // WARNING - this object is created in the event-thread
+ // The scene-graph mPropertyOwner object cannot be observed here
}
// Undefined
float mCurrentProgress;
};
+
+
+/**
+ * An animator for a specific property type PropertyType.
+ */
+template <typename T, typename PropertyAccessorType>
+class AnimatorTransformProperty : public AnimatorBase, public PropertyOwner::Observer
+{
+public:
+
+ /**
+ * Construct a new property animator.
+ * @param[in] property The animatable property; only valid while the Animator is attached.
+ * @param[in] animatorFunction The function used to animate the property.
+ * @param[in] alphaFunction The alpha function to apply.
+ * @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 )
+ {
+
+ // 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;
+ }
+
+ /**
+ * Virtual destructor.
+ */
+ 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 ( mActive && mDisconnectAction != Dali::Animation::Discard )
+ {
+ // Bake to target-value if BakeFinal, otherwise bake current value
+ Update( bufferIndex, ( mDisconnectAction == Dali::Animation::Bake ? mCurrentProgress : 1.0f ), true );
+ }
+
+ mActive = false;
+ mEnabled = false;
+ }
+
+ /**
+ * Called shortly before mPropertyOwner is destroyed
+ */
+ virtual void PropertyOwnerDestroyed( PropertyOwner& owner )
+ {
+ mPropertyOwner = NULL;
+ }
+
+ /**
+ * From AnimatorBase.
+ */
+ virtual void Update( BufferIndex bufferIndex, float progress, bool bake )
+ {
+ float alpha = ApplyAlphaFunction(progress);
+
+ const T& current = mPropertyAccessor.Get( bufferIndex );
+
+ const T result = (*mAnimatorFunction)( alpha, current );
+
+
+ if ( bake )
+ {
+ mPropertyAccessor.Bake( bufferIndex, result );
+ }
+ else
+ {
+ mPropertyAccessor.Set( bufferIndex, result );
+ }
+
+ mCurrentProgress = progress;
+ }
+
+ /**
+ * From AnimatorBase.
+ */
+ virtual bool Orphan()
+ {
+ return (mPropertyOwner == NULL);
+ }
+
+private:
+
+ /**
+ * Private constructor; see also Animator::New().
+ */
+ AnimatorTransformProperty( PropertyOwner* propertyOwner,
+ PropertyBase* property,
+ AnimatorFunctionBase* animatorFunction )
+ : mPropertyOwner( propertyOwner ),
+ mPropertyAccessor( property ),
+ mAnimatorFunction( animatorFunction ),
+ mCurrentProgress( 0.0f )
+ {
+ // 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& );
+
+protected:
+
+ PropertyOwner* mPropertyOwner;
+ PropertyAccessorType mPropertyAccessor;
+
+ AnimatorFunctionBase* mAnimatorFunction;
+ float mCurrentProgress;
+};
+
} // namespace SceneGraph
/*
{
}
+ using AnimatorFunctionBase::operator();
float operator()(float alpha, const int& property)
{
return int(property + mRelative * alpha + 0.5f );
{
}
+ using AnimatorFunctionBase::operator();
float operator()(float alpha, const int& property)
{
return int(property + ((mTarget - property) * alpha) + 0.5f);
{
}
+ using AnimatorFunctionBase::operator();
float operator()(float alpha, const float& property)
{
return float(property + mRelative * alpha);
{
}
+ using AnimatorFunctionBase::operator();
float operator()(float alpha, const float& property)
{
return float(property + ((mTarget - property) * alpha));
{
}
+ using AnimatorFunctionBase::operator();
Vector2 operator()(float alpha, const Vector2& property)
{
return Vector2(property + mRelative * alpha);
{
}
+ using AnimatorFunctionBase::operator();
Vector2 operator()(float alpha, const Vector2& property)
{
return Vector2(property + ((mTarget - property) * alpha));
{
}
+ using AnimatorFunctionBase::operator();
Vector3 operator()(float alpha, const Vector3& property)
{
return Vector3(property + mRelative * alpha);
{
}
+ using AnimatorFunctionBase::operator();
Vector3 operator()(float alpha, const Vector3& property)
{
return Vector3(property + ((mTarget - property) * alpha));
{
}
+ using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
return Vector4(property + mRelative * alpha);
{
}
+ using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
return Vector4(property + ((mTarget - property) * alpha));
{
}
+ using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
Vector4 result(property);
{
}
+ using AnimatorFunctionBase::operator();
Vector4 operator()(float alpha, const Vector4& property)
{
Vector4 result(property);
{
}
+ 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
{
}
+ 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
{
}
+ using AnimatorFunctionBase::operator();
Quaternion operator()(float alpha, const Quaternion& rotation)
{
if (alpha > 0.0f)
{
}
+ using AnimatorFunctionBase::operator();
Quaternion operator()(float alpha, const Quaternion& rotation)
{
return Quaternion::Slerp(rotation, mTarget, alpha);
{
}
+ using AnimatorFunctionBase::operator();
bool operator()(float progress, const bool& property)
{
if(mKeyFrames->IsActive(progress))
{
}
+ using AnimatorFunctionBase::operator();
float operator()(float progress, const int& property)
{
if(mKeyFrames->IsActive(progress))
{
}
+ using AnimatorFunctionBase::operator();
float operator()(float progress, const float& property)
{
if(mKeyFrames->IsActive(progress))
{
}
+ using AnimatorFunctionBase::operator();
Vector2 operator()(float progress, const Vector2& property)
{
if(mKeyFrames->IsActive(progress))
{
}
+ using AnimatorFunctionBase::operator();
Vector3 operator()(float progress, const Vector3& property)
{
if(mKeyFrames->IsActive(progress))
{
}
+ using AnimatorFunctionBase::operator();
Vector4 operator()(float progress, const Vector4& property)
{
if(mKeyFrames->IsActive(progress))
{
}
+ using AnimatorFunctionBase::operator();
Quaternion operator()(float progress, const Quaternion& property)
{
if(mKeyFrames->IsActive(progress))
{
}
+ using AnimatorFunctionBase::operator();
Vector3 operator()(float progress, const Vector3& property)
{
- return mPath->SamplePosition(progress );
+ Vector3 position(property);
+ static_cast<void>( mPath->SamplePosition(progress, position) );
+ return position;
}
PathPtr mPath;
mForward.Normalize();
}
+ using AnimatorFunctionBase::operator();
Quaternion operator()(float progress, const Quaternion& property)
{
- Vector3 tangent( mPath->SampleTangent(progress) );
- return Quaternion( mForward, tangent );
+ Vector3 tangent;
+ if( mPath->SampleTangent(progress, tangent) )
+ {
+ return Quaternion( mForward, tangent );
+ }
+ else
+ {
+ return property;
+ }
}
PathPtr mPath;
Vector3 mForward;
};
-
} // namespace Internal
} // namespace Dali