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 // empty handle as we cannot create button (but type registered for clicked signal)
60 TypeRegistration typeRegistration( typeid(Toolkit::Button), typeid(Toolkit::Control), Create );
62 SignalConnectorType signalConnector1( typeRegistration, Toolkit::Button::SIGNAL_PRESSED , &Button::DoConnectSignal );
63 SignalConnectorType signalConnector2( typeRegistration, Toolkit::Button::SIGNAL_RELEASED, &Button::DoConnectSignal );
64 SignalConnectorType signalConnector3( typeRegistration, Toolkit::Button::SIGNAL_CLICKED, &Button::DoConnectSignal );
65 SignalConnectorType signalConnector4( typeRegistration, Toolkit::Button::SIGNAL_STATE_CHANGED, &Button::DoConnectSignal );
67 TypeAction action1( typeRegistration, Toolkit::Button::ACTION_BUTTON_CLICK, &Button::DoAction );
69 PropertyRegistration property1( typeRegistration, "disabled", Toolkit::Button::PROPERTY_DISABLED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
70 PropertyRegistration property2( typeRegistration, "auto-repeating", Toolkit::Button::PROPERTY_AUTO_REPEATING, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
71 PropertyRegistration property3( typeRegistration, "initial-auto-repeating-delay", Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
72 PropertyRegistration property4( typeRegistration, "next-auto-repeating-delay", Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty );
73 PropertyRegistration property5( typeRegistration, "togglable", Toolkit::Button::PROPERTY_TOGGLABLE, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
74 PropertyRegistration property6( typeRegistration, "selected", Toolkit::Button::PROPERTY_SELECTED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty );
75 PropertyRegistration property7( typeRegistration, "normal-state-actor", Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
76 PropertyRegistration property8( typeRegistration, "selected-state-actor", Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
77 PropertyRegistration property9( typeRegistration, "disabled-state-actor", Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
78 PropertyRegistration property10( typeRegistration, "label-actor", Toolkit::Button::PROPERTY_LABEL_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty );
80 } // unnamed namespace
83 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
84 mTogglableButton( false ),
87 mAutoRepeatingTimer(),
89 mAutoRepeating( false ),
90 // mTogglableButton( false ),
91 // mSelected( false ),
92 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
93 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
94 mClickActionPerforming( false ),
101 if( mAutoRepeatingTimer )
103 mAutoRepeatingTimer.Reset();
107 void Button::SetDisabled( bool disabled )
109 mDisabled = disabled;
111 // Notifies the painter.
112 Toolkit::Button handle( GetOwner() );
115 mPainter->SetDisabled( handle, mDisabled );
119 bool Button::IsDisabled() const
124 void Button::SetAutoRepeating( bool autoRepeating )
126 mAutoRepeating = autoRepeating;
128 // An autorepeating button can't be a togglable button.
131 mTogglableButton = false;
134 // Emit a signal is not wanted, only change the appearance.
135 Toolkit::Button handle( GetOwner() );
136 mPainter->Selected( handle );
141 // Notifies the painter.
142 mPainter->SetAutoRepeating( mAutoRepeating );
145 bool Button::IsAutoRepeating() const
147 return mAutoRepeating;
150 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
152 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
153 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
156 float Button::GetInitialAutoRepeatingDelay() const
158 return mInitialAutoRepeatingDelay;
161 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
163 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
164 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
167 float Button::GetNextAutoRepeatingDelay() const
169 return mNextAutoRepeatingDelay;
172 void Button::SetTogglableButton( bool togglable )
174 mTogglableButton = togglable;
176 // A togglable button can't be an autorepeating button.
179 mAutoRepeating = false;
181 // Notifies the painter.
182 mPainter->SetAutoRepeating( mAutoRepeating );
186 bool Button::IsTogglableButton() const
188 return mTogglableButton;
191 void Button::SetSelected( bool selected )
193 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
195 mSelected = selected;
197 Toolkit::Button handle( GetOwner() );
199 // Notifies the painter the button has been selected.
200 mPainter->Selected( handle );
203 mStateChangedSignal.Emit( handle );
207 bool Button::IsSelected() const
209 return mTogglableButton && mSelected;
212 void Button::SetAnimationTime( float animationTime )
214 OnAnimationTimeSet( animationTime );
217 float Button::GetAnimationTime() const
219 return OnAnimationTimeRequested();
222 void Button::SetLabel( const std::string& label )
224 Toolkit::TextView textView = Toolkit::TextView::New( label );
225 textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button
226 SetLabel( textView );
231 void Button::SetLabel( Actor label )
233 Toolkit::Button handle( GetOwner() );
235 mPainter->SetLabel( handle, label );
238 Actor Button::GetLabel() const
243 Actor& Button::GetLabel()
248 void Button::SetButtonImage( Image image )
250 SetButtonImage( ImageActor::New( image ) );
253 void Button::SetButtonImage( Actor image )
255 Toolkit::Button handle( GetOwner() );
256 mPainter->SetButtonImage( handle, image );
259 Actor Button::GetButtonImage() const
264 Actor& Button::GetButtonImage()
269 void Button::SetSelectedImage( Image image )
271 SetSelectedImage( ImageActor::New( image ) );
274 void Button::SetSelectedImage( Actor image )
276 Toolkit::Button handle( GetOwner() );
277 mPainter->SetSelectedImage( handle, image );
280 Actor Button::GetSelectedImage() const
282 return mSelectedImage;
285 Actor& Button::GetSelectedImage()
287 return mSelectedImage;
290 void Button::SetBackgroundImage( Image image )
292 SetBackgroundImage( ImageActor::New( image ) );
295 void Button::SetBackgroundImage( Actor image )
297 Toolkit::Button handle( GetOwner() );
298 mPainter->SetBackgroundImage( handle, image );
301 Actor Button::GetBackgroundImage() const
303 return mBackgroundImage;
306 Actor& Button::GetBackgroundImage()
308 return mBackgroundImage;
311 void Button::SetDisabledImage( Image image )
313 SetDisabledImage( ImageActor::New( image ) );
316 void Button::SetDisabledImage( Actor image )
318 Toolkit::Button handle( GetOwner() );
319 mPainter->SetDisabledImage( handle, image );
322 Actor Button::GetDisabledImage() const
324 return mDisabledImage;
327 Actor& Button::GetDisabledImage()
329 return mDisabledImage;
332 void Button::SetDisabledSelectedImage( Image image )
334 SetDisabledSelectedImage( ImageActor::New( image ) );
337 void Button::SetDisabledSelectedImage( Actor image )
339 Toolkit::Button handle( GetOwner() );
340 mPainter->SetDisabledSelectedImage( handle, image );
343 Actor Button::GetDisabledSelectedImage() const
345 return mDisabledSelectedImage;
348 Actor& Button::GetDisabledSelectedImage()
350 return mDisabledSelectedImage;
353 void Button::SetDisabledBackgroundImage( Image image )
355 SetDisabledBackgroundImage( ImageActor::New( image ) );
358 void Button::SetDisabledBackgroundImage( Actor image )
360 Toolkit::Button handle( GetOwner() );
361 mPainter->SetDisabledBackgroundImage( handle, image );
364 Actor Button::GetDisabledBackgroundImage() const
366 return mDisabledBackgroundImage;
369 Actor& Button::GetDisabledBackgroundImage()
371 return mDisabledBackgroundImage;
374 Actor& Button::GetFadeOutButtonImage()
376 return mFadeOutButtonImage;
379 Actor& Button::GetFadeOutSelectedImage()
381 return mFadeOutSelectedImage;
384 Actor& Button::GetFadeOutBackgroundImage()
386 return mFadeOutBackgroundImage;
389 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
393 Dali::BaseHandle handle( object );
395 Toolkit::Button button = Toolkit::Button::DownCast( handle );
397 DALI_ASSERT_ALWAYS( button );
399 if( Toolkit::Button::ACTION_BUTTON_CLICK == actionName )
401 GetImplementation( button ).DoClickAction( attributes );
408 void Button::DoClickAction( const PropertyValueContainer& attributes )
410 // Prevents the button signals from doing a recursive loop by sending an action
411 // and re-emitting the signals.
412 if( !mClickActionPerforming )
414 mClickActionPerforming = true;
418 mClickActionPerforming = false;
422 void Button::OnAnimationTimeSet( float animationTime )
424 mPainter->SetAnimationTime( animationTime );
427 float Button::OnAnimationTimeRequested() const
429 return mPainter->GetAnimationTime();
432 void Button::OnButtonStageDisconnection()
434 if( ButtonDown == mState )
436 if( !mTogglableButton )
438 Toolkit::Button handle( GetOwner() );
440 // Notifies the painter the button has been released.
441 mPainter->Released( handle );
445 mAutoRepeatingTimer.Reset();
451 void Button::OnButtonDown()
453 if( !mTogglableButton )
455 Toolkit::Button handle( GetOwner() );
457 // Notifies the painter the button has been pressed.
458 mPainter->Pressed( handle );
462 SetUpTimer( mInitialAutoRepeatingDelay );
466 mPressedSignal.Emit( handle );
470 void Button::OnButtonUp()
472 if( ButtonDown == mState )
474 if( mTogglableButton )
476 SetSelected( !mSelected );
480 Toolkit::Button handle( GetOwner() );
482 // Notifies the painter the button has been clicked.
483 mPainter->Released( handle );
484 mPainter->Clicked( handle );
488 mAutoRepeatingTimer.Reset();
492 mReleasedSignal.Emit( handle );
493 mClickedSignal.Emit( handle );
498 void Button::OnTouchPointLeave()
500 if( ButtonDown == mState )
502 if( !mTogglableButton )
504 Toolkit::Button handle( GetOwner() );
506 // Notifies the painter the button has been released.
507 mPainter->Released( handle );
511 mAutoRepeatingTimer.Reset();
515 mReleasedSignal.Emit( handle );
520 void Button::OnTouchPointInterrupted()
525 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
527 return mPressedSignal;
530 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
532 return mReleasedSignal;
535 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
537 return mClickedSignal;
540 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
542 return mStateChangedSignal;
545 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
547 Dali::BaseHandle handle( object );
549 bool connected( true );
550 Toolkit::Button button = Toolkit::Button::DownCast(handle);
552 if( Toolkit::Button::SIGNAL_PRESSED == signalName )
554 button.PressedSignal().Connect( tracker, functor );
556 else if( Toolkit::Button::SIGNAL_RELEASED == signalName )
558 button.ReleasedSignal().Connect( tracker, functor );
560 else if( Dali::Toolkit::Button::SIGNAL_CLICKED == signalName )
562 button.ClickedSignal().Connect( tracker, functor );
564 else if( Dali::Toolkit::Button::SIGNAL_STATE_CHANGED == signalName )
566 button.StateChangedSignal().Connect( tracker, functor );
570 // signalName does not match any signal
577 bool Button::OnTouchEvent(const TouchEvent& event)
579 // Only events are processed when the button is not disabled and the touch event has only
581 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
583 switch( event.GetPoint(0).state )
585 case TouchPoint::Down:
587 OnButtonDown(); // Notification for derived classes.
589 // Sets the button state to ButtonDown.
595 OnButtonUp(); // Notification for derived classes.
597 // Sets the button state to ButtonUp.
601 case TouchPoint::Interrupted:
603 OnTouchPointInterrupted(); // Notification for derived classes.
605 // Sets the button state to the default (ButtonUp).
609 case TouchPoint::Leave:
611 OnTouchPointLeave(); // Notification for derived classes.
613 // Sets the button state to the default (ButtonUp).
617 case TouchPoint::Motion:
618 case TouchPoint::Stationary: // FALLTHROUGH
625 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
630 else if( 1 < event.GetPointCount() )
632 OnTouchPointLeave(); // Notification for derived classes.
634 // Sets the button state to the default (ButtonUp).
641 void Button::OnInitialize()
643 // Initialize the painter and notifies subclasses.
644 Toolkit::Button handle( GetOwner() );
647 mPainter->Initialize( handle );
652 mTapDetector = TapGestureDetector::New();
653 mTapDetector.Attach( self );
654 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
656 OnButtonInitialize();
658 self.SetKeyboardFocusable( true );
661 void Button::OnActivated()
663 // When the button is activated, it performs the click action
664 PropertyValueContainer attributes;
665 DoClickAction( attributes );
668 void Button::OnControlSizeSet(const Vector3& targetSize)
670 Toolkit::Button handle( GetOwner() );
673 mPainter->SetSize( handle, targetSize );
677 void Button::OnTap(Actor actor, const TapGesture& tap)
682 void Button::SetUpTimer( float delay )
684 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
685 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
686 mAutoRepeatingTimer.Start();
689 bool Button::AutoRepeatingSlot()
691 bool consumed = false;
694 // Restart the autorepeat timer.
695 SetUpTimer( mNextAutoRepeatingDelay );
697 Toolkit::Button handle( GetOwner() );
699 // Notifies the painter the button has been pressed.
700 mPainter->Pressed( handle );
703 consumed = mReleasedSignal.Emit( handle );
704 consumed |= mClickedSignal.Emit( handle );
705 consumed |= mPressedSignal.Emit( handle );
711 void Button::OnControlStageDisconnection()
713 OnButtonStageDisconnection(); // Notification for derived classes.
717 void Button::SetPainter(ButtonPainterPtr painter)
722 Button::ButtonState Button::GetState()
727 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
729 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
735 case Toolkit::Button::PROPERTY_DISABLED:
737 GetImplementation( button ).SetDisabled( value.Get<bool>() );
741 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
743 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
747 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
749 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
753 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
755 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
759 case Toolkit::Button::PROPERTY_TOGGLABLE:
761 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
765 case Toolkit::Button::PROPERTY_SELECTED:
767 GetImplementation( button ).SetSelected( value.Get< bool >() );
771 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
773 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
777 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
779 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
783 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
785 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
789 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
791 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
798 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
800 Property::Value value;
802 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
806 switch ( propertyIndex )
808 case Toolkit::Button::PROPERTY_DISABLED:
810 value = GetImplementation( button ).mDisabled;
814 case Toolkit::Button::PROPERTY_AUTO_REPEATING:
816 value = GetImplementation( button ).mAutoRepeating;
820 case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY:
822 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
826 case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY:
828 value = GetImplementation( button ).mNextAutoRepeatingDelay;
832 case Toolkit::Button::PROPERTY_TOGGLABLE:
834 value = GetImplementation( button ).mTogglableButton;
838 case Toolkit::Button::PROPERTY_SELECTED:
840 value = GetImplementation( button ).mSelected;
844 case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR:
847 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonImage, map );
852 case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR:
855 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedImage, map );
860 case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR:
863 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledImage, map );
868 case Toolkit::Button::PROPERTY_LABEL_ACTOR:
871 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
881 } // namespace Internal
883 } // namespace Toolkit