Add size animation in layout measure phase.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / layout-item-impl.cpp
index f70a5f1..fe26362 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,49 @@ 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_CHILD_FOCUS:
+    mImpl->mOnChildFocusTransitionData = layoutTransitionDataPtr;
+    break;
+  case Dali::Toolkit::LayoutTransitionData::ON_OWNER_SET:
+    mImpl->mOnOwnerSetTransitionData = layoutTransitionDataPtr;
+    break;
+  case Dali::Toolkit::LayoutTransitionData::ON_LAYOUT_CHANGE:
+    mImpl->mOnLayoutChangeTransitionData = 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_CHILD_FOCUS:
+    return mImpl->mOnChildFocusTransitionData.Get();
+  case Dali::Toolkit::LayoutTransitionData::ON_OWNER_SET:
+    return mImpl->mOnOwnerSetTransitionData.Get();
+  case Dali::Toolkit::LayoutTransitionData::ON_LAYOUT_CHANGE:
+    return mImpl->mOnLayoutChangeTransitionData.Get();
+  default:
+    return LayoutTransitionDataPtr();
+  }
+}
+
 void LayoutItem::RegisterChildProperties( const std::string& containerType )
 {
   // Call on derived types
@@ -198,14 +272,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.childrenLayoutDataArray.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.childrenLayoutDataArray.size() )
+  {
+    layoutData.childrenLayoutDataArray.resize( size );
+  }
+
   mImpl->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_LAYOUT );
   mImpl->SetPrivateFlag( Impl::PRIVATE_FLAG_IS_LAID_OUT );
 }
@@ -333,14 +416,43 @@ 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::Type layoutAnimationType )
+{
+  Toolkit::Control control = Toolkit::Control::DownCast( mImpl->mOwner );
+  if ( control )
+  {
+    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), layoutAnimationType );
+}
+
+void LayoutItem::RequestLayout( Dali::Toolkit::LayoutTransitionData::Type layoutAnimationType, Actor gainedChild, Actor lostChild )
+{
+  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, gainedChild, lostChild );
 }
 
 bool LayoutItem::IsLayoutRequested() const
@@ -384,7 +496,7 @@ void LayoutItem::SetMeasuredDimensions( MeasuredSize measuredWidth, MeasuredSize
 LayoutLength LayoutItem::GetMeasuredWidth() const
 {
   // Get the size portion of the measured width
-  return  mImpl->mMeasuredWidth.GetSize();
+  return mImpl->mMeasuredWidth.GetSize();
 }
 
 LayoutLength LayoutItem::GetMeasuredHeight() const
@@ -469,54 +581,76 @@ 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;
+    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);
-
-    mImpl->mLeft = left;
-    mImpl->mTop = top;
-    mImpl->mRight = right;
-    mImpl->mBottom = bottom;
+  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->ClearPrivateFlag( Impl::PRIVATE_FLAG_FORCE_SET_FRAME );
+  mImpl->mLeft = left;
+  mImpl->mTop = top;
+  mImpl->mRight = right;
+  mImpl->mBottom = bottom;
 
+  // Reflect up to parent control
+  auto owner = GetOwner();
+  auto actor = Actor::DownCast( owner );
+  LayoutData& layoutData = *mImpl->sLayoutData;
 
-    // Reflect up to parent control
-    auto owner = GetOwner();
-    auto actor = Actor::DownCast(owner);
-    if( actor )
+  if( actor )
+  {
+    if( changed || mImpl->mAnimated )
     {
-      DALI_LOG_STREAM( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame owner(" << left << ", " << top << ", " << right << ", " << bottom << ")\n" );
+      layoutData.layoutPositionDataArray.push_back(
+        LayoutPositionData( actor, left.AsDecimal(), top.AsDecimal(), right.AsDecimal(), bottom.AsDecimal(), mImpl->mAnimated ) );
+    }
 
-      if( mImpl->mAnimated )
+    if( mImpl->mAnimated && !layoutData.speculativeLayout )
+    {
+      if( layoutData.layoutTransition.layoutTransitionType != -1 )
       {
-        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() );
-
-        animation.AnimateTo( Property( actor, Actor::Property::SIZE_WIDTH ), newWidth.AsInteger() );
-        animation.AnimateTo( Property( actor, Actor::Property::SIZE_HEIGHT ), newHeight.AsInteger() );
-
-        animation.FinishedSignal().Connect( mSlotDelegate, &LayoutItem::OnLayoutAnimationFinished );
-        animation.Play();
+        LayoutItem* transitionOwner = layoutData.layoutTransition.layoutItem.Get();
+        LayoutTransitionDataPtr layoutTransitionDataPtr = GetTransitionData( layoutData.layoutTransition.layoutTransitionType );
+
+        // 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 );
+          layoutTransitionDataPtr->CollectLayoutDataElements( actor, layoutData );
+          changed = true;
+        }
+        else
+        {
+          LayoutTransitionData::CollectChildrenLayoutDataElements( actor, layoutData );
+        }
       }
       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 )
+        {
+          LayoutTransitionDataPtr layoutTransitionDataPtr = GetTransitionData( Dali::Toolkit::LayoutTransitionData::ON_LAYOUT_CHANGE );
+          if( layoutTransitionDataPtr ) // has custom default animation and normal update
+          {
+            DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame apply custom default transition to (%s), transition type (%d)\n", actor.GetName().c_str(), Dali::Toolkit::LayoutTransitionData::ON_LAYOUT_CHANGE );
+            layoutTransitionDataPtr->CollectLayoutDataElements( actor, layoutData );
+          }
+          else
+          {
+            DALI_LOG_INFO( gLayoutFilter, Debug::Verbose, "LayoutItem::SetFrame apply default transition to (%s), transition type (%d)\n", actor.GetName().c_str(), layoutData.layoutTransition.layoutTransitionType );
+            GetDefaultTransition()->CollectLayoutDataElements( actor, layoutData );
+          }
+        }
       }
     }
+  }
 
-    if( sizeChanged )
-    {
-      SizeChange( LayoutSize( newWidth, newHeight ), LayoutSize( oldWidth, oldHeight ) );
-    }
+  // 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" );