Refactored Button and derived classes, moving state change and transition logic to...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-button-impl.cpp
index 0df4e3c..1ee6b92 100644 (file)
 #include "push-button-impl.h"
 
 // EXTERNAL INCLUDES
-#include <algorithm>
 #include <dali/public-api/actors/image-actor.h>
 #include <dali/public-api/object/type-registry.h>
+#include <dali/public-api/images/resource-image.h>
 
 // INTERNAL INCLUDES
-#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>
+#include <dali-toolkit/public-api/controls/text-controls/text-label.h>
 
 namespace Dali
 {
@@ -41,6 +38,9 @@ namespace Internal
 namespace
 {
 
+const float TEXT_PADDING = 12.0f;
+const float ANIMATION_TIME( 0.2f );
+
 BaseHandle Create()
 {
   return Toolkit::PushButton::New();
@@ -48,35 +48,31 @@ BaseHandle Create()
 
 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
 
+const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-up.9.png";
+const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down.9.png";
+const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-disabled.9.png";
+const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down-disabled.9.png";
+
 } // unnamed namespace
 
 namespace
 {
 
-const float TEXT_PADDING = 12.0f;
-
 /**
- * Find the first image actor in the actor hierarchy
+ * Get size of Actor if larger than given size
+ * @param[in] root the actor to get the size of
+ * @param[out] size the greater of the given size or the size of the Actor
  */
-ImageActor FindImageActor( Actor root )
+void SizeOfActorIfLarger( Actor root, Vector3& size )
 {
-  ImageActor imageActor = ImageActor::DownCast( root );
-  if( !imageActor && root )
+  if ( root )
   {
-    for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
-    {
-      ImageActor childImageActor = FindImageActor( root.GetChildAt( i ) );
-      if( childImageActor )
-      {
-        return childImageActor;
-      }
-    }
+    // RelayoutSize retreived for Actor to use any padding set to it.
+    size.width = std::max( root.GetRelayoutSize( Dimension::WIDTH ), size.width );
+    size.height = std::max( root.GetRelayoutSize( Dimension::HEIGHT ), size.height );
   }
-
-  return imageActor;
 }
 
-
 } // unnamed namespace
 
 Dali::Toolkit::PushButton PushButton::New()
@@ -94,87 +90,258 @@ Dali::Toolkit::PushButton PushButton::New()
   return pushButton;
 }
 
+PushButton::PushButton()
+: Button(),
+  mSize()
+{
+  SetAnimationTime( ANIMATION_TIME );
+}
+
+PushButton::~PushButton()
+{
+}
+
 void PushButton::OnButtonInitialize()
 {
   // Push button requires the Leave event.
-  Actor root = Self();
-  root.SetLeaveRequired( true );
+  Actor self = Self();
+  self.SetLeaveRequired( true );
+
+  // Set resize policy to natural size so that buttons will resize to background images
+  self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
+
+  Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+  Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+  Image disabledImage = Dali::ResourceImage::New( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+  Image disabledSelectedImage = Dali::ResourceImage::New( DISABLED_SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
+
+  SetButtonImage( ImageActor::New( buttonImage ) );
+  SetSelectedImage( ImageActor::New( selectedImage ) );
+  SetDisabledImage( ImageActor::New( disabledImage ) );
+  SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
 }
 
-PushButton::PushButton()
-: Button()
+void PushButton::OnLabelSet()
+{
+  Actor& label = GetLabel();
+
+  if( label )
+  {
+    label.SetAnchorPoint( AnchorPoint::CENTER );
+    label.SetParentOrigin( ParentOrigin::CENTER );
+
+    Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label );
+    if( textLabel )
+    {
+      textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
+      textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
+      textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
+    }
+
+    ConfigureSizeNegotiation();
+  }
+}
+
+void PushButton::OnButtonImageSet()
 {
-  // Creates specific painter.GetBu
-  ButtonPainterPtr painter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() );
-  SetPainter( painter );
+  ConfigureSizeNegotiation();
+  RelayoutRequest();
 }
 
-PushButton::~PushButton()
+void PushButton::OnSelectedImageSet()
 {
-  SetPainter( NULL );
+  ConfigureSizeNegotiation();
+  RelayoutRequest();
 }
 
-Vector3 PushButton::GetNaturalSize()
+void PushButton::OnBackgroundImageSet()
+{
+  ConfigureSizeNegotiation();
+  RelayoutRequest();
+}
+
+void PushButton::OnSelectedBackgroundImageSet()
 {
-  Vector3 size = Control::GetNaturalSize();
+  ConfigureSizeNegotiation();
+  RelayoutRequest();
+}
+
+void PushButton::OnDisabledImageSet()
+{
+  ConfigureSizeNegotiation();
+  RelayoutRequest();
+}
 
-  const bool widthIsZero = EqualsZero( size.width );
-  const bool heightIsZero = EqualsZero( size.height );
+void PushButton::OnDisabledSelectedImageSet()
+{
+  ConfigureSizeNegotiation();
+  RelayoutRequest();
+}
 
-  if( widthIsZero || heightIsZero )
+void PushButton::OnDisabledBackgroundImageSet()
+{
+  ConfigureSizeNegotiation();
+  RelayoutRequest();
+}
+
+void PushButton::OnSizeSet( const Vector3& targetSize )
+{
+  if( targetSize != mSize )
   {
-    // If background and background not scale9 try get size from that
-    ImageActor imageActor = FindImageActor( GetButtonImage() );
-    if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
-    {
-      Vector3 imageSize = RelayoutHelper::GetNaturalSize( imageActor );
+    mSize = targetSize;
 
-      if( widthIsZero )
-      {
-        size.width = imageSize.width;
-      }
+    Actor& label = GetLabel();
 
-      if( heightIsZero )
-      {
-        size.height = imageSize.height;
-      }
+    if( label )
+    {
+      label.SetSize( mSize );
     }
+  }
+}
 
-    ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
-    if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
+void PushButton::PrepareForTranstionIn( Actor actor )
+{
+  actor.SetOpacity( 0.0f );
+}
+
+void PushButton::PrepareForTranstionOut( Actor actor )
+{
+  actor.SetOpacity( 1.0f );
+}
+
+void PushButton::OnTransitionIn( Actor actor )
+{
+  FadeImageTo( actor, 1.f );
+}
+
+void PushButton::OnTransitionOut( Actor actor )
+{
+  FadeImageTo( actor, 0.0f );
+}
+
+void PushButton::FadeImageTo( Actor actor, float opacity )
+{
+  if( actor )
+  {
+    Dali::Animation transitionAnimation = GetTransitionAnimation();
+    DALI_ASSERT_DEBUG( transitionAnimation );
+
+    if( transitionAnimation )
     {
-      Vector3 imageSize = RelayoutHelper::GetNaturalSize( backgroundImageActor );
+      transitionAnimation.AnimateTo( Property( actor, Actor::Property::COLOR_ALPHA ), opacity );
+    }
+  }
+}
 
-      if( widthIsZero )
-      {
-        size.width = std::max( size.width, imageSize.width );
-      }
+Vector3 PushButton::GetNaturalSize()
+{
+  Vector3 size;
 
-      if( heightIsZero )
-      {
-        size.height = std::max( size.height, imageSize.height );
-      }
+  // If label, test against it's size
+  Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() );
+  if( label )
+  {
+    size.width  = std::max( size.width,  label.GetRelayoutSize( Dimension::WIDTH ) );
+    size.height = std::max( size.height, label.GetRelayoutSize( Dimension::HEIGHT ) );
+  }
+  else
+  {
+    // Check Image and Background image and use the largest size as the control's Natural size.
+    SizeOfActorIfLarger( GetButtonImage(), size );
+    SizeOfActorIfLarger( GetBackgroundImage(), size );
+  }
+
+  return size;
+}
+
+void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
+{
+  ConfigureSizeNegotiation();
+}
+
+void PushButton::ConfigureSizeNegotiation()
+{
+  std::vector< Actor > images;
+  images.reserve( 7 );
+
+  images.push_back( GetButtonImage() );
+  images.push_back( GetSelectedImage() );
+  images.push_back( GetSelectedBackgroundImage() );
+  images.push_back( GetBackgroundImage() );
+  images.push_back( GetDisabledImage() );
+  images.push_back( GetDisabledSelectedImage() );
+  images.push_back( GetDisabledBackgroundImage() );
+
+  Actor label = GetLabel();
+
+  for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
+  {
+    ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label );
+  }
+
+  if( label )
+  {
+    Padding padding;
+
+    if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::USE_NATURAL_SIZE )
+    {
+      padding.left = TEXT_PADDING;
+      padding.right = TEXT_PADDING;
     }
 
-    // If label, test against it's size
-    Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
-    if( textView )
+    if( label.GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::USE_NATURAL_SIZE )
     {
-      Vector3 textViewSize = textView.GetNaturalSize();
+      padding.top = TEXT_PADDING;
+      padding.bottom = TEXT_PADDING;
+    }
+
+    label.SetPadding( padding );
+  }
+}
+
+void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label )
+{
+  ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT;
+  ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT;
 
-      if( widthIsZero )
+  switch( Self().GetResizePolicy( dimension ) )
+  {
+    case ResizePolicy::FIT_TO_CHILDREN:
+    {
+      imageResizePolicy = labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
+      break;
+    }
+    case ResizePolicy::USE_NATURAL_SIZE:
+    {
+      if( label )
       {
-        size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
+        labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
       }
-
-      if( heightIsZero )
+      else
       {
-        size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
+        imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
       }
+      break;
+    }
+    default:
+    {
+      break;
     }
   }
 
-  return size;
+  if( label )
+  {
+    label.SetResizePolicy( labelResizePolicy, dimension );
+  }
+
+  for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it )
+  {
+    Actor actor = *it;
+    if( actor )
+    {
+      actor.SetResizePolicy( imageResizePolicy, dimension );
+    }
+  }
 }
 
 } // namespace Internal