Refactoring Button: remove painter
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
index 4eb7831..d12eb43 100644 (file)
@@ -1,30 +1,26 @@
-//
-// Copyright (c) 2014 Samsung Electronics Co., Ltd.
-//
-// Licensed under the Flora License, Version 1.0 (the License);
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//     http://floralicense.org/license/
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an AS IS BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
 
 // CLASS HEADER
-
 #include "check-box-button-impl.h"
 
 // EXTERNAL INCLUDES
-
-#include <algorithm>
-
-// INTERNAL INCLUDES
-
-#include "check-box-button-default-painter-impl.h"
+#include <dali/public-api/actors/image-actor.h>
+#include <dali/public-api/object/type-registry.h>
 
 namespace Dali
 {
@@ -38,6 +34,13 @@ namespace Internal
 namespace
 {
 
+const float FOREGROUND_DEPTH( 0.5f );
+const float BACKGROUND_DEPTH( 0.25f );
+
+const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
+
+const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
+
 BaseHandle Create()
 {
   return Toolkit::CheckBoxButton::New();
@@ -45,19 +48,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,195 +65,427 @@ Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
   return checkBoxButton;
 }
 
-void CheckBoxButton::SetChecked( bool checked )
+CheckBoxButton::CheckBoxButton()
+: Button(),
+  mPaintState( UnselectedState )
 {
-  if( !mDimmed && ( checked != mChecked ) )
-  {
-    // Stores the state.
-    mChecked = checked;
-
-    Toolkit::CheckBoxButton handle( GetOwner() );
-
-    // Notifies the painter the checkbox has been checked.
-    GetCheckBoxButtonPainter( mPainter )->Checked( handle );
+  SetTogglableButton( true );
 
-    // Emit signal.
-    mClickedSignalV2.Emit( handle );
-  }
+  SetAnimationTime( ANIMATION_TIME );
 }
 
-bool CheckBoxButton::IsChecked() const
+CheckBoxButton::~CheckBoxButton()
 {
-  return mChecked;
+  if( mCheckInAnimation )
+  {
+    mCheckInAnimation.Clear();
+  }
 }
 
-void CheckBoxButton::SetBackgroundImage( Image image )
+void CheckBoxButton::SetSelectedImage( Actor image )
 {
-  SetBackgroundImage( ImageActor::New( image ) );
+  Actor& selectedImage = GetSelectedImage();
+
+  switch( mPaintState )
+  {
+    case SelectedState:
+    {
+      if( selectedImage && selectedImage.GetParent() )
+      {
+        Self().Remove( selectedImage );
+      }
+
+      selectedImage = image;
+      Self().Add( selectedImage );
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      StopCheckInAnimation();
+      Self().Remove( selectedImage );
+
+      selectedImage = image;
+      Self().Add( selectedImage );
+
+      mPaintState = SelectedState;
+      break;
+    }
+    default:
+    {
+      selectedImage = image;
+      break;
+    }
+  }
+
+  selectedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  selectedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
+  selectedImage.SetZ( FOREGROUND_DEPTH );
 }
 
 void CheckBoxButton::SetBackgroundImage( Actor image )
 {
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetBackgroundImage( handle, image );
-}
+  Actor& backgroundImage = GetBackgroundImage();
 
-Actor& CheckBoxButton::GetBackgroundImage()
-{
-  return mBackgroundImage;
-}
+  switch( mPaintState )
+  {
+    case UnselectedState:             // FALLTHROUGH
+    case SelectedState:
+    case UnselectedSelectedTransition:
+    {
+      if( backgroundImage && backgroundImage.GetParent() )
+      {
+        Self().Remove( backgroundImage );
+
+        Actor& label = GetLabel();
+
+        if( label )
+        {
+          backgroundImage.Remove( label );
+          image.Add( label );
+        }
+      }
+
+      backgroundImage = image;
+      Self().Add( backgroundImage );
+      break;
+    }
+    default:
+    {
+      backgroundImage = image;
+      break;
+    }
+  }
 
-Actor CheckBoxButton::GetBackgroundImage() const
-{
-  return mBackgroundImage;
+  backgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  backgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
+  backgroundImage.SetZ( BACKGROUND_DEPTH );
 }
 
-void CheckBoxButton::SetCheckedImage( Image image )
+void CheckBoxButton::SetDisabledSelectedImage( Actor image )
 {
-  SetCheckedImage( ImageActor::New( image ) );
-}
+  Actor& disabledSelectedImage = GetDisabledSelectedImage();
 
-void CheckBoxButton::SetCheckedImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetCheckedImage( handle, image );
-}
+  switch( mPaintState )
+  {
+    case DisabledSelectedState:
+    {
+      if( disabledSelectedImage && disabledSelectedImage.GetParent() )
+      {
+        Self().Remove( disabledSelectedImage );
+      }
+
+      disabledSelectedImage = image;
+      Self().Add( disabledSelectedImage );
+      break;
+    }
+    default:
+    {
+      disabledSelectedImage = image;
+      break;
+    }
+  }
 
-Actor& CheckBoxButton::GetCheckedImage()
-{
-  return mCheckedImage;
+  disabledSelectedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  disabledSelectedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
+  disabledSelectedImage.SetZ( FOREGROUND_DEPTH );
 }
 
-Actor CheckBoxButton::GetCheckedImage() const
+void CheckBoxButton::SetDisabledBackgroundImage( Actor image )
 {
-  return mCheckedImage;
-}
+  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
 
-void CheckBoxButton::SetDimmedBackgroundImage( Image image )
-{
-  SetDimmedBackgroundImage( ImageActor::New( image ) );
-}
+  switch( mPaintState )
+  {
+    case DisabledSelectedState:
+    case DisabledUnselectedState:
+    {
+      if( disabledBackgroundImage && disabledBackgroundImage.GetParent() )
+      {
+        Self().Remove( disabledBackgroundImage );
+
+        Actor& label = GetLabel();
+
+        if( label )
+        {
+          disabledBackgroundImage.Remove( label );
+          image.Add( label );
+        }
+      }
+
+      disabledBackgroundImage = image;
+      Self().Add( disabledBackgroundImage );
+      break;
+    }
+    default:
+    {
+      disabledBackgroundImage = image;
+      break;
+    }
+  }
 
-void CheckBoxButton::SetDimmedBackgroundImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetDimmedBackgroundImage( handle, image );
+  disabledBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+  disabledBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
+  disabledBackgroundImage.SetZ( BACKGROUND_DEPTH );
 }
 
-Actor& CheckBoxButton::GetDimmedBackgroundImage()
+void CheckBoxButton::OnLabelSet()
 {
-  return mDimmedBackgroundImage;
-}
+  Actor& label = GetLabel();
 
-Actor CheckBoxButton::GetDimmedBackgroundImage() const
-{
-  return mDimmedBackgroundImage;
+  if( label )
+  {
+    label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
+    label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
+    label.MoveBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
+
+    if( IsDisabled() && GetDisabledBackgroundImage() )
+    {
+      GetDisabledBackgroundImage().Add( label );
+    }
+    else if ( GetBackgroundImage() )
+    {
+      GetBackgroundImage().Add( label );
+    }
+    else
+    {
+      Self().Add( label );
+    }
+  }
 }
 
-void CheckBoxButton::SetDimmedCheckedImage( Image image )
+void CheckBoxButton::OnSelected( bool selected )
 {
-  SetDimmedCheckedImage( ImageActor::New( image ) );
-}
+  Actor& selectedImage = GetSelectedImage();
 
-void CheckBoxButton::SetDimmedCheckedImage( Actor image )
-{
-  Toolkit::CheckBoxButton handle( GetOwner() );
-  GetCheckBoxButtonPainter( mPainter )->SetDimmedCheckedImage( handle, image );
+  switch( mPaintState )
+  {
+    case UnselectedState:
+    {
+      AddChild( selectedImage );
+      StartCheckInAnimation( selectedImage );    // Animate in the check actor
+
+      mPaintState = UnselectedSelectedTransition;
+      break;
+    }
+    case SelectedState:
+    {
+      RemoveChild( selectedImage );
+
+      mPaintState = UnselectedState;
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      StopCheckInAnimation();
+      RemoveChild( selectedImage );
+
+      mPaintState = UnselectedState;
+      break;
+    }
+    default:
+      break;
+  }
 }
 
-Actor& CheckBoxButton::GetDimmedCheckedImage()
+void CheckBoxButton::OnDisabled( bool disabled )
 {
-  return mDimmedCheckedImage;
-}
+  Actor& backgroundImage = GetBackgroundImage();
+  Actor& selectedImage = GetSelectedImage();
+  Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
+  Actor& disabledSelectedImage = GetDisabledSelectedImage();
 
-Actor CheckBoxButton::GetDimmedCheckedImage() const
-{
-  return mDimmedCheckedImage;
-}
+  switch( mPaintState )
+  {
+    case UnselectedState:
+    {
+      if( disabled )
+      {
+        RemoveChild( backgroundImage );
+        AddChild( disabledBackgroundImage );
+        mPaintState = DisabledUnselectedState;
+      }
+      break;
+    }
+    case SelectedState:
+    {
+      if( disabled )
+      {
+        RemoveChild( backgroundImage );
+        RemoveChild( selectedImage );
+        AddChild( disabledBackgroundImage );
+        AddChild( disabledSelectedImage );
+
+        mPaintState = DisabledSelectedState;
+      }
+      break;
+    }
+    case DisabledUnselectedState:
+    {
+      if( !disabled )
+      {
+        RemoveChild( disabledBackgroundImage );
+        AddChild( backgroundImage );
+
+        mPaintState = UnselectedState;
+      }
+      break;
+    }
+    case DisabledSelectedState:
+    {
+      if( !disabled )
+      {
+        RemoveChild( disabledBackgroundImage );
+        RemoveChild( disabledSelectedImage );
+        AddChild( backgroundImage );
+        AddChild( selectedImage );
+
+        mPaintState = SelectedState;
+      }
+      break;
+    }
+    case UnselectedSelectedTransition:
+    {
+      if( disabled )
+      {
+        StopCheckInAnimation();
+
+        RemoveChild( backgroundImage );
+        RemoveChild( selectedImage );
+        AddChild( disabledBackgroundImage );
+        AddChild( disabledSelectedImage );
+
+        mPaintState = DisabledSelectedState;
+      }
+      break;
+    }
+    default:
+      break;
+  }
 
-Actor& CheckBoxButton::GetFadeOutBackgroundImage()
-{
-  return mFadeOutBackgroundImage;
-}
+  Actor& label = GetLabel();
 
-Actor& CheckBoxButton::GetFadeOutCheckedImage()
-{
-  return mFadeOutCheckedImage;
+  if( label )
+  {
+    if( label.GetParent() )
+    {
+      label.GetParent().Remove( label );
+    }
+
+    if( disabled && disabledBackgroundImage)
+    {
+      disabledBackgroundImage.Add( label );
+    }
+    else if( backgroundImage )
+    {
+      backgroundImage.Add( label );
+    }
+  }
 }
 
-void CheckBoxButton::OnButtonInitialize()
+void CheckBoxButton::OnRelayout( const Vector2& size, ActorSizeContainer& container )
 {
-  mUseFadeAnimationProperty = Self().RegisterProperty( Toolkit::CheckBoxButton::USE_FADE_ANIMATION_PROPERTY_NAME, false );
-  mUseCheckAnimationProperty = Self().RegisterProperty( Toolkit::CheckBoxButton::USE_CHECK_ANIMATION_PROPERTY_NAME, true );
-}
+  Vector3 newSize;
 
-void CheckBoxButton::OnButtonUp()
-{
-  if( ButtonDown == mState )
+  if( IsDisabled() && GetDisabledBackgroundImage() )
   {
-    // Stores the state, notifies the painter and emits a signal.
-    SetChecked( !mChecked );
+    newSize = GetDisabledBackgroundImage().GetNaturalSize();
+  }
+  else if( GetBackgroundImage() )
+  {
+    newSize = GetBackgroundImage().GetNaturalSize();
   }
-}
 
-void CheckBoxButton::OnAnimationTimeSet( float animationTime )
-{
-  GetCheckBoxButtonPainter( mPainter )->SetAnimationTime( animationTime );
-}
+  Actor& label = GetLabel();
 
-float CheckBoxButton::OnAnimationTimeRequested() const
-{
-  return GetCheckBoxButtonPainter( mPainter )->GetAnimationTime();
+  if( label )
+  {
+    // Offset the label from the radio button image
+    newSize.width += DISTANCE_BETWEEN_IMAGE_AND_LABEL.width;
+
+    // Find the size of the control using size negotiation
+    Vector3 actorNaturalSize( label.GetNaturalSize() );
+    Control::Relayout( label, Vector2( actorNaturalSize.width, actorNaturalSize.height ), container );
+
+    Vector3 actorSize( label.GetSize() );
+    newSize.width += actorSize.width;
+    newSize.height = std::max( newSize.height, actorSize.height );
+  }
+
+  Self().SetSize( newSize );
 }
 
-void CheckBoxButton::OnActivated()
+void CheckBoxButton::AddChild( Actor& actor )
 {
-  // When the button is activated, it performs the click action
-  std::vector<Property::Value> attributes;
-  DoClickAction(attributes);
+  if( actor )
+  {
+    Self().Add( actor);
+  }
 }
 
-void CheckBoxButton::DoClickAction(const PropertyValueContainer& attributes)
+void CheckBoxButton::RemoveChild( Actor& actor )
 {
-  // Prevents the button signals from doing a recursive loop by sending an action
-  // and re-emitting the signals.
-  if(!mClickActionPerforming)
+  if( actor )
   {
-    mClickActionPerforming = true;
-    SetChecked( !mChecked );
-    mClickActionPerforming = false;
+    Self().Remove( actor );
   }
 }
 
-bool CheckBoxButton::DoAction(BaseObject* object, const std::string& actionName, const std::vector<Property::Value>& attributes)
+void CheckBoxButton::StartCheckInAnimation( Actor& actor )
 {
-  bool ret = false;
+  if( actor )
+  {
+    if( !mTickUVEffect )
+    {
+      ImageActor imageActor = ImageActor::DownCast( actor );
+      mTickUVEffect = ImageRegionEffect::New();
+      imageActor.SetShaderEffect( mTickUVEffect );
+    }
 
-  Dali::BaseHandle handle(object);
+    actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
 
-  Toolkit::CheckBoxButton button = Toolkit::CheckBoxButton::DownCast(handle);
+    mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
 
-  if(Toolkit::CheckBoxButton::ACTION_CHECK_BOX_BUTTON_CLICK == actionName)
-  {
-    GetImplementation(button).DoClickAction(attributes);
-    ret = true;
-  }
+    if( !mCheckInAnimation )
+    {
+      mCheckInAnimation = Dali::Animation::New( GetAnimationTime()  );
+    }
 
-  return ret;
+    // UV anim
+    mCheckInAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
+
+    // Actor size anim
+    mCheckInAnimation.AnimateTo( Property( actor, Actor::SCALE_X ), 1.0f);
+
+    mCheckInAnimation.FinishedSignal().Connect( this, &CheckBoxButton::CheckInAnimationFinished );
+    mCheckInAnimation.Play();
+  }
 }
 
-CheckBoxButton::CheckBoxButton()
-: Button(),
-  mChecked( false ),
-  mClickActionPerforming(false)
+void CheckBoxButton::StopCheckInAnimation()
 {
-  // Creates specific painter.
-  mPainter = new CheckBoxButtonDefaultPainter();
+  if( mCheckInAnimation )
+  {
+    mCheckInAnimation.Clear();
+    mCheckInAnimation.Reset();
+  }
 }
 
-CheckBoxButton::~CheckBoxButton()
+void CheckBoxButton::CheckInAnimationFinished( Dali::Animation& source )
 {
-  mPainter = NULL;
+  switch( mPaintState )
+  {
+    case UnselectedSelectedTransition:
+    {
+      mPaintState = SelectedState;
+      break;
+    }
+    default:
+    {
+      break;
+    }
+  }
+
+  StopCheckInAnimation();
 }
 
 } // namespace Internal