X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Fupdate%2Fanimation%2Fscene-graph-animator.h;h=ce7b1c0b798781845f86fa71ec554e292972b879;hb=3e5a6bbb6a052fba7e514bd7e8781afc1db6fa37;hp=486d2debe01eca64d9be83d104a6faa5088de164;hpb=498f1cb8ba757d9cac46920b3956beea66891384;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/update/animation/scene-graph-animator.h b/dali/internal/update/animation/scene-graph-animator.h index 486d2de..ce7b1c0 100644 --- a/dali/internal/update/animation/scene-graph-animator.h +++ b/dali/internal/update/animation/scene-graph-animator.h @@ -2,7 +2,7 @@ #define __DALI_INTERNAL_SCENE_GRAPH_ANIMATOR_H__ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 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. @@ -19,11 +19,6 @@ */ // INTERNAL INCLUDES -#include -#include -#include -#include -#include #include #include #include @@ -31,6 +26,13 @@ #include #include #include +#include +#include +#include +#include +#include +#include +#include namespace Dali { @@ -63,16 +65,39 @@ public: typedef float (*AlphaFunc)(float progress); ///< Definition of an alpha function /** + * Observer to determine when the animator is no longer present + */ + class LifecycleObserver + { + public: + /** + * Called shortly before the animator is destroyed. + */ + virtual void ObjectDestroyed() = 0; + + protected: + /** + * Virtual destructor, no deletion through this interface + */ + virtual ~LifecycleObserver() = default; + }; + + + /** * Constructor. */ AnimatorBase() - : mDurationSeconds(1.0f), - mInitialDelaySeconds(0.0f), + : mLifecycleObserver(nullptr), + mDurationSeconds(1.0f), + mIntervalDelaySeconds(0.0f), + mSpeedFactor(1.0f), + mLoopCount(1), mAlphaFunction(AlphaFunction::DEFAULT), mDisconnectAction(Dali::Animation::BakeFinal), mActive(false), mEnabled(true), - mConnectedToSceneGraph(false) + mConnectedToSceneGraph(false), + mAutoReverseEnabled( false ) { } @@ -81,6 +106,20 @@ public: */ virtual ~AnimatorBase() { + if( mLifecycleObserver != nullptr ) + { + mLifecycleObserver->ObjectDestroyed(); + } + } + + void AddLifecycleObserver( LifecycleObserver& observer ) + { + mLifecycleObserver = &observer; + } + + void RemoveLifecycleObserver( LifecycleObserver& observer ) + { + mLifecycleObserver = nullptr; } /** @@ -104,28 +143,62 @@ public: * Retrieve the duration of the animator. * @return The duration in seconds. */ - float GetDuration() + float GetDuration() const { return mDurationSeconds; } + void SetSpeedFactor( float factor ) + { + mSpeedFactor = factor; + } + + void SetLoopCount(int32_t loopCount) + { + mLoopCount = loopCount; + } + + float SetProgress( float progress ) + { + float value = 0.0f; + + if( mAutoReverseEnabled ) + { + if( mSpeedFactor > 0.0f ) + { + value = 1.0f - 2.0f * std::abs( progress - 0.5f ); + } + // Reverse mode + else if( mSpeedFactor < 0.0f ) + { + value = 2.0f * std::abs( progress - 0.5f ); + } + } + else + { + value = progress; + } + + return value; + } + /** * Set the delay before the animator should take effect. * 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; } /** @@ -146,7 +219,7 @@ public: return mAlphaFunction; } - /* + /** * Applies the alpha function to the specified progress * @param[in] Current progress * @return The progress after the alpha function has been aplied @@ -256,7 +329,7 @@ public: float upperBound(1.0f); float currentT(0.5f); float currentX = EvaluateCubicBezier( controlPoints.x, controlPoints.z, currentT); - while( fabs( progress - currentX ) > tolerance ) + while( fabsf( progress - currentX ) > tolerance ) { if( progress > currentX ) { @@ -315,7 +388,7 @@ public: return mActive; } - /* + /** * Retrive wheter the animator's target object is valid and on the stage. * @return The enabled state. */ @@ -323,6 +396,16 @@ public: { return mEnabled; } + + /** + * @brief Sets the looping mode. + * @param[in] loopingMode True when the looping mode is AUTO_REVERSE + */ + void SetLoopingMode( bool loopingMode ) + { + mAutoReverseEnabled = loopingMode; + } + /** * Returns wheter the target object of the animator is still valid * or has been destroyed. @@ -354,8 +437,12 @@ protected: return 3.0f*(1.0f-t)*(1.0f-t)*t*p0 + 3.0f*(1.0f-t)*tSquare*p1 + tSquare*t; } + LifecycleObserver* mLifecycleObserver; float mDurationSeconds; - float mInitialDelaySeconds; + float mIntervalDelaySeconds; + float mSpeedFactor; + + int32_t mLoopCount; AlphaFunction mAlphaFunction; @@ -363,6 +450,7 @@ protected: 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. + bool mAutoReverseEnabled:1; }; /** @@ -395,7 +483,7 @@ public: animatorFunction ); animator->SetAlphaFunction( alphaFunction ); - animator->SetInitialDelay( timePeriod.delaySeconds ); + animator->SetIntervalDelay( timePeriod.delaySeconds ); animator->SetDuration( timePeriod.durationSeconds ); return animator; @@ -411,10 +499,7 @@ public: mPropertyOwner->RemoveObserver(*this); } - if( mAnimatorFunction ) - { - delete mAnimatorFunction; - } + delete mAnimatorFunction; } /** @@ -456,8 +541,6 @@ public: virtual void PropertyOwnerDestroyed( PropertyOwner& owner ) { mPropertyOwner = NULL; - mPropertyAccessor.Reset(); - mEnabled = false; } /** @@ -465,11 +548,18 @@ public: */ virtual void Update( BufferIndex bufferIndex, float progress, bool bake ) { - float alpha = ApplyAlphaFunction(progress); + if( mLoopCount >= 0 ) + { + // Update the progress value + progress = SetProgress( progress ); + } + + float alpha = ApplyAlphaFunction( progress ); const PropertyType& current = mPropertyAccessor.Get( bufferIndex ); - const PropertyType result = (*mAnimatorFunction)( alpha, current ); + // need to cast the return value in case property is integer + const PropertyType result = static_cast( (*mAnimatorFunction)( alpha, current ) ); if ( bake ) { mPropertyAccessor.Bake( bufferIndex, result ); @@ -522,6 +612,167 @@ protected: float mCurrentProgress; }; + + +/** + * An animator for a specific property type PropertyType. + */ +template +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 ), + const_cast( &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 ) + { + if( mLoopCount >= 0 ) + { + // Update the progress value + progress = SetProgress( progress ); + } + + float alpha = ApplyAlphaFunction( progress ); + + const T& current = mPropertyAccessor.Get( bufferIndex ); + + // need to cast the return value in case property is integer + T result = static_cast( (*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 /* @@ -546,14 +797,9 @@ struct AnimatorFunctionBase return property; } - virtual float operator()(float progress, const int& property) + virtual float operator()(float progress, const int32_t& property) { - return property; - } - - virtual float operator()(float progress, const unsigned int& property) - { - return property; + return static_cast( property ); } virtual float operator()(float progress, const float& property) @@ -591,12 +837,13 @@ struct AnimateByInteger : public AnimatorFunctionBase { } - float operator()(float alpha, const int& property) + using AnimatorFunctionBase::operator(); + float operator()(float alpha, const int32_t& property) { - return int(property + mRelative * alpha + 0.5f ); + return truncf(static_cast( property ) + static_cast( mRelative ) * alpha + 0.5f ); } - int mRelative; + int32_t mRelative; }; struct AnimateToInteger : public AnimatorFunctionBase @@ -606,12 +853,13 @@ struct AnimateToInteger : public AnimatorFunctionBase { } - float operator()(float alpha, const int& property) + using AnimatorFunctionBase::operator(); + float operator()(float alpha, const int32_t& property) { - return int(property + ((mTarget - property) * alpha) + 0.5f); + return truncf(static_cast( property ) + (static_cast(mTarget - property) * alpha) + 0.5f); } - int mTarget; + int32_t mTarget; }; struct AnimateByFloat : public AnimatorFunctionBase @@ -621,6 +869,7 @@ struct AnimateByFloat : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); float operator()(float alpha, const float& property) { return float(property + mRelative * alpha); @@ -636,6 +885,7 @@ struct AnimateToFloat : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); float operator()(float alpha, const float& property) { return float(property + ((mTarget - property) * alpha)); @@ -651,6 +901,7 @@ struct AnimateByVector2 : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector2 operator()(float alpha, const Vector2& property) { return Vector2(property + mRelative * alpha); @@ -666,6 +917,7 @@ struct AnimateToVector2 : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector2 operator()(float alpha, const Vector2& property) { return Vector2(property + ((mTarget - property) * alpha)); @@ -681,6 +933,7 @@ struct AnimateByVector3 : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector3 operator()(float alpha, const Vector3& property) { return Vector3(property + mRelative * alpha); @@ -696,6 +949,7 @@ struct AnimateToVector3 : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector3 operator()(float alpha, const Vector3& property) { return Vector3(property + ((mTarget - property) * alpha)); @@ -711,6 +965,7 @@ struct AnimateByVector4 : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector4 operator()(float alpha, const Vector4& property) { return Vector4(property + mRelative * alpha); @@ -726,6 +981,7 @@ struct AnimateToVector4 : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector4 operator()(float alpha, const Vector4& property) { return Vector4(property + ((mTarget - property) * alpha)); @@ -741,6 +997,7 @@ struct AnimateByOpacity : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector4 operator()(float alpha, const Vector4& property) { Vector4 result(property); @@ -759,6 +1016,7 @@ struct AnimateToOpacity : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector4 operator()(float alpha, const Vector4& property) { Vector4 result(property); @@ -777,6 +1035,7 @@ struct AnimateByBoolean : public AnimatorFunctionBase { } + 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 @@ -793,6 +1052,7 @@ struct AnimateToBoolean : public AnimatorFunctionBase { } + 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 @@ -810,6 +1070,7 @@ struct RotateByAngleAxis : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Quaternion operator()(float alpha, const Quaternion& rotation) { if (alpha > 0.0f) @@ -831,6 +1092,7 @@ struct RotateToQuaternion : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Quaternion operator()(float alpha, const Quaternion& rotation) { return Quaternion::Slerp(rotation, mTarget, alpha); @@ -847,6 +1109,7 @@ struct KeyFrameBooleanFunctor : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); bool operator()(float progress, const bool& property) { if(mKeyFrames->IsActive(progress)) @@ -866,13 +1129,14 @@ struct KeyFrameIntegerFunctor : public AnimatorFunctionBase { } - float operator()(float progress, const int& property) + using AnimatorFunctionBase::operator(); + float operator()(float progress, const int32_t& property) { if(mKeyFrames->IsActive(progress)) { - return mKeyFrames->GetValue(progress, mInterpolation); + return static_cast( mKeyFrames->GetValue(progress, mInterpolation) ); } - return property; + return static_cast( property ); } KeyFrameIntegerPtr mKeyFrames; @@ -886,6 +1150,7 @@ struct KeyFrameNumberFunctor : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); float operator()(float progress, const float& property) { if(mKeyFrames->IsActive(progress)) @@ -906,6 +1171,7 @@ struct KeyFrameVector2Functor : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector2 operator()(float progress, const Vector2& property) { if(mKeyFrames->IsActive(progress)) @@ -927,6 +1193,7 @@ struct KeyFrameVector3Functor : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector3 operator()(float progress, const Vector3& property) { if(mKeyFrames->IsActive(progress)) @@ -947,6 +1214,7 @@ struct KeyFrameVector4Functor : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector4 operator()(float progress, const Vector4& property) { if(mKeyFrames->IsActive(progress)) @@ -967,6 +1235,7 @@ struct KeyFrameQuaternionFunctor : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Quaternion operator()(float progress, const Quaternion& property) { if(mKeyFrames->IsActive(progress)) @@ -986,6 +1255,7 @@ struct PathPositionFunctor : public AnimatorFunctionBase { } + using AnimatorFunctionBase::operator(); Vector3 operator()(float progress, const Vector3& property) { Vector3 position(property); @@ -1005,6 +1275,7 @@ struct PathRotationFunctor : public AnimatorFunctionBase mForward.Normalize(); } + using AnimatorFunctionBase::operator(); Quaternion operator()(float progress, const Quaternion& property) { Vector3 tangent; @@ -1022,7 +1293,6 @@ struct PathRotationFunctor : public AnimatorFunctionBase Vector3 mForward; }; - } // namespace Internal } // namespace Dali