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>
25 #include <dali-toolkit/public-api/controls/buttons/button.h>
26 #include <dali-toolkit/public-api/controls/control-impl.h>
27 #include "button-painter-impl.h"
41 * Button is the base class implementation for all buttons.
43 class Button : public Control
50 BUTTON_PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
51 BUTTON_PROPERTY_END_INDEX = BUTTON_PROPERTY_START_INDEX + 1000 ///< Reserving 1000 property indices
57 * Construct a new Button.
62 * A reference counted object may only be deleted by calling Unreference()
69 * @copydoc Dali::Toolkit::Button::SetDisabled( bool disabled )
71 void SetDisabled( bool disabled );
74 * @copydoc Dali::Toolkit::Button::IsDisabled() const
76 bool IsDisabled() const;
79 * @copydoc Dali::Toolkit::Button::SetAutoRepeating( bool autoRepeating )
81 void SetAutoRepeating( bool autoRepeating );
84 * @copydoc Dali::Toolkit::Button::IsAutoRepeating() const
86 bool IsAutoRepeating() const;
89 * @copydoc Dali::Toolkit::Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
91 void SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay );
94 * @copydoc Dali::Toolkit::Button::GetInitialAutoRepeatingDelay() const
96 float GetInitialAutoRepeatingDelay() const;
99 * @copydoc Dali::Toolkit::Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
101 void SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay );
104 * @copydoc Dali::Toolkit::Button::GetNextAutoRepeatingDelay() const
106 float GetNextAutoRepeatingDelay() const;
109 * @copydoc Dali::Toolkit::Button::SetTogglableButton( bool togglable )
111 void SetTogglableButton( bool togglable );
114 * @copydoc Dali::Toolkit::Button::IsTogglableButton() const
116 bool IsTogglableButton() const;
119 * @copydoc Dali::Toolkit::Button::SetSelected( bool selected )
121 virtual void SetSelected( bool selected );
124 * @copydoc Dali::Toolkit::Button::IsSelected() const
126 bool IsSelected() const;
129 * @copydoc Dali::Toolkit::Button::SetAnimationTime()
131 void SetAnimationTime( float animationTime );
134 * @copydoc Dali::Toolkit::Button::GetAnimationTime()
136 float GetAnimationTime() const;
139 * @copydoc Dali::Toolkit::Button::SetLabel( const std::string& label )
141 void SetLabel( const std::string& label );
144 * @copydoc Dali::Toolkit::Button::SetLabel( Actor label )
146 virtual void SetLabel( Actor label ); // TODO: After refactoring painter, virtual will be removed
149 * @copydoc Dali::Toolkit::Button::GetLabel()
151 Actor GetLabel() const;
154 * Used by the painter only.
155 * @return A reference to the label actor.
160 * @copydoc Dali::Toolkit::PushButton::SetButtonImage( const Image image )
162 void SetButtonImage( Image image );
165 * @copydoc Dali::Toolkit::PushButton::SetButtonImage( Actor image )
167 void SetButtonImage( Actor image );
170 * @copydoc Dali::Toolkit::PushButton::GetButtonImage()
172 Actor GetButtonImage() const;
175 * Used by the painter only.
176 * @return A reference to the button image.
178 Actor& GetButtonImage();
181 * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( const Image image )
183 void SetSelectedImage( Image image );
186 * @copydoc Dali::Toolkit::PushButton::SetSelectedImage( Actor image )
188 void SetSelectedImage( Actor image );
191 * @copydoc Dali::Toolkit::PushButton::GetSelectedImage()
193 Actor GetSelectedImage() const;
196 * Used by the painter only.
197 * @return A reference to the selected image.
199 Actor& GetSelectedImage();
202 * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( const Image image )
204 void SetBackgroundImage( Image image );
207 * @copydoc Dali::Toolkit::PushButton::SetBackgroundImage( Actor image )
209 void SetBackgroundImage( Actor image );
212 * @copydoc Dali::Toolkit::PushButton::GetBackgroundImage()
214 Actor GetBackgroundImage() const;
217 * Used by the painter only.
218 * @return A reference to the background image.
220 Actor& GetBackgroundImage();
223 * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Image image )
225 void SetDisabledImage( Image image );
228 * @copydoc Dali::Toolkit::PushButton::SetDisabledImage( Actor image )
230 void SetDisabledImage( Actor image );
233 * @copydoc Dali::Toolkit::PushButton::GetDisabledImage()
235 Actor GetDisabledImage() const;
238 * Used by the painter only.
239 * @return A reference to the disabled button image.
241 Actor& GetDisabledImage();
244 * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Image image )
246 void SetDisabledSelectedImage( Image image );
249 * @copydoc Dali::Toolkit::CheckBoxButton::SetDisabledSelectedImage( Actor image )
251 void SetDisabledSelectedImage( Actor image );
254 * @copydoc Dali::Toolkit::CheckBoxButton::GetDisabledSelectedImage()
256 Actor GetDisabledSelectedImage() const;
259 * Used by the painter only.
260 * @return A reference to the disabled selected image.
262 Actor& GetDisabledSelectedImage();
265 * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Image image )
267 void SetDisabledBackgroundImage( Image image );
270 * @copydoc Dali::Toolkit::PushButton::SetDisabledBackgroundImage( Actor image )
272 void SetDisabledBackgroundImage( Actor image );
275 * @copydoc Dali::Toolkit::PushButton::GetDisabledBackgroundImage()
277 Actor GetDisabledBackgroundImage() const;
280 * Used by the painter only.
281 * @return A reference to the disabled background image.
283 Actor& GetDisabledBackgroundImage();
286 * Used by the painter only.
287 * @return A reference to the button image that is fading out.
289 Actor& GetFadeOutButtonImage();
292 * Used by the painter only.
293 * @return A reference to the selected image that is fading out.
295 Actor& GetFadeOutSelectedImage();
298 * Used by the painter only.
299 * @return A reference to the background image that is fading out.
301 Actor& GetFadeOutBackgroundImage();
304 * Performs actions as requested using the action name.
305 * @param[in] object The object on which to perform the action.
306 * @param[in] actionName The action to perform.
307 * @param[in] attributes The attributes with which to perfrom this action.
308 * @return true if action has been accepted by this control
310 static bool DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes );
314 * Perform the click action to click the button.
315 * @param[in] attributes The attributes to perfrom this action.
317 void DoClickAction( const PropertyValueContainer& attributes );
320 * This method is called after the button initialization.
321 * Could be reimplemented in subclasses to provide specific behaviour.
323 virtual void OnButtonInitialize() { }
326 * This method is called from the OnTouchEvent method when the button is down.
327 * Could be reimplemented in subclasses to provide specific behaviour.
329 virtual void OnButtonDown();
332 * This method is called from the OnTouchEvent method when the button is up.
333 * Could be reimplemented in subclasses to provide specific behaviour.
335 virtual void OnButtonUp();
338 * This method is called from the OnTouchEvent method when the touch point leaves the boundary of the button or
339 * more than one touch points are received.
340 * Could be reimplemented in subclasses to provide specific behaviour.
342 virtual void OnTouchPointLeave();
345 * This method is called from the OnTouchEvent method when the touch point is interrupted.
346 * Could be reimplemented in subclasses to provide specific behaviour.
348 virtual void OnTouchPointInterrupted();
351 * This method is called when the animation time is set.
352 * Needs to be reimplemented in subclasses to set the animation time in different buttons.
353 * @param animationTime The animation time in seconds.
355 virtual void OnAnimationTimeSet( float animationTime );
358 * This method is called when the animation time is requested.
359 * Needs to be reimplemented in subclases to return the animation time.
360 * @return The animation time in seconds.
362 virtual float OnAnimationTimeRequested() const;
365 * This method is called when the button is removed from the stage.
366 * Could be reimplemented in subclasses to provide specific behaviour.
368 virtual void OnButtonStageDisconnection();
373 * @copydoc Dali::Toolkit::PushButton::PressedSignal()
375 Toolkit::Button::ButtonSignalType& PressedSignal();
378 * @copydoc Dali::Toolkit::PushButton::ReleasedSignal()
380 Toolkit::Button::ButtonSignalType& ReleasedSignal();
383 * @copydoc Dali::Toolkit::Button::ClickedSignal()
385 Toolkit::Button::ButtonSignalType& ClickedSignal();
388 * @copydoc Dali::Toolkit::Button::StateChangedSignal()
390 Toolkit::Button::ButtonSignalType& StateChangedSignal();
393 * Connects a callback function with the object's signals.
394 * @param[in] object The object providing the signal.
395 * @param[in] tracker Used to disconnect the signal.
396 * @param[in] signalName The signal to connect to.
397 * @param[in] functor A newly allocated FunctorDelegate.
398 * @return True if the signal was connected.
399 * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
401 static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
406 * Called when a property of an object of this type is set.
407 * @param[in] object The object whose property is set.
408 * @param[in] index The property index.
409 * @param[in] value The new property value.
411 static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
414 * Called to retrieve a property of an object of this type.
415 * @param[in] object The object whose property is to be retrieved.
416 * @param[in] index The property index.
417 * @return The current value of the property.
419 static Property::Value GetProperty( BaseObject* object, Property::Index propertyIndex );
421 protected: // From CustomActorImpl
424 * @copydoc Dali::CustomActorImpl::OnTouchEvent( const TouchEvent& event )
426 virtual bool OnTouchEvent( const TouchEvent& event );
428 private: // From Control
431 * @copydoc Toolkit::Control::OnInitialize()
433 virtual void OnInitialize();
436 * @copydoc Toolkit::Control::OnActivated()
438 virtual void OnActivated();
441 * @copydoc Toolkit::Control::OnControlSizeSet( const Vector3& targetSize )
443 virtual void OnControlSizeSet( const Vector3& targetSize );
446 * Callback received when the button is disconnected from the stage.
447 * It resets the button status.
449 void OnControlStageDisconnection();
454 * Handler for tap events.
455 * We do not actually do anything when we receive a tap as the button handles tap event through
456 * the touch event system itself as it requires more than just tap handling (e.g. leave events).
457 * This stops any of our parents receiving a tap gesture when it occurs within our area.
458 * @param[in] actor The tapped actor.
459 * @param[in] tap The tap gesture.
461 void OnTap(Actor actor, const TapGesture& tap);
464 * Sets up the autorepeating timer.
465 * @param[in] delay The delay time in seconds.
467 void SetUpTimer( float delay );
470 * Slot called when Dali::Timer::SignalTick signal. Resets the autorepeating timer.
472 bool AutoRepeatingSlot();
478 ButtonUp, ///< The button is up.
479 ButtonDown, ///< The button is down.
482 void SetPainter(ButtonPainterPtr painter);
484 ButtonState GetState();
486 Actor mLabel; //TODO: After refactoring painter, this will be private
487 bool mTogglableButton; //TODO: After refactoring painter, this will be private
488 bool mSelected; //TODO: After refactoring painter, this will be private
493 Button( const Button& );
496 Button& operator = ( const Button& );
500 ButtonPainterPtr mPainter; ///< Pointer to a ButtonPainter base class.
503 Toolkit::Button::ButtonSignalType mPressedSignal; ///< Signal emitted when the button is pressed.
504 Toolkit::Button::ButtonSignalType mReleasedSignal; ///< Signal emitted when the button is released.
505 Toolkit::Button::ButtonSignalType mClickedSignal; ///< Signal emitted when the button is clicked.
506 Toolkit::Button::ButtonSignalType mStateChangedSignal; ///< Signal emitted when the button's state is changed.
508 Timer mAutoRepeatingTimer; ///< Timer used to implement the autorepeating property.
510 // Actor mLabel; ///< Stores the button label.
512 Actor mButtonImage; ///< Stores the unselected image.
513 Actor mSelectedImage; ///< Stores the selected image.
514 Actor mBackgroundImage; ///< Stores the background image.
515 Actor mDisabledImage; ///< Stores the disabled image.
516 Actor mDisabledSelectedImage; ///< Stores the disabled selected image.
517 Actor mDisabledBackgroundImage; ///< Stores the disabled background image.
519 Actor mFadeOutButtonImage; ///< Stores a foreground image, which is in a fade out animation, to be removed when the animation finishes.
520 Actor mFadeOutSelectedImage; ///< Stores a foreground image, which is in a fade out animation, to be removed when the animation finishes.
521 Actor mFadeOutBackgroundImage; ///< Stores a background image, which is in a fade out animation, to be removed when the animation finishes.
523 TapGestureDetector mTapDetector;
525 bool mDisabled; ///< Stores the disabled property.
526 bool mAutoRepeating; ///< Stores the autorepeating property.
527 // bool mTogglableButton; ///< Stores the togglable property.
528 // bool mSelected; ///< Stores the selected state.
529 float mInitialAutoRepeatingDelay; ///< Stores the initial autorepeating delay in seconds.
530 float mNextAutoRepeatingDelay; ///< Stores the next autorepeating delay in seconds.
533 bool mClickActionPerforming;
535 ButtonState mState; ///< Stores the button state.
538 } // namespace Internal
541 // Helpers for public-api forwarding methods
543 inline Toolkit::Internal::Button& GetImplementation( Toolkit::Button& button )
545 DALI_ASSERT_ALWAYS( button );
547 Dali::RefObject& handle = button.GetImplementation();
549 return static_cast<Toolkit::Internal::Button&>( handle );
552 inline const Toolkit::Internal::Button& GetImplementation( const Toolkit::Button& button )
554 DALI_ASSERT_ALWAYS( button );
556 const Dali::RefObject& handle = button.GetImplementation();
558 return static_cast<const Toolkit::Internal::Button&>( handle );
561 } // namespace Toolkit
565 #endif // __DALI_TOOLKIT_INTERNAL_BUTTON_H__