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>
26 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
34 const Property::Index Button::PROPERTY_DISABLED = Internal::Button::BUTTON_PROPERTY_START_INDEX;
35 const Property::Index Button::PROPERTY_AUTO_REPEATING = Internal::Button::BUTTON_PROPERTY_START_INDEX + 1;
36 const Property::Index Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 2;
37 const Property::Index Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 3;
38 const Property::Index Button::PROPERTY_TOGGLABLE = Internal::Button::BUTTON_PROPERTY_START_INDEX + 4;
39 const Property::Index Button::PROPERTY_SELECTED = Internal::Button::BUTTON_PROPERTY_START_INDEX + 5;
40 const Property::Index Button::PROPERTY_NORMAL_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 6;
41 const Property::Index Button::PROPERTY_SELECTED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 7;
42 const Property::Index Button::PROPERTY_DISABLED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 8;
43 const Property::Index Button::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 9;
51 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
52 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
56 const char* const SIGNAL_PRESSED = "pressed";
57 const char* const SIGNAL_RELEASED = "released";
58 const char* const SIGNAL_CLICKED = "clicked";
59 const char* const SIGNAL_STATE_CHANGED = "state-changed";
63 const char* const ACTION_BUTTON_CLICK = "button-click";
67 // empty handle as we cannot create button (but type registered for clicked signal)
71 TypeRegistration typeRegistration( typeid( Toolkit::Button ), typeid( Toolkit::Control ), Create );
73 SignalConnectorType signalConnector1( typeRegistration, SIGNAL_PRESSED , &Button::DoConnectSignal );
74 SignalConnectorType signalConnector2( typeRegistration, SIGNAL_RELEASED, &Button::DoConnectSignal );
75 SignalConnectorType signalConnector3( typeRegistration, SIGNAL_CLICKED, &Button::DoConnectSignal );
76 SignalConnectorType signalConnector4( typeRegistration, SIGNAL_STATE_CHANGED, &Button::DoConnectSignal );
78 TypeAction action1( typeRegistration, ACTION_BUTTON_CLICK, &Button::DoAction );
80 PropertyRegistration property1( typeRegistration, "disabled", Toolkit::Button::PROPERTY_DISABLED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
81 PropertyRegistration property2( typeRegistration, "auto-repeating", Toolkit::Button::PROPERTY_AUTO_REPEATING, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
82 PropertyRegistration property3( typeRegistration, "initial-auto-repeating-delay", Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
83 PropertyRegistration property4( typeRegistration, "next-auto-repeating-delay", Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
84 PropertyRegistration property5( typeRegistration, "togglable", Toolkit::Button::PROPERTY_TOGGLABLE, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
85 PropertyRegistration property6( typeRegistration, "selected", Toolkit::Button::PROPERTY_SELECTED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
86 PropertyRegistration property7( typeRegistration, "normal-state-actor", Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
87 PropertyRegistration property8( typeRegistration, "selected-state-actor", Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
88 PropertyRegistration property9( typeRegistration, "disabled-state-actor", Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
89 PropertyRegistration property10( typeRegistration, "label-actor", Toolkit::Button::PROPERTY_LABEL_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
91 } // unnamed namespace
94 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
95 mTogglableButton( false ),
98 mAutoRepeatingTimer(),
100 mAutoRepeating( false ),
101 // mTogglableButton( false ),
102 // mSelected( false ),
103 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
104 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
105 mClickActionPerforming( false ),
112 if( mAutoRepeatingTimer )
114 mAutoRepeatingTimer.Reset();
118 void Button::SetDisabled( bool disabled )
120 mDisabled = disabled;
122 // Notifies the painter.
123 Toolkit::Button handle( GetOwner() );
126 mPainter->SetDisabled( handle, mDisabled );
130 bool Button::IsDisabled() const
135 void Button::SetAutoRepeating( bool autoRepeating )
137 mAutoRepeating = autoRepeating;
139 // An autorepeating button can't be a togglable button.
142 mTogglableButton = false;
145 // Emit a signal is not wanted, only change the appearance.
146 Toolkit::Button handle( GetOwner() );
147 mPainter->Selected( handle );
152 // Notifies the painter.
153 mPainter->SetAutoRepeating( mAutoRepeating );
156 bool Button::IsAutoRepeating() const
158 return mAutoRepeating;
161 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
163 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
164 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
167 float Button::GetInitialAutoRepeatingDelay() const
169 return mInitialAutoRepeatingDelay;
172 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
174 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
175 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
178 float Button::GetNextAutoRepeatingDelay() const
180 return mNextAutoRepeatingDelay;
183 void Button::SetTogglableButton( bool togglable )
185 mTogglableButton = togglable;
187 // A togglable button can't be an autorepeating button.
190 mAutoRepeating = false;
192 // Notifies the painter.
193 mPainter->SetAutoRepeating( mAutoRepeating );
197 bool Button::IsTogglableButton() const
199 return mTogglableButton;
202 void Button::SetSelected( bool selected )
204 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
206 mSelected = selected;
208 Toolkit::Button handle( GetOwner() );
210 // Notifies the painter the button has been selected.
211 mPainter->Selected( handle );
214 mStateChangedSignal.Emit( handle );
218 bool Button::IsSelected() const
220 return mTogglableButton && mSelected;
223 void Button::SetAnimationTime( float animationTime )
225 OnAnimationTimeSet( animationTime );
228 float Button::GetAnimationTime() const
230 return OnAnimationTimeRequested();
233 void Button::SetLabel( const std::string& label )
235 Toolkit::TextView textView = Toolkit::TextView::New( label );
236 textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button
237 SetLabel( textView );
242 void Button::SetLabel( Actor label )
244 Toolkit::Button handle( GetOwner() );
246 mPainter->SetLabel( handle, label );
249 Actor Button::GetLabel() const
254 Actor& Button::GetLabel()
259 void Button::SetButtonImage( Image image )
261 SetButtonImage( ImageActor::New( image ) );
264 void Button::SetButtonImage( Actor image )
266 Toolkit::Button handle( GetOwner() );
267 mPainter->SetButtonImage( handle, image );
270 Actor Button::GetButtonImage() const
275 Actor& Button::GetButtonImage()
280 void Button::SetSelectedImage( Image image )
282 SetSelectedImage( ImageActor::New( image ) );
285 void Button::SetSelectedImage( Actor image )
287 Toolkit::Button handle( GetOwner() );
288 mPainter->SetSelectedImage( handle, image );
291 Actor Button::GetSelectedImage() const
293 return mSelectedImage;
296 Actor& Button::GetSelectedImage()
298 return mSelectedImage;
301 void Button::SetBackgroundImage( Image image )
303 SetBackgroundImage( ImageActor::New( image ) );
306 void Button::SetBackgroundImage( Actor image )
308 Toolkit::Button handle( GetOwner() );
309 mPainter->SetBackgroundImage( handle, image );
312 Actor Button::GetBackgroundImage() const
314 return mBackgroundImage;
317 Actor& Button::GetBackgroundImage()
319 return mBackgroundImage;
322 void Button::SetDisabledImage( Image image )
324 SetDisabledImage( ImageActor::New( image ) );
327 void Button::SetDisabledImage( Actor image )
329 Toolkit::Button handle( GetOwner() );
330 mPainter->SetDisabledImage( handle, image );
333 Actor Button::GetDisabledImage() const
335 return mDisabledImage;
338 Actor& Button::GetDisabledImage()
340 return mDisabledImage;
343 void Button::SetDisabledSelectedImage( Image image )
345 SetDisabledSelectedImage( ImageActor::New( image ) );
348 void Button::SetDisabledSelectedImage( Actor image )
350 Toolkit::Button handle( GetOwner() );
351 mPainter->SetDisabledSelectedImage( handle, image );
354 Actor Button::GetDisabledSelectedImage() const
356 return mDisabledSelectedImage;
359 Actor& Button::GetDisabledSelectedImage()
361 return mDisabledSelectedImage;
364 void Button::SetDisabledBackgroundImage( Image image )
366 SetDisabledBackgroundImage( ImageActor::New( image ) );
369 void Button::SetDisabledBackgroundImage( Actor image )
371 Toolkit::Button handle( GetOwner() );
372 mPainter->SetDisabledBackgroundImage( handle, image );
375 Actor Button::GetDisabledBackgroundImage() const
377 return mDisabledBackgroundImage;
380 Actor& Button::GetDisabledBackgroundImage()
382 return mDisabledBackgroundImage;
385 Actor& Button::GetFadeOutButtonImage()
387 return mFadeOutButtonImage;
390 Actor& Button::GetFadeOutSelectedImage()
392 return mFadeOutSelectedImage;
395 Actor& Button::GetFadeOutBackgroundImage()
397 return mFadeOutBackgroundImage;
400 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
404 Dali::BaseHandle handle( object );
406 Toolkit::Button button = Toolkit::Button::DownCast( handle );
408 DALI_ASSERT_ALWAYS( button );
410 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
412 GetImplementation( button ).DoClickAction( attributes );
419 void Button::DoClickAction( const PropertyValueContainer& attributes )
421 // Prevents the button signals from doing a recursive loop by sending an action
422 // and re-emitting the signals.
423 if( !mClickActionPerforming )
425 mClickActionPerforming = true;
429 mClickActionPerforming = false;
433 void Button::OnAnimationTimeSet( float animationTime )
435 mPainter->SetAnimationTime( animationTime );
438 float Button::OnAnimationTimeRequested() const
440 return mPainter->GetAnimationTime();
443 void Button::OnButtonStageDisconnection()
445 if( ButtonDown == mState )
447 if( !mTogglableButton )
449 Toolkit::Button handle( GetOwner() );
451 // Notifies the painter the button has been released.
452 mPainter->Released( handle );
456 mAutoRepeatingTimer.Reset();
462 void Button::OnButtonDown()
464 if( !mTogglableButton )
466 Toolkit::Button handle( GetOwner() );
468 // Notifies the painter the button has been pressed.
469 mPainter->Pressed( handle );
473 SetUpTimer( mInitialAutoRepeatingDelay );
477 mPressedSignal.Emit( handle );
481 void Button::OnButtonUp()
483 if( ButtonDown == mState )
485 if( mTogglableButton )
487 SetSelected( !mSelected );
491 Toolkit::Button handle( GetOwner() );
493 // Notifies the painter the button has been clicked.
494 mPainter->Released( handle );
495 mPainter->Clicked( handle );
499 mAutoRepeatingTimer.Reset();
503 mReleasedSignal.Emit( handle );
504 mClickedSignal.Emit( handle );
509 void Button::OnTouchPointLeave()
511 if( ButtonDown == mState )
513 if( !mTogglableButton )
515 Toolkit::Button handle( GetOwner() );
517 // Notifies the painter the button has been released.
518 mPainter->Released( handle );
522 mAutoRepeatingTimer.Reset();
526 mReleasedSignal.Emit( handle );
531 void Button::OnTouchPointInterrupted()
536 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
538 return mPressedSignal;
541 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
543 return mReleasedSignal;
546 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
548 return mClickedSignal;
551 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
553 return mStateChangedSignal;
556 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
558 Dali::BaseHandle handle( object );
560 bool connected( true );
561 Toolkit::Button button = Toolkit::Button::DownCast( handle );
563 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
565 button.PressedSignal().Connect( tracker, functor );
567 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
569 button.ReleasedSignal().Connect( tracker, functor );
571 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
573 button.ClickedSignal().Connect( tracker, functor );
575 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
577 button.StateChangedSignal().Connect( tracker, functor );
581 // signalName does not match any signal
588 bool Button::OnTouchEvent(const TouchEvent& event)
590 // Only events are processed when the button is not disabled and the touch event has only
592 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
594 switch( event.GetPoint(0).state )
596 case TouchPoint::Down:
598 OnButtonDown(); // Notification for derived classes.
600 // Sets the button state to ButtonDown.
606 OnButtonUp(); // Notification for derived classes.
608 // Sets the button state to ButtonUp.
612 case TouchPoint::Interrupted:
614 OnTouchPointInterrupted(); // Notification for derived classes.
616 // Sets the button state to the default (ButtonUp).
620 case TouchPoint::Leave:
622 OnTouchPointLeave(); // Notification for derived classes.
624 // Sets the button state to the default (ButtonUp).
628 case TouchPoint::Motion:
629 case TouchPoint::Stationary: // FALLTHROUGH
636 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
641 else if( 1 < event.GetPointCount() )
643 OnTouchPointLeave(); // Notification for derived classes.
645 // Sets the button state to the default (ButtonUp).
652 void Button::OnInitialize()
654 // Initialize the painter and notifies subclasses.
655 Toolkit::Button handle( GetOwner() );
658 mPainter->Initialize( handle );
663 mTapDetector = TapGestureDetector::New();
664 mTapDetector.Attach( self );
665 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
667 OnButtonInitialize();
669 self.SetKeyboardFocusable( true );
672 void Button::OnActivated()
674 // When the button is activated, it performs the click action
675 PropertyValueContainer attributes;
676 DoClickAction( attributes );
679 void Button::OnControlSizeSet(const Vector3& targetSize)
681 Toolkit::Button handle( GetOwner() );
684 mPainter->SetSize( handle, targetSize );
688 void Button::OnTap(Actor actor, const TapGesture& tap)
693 void Button::SetUpTimer( float delay )
695 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
696 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
697 mAutoRepeatingTimer.Start();
700 bool Button::AutoRepeatingSlot()
702 bool consumed = false;
705 // Restart the autorepeat timer.
706 SetUpTimer( mNextAutoRepeatingDelay );
708 Toolkit::Button handle( GetOwner() );
710 // Notifies the painter the button has been pressed.
711 mPainter->Pressed( handle );
714 consumed = mReleasedSignal.Emit( handle );
715 consumed |= mClickedSignal.Emit( handle );
716 consumed |= mPressedSignal.Emit( handle );
722 void Button::OnControlStageDisconnection()
724 OnButtonStageDisconnection(); // Notification for derived classes.
728 void Button::SetPainter(ButtonPainterPtr painter)
733 Button::ButtonState Button::GetState()
738 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
740 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
746 case Toolkit::Button::PROPERTY_DISABLED:
748 GetImplementation( button ).SetDisabled( value.Get<bool>() );
752 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
754 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
758 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
760 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
764 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
766 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
770 case Toolkit::Button::PROPERTY_TOGGLABLE:
772 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
776 case Toolkit::Button::PROPERTY_SELECTED:
778 GetImplementation( button ).SetSelected( value.Get< bool >() );
782 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
784 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
788 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
790 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
794 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
796 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
800 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
802 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
809 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
811 Property::Value value;
813 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
817 switch ( propertyIndex )
819 case Toolkit::Button::PROPERTY_DISABLED:
821 value = GetImplementation( button ).mDisabled;
825 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
827 value = GetImplementation( button ).mAutoRepeating;
831 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
833 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
837 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
839 value = GetImplementation( button ).mNextAutoRepeatingDelay;
843 case Toolkit::Button::PROPERTY_TOGGLABLE:
845 value = GetImplementation( button ).mTogglableButton;
849 case Toolkit::Button::PROPERTY_SELECTED:
851 value = GetImplementation( button ).mSelected;
855 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
858 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonImage, map );
863 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
866 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedImage, map );
871 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
874 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledImage, map );
879 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
882 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
892 } // namespace Internal
894 } // namespace Toolkit