1 #ifndef __DALI_TOOLKIT_INTERNAL_BUTTON_H__
2 #define __DALI_TOOLKIT_INTERNAL_BUTTON_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/adaptor-framework/timer.h>
23 #include <dali/public-api/animation/animation.h>
26 #include <dali-toolkit/public-api/controls/buttons/button.h>
27 #include <dali-toolkit/public-api/controls/control-impl.h>
41 * Button is the base class implementation for all buttons.
43 class Button : public Control
49 * Construct a new Button.
54 * A reference counted object may only be deleted by calling Unreference()
61 * @copydoc Dali::Toolkit::Button::SetDisabled( bool disabled )
63 void SetDisabled( bool disabled );
66 * @copydoc Dali::Toolkit::Button::IsDisabled() const
68 bool IsDisabled() const;
71 * @copydoc Dali::Toolkit::Button::SetAutoRepeating( bool autoRepeating )
73 void SetAutoRepeating( bool autoRepeating );
76 * @copydoc Dali::Toolkit::Button::IsAutoRepeating() const
78 bool IsAutoRepeating() const;
81 * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
83 void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
86 * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay() const
88 float GetInitialAutoRepeatingDelay() const;
91 * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
93 void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
96 * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay() const
98 float GetNextAutoRepeatingDelay() const;
101 * @copydoc Dali::Toolkit::Button::SetTogglableButton( bool togglable )
103 void SetTogglableButton( bool togglable );
106 * @copydoc Dali::Toolkit::Button::IsTogglableButton() const
108 bool IsTogglableButton() const;
111 * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
113 void SetSelected( bool selected );
116 * @copydoc Dali::Toolkit::Button::IsSelected() const
118 bool IsSelected() const;
121 * @copydoc Dali::Toolkit::Button::SetAnimationTime()
123 void SetAnimationTime( float animationTime );
126 * @copydoc Dali::Toolkit::Button::GetAnimationTime()
128 float GetAnimationTime() const;
131 * @copydoc Dali::Toolkit::Button::SetLabelText
133 void SetLabelText( const std::string& label );
136 * @copydoc Dali::Toolkit::Button::GetLabelText
138 std::string GetLabelText() const;
141 * @copydoc Dali::Toolkit::PushButton::SetUnselectedImage
143 void SetUnselectedImage( const std::string& filename );
146 * @copydoc Dali::Toolkit::PushButton::SetSelectedImage
148 void SetSelectedImage( const std::string& filename );
151 * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage
153 void SetBackgroundImage( const std::string& filename );
156 * @copydoc Dali::Toolkit::PushButton::SetSelectedBackgroundImage
158 void SetSelectedBackgroundImage( const std::string& filename );
161 * @copydoc Dali::Toolkit::PushButton::SetDisabledImage
163 void SetDisabledImage( const std::string& filename );
166 * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage
168 void SetDisabledSelectedImage( const std::string& filename );
171 * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage
173 void SetDisabledBackgroundImage( const std::string& filename );
176 * @return The filename used for the button image.
178 std::string GetUnselectedImageFilename() const;
181 * @return The filename used for the selected image.
183 std::string GetSelectedImageFilename() const;
186 * @return The filename used for the background image.
188 std::string GetBackgroundImageFilename() const;
191 * @return The filename used for the selected background image.
193 std::string GetSelectedBackgroundImageFilename() const;
196 * @return The filename used for the disabled button image.
198 std::string GetDisabledImageFilename() const;
201 * @return The filename used for the disabled selected image.
203 std::string GetDisabledSelectedImageFilename() const;
206 * @return The filename used for the disabled background image.
208 std::string GetDisabledBackgroundImageFilename() const;
211 * Performs actions as requested using the action name.
212 * @param[in] object The object on which to perform the action.
213 * @param[in] actionName The action to perform.
214 * @param[in] attributes The attributes with which to perfrom this action.
215 * @return true if action has been accepted by this control
217 static bool DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes );
219 public: // Deprecated API
222 * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
224 void SetLabel( Actor label );
227 * @deprecated Sets the unselected image with an Actor.
228 * @param[in] image The Actor to use.
230 void SetButtonImage( Actor image );
233 * @deprecated Sets the selected image with an Actor.
234 * @param[in] image The Actor to use.
236 void SetSelectedImage( Actor image );
239 * @deprecated Sets the background image with an Actor.
240 * @param[in] image The Actor to use.
242 void SetBackgroundImage( Actor image );
245 * @deprecated Sets the selected background image with an Actor.
246 * @param[in] image The Actor to use.
248 void SetSelectedBackgroundImage( Actor image );
251 * @deprecated Sets the disabled image with an Actor.
252 * @param[in] image The Actor to use.
254 void SetDisabledImage( Actor image );
257 * @deprecated Sets the disabled selected image with an Actor.
258 * @param[in] image The Actor to use.
260 void SetDisabledSelectedImage( Actor image );
263 * @deprecated Sets the disabled background image with an Actor.
264 * @param[in] image The Actor to use.
266 void SetDisabledBackgroundImage( Actor image );
269 * @copydoc Dali::Toolkit::Button::GetButtonImage()
271 Actor GetButtonImage() const;
274 * @copydoc Dali::Toolkit::Button::GetSelectedImage()
276 Actor GetSelectedImage() const;
281 * @return A reference to the label actor.
283 Actor& GetLabelActor();
286 * @return A reference to the unselected button image.
288 Actor& GetUnselectedImage();
291 * @return A reference to the selected image.
293 Actor& GetSelectedImage();
296 * @return A reference to the background image.
298 Actor& GetBackgroundImage();
301 * @return A reference to the selected background image.
303 Actor& GetSelectedBackgroundImage();
306 * @return A reference to the disabled button image.
308 Actor& GetDisabledImage();
311 * @return A reference to the disabled selected image.
313 Actor& GetDisabledSelectedImage();
316 * @return A reference to the disabled background image.
318 Actor& GetDisabledBackgroundImage();
323 * Perform the click action to click the button.
324 * @param[in] attributes The attributes to perfrom this action.
325 * @return true if this control can perform action.
327 bool DoClickAction( const Property::Map& attributes );
330 * This method is called after the button initialization.
331 * Could be reimplemented in subclasses to provide specific behaviour.
333 virtual void OnButtonInitialize() { }
336 * This method is called when the label is set.
338 virtual void OnLabelSet() {}
341 * This method is called when the unselected button image is set
343 virtual void OnUnselectedImageSet() {}
346 * This method is called when the selected image is set
348 virtual void OnSelectedImageSet() {}
351 * This method is called when the background image is set
353 virtual void OnBackgroundImageSet() {}
356 * This method is called when the selected background image is set
358 virtual void OnSelectedBackgroundImageSet() {}
361 * This method is called when the disabled button image is set
363 virtual void OnDisabledImageSet() {}
366 * This method is called when the disabled selected image is set
368 virtual void OnDisabledSelectedImageSet() {}
371 * This method is called when the disabled background image is set
373 virtual void OnDisabledBackgroundImageSet() {}
376 * This method is called from the OnTouchEvent method when the button is down.
377 * Could be reimplemented in subclasses to provide specific behaviour.
379 virtual void OnButtonDown();
382 * This method is called from the OnTouchEvent method when the button is up.
383 * Could be reimplemented in subclasses to provide specific behaviour.
385 virtual void OnButtonUp();
388 * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
389 * more than one touch points are received.
390 * Could be reimplemented in subclasses to provide specific behaviour.
392 virtual void OnTouchPointLeave();
395 * This method is called from the OnTouchEvent method when the touch point is interrupted.
396 * Could be reimplemented in subclasses to provide specific behaviour.
398 virtual void OnTouchPointInterrupted();
401 * This method is called when the button is removed from the stage.
402 * Could be reimplemented in subclasses to provide specific behaviour.
404 virtual void OnButtonStageDisconnection();
407 * This method is called when the \e selected property is changed.
409 virtual void OnSelected() {}
412 * This method is called when the \e disabled property is changed.
414 virtual void OnDisabled() {}
417 * This method is called when the button is pressed.
419 virtual void OnPressed() {}
422 * This method is called when the button is released.
424 virtual void OnReleased() {}
429 * @copydoc Dali::Toolkit::PushButton::PressedSignal()
431 Toolkit::Button::ButtonSignalType& PressedSignal();
434 * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
436 Toolkit::Button::ButtonSignalType& ReleasedSignal();
439 * @copydoc Dali::Toolkit::Button::ClickedSignal()
441 Toolkit::Button::ButtonSignalType& ClickedSignal();
444 * @copydoc Dali::Toolkit::Button::StateChangedSignal()
446 Toolkit::Button::ButtonSignalType& StateChangedSignal();
449 * Connects a callback function with the object's signals.
450 * @param[in] object The object providing the signal.
451 * @param[in] tracker Used to disconnect the signal.
452 * @param[in] signalName The signal to connect to.
453 * @param[in] functor A newly allocated FunctorDelegate.
454 * @return True if the signal was connected.
455 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
457 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
462 * Called when a property of an object of this type is set.
463 * @param[in] object The object whose property is set.
464 * @param[in] index The property index.
465 * @param[in] value The new property value.
467 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
470 * Called to retrieve a property of an object of this type.
471 * @param[in] object The object whose property is to be retrieved.
472 * @param[in] index The property index.
473 * @return The current value of the property.
475 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
477 protected: // From CustomActorImpl
480 * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
482 virtual bool OnTouchEvent( const TouchEvent& event );
484 private: // From Control
487 * @copydoc Toolkit::Control::OnInitialize()
489 virtual void OnInitialize();
492 * @copydoc Toolkit::Control::OnAccessibilityActivated()
494 virtual bool OnAccessibilityActivated();
497 * @copydoc Toolkit::Control::OnKeyboardEnter()
499 virtual bool OnKeyboardEnter();
502 * Callback received when the button is disconnected from the stage.
503 * It resets the button status.
505 void OnControlStageDisconnection();
510 * Handler for tap events.
511 * We do not actually do anything when we receive a tap as the button handles tap event through
512 * the touch event system itself as it requires more than just tap handling (e.g. leave events).
513 * This stops any of our parents receiving a tap gesture when it occurs within our area.
514 * @param[in] actor The tapped actor.
515 * @param[in] tap The tap gesture.
517 void OnTap(Actor actor, const TapGesture& tap);
520 * Sets up the autorepeating timer.
521 * @param[in] delay The delay time in seconds.
523 void SetUpTimer( float delay );
526 * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
528 bool AutoRepeatingSlot();
531 * Sets the button as selected or unselected.
532 * @param[in] selected \e selected property value.
533 * @param[in] emitSignal Emit a signal if this value is \e true.
535 void SetSelected( bool selected, bool emitSignal );
538 * This method is called when the button is pressed.
543 * This method is called when the button is released.
548 * Used to perform common setup applied to images within button.
549 * This will replace the current image with the specifed one.
550 * @param[in] actorToModify The image to replace.
551 * @param[out] newActor The new image to use.
553 void SetupContent( Actor& actorToModify, Actor newActor );
556 * Sets the color of the unselected image.
557 * If no image exists, it is created.
558 * @param[in] color The color to use.
560 void SetUnselectedColor( const Vector4& color );
563 * Gets the unselected content color.
564 * @return The currently used unselected color.
566 const Vector4 GetUnselectedColor() const;
569 * Sets the color of the selected image.
570 * If no image exists, it is created.
571 * @param[in] color The color to use.
573 void SetSelectedColor( const Vector4& color );
576 * Gets the selected content color.
577 * @return The currently used selected color.
579 const Vector4 GetSelectedColor() const;
585 ButtonUp, ///< The button is up.
586 ButtonDown, ///< The button is down.
590 * Button paint states.
594 UnselectedState, ///< The button is unselected.
595 SelectedState, ///< The button is selected.
596 DisabledUnselectedState, ///< The button is disabled and unselected.
597 DisabledSelectedState, ///< The button is disabled and selected.
601 * Enum to specify which decoration when getting and setting decorations.
605 UNSELECTED_DECORATION = 0,
610 ButtonState GetState();
611 PaintState GetPaintState();
612 void SetDecoration( DecorationState state, Actor actor );
613 Actor& GetDecoration( DecorationState state );
617 * Returns the animation to be used for transitioning creating the animation if needed.
618 * @return The initialised transition animation.
620 Dali::Animation GetTransitionAnimation();
623 * Prepares the actor to be transitioned in.
624 * @param[in] actor The actor that will be transitioned in.
626 virtual void PrepareForTranstionIn( Actor actor ) {}
629 * Prepares the actor to be transitioned in.
630 * @param[in] actor The actor that will be transitioned out.
632 virtual void PrepareForTranstionOut( Actor actor ) {}
635 * Transitions the actor in, allowing derived classes to configure
636 * the GetTransitionAnimation() animation ready.
637 * Button is in charge of calling Dali::Animation::Play and so derived classes
638 * only need to add the animation.
640 virtual void OnTransitionIn( Actor actor ) {}
643 * Transitions the actor out, allowing derived classes to configure
644 * the GetTransitionAnimation() animation ready.
645 * Button is in charge of calling Dali::Animation::Play and so derived classes
646 * only need to add the animation.
648 virtual void OnTransitionOut( Actor actor ) {}
653 * Starts the transition animation.
654 * Button::TransitionFinished is called when the animation finishes.
656 void StartTransitionAnimation();
659 * This method stops all transition animations
661 void StopTransitionAnimation();
664 * Called when the transition animation finishes.
666 void TransitionAnimationFinished( Dali::Animation& source );
669 * Resets the Button to the base state for the current paint state.
670 * Any additionally inserted images needed for transitions that are
671 * no longer needed and the removed.
673 void ResetImageLayers();
676 * Transitions out the actor
678 void TransitionOut( Actor actor );
681 * Removes the actor from the button and prepares it to be transitioned out
683 void RemoveButtonImage( Actor& actor );
686 * Finds the index of the actor.
687 * If the actor doesn't exist, return the last index + 1.
689 unsigned int FindChildIndex( Actor& actor );
692 * Adds an actor to the hierarchy and prepares it to be transitioned.
693 * @param[in] actor The actor to add
695 void PrepareAddButtonImage( Actor& actor );
698 * Adds an actor to the hierarchy and marks it to be transitioned.
699 * @param[in] actor The actor to add
701 void TransitionButtonImage( Actor& actor );
704 * Adds an actor to the hierarchy.
705 * @param[in] actor The actor to add
707 void AddButtonImage( Actor& actor );
710 * (Re)Adds the label (if exists) to the hierarchy (so it is always on top).
715 Button( const Button& );
718 Button& operator = ( const Button& );
723 Toolkit::Button::ButtonSignalType mPressedSignal; ///< Signal emitted when the button is pressed.
724 Toolkit::Button::ButtonSignalType mReleasedSignal; ///< Signal emitted when the button is released.
725 Toolkit::Button::ButtonSignalType mClickedSignal; ///< Signal emitted when the button is clicked.
726 Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
728 Timer mAutoRepeatingTimer; ///< Timer used to implement the autorepeating property.
730 Actor mLabel; ///< Stores the button label.
732 Actor mDecoration[ DECORATION_STATES ]; ///< Stores the decorations for both selected and unselected states.
734 Actor mUnselectedContent; ///< Stores the unselected content.
735 Actor mSelectedContent; ///< Stores the selected content.
736 Actor mBackgroundContent; ///< Stores the background content.
737 Actor mSelectedBackgroundContent; ///< Stores the selected background content.
738 Actor mDisabledContent; ///< Stores the disabled content.
739 Actor mDisabledSelectedContent; ///< Stores the disabled selected content.
740 Actor mDisabledBackgroundContent; ///< Stores the disabled background content.
742 Animation mTransitionAnimation; ///< Animation used in the state transitions.
744 TapGestureDetector mTapDetector;
746 Vector4 mUnselectedColor; ///< Color to use for unselected content.
747 Vector4 mSelectedColor; ///< Color to use for selected content.
749 bool mDisabled; ///< Stores the disabled property.
750 bool mAutoRepeating; ///< Stores the autorepeating property.
751 bool mTogglableButton; ///< Stores the togglable property.
752 bool mSelected; ///< Stores the selected state.
753 float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
754 float mNextAutoRepeatingDelay; ///< Stores the next autorepeating delay in seconds.
756 float mAnimationTime; ///< The animation time.
759 bool mClickActionPerforming;
761 ButtonState mState; ///< Stores the button state.
762 PaintState mPaintState; ///< Stores the paint state.
765 } // namespace Internal
767 // Helpers for public-api forwarding methods
769 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
771 DALI_ASSERT_ALWAYS( button );
773 Dali::RefObject& handle = button.GetImplementation();
775 return static_cast<Toolkit::Internal::Button&>( handle );
778 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
780 DALI_ASSERT_ALWAYS( button );
782 const Dali::RefObject& handle = button.GetImplementation();
784 return static_cast<const Toolkit::Internal::Button&>( handle );
787 } // namespace Toolkit
791 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__