/*
- * 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.
// CLASS HEADER
#include <dali/internal/event/animation/animation-impl.h>
+#include <dali/public-api/object/property-map.h>
// EXTERNAL INCLUDES
-#include <cstring> // for strcmp
// INTERNAL INCLUDES
-#include <dali/public-api/actors/actor.h>
#include <dali/public-api/animation/alpha-function.h>
#include <dali/public-api/animation/time-period.h>
#include <dali/public-api/common/dali-common.h>
#include <dali/public-api/object/type-registry.h>
#include <dali/public-api/math/vector2.h>
#include <dali/public-api/math/radian.h>
-#include <dali/internal/event/actors/actor-impl.h>
#include <dali/internal/event/animation/animation-playlist.h>
#include <dali/internal/event/animation/animator-connector.h>
#include <dali/internal/event/common/notification-manager.h>
#include <dali/internal/event/common/property-helper.h>
#include <dali/internal/event/common/stage-impl.h>
#include <dali/internal/event/common/thread-local-storage.h>
-#include <dali/internal/event/effects/shader-effect-impl.h>
+#include <dali/internal/update/animation/scene-graph-animator.h>
#include <dali/internal/update/manager/update-manager.h>
using Dali::Internal::SceneGraph::UpdateManager;
{
Stage* stage = Stage::GetCurrent();
- AnimationPlaylist& playlist = stage->GetAnimationPlaylist();
-
- if( durationSeconds < 0.0f )
+ if( stage )
{
- DALI_LOG_WARNING("duration should be greater than 0.0f.\n");
- durationSeconds = 0.0f;
- }
+ AnimationPlaylist& playlist = stage->GetAnimationPlaylist();
+
+ if( durationSeconds < 0.0f )
+ {
+ DALI_LOG_WARNING("duration should be greater than 0.0f.\n");
+ durationSeconds = 0.0f;
+ }
- AnimationPtr animation = new Animation( *stage, playlist, durationSeconds, DEFAULT_END_ACTION, DEFAULT_DISCONNECT_ACTION, DEFAULT_ALPHA_FUNCTION );
+ AnimationPtr animation = new Animation( *stage, playlist, durationSeconds, DEFAULT_END_ACTION, DEFAULT_DISCONNECT_ACTION, DEFAULT_ALPHA_FUNCTION );
- // Second-phase construction
- animation->Initialize();
+ // Second-phase construction
+ animation->Initialize();
- return animation;
+ return animation;
+ }
+ else
+ {
+ return NULL;
+ }
}
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),
- mIsLooping( false ),
- mPlayRange( Vector2(0.0f,1.0f)),
+ mNotificationCount( 0 ),
+ mLoopCount(1),
+ mCurrentLoop(0),
mEndAction( endAction ),
mDisconnectAction( disconnectAction ),
- mDefaultAlpha( defaultAlpha )
+ mDefaultAlpha( defaultAlpha ),
+ mState(Dali::Animation::STOPPED)
{
}
DALI_ASSERT_DEBUG( mAnimation == NULL );
// Create a new animation, temporarily owned
- SceneGraph::Animation* animation = SceneGraph::Animation::New( mDurationSeconds, mSpeedFactor, mPlayRange, mIsLooping, mEndAction, mDisconnectAction );
+ SceneGraph::Animation* animation = SceneGraph::Animation::New( mDurationSeconds, mSpeedFactor, mPlayRange, mLoopCount, mEndAction, mDisconnectAction );
// Keep a const pointer to the animation.
mAnimation = animation;
return mDurationSeconds;
}
-void Animation::SetLooping(bool looping)
+void Animation::SetLooping(bool on)
+{
+ SetLoopCount( on ? 0 : 1 );
+}
+
+void Animation::SetLoopCount(int count)
{
// Cache for public getters
- mIsLooping = looping;
+ mLoopCount = count;
// mAnimation is being used in a separate thread; queue a message to set the value
- SetLoopingMessage( mEventThreadServices, *mAnimation, looping );
+ SetLoopingMessage( mEventThreadServices, *mAnimation, mLoopCount );
+}
+
+int Animation::GetLoopCount()
+{
+ return mLoopCount;
+}
+
+int Animation::GetCurrentLoop()
+{
+ return mCurrentLoop;
}
bool Animation::IsLooping() const
{
- // This is not animatable; the cached value is up-to-date.
- return mIsLooping;
+ return mLoopCount != 1;
}
void Animation::SetEndAction(EndAction action)
// Update the current playlist
mPlaylist.OnPlay( *this );
+ mState = Dali::Animation::PLAYING;
+
+ 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 );
+ }
+ }
+ }
+
// mAnimation is being used in a separate thread; queue a Play message
PlayAnimationMessage( mEventThreadServices, *mAnimation );
}
// Update the current playlist
mPlaylist.OnPlay( *this );
+ mState = Dali::Animation::PLAYING;
+
// mAnimation is being used in a separate thread; queue a Play message
PlayAnimationFromMessage( mEventThreadServices, *mAnimation, progress );
}
void Animation::Pause()
{
+ mState = Dali::Animation::PAUSED;
+
// mAnimation is being used in a separate thread; queue a Pause message
PauseAnimationMessage( mEventThreadServices, *mAnimation );
}
+Dali::Animation::State Animation::GetState() const
+{
+ return mState;
+}
+
void Animation::Stop()
{
+ mState = Dali::Animation::STOPPED;
+
// mAnimation is being used in a separate thread; queue a Stop message
StopAnimationMessage( mEventThreadServices.GetUpdateManager(), *mAnimation );
}
// Remove all the connectors
mConnectors.Clear();
+ // Reset the connector target values
+ mConnectorTargetValues.clear();
+
// Replace the old scene-object with a new one
DestroySceneObject();
CreateSceneObject();
void Animation::AnimateBy(Property& target, Property::Value& relativeValue, AlphaFunction alpha, TimePeriod period)
{
- Object& object = dynamic_cast<Object&>( GetImplementation(target.object) );
+ Object& object = GetImplementation( target.object );
+ const Property::Type targetType = object.GetPropertyType( target.propertyIndex );
+ const Property::Type destinationType = relativeValue.GetType();
+ DALI_ASSERT_ALWAYS( targetType == destinationType && "Animated value and Property type don't match" );
ExtendDuration( period );
- switch ( relativeValue.GetType() )
+ switch ( targetType )
{
case Property::BOOLEAN:
{
break;
}
- case Property::UNSIGNED_INTEGER:
- {
- AddAnimatorConnector( AnimatorConnector<unsigned int>::New( object,
- target.propertyIndex,
- target.componentIndex,
- new AnimateByUnsignedInteger(relativeValue.Get<unsigned int>()),
- alpha,
- period ) );
- break;
- }
-
case Property::FLOAT:
{
AddAnimatorConnector( AnimatorConnector<float>::New( object,
}
default:
- DALI_ASSERT_ALWAYS( false && "Property type enumeration out of bounds" ); // should never come here
- break;
+ {
+ // non animatable types handled already
+ }
}
}
void Animation::AnimateTo(Property& target, Property::Value& destinationValue, AlphaFunction alpha, TimePeriod period)
{
- Object& object = dynamic_cast<Object&>( 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;
+ mConnectorTargetValues.push_back( connectorPair );
+
+ switch ( destinationType )
{
case Property::BOOLEAN:
{
break;
}
- case Property::UNSIGNED_INTEGER:
- {
- AddAnimatorConnector( AnimatorConnector<unsigned int>::New( targetObject,
- targetPropertyIndex,
- componentIndex,
- new AnimateToUnsignedInteger( destinationValue.Get<unsigned int>() ),
- alpha,
- period ) );
- break;
- }
-
case Property::FLOAT:
{
AddAnimatorConnector( AnimatorConnector<float>::New( targetObject,
case Property::VECTOR3:
{
- if ( Dali::Actor::Property::SIZE == targetPropertyIndex )
- {
- // Test whether this is actually an Actor
- Actor* maybeActor = dynamic_cast<Actor*>( &targetObject );
- if ( maybeActor )
- {
- // Notify the actor that its size is being animated
- maybeActor->NotifySizeAnimation( *this, destinationValue.Get<Vector3>() );
- }
- }
-
AddAnimatorConnector( AnimatorConnector<Vector3>::New( targetObject,
targetPropertyIndex,
componentIndex,
}
default:
- DALI_ASSERT_ALWAYS( false && "Property type enumeration out of bounds" ); // should never come here
- break;
+ {
+ // non animatable types handled already
+ }
}
}
void Animation::AnimateBetween(Property target, const KeyFrames& keyFrames, AlphaFunction alpha, TimePeriod period, Interpolation interpolation)
{
- Object& object = dynamic_cast<Object&>( GetImplementation(target.object) );
+ Object& object = GetImplementation( target.object );
ExtendDuration( period );
break;
}
- case Dali::Property::UNSIGNED_INTEGER:
- {
- const KeyFrameUnsignedInteger* kf;
- GetSpecialization(keyFrames, kf);
- KeyFrameUnsignedIntegerPtr kfCopy = KeyFrameUnsignedInteger::Clone(*kf);
- AddAnimatorConnector( AnimatorConnector<int>::New( object,
- target.propertyIndex,
- target.componentIndex,
- new KeyFrameUnsignedIntegerFunctor(kfCopy,interpolation),
- alpha,
- period ) );
- break;
- }
-
case Dali::Property::FLOAT:
{
const KeyFrameNumber* kf;
break;
}
- default: // not all property types are animateable
- break;
+ default:
+ {
+ // non animatable types handled by keyframes
+ }
}
}
bool Animation::HasFinished()
{
bool hasFinished(false);
- const int playCount(mAnimation->GetPlayCount());
+ const int playedCount(mAnimation->GetPlayedCount());
// If the play count has been incremented, then another notification is required
- if (playCount > mNotificationCount)
+ mCurrentLoop = mAnimation->GetCurrentLoop();
+
+ if (playedCount > mNotificationCount)
{
// Note that only one signal is emitted, if the animation has been played repeatedly
- mNotificationCount = playCount;
+ mNotificationCount = playedCount;
hasFinished = true;
+
+ mState = Dali::Animation::STOPPED;
}
return hasFinished;
Dali::Animation handle( this );
mFinishedSignal.Emit( handle );
}
-
- // This callback is used internally, to avoid the overhead of using a signal.
- if ( mFinishedCallback )
- {
- mFinishedCallback( mFinishedCallbackObject );
- }
}
bool Animation::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
{
bool connected( true );
- Animation* animation = dynamic_cast<Animation*>(object);
+ Animation* animation = static_cast< Animation* >(object); // TypeRegistry guarantees that this is the correct type.
- if ( 0 == strcmp( signalName.c_str(), SIGNAL_FINISHED ) )
+ if( 0 == signalName.compare( SIGNAL_FINISHED ) )
{
animation->FinishedSignal().Connect( tracker, functor );
}
return connected;
}
-void Animation::SetFinishedCallback( FinishedCallback callback, Object* object )
-{
- mFinishedCallback = callback;
- mFinishedCallbackObject = object;
-}
-
void Animation::AddAnimatorConnector( AnimatorConnectorBase* connector )
{
DALI_ASSERT_DEBUG( NULL != connector );
TimePeriod(delaySeconds, 0.0f/*immediate*/) ) );
}
-bool Animation::DoAction( BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes )
+bool Animation::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
{
bool done = false;
Animation* animation = dynamic_cast<Animation*>( object );
if( animation )
{
- if( 0 == strcmp( actionName.c_str(), ACTION_PLAY ) )
+ if( 0 == actionName.compare( ACTION_PLAY ) )
{
- if( attributes.size() > 0 )
+ if( Property::Value* value = attributes.Find("duration", Property::FLOAT) )
{
- animation->SetDuration( attributes[0].Get<float>() );
+ animation->SetDuration( value->Get<float>() );
}
animation->Play();
done = true;
}
- else if( 0 == strcmp( actionName.c_str(), ACTION_STOP ) )
+ else if( 0 == actionName.compare( ACTION_STOP ) )
{
animation->Stop();
done = true;
}
- else if( 0 == strcmp( actionName.c_str(), ACTION_PAUSE ) )
+ else if( 0 == actionName.compare( ACTION_PAUSE ) )
{
animation->Pause();
done = true;
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 ) );
+}
} // namespace Internal