Refactored Button and derived classes, moving state change and transition logic to...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / button-impl.cpp
index c0d52ca..4072f43 100644 (file)
@@ -32,7 +32,7 @@
 /**
  * Button states and contents
  *                                         (3) mSelectedContent
- *  (2) mButtonContent                     (2) mSelectedBackgroundContent
+ *  (2) mUnselectedContent                 (2) mSelectedBackgroundContent
  *  (1) mBackgroundContent                 (1) mBackgroundContent
  * < unselected > ----------------------- < selected >
  *       |                OnSelect()            |
@@ -45,7 +45,7 @@
  * The drawing order of child actors is as follows.
  *
  *  Top      mLabel
- *   |       mButtonContent / mSelectedContent / mDisabledContent / mDisabledSelectedContent
+ *   |       mUnselectedContent / mSelectedContent / mDisabledContent / mDisabledSelectedContent
  *   |       mSelectedBackgroundContent
  * Bottom    mBackgroundContent / mDisabledBackgroundContent
  *
@@ -117,252 +117,109 @@ Button::Button()
 
 Button::~Button()
 {
-  if( mAutoRepeatingTimer )
-  {
-    mAutoRepeatingTimer.Reset();
-  }
 }
 
 void Button::SetDisabled( bool disabled )
 {
-  if( disabled != mDisabled )
+  if( disabled == mDisabled )
   {
-    unsigned int backgroundIndex;
-    unsigned int buttonIndex;
-
-    bool animationStarted = false;
-
-    mDisabled = disabled;
-
-    switch( mPaintState )
-    {
-      case UnselectedState:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mDisabledContent );
-
-        if( mBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
-
-        InsertChild( backgroundIndex, mDisabledBackgroundContent );
-
-        animationStarted = OnDisabled();
-
-        if( animationStarted )
-        {
-          mPaintState = UnselectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledUnselectedState;
-        }
-        break;
-      }
-      case SelectedState:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mDisabledSelectedContent );
-
-        if( mBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
-
-        InsertChild( backgroundIndex, mDisabledBackgroundContent );
-
-        animationStarted = OnDisabled();
-
-        if( animationStarted )
-        {
-          mPaintState = SelectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledSelectedState;
-        }
-        break;
-      }
-      case DisabledUnselectedState:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mButtonContent );
-
-        if( mDisabledBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
-
-        InsertChild( backgroundIndex, mBackgroundContent );
-
-        animationStarted = OnDisabled();
-
-        if( animationStarted )
-        {
-          mPaintState = DisabledUnselectedTransition;
-        }
-        else
-        {
-          mPaintState = UnselectedState;
-        }
-        break;
-      }
-      case DisabledSelectedState:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mSelectedContent );
-
-        if( mDisabledBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
-
-        InsertChild( backgroundIndex, mSelectedBackgroundContent );
-        InsertChild( backgroundIndex, mBackgroundContent );
+    return;
+  }
 
-        animationStarted = OnDisabled();
+  StopTransitionAnimation();
 
-        if( animationStarted )
-        {
-          mPaintState = DisabledSelectedTransition;
-        }
-        else
-        {
-          mPaintState = SelectedState;
-        }
-        break;
-      }
-      case UnselectedSelectedTransition:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mDisabledSelectedContent );
+  mDisabled = disabled;
 
-        if( mBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
+  // Notifies the derived class the button has been disabled.
+  OnDisabled();
 
-        InsertChild( backgroundIndex, mDisabledBackgroundContent );
+  switch( mPaintState )
+  {
+    case UnselectedState:
+    {
+      //Layer Order
+      //(3) mDisabledContent (Inserted)
+      //(4) mUnselectedContent
+      //(2) mDisabledBackgroundContent (Inserted)
+      //(1) mBackgroundContent
 
-        animationStarted = OnDisabled();
+      TransitionInBetween( mUnselectedContent, mLabel, mDisabledContent );
+      TransitionInAbove( mBackgroundContent, mDisabledBackgroundContent );
 
-        if( animationStarted )
-        {
-          mPaintState = SelectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledSelectedState;
-        }
-        break;
-      }
-      case SelectedUnselectedTransition:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mDisabledContent );
+      TransitionOut( mUnselectedContent );
+      TransitionOut( mSelectedContent );
+      TransitionOut( mBackgroundContent );
+      TransitionOut( mSelectedBackgroundContent );
+      TransitionOut( mDisabledSelectedContent );
 
-        if( mBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
+      mPaintState = DisabledUnselectedState;
+      break;
+    }
+    case SelectedState:
+    {
+      //Layer Order
+      //(5) mDisabledSelectedContent (Inserted)
+      //(4) mSelectedContent
+      //(3) mDisabledBackgroundContent (Inserted)
+      //(2) mSelectedBackgroundContent
+      //(1) mBackgroundContent
+
+      TransitionInBetween( mSelectedContent, mLabel, mDisabledSelectedContent );
+      TransitionInAbove( mSelectedBackgroundContent, mDisabledBackgroundContent );
+
+      TransitionOut( mUnselectedContent );
+      TransitionOut( mSelectedContent );
+      TransitionOut( mBackgroundContent );
+      TransitionOut( mSelectedBackgroundContent );
+      TransitionOut( mDisabledContent );
 
-        InsertChild( backgroundIndex, mDisabledBackgroundContent );
+      mPaintState = DisabledSelectedState;
+      break;
+    }
+    case DisabledUnselectedState:
+    {
+      //Layer Order
+      //(3) mUnselectedContent (Inserted)
+      //(4) mDisabledContent
+      //(2) mBackgroundContent (Inserted)
+      //(1) mDisabledBackgroundContent
 
-        animationStarted = OnDisabled();
+      TransitionInBetween( mDisabledContent, mLabel, mUnselectedContent );
+      TransitionInAbove( mDisabledBackgroundContent, mBackgroundContent );
 
-        if( animationStarted )
-        {
-          mPaintState = UnselectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledUnselectedState;
-        }
-        break;
-      }
-      case UnselectedDisabledTransition:
-      {
-        animationStarted = OnDisabled();
+      TransitionOut( mSelectedContent );
+      TransitionOut( mSelectedBackgroundContent );
+      TransitionOut( mDisabledContent );
+      TransitionOut( mDisabledSelectedContent );
+      TransitionOut( mDisabledBackgroundContent );
 
-        if( animationStarted )
-        {
-          mPaintState = DisabledUnselectedTransition;
-        }
-        else
-        {
-          mPaintState = UnselectedState;
-        }
-        break;
-      }
-      case DisabledUnselectedTransition:
-      {
-        animationStarted = OnDisabled();
-
-        if( animationStarted )
-        {
-          mPaintState = UnselectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledUnselectedState;
-        }
-        break;
-      }
-      case SelectedDisabledTransition:
-      {
-        animationStarted = OnDisabled();
-
-        if( animationStarted )
-        {
-          mPaintState = DisabledSelectedTransition;
-        }
-        else
-        {
-          mPaintState = SelectedState;
-        }
-        break;
-      }
-      case DisabledSelectedTransition:
-      {
-        animationStarted = OnDisabled();
+      mPaintState = UnselectedState;
+      break;
+    }
+    case DisabledSelectedState:
+    {
+      //Layer Order
+      //(4) mSelectedContent (Inserted)
+      //(5) mDisabledSelectedContent
+      //(3) mSelectedBackgroundContent (Inserted)
+      //(2) mBackgroundContent (Inserted)
+      //(1) mDisabledBackgroundContent
+
+      TransitionInBetween( mDisabledSelectedContent, mLabel, mSelectedContent );
+      TransitionInAbove( mDisabledBackgroundContent, mSelectedBackgroundContent );
+      TransitionInAbove( mDisabledBackgroundContent, mBackgroundContent );
+
+      TransitionOut( mUnselectedContent );
+      TransitionOut( mDisabledContent );
+      TransitionOut( mDisabledSelectedContent );
+      TransitionOut( mDisabledBackgroundContent );
 
-        if( animationStarted )
-        {
-          mPaintState = SelectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledSelectedState;
-        }
-        break;
-      }
+      mPaintState = SelectedState;
+      break;
     }
   }
+
+  StartTransitionAnimation();
 }
 
 bool Button::IsDisabled() const
@@ -440,154 +297,64 @@ void Button::SetSelected( bool selected )
 
 void Button::SetSelected( bool selected, bool emitSignal )
 {
-  unsigned int buttonIndex, backgroundIndex;
-  bool animationStarted = false;
+  StopTransitionAnimation();
 
   mSelected = selected;
 
+  // Notifies the derived class the button has been selected.
+  OnSelected();
+
   switch( mPaintState )
   {
     case UnselectedState:
     {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mSelectedContent );
+      //Layer Order
+      //(3) mSelectedContent (Inserted)
+      //(4) mUnselectedContent
+      //(2) mSelectedBackgroundContent (Inserted)
+      //(1) mBackgroundContent
 
-      if( mBackgroundContent )
-      {
-        backgroundIndex = 1;
-      }
-      else
-      {
-        backgroundIndex = 0;
-      }
+      TransitionInBetween( mUnselectedContent, mLabel, mSelectedContent );
+      TransitionInAbove( mBackgroundContent, mSelectedBackgroundContent );
+      TransitionInAtIndex( 0, mBackgroundContent );
 
-      InsertChild( backgroundIndex, mSelectedBackgroundContent );
+      TransitionOut( mUnselectedContent );
+      TransitionOut( mDisabledContent );
+      TransitionOut( mDisabledSelectedContent );
+      TransitionOut( mDisabledBackgroundContent );
 
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
-
-      if( animationStarted )
-      {
-        mPaintState = UnselectedSelectedTransition;
-      }
-      else
-      {
-        mPaintState = SelectedState;
-      }
+      mPaintState = SelectedState;
       break;
     }
     case SelectedState:
     {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mButtonContent );
-
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
-
-      if( animationStarted )
-      {
-        mPaintState = SelectedUnselectedTransition;
-      }
-      else
-      {
-        mPaintState = UnselectedState;
-      }
-      break;
-    }
-    case UnselectedSelectedTransition:
-    {
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
-
-      if( animationStarted )
-      {
-        mPaintState = SelectedUnselectedTransition;
-      }
-      else
-      {
-        mPaintState = UnselectedState;
-      }
-      break;
-    }
-    case SelectedUnselectedTransition:
-    {
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
-
-      if( animationStarted )
-      {
-        mPaintState = UnselectedSelectedTransition;
-      }
-      else
-      {
-        mPaintState = SelectedState;
-      }
-      break;
-    }
-    case DisabledUnselectedTransition:
-    {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mSelectedContent );
-
-      if( mDisabledBackgroundContent )
-      {
-        if(  mBackgroundContent )
-        {
-          backgroundIndex = 2;
-        }
-        else
-        {
-          backgroundIndex = 1;
-        }
-      }
-      else if( mBackgroundContent )
-      {
-        backgroundIndex = 1;
-      }
-      else
-      {
-        backgroundIndex = 0;
-      }
+      //Layer Order
+      //(3) mUnselectedContent (Inserted)
+      //(2) mSelectedContent
+      //(1) mBackgroundContent
 
-      InsertChild( backgroundIndex, mSelectedBackgroundContent );
+      TransitionInBetween( mSelectedContent, mLabel, mUnselectedContent );
+      TransitionInAtIndex( 0, mBackgroundContent );
 
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
+      TransitionOut( mSelectedContent );
+      TransitionOut( mSelectedBackgroundContent );
+      TransitionOut( mDisabledContent );
+      TransitionOut( mDisabledSelectedContent );
+      TransitionOut( mDisabledBackgroundContent );
 
-      if( animationStarted )
-      {
-        mPaintState = UnselectedSelectedTransition;
-      }
-      else
-      {
-        mPaintState = SelectedState;
-      }
-      break;
-    }
-    case DisabledSelectedTransition:
-    {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mButtonContent );
-
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
-
-      if( animationStarted )
-      {
-        mPaintState = SelectedUnselectedTransition;
-      }
-      else
-      {
-        mPaintState = UnselectedState;
-      }
+      mPaintState = UnselectedState;
       break;
     }
-    default:
+    case DisabledUnselectedState:
+    case DisabledSelectedState:
     {
+      DALI_ASSERT_DEBUG( 0 && "Shouldn't be able to change paint state if the button is disabled." );
       break;
     }
   }
 
+  StartTransitionAnimation();
+
   if( emitSignal )
   {
     Toolkit::Button handle( GetOwner() );
@@ -653,26 +420,21 @@ Actor& Button::GetLabel()
 
 void Button::SetButtonImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
-  if( mButtonContent && mButtonContent.GetParent() )
+  if( mUnselectedContent && mUnselectedContent.GetParent() )
   {
-    Self().Remove( mButtonContent );
+    Self().Remove( mUnselectedContent );
   }
 
-  mButtonContent = image;
-
-  mButtonContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-  mButtonContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
-  mButtonContent.SetPosition( 0.f, 0.f );
-
-  if( mPaintState == UnselectedState )
+  mUnselectedContent = image;
+  if( mUnselectedContent )
   {
-    unsigned int index = FindChildIndex( mLabel );
-
-    Self().Insert( index, mButtonContent );
+    mUnselectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mUnselectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    mUnselectedContent.SetPosition( 0.f, 0.f );
   }
-
+  ResetImageLayers();
   OnButtonImageSet();
 
   RelayoutRequest();
@@ -680,17 +442,17 @@ void Button::SetButtonImage( Actor image )
 
 Actor Button::GetButtonImage() const
 {
-  return mButtonContent;
+  return mUnselectedContent;
 }
 
 Actor& Button::GetButtonImage()
 {
-  return mButtonContent;
+  return mUnselectedContent;
 }
 
 void Button::SetSelectedImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mSelectedContent && mSelectedContent.GetParent() )
   {
@@ -698,18 +460,13 @@ void Button::SetSelectedImage( Actor image )
   }
 
   mSelectedContent = image;
-
-  mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-  mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
-  mSelectedContent.SetPosition( 0.f, 0.f );
-
-  if( mPaintState == SelectedState )
+  if( mSelectedContent )
   {
-    unsigned int index = FindChildIndex( mLabel );
-
-    Self().Insert( index, mSelectedContent );
+    mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    mSelectedContent.SetPosition( 0.f, 0.f );
   }
-
+  ResetImageLayers();
   OnSelectedImageSet();
 
   RelayoutRequest();
@@ -727,7 +484,7 @@ Actor& Button::GetSelectedImage()
 
 void Button::SetBackgroundImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mBackgroundContent && mBackgroundContent.GetParent() )
   {
@@ -735,16 +492,13 @@ void Button::SetBackgroundImage( Actor image )
   }
 
   mBackgroundContent = image;
-
-  mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-  mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
-  mBackgroundContent.SetPosition( 0.f, 0.f );
-
-  if( mPaintState == UnselectedState || mPaintState == SelectedState )
+  if( mBackgroundContent )
   {
-    Self().Insert( 0, mBackgroundContent );
+    mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    mBackgroundContent.SetPosition( 0.f, 0.f );
   }
-
+  ResetImageLayers();
   OnBackgroundImageSet();
 
   RelayoutRequest();
@@ -762,7 +516,7 @@ Actor& Button::GetBackgroundImage()
 
 void Button::SetSelectedBackgroundImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
   {
@@ -770,23 +524,13 @@ void Button::SetSelectedBackgroundImage( Actor image )
   }
 
   mSelectedBackgroundContent = image;
-
-  mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-  mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
-  mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
-
-  if( mPaintState == SelectedState )
+  if( mSelectedBackgroundContent )
   {
-    if( mBackgroundContent )
-    {
-      Self().Insert( 1, mSelectedBackgroundContent );
-    }
-    else
-    {
-      Self().Insert( 0, mSelectedBackgroundContent );
-    }
+    mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
   }
-
+  ResetImageLayers();
   OnSelectedBackgroundImageSet();
 
   RelayoutRequest();
@@ -804,7 +548,7 @@ Actor& Button::GetSelectedBackgroundImage()
 
 void Button::SetDisabledImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mDisabledContent && mDisabledContent.GetParent() )
   {
@@ -812,18 +556,14 @@ void Button::SetDisabledImage( Actor image )
   }
 
   mDisabledContent = image;
-
-  mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-  mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
-  mDisabledContent.SetPosition( 0.f, 0.f );
-
-  if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
+  if( mDisabledContent )
   {
-    unsigned int index = FindChildIndex( mLabel );
-
-    Self().Insert( index, mDisabledContent );
+    mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    mDisabledContent.SetPosition( 0.f, 0.f );
   }
 
+  ResetImageLayers();
   OnDisabledImageSet();
 }
 
@@ -839,7 +579,7 @@ Actor& Button::GetDisabledImage()
 
 void Button::SetDisabledSelectedImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
   {
@@ -847,18 +587,14 @@ void Button::SetDisabledSelectedImage( Actor image )
   }
 
   mDisabledSelectedContent = image;
-
-  mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-  mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
-  mDisabledSelectedContent.SetPosition( 0.f, 0.f );
-
-  if( mPaintState == DisabledSelectedState )
+  if( mDisabledSelectedContent )
   {
-    unsigned int index = FindChildIndex( mLabel );
-
-    Self().Insert( index, mDisabledSelectedContent );
+    mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    mDisabledSelectedContent.SetPosition( 0.f, 0.f );
   }
 
+  ResetImageLayers();
   OnDisabledSelectedImageSet();
 }
 
@@ -874,7 +610,7 @@ Actor& Button::GetDisabledSelectedImage()
 
 void Button::SetDisabledBackgroundImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
   {
@@ -882,16 +618,13 @@ void Button::SetDisabledBackgroundImage( Actor image )
   }
 
   mDisabledBackgroundContent = image;
-
-  mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
-  mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
-  mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
-
-  if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
+  if( mDisabledBackgroundContent )
   {
-    Self().Insert( 0, mDisabledBackgroundContent );
+    mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
+    mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
+    mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
   }
-
+  ResetImageLayers();
   OnDisabledBackgroundImageSet();
 }
 
@@ -905,7 +638,7 @@ Actor& Button::GetDisabledBackgroundImage()
   return mDisabledBackgroundContent;
 }
 
-  bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
+bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
 {
   bool ret = false;
 
@@ -917,14 +650,13 @@ Actor& Button::GetDisabledBackgroundImage()
 
   if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
   {
-    GetImplementation( button ).DoClickAction( attributes );
-    ret = true;
+    ret = GetImplementation( button ).DoClickAction( attributes );
   }
 
   return ret;
 }
 
-  void Button::DoClickAction( const Property::Map& attributes )
+bool Button::DoClickAction( const Property::Map& attributes )
 {
   // Prevents the button signals from doing a recursive loop by sending an action
   // and re-emitting the signals.
@@ -935,60 +667,11 @@ Actor& Button::GetDisabledBackgroundImage()
     mState = ButtonDown;
     OnButtonUp();
     mClickActionPerforming = false;
-  }
-}
 
-void Button::UpdatePaintTransitionState()
-{
-  switch( mPaintState )
-  {
-    case UnselectedSelectedTransition:
-    {
-      RemoveChild( mButtonContent );
-      mPaintState = SelectedState;
-      break;
-    }
-    case SelectedUnselectedTransition:
-    {
-      RemoveChild( mSelectedBackgroundContent );
-      RemoveChild( mSelectedContent );
-      mPaintState = UnselectedState;
-      break;
-    }
-    case UnselectedDisabledTransition:
-    {
-      RemoveChild( mBackgroundContent );
-      RemoveChild( mButtonContent );
-      mPaintState = DisabledUnselectedState;
-      break;
-    }
-    case DisabledUnselectedTransition:
-    {
-      RemoveChild( mDisabledBackgroundContent );
-      RemoveChild( mDisabledContent );
-      mPaintState = UnselectedState;
-      break;
-    }
-    case SelectedDisabledTransition:
-    {
-      RemoveChild( mBackgroundContent );
-      RemoveChild( mSelectedBackgroundContent );
-      RemoveChild( mSelectedContent );
-      mPaintState = DisabledSelectedState;
-      break;
-    }
-    case DisabledSelectedTransition:
-    {
-      RemoveChild( mDisabledBackgroundContent );
-      RemoveChild( mDisabledSelectedContent );
-      mPaintState = SelectedState;
-      break;
-    }
-    default:
-    {
-      break;
-    }
+    return true;
   }
+
+  return false;
 }
 
 void Button::OnButtonStageDisconnection()
@@ -1203,15 +886,21 @@ void Button::OnInitialize()
 
   OnButtonInitialize();
 
-  self.SetDrawMode( DrawMode::OVERLAY );
   self.SetKeyboardFocusable( true );
 }
 
-void Button::OnActivated()
+bool Button::OnAccessibilityActivated()
 {
-  // When the button is activated, it performs the click action
+  return OnKeyboardEnter();
+}
+
+bool Button::OnKeyboardEnter()
+{
+  // When the enter key is pressed, or button is activated, the click action is performed.
   Property::Map attributes;
-  DoClickAction( attributes );
+  bool ret = DoClickAction( attributes );
+
+  return ret;
 }
 
 void Button::OnControlStageDisconnection()
@@ -1255,206 +944,297 @@ bool Button::AutoRepeatingSlot()
 
 void Button::Pressed()
 {
-  unsigned int buttonIndex, backgroundIndex;
-  bool animationStarted = false;
+  if( mPaintState == UnselectedState )
+  {
+    StopTransitionAnimation();
 
-  switch( mPaintState )
+    // Notifies the derived class the button has been pressed.
+     OnPressed();
+
+     //Layer Order
+    //(4) mSelectedContent (Inserted)
+    //(3) mUnselectedContent
+    //(2) mSelectedBackgroundContent (Inserted)
+    //(1) mBackgroundContent
+
+    TransitionInBetween( mUnselectedContent, mLabel, mSelectedContent );
+    TransitionInAbove( mBackgroundContent, mSelectedBackgroundContent );
+    TransitionInAtIndex( 0, mBackgroundContent );
+
+    TransitionOut( mUnselectedContent );
+    TransitionOut( mDisabledContent );
+    TransitionOut( mDisabledSelectedContent );
+    TransitionOut( mDisabledBackgroundContent );
+
+    mPaintState = SelectedState;
+
+    StartTransitionAnimation();
+  }
+}
+
+void Button::Released()
+{
+  if( mPaintState == SelectedState )
   {
-    case UnselectedState:
-    {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mSelectedContent );
+    StopTransitionAnimation();
 
-      if( mBackgroundContent )
-      {
-        backgroundIndex = 1;
-      }
-      else
-      {
-        backgroundIndex = 0;
-      }
+    // Notifies the derived class the button has been released.
+    OnReleased();
 
-      InsertChild( backgroundIndex, mSelectedBackgroundContent );
+    //Layer Order
+    //(3) mUnselectedContent (Inserted)
+    //(2) mSelectedContent
+    //(1) mBackgroundContent
 
-      // Notifies the derived class the button has been pressed.
-      animationStarted = OnPressed();
+    TransitionInBetween( mSelectedContent, mLabel, mUnselectedContent );
+    TransitionInAtIndex( 0, mBackgroundContent );
 
-      if( animationStarted )
-      {
-        mPaintState = UnselectedSelectedTransition;
-      }
-      else
-      {
-        mPaintState = SelectedState;
-      }
-      break;
-    }
-    case SelectedUnselectedTransition:
-    {
-      // Notifies the derived class the button has been pressed.
-      animationStarted = OnPressed();
+    TransitionOut( mSelectedContent );
+    TransitionOut( mSelectedBackgroundContent );
+    TransitionOut( mDisabledContent );
+    TransitionOut( mDisabledSelectedContent );
+    TransitionOut( mDisabledBackgroundContent );
 
-      if( animationStarted )
-      {
-        mPaintState = UnselectedSelectedTransition;
-      }
-      else
-      {
-        mPaintState = SelectedState;
-      }
-      break;
+    mPaintState = UnselectedState;
+
+    StartTransitionAnimation();
+  }
+}
+
+Button::ButtonState Button::GetState()
+{
+  return mState;
+}
+
+Button::PaintState Button::GetPaintState()
+{
+  return mPaintState;
+}
+
+bool Button::InsertButtonImage( unsigned int index, Actor& actor )
+{
+  if( actor )
+  {
+    Self().Insert( index, actor );
+    PrepareForTranstionOut( actor );
+    return true;
+  }
+
+  return false;
+}
+
+void Button::RemoveButtonImage( Actor& actor )
+{
+  if( actor )
+  {
+    if( actor.GetParent() )
+    {
+      Self().Remove( actor );
     }
-    case DisabledUnselectedTransition:
+    PrepareForTranstionIn( actor );
+  }
+}
+
+unsigned int Button::FindChildIndex( Actor& actor )
+{
+  Actor self = Self();
+  unsigned int childrenNum = self.GetChildCount();
+
+  for( unsigned int i = 0; i < childrenNum; i++ )
+  {
+    Actor child = self.GetChildAt( i );
+    if( child == actor )
     {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mSelectedContent );
+      return i;
+    }
+  }
 
-      if( mDisabledBackgroundContent )
-      {
-        if(  mBackgroundContent )
-        {
-          backgroundIndex = 2;
-        }
-        else
-        {
-          backgroundIndex = 1;
-        }
-      }
-      else if( mBackgroundContent )
-      {
-        backgroundIndex = 1;
-      }
-      else
-      {
-        backgroundIndex = 0;
-      }
+  return childrenNum;
+}
 
-      InsertChild( backgroundIndex, mSelectedBackgroundContent );
+void Button::TransitionInBetween(  Actor childLower, Actor childUpper, Actor actor )
+{
+  unsigned int index = childLower ? FindChildIndex( childLower ) + 1 : FindChildIndex( childUpper );
+  TransitionInAtIndex( index, actor );
+}
 
-      // Notifies the derived class the button has been pressed.
-      animationStarted = OnPressed();
+void Button::TransitionInAbove( Actor child, Actor actor )
+{
+  unsigned int index = child ? FindChildIndex( child ) + 1 : 0;
+  TransitionInAtIndex( index, actor );
+}
 
-      if( animationStarted )
-      {
-        mPaintState = UnselectedSelectedTransition;
-      }
-      else
-      {
-        mPaintState = SelectedState;
-      }
-      break;
+void Button::TransitionInAtIndex( unsigned int index, Actor actor )
+{
+  if( actor )
+  {
+    if( !actor.GetParent() )
+    {
+      Self().Insert( index, actor );
     }
-    default:
-      break;
+
+    OnTransitionIn( actor );
   }
 }
 
-void Button::Released()
+void Button::TransitionOut( Actor actor )
+{
+  OnTransitionOut( actor );
+}
+
+void Button::ResetImageLayers()
 {
-  unsigned int buttonIndex;
-  bool animationStarted = false;
+  //ensure that all layers are in the correct order and state according to the paint state
 
+  int index = 0;
   switch( mPaintState )
   {
-    case SelectedState:
+    case UnselectedState:
     {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mButtonContent );
+      //Layer Order
+      //(2) mUnselectedContent
+      //(1) mBackgroundContent
 
-      // Notifies the derived class the button has been released.
-      animationStarted = OnReleased();
+      RemoveButtonImage( mSelectedContent );
+      RemoveButtonImage( mSelectedBackgroundContent );
+      RemoveButtonImage( mDisabledContent );
+      RemoveButtonImage( mDisabledSelectedContent );
+      RemoveButtonImage( mDisabledBackgroundContent );
 
-      if( animationStarted )
+      if( InsertButtonImage( index, mBackgroundContent ) )
       {
-        mPaintState = SelectedUnselectedTransition;
+        ++index;
       }
-      else
+      if( InsertButtonImage( index, mUnselectedContent ) )
       {
-        mPaintState = UnselectedState;
+        ++index;
       }
       break;
     }
-    case UnselectedSelectedTransition:
+    case SelectedState:
     {
-      // Notifies the derived class the button has been released.
-      animationStarted = OnReleased();
+      //Layer Order
+      //(3) mSelectedContent
+      //(2) mSelectedBackgroundContent
+      //(1) mBackgroundContent
+
+      RemoveButtonImage( mUnselectedContent );
+      RemoveButtonImage( mDisabledContent );
+      RemoveButtonImage( mDisabledSelectedContent );
+      RemoveButtonImage( mDisabledBackgroundContent );
 
-      if( animationStarted )
+      if( InsertButtonImage( index, mBackgroundContent ) )
       {
-        mPaintState = SelectedUnselectedTransition;
+        ++index;
       }
-      else
+      if( InsertButtonImage( index, mSelectedBackgroundContent ) )
       {
-        mPaintState = UnselectedState;
+        ++index;
+      }
+      if( InsertButtonImage( index, mSelectedContent ) )
+      {
+        ++index;
       }
       break;
     }
-    case DisabledSelectedTransition:
+    case DisabledUnselectedState:
     {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mButtonContent );
+      //Layer Order
+      //(2) mDisabledContent
+      //(1) mDisabledBackgroundContent
 
-      // Notifies the derived class the button has been released.
-      animationStarted = OnReleased();
+      RemoveButtonImage( mUnselectedContent );
+      RemoveButtonImage( mBackgroundContent );
+      RemoveButtonImage( mSelectedContent );
+      RemoveButtonImage( mDisabledSelectedContent );
+      RemoveButtonImage( mSelectedBackgroundContent );
 
-      if( animationStarted )
+      if( InsertButtonImage( index, mDisabledBackgroundContent ? mDisabledBackgroundContent : mBackgroundContent ) )
       {
-        mPaintState = SelectedUnselectedTransition;
+        ++index;
       }
-      else
+      if( InsertButtonImage( index, mDisabledContent ? mDisabledContent : mUnselectedContent ) )
       {
-        mPaintState = UnselectedState;
+        ++index;
       }
       break;
     }
-    default:
+    case DisabledSelectedState:
     {
+      //Layer Order
+      // (2) mDisabledSelectedContent
+      // (1) mDisabledBackgroundContent
+
+      RemoveButtonImage( mUnselectedContent );
+      RemoveButtonImage( mSelectedContent );
+      RemoveButtonImage( mBackgroundContent );
+      RemoveButtonImage( mSelectedBackgroundContent );
+      RemoveButtonImage( mDisabledContent );
+
+      if( mDisabledBackgroundContent )
+      {
+        if( InsertButtonImage( index, mDisabledBackgroundContent) )
+        {
+          ++index;
+        }
+      }
+      else
+      {
+        if( InsertButtonImage( index, mBackgroundContent ) )
+        {
+          ++index;
+        }
+        if( InsertButtonImage( index, mSelectedBackgroundContent ) )
+        {
+          ++index;
+        }
+      }
+
+      if( InsertButtonImage( index, mDisabledSelectedContent ? mDisabledSelectedContent : mSelectedContent) )
+      {
+        ++index;
+      }
       break;
     }
   }
 }
 
-Button::ButtonState Button::GetState()
+void Button::StartTransitionAnimation()
 {
-  return mState;
-}
-
-Button::PaintState Button::GetPaintState()
-{
-  return mPaintState;
-}
-
-void Button::InsertChild( unsigned int index, Actor& actor )
-{
-  if( actor )
+  if( mTransitionAnimation )
   {
-    Self().Insert( index, actor);
+    mTransitionAnimation.Play();
+  }
+  else
+  {
+    ResetImageLayers();
   }
 }
 
-void Button::RemoveChild( Actor& actor )
+void Button::StopTransitionAnimation()
 {
-  if( actor && actor.GetParent() )
+  if( mTransitionAnimation )
   {
-    Self().Remove( actor );
+    mTransitionAnimation.Clear();
+    mTransitionAnimation.Reset();
   }
 }
 
-unsigned int Button::FindChildIndex( Actor& actor )
+Dali::Animation Button::GetTransitionAnimation()
 {
-  Actor self = Self();
-  unsigned int childrenNum = self.GetChildCount();
-
-  for( unsigned int i = 0; i < childrenNum; i++ )
+  if( !mTransitionAnimation )
   {
-    Actor child = self.GetChildAt( i );
-    if( child == actor )
-    {
-      return i;
-    }
+    mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
+    mTransitionAnimation.FinishedSignal().Connect( this, &Button::TransitionAnimationFinished );
   }
 
-  return childrenNum;
+  return mTransitionAnimation;
+}
+
+void Button::TransitionAnimationFinished( Dali::Animation& source )
+{
+  StopTransitionAnimation();
+  ResetImageLayers();
 }
 
 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
@@ -1467,7 +1247,7 @@ void Button::SetProperty( BaseObject* object, Property::Index index, const Prope
     {
       case Toolkit::Button::Property::DISABLED:
       {
-        GetImplementation( button ).SetDisabled( value.Get<bool>() );
+        GetImplementation( button ).SetDisabled( value.Get< bool >() );
         break;
       }
 
@@ -1577,7 +1357,7 @@ Property::Value Button::GetProperty( BaseObject* object, Property::Index propert
       case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
       {
         Property::Map map;
-        Scripting::CreatePropertyMap( GetImplementation( button ).mButtonContent, map );
+        Scripting::CreatePropertyMap( GetImplementation( button ).mUnselectedContent, map );
         value = map;
         break;
       }