Refactored Internal::Actor by moving some methods into the Relayouter
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / actor-relayouter.cpp
index 5348273..0e1fcf3 100644 (file)
 #include <dali/public-api/math/vector3.h>
 #include <dali/internal/event/size-negotiation/relayout-controller-impl.h>
 
+namespace
+{
+#if defined(DEBUG_ENABLED)
+Debug::Filter* gLogRelayoutFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RELAYOUT_TIMER" );
+#endif
+} // unnamed namespace
+
 namespace Dali
 {
 
@@ -363,6 +370,29 @@ bool Actor::Relayouter::IsLayoutDirty( Dimension::Type dimension ) const
   return false;
 }
 
+void Actor::Relayouter::SetPreferredSize( Actor& actor, const Vector2& size )
+{
+  // If valid width or height, then set the resize policy to FIXED
+  // A 0 width or height may also be required so if the resize policy has not been changed, i.e. is still set to DEFAULT,
+  // then change to FIXED as well
+
+  if( size.width > 0.0f || GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::DEFAULT )
+  {
+    actor.SetResizePolicy( ResizePolicy::FIXED, Dimension::WIDTH );
+  }
+
+  if( size.height > 0.0f || GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::DEFAULT )
+  {
+    actor.SetResizePolicy( ResizePolicy::FIXED, Dimension::HEIGHT );
+  }
+
+  actor.mRelayoutData->preferredSize = size;
+
+  actor.mUseAnimatedSize = AnimatedSizeFlag::CLEAR;
+
+  actor.RelayoutRequest();
+}
+
 float Actor::Relayouter::ClampDimension( const Internal::Actor& actor, float size, Dimension::Type dimension )
 {
   const float minSize = actor.GetMinimumSize( dimension );
@@ -371,6 +401,152 @@ float Actor::Relayouter::ClampDimension( const Internal::Actor& actor, float siz
   return std::max( minSize, std::min( size, maxSize ) );
 }
 
+void Actor::Relayouter::NegotiateDimension( Actor& actor, Dimension::Type dimension, const Vector2& allocatedSize, Actor::ActorDimensionStack& recursionStack )
+{
+  // Check if it needs to be negotiated
+  if( actor.IsLayoutDirty( dimension ) && !actor.IsLayoutNegotiated( dimension ) )
+  {
+    // Check that we havn't gotten into an infinite loop
+    Actor::ActorDimensionPair searchActor = Actor::ActorDimensionPair( &actor, dimension );
+    bool recursionFound = false;
+    for( auto& element : recursionStack )
+    {
+      if( element == searchActor )
+      {
+        recursionFound = true;
+        break;
+      }
+    }
+
+    if( !recursionFound )
+    {
+      // Record the path that we have taken
+      recursionStack.push_back( Actor::ActorDimensionPair( &actor, dimension ) );
+
+      // Dimension dependency check
+      for( uint32_t i = 0; i < Dimension::DIMENSION_COUNT; ++i )
+      {
+        Dimension::Type dimensionToCheck = static_cast< Dimension::Type >( 1 << i );
+
+        if( actor.RelayoutDependentOnDimension( dimension, dimensionToCheck ) )
+        {
+          NegotiateDimension( actor, dimensionToCheck, allocatedSize, recursionStack );
+        }
+      }
+
+      // Parent dependency check
+      Actor* parent = actor.GetParent();
+      if( parent && actor.RelayoutDependentOnParent( dimension ) )
+      {
+        NegotiateDimension( *parent, dimension, allocatedSize, recursionStack );
+      }
+
+      // Children dependency check
+      if( actor.RelayoutDependentOnChildren( dimension ) )
+      {
+        for( uint32_t i = 0, count = actor.GetChildCount(); i < count; ++i )
+        {
+          ActorPtr child = actor.GetChildAt( i );
+
+          // Only relayout child first if it is not dependent on this actor
+          if( !child->RelayoutDependentOnParent( dimension ) )
+          {
+            NegotiateDimension( *child, dimension, allocatedSize, recursionStack );
+          }
+        }
+      }
+
+      // For deriving classes
+      actor.OnCalculateRelayoutSize( dimension );
+
+      // All dependencies checked, calculate the size and set negotiated flag
+      const float newSize = ClampDimension( actor, actor.CalculateSize( dimension, allocatedSize ), dimension );
+
+      actor.SetNegotiatedDimension( newSize, dimension );
+      actor.SetLayoutNegotiated( true, dimension );
+
+      // For deriving classes
+      actor.OnLayoutNegotiated( newSize, dimension );
+
+      // This actor has been successfully processed, pop it off the recursion stack
+      recursionStack.pop_back();
+    }
+    else
+    {
+      // TODO: Break infinite loop
+      actor.SetLayoutNegotiated( true, dimension );
+    }
+  }
+}
+
+void Actor::Relayouter::NegotiateDimensions(Actor& actor, const Vector2& allocatedSize)
+{
+  // Negotiate all dimensions that require it
+  ActorDimensionStack recursionStack;
+
+  for( uint32_t i = 0; i < Dimension::DIMENSION_COUNT; ++i )
+  {
+    const Dimension::Type dimension = static_cast< Dimension::Type >( 1 << i );
+
+    // Negotiate
+    NegotiateDimension(actor, dimension, allocatedSize, recursionStack);
+  }
+}
+
+void Actor::Relayouter::NegotiateSize(Actor& actor, const Vector2& allocatedSize, RelayoutContainer& container)
+{
+  // Force a size negotiation for actors that has assigned size during relayout
+  // This is required as otherwise the flags that force a relayout will not
+  // necessarilly be set. This will occur if the actor has already been laid out.
+  // The dirty flags are then cleared. Then if the actor is added back into the
+  // relayout container afterwards, the dirty flags would still be clear...
+  // causing a relayout to be skipped. Here we force any actors added to the
+  // container to be relayed out.
+  DALI_LOG_TIMER_START( NegSizeTimer1 );
+
+  if( actor.GetUseAssignedSize(Dimension::WIDTH ) )
+  {
+    actor.SetLayoutNegotiated( false, Dimension::WIDTH );
+  }
+  if( actor.GetUseAssignedSize( Dimension::HEIGHT ) )
+  {
+    actor.SetLayoutNegotiated( false, Dimension::HEIGHT );
+  }
+
+  // Do the negotiation
+  NegotiateDimensions(actor, allocatedSize);
+
+  // Set the actor size
+  actor.SetNegotiatedSize( container );
+
+  // Negotiate down to children
+  for( uint32_t i = 0, count = actor.GetChildCount(); i < count; ++i )
+  {
+    ActorPtr child = actor.GetChildAt( i );
+
+    // Forces children that have already been laid out to be relayed out
+    // if they have assigned size during relayout.
+    if( child->GetUseAssignedSize(Dimension::WIDTH) )
+    {
+      child->SetLayoutNegotiated(false, Dimension::WIDTH);
+      child->SetLayoutDirty(true, Dimension::WIDTH);
+    }
+
+    if( child->GetUseAssignedSize(Dimension::HEIGHT) )
+    {
+      child->SetLayoutNegotiated(false, Dimension::HEIGHT);
+      child->SetLayoutDirty(true, Dimension::HEIGHT);
+    }
+
+    // Only relayout if required
+    if( child->RelayoutRequired() )
+    {
+      container.Add( Dali::Actor( child.Get() ), actor.mTargetSize.GetVectorXY() );
+    }
+  }
+  DALI_LOG_TIMER_END( NegSizeTimer1, gLogRelayoutFilter, Debug::Concise, "NegotiateSize() took: ");
+}
+
 } // namespace Internal
 
 } // namespace Dali