Fix for checkbox tick not animating on gradually
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
index 88e5364..e468c0e 100644 (file)
 // EXTERNAL INCLUDES
 #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 <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
 
 namespace Dali
 {
@@ -36,6 +40,8 @@ namespace
 
 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
+// Required for the UV reveal shader to render the tick on top of the rest of the checkbox.
+const float SHADER_DEPTH_OFFSET = 1.0f;
 
 BaseHandle Create()
 {
@@ -44,6 +50,10 @@ BaseHandle Create()
 
 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
 
+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";
 }
 
 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
@@ -71,21 +81,22 @@ CheckBoxButton::CheckBoxButton()
 
 CheckBoxButton::~CheckBoxButton()
 {
-  if( mTransitionAnimation )
-  {
-    mTransitionAnimation.Clear();
-  }
 }
 
 void CheckBoxButton::OnButtonInitialize()
 {
   // Wrap around all children
   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
+
+  SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
+  SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
+  SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
+  SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
 }
 
-void CheckBoxButton::OnLabelSet()
+void CheckBoxButton::OnLabelSet( bool noPadding )
 {
-  Actor& label = GetLabel();
+  Actor& label = GetLabelActor();
 
   if( label )
   {
@@ -100,179 +111,128 @@ void CheckBoxButton::OnLabelSet()
     {
       label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
-    else
-    {
-      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
-    }
-  }
-}
-
-bool CheckBoxButton::OnSelected()
-{
-  Actor& selectedImage = GetSelectedImage();
-
-  PaintState paintState = GetPaintState();
-
-  switch( paintState )
-  {
-    case UnselectedState:
-    {
-      StartTransitionAnimation( selectedImage );
-      break;
-    }
-    case SelectedState:
+    else if( IsSelected() && GetSelectedImage())
     {
-      RemoveChild( selectedImage );
-      break;
+      label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
-    case UnselectedSelectedTransition:
+    else if( GetUnselectedImage() )
     {
-      StopTransitionAnimation( false );
-      RemoveChild( selectedImage );
-      break;
+      label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
-    default:
+    else
     {
-      break;
+      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
   }
-
-  if( mTransitionAnimation )
-  {
-    return true;
-  }
-
-  return false;
 }
 
-bool CheckBoxButton::OnDisabled()
+void CheckBoxButton::OnDisabled()
 {
   Actor& backgroundImage = GetBackgroundImage();
-  Actor& selectedImage = GetSelectedImage();
   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
-  Actor& disabledSelectedImage = GetDisabledSelectedImage();
 
-  PaintState paintState = GetPaintState();
-
-  switch( paintState )
+  Actor& label = GetLabelActor();
+  if( label )
   {
-    case UnselectedState:
+    if( IsDisabled() && disabledBackgroundImage )
     {
-      RemoveChild( backgroundImage );
-      break;
-    }
-    case SelectedState:
-    {
-      RemoveChild( backgroundImage );
-      RemoveChild( selectedImage );
-      break;
+      label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
-    case DisabledUnselectedState:
+    else if( backgroundImage )
     {
-      RemoveChild( disabledBackgroundImage );
-      break;
+      label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
-    case DisabledSelectedState:
+    else if( IsSelected() && GetSelectedImage())
     {
-      RemoveChild( disabledBackgroundImage );
-      RemoveChild( disabledSelectedImage );
-      break;
+      label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
-    case UnselectedSelectedTransition:
+    else if( GetUnselectedImage() )
     {
-      StopTransitionAnimation();
-
-      RemoveChild( backgroundImage );
-      RemoveChild( selectedImage );
-      break;
+      label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
-    default:
+    else
     {
-      break;
+      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
     }
   }
+}
 
-  Actor& label = GetLabel();
-
-  if( label )
+void CheckBoxButton::PrepareForTranstionIn( Actor actor )
+{
+  Actor& selectedImage = GetSelectedImage();
+  if( actor == selectedImage )
   {
-    if( IsDisabled() && disabledBackgroundImage)
-    {
-      label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
-    }
-    else if( backgroundImage )
+    actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
+
+    if( !mTickUVEffect )
     {
-      label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+      mTickUVEffect = CreateImageRegionEffect();
     }
-    else
+    mTickUVEffect.SetUniform( "uBottomRight", Vector2( 0.0f, 1.0f ) );
+
+    ImageActor imageActor = ImageActor::DownCast( actor );
+    if( imageActor )
     {
-      label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+      // Ensure the tick effect is rendered above the reset of the checkbox.
+      imageActor.SetSortModifier( SHADER_DEPTH_OFFSET );
+      imageActor.SetShaderEffect( mTickUVEffect );
     }
   }
-
-  if( mTransitionAnimation )
-  {
-    return true;
-  }
-
-  return false;
 }
 
-void CheckBoxButton::StopAllAnimations()
+void CheckBoxButton::PrepareForTranstionOut( Actor actor )
 {
-  StopTransitionAnimation();
-}
-
-void CheckBoxButton::StartTransitionAnimation( Actor& actor )
-{
-  if( actor )
+  Actor& selectedImage = GetSelectedImage();
+  if( actor == selectedImage )
   {
+    actor.SetScale( Vector3::ONE );
+
     if( !mTickUVEffect )
     {
-      ImageActor imageActor = ImageActor::DownCast( actor );
-      mTickUVEffect = ImageRegionEffect::New();
-      imageActor.SetShaderEffect( mTickUVEffect );
+      mTickUVEffect = CreateImageRegionEffect();
     }
+    mTickUVEffect.SetUniform( "uBottomRight", Vector2::ONE );
 
-    actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
-
-    mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
-
-    if( !mTransitionAnimation )
+    ImageActor imageActor = ImageActor::DownCast( actor );
+    if( imageActor )
     {
-      mTransitionAnimation = Dali::Animation::New( GetAnimationTime()  );
+      imageActor.SetShaderEffect( mTickUVEffect );
     }
-
-    // UV anim
-    mTransitionAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
-
-    // Actor size anim
-    mTransitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
-
-    mTransitionAnimation.FinishedSignal().Connect( this, &CheckBoxButton::TransitionAnimationFinished );
-    mTransitionAnimation.Play();
   }
 }
 
-void CheckBoxButton::StopTransitionAnimation( bool remove )
+void CheckBoxButton::OnTransitionIn( Actor actor )
 {
-  if( mTransitionAnimation )
-  {
-    mTransitionAnimation.Clear();
-    mTransitionAnimation.Reset();
-  }
-
-  if( remove )
+  Actor& selectedImage = GetSelectedImage();
+  if( actor && actor == selectedImage )
   {
-    UpdatePaintTransitionState();
+    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 );
+      }
+    }
   }
 }
 
-void CheckBoxButton::TransitionAnimationFinished( Dali::Animation& source )
-{
-  StopTransitionAnimation();
-}
-
 } // namespace Internal
 
 } // namespace Toolkit