(Button) Allow button size to return natural size
authorKingsley Stephens <k.stephens@partner.samsung.com>
Thu, 8 May 2014 13:32:33 +0000 (14:32 +0100)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Tue, 27 May 2014 14:35:04 +0000 (15:35 +0100)
[Problem] Button needs to give itself a sensible size if no size is set on it
[Cause] Currently buttons with no size will take up the size of the stage
[Solution] Make buttons adjust natural size to wrap around any label they have

Change-Id: I17600d7376ae33b605d0b21dca3a8a7362ed7fb8

base/dali-toolkit/internal/controls/buttons/push-button-impl.cpp
base/dali-toolkit/internal/controls/buttons/push-button-impl.h
base/dali-toolkit/public-api/controls/default-controls/push-button-factory.cpp

index 03752f1..d007c03 100644 (file)
@@ -27,6 +27,7 @@
 #include "push-button-default-painter-impl.h"
 
 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
 #include "push-button-default-painter-impl.h"
 
 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
+#include <dali-toolkit/internal/controls/relayout-helper.h>
 
 namespace Dali
 {
 
 namespace Dali
 {
@@ -81,12 +82,36 @@ namespace
 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
 
 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
 
+const float TEXT_PADDING = 12.0f;
+
 // Helper function used to cast a ButtonPainter to PushButtonDefaultPainter
 PushButtonDefaultPainterPtr GetPushButtonPainter( Dali::Toolkit::Internal::ButtonPainterPtr painter )
 {
   return static_cast<PushButtonDefaultPainter*>( painter.Get() );
 }
 
 // Helper function used to cast a ButtonPainter to PushButtonDefaultPainter
 PushButtonDefaultPainterPtr GetPushButtonPainter( Dali::Toolkit::Internal::ButtonPainterPtr painter )
 {
   return static_cast<PushButtonDefaultPainter*>( painter.Get() );
 }
 
+/**
+ * Find the first image actor in the actor hierarchy
+ */
+ImageActor FindImageActor( Actor root )
+{
+  ImageActor imageActor = ImageActor::DownCast( root );
+  if( !imageActor && root )
+  {
+    for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
+    {
+      ImageActor childImageActor = FindImageActor( root.GetChildAt( i ) );
+      if( childImageActor )
+      {
+        return childImageActor;
+      }
+    }
+  }
+
+  return imageActor;
+}
+
+
 } // unnamed namespace
 
 Dali::Toolkit::PushButton PushButton::New()
 } // unnamed namespace
 
 Dali::Toolkit::PushButton PushButton::New()
@@ -678,6 +703,69 @@ void PushButton::OnActivated()
   DoClickAction(attributes);
 }
 
   DoClickAction(attributes);
 }
 
+Vector3 PushButton::GetNaturalSize()
+{
+  Vector3 size = ControlImpl::GetNaturalSize();
+
+  const bool widthIsZero = EqualsZero( size.width );
+  const bool heightIsZero = EqualsZero( size.height );
+
+  if( widthIsZero || heightIsZero )
+  {
+    // If background and background not scale9 try get size from that
+    ImageActor imageActor = FindImageActor( mButtonImage );
+    if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
+    {
+      Vector3 imageSize = RelayoutHelper::GetNaturalSize( imageActor );
+
+      if( widthIsZero )
+      {
+        size.width = imageSize.width;
+      }
+
+      if( heightIsZero )
+      {
+        size.height = imageSize.height;
+      }
+    }
+
+    ImageActor backgroundImageActor = FindImageActor( mBackgroundImage );
+    if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
+    {
+      Vector3 imageSize = RelayoutHelper::GetNaturalSize( backgroundImageActor );
+
+      if( widthIsZero )
+      {
+        size.width = std::max( size.width, imageSize.width );
+      }
+
+      if( heightIsZero )
+      {
+        size.height = std::max( size.height, imageSize.height );
+      }
+    }
+
+    // If label, test against it's size
+    Toolkit::TextView textView = Toolkit::TextView::DownCast( mLabel );
+    if( textView )
+    {
+      Vector3 textViewSize = textView.GetNaturalSize();
+
+      if( widthIsZero )
+      {
+        size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
+      }
+
+      if( heightIsZero )
+      {
+        size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
+      }
+    }
+  }
+
+  return size;
+}
+
 void PushButton::DoClickAction(const PropertyValueContainer& attributes)
 {
   // Prevents the button signals from doing a recursive loop by sending an action
 void PushButton::DoClickAction(const PropertyValueContainer& attributes)
 {
   // Prevents the button signals from doing a recursive loop by sending an action
index b463513..8f65610 100644 (file)
@@ -327,6 +327,11 @@ protected: // From ControlImpl
    */
   virtual void OnActivated();
 
    */
   virtual void OnActivated();
 
+  /**
+   * @copydoc Control::GetNaturalSize()
+   */
+  virtual Vector3 GetNaturalSize();
+
 private:
 
   /**
 private:
 
   /**
index bbc21dc..2484f02 100644 (file)
@@ -43,7 +43,6 @@ Alignment CreateAlignedImage( Actor image )
 Alignment CreateAlignedImage( const std::string& imagePath )
 {
   Image image = Image::New( imagePath );
 Alignment CreateAlignedImage( const std::string& imagePath )
 {
   Image image = Image::New( imagePath );
-
   return CreateAlignedImage( ImageActor::New( image ) );
 }
 
   return CreateAlignedImage( ImageActor::New( image ) );
 }