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/actors/image-actor.h>
25 #include <dali/public-api/scripting/scripting.h>
35 const Property::Index Button::PROPERTY_DISABLED = Internal::Button::BUTTON_PROPERTY_START_INDEX;
36 const Property::Index Button::PROPERTY_AUTO_REPEATING = Internal::Button::BUTTON_PROPERTY_START_INDEX + 1;
37 const Property::Index Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 2;
38 const Property::Index Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 3;
39 const Property::Index Button::PROPERTY_TOGGLABLE = Internal::Button::BUTTON_PROPERTY_START_INDEX + 4;
40 const Property::Index Button::PROPERTY_SELECTED = Internal::Button::BUTTON_PROPERTY_START_INDEX + 5;
41 const Property::Index Button::PROPERTY_NORMAL_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 6;
42 const Property::Index Button::PROPERTY_SELECTED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 7;
43 const Property::Index Button::PROPERTY_DISABLED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 8;
44 const Property::Index Button::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 9;
52 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
53 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
57 const char* const SIGNAL_PRESSED = "pressed";
58 const char* const SIGNAL_RELEASED = "released";
59 const char* const SIGNAL_CLICKED = "clicked";
60 const char* const SIGNAL_STATE_CHANGED = "state-changed";
64 const char* const ACTION_BUTTON_CLICK = "button-click";
68 // empty handle as we cannot create button (but type registered for clicked signal)
72 TypeRegistration typeRegistration( typeid( Toolkit::Button ), typeid( Toolkit::Control ), Create );
74 SignalConnectorType signalConnector1( typeRegistration, SIGNAL_PRESSED , &Button::DoConnectSignal );
75 SignalConnectorType signalConnector2( typeRegistration, SIGNAL_RELEASED, &Button::DoConnectSignal );
76 SignalConnectorType signalConnector3( typeRegistration, SIGNAL_CLICKED, &Button::DoConnectSignal );
77 SignalConnectorType signalConnector4( typeRegistration, SIGNAL_STATE_CHANGED, &Button::DoConnectSignal );
79 TypeAction action1( typeRegistration, ACTION_BUTTON_CLICK, &Button::DoAction );
81 PropertyRegistration property1( typeRegistration, "disabled", Toolkit::Button::PROPERTY_DISABLED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
82 PropertyRegistration property2( typeRegistration, "auto-repeating", Toolkit::Button::PROPERTY_AUTO_REPEATING, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
83 PropertyRegistration property3( typeRegistration, "initial-auto-repeating-delay", Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
84 PropertyRegistration property4( typeRegistration, "next-auto-repeating-delay", Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
85 PropertyRegistration property5( typeRegistration, "togglable", Toolkit::Button::PROPERTY_TOGGLABLE, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
86 PropertyRegistration property6( typeRegistration, "selected", Toolkit::Button::PROPERTY_SELECTED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
87 PropertyRegistration property7( typeRegistration, "normal-state-actor", Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
88 PropertyRegistration property8( typeRegistration, "selected-state-actor", Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
89 PropertyRegistration property9( typeRegistration, "disabled-state-actor", Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
90 PropertyRegistration property10( typeRegistration, "label-actor", Toolkit::Button::PROPERTY_LABEL_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
92 } // unnamed namespace
95 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
96 mTogglableButton( false ),
99 mAutoRepeatingTimer(),
101 mAutoRepeating( false ),
102 // mTogglableButton( false ),
103 // mSelected( false ),
104 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
105 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
106 mClickActionPerforming( false ),
113 if( mAutoRepeatingTimer )
115 mAutoRepeatingTimer.Reset();
119 void Button::SetDisabled( bool disabled )
121 mDisabled = disabled;
123 // Notifies the painter.
124 Toolkit::Button handle( GetOwner() );
127 mPainter->SetDisabled( handle, mDisabled );
131 bool Button::IsDisabled() const
136 void Button::SetAutoRepeating( bool autoRepeating )
138 mAutoRepeating = autoRepeating;
140 // An autorepeating button can't be a togglable button.
143 mTogglableButton = false;
146 // Emit a signal is not wanted, only change the appearance.
147 Toolkit::Button handle( GetOwner() );
148 mPainter->Selected( handle );
153 // Notifies the painter.
154 mPainter->SetAutoRepeating( mAutoRepeating );
157 bool Button::IsAutoRepeating() const
159 return mAutoRepeating;
162 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
164 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
165 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
168 float Button::GetInitialAutoRepeatingDelay() const
170 return mInitialAutoRepeatingDelay;
173 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
175 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
176 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
179 float Button::GetNextAutoRepeatingDelay() const
181 return mNextAutoRepeatingDelay;
184 void Button::SetTogglableButton( bool togglable )
186 mTogglableButton = togglable;
188 // A togglable button can't be an autorepeating button.
191 mAutoRepeating = false;
193 // Notifies the painter.
194 mPainter->SetAutoRepeating( mAutoRepeating );
198 bool Button::IsTogglableButton() const
200 return mTogglableButton;
203 void Button::SetSelected( bool selected )
205 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
207 mSelected = selected;
209 Toolkit::Button handle( GetOwner() );
211 // Notifies the painter the button has been selected.
212 mPainter->Selected( handle );
215 mStateChangedSignal.Emit( handle );
219 bool Button::IsSelected() const
221 return mTogglableButton && mSelected;
224 void Button::SetAnimationTime( float animationTime )
226 OnAnimationTimeSet( animationTime );
229 float Button::GetAnimationTime() const
231 return OnAnimationTimeRequested();
234 void Button::SetLabel( const std::string& label )
239 void Button::SetLabel( Actor label )
241 Toolkit::Button handle( GetOwner() );
243 mPainter->SetLabel( handle, label );
246 Actor Button::GetLabel() const
251 Actor& Button::GetLabel()
256 void Button::SetButtonImage( Image image )
258 SetButtonImage( ImageActor::New( image ) );
261 void Button::SetButtonImage( Actor image )
263 Toolkit::Button handle( GetOwner() );
264 mPainter->SetButtonImage( handle, image );
267 Actor Button::GetButtonImage() const
272 Actor& Button::GetButtonImage()
277 void Button::SetSelectedImage( Image image )
279 SetSelectedImage( ImageActor::New( image ) );
282 void Button::SetSelectedImage( Actor image )
284 Toolkit::Button handle( GetOwner() );
285 mPainter->SetSelectedImage( handle, image );
288 Actor Button::GetSelectedImage() const
290 return mSelectedImage;
293 Actor& Button::GetSelectedImage()
295 return mSelectedImage;
298 void Button::SetBackgroundImage( Image image )
300 SetBackgroundImage( ImageActor::New( image ) );
303 void Button::SetBackgroundImage( Actor image )
305 Toolkit::Button handle( GetOwner() );
306 mPainter->SetBackgroundImage( handle, image );
309 Actor Button::GetBackgroundImage() const
311 return mBackgroundImage;
314 Actor& Button::GetBackgroundImage()
316 return mBackgroundImage;
319 void Button::SetDisabledImage( Image image )
321 SetDisabledImage( ImageActor::New( image ) );
324 void Button::SetDisabledImage( Actor image )
326 Toolkit::Button handle( GetOwner() );
327 mPainter->SetDisabledImage( handle, image );
330 Actor Button::GetDisabledImage() const
332 return mDisabledImage;
335 Actor& Button::GetDisabledImage()
337 return mDisabledImage;
340 void Button::SetDisabledSelectedImage( Image image )
342 SetDisabledSelectedImage( ImageActor::New( image ) );
345 void Button::SetDisabledSelectedImage( Actor image )
347 Toolkit::Button handle( GetOwner() );
348 mPainter->SetDisabledSelectedImage( handle, image );
351 Actor Button::GetDisabledSelectedImage() const
353 return mDisabledSelectedImage;
356 Actor& Button::GetDisabledSelectedImage()
358 return mDisabledSelectedImage;
361 void Button::SetDisabledBackgroundImage( Image image )
363 SetDisabledBackgroundImage( ImageActor::New( image ) );
366 void Button::SetDisabledBackgroundImage( Actor image )
368 Toolkit::Button handle( GetOwner() );
369 mPainter->SetDisabledBackgroundImage( handle, image );
372 Actor Button::GetDisabledBackgroundImage() const
374 return mDisabledBackgroundImage;
377 Actor& Button::GetDisabledBackgroundImage()
379 return mDisabledBackgroundImage;
382 Actor& Button::GetFadeOutButtonImage()
384 return mFadeOutButtonImage;
387 Actor& Button::GetFadeOutSelectedImage()
389 return mFadeOutSelectedImage;
392 Actor& Button::GetFadeOutBackgroundImage()
394 return mFadeOutBackgroundImage;
397 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
401 Dali::BaseHandle handle( object );
403 Toolkit::Button button = Toolkit::Button::DownCast( handle );
405 DALI_ASSERT_ALWAYS( button );
407 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
409 GetImplementation( button ).DoClickAction( attributes );
416 void Button::DoClickAction( const PropertyValueContainer& attributes )
418 // Prevents the button signals from doing a recursive loop by sending an action
419 // and re-emitting the signals.
420 if( !mClickActionPerforming )
422 mClickActionPerforming = true;
426 mClickActionPerforming = false;
430 void Button::OnAnimationTimeSet( float animationTime )
432 mPainter->SetAnimationTime( animationTime );
435 float Button::OnAnimationTimeRequested() const
437 return mPainter->GetAnimationTime();
440 void Button::OnButtonStageDisconnection()
442 if( ButtonDown == mState )
444 if( !mTogglableButton )
446 Toolkit::Button handle( GetOwner() );
448 // Notifies the painter the button has been released.
449 mPainter->Released( handle );
453 mAutoRepeatingTimer.Reset();
459 void Button::OnButtonDown()
461 if( !mTogglableButton )
463 Toolkit::Button handle( GetOwner() );
465 // Notifies the painter the button has been pressed.
466 mPainter->Pressed( handle );
470 SetUpTimer( mInitialAutoRepeatingDelay );
474 mPressedSignal.Emit( handle );
478 void Button::OnButtonUp()
480 if( ButtonDown == mState )
482 if( mTogglableButton )
484 SetSelected( !mSelected );
488 Toolkit::Button handle( GetOwner() );
490 // Notifies the painter the button has been clicked.
491 mPainter->Released( handle );
492 mPainter->Clicked( handle );
496 mAutoRepeatingTimer.Reset();
500 mReleasedSignal.Emit( handle );
501 mClickedSignal.Emit( handle );
506 void Button::OnTouchPointLeave()
508 if( ButtonDown == mState )
510 if( !mTogglableButton )
512 Toolkit::Button handle( GetOwner() );
514 // Notifies the painter the button has been released.
515 mPainter->Released( handle );
519 mAutoRepeatingTimer.Reset();
523 mReleasedSignal.Emit( handle );
528 void Button::OnTouchPointInterrupted()
533 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
535 return mPressedSignal;
538 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
540 return mReleasedSignal;
543 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
545 return mClickedSignal;
548 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
550 return mStateChangedSignal;
553 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
555 Dali::BaseHandle handle( object );
557 bool connected( true );
558 Toolkit::Button button = Toolkit::Button::DownCast( handle );
560 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
562 button.PressedSignal().Connect( tracker, functor );
564 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
566 button.ReleasedSignal().Connect( tracker, functor );
568 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
570 button.ClickedSignal().Connect( tracker, functor );
572 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
574 button.StateChangedSignal().Connect( tracker, functor );
578 // signalName does not match any signal
585 bool Button::OnTouchEvent(const TouchEvent& event)
587 // Only events are processed when the button is not disabled and the touch event has only
589 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
591 switch( event.GetPoint(0).state )
593 case TouchPoint::Down:
595 OnButtonDown(); // Notification for derived classes.
597 // Sets the button state to ButtonDown.
603 OnButtonUp(); // Notification for derived classes.
605 // Sets the button state to ButtonUp.
609 case TouchPoint::Interrupted:
611 OnTouchPointInterrupted(); // Notification for derived classes.
613 // Sets the button state to the default (ButtonUp).
617 case TouchPoint::Leave:
619 OnTouchPointLeave(); // Notification for derived classes.
621 // Sets the button state to the default (ButtonUp).
625 case TouchPoint::Motion:
626 case TouchPoint::Stationary: // FALLTHROUGH
633 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
638 else if( 1 < event.GetPointCount() )
640 OnTouchPointLeave(); // Notification for derived classes.
642 // Sets the button state to the default (ButtonUp).
649 void Button::OnInitialize()
651 // Initialize the painter and notifies subclasses.
652 Toolkit::Button handle( GetOwner() );
655 mPainter->Initialize( handle );
660 mTapDetector = TapGestureDetector::New();
661 mTapDetector.Attach( self );
662 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
664 OnButtonInitialize();
666 self.SetKeyboardFocusable( true );
669 void Button::OnActivated()
671 // When the button is activated, it performs the click action
672 PropertyValueContainer attributes;
673 DoClickAction( attributes );
676 void Button::OnControlSizeSet(const Vector3& targetSize)
678 Toolkit::Button handle( GetOwner() );
681 mPainter->SetSize( handle, targetSize );
685 void Button::OnTap(Actor actor, const TapGesture& tap)
690 void Button::SetUpTimer( float delay )
692 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
693 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
694 mAutoRepeatingTimer.Start();
697 bool Button::AutoRepeatingSlot()
699 bool consumed = false;
702 // Restart the autorepeat timer.
703 SetUpTimer( mNextAutoRepeatingDelay );
705 Toolkit::Button handle( GetOwner() );
707 // Notifies the painter the button has been pressed.
708 mPainter->Pressed( handle );
711 consumed = mReleasedSignal.Emit( handle );
712 consumed |= mClickedSignal.Emit( handle );
713 consumed |= mPressedSignal.Emit( handle );
719 void Button::OnControlStageDisconnection()
721 OnButtonStageDisconnection(); // Notification for derived classes.
725 void Button::SetPainter(ButtonPainterPtr painter)
730 Button::ButtonState Button::GetState()
735 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
737 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
743 case Toolkit::Button::PROPERTY_DISABLED:
745 GetImplementation( button ).SetDisabled( value.Get<bool>() );
749 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
751 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
755 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
757 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
761 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
763 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
767 case Toolkit::Button::PROPERTY_TOGGLABLE:
769 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
773 case Toolkit::Button::PROPERTY_SELECTED:
775 GetImplementation( button ).SetSelected( value.Get< bool >() );
779 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
781 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
785 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
787 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
791 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
793 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
797 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
799 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
806 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
808 Property::Value value;
810 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
814 switch ( propertyIndex )
816 case Toolkit::Button::PROPERTY_DISABLED:
818 value = GetImplementation( button ).mDisabled;
822 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
824 value = GetImplementation( button ).mAutoRepeating;
828 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
830 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
834 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
836 value = GetImplementation( button ).mNextAutoRepeatingDelay;
840 case Toolkit::Button::PROPERTY_TOGGLABLE:
842 value = GetImplementation( button ).mTogglableButton;
846 case Toolkit::Button::PROPERTY_SELECTED:
848 value = GetImplementation( button ).mSelected;
852 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
855 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonImage, map );
860 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
863 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedImage, map );
868 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
871 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledImage, map );
876 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
879 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
889 } // namespace Internal
891 } // namespace Toolkit