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.h
index f0884e1..89f0591 100644 (file)
 
 // EXTERNAL INCLUDES
 #include <dali/public-api/adaptor-framework/timer.h>
+#include <dali/public-api/animation/animation.h>
 
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/buttons/button.h>
 #include <dali-toolkit/public-api/controls/control-impl.h>
-#include "button-painter-impl.h"
 
 namespace Dali
 {
@@ -42,14 +42,6 @@ namespace Internal
  */
 class Button : public Control
 {
-public:
-
-  // Properties
-  enum
-  {
-    BUTTON_PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
-    BUTTON_PROPERTY_END_INDEX = BUTTON_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
-  };
 
 protected:
 
@@ -118,7 +110,7 @@ public:
   /**
    * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
    */
-  virtual void SetSelected( bool selected );
+  void SetSelected( bool selected );
 
   /**
    * @copydoc Dali::Toolkit::Button::IsSelected() const
@@ -143,7 +135,7 @@ public:
   /**
    * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
    */
-  virtual void SetLabel( Actor label );   // TODO: After refactoring painter, virtual will be removed
+  void SetLabel( Actor label );
 
   /**
    * @copydoc Dali::Toolkit::Button::GetLabel()
@@ -151,17 +143,6 @@ public:
   Actor GetLabel() const;
 
   /**
-   * Used by the painter only.
-   * @return A reference to the label actor.
-   */
-  Actor& GetLabel();
-
-  /**
-   * @copydoc Dali::Toolkit::PushButton::SetButtonImage( const Image image )
-   */
-  void SetButtonImage( Image image );
-
-  /**
    * @copydoc Dali::Toolkit::PushButton::SetButtonImage( Actor image )
    */
   void SetButtonImage( Actor image );
@@ -172,17 +153,12 @@ public:
   Actor GetButtonImage() const;
 
   /**
-   * Used by the painter only.
+   * Internal use only.
    * @return A reference to the button image.
    */
   Actor& GetButtonImage();
 
   /**
-   * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( const Image image )
-   */
-  void SetSelectedImage( Image image );
-
-  /**
    * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( Actor image )
    */
   void SetSelectedImage( Actor image );
@@ -193,17 +169,12 @@ public:
   Actor GetSelectedImage() const;
 
   /**
-   * Used by the painter only.
+   * Internal use only.
    * @return A reference to the selected image.
    */
   Actor& GetSelectedImage();
 
   /**
-   * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( const Image image )
-   */
-  void SetBackgroundImage( Image image );
-
-  /**
    * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( Actor image )
    */
   void SetBackgroundImage( Actor image );
@@ -214,15 +185,26 @@ public:
   Actor GetBackgroundImage() const;
 
   /**
-   * Used by the painter only.
+   * Internal use only.
    * @return A reference to the background image.
    */
   Actor& GetBackgroundImage();
 
   /**
-   * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Image image )
+   * @copydoc Dali::Toolkit::PushButton::SetSelectedBackgroundImage( Actor image )
    */
-  void SetDisabledImage( Image image );
+  void SetSelectedBackgroundImage( Actor image );
+
+  /**
+   * @copydoc Dali::Toolkit::PushButton::GetSelectedBackgroundImage()
+   */
+  Actor GetSelectedBackgroundImage() const;
+
+  /**
+   * Internal use only.
+   * @return A reference to the selected background image.
+   */
+  Actor& GetSelectedBackgroundImage();
 
   /**
    * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Actor image )
@@ -235,17 +217,12 @@ public:
   Actor GetDisabledImage() const;
 
   /**
-   * Used by the painter only.
+   * Internal use only.
    * @return A reference to the disabled button image.
    */
   Actor& GetDisabledImage();
 
   /**
-   * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Image image )
-   */
-  void SetDisabledSelectedImage( Image image );
-
-  /**
    * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Actor image )
    */
   void SetDisabledSelectedImage( Actor image );
@@ -256,17 +233,12 @@ public:
   Actor GetDisabledSelectedImage() const;
 
   /**
-   * Used by the painter only.
+   * Internal use only.
    * @return A reference to the disabled selected image.
    */
   Actor& GetDisabledSelectedImage();
 
   /**
-   * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Image image )
-   */
-  void SetDisabledBackgroundImage( Image image );
-
-  /**
    * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Actor image )
    */
   void SetDisabledBackgroundImage( Actor image );
@@ -277,44 +249,35 @@ public:
   Actor GetDisabledBackgroundImage() const;
 
   /**
-   * Used by the painter only.
+   * Internal use only.
    * @return A reference to the disabled background image.
    */
   Actor& GetDisabledBackgroundImage();
 
   /**
-   * Used by the painter only.
-   * @return A reference to the button image that is fading out.
-   */
-  Actor& GetFadeOutButtonImage();
-
-  /**
-   * Used by the painter only.
-   * @return A reference to the selected image that is fading out.
-   */
-  Actor& GetFadeOutSelectedImage();
-
-  /**
-   * Used by the painter only.
-   * @return A reference to the background image that is fading out.
-   */
-  Actor& GetFadeOutBackgroundImage();
-
-  /**
    * Performs actions as requested using the action name.
    * @param[in] object The object on which to perform the action.
    * @param[in] actionName The action to perform.
    * @param[in] attributes The attributes with which to perfrom this action.
    * @return true if action has been accepted by this control
    */
-  static bool DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes );
+  static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
+
+protected:
+
+  /**
+   * @return A reference to the label actor.
+   */
+  Actor& GetLabel();
 
 private:
+
   /**
    * Perform the click action to click the button.
    * @param[in] attributes The attributes to perfrom this action.
+   * @return true if this control can perform action.
    */
-  void DoClickAction( const PropertyValueContainer& attributes );
+  bool DoClickAction( const Property::Map& attributes );
 
   /**
    * This method is called after the button initialization.
@@ -323,6 +286,46 @@ private:
   virtual void OnButtonInitialize() { }
 
   /**
+   * This method is called when the label is set.
+   */
+  virtual void OnLabelSet() {}
+
+  /**
+   * This method is called when the button image is set
+   */
+  virtual void OnButtonImageSet() {}
+
+  /**
+   * This method is called when the selected image is set
+   */
+  virtual void OnSelectedImageSet() {}
+
+  /**
+   * This method is called when the background image is set
+   */
+  virtual void OnBackgroundImageSet() {}
+
+  /**
+   * This method is called when the selected background image is set
+   */
+  virtual void OnSelectedBackgroundImageSet() {}
+
+  /**
+   * This method is called when the disabled button image is set
+   */
+  virtual void OnDisabledImageSet() {}
+
+  /**
+   * This method is called when the disabled selected image is set
+   */
+  virtual void OnDisabledSelectedImageSet() {}
+
+  /**
+   * This method is called when the disabled background image is set
+   */
+  virtual void OnDisabledBackgroundImageSet() {}
+
+  /**
    * This method is called from the OnTouchEvent method when the button is down.
    * Could be reimplemented in subclasses to provide specific behaviour.
    */
@@ -348,24 +351,30 @@ private:
   virtual void OnTouchPointInterrupted();
 
   /**
-   * This method is called when the animation time is set.
-   * Needs to be reimplemented in subclasses to set the animation time in different buttons.
-   * @param animationTime The animation time in seconds.
+   * This method is called when the button is removed from the stage.
+   * Could be reimplemented in subclasses to provide specific behaviour.
+   */
+  virtual void OnButtonStageDisconnection();
+
+  /**
+   * This method is called when the \e selected property is changed.
    */
-  virtual void OnAnimationTimeSet( float animationTime );
+  virtual void OnSelected() {}
 
   /**
-   * This method is called when the animation time is requested.
-   * Needs to be reimplemented in subclases to return the animation time.
-   * @return The animation time in seconds.
+   * This method is called when the \e disabled property is changed.
    */
-  virtual float OnAnimationTimeRequested() const;
+  virtual void OnDisabled() {}
 
   /**
-   * This method is called when the button is removed from the stage.
-   * Could be reimplemented in subclasses to provide specific behaviour.
+   * This method is called when the button is pressed.
    */
-  virtual void OnButtonStageDisconnection();
+  virtual void OnPressed() {}
+
+  /**
+   * This method is called when the button is released.
+   */
+  virtual void OnReleased() {}
 
 public:
 
@@ -433,14 +442,14 @@ private: // From Control
   virtual void OnInitialize();
 
   /**
-   * @copydoc Toolkit::Control::OnActivated()
+   * @copydoc Toolkit::Control::OnAccessibilityActivated()
    */
-  virtual void OnActivated();
+  virtual bool OnAccessibilityActivated();
 
   /**
-   * @copydoc Toolkit::Control::OnControlSizeSet( const Vector3& targetSize )
+   * @copydoc Toolkit::Control::OnKeyboardEnter()
    */
-  virtual void OnControlSizeSet( const Vector3& targetSize );
+  virtual bool OnKeyboardEnter();
 
   /**
    * Callback received when the button is disconnected from the stage.
@@ -471,6 +480,23 @@ private:
    */
   bool AutoRepeatingSlot();
 
+  /**
+   * Sets the button as selected or unselected.
+   * @param[in] selected \e selected property value.
+   * @param[in] emitSignal Emit a signal if this value is \e true.
+   */
+  void SetSelected( bool selected, bool emitSignal );
+
+  /**
+   * This method is called when the button is pressed.
+   */
+  void Pressed();
+
+  /**
+   * This method is called when the button is released.
+   */
+  void Released();
+
 protected:
 
   enum ButtonState
@@ -479,15 +505,118 @@ protected:
     ButtonDown,                                ///< The button is down.
   };
 
-  void SetPainter(ButtonPainterPtr painter);
+  /**
+   * Button paint states.
+   */
+  enum PaintState
+  {
+    UnselectedState,              ///< The button is unselected.
+    SelectedState,                ///< The button is selected.
+    DisabledUnselectedState,      ///< The button is disabled and unselected.
+    DisabledSelectedState,        ///< The button is disabled and selected.
+  };
 
   ButtonState GetState();
+  PaintState GetPaintState();
+
+  /**
+   * Returns the animation to be used for transitioning creating the animation if needed.
+   * @return The initialised transition animation.
+   */
+  Dali::Animation GetTransitionAnimation();
+
+  /**
+   * Prepares the actor to be transitioned in.
+   * @param[in]  actor  The actor that will be transitioned in.
+   */
+  virtual void PrepareForTranstionIn( Actor actor ) {}
 
-  Actor mLabel;                                //TODO: After refactoring painter, this will be private
-  bool  mTogglableButton;                      //TODO: After refactoring painter, this will be private
-  bool  mSelected;                             //TODO: After refactoring painter, this will be private
+  /**
+   * Prepares the actor to be transitioned in.
+   * @param[in]  actor  The actor that will be transitioned out.
+   */
+  virtual void PrepareForTranstionOut( Actor actor ) {}
+
+  /**
+   * Transitions the actor in, allowing derived classes to configure
+   * the GetTransitionAnimation() animation ready.
+   * Button is in charge of calling Dali::Animation::Play and so derived classes
+   * only need to add the animation.
+   */
+  virtual void OnTransitionIn( Actor actor ) {}
+
+  /**
+   * Transitions the actor out, allowing derived classes to configure
+   * the GetTransitionAnimation() animation ready.
+   * Button is in charge of calling Dali::Animation::Play and so derived classes
+   * only need to add the animation.
+   */
+  virtual void OnTransitionOut( Actor actor ) {}
 
 private:
+  /**
+   * Starts the transition animation.
+   * Button::TransitionFinished is called when the animation finishes.
+   */
+  void StartTransitionAnimation();
+
+  /**
+   * This method stops all transition animations
+   */
+  void StopTransitionAnimation();
+
+  /**
+   * Called when the transition animation finishes.
+   */
+  void TransitionAnimationFinished( Dali::Animation& source );
+
+  /**
+   * Resets the Button to the base state for the current paint state.
+   * Any additionally inserted images needed for transitions that are
+   * no longer needed and the removed.
+   */
+  void ResetImageLayers();
+
+  /**
+   * Transitions in the actor, inserting the actor above childLower below the childUpper.
+   * Will not insert the actor if it is already attached to a parent (and so will not reorder the actor)
+   */
+  void TransitionInBetween( Actor childLower, Actor childUpper, Actor actor );
+
+  /**
+   * Transitions in the actor, inserting the actor above the child if the child exists or at the bottom otherwise
+   * Will not insert the actor if it is already attached to a parent (and so will not reorder the actor)
+   */
+  void TransitionInAbove( Actor child, Actor actor );
+
+  /**
+   * Transitions in the actor, inserting the actor at the index
+   * Will not insert the actor if it is already attached to a parent (and so will not reorder the actor)
+   */
+  void TransitionInAtIndex( unsigned int index, Actor actor );
+
+  /**
+   * Transitions out the actor
+   */
+  void TransitionOut( Actor actor );
+
+  /**
+   * Inserts the actor to the button and prepares it to be transitioned out
+   * @return true if the child was inserted, false otherwise
+   */
+  bool InsertButtonImage( unsigned int index, Actor& actor );
+
+  /**
+   * Removes the actor from the button and prepares it to be transitioned out
+   */
+  void RemoveButtonImage( Actor& actor );
+
+  /**
+   * Finds the index of the actor.
+   * If the actor doesn't exist, return the last index + 1.
+   */
+  unsigned int FindChildIndex( Actor& actor );
+
 
   // Undefined
   Button( const Button& );
@@ -497,47 +626,46 @@ private:
 
 private:
 
-  ButtonPainterPtr mPainter;                   ///< Pointer to a ButtonPainter base class.
-
   // Signals
   Toolkit::Button::ButtonSignalType mPressedSignal;           ///< Signal emitted when the button is pressed.
-  Toolkit::Button::ButtonSignalType mReleasedSignal;         ///< Signal emitted when the button is released.
+  Toolkit::Button::ButtonSignalType mReleasedSignal;          ///< Signal emitted when the button is released.
   Toolkit::Button::ButtonSignalType mClickedSignal;           ///< Signal emitted when the button is clicked.
-  Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
+  Toolkit::Button::ButtonSignalType mStateChangedSignal;      ///< Signal emitted when the button's state is changed.
 
   Timer mAutoRepeatingTimer;                   ///< Timer used to implement the autorepeating property.
 
-//  Actor mLabel;                                ///< Stores the button label.
+  Actor mLabel;                                ///< Stores the button label.
 
-  Actor mButtonImage;                          ///< Stores the unselected image.
-  Actor mSelectedImage;                        ///< Stores the selected image.
-  Actor mBackgroundImage;                      ///< Stores the background image.
-  Actor mDisabledImage;                        ///< Stores the disabled image.
-  Actor mDisabledSelectedImage;                ///< Stores the disabled selected image.
-  Actor mDisabledBackgroundImage;              ///< Stores the disabled background image.
+  Actor mUnselectedContent;                    ///< Stores the unselected content.
+  Actor mSelectedContent;                      ///< Stores the selected content.
+  Actor mBackgroundContent;                    ///< Stores the background content.
+  Actor mSelectedBackgroundContent;            ///< Stores the selected background content.
+  Actor mDisabledContent;                      ///< Stores the disabled content.
+  Actor mDisabledSelectedContent;              ///< Stores the disabled selected content.
+  Actor mDisabledBackgroundContent;            ///< Stores the disabled background content.
 
-  Actor mFadeOutButtonImage;                   ///< Stores a foreground image, which is in a fade out animation, to be removed when the animation finishes.
-  Actor mFadeOutSelectedImage;                 ///< Stores a foreground image, which is in a fade out animation, to be removed when the animation finishes.
-  Actor mFadeOutBackgroundImage;               ///< Stores a background image, which is in a fade out animation, to be removed when the animation finishes.
+  Animation        mTransitionAnimation;       ///< Animation used in the state transitions.
 
   TapGestureDetector mTapDetector;
 
   bool             mDisabled;                  ///< Stores the disabled property.
   bool             mAutoRepeating;             ///< Stores the autorepeating property.
-//  bool             mTogglableButton;           ///< Stores the togglable property.
-//  bool             mSelected;                  ///< Stores the selected state.
+  bool             mTogglableButton;           ///< Stores the togglable property.
+  bool             mSelected;                  ///< Stores the selected state.
   float            mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
   float            mNextAutoRepeatingDelay;    ///< Stores the next autorepeating delay in seconds.
 
+  float            mAnimationTime;             ///< The animation time.
+
   // Actions
   bool             mClickActionPerforming;
 
   ButtonState      mState;                     ///< Stores the button state.
+  PaintState       mPaintState;                ///< Stores the paint state.
 };
 
 } // namespace Internal
 
-
 // Helpers for public-api forwarding methods
 
 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
@@ -563,4 +691,3 @@ inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button
 } // namespace Dali
 
 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__
-