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 c42531f..4072f43 100644 (file)
 #include "button-impl.h"
 
 // EXTERNAL INCLUDES
+#include <cstring> // for strcmp
 #include <dali/public-api/events/touch-event.h>
 #include <dali/public-api/object/type-registry.h>
-#include <dali/public-api/object/type-registry-helper.h>
+#include <dali/devel-api/object/type-registry-helper.h>
 #include <dali/public-api/actors/image-actor.h>
-#include <dali/public-api/scripting/scripting.h>
+#include <dali/devel-api/scripting/scripting.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
@@ -31,7 +32,7 @@
 /**
  * Button states and contents
  *                                         (3) mSelectedContent
- *  (2) mButtonContent                     (2) mSelectedBackgroundContent
+ *  (2) mUnselectedContent                 (2) mSelectedBackgroundContent
  *  (1) mBackgroundContent                 (1) mBackgroundContent
  * < unselected > ----------------------- < selected >
  *       |                OnSelect()            |
@@ -44,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
  *
@@ -73,23 +74,23 @@ BaseHandle Create()
 // Setup properties, signals and actions using the type-registry.
 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create );
 
-DALI_PROPERTY_REGISTRATION( Button, "disabled",                     BOOLEAN, DISABLED                     )
-DALI_PROPERTY_REGISTRATION( Button, "auto-repeating",               BOOLEAN, AUTO_REPEATING               )
-DALI_PROPERTY_REGISTRATION( Button, "initial-auto-repeating-delay", FLOAT,   INITIAL_AUTO_REPEATING_DELAY )
-DALI_PROPERTY_REGISTRATION( Button, "next-auto-repeating-delay",    FLOAT,   NEXT_AUTO_REPEATING_DELAY    )
-DALI_PROPERTY_REGISTRATION( Button, "togglable",                    BOOLEAN, TOGGLABLE                    )
-DALI_PROPERTY_REGISTRATION( Button, "selected",                     BOOLEAN, SELECTED                     )
-DALI_PROPERTY_REGISTRATION( Button, "normal-state-actor",           MAP,     NORMAL_STATE_ACTOR           )
-DALI_PROPERTY_REGISTRATION( Button, "selected-state-actor",         MAP,     SELECTED_STATE_ACTOR         )
-DALI_PROPERTY_REGISTRATION( Button, "disabled-state-actor",         MAP,     DISABLED_STATE_ACTOR         )
-DALI_PROPERTY_REGISTRATION( Button, "label-actor",                  MAP,     LABEL_ACTOR                  )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled",                     BOOLEAN, DISABLED                     )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "auto-repeating",               BOOLEAN, AUTO_REPEATING               )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "initial-auto-repeating-delay", FLOAT,   INITIAL_AUTO_REPEATING_DELAY )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "next-auto-repeating-delay",    FLOAT,   NEXT_AUTO_REPEATING_DELAY    )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "togglable",                    BOOLEAN, TOGGLABLE                    )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected",                     BOOLEAN, SELECTED                     )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "normal-state-actor",           MAP,     NORMAL_STATE_ACTOR           )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected-state-actor",         MAP,     SELECTED_STATE_ACTOR         )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled-state-actor",         MAP,     DISABLED_STATE_ACTOR         )
+DALI_PROPERTY_REGISTRATION( Toolkit, Button, "label-actor",                  MAP,     LABEL_ACTOR                  )
 
-DALI_SIGNAL_REGISTRATION(   Button, "pressed",                               SIGNAL_PRESSED               )
-DALI_SIGNAL_REGISTRATION(   Button, "released",                              SIGNAL_RELEASED              )
-DALI_SIGNAL_REGISTRATION(   Button, "clicked",                               SIGNAL_CLICKED               )
-DALI_SIGNAL_REGISTRATION(   Button, "state-changed",                         SIGNAL_STATE_CHANGED         )
+DALI_SIGNAL_REGISTRATION(   Toolkit, Button, "pressed",                               SIGNAL_PRESSED               )
+DALI_SIGNAL_REGISTRATION(   Toolkit, Button, "released",                              SIGNAL_RELEASED              )
+DALI_SIGNAL_REGISTRATION(   Toolkit, Button, "clicked",                               SIGNAL_CLICKED               )
+DALI_SIGNAL_REGISTRATION(   Toolkit, Button, "state-changed",                         SIGNAL_STATE_CHANGED         )
 
-DALI_ACTION_REGISTRATION(   Button, "button-click",                          ACTION_BUTTON_CLICK          )
+DALI_ACTION_REGISTRATION(   Toolkit, Button, "button-click",                          ACTION_BUTTON_CLICK          )
 
 DALI_TYPE_REGISTRATION_END()
 
@@ -116,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 );
-
-        animationStarted = OnDisabled();
+    return;
+  }
 
-        if( animationStarted )
-        {
-          mPaintState = DisabledSelectedTransition;
-        }
-        else
-        {
-          mPaintState = SelectedState;
-        }
-        break;
-      }
-      case UnselectedSelectedTransition:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mDisabledSelectedContent );
+  StopTransitionAnimation();
 
-        if( mBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
+  mDisabled = disabled;
 
-        InsertChild( backgroundIndex, mDisabledBackgroundContent );
+  // Notifies the derived class the button has been disabled.
+  OnDisabled();
 
-        animationStarted = OnDisabled();
+  switch( mPaintState )
+  {
+    case UnselectedState:
+    {
+      //Layer Order
+      //(3) mDisabledContent (Inserted)
+      //(4) mUnselectedContent
+      //(2) mDisabledBackgroundContent (Inserted)
+      //(1) mBackgroundContent
 
-        if( animationStarted )
-        {
-          mPaintState = SelectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledSelectedState;
-        }
-        break;
-      }
-      case SelectedUnselectedTransition:
-      {
-        buttonIndex = FindChildIndex( mLabel );
-        InsertChild( buttonIndex, mDisabledContent );
+      TransitionInBetween( mUnselectedContent, mLabel, mDisabledContent );
+      TransitionInAbove( mBackgroundContent, mDisabledBackgroundContent );
 
-        if( mBackgroundContent )
-        {
-          backgroundIndex = 1;
-        }
-        else
-        {
-          backgroundIndex = 0;
-        }
+      TransitionOut( mUnselectedContent );
+      TransitionOut( mSelectedContent );
+      TransitionOut( mBackgroundContent );
+      TransitionOut( mSelectedBackgroundContent );
+      TransitionOut( mDisabledSelectedContent );
 
-        InsertChild( backgroundIndex, mDisabledBackgroundContent );
+      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 );
 
-        animationStarted = OnDisabled();
+      mPaintState = DisabledSelectedState;
+      break;
+    }
+    case DisabledUnselectedState:
+    {
+      //Layer Order
+      //(3) mUnselectedContent (Inserted)
+      //(4) mDisabledContent
+      //(2) mBackgroundContent (Inserted)
+      //(1) mDisabledBackgroundContent
 
-        if( animationStarted )
-        {
-          mPaintState = UnselectedDisabledTransition;
-        }
-        else
-        {
-          mPaintState = DisabledUnselectedState;
-        }
-        break;
-      }
-      case UnselectedDisabledTransition:
-      {
-        animationStarted = OnDisabled();
+      TransitionInBetween( mDisabledContent, mLabel, mUnselectedContent );
+      TransitionInAbove( mDisabledBackgroundContent, mBackgroundContent );
 
-        if( animationStarted )
-        {
-          mPaintState = DisabledUnselectedTransition;
-        }
-        else
-        {
-          mPaintState = UnselectedState;
-        }
-        break;
-      }
-      case DisabledUnselectedTransition:
-      {
-        animationStarted = OnDisabled();
+      TransitionOut( mSelectedContent );
+      TransitionOut( mSelectedBackgroundContent );
+      TransitionOut( mDisabledContent );
+      TransitionOut( mDisabledSelectedContent );
+      TransitionOut( mDisabledBackgroundContent );
 
-        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
@@ -439,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 );
-
-      if( mBackgroundContent )
-      {
-        backgroundIndex = 1;
-      }
-      else
-      {
-        backgroundIndex = 0;
-      }
+      //Layer Order
+      //(3) mSelectedContent (Inserted)
+      //(4) mUnselectedContent
+      //(2) mSelectedBackgroundContent (Inserted)
+      //(1) mBackgroundContent
 
-      InsertChild( backgroundIndex, mSelectedBackgroundContent );
+      TransitionInBetween( mUnselectedContent, mLabel, mSelectedContent );
+      TransitionInAbove( mBackgroundContent, mSelectedBackgroundContent );
+      TransitionInAtIndex( 0, mBackgroundContent );
 
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
+      TransitionOut( mUnselectedContent );
+      TransitionOut( mDisabledContent );
+      TransitionOut( mDisabledSelectedContent );
+      TransitionOut( mDisabledBackgroundContent );
 
-      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();
+      //Layer Order
+      //(3) mUnselectedContent (Inserted)
+      //(2) mSelectedContent
+      //(1) mBackgroundContent
 
-      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;
-      }
-
-      InsertChild( backgroundIndex, mSelectedBackgroundContent );
-
-      // Notifies the derived class the button has been selected.
-      animationStarted = OnSelected();
-
-      if( animationStarted )
-      {
-        mPaintState = UnselectedSelectedTransition;
-      }
-      else
-      {
-        mPaintState = SelectedState;
-      }
-      break;
-    }
-    case DisabledSelectedTransition:
-    {
-      buttonIndex = FindChildIndex( mLabel );
-      InsertChild( buttonIndex, mButtonContent );
+      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 = 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() );
@@ -652,42 +420,39 @@ 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();
 }
 
 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() )
   {
@@ -695,19 +460,16 @@ 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();
 }
 
 Actor Button::GetSelectedImage() const
@@ -722,7 +484,7 @@ Actor& Button::GetSelectedImage()
 
 void Button::SetBackgroundImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mBackgroundContent && mBackgroundContent.GetParent() )
   {
@@ -730,17 +492,16 @@ 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();
 }
 
 Actor Button::GetBackgroundImage() const
@@ -755,7 +516,7 @@ Actor& Button::GetBackgroundImage()
 
 void Button::SetSelectedBackgroundImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
   {
@@ -763,24 +524,16 @@ 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();
 }
 
 Actor Button::GetSelectedBackgroundImage() const
@@ -795,7 +548,7 @@ Actor& Button::GetSelectedBackgroundImage()
 
 void Button::SetDisabledImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mDisabledContent && mDisabledContent.GetParent() )
   {
@@ -803,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();
 }
 
@@ -830,7 +579,7 @@ Actor& Button::GetDisabledImage()
 
 void Button::SetDisabledSelectedImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
   {
@@ -838,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();
 }
 
@@ -865,7 +610,7 @@ Actor& Button::GetDisabledSelectedImage()
 
 void Button::SetDisabledBackgroundImage( Actor image )
 {
-  StopAllAnimations();
+  StopTransitionAnimation();
 
   if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
   {
@@ -873,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();
 }
 
@@ -896,7 +638,7 @@ Actor& Button::GetDisabledBackgroundImage()
   return mDisabledBackgroundContent;
 }
 
-bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
+bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
 {
   bool ret = false;
 
@@ -908,14 +650,13 @@ bool Button::DoAction( BaseObject* object, const std::string& actionName, const
 
   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 PropertyValueContainer& 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.
@@ -926,60 +667,11 @@ void Button::DoClickAction( const PropertyValueContainer& attributes )
     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()
@@ -1194,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
-  PropertyValueContainer attributes;
-  DoClickAction( attributes );
+  return OnKeyboardEnter();
+}
+
+bool Button::OnKeyboardEnter()
+{
+  // When the enter key is pressed, or button is activated, the click action is performed.
+  Property::Map attributes;
+  bool ret = DoClickAction( attributes );
+
+  return ret;
 }
 
 void Button::OnControlStageDisconnection()
@@ -1246,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 ) )
+      {
+        ++index;
+      }
+      if( InsertButtonImage( index, mSelectedContent ) )
       {
-        mPaintState = UnselectedState;
+        ++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()
-{
-  return mState;
-}
-
-Button::PaintState Button::GetPaintState()
-{
-  return mPaintState;
-}
-
-void Button::InsertChild( unsigned int index, Actor& actor )
+void Button::StartTransitionAnimation()
 {
-  if( actor )
+  if( mTransitionAnimation )
+  {
+    mTransitionAnimation.Play();
+  }
+  else
   {
-    Self().Insert( index, actor);
+    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 )
@@ -1458,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;
       }
 
@@ -1568,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;
       }