Merge "Added TextLabel and TextField tests" into tizen
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
index c15c15e..88e5364 100644 (file)
 #include "check-box-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 "check-box-button-default-painter-impl.h"
-
 namespace Dali
 {
 
@@ -38,6 +34,9 @@ namespace Internal
 namespace
 {
 
+const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
+const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
+
 BaseHandle Create()
 {
   return Toolkit::CheckBoxButton::New();
@@ -45,19 +44,8 @@ BaseHandle Create()
 
 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
 
-TypeAction a1(mType, Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK, &CheckBoxButton::DoAction);
-
 }
 
-namespace
-{
-  // Helper function used to cast a ButtonPainterPtr to CheckBoxButtonDefaultPainterPtr
-  CheckBoxButtonDefaultPainterPtr GetCheckBoxButtonPainter( ButtonPainterPtr painter )
-  {
-    return static_cast<CheckBoxButtonDefaultPainter*>( painter.Get() );
-  }
-} // namespace
-
 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
 {
   // Create the implementation, temporarily owned on stack
@@ -73,193 +61,216 @@ Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
   return checkBoxButton;
 }
 
-void CheckBoxButton::SetChecked( bool checked )
+CheckBoxButton::CheckBoxButton()
+: Button()
 {
-  if( !mDisabled && ( checked != mChecked ) )
-  {
-    // Stores the state.
-    mChecked = checked;
+  SetTogglableButton( true );
 
-    Toolkit::CheckBoxButton handle( GetOwner() );
-
-    // Notifies the painter the checkbox has been checked.
-    GetCheckBoxButtonPainter( mPainter )->Checked( handle );
-
-    // Raise state changed signal
-    mStateChangedSignal.Emit( handle, mChecked );
-  }
+  SetAnimationTime( ANIMATION_TIME );
 }
 
-bool CheckBoxButton::IsChecked() const
+CheckBoxButton::~CheckBoxButton()
 {
-  return mChecked;
+  if( mTransitionAnimation )
+  {
+    mTransitionAnimation.Clear();
+  }
 }
 
-void CheckBoxButton::SetBackgroundImage( Image image )
+void CheckBoxButton::OnButtonInitialize()
 {
-  SetBackgroundImage( ImageActor::New( image ) );
+  // Wrap around all children
+  Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
 }
 
-void CheckBoxButton::SetBackgroundImage( Actor image )
+void CheckBoxButton::OnLabelSet()
 {
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetBackgroundImage( handle, image );
-}
+  Actor& label = GetLabel();
 
-Actor& CheckBoxButton::GetBackgroundImage()
-{
-  return mBackgroundImage;
+  if( label )
+  {
+    label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
+    label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
+
+    if( IsDisabled() && GetDisabledBackgroundImage() )
+    {
+      label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else if ( GetBackgroundImage() )
+    {
+      label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else
+    {
+      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+  }
 }
 
-Actor CheckBoxButton::GetBackgroundImage() const
+bool CheckBoxButton::OnSelected()
 {
-  return mBackgroundImage;
-}
+  Actor& selectedImage = GetSelectedImage();
 
-void CheckBoxButton::SetCheckedImage( Image image )
-{
-  SetCheckedImage( ImageActor::New( image ) );
-}
+  PaintState paintState = GetPaintState();
 
-void CheckBoxButton::SetCheckedImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetCheckedImage( handle, image );
-}
+  switch( paintState )
+  {
+    case UnselectedState:
+    {
+      StartTransitionAnimation( selectedImage );
+      break;
+    }
+    case SelectedState:
+    {
+      RemoveChild( selectedImage );
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      StopTransitionAnimation( false );
+      RemoveChild( selectedImage );
+      break;
+    }
+    default:
+    {
+      break;
+    }
+  }
 
-Actor& CheckBoxButton::GetCheckedImage()
-{
-  return mCheckedImage;
-}
+  if( mTransitionAnimation )
+  {
+    return true;
+  }
 
-Actor CheckBoxButton::GetCheckedImage() const
-{
-  return mCheckedImage;
+  return false;
 }
 
-void CheckBoxButton::SetDisabledBackgroundImage( Image image )
+bool CheckBoxButton::OnDisabled()
 {
-  SetDisabledBackgroundImage( ImageActor::New( image ) );
-}
+  Actor& backgroundImage = GetBackgroundImage();
+  Actor& selectedImage = GetSelectedImage();
+  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
+  Actor& disabledSelectedImage = GetDisabledSelectedImage();
 
-void CheckBoxButton::SetDisabledBackgroundImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetDisabledBackgroundImage( handle, image );
-}
+  PaintState paintState = GetPaintState();
 
-Actor& CheckBoxButton::GetDisabledBackgroundImage()
-{
-  return mDisabledBackgroundImage;
-}
+  switch( paintState )
+  {
+    case UnselectedState:
+    {
+      RemoveChild( backgroundImage );
+      break;
+    }
+    case SelectedState:
+    {
+      RemoveChild( backgroundImage );
+      RemoveChild( selectedImage );
+      break;
+    }
+    case DisabledUnselectedState:
+    {
+      RemoveChild( disabledBackgroundImage );
+      break;
+    }
+    case DisabledSelectedState:
+    {
+      RemoveChild( disabledBackgroundImage );
+      RemoveChild( disabledSelectedImage );
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      StopTransitionAnimation();
+
+      RemoveChild( backgroundImage );
+      RemoveChild( selectedImage );
+      break;
+    }
+    default:
+    {
+      break;
+    }
+  }
 
-Actor CheckBoxButton::GetDisabledBackgroundImage() const
-{
-  return mDisabledBackgroundImage;
-}
+  Actor& label = GetLabel();
 
-void CheckBoxButton::SetDisabledCheckedImage( Image image )
-{
-  SetDisabledCheckedImage( ImageActor::New( image ) );
-}
+  if( label )
+  {
+    if( IsDisabled() && disabledBackgroundImage)
+    {
+      label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else if( backgroundImage )
+    {
+      label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else
+    {
+      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+  }
 
-void CheckBoxButton::SetDisabledCheckedImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetDisabledCheckedImage( handle, image );
-}
+  if( mTransitionAnimation )
+  {
+    return true;
+  }
 
-Actor& CheckBoxButton::GetDisabledCheckedImage()
-{
-  return mDisabledCheckedImage;
+  return false;
 }
 
-Actor CheckBoxButton::GetDisabledCheckedImage() const
+void CheckBoxButton::StopAllAnimations()
 {
-  return mDisabledCheckedImage;
+  StopTransitionAnimation();
 }
 
-Actor& CheckBoxButton::GetFadeOutBackgroundImage()
+void CheckBoxButton::StartTransitionAnimation( Actor& actor )
 {
-  return mFadeOutBackgroundImage;
-}
+  if( actor )
+  {
+    if( !mTickUVEffect )
+    {
+      ImageActor imageActor = ImageActor::DownCast( actor );
+      mTickUVEffect = ImageRegionEffect::New();
+      imageActor.SetShaderEffect( mTickUVEffect );
+    }
 
-Actor& CheckBoxButton::GetFadeOutCheckedImage()
-{
-  return mFadeOutCheckedImage;
-}
+    actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
 
-void CheckBoxButton::OnButtonInitialize()
-{
-}
+    mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
 
-void CheckBoxButton::OnButtonUp()
-{
-  if( ButtonDown == mState )
-  {
-    // Stores the state, notifies the painter and emits a signal.
-    SetChecked( !mChecked );
-  }
-}
+    if( !mTransitionAnimation )
+    {
+      mTransitionAnimation = Dali::Animation::New( GetAnimationTime()  );
+    }
 
-void CheckBoxButton::OnAnimationTimeSet( float animationTime )
-{
-  GetCheckBoxButtonPainter( mPainter )->SetAnimationTime( animationTime );
-}
+    // UV anim
+    mTransitionAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
 
-float CheckBoxButton::OnAnimationTimeRequested() const
-{
-  return GetCheckBoxButtonPainter( mPainter )->GetAnimationTime();
-}
+    // Actor size anim
+    mTransitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
 
-void CheckBoxButton::OnActivated()
-{
-  // When the button is activated, it performs the click action
-  PropertyValueContainer attributes;
-  DoClickAction(attributes);
+    mTransitionAnimation.FinishedSignal().Connect( this, &CheckBoxButton::TransitionAnimationFinished );
+    mTransitionAnimation.Play();
+  }
 }
 
-void CheckBoxButton::DoClickAction(const PropertyValueContainer& attributes)
+void CheckBoxButton::StopTransitionAnimation( bool remove )
 {
-  // Prevents the button signals from doing a recursive loop by sending an action
-  // and re-emitting the signals.
-  if(!mClickActionPerforming)
+  if( mTransitionAnimation )
   {
-    mClickActionPerforming = true;
-    SetChecked( !mChecked );
-    mClickActionPerforming = false;
+    mTransitionAnimation.Clear();
+    mTransitionAnimation.Reset();
   }
-}
 
-bool CheckBoxButton::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
-{
-  bool ret = false;
-
-  Dali::BaseHandle handle(object);
-
-  Toolkit::CheckBoxButton button = Toolkit::CheckBoxButton::DownCast(handle);
-
-  if(Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK == actionName)
+  if( remove )
   {
-    GetImplementation(button).DoClickAction(attributes);
-    ret = true;
+    UpdatePaintTransitionState();
   }
-
-  return ret;
 }
 
-CheckBoxButton::CheckBoxButton()
-: Button(),
-  mChecked( false ),
-  mClickActionPerforming(false)
-{
-  // Creates specific painter.
-  mPainter = new CheckBoxButtonDefaultPainter();
-}
-
-CheckBoxButton::~CheckBoxButton()
+void CheckBoxButton::TransitionAnimationFinished( Dali::Animation& source )
 {
-  mPainter = NULL;
+  StopTransitionAnimation();
 }
 
 } // namespace Internal