X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=dali%2Finternal%2Fevent%2Fanimation%2Fanimation-impl.cpp;h=b7cc8995b0dd1132253a826b0596fb94af045243;hb=77de4c4ae763aeb42c5bf02be9191ab0694b48a1;hp=ec49265abda3eca7aa5ef912b3a966eec215eee7;hpb=7a2a26a067225660d71bda7c10ed042c5e58fb1d;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/event/animation/animation-impl.cpp b/dali/internal/event/animation/animation-impl.cpp index ec49265..b7cc899 100644 --- a/dali/internal/event/animation/animation-impl.cpp +++ b/dali/internal/event/animation/animation-impl.cpp @@ -1,5 +1,5 @@ /* - * 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. @@ -22,21 +22,20 @@ // EXTERNAL INCLUDES // INTERNAL INCLUDES -#include #include #include #include #include #include #include -#include #include #include +#include #include #include #include #include -#include +#include #include using Dali::Internal::SceneGraph::UpdateManager; @@ -114,21 +113,24 @@ AnimationPtr Animation::New(float durationSeconds) } Animation::Animation( EventThreadServices& eventThreadServices, AnimationPlaylist& playlist, float durationSeconds, EndAction endAction, EndAction disconnectAction, AlphaFunction defaultAlpha ) -: mEventThreadServices( eventThreadServices ), +: mAnimation( NULL ), + mEventThreadServices( eventThreadServices ), mPlaylist( playlist ), - mAnimation( NULL ), - mNotificationCount( 0 ), - mFinishedCallback( NULL ), - mFinishedCallbackObject( NULL ), + mFinishedSignal(), + mConnectors(), + mConnectorTargetValues(), + mPlayRange( Vector2(0.0f,1.0f)), mDurationSeconds( durationSeconds ), mSpeedFactor(1.0f), + mNotificationCount( 0 ), mLoopCount(1), mCurrentLoop(0), - mPlayRange( Vector2(0.0f,1.0f)), mEndAction( endAction ), mDisconnectAction( disconnectAction ), mDefaultAlpha( defaultAlpha ), - mState(Dali::Animation::STOPPED) + mState(Dali::Animation::STOPPED), + mProgressReachedMarker( 0.0f ), + mDelaySeconds( 0.0f ) { } @@ -160,14 +162,10 @@ void Animation::CreateSceneObject() { DALI_ASSERT_DEBUG( mAnimation == NULL ); - // Create a new animation, temporarily owned - SceneGraph::Animation* animation = SceneGraph::Animation::New( mDurationSeconds, mSpeedFactor, mPlayRange, mLoopCount, mEndAction, mDisconnectAction ); - - // Keep a const pointer to the animation. - mAnimation = animation; - - // Transfer animation ownership to the update manager through a message - AddAnimationMessage( mEventThreadServices.GetUpdateManager(), animation ); + // Create a new animation, Keep a const pointer to the animation. + mAnimation = SceneGraph::Animation::New( mDurationSeconds, mSpeedFactor, mPlayRange, mLoopCount, mEndAction, mDisconnectAction ); + OwnerPointer< SceneGraph::Animation > transferOwnership( const_cast< SceneGraph::Animation* >( mAnimation ) ); + AddAnimationMessage( mEventThreadServices.GetUpdateManager(), transferOwnership ); } void Animation::DestroySceneObject() @@ -195,6 +193,17 @@ void Animation::SetDuration(float seconds) SetDurationMessage( mEventThreadServices, *mAnimation, seconds ); } +void Animation::SetProgressNotification( float progress ) +{ + // mAnimation is being used in a separate thread; queue a message to set the value + mProgressReachedMarker = progress; +} + +float Animation::GetProgressNotification() +{ + return mProgressReachedMarker; +} + float Animation::GetDuration() const { // This is not animatable; the cached value is up-to-date. @@ -267,6 +276,10 @@ void Animation::Play() mState = Dali::Animation::PLAYING; + NotifyObjects(); + + SendFinalProgressNotificationMessage(); + // mAnimation is being used in a separate thread; queue a Play message PlayAnimationMessage( mEventThreadServices, *mAnimation ); } @@ -280,11 +293,35 @@ void Animation::PlayFrom( float progress ) mState = Dali::Animation::PLAYING; + NotifyObjects(); + + SendFinalProgressNotificationMessage(); + // mAnimation is being used in a separate thread; queue a Play message PlayAnimationFromMessage( mEventThreadServices, *mAnimation, progress ); } } +void Animation::PlayAfter( float delaySeconds ) +{ + // The negative delay means play immediately. + delaySeconds = std::max( 0.f, delaySeconds ); + + mDelaySeconds = delaySeconds; + + // Update the current playlist + mPlaylist.OnPlay( *this ); + + mState = Dali::Animation::PLAYING; + + NotifyObjects(); + + SendFinalProgressNotificationMessage(); + + // mAnimation is being used in a separate thread; queue a message to set the value + PlayAfterMessage( mEventThreadServices, *mAnimation, delaySeconds ); +} + void Animation::Pause() { mState = Dali::Animation::PAUSED; @@ -313,6 +350,9 @@ void Animation::Clear() // Remove all the connectors mConnectors.Clear(); + // Reset the connector target values + mConnectorTargetValues.clear(); + // Replace the old scene-object with a new one DestroySceneObject(); CreateSceneObject(); @@ -341,11 +381,30 @@ void Animation::AnimateBy(Property& target, Property::Value& relativeValue, Time void Animation::AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha, TimePeriod period) { - Object& object = dynamic_cast( GetImplementation(target.object) ); + Object& object = GetImplementation(target.object); + const Property::Type targetType = object.GetPropertyType(target.propertyIndex); + const Property::Type destinationType = relativeValue.GetType(); - ExtendDuration( period ); + if ( object.GetPropertyComponentIndex( target.propertyIndex ) != Property::INVALID_COMPONENT_INDEX ) + { + DALI_ASSERT_ALWAYS(Property::FLOAT == destinationType && "Animated value and Property type don't match"); + } + else + { + DALI_ASSERT_ALWAYS(targetType == destinationType && "Animated value and Property type don't match"); + } - switch ( relativeValue.GetType() ) + ExtendDuration(period); + + // Store data to later notify the object that its property is being animated + ConnectorTargetValues connectorPair; + connectorPair.targetValue = relativeValue; + connectorPair.connectorIndex = mConnectors.Count(); + connectorPair.timePeriod = period; + connectorPair.animatorType = Animation::BY; + mConnectorTargetValues.push_back( connectorPair ); + + switch ( targetType ) { case Property::BOOLEAN: { @@ -427,8 +486,9 @@ void Animation::AnimateBy(Property& target, Property::Value& relativeValue, Alph } default: - DALI_ASSERT_ALWAYS( false && "Property type enumeration out of bounds" ); // should never come here - break; + { + // non animatable types handled already + } } } @@ -449,28 +509,37 @@ void Animation::AnimateTo(Property& target, Property::Value& destinationValue, T void Animation::AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period) { - Object& object = dynamic_cast( GetImplementation(target.object) ); + Object& object = GetImplementation(target.object); AnimateTo( object, target.propertyIndex, target.componentIndex, destinationValue, alpha, period ); } void Animation::AnimateTo(Object& targetObject, Property::Index targetPropertyIndex, int componentIndex, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period) { - Property::Type type = targetObject.GetPropertyType(targetPropertyIndex); - if(componentIndex != Property::INVALID_COMPONENT_INDEX) + Property::Type targetType = targetObject.GetPropertyType(targetPropertyIndex); + if( componentIndex != Property::INVALID_COMPONENT_INDEX ) { - if( type == Property::VECTOR2 - || type == Property::VECTOR3 - || type == Property::VECTOR4 ) + if( ( targetType == Property::VECTOR2 ) || + ( targetType == Property::VECTOR3 ) || + ( targetType == Property::VECTOR4 ) ) { - type = Property::FLOAT; + targetType = Property::FLOAT; } } - DALI_ASSERT_ALWAYS( type == destinationValue.GetType() && "DestinationValue does not match Target Property type" ); + const Property::Type destinationType = destinationValue.GetType(); + DALI_ASSERT_ALWAYS( targetType == destinationType && "Animated value and Property type don't match" ); ExtendDuration( period ); - switch (destinationValue.GetType()) + // Store data to later notify the object that its property is being animated + ConnectorTargetValues connectorPair; + connectorPair.targetValue = destinationValue; + connectorPair.connectorIndex = mConnectors.Count(); + connectorPair.timePeriod = period; + connectorPair.animatorType = Animation::TO; + mConnectorTargetValues.push_back( connectorPair ); + + switch ( destinationType ) { case Property::BOOLEAN: { @@ -496,31 +565,6 @@ void Animation::AnimateTo(Object& targetObject, Property::Index targetPropertyIn case Property::FLOAT: { - if ( ( Dali::Actor::Property::SIZE_WIDTH == targetPropertyIndex ) || - ( Dali::Actor::Property::SIZE_HEIGHT == targetPropertyIndex ) || - ( Dali::Actor::Property::SIZE_DEPTH == targetPropertyIndex ) ) - { - // Test whether this is actually an Actor - Actor* maybeActor = dynamic_cast( &targetObject ); - if ( maybeActor ) - { - // Notify the actor that its size is being animated - maybeActor->NotifySizeAnimation( *this, destinationValue.Get(), targetPropertyIndex ); - } - } - else if ( ( Dali::Actor::Property::POSITION_X == targetPropertyIndex ) || - ( Dali::Actor::Property::POSITION_Y == targetPropertyIndex ) || - ( Dali::Actor::Property::POSITION_Z == targetPropertyIndex ) ) - { - // Test whether this is actually an Actor - Actor* maybeActor = dynamic_cast( &targetObject ); - if ( maybeActor ) - { - // Notify the actor that its position is being animated - maybeActor->NotifyPositionAnimation( *this, destinationValue.Get(), targetPropertyIndex ); - } - } - AddAnimatorConnector( AnimatorConnector::New( targetObject, targetPropertyIndex, componentIndex, @@ -543,27 +587,6 @@ void Animation::AnimateTo(Object& targetObject, Property::Index targetPropertyIn case Property::VECTOR3: { - if ( Dali::Actor::Property::SIZE == targetPropertyIndex ) - { - // Test whether this is actually an Actor - Actor* maybeActor = dynamic_cast( &targetObject ); - if ( maybeActor ) - { - // Notify the actor that its size is being animated - maybeActor->NotifySizeAnimation( *this, destinationValue.Get() ); - } - } - else if ( Dali::Actor::Property::POSITION == targetPropertyIndex ) - { - // Test whether this is actually an Actor - Actor* maybeActor = dynamic_cast( &targetObject ); - if ( maybeActor ) - { - // Notify the actor that its position is being animated - maybeActor->NotifyPositionAnimation( *this, destinationValue.Get() ); - } - } - AddAnimatorConnector( AnimatorConnector::New( targetObject, targetPropertyIndex, componentIndex, @@ -596,8 +619,9 @@ void Animation::AnimateTo(Object& targetObject, Property::Index targetPropertyIn } default: - DALI_ASSERT_ALWAYS( false && "Property type enumeration out of bounds" ); // should never come here - break; + { + // non animatable types handled already + } } } @@ -638,10 +662,18 @@ void Animation::AnimateBetween(Property target, const KeyFrames& keyFrames, Alph void Animation::AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation) { - Object& object = dynamic_cast( GetImplementation(target.object) ); + Object& object = GetImplementation( target.object ); ExtendDuration( period ); + // Store data to later notify the object that its property is being animated + ConnectorTargetValues connectorPair; + connectorPair.targetValue = keyFrames.GetLastKeyFrameValue(); + connectorPair.connectorIndex = mConnectors.Count(); + connectorPair.timePeriod = period; + connectorPair.animatorType = BETWEEN; + mConnectorTargetValues.push_back( connectorPair ); + switch(keyFrames.GetType()) { case Dali::Property::BOOLEAN: @@ -742,8 +774,10 @@ void Animation::AnimateBetween(Property target, const KeyFrames& keyFrames, Alph break; } - default: // not all property types are animateable - break; + default: + { + // non animatable types handled by keyframes + } } } @@ -773,6 +807,11 @@ Dali::Animation::AnimationSignalType& Animation::FinishedSignal() return mFinishedSignal; } +Dali::Animation::AnimationSignalType& Animation::ProgressReachedSignal() +{ + return mProgressReachedSignal; +} + void Animation::EmitSignalFinish() { if ( !mFinishedSignal.Empty() ) @@ -780,18 +819,21 @@ void Animation::EmitSignalFinish() Dali::Animation handle( this ); mFinishedSignal.Emit( handle ); } +} - // This callback is used internally, to avoid the overhead of using a signal. - if ( mFinishedCallback ) +void Animation::EmitSignalProgressReached() +{ + if ( !mProgressReachedSignal.Empty() ) { - mFinishedCallback( mFinishedCallbackObject ); + Dali::Animation handle( this ); + mProgressReachedSignal.Emit( handle ); } } bool Animation::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor ) { bool connected( true ); - Animation* animation = dynamic_cast(object); + Animation* animation = static_cast< Animation* >(object); // TypeRegistry guarantees that this is the correct type. if( 0 == signalName.compare( SIGNAL_FINISHED ) ) { @@ -806,12 +848,6 @@ bool Animation::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* return connected; } -void Animation::SetFinishedCallback( FinishedCallback callback, Object* object ) -{ - mFinishedCallback = callback; - mFinishedCallbackObject = object; -} - void Animation::AddAnimatorConnector( AnimatorConnectorBase* connector ) { DALI_ASSERT_DEBUG( NULL != connector ); @@ -987,6 +1023,43 @@ Vector2 Animation::GetPlayRange() const return mPlayRange; } +bool Animation::CompareConnectorEndTimes( const Animation::ConnectorTargetValues& lhs, const Animation::ConnectorTargetValues& rhs ) +{ + return ( ( lhs.timePeriod.delaySeconds + lhs.timePeriod.durationSeconds ) < ( rhs.timePeriod.delaySeconds + rhs.timePeriod.durationSeconds ) ); +} + +void Animation::NotifyObjects() +{ + if( mEndAction != EndAction::Discard ) // If the animation is discarded, then we do not want to change the target values + { + // Sort according to end time with earlier end times coming first, if the end time is the same, then the connectors are not moved + std::stable_sort( mConnectorTargetValues.begin(), mConnectorTargetValues.end(), CompareConnectorEndTimes ); + + // Loop through all connector target values sorted by increasing end time + ConnectorTargetValuesContainer::const_iterator iter = mConnectorTargetValues.begin(); + const ConnectorTargetValuesContainer::const_iterator endIter = mConnectorTargetValues.end(); + for( ; iter != endIter; ++iter ) + { + AnimatorConnectorBase* connector = mConnectors[ iter->connectorIndex ]; + + Object* object = connector->GetObject(); + if( object ) + { + object->NotifyPropertyAnimation( *this, connector->GetPropertyIndex(), iter->targetValue, iter->animatorType ); + } + } + } +} + + +void Animation::SendFinalProgressNotificationMessage() +{ + if ( mProgressReachedMarker > 0.0f ) + { + float progressMarkerSeconds = mDurationSeconds * mProgressReachedMarker; + SetProgressNotificationMessage( mEventThreadServices, *mAnimation, progressMarkerSeconds ); + } +} } // namespace Internal