Size negotiation patch 3: Scope size negotiation enums
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-button-impl.cpp
index 0df4e3c..cf34f85 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>
 
 // 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 +37,9 @@ namespace Internal
 namespace
 {
 
+const float TEXT_PADDING = 12.0f;
+const float ANIMATION_TIME( 0.2f );
+
 BaseHandle Create()
 {
   return Toolkit::PushButton::New();
@@ -53,30 +52,21 @@ TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::
 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,89 +84,581 @@ 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 );
 }
 
-PushButton::PushButton()
-: Button()
+void PushButton::OnLabelSet()
 {
-  // Creates specific painter.GetBu
-  ButtonPainterPtr painter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() );
-  SetPainter( painter );
+  Actor& label = GetLabel();
+
+  if( label )
+  {
+    label.SetAnchorPoint( AnchorPoint::CENTER );
+    label.SetParentOrigin( ParentOrigin::CENTER );
+    label.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+
+    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 );
+    }
+  }
 }
 
-PushButton::~PushButton()
+void PushButton::OnButtonImageSet()
 {
-  SetPainter( NULL );
+  Actor& buttonImage = GetButtonImage();
+
+  buttonImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+
+  buttonImage.RelayoutRequestTree();
+
+  RelayoutRequest();
 }
 
-Vector3 PushButton::GetNaturalSize()
+void PushButton::OnSelectedImageSet()
+{
+  Actor& selectedImage = GetSelectedImage();
+
+  selectedImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+
+  selectedImage.RelayoutRequestTree();
+
+  RelayoutRequest();
+}
+
+void PushButton::OnBackgroundImageSet()
+{
+  Actor& backgroundImage = GetBackgroundImage();
+
+  backgroundImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+
+  backgroundImage.RelayoutRequestTree();
+
+  RelayoutRequest();
+}
+
+void PushButton::OnSelectedBackgroundImageSet()
 {
-  Vector3 size = Control::GetNaturalSize();
+  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
 
-  const bool widthIsZero = EqualsZero( size.width );
-  const bool heightIsZero = EqualsZero( size.height );
+  selectedBackgroundImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+}
+
+void PushButton::OnDisabledImageSet()
+{
+  Actor& disabledImage = GetDisabledImage();
+
+  disabledImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+
+  disabledImage.RelayoutRequestTree();
+
+  RelayoutRequest();
+}
+
+void PushButton::OnDisabledBackgroundImageSet()
+{
+  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
 
-  if( widthIsZero || heightIsZero )
+  disabledBackgroundImage.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+
+  disabledBackgroundImage.RelayoutRequestTree();
+
+  RelayoutRequest();
+}
+
+bool PushButton::OnSelected()
+{
+  Actor& buttonImage = GetButtonImage();
+  Actor& selectedImage = GetSelectedImage();
+  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
+
+  PaintState paintState = GetPaintState();
+
+  switch( paintState )
   {
-    // If background and background not scale9 try get size from that
-    ImageActor imageActor = FindImageActor( GetButtonImage() );
-    if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
+    case UnselectedState:
+    {
+      FadeOutImage( buttonImage );
+      FadeInImage( selectedBackgroundImage );
+      FadeInImage( selectedImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case SelectedState:
+    {
+      FadeOutImage( selectedBackgroundImage );
+      FadeOutImage( selectedImage );
+      FadeInImage( buttonImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      float opacity = 1.f;
+      if( selectedImage )
+      {
+        opacity = selectedImage.GetCurrentOpacity();
+      }
+
+      StopTransitionAnimation( false );
+      FadeOutImage( selectedBackgroundImage, opacity );
+      FadeOutImage( selectedImage, opacity );
+      FadeInImage( buttonImage, 1.f - opacity );
+      StartTransitionAnimation();
+      break;
+    }
+    case SelectedUnselectedTransition:
     {
-      Vector3 imageSize = RelayoutHelper::GetNaturalSize( imageActor );
+      float opacity = 0.f;
+      if( selectedImage )
+      {
+        opacity = selectedImage.GetCurrentOpacity();
+      }
+
+      StopTransitionAnimation( false );
+      FadeOutImage( buttonImage, 1.f - opacity );
+      FadeInImage( selectedBackgroundImage, opacity );
+      FadeInImage( selectedImage, opacity );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledUnselectedTransition:
+    {
+      StopTransitionAnimation();
+      FadeOutImage( buttonImage );
+      FadeInImage( selectedBackgroundImage );
+      FadeInImage( selectedImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledSelectedTransition:
+    {
+      StopTransitionAnimation();
+      FadeOutImage( selectedBackgroundImage );
+      FadeOutImage( selectedImage );
+      FadeInImage( buttonImage );
+      StartTransitionAnimation();
+      break;
+    }
+    default:
+    {
+      break;
+    }
+  }
+
+  if( mTransitionAnimation )
+  {
+    return true;
+  }
 
-      if( widthIsZero )
+  return false;
+}
+
+bool PushButton::OnDisabled()
+{
+  Actor& buttonImage = GetButtonImage();
+  Actor& selectedImage = GetSelectedImage();
+  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
+  Actor& backgroundImage = GetBackgroundImage();
+  Actor& disabledImage = GetDisabledImage();
+  Actor& disabledSelectedImage = GetDisabledSelectedImage();
+  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
+
+  PaintState paintState = GetPaintState();
+
+  switch( paintState )
+  {
+    case UnselectedState:
+    {
+      FadeOutImage( backgroundImage );
+      FadeOutImage( buttonImage );
+      FadeInImage( disabledBackgroundImage );
+      FadeInImage( disabledImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case SelectedState:
+    {
+      FadeOutImage( backgroundImage );
+      FadeOutImage( selectedBackgroundImage );
+      FadeOutImage( selectedImage );
+      FadeInImage( disabledBackgroundImage );
+      FadeInImage( disabledSelectedImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledUnselectedState:
+    {
+      FadeOutImage( disabledBackgroundImage );
+      FadeOutImage( disabledImage );
+      FadeInImage( backgroundImage );
+      FadeInImage( buttonImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledSelectedState:
+    {
+      FadeOutImage( disabledBackgroundImage );
+      FadeOutImage( disabledSelectedImage );
+      FadeInImage( backgroundImage );
+      FadeInImage( selectedBackgroundImage );
+      FadeInImage( selectedImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      float opacity = 1.f;
+      if( selectedImage )
       {
-        size.width = imageSize.width;
+        opacity = selectedImage.GetCurrentOpacity();
       }
 
-      if( heightIsZero )
+      StopTransitionAnimation();
+      FadeOutImage( backgroundImage );
+      FadeOutImage( selectedBackgroundImage, opacity );
+      FadeOutImage( selectedImage, opacity );
+      FadeInImage( disabledBackgroundImage );
+      FadeInImage( disabledSelectedImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case SelectedUnselectedTransition:
+    {
+      float opacity = 1.f;
+      if( buttonImage )
       {
-        size.height = imageSize.height;
+        opacity = buttonImage.GetCurrentOpacity();
       }
+
+      StopTransitionAnimation();
+      FadeOutImage( backgroundImage );
+      FadeOutImage( buttonImage, opacity );
+      FadeInImage( disabledBackgroundImage );
+      FadeInImage( disabledImage );
+      StartTransitionAnimation();
+      break;
     }
+    case UnselectedDisabledTransition:
+    {
+      float opacity = 1.f;
+      if( disabledImage )
+      {
+        opacity = disabledImage.GetCurrentOpacity();
+      }
 
-    ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
-    if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
+      StopTransitionAnimation( false );
+      FadeOutImage( disabledBackgroundImage, opacity );
+      FadeOutImage( disabledImage, opacity );
+      FadeInImage( backgroundImage, 1.f - opacity );
+      FadeInImage( buttonImage, 1.f - opacity );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledUnselectedTransition:
     {
-      Vector3 imageSize = RelayoutHelper::GetNaturalSize( backgroundImageActor );
+      float opacity = 1.f;
+      if( buttonImage )
+      {
+        opacity = buttonImage.GetCurrentOpacity();
+      }
 
-      if( widthIsZero )
+      StopTransitionAnimation( false );
+      FadeOutImage( backgroundImage, opacity );
+      FadeOutImage( buttonImage, opacity );
+      FadeInImage( disabledBackgroundImage, 1.f - opacity );
+      FadeInImage( disabledImage, 1.f - opacity );
+      StartTransitionAnimation();
+      break;
+    }
+    case SelectedDisabledTransition:
+    {
+      float opacity = 1.f;
+      if( disabledSelectedImage )
       {
-        size.width = std::max( size.width, imageSize.width );
+        opacity = disabledSelectedImage.GetCurrentOpacity();
       }
 
-      if( heightIsZero )
+      StopTransitionAnimation( false );
+      FadeOutImage( disabledBackgroundImage, opacity );
+      FadeOutImage( disabledSelectedImage, opacity );
+      FadeInImage( backgroundImage, 1.f - opacity );
+      FadeInImage( selectedBackgroundImage, 1.f - opacity );
+      FadeInImage( selectedImage, 1.f - opacity );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledSelectedTransition:
+    {
+      float opacity = 1.f;
+      if( selectedImage )
       {
-        size.height = std::max( size.height, imageSize.height );
+        opacity = selectedImage.GetCurrentOpacity();
       }
+
+      StopTransitionAnimation( false );
+      FadeOutImage( backgroundImage, opacity );
+      FadeOutImage( selectedBackgroundImage, opacity );
+      FadeOutImage( selectedImage, opacity );
+      FadeInImage( disabledBackgroundImage, 1.f - opacity );
+      FadeInImage( disabledSelectedImage, 1.f - opacity );
+      StartTransitionAnimation();
+      break;
     }
+  }
+
+  if( mTransitionAnimation )
+  {
+    return true;
+  }
 
-    // If label, test against it's size
-    Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
-    if( textView )
+  return false;
+}
+
+bool PushButton::OnPressed()
+{
+  Actor& buttonImage = GetButtonImage();
+  Actor& selectedImage = GetSelectedImage();
+  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
+
+  PaintState paintState = GetPaintState();
+
+  switch( paintState )
+  {
+    case UnselectedState:
     {
-      Vector3 textViewSize = textView.GetNaturalSize();
+      FadeOutImage( buttonImage );
+      FadeInImage( selectedBackgroundImage );
+      FadeInImage( selectedImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case SelectedUnselectedTransition:
+    {
+      float opacity = 1.f;
+      if( buttonImage )
+      {
+        opacity = buttonImage.GetCurrentOpacity();
+      }
 
-      if( widthIsZero )
+      StopTransitionAnimation( false );
+      FadeOutImage( buttonImage, opacity );
+      FadeInImage( selectedBackgroundImage, 1.f - opacity );
+      FadeInImage( selectedImage, 1.f - opacity );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledUnselectedTransition:
+    {
+      float opacity = 1.f;
+      if( buttonImage )
       {
-        size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
+        opacity = buttonImage.GetCurrentOpacity();
       }
 
-      if( heightIsZero )
+      StopTransitionAnimation();
+      FadeOutImage( buttonImage, opacity );
+      FadeInImage( selectedBackgroundImage );
+      FadeInImage( selectedImage );
+      StartTransitionAnimation();
+      break;
+    }
+    default:
+      break;
+  }
+
+  if( mTransitionAnimation )
+  {
+    return true;
+  }
+
+  return false;
+}
+
+bool PushButton::OnReleased()
+{
+  Actor& buttonImage = GetButtonImage();
+  Actor& selectedImage = GetSelectedImage();
+  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
+
+  PaintState paintState = GetPaintState();
+
+  switch( paintState )
+  {
+    case SelectedState:
+    {
+      FadeOutImage( selectedBackgroundImage );
+      FadeOutImage( selectedImage );
+      FadeInImage( buttonImage );
+      StartTransitionAnimation();
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      float opacity = 1.f;
+      if( selectedImage )
+      {
+        opacity = selectedImage.GetCurrentOpacity();
+      }
+
+      StopTransitionAnimation( false );
+      FadeOutImage( selectedBackgroundImage, opacity );
+      FadeOutImage( selectedImage, opacity );
+      FadeInImage( buttonImage, 1.f - opacity );
+      StartTransitionAnimation();
+      break;
+    }
+    case DisabledSelectedTransition:
+    {
+      float opacity = 1.f;
+      if( selectedImage )
       {
-        size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
+        opacity = selectedImage.GetCurrentOpacity();
       }
+
+      StopTransitionAnimation();
+      FadeOutImage( selectedBackgroundImage, opacity );
+      FadeOutImage( selectedImage, opacity );
+      FadeInImage( buttonImage );
+      StartTransitionAnimation();
+      break;
+    }
+    default:
+    {
+      break;
     }
   }
 
+  if( mTransitionAnimation )
+  {
+    return true;
+  }
+
+  return false;
+}
+
+void PushButton::StopAllAnimations()
+{
+  StopTransitionAnimation();
+}
+
+void PushButton::OnControlSizeSet( const Vector3& targetSize )
+{
+  if( targetSize != mSize )
+  {
+    mSize = targetSize;
+
+    Actor& label = GetLabel();
+
+    if( label )
+    {
+      label.SetSize( mSize );
+    }
+  }
+}
+
+Vector3 PushButton::GetNaturalSize()
+{
+  Vector3 size;
+
+  // Check Image and Background image and use the largest size as the control's Natural size.
+  SizeOfActorIfLarger( GetButtonImage(), size );
+  SizeOfActorIfLarger( GetBackgroundImage(), size );
+
+  // If label, test against it's size
+  Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() );
+  if( label )
+  {
+    Vector3 labelSize = label.GetNaturalSize();
+
+    size.width  = std::max( size.width,  labelSize.width  + TEXT_PADDING * 2.0f );
+    size.height = std::max( size.height, labelSize.height + TEXT_PADDING * 2.0f );
+  }
+
   return size;
 }
 
+void PushButton::StartTransitionAnimation()
+{
+  if( mTransitionAnimation )
+  {
+    mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished );
+    mTransitionAnimation.Play();
+  }
+}
+
+void PushButton::StopTransitionAnimation( bool remove )
+{
+  if( mTransitionAnimation )
+  {
+    mTransitionAnimation.Clear();
+    mTransitionAnimation.Reset();
+  }
+
+  if( remove )
+  {
+    UpdatePaintTransitionState();
+  }
+}
+
+void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale )
+{
+  if( image )
+  {
+    image.SetOpacity( opacity );
+    image.SetScale( scale );
+
+    if( !mTransitionAnimation )
+    {
+      mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
+    }
+
+    mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f );
+  }
+}
+
+void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale )
+{
+  if( image )
+  {
+    image.SetOpacity( opacity );
+    image.SetScale( scale );
+
+    if( !mTransitionAnimation )
+    {
+      mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
+    }
+
+    mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f );
+  }
+}
+
+void PushButton::TransitionAnimationFinished( Dali::Animation& source )
+{
+  StopTransitionAnimation();
+}
+
 } // namespace Internal
 
 } // namespace Toolkit