2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "button-impl.h"
22 #include <dali/public-api/events/touch-event.h>
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/public-api/actors/image-actor.h>
26 #include <dali/public-api/scripting/scripting.h>
29 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
45 // empty handle as we cannot create button (but type registered for clicked signal)
49 // Setup properties, signals and actions using the type-registry.
50 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create );
52 DALI_PROPERTY_REGISTRATION( Button, "disabled", BOOLEAN, DISABLED )
53 DALI_PROPERTY_REGISTRATION( Button, "auto-repeating", BOOLEAN, AUTO_REPEATING )
54 DALI_PROPERTY_REGISTRATION( Button, "initial-auto-repeating-delay", FLOAT, INITIAL_AUTO_REPEATING_DELAY )
55 DALI_PROPERTY_REGISTRATION( Button, "next-auto-repeating-delay", FLOAT, NEXT_AUTO_REPEATING_DELAY )
56 DALI_PROPERTY_REGISTRATION( Button, "togglable", BOOLEAN, TOGGLABLE )
57 DALI_PROPERTY_REGISTRATION( Button, "selected", BOOLEAN, SELECTED )
58 DALI_PROPERTY_REGISTRATION( Button, "normal-state-actor", MAP, NORMAL_STATE_ACTOR )
59 DALI_PROPERTY_REGISTRATION( Button, "selected-state-actor", MAP, SELECTED_STATE_ACTOR )
60 DALI_PROPERTY_REGISTRATION( Button, "disabled-state-actor", MAP, DISABLED_STATE_ACTOR )
61 DALI_PROPERTY_REGISTRATION( Button, "label-actor", MAP, LABEL_ACTOR )
63 DALI_SIGNAL_REGISTRATION( Button, "pressed", SIGNAL_PRESSED )
64 DALI_SIGNAL_REGISTRATION( Button, "released", SIGNAL_RELEASED )
65 DALI_SIGNAL_REGISTRATION( Button, "clicked", SIGNAL_CLICKED )
66 DALI_SIGNAL_REGISTRATION( Button, "state-changed", SIGNAL_STATE_CHANGED )
68 DALI_ACTION_REGISTRATION( Button, "button-click", ACTION_BUTTON_CLICK )
70 DALI_TYPE_REGISTRATION_END()
72 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
73 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
75 } // unnamed namespace
78 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
79 mAutoRepeatingTimer(),
81 mAutoRepeating( false ),
82 mTogglableButton( false ),
84 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
85 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
86 mAnimationTime( 0.0f ),
87 mClickActionPerforming( false ),
94 if( mAutoRepeatingTimer )
96 mAutoRepeatingTimer.Reset();
100 void Button::SetDisabled( bool disabled )
102 if( disabled != mDisabled )
104 mDisabled = disabled;
106 OnDisabled( mDisabled );
110 bool Button::IsDisabled() const
115 void Button::SetAutoRepeating( bool autoRepeating )
117 mAutoRepeating = autoRepeating;
119 // An autorepeating button can't be a togglable button.
122 mTogglableButton = false;
126 // Emit a signal is not wanted, only change the appearance.
136 bool Button::IsAutoRepeating() const
138 return mAutoRepeating;
141 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
143 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
144 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
147 float Button::GetInitialAutoRepeatingDelay() const
149 return mInitialAutoRepeatingDelay;
152 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
154 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
155 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
158 float Button::GetNextAutoRepeatingDelay() const
160 return mNextAutoRepeatingDelay;
163 void Button::SetTogglableButton( bool togglable )
165 mTogglableButton = togglable;
167 // A togglable button can't be an autorepeating button.
170 mAutoRepeating = false;
174 bool Button::IsTogglableButton() const
176 return mTogglableButton;
179 void Button::SetSelected( bool selected )
181 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
183 // Notifies the derived class the button has been selected.
184 OnSelected( selected );
186 mSelected = selected;
188 Toolkit::Button handle( GetOwner() );
191 mStateChangedSignal.Emit( handle );
197 bool Button::IsSelected() const
199 return mTogglableButton && mSelected;
202 void Button::SetAnimationTime( float animationTime )
204 mAnimationTime = animationTime;
207 float Button::GetAnimationTime() const
209 return mAnimationTime;
212 void Button::SetLabel( const std::string& label )
214 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( label );
215 SetLabel( textLabel );
218 void Button::SetLabel( Actor label )
220 if( mLabel != label )
222 if( mLabel && mLabel.GetParent() )
224 mLabel.GetParent().Remove( mLabel );
235 Actor Button::GetLabel() const
240 Actor& Button::GetLabel()
245 Actor Button::GetButtonImage() const
247 return mButtonContent;
250 Actor& Button::GetButtonImage()
252 return mButtonContent;
255 Actor Button::GetSelectedImage() const
257 return mSelectedContent;
260 Actor& Button::GetSelectedImage()
262 return mSelectedContent;
265 Actor Button::GetBackgroundImage() const
267 return mBackgroundContent;
270 Actor& Button::GetBackgroundImage()
272 return mBackgroundContent;
275 Actor Button::GetDisabledImage() const
277 return mDisabledContent;
280 Actor& Button::GetDisabledImage()
282 return mDisabledContent;
285 Actor Button::GetDisabledSelectedImage() const
287 return mDisabledSelectedContent;
290 Actor& Button::GetDisabledSelectedImage()
292 return mDisabledSelectedContent;
295 Actor Button::GetDisabledBackgroundImage() const
297 return mDisabledBackgroundContent;
300 Actor& Button::GetDisabledBackgroundImage()
302 return mDisabledBackgroundContent;
305 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
309 Dali::BaseHandle handle( object );
311 Toolkit::Button button = Toolkit::Button::DownCast( handle );
313 DALI_ASSERT_ALWAYS( button );
315 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
317 GetImplementation( button ).DoClickAction( attributes );
324 void Button::DoClickAction( const PropertyValueContainer& attributes )
326 // Prevents the button signals from doing a recursive loop by sending an action
327 // and re-emitting the signals.
328 if( !mClickActionPerforming )
330 mClickActionPerforming = true;
334 mClickActionPerforming = false;
338 void Button::OnButtonStageDisconnection()
340 if( ButtonDown == mState )
342 if( !mTogglableButton )
344 Toolkit::Button handle( GetOwner() );
346 // Notifies the derived class the button has been released.
351 mAutoRepeatingTimer.Reset();
357 void Button::OnButtonDown()
359 if( !mTogglableButton )
361 Toolkit::Button handle( GetOwner() );
363 // Notifies the derived class the button has been pressed.
368 SetUpTimer( mInitialAutoRepeatingDelay );
372 mPressedSignal.Emit( handle );
376 void Button::OnButtonUp()
378 if( ButtonDown == mState )
380 if( mTogglableButton )
382 SetSelected( !mSelected );
386 // Notifies the derived class the button has been clicked.
392 mAutoRepeatingTimer.Reset();
395 Toolkit::Button handle( GetOwner() );
398 mReleasedSignal.Emit( handle );
399 mClickedSignal.Emit( handle );
404 void Button::OnTouchPointLeave()
406 if( ButtonDown == mState )
408 if( !mTogglableButton )
410 Toolkit::Button handle( GetOwner() );
412 // Notifies the derived class the button has been released.
417 mAutoRepeatingTimer.Reset();
421 mReleasedSignal.Emit( handle );
426 void Button::OnTouchPointInterrupted()
431 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
433 return mPressedSignal;
436 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
438 return mReleasedSignal;
441 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
443 return mClickedSignal;
446 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
448 return mStateChangedSignal;
451 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
453 Dali::BaseHandle handle( object );
455 bool connected( true );
456 Toolkit::Button button = Toolkit::Button::DownCast( handle );
458 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
460 button.PressedSignal().Connect( tracker, functor );
462 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
464 button.ReleasedSignal().Connect( tracker, functor );
466 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
468 button.ClickedSignal().Connect( tracker, functor );
470 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
472 button.StateChangedSignal().Connect( tracker, functor );
476 // signalName does not match any signal
483 bool Button::OnTouchEvent(const TouchEvent& event)
485 // Only events are processed when the button is not disabled and the touch event has only
487 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
489 switch( event.GetPoint(0).state )
491 case TouchPoint::Down:
493 OnButtonDown(); // Notification for derived classes.
495 // Sets the button state to ButtonDown.
501 OnButtonUp(); // Notification for derived classes.
503 // Sets the button state to ButtonUp.
507 case TouchPoint::Interrupted:
509 OnTouchPointInterrupted(); // Notification for derived classes.
511 // Sets the button state to the default (ButtonUp).
515 case TouchPoint::Leave:
517 OnTouchPointLeave(); // Notification for derived classes.
519 // Sets the button state to the default (ButtonUp).
523 case TouchPoint::Motion:
524 case TouchPoint::Stationary: // FALLTHROUGH
531 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
536 else if( 1 < event.GetPointCount() )
538 OnTouchPointLeave(); // Notification for derived classes.
540 // Sets the button state to the default (ButtonUp).
547 void Button::OnInitialize()
551 mTapDetector = TapGestureDetector::New();
552 mTapDetector.Attach( self );
553 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
555 OnButtonInitialize();
557 self.SetKeyboardFocusable( true );
560 void Button::OnActivated()
562 // When the button is activated, it performs the click action
563 PropertyValueContainer attributes;
564 DoClickAction( attributes );
567 void Button::OnTap(Actor actor, const TapGesture& tap)
572 void Button::SetUpTimer( float delay )
574 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
575 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
576 mAutoRepeatingTimer.Start();
579 bool Button::AutoRepeatingSlot()
581 bool consumed = false;
584 // Restart the autorepeat timer.
585 SetUpTimer( mNextAutoRepeatingDelay );
587 Toolkit::Button handle( GetOwner() );
589 // Notifies the derived class the button has been pressed.
593 consumed = mReleasedSignal.Emit( handle );
594 consumed |= mClickedSignal.Emit( handle );
595 consumed |= mPressedSignal.Emit( handle );
601 void Button::OnControlStageDisconnection()
603 OnButtonStageDisconnection(); // Notification for derived classes.
607 Button::ButtonState Button::GetState()
612 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
614 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
620 case Toolkit::Button::Property::DISABLED:
622 GetImplementation( button ).SetDisabled( value.Get<bool>() );
626 case Toolkit::Button::Property::AUTO_REPEATING:
628 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
632 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
634 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
638 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
640 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
644 case Toolkit::Button::Property::TOGGLABLE:
646 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
650 case Toolkit::Button::Property::SELECTED:
652 GetImplementation( button ).SetSelected( value.Get< bool >() );
656 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
658 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
662 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
664 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
668 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
670 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
674 case Toolkit::Button::Property::LABEL_ACTOR:
676 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
683 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
685 Property::Value value;
687 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
691 switch ( propertyIndex )
693 case Toolkit::Button::Property::DISABLED:
695 value = GetImplementation( button ).mDisabled;
699 case Toolkit::Button::Property::AUTO_REPEATING:
701 value = GetImplementation( button ).mAutoRepeating;
705 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
707 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
711 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
713 value = GetImplementation( button ).mNextAutoRepeatingDelay;
717 case Toolkit::Button::Property::TOGGLABLE:
719 value = GetImplementation( button ).mTogglableButton;
723 case Toolkit::Button::Property::SELECTED:
725 value = GetImplementation( button ).mSelected;
729 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
732 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonContent, map );
737 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
740 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedContent, map );
745 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
748 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledContent, map );
753 case Toolkit::Button::Property::LABEL_ACTOR:
756 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
766 } // namespace Internal
768 } // namespace Toolkit