Refactored Button and derived classes, moving state change and transition logic to...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
index c15c15e..b1f7474 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>
+#include <dali/public-api/images/resource-image.h>
 
-// INTERNAL INCLUDES
-#include "check-box-button-default-painter-impl.h"
+//INTERNAL INCLUDES
+#include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
 
 namespace Dali
 {
@@ -38,6 +38,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 +48,12 @@ BaseHandle Create()
 
 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
 
-TypeAction a1(mType, Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK, &CheckBoxButton::DoAction);
-
+const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected.png";
+const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected.png";
+const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected-disabled.png";
+const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected-diabled.png";
 }
 
-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 +69,169 @@ Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
   return checkBoxButton;
 }
 
-void CheckBoxButton::SetChecked( bool checked )
-{
-  if( !mDisabled && ( checked != mChecked ) )
-  {
-    // Stores the state.
-    mChecked = checked;
-
-    Toolkit::CheckBoxButton handle( GetOwner() );
-
-    // Notifies the painter the checkbox has been checked.
-    GetCheckBoxButtonPainter( mPainter )->Checked( handle );
-
-    // Raise state changed signal
-    mStateChangedSignal.Emit( handle, mChecked );
-  }
-}
-
-bool CheckBoxButton::IsChecked() const
-{
-  return mChecked;
-}
-
-void CheckBoxButton::SetBackgroundImage( Image image )
-{
-  SetBackgroundImage( ImageActor::New( image ) );
-}
-
-void CheckBoxButton::SetBackgroundImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetBackgroundImage( handle, image );
-}
-
-Actor& CheckBoxButton::GetBackgroundImage()
-{
-  return mBackgroundImage;
-}
-
-Actor CheckBoxButton::GetBackgroundImage() const
-{
-  return mBackgroundImage;
-}
-
-void CheckBoxButton::SetCheckedImage( Image image )
-{
-  SetCheckedImage( ImageActor::New( image ) );
-}
-
-void CheckBoxButton::SetCheckedImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetCheckedImage( handle, image );
-}
-
-Actor& CheckBoxButton::GetCheckedImage()
-{
-  return mCheckedImage;
-}
-
-Actor CheckBoxButton::GetCheckedImage() const
-{
-  return mCheckedImage;
-}
-
-void CheckBoxButton::SetDisabledBackgroundImage( Image image )
-{
-  SetDisabledBackgroundImage( ImageActor::New( image ) );
-}
-
-void CheckBoxButton::SetDisabledBackgroundImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetDisabledBackgroundImage( handle, image );
-}
-
-Actor& CheckBoxButton::GetDisabledBackgroundImage()
-{
-  return mDisabledBackgroundImage;
-}
-
-Actor CheckBoxButton::GetDisabledBackgroundImage() const
-{
-  return mDisabledBackgroundImage;
-}
-
-void CheckBoxButton::SetDisabledCheckedImage( Image image )
+CheckBoxButton::CheckBoxButton()
+: Button()
 {
-  SetDisabledCheckedImage( ImageActor::New( image ) );
-}
+  SetTogglableButton( true );
 
-void CheckBoxButton::SetDisabledCheckedImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetDisabledCheckedImage( handle, image );
+  SetAnimationTime( ANIMATION_TIME );
 }
 
-Actor& CheckBoxButton::GetDisabledCheckedImage()
+CheckBoxButton::~CheckBoxButton()
 {
-  return mDisabledCheckedImage;
 }
 
-Actor CheckBoxButton::GetDisabledCheckedImage() const
+void CheckBoxButton::OnButtonInitialize()
 {
-  return mDisabledCheckedImage;
-}
+  // Wrap around all children
+  Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
 
-Actor& CheckBoxButton::GetFadeOutBackgroundImage()
-{
-  return mFadeOutBackgroundImage;
-}
+  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 );
 
-Actor& CheckBoxButton::GetFadeOutCheckedImage()
-{
-  return mFadeOutCheckedImage;
+  SetButtonImage( ImageActor::New( buttonImage ) );
+  SetSelectedImage( ImageActor::New( selectedImage ) );
+  SetDisabledImage( ImageActor::New( disabledImage ) );
+  SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
 }
 
-void CheckBoxButton::OnButtonInitialize()
+void CheckBoxButton::OnLabelSet()
 {
-}
+  Actor& label = GetLabel();
 
-void CheckBoxButton::OnButtonUp()
-{
-  if( ButtonDown == mState )
+  if( label )
   {
-    // Stores the state, notifies the painter and emits a signal.
-    SetChecked( !mChecked );
+    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 if( IsSelected() && GetSelectedImage())
+    {
+      label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else if( GetButtonImage() )
+    {
+      label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else
+    {
+      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
   }
 }
 
-void CheckBoxButton::OnAnimationTimeSet( float animationTime )
+void CheckBoxButton::OnDisabled()
 {
-  GetCheckBoxButtonPainter( mPainter )->SetAnimationTime( animationTime );
-}
+  Actor& backgroundImage = GetBackgroundImage();
+  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
 
-float CheckBoxButton::OnAnimationTimeRequested() const
-{
-  return GetCheckBoxButtonPainter( mPainter )->GetAnimationTime();
-}
-
-void CheckBoxButton::OnActivated()
-{
-  // When the button is activated, it performs the click action
-  PropertyValueContainer attributes;
-  DoClickAction(attributes);
-}
-
-void CheckBoxButton::DoClickAction(const PropertyValueContainer& attributes)
-{
-  // Prevents the button signals from doing a recursive loop by sending an action
-  // and re-emitting the signals.
-  if(!mClickActionPerforming)
+  Actor& label = GetLabel();
+  if( label )
   {
-    mClickActionPerforming = true;
-    SetChecked( !mChecked );
-    mClickActionPerforming = false;
+    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 if( IsSelected() && GetSelectedImage())
+    {
+      label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else if( GetButtonImage() )
+    {
+      label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
+    else
+    {
+      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+    }
   }
 }
 
-bool CheckBoxButton::DoAction(BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes)
+void CheckBoxButton::PrepareForTranstionIn( Actor actor )
 {
-  bool ret = false;
-
-  Dali::BaseHandle handle(object);
-
-  Toolkit::CheckBoxButton button = Toolkit::CheckBoxButton::DownCast(handle);
-
-  if(Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK == actionName)
+  Actor& selectedImage = GetSelectedImage();
+  if( actor == selectedImage )
   {
-    GetImplementation(button).DoClickAction(attributes);
-    ret = true;
+    actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
+
+    if( !mTickUVEffect )
+    {
+      mTickUVEffect = CreateImageRegionEffect();
+    }
+    mTickUVEffect.SetUniform("uBottomRight", Vector2( 0.0f, 1.0f ) );
+
+    ImageActor imageActor = ImageActor::DownCast( actor );
+    if( imageActor )
+    {
+      imageActor.SetShaderEffect( mTickUVEffect );
+    }
   }
-
-  return ret;
 }
 
-CheckBoxButton::CheckBoxButton()
-: Button(),
-  mChecked( false ),
-  mClickActionPerforming(false)
+void CheckBoxButton::PrepareForTranstionOut( Actor actor )
 {
-  // Creates specific painter.
-  mPainter = new CheckBoxButtonDefaultPainter();
+  Actor& selectedImage = GetSelectedImage();
+  if( actor == selectedImage )
+  {
+    actor.SetScale( Vector3::ONE );
+
+    if( !mTickUVEffect )
+    {
+        mTickUVEffect = CreateImageRegionEffect();
+    }
+    mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
+
+    ImageActor imageActor = ImageActor::DownCast( actor );
+    if( imageActor )
+    {
+      imageActor.SetShaderEffect( mTickUVEffect );
+    }
+  }
 }
 
-CheckBoxButton::~CheckBoxButton()
+void CheckBoxButton::OnTransitionIn( Actor actor )
 {
-  mPainter = NULL;
+  Actor& selectedImage = GetSelectedImage();
+  if( actor && actor == selectedImage )
+  {
+    if( GetPaintState() == UnselectedState )
+    {
+      Dali::Animation transitionAnimation = GetTransitionAnimation();
+      if( transitionAnimation )
+      {
+        DALI_ASSERT_DEBUG( mTickUVEffect );
+        if( mTickUVEffect )
+        {
+          // UV anim
+          transitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2::ONE );
+        }
+        // Actor size anim
+        transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
+      }
+    }
+    else
+    {
+      //explicitly end the swipe animation
+      actor.SetScale( Vector3::ONE );
+      if( mTickUVEffect )
+      {
+       mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
+      }
+    }
+  }
 }
 
 } // namespace Internal