Add Layout complex animation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-item-impl.cpp
index d68e544..cd4cf40 100644 (file)
 #include <dali/public-api/animation/animation.h>
 #include <dali/public-api/object/type-registry-helper.h>
 #include <dali-toolkit/public-api/controls/control.h>
+#include <dali/devel-api/object/handle-devel.h>
 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
 #include <dali-toolkit/devel-api/layouting/layout-group-impl.h>
+#include <dali-toolkit/internal/layouting/layout-transition-data-impl.h>
 #include <dali-toolkit/internal/layouting/layout-item-data-impl.h>
 
+#include <dali/devel-api/scripting/enum-helper.h>
+
 namespace
 {
 
@@ -33,7 +37,6 @@ Debug::Filter* gLayoutFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG
 const char* WIDTH_SPECIFICATION_NAME( "widthSpecification" );
 const char* HEIGHT_SPECIFICATION_NAME( "heightSpecification" );
 
-const float DEFAULT_TRANSITION_DURATION( 0.5f );
 }
 
 namespace Dali
@@ -66,7 +69,6 @@ void LayoutItem::Initialize( Handle& owner, const std::string& containerType )
   mImpl->mOwner = &(owner.GetBaseObject());
   RegisterChildProperties( containerType );
   OnInitialize(); // Ensure direct deriving class gets initialized
-  RequestLayout();
 }
 
 Handle LayoutItem::GetOwner() const
@@ -93,6 +95,35 @@ void LayoutItem::Unparent()
   mImpl->mOwner = NULL;
 }
 
+LayoutTransitionDataPtr LayoutItem::GetDefaultTransition()
+{
+  DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::GetDefaultTransition\n" );
+  if ( !mImpl->mDefaultTransitionData.Get() )
+  {
+    auto owner = GetOwner();
+    auto actor = Actor::DownCast( owner );
+
+    mImpl->mDefaultTransitionData = LayoutTransitionData::New();
+    {
+      Property::Map map;
+      map[ Dali::Toolkit::LayoutTransitionData::AnimatorKey::PROPERTY ] = Actor::Property::POSITION;
+      map[ Dali::Toolkit::LayoutTransitionData::AnimatorKey::TARGET_VALUE ] = Property::Value(); // capture from layout update
+      map[ Dali::Toolkit::LayoutTransitionData::AnimatorKey::ANIMATOR ] = std::string(); // default animator with default duration
+      // Capture calculated position after layout, apply default linear animation
+      mImpl->mDefaultTransitionData->AddPropertyAnimator( actor, map );
+    }
+    {
+      Property::Map map;
+      map[ Dali::Toolkit::LayoutTransitionData::AnimatorKey::PROPERTY ] = Actor::Property::SIZE;
+      map[ Dali::Toolkit::LayoutTransitionData::AnimatorKey::TARGET_VALUE ] = Property::Value(); // capture from layout update
+      map[ Dali::Toolkit::LayoutTransitionData::AnimatorKey::ANIMATOR ] = std::string(); // default animator with default duration
+      // Capture calculated size after layout, apply default linear animation
+      mImpl->mDefaultTransitionData->AddPropertyAnimator( actor, map );
+    }
+  }
+  return mImpl->mDefaultTransitionData;
+}
+
 void LayoutItem::SetAnimateLayout( bool animateLayout )
 {
   auto owner = GetOwner();
@@ -111,6 +142,39 @@ bool LayoutItem::IsLayoutAnimated() const
   return mImpl->mAnimated;
 }
 
+void LayoutItem::SetTransitionData( int layoutTransitionType, Internal::LayoutTransitionDataPtr layoutTransitionDataPtr )
+{
+  switch ( layoutTransitionType )
+  {
+  case Dali::Toolkit::LayoutTransitionData::ON_CHILD_ADD:
+    mImpl->mOnChildAddTransitionData = layoutTransitionDataPtr;
+    break;
+  case Dali::Toolkit::LayoutTransitionData::ON_CHILD_REMOVE:
+    mImpl->mOnChildRemoveTransitionData = layoutTransitionDataPtr;
+    break;
+  case Dali::Toolkit::LayoutTransitionData::ON_OWNER_SET:
+    mImpl->mOnOwnerSetTransitionData = layoutTransitionDataPtr;
+    break;
+  default:
+    break;
+  }
+}
+
+Internal::LayoutTransitionDataPtr LayoutItem::GetTransitionData( int layoutTransitionType ) const
+{
+  switch ( layoutTransitionType )
+  {
+  case Dali::Toolkit::LayoutTransitionData::ON_CHILD_ADD:
+    return mImpl->mOnChildAddTransitionData.Get();
+  case Dali::Toolkit::LayoutTransitionData::ON_CHILD_REMOVE:
+    return mImpl->mOnChildRemoveTransitionData.Get();
+  case Dali::Toolkit::LayoutTransitionData::ON_OWNER_SET:
+    return mImpl->mOnOwnerSetTransitionData.Get();
+  default:
+    return LayoutTransitionDataPtr();
+  }
+}
+
 void LayoutItem::RegisterChildProperties( const std::string& containerType )
 {
   // Call on derived types
@@ -159,7 +223,11 @@ void LayoutItem::Measure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasur
 
   const bool needsLayout = specChanged && ( !isSpecExactly || !matchesSpecSize );
 
-  DALI_LOG_STREAM( gLayoutFilter, Debug::General, "LayoutItem::Measure("<<widthMeasureSpec<<", "<<heightMeasureSpec<<") Owner:"<<Actor::DownCast(GetOwner()).GetName() <<"  forceLayout="<<forceLayout<<", specChanged="<<specChanged<<", isSpecExactly="<<isSpecExactly<<", matchesSpecSize="<<matchesSpecSize<<", needsLayout="<<needsLayout <<std::endl <<(forceLayout||needsLayout?"  Remeasuring":"  NoChange"));
+  DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::Measure("<<widthMeasureSpec<<", "<<heightMeasureSpec<<") Owner:"
+                                                  <<Actor::DownCast(GetOwner()).GetName() <<"  forceLayout="<<forceLayout
+                                                  <<", specChanged="<<specChanged<<", isSpecExactly="<<isSpecExactly
+                                                  <<", matchesSpecSize="<<matchesSpecSize
+                                                  <<", needsLayout="<<needsLayout <<(forceLayout||needsLayout?"  Remeasuring":"  NoChange"));
 
   if( forceLayout || needsLayout )
   {
@@ -194,14 +262,23 @@ void LayoutItem::Layout( LayoutLength l, LayoutLength t, LayoutLength r, LayoutL
     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_MEASURE_NEEDED_BEFORE_LAYOUT );
   }
 
+  LayoutData& layoutData = *mImpl->sLayoutData;
+  size_t size = layoutData.childrenPropertyAnimators.size();
+
   bool changed = SetFrame( l, t, r, b );
 
   if( changed || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED ) )
   {
+
     OnLayout( changed, l, t, r, b );
     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_LAYOUT_REQUIRED );
   }
 
+  if ( size != layoutData.childrenPropertyAnimators.size() )
+  {
+    layoutData.childrenPropertyAnimators.resize( size );
+  }
+
   mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
 }
@@ -266,6 +343,8 @@ LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureS
   auto specMode = measureSpec.GetMode();
   auto specSize = measureSpec.GetSize();
 
+  DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::GetDefaultSize MeasureSpec("<<measureSpec<< ") size:" << size << "\n" );
+
   switch (specMode)
   {
     case MeasureSpec::Mode::UNSPECIFIED:
@@ -275,8 +354,12 @@ LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureS
     }
     case MeasureSpec::Mode::AT_MOST:
     {
+      // Ensure the default size does not exceed the spec size unless the default size is 0.
+      // Another container could provide a default size of 0.
       LayoutLength tmp = specSize;
-      if( size < tmp )
+
+      // Do not set size to 0, use specSize in this case as could be a legacy container
+      if( size < tmp && size > LayoutLength( 0 ) )
       {
         result = size;
       }
@@ -292,6 +375,7 @@ LayoutLength LayoutItem::GetDefaultSize( LayoutLength size, MeasureSpec measureS
       break;
     }
   }
+  DALI_LOG_STREAM( gLayoutFilter, Debug::General, "LayoutItem::GetDefaultSize setting default size:" << result << "\n" );
   return result;
 }
 
@@ -299,6 +383,7 @@ void LayoutItem::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeas
 {
   DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::OnMeasure\n");
 
+  // GetDefaultSize will limit the MeasureSpec to the suggested minimumWidth and minimumHeight
   SetMeasuredDimensions( GetDefaultSize( GetSuggestedMinimumWidth(), widthMeasureSpec ),
                          GetDefaultSize( GetSuggestedMinimumHeight(), heightMeasureSpec ) );
 }
@@ -321,14 +406,29 @@ LayoutParent* LayoutItem::GetParent()
 void LayoutItem::RequestLayout()
 {
   Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
+  if( control )
+  {
+    DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout control(%s)\n",
+        control.GetName().c_str() );
+  }
+  // @todo Enforce failure if called in Measure/Layout passes.
+  mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
+  Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
+  layoutController.RequestLayout( Toolkit::LayoutItem( this ) );
+}
+
+void LayoutItem::RequestLayout( Dali::Toolkit::LayoutTransitionData::LayoutTransitionType layoutAnimationType )
+{
+  Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
   if ( control )
   {
-    DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout %s\n", control.GetName().c_str());
+    DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::RequestLayout control(%s) layoutTranstionType(%d)\n",
+        control.GetName().c_str(), (int)layoutAnimationType );
   }
   // @todo Enforce failure if called in Measure/Layout passes.
   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
   Toolkit::LayoutController layoutController = Toolkit::LayoutController::Get();
-  layoutController.RequestLayout( Toolkit::LayoutItem(this) );
+  layoutController.RequestLayout( Toolkit::LayoutItem(this), layoutAnimationType );
 }
 
 bool LayoutItem::IsLayoutRequested() const
@@ -341,9 +441,28 @@ void LayoutItem::SetLayoutRequested()
   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
 }
 
+bool LayoutItem::IsResizePolicyRequired() const
+{
+  return mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_USE_RESIZE_POLICY );
+}
+
+void LayoutItem::SetResizePolicyRequired( bool resizePolicyRequired )
+{
+  if( resizePolicyRequired )
+  {
+    mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_USE_RESIZE_POLICY );
+  }
+  else
+  {
+    mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_USE_RESIZE_POLICY );
+  }
+}
+
 void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize measuredHeight )
 {
-  DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetMeasuredDimensions width(" << measuredWidth.GetSize() << ") height(" << measuredHeight.GetSize() << ") \n" );
+
+  DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetMeasuredDimensions width(" << measuredWidth.GetSize() << ") height(" << measuredHeight.GetSize() << ") Control:" <<
+                        ( ( Actor::DownCast( GetOwner()) ) ? Actor::DownCast(GetOwner()).GetName().c_str() : "Invalid Actor" ) << "\n" );
 
   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_MEASURED_DIMENSION_SET );
   mImpl->mMeasuredWidth = measuredWidth;
@@ -438,56 +557,65 @@ bool LayoutItem::SetFrame( LayoutLength left, LayoutLength top, LayoutLength rig
   if( mImpl->mLeft != left || mImpl->mRight != right || mImpl->mTop != top || mImpl->mBottom != bottom || mImpl->GetPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME ) )
   {
     changed = true;
-
-    LayoutLength oldWidth = mImpl->mRight - mImpl->mLeft;
-    LayoutLength oldHeight = mImpl->mBottom - mImpl->mTop;
-    LayoutLength newWidth = right - left;
-    LayoutLength newHeight = bottom - top;
-    bool sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
-
-    mImpl->mLeft = left;
-    mImpl->mTop = top;
-    mImpl->mRight = right;
-    mImpl->mBottom = bottom;
-
     mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
+  }
 
+  LayoutLength oldWidth = mImpl->mRight - mImpl->mLeft;
+  LayoutLength oldHeight = mImpl->mBottom - mImpl->mTop;
+  LayoutLength newWidth = right - left;
+  LayoutLength newHeight = bottom - top;
+  bool sizeChanged = ( newWidth != oldWidth ) || ( newHeight != oldHeight );
 
-    // Reflect up to parent control
-    auto owner = GetOwner();
-    auto actor = Actor::DownCast(owner);
-    if( actor )
-    {
-      DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame owner(" << left << ", " << top << ", " << right << ", " << bottom << ")\n" );
+  mImpl->mLeft = left;
+  mImpl->mTop = top;
+  mImpl->mRight = right;
+  mImpl->mBottom = bottom;
 
-      if( mImpl->mAnimated )
-      {
-        auto animation = Animation::New( 0.5f );
-        animation.AnimateTo( Property( actor, Actor::Property::POSITION_X ), left.AsInteger() );
-        animation.AnimateTo( Property( actor, Actor::Property::POSITION_Y ), top.AsInteger() );
+  // Reflect up to parent control
+  auto owner = GetOwner();
+  auto actor = Actor::DownCast( owner );
+  LayoutData& layoutData = *mImpl->sLayoutData;
+  if( actor )
+  {
+    if( mImpl->mAnimated && !layoutData.speculativeLayout )
+    {
 
-        animation.AnimateTo( Property( actor, Actor::Property::SIZE_WIDTH ), newWidth.AsInteger() );
-        animation.AnimateTo( Property( actor, Actor::Property::SIZE_HEIGHT ), newHeight.AsInteger() );
+      LayoutItem* transitionOwner = layoutData.layoutTransition.layoutItem.Get();
+      LayoutTransitionDataPtr layoutTransitionDataPtr = GetTransitionData( layoutData.layoutTransition.layoutTransitionType );
 
-        animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
-        animation.Play();
+      // Found transition owner
+      if( transitionOwner == this && layoutTransitionDataPtr.Get() )
+      {
+        DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame apply transition to (%s), transition type (%d)\n", actor.GetName().c_str(), layoutData.layoutTransition.layoutTransitionType );
+        layoutData.layoutPositionDataArray.push_back( LayoutPositionData( actor, left.AsDecimal(), top.AsDecimal(), right.AsDecimal(), bottom.AsDecimal(), true ) );
+        layoutTransitionDataPtr->ConvertToLayoutDataElements( actor, layoutData );
+        changed = true;
       }
       else
       {
-        // @todo Collate into list of Property & Property::Value pairs.
-        actor.SetX( left.AsInteger() );
-        actor.SetY( top.AsInteger() );
-        actor.SetProperty( Actor::Property::SIZE_WIDTH, newWidth.AsInteger() );
-        actor.SetProperty( Actor::Property::SIZE_HEIGHT, newHeight.AsInteger() );
+        if( changed )
+        {
+          DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame apply default transition to (%s), transition type (%d)\n", actor.GetName().c_str(), layoutData.layoutTransition.layoutTransitionType );
+          layoutData.layoutPositionDataArray.push_back( LayoutPositionData( actor, left.AsDecimal(), top.AsDecimal(), right.AsDecimal(), bottom.AsDecimal(), true ) );
+          GetDefaultTransition()->ConvertToLayoutDataElements( actor, layoutData );
+        }
       }
     }
-
-    if( sizeChanged )
+    else
     {
-      SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
+      if( changed )
+      {
+        layoutData.layoutPositionDataArray.push_back( LayoutPositionData( actor, left.AsDecimal(), top.AsDecimal(), right.AsDecimal(), bottom.AsDecimal(), false ) );
+      }
     }
   }
 
+  // TODO: do we need it
+  if( sizeChanged )
+  {
+    SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
+  }
+
   DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame  exit(" << left << ", " << top << ", " << right << ", " << bottom << ")\n" );
 
   return changed;