Removed OnButton virtual functions and simplified RadioButton logic
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-button-impl.cpp
index a2cb95d..4e96f14 100644 (file)
 #include "push-button-impl.h"
 
 // EXTERNAL INCLUDES
-#include <dali/public-api/actors/image-actor.h>
 #include <dali/public-api/object/type-registry.h>
+#include <dali/public-api/object/type-registry-helper.h>
+#include <dali/public-api/images/resource-image.h>
+#include <dali/devel-api/scripting/scripting.h>
+#include <dali/integration-api/debug.h>
 
 // INTERNAL INCLUDES
-#include <dali-toolkit/public-api/controls/text-view/text-view.h>
+#include <dali-toolkit/public-api/controls/image-view/image-view.h>
+#include <dali-toolkit/public-api/controls/text-controls/text-label.h>
+#include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
+#include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
+
+#if defined(DEBUG_ENABLED)
+  extern Debug::Filter* gLogButtonFilter;
+#endif
 
 namespace Dali
 {
@@ -37,41 +47,37 @@ namespace Internal
 namespace
 {
 
-const float TEXT_PADDING = 12.0f;
-const float ANIMATION_TIME( 0.2f );
-
 BaseHandle Create()
 {
   return Toolkit::PushButton::New();
 }
 
-TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
+// Properties
 
-} // unnamed namespace
+DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create )
 
-namespace
-{
+DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "unselectedIcon",  STRING, UNSELECTED_ICON )
+DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "selectedIcon",  STRING, SELECTED_ICON )
+DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconAlignment",  STRING, ICON_ALIGNMENT )
+DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "labelPadding",  STRING, LABEL_PADDING )
+DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconPadding",  STRING, ICON_PADDING )
+
+DALI_TYPE_REGISTRATION_END()
 
-/**
- * Find the first image actor in the actor hierarchy
+/*
+ * Table to define Text-to-enum conversions for IconAlignment.
  */
-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;
-      }
-    }
-  }
+const Dali::Scripting::StringEnum IconAlignmentTable[] = {
+  { "LEFT",   Toolkit::Internal::PushButton::LEFT },
+  { "RIGHT",  Toolkit::Internal::PushButton::RIGHT },
+  { "TOP",    Toolkit::Internal::PushButton::TOP },
+  { "BOTTOM", Toolkit::Internal::PushButton::BOTTOM },
+}; const unsigned int IconAlignmentTableCount = sizeof( IconAlignmentTable ) / sizeof( IconAlignmentTable[0] );
 
-  return imageActor;
-}
+} // unnamed namespace
+
+namespace
+{
 
 } // unnamed namespace
 
@@ -92,580 +98,148 @@ Dali::Toolkit::PushButton PushButton::New()
 
 PushButton::PushButton()
 : Button(),
-  mSize()
+  mIconAlignment( RIGHT )
 {
-  SetAnimationTime( ANIMATION_TIME );
 }
 
 PushButton::~PushButton()
 {
 }
 
-void PushButton::OnButtonInitialize()
+void PushButton::OnInitialize()
 {
+  Button::OnInitialize();
+
   // Push button requires the Leave event.
   Actor self = Self();
   self.SetLeaveRequired( true );
-
-  // Set resize policy to natural size so that buttons will resize to background images
-  self.SetResizePolicy( USE_NATURAL_SIZE, ALL_DIMENSIONS );
 }
 
-void PushButton::OnLabelSet()
+void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment )
 {
-  Actor& label = GetLabel();
-
-  if( label )
+  mIconAlignment = iconAlignment;
+  Button::Align labelAlignment;
+  switch ( iconAlignment )
+  {
+  case RIGHT:
   {
-    label.SetAnchorPoint( AnchorPoint::CENTER );
-    label.SetParentOrigin( ParentOrigin::CENTER );
-    label.SetSize( mSize );
+    labelAlignment = Button::BEGIN;
+    break;
   }
-}
-
-void PushButton::OnButtonImageSet()
-{
-  Actor& buttonImage = GetButtonImage();
-
-  buttonImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
-
-  buttonImage.RelayoutRequestTree();
-
-  RelayoutRequest();
-}
-
-void PushButton::OnSelectedImageSet()
-{
-  Actor& selectedImage = GetSelectedImage();
-
-  selectedImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
-
-  selectedImage.RelayoutRequestTree();
-
-  RelayoutRequest();
-}
-
-void PushButton::OnBackgroundImageSet()
-{
-  Actor& backgroundImage = GetBackgroundImage();
-
-  backgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
-
-  backgroundImage.RelayoutRequestTree();
-
-  RelayoutRequest();
-}
-
-void PushButton::OnSelectedBackgroundImageSet()
-{
-  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
-
-  selectedBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
-}
-
-void PushButton::OnDisabledImageSet()
-{
-  Actor& disabledImage = GetDisabledImage();
-
-  disabledImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
-
-  disabledImage.RelayoutRequestTree();
-
-  RelayoutRequest();
-}
-
-void PushButton::OnDisabledBackgroundImageSet()
-{
-  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
-
-  disabledBackgroundImage.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
-
-  disabledBackgroundImage.RelayoutRequestTree();
-
-  RelayoutRequest();
-}
-
-bool PushButton::OnSelected()
-{
-  Actor& buttonImage = GetButtonImage();
-  Actor& selectedImage = GetSelectedImage();
-  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
-
-  PaintState paintState = GetPaintState();
-
-  switch( paintState )
+  case TOP:
   {
-    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:
-    {
-      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;
-    }
+    labelAlignment = Button::BOTTOM;
+    break;
   }
-
-  if( mTransitionAnimation )
+  case BOTTOM:
   {
-    return true;
+    labelAlignment = Button::TOP;
+    break;
+  }
+  case LEFT:
+  default:
+    labelAlignment = Button::END;
+    break;
   }
 
-  return false;
+  Button::SetLabelAlignment( labelAlignment );
 }
 
-bool PushButton::OnDisabled()
+const PushButton::IconAlignment PushButton::GetIconAlignment() const
 {
-  Actor& buttonImage = GetButtonImage();
-  Actor& selectedImage = GetSelectedImage();
-  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
-  Actor& backgroundImage = GetBackgroundImage();
-  Actor& disabledImage = GetDisabledImage();
-  Actor& disabledSelectedImage = GetDisabledSelectedImage();
-  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
+  return mIconAlignment;
+}
 
-  PaintState paintState = GetPaintState();
+void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
+{
+  Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
 
-  switch( paintState )
+  if ( pushButton )
   {
-    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 )
-      {
-        opacity = selectedImage.GetCurrentOpacity();
-      }
+    PushButton& pushButtonImpl( GetImplementation( pushButton ) );
 
-      StopTransitionAnimation();
-      FadeOutImage( backgroundImage );
-      FadeOutImage( selectedBackgroundImage, opacity );
-      FadeOutImage( selectedImage, opacity );
-      FadeInImage( disabledBackgroundImage );
-      FadeInImage( disabledSelectedImage );
-      StartTransitionAnimation();
-      break;
-    }
-    case SelectedUnselectedTransition:
+    // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class
+
+    switch ( propertyIndex )
     {
-      float opacity = 1.f;
-      if( buttonImage )
+      case Toolkit::PushButton::Property::UNSELECTED_ICON:
       {
-        opacity = buttonImage.GetCurrentOpacity();
+        pushButtonImpl.CreateVisualsForComponent( Toolkit::Button::Property::UNSELECTED_VISUAL, value, DepthIndex::CONTENT );
+        break;
       }
-
-      StopTransitionAnimation();
-      FadeOutImage( backgroundImage );
-      FadeOutImage( buttonImage, opacity );
-      FadeInImage( disabledBackgroundImage );
-      FadeInImage( disabledImage );
-      StartTransitionAnimation();
-      break;
-    }
-    case UnselectedDisabledTransition:
-    {
-      float opacity = 1.f;
-      if( disabledImage )
+      case Toolkit::PushButton::Property::SELECTED_ICON:
       {
-        opacity = disabledImage.GetCurrentOpacity();
+        pushButtonImpl.CreateVisualsForComponent( Toolkit::Button::Property::SELECTED_VISUAL, value, DepthIndex::CONTENT );
+        break;
       }
-
-      StopTransitionAnimation( false );
-      FadeOutImage( disabledBackgroundImage, opacity );
-      FadeOutImage( disabledImage, opacity );
-      FadeInImage( backgroundImage, 1.f - opacity );
-      FadeInImage( buttonImage, 1.f - opacity );
-      StartTransitionAnimation();
-      break;
-    }
-    case DisabledUnselectedTransition:
-    {
-      float opacity = 1.f;
-      if( buttonImage )
+      case Toolkit::PushButton::Property::ICON_ALIGNMENT:
       {
-        opacity = buttonImage.GetCurrentOpacity();
+        IconAlignment iconAlignment;
+        if( Scripting::GetEnumeration< IconAlignment >( value.Get< std::string >().c_str(), IconAlignmentTable, IconAlignmentTableCount, iconAlignment ) )
+        {
+          pushButtonImpl.SetIconAlignment( iconAlignment );
+        }
+        break;
       }
-
-      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 )
+      case Toolkit::PushButton::Property::LABEL_PADDING:
       {
-        opacity = disabledSelectedImage.GetCurrentOpacity();
+        Vector4 padding ( value.Get< Vector4 >() );
+        pushButtonImpl.Button::SetLabelPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
+        break;
       }
-
-      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 )
+      case Toolkit::PushButton::Property::ICON_PADDING:
       {
-        opacity = selectedImage.GetCurrentOpacity();
+        Vector4 padding ( value.Get< Vector4 >() );
+        pushButtonImpl.Button::SetForegroundPadding( Padding( padding.x, padding.y, padding.z, padding.w ) );
+        break;
       }
-
-      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;
-  }
-
-  return false;
 }
 
-bool PushButton::OnPressed()
+Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
 {
-  Actor& buttonImage = GetButtonImage();
-  Actor& selectedImage = GetSelectedImage();
-  Actor& selectedBackgroundImage = GetSelectedBackgroundImage();
+  Property::Value value;
 
-  PaintState paintState = GetPaintState();
+  Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
 
-  switch( paintState )
+  if ( pushButton )
   {
-    case UnselectedState:
-    {
-      FadeOutImage( buttonImage );
-      FadeInImage( selectedBackgroundImage );
-      FadeInImage( selectedImage );
-      StartTransitionAnimation();
-      break;
-    }
-    case SelectedUnselectedTransition:
+    PushButton& pushButtonImpl( GetImplementation( pushButton ) );
+
+    switch ( propertyIndex )
     {
-      float opacity = 1.f;
-      if( buttonImage )
+      case Toolkit::PushButton::Property::UNSELECTED_ICON:
       {
-        opacity = buttonImage.GetCurrentOpacity();
+        //value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION );
+        break;
       }
-
-      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 )
+      case Toolkit::PushButton::Property::SELECTED_ICON:
       {
-        opacity = buttonImage.GetCurrentOpacity();
+        //value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION );
+        break;
       }
-
-      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 )
+      case Toolkit::PushButton::Property::ICON_ALIGNMENT:
       {
-        opacity = selectedImage.GetCurrentOpacity();
+        value = Scripting::GetLinearEnumerationName< IconAlignment >( pushButtonImpl.GetIconAlignment(), IconAlignmentTable, IconAlignmentTableCount );
+        break;
       }
-
-      StopTransitionAnimation( false );
-      FadeOutImage( selectedBackgroundImage, opacity );
-      FadeOutImage( selectedImage, opacity );
-      FadeInImage( buttonImage, 1.f - opacity );
-      StartTransitionAnimation();
-      break;
-    }
-    case DisabledSelectedTransition:
-    {
-      float opacity = 1.f;
-      if( selectedImage )
+      case Toolkit::PushButton::Property::LABEL_PADDING:
       {
-        opacity = selectedImage.GetCurrentOpacity();
+        Padding padding = pushButtonImpl.Button::GetLabelPadding();
+        value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
+        break;
+      }
+      case Toolkit::PushButton::Property::ICON_PADDING:
+      {
+        Padding padding = pushButtonImpl.Button::GetForegroundPadding();
+        value = Vector4( padding.x, padding.y, padding.top, padding.bottom);
+        break;
       }
-
-      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;
-
-  // If background and background not scale9 try get size from that
-  ImageActor imageActor = FindImageActor( GetButtonImage() );
-  if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
-  {
-    size.width = imageActor.GetRelayoutSize( WIDTH );
-    size.height = imageActor.GetRelayoutSize( HEIGHT );
-  }
-
-  ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
-  if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
-  {
-    size.width = std::max( size.width, backgroundImageActor.GetRelayoutSize( WIDTH ) );
-    size.height = std::max( size.height, backgroundImageActor.GetRelayoutSize( HEIGHT ) );
-  }
-
-  // If label, test against it's size
-  Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
-  if( textView )
-  {
-    Vector3 textViewSize = textView.GetNaturalSize();
-
-    size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
-    size.height = std::max( size.height, textViewSize.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();
+  return value;
 }
 
 } // namespace Internal