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 <cstring> // for strcmp
23 #include <dali/public-api/events/touch-event.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/object/type-registry-helper.h>
26 #include <dali/public-api/actors/image-actor.h>
27 #include <dali/public-api/scripting/scripting.h>
30 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
33 * Button states and contents
34 * (3) mSelectedContent
35 * (2) mButtonContent (2) mSelectedBackgroundContent
36 * (1) mBackgroundContent (1) mBackgroundContent
37 * < unselected > ----------------------- < selected >
39 * | OnDisabled() | OnDisabled()
41 * < disabled > < disabled-selected >
42 * (2) mDisabledContent (2) mDisabledSelectedContent
43 * (1) mDisabledBackgroundContent (1) mDisabledBackgroundContent
45 * The drawing order of child actors is as follows.
48 * | mButtonContent / mSelectedContent / mDisabledContent / mDisabledSelectedContent
49 * | mSelectedBackgroundContent
50 * Bottom mBackgroundContent / mDisabledBackgroundContent
52 * Some of contents may be missed.
53 * And 2 images - fade-in image and fade-out image - in the same layer can be shown during the transition animation. Fade-in image should be above fade-out image.
70 // empty handle as we cannot create button (but type registered for clicked signal)
74 // Setup properties, signals and actions using the type-registry.
75 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create );
77 DALI_PROPERTY_REGISTRATION( Button, "disabled", BOOLEAN, DISABLED )
78 DALI_PROPERTY_REGISTRATION( Button, "auto-repeating", BOOLEAN, AUTO_REPEATING )
79 DALI_PROPERTY_REGISTRATION( Button, "initial-auto-repeating-delay", FLOAT, INITIAL_AUTO_REPEATING_DELAY )
80 DALI_PROPERTY_REGISTRATION( Button, "next-auto-repeating-delay", FLOAT, NEXT_AUTO_REPEATING_DELAY )
81 DALI_PROPERTY_REGISTRATION( Button, "togglable", BOOLEAN, TOGGLABLE )
82 DALI_PROPERTY_REGISTRATION( Button, "selected", BOOLEAN, SELECTED )
83 DALI_PROPERTY_REGISTRATION( Button, "normal-state-actor", MAP, NORMAL_STATE_ACTOR )
84 DALI_PROPERTY_REGISTRATION( Button, "selected-state-actor", MAP, SELECTED_STATE_ACTOR )
85 DALI_PROPERTY_REGISTRATION( Button, "disabled-state-actor", MAP, DISABLED_STATE_ACTOR )
86 DALI_PROPERTY_REGISTRATION( Button, "label-actor", MAP, LABEL_ACTOR )
88 DALI_SIGNAL_REGISTRATION( Button, "pressed", SIGNAL_PRESSED )
89 DALI_SIGNAL_REGISTRATION( Button, "released", SIGNAL_RELEASED )
90 DALI_SIGNAL_REGISTRATION( Button, "clicked", SIGNAL_CLICKED )
91 DALI_SIGNAL_REGISTRATION( Button, "state-changed", SIGNAL_STATE_CHANGED )
93 DALI_ACTION_REGISTRATION( Button, "button-click", ACTION_BUTTON_CLICK )
95 DALI_TYPE_REGISTRATION_END()
97 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
98 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
100 } // unnamed namespace
103 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
104 mAutoRepeatingTimer(),
106 mAutoRepeating( false ),
107 mTogglableButton( false ),
109 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
110 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
111 mAnimationTime( 0.0f ),
112 mClickActionPerforming( false ),
114 mPaintState( UnselectedState )
120 if( mAutoRepeatingTimer )
122 mAutoRepeatingTimer.Reset();
126 void Button::SetDisabled( bool disabled )
128 if( disabled != mDisabled )
130 unsigned int backgroundIndex;
131 unsigned int buttonIndex;
133 bool animationStarted = false;
135 mDisabled = disabled;
137 switch( mPaintState )
139 case UnselectedState:
141 buttonIndex = FindChildIndex( mLabel );
142 InsertChild( buttonIndex, mDisabledContent );
144 if( mBackgroundContent )
153 InsertChild( backgroundIndex, mDisabledBackgroundContent );
155 animationStarted = OnDisabled();
157 if( animationStarted )
159 mPaintState = UnselectedDisabledTransition;
163 mPaintState = DisabledUnselectedState;
169 buttonIndex = FindChildIndex( mLabel );
170 InsertChild( buttonIndex, mDisabledSelectedContent );
172 if( mBackgroundContent )
181 InsertChild( backgroundIndex, mDisabledBackgroundContent );
183 animationStarted = OnDisabled();
185 if( animationStarted )
187 mPaintState = SelectedDisabledTransition;
191 mPaintState = DisabledSelectedState;
195 case DisabledUnselectedState:
197 buttonIndex = FindChildIndex( mLabel );
198 InsertChild( buttonIndex, mButtonContent );
200 if( mDisabledBackgroundContent )
209 InsertChild( backgroundIndex, mBackgroundContent );
211 animationStarted = OnDisabled();
213 if( animationStarted )
215 mPaintState = DisabledUnselectedTransition;
219 mPaintState = UnselectedState;
223 case DisabledSelectedState:
225 buttonIndex = FindChildIndex( mLabel );
226 InsertChild( buttonIndex, mSelectedContent );
228 if( mDisabledBackgroundContent )
237 InsertChild( backgroundIndex, mSelectedBackgroundContent );
238 InsertChild( backgroundIndex, mBackgroundContent );
240 animationStarted = OnDisabled();
242 if( animationStarted )
244 mPaintState = DisabledSelectedTransition;
248 mPaintState = SelectedState;
252 case UnselectedSelectedTransition:
254 buttonIndex = FindChildIndex( mLabel );
255 InsertChild( buttonIndex, mDisabledSelectedContent );
257 if( mBackgroundContent )
266 InsertChild( backgroundIndex, mDisabledBackgroundContent );
268 animationStarted = OnDisabled();
270 if( animationStarted )
272 mPaintState = SelectedDisabledTransition;
276 mPaintState = DisabledSelectedState;
280 case SelectedUnselectedTransition:
282 buttonIndex = FindChildIndex( mLabel );
283 InsertChild( buttonIndex, mDisabledContent );
285 if( mBackgroundContent )
294 InsertChild( backgroundIndex, mDisabledBackgroundContent );
296 animationStarted = OnDisabled();
298 if( animationStarted )
300 mPaintState = UnselectedDisabledTransition;
304 mPaintState = DisabledUnselectedState;
308 case UnselectedDisabledTransition:
310 animationStarted = OnDisabled();
312 if( animationStarted )
314 mPaintState = DisabledUnselectedTransition;
318 mPaintState = UnselectedState;
322 case DisabledUnselectedTransition:
324 animationStarted = OnDisabled();
326 if( animationStarted )
328 mPaintState = UnselectedDisabledTransition;
332 mPaintState = DisabledUnselectedState;
336 case SelectedDisabledTransition:
338 animationStarted = OnDisabled();
340 if( animationStarted )
342 mPaintState = DisabledSelectedTransition;
346 mPaintState = SelectedState;
350 case DisabledSelectedTransition:
352 animationStarted = OnDisabled();
354 if( animationStarted )
356 mPaintState = SelectedDisabledTransition;
360 mPaintState = DisabledSelectedState;
368 bool Button::IsDisabled() const
373 void Button::SetAutoRepeating( bool autoRepeating )
375 mAutoRepeating = autoRepeating;
377 // An autorepeating button can't be a togglable button.
380 mTogglableButton = false;
384 // Emit a signal is not wanted, only change the appearance.
385 SetSelected( false, false );
390 bool Button::IsAutoRepeating() const
392 return mAutoRepeating;
395 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
397 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
398 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
401 float Button::GetInitialAutoRepeatingDelay() const
403 return mInitialAutoRepeatingDelay;
406 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
408 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
409 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
412 float Button::GetNextAutoRepeatingDelay() const
414 return mNextAutoRepeatingDelay;
417 void Button::SetTogglableButton( bool togglable )
419 mTogglableButton = togglable;
421 // A togglable button can't be an autorepeating button.
424 mAutoRepeating = false;
428 bool Button::IsTogglableButton() const
430 return mTogglableButton;
433 void Button::SetSelected( bool selected )
435 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
437 SetSelected( selected, true );
441 void Button::SetSelected( bool selected, bool emitSignal )
443 unsigned int buttonIndex, backgroundIndex;
444 bool animationStarted = false;
446 mSelected = selected;
448 switch( mPaintState )
450 case UnselectedState:
452 buttonIndex = FindChildIndex( mLabel );
453 InsertChild( buttonIndex, mSelectedContent );
455 if( mBackgroundContent )
464 InsertChild( backgroundIndex, mSelectedBackgroundContent );
466 // Notifies the derived class the button has been selected.
467 animationStarted = OnSelected();
469 if( animationStarted )
471 mPaintState = UnselectedSelectedTransition;
475 mPaintState = SelectedState;
481 buttonIndex = FindChildIndex( mLabel );
482 InsertChild( buttonIndex, mButtonContent );
484 // Notifies the derived class the button has been selected.
485 animationStarted = OnSelected();
487 if( animationStarted )
489 mPaintState = SelectedUnselectedTransition;
493 mPaintState = UnselectedState;
497 case UnselectedSelectedTransition:
499 // Notifies the derived class the button has been selected.
500 animationStarted = OnSelected();
502 if( animationStarted )
504 mPaintState = SelectedUnselectedTransition;
508 mPaintState = UnselectedState;
512 case SelectedUnselectedTransition:
514 // Notifies the derived class the button has been selected.
515 animationStarted = OnSelected();
517 if( animationStarted )
519 mPaintState = UnselectedSelectedTransition;
523 mPaintState = SelectedState;
527 case DisabledUnselectedTransition:
529 buttonIndex = FindChildIndex( mLabel );
530 InsertChild( buttonIndex, mSelectedContent );
532 if( mDisabledBackgroundContent )
534 if( mBackgroundContent )
543 else if( mBackgroundContent )
552 InsertChild( backgroundIndex, mSelectedBackgroundContent );
554 // Notifies the derived class the button has been selected.
555 animationStarted = OnSelected();
557 if( animationStarted )
559 mPaintState = UnselectedSelectedTransition;
563 mPaintState = SelectedState;
567 case DisabledSelectedTransition:
569 buttonIndex = FindChildIndex( mLabel );
570 InsertChild( buttonIndex, mButtonContent );
572 // Notifies the derived class the button has been selected.
573 animationStarted = OnSelected();
575 if( animationStarted )
577 mPaintState = SelectedUnselectedTransition;
581 mPaintState = UnselectedState;
593 Toolkit::Button handle( GetOwner() );
596 mStateChangedSignal.Emit( handle );
602 bool Button::IsSelected() const
604 return mTogglableButton && mSelected;
607 void Button::SetAnimationTime( float animationTime )
609 mAnimationTime = animationTime;
612 float Button::GetAnimationTime() const
614 return mAnimationTime;
617 void Button::SetLabel( const std::string& label )
619 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( label );
620 textLabel.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
621 textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
622 textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
623 textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
625 SetLabel( textLabel );
628 void Button::SetLabel( Actor label )
630 if( mLabel != label )
632 if( mLabel && mLabel.GetParent() )
634 mLabel.GetParent().Remove( mLabel );
638 mLabel.SetPosition( 0.f, 0.f );
640 // label should be the top of the button
641 Self().Add( mLabel );
649 Actor Button::GetLabel() const
654 Actor& Button::GetLabel()
659 void Button::SetButtonImage( Actor image )
663 if( mButtonContent && mButtonContent.GetParent() )
665 Self().Remove( mButtonContent );
668 mButtonContent = image;
670 mButtonContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
671 mButtonContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
672 mButtonContent.SetPosition( 0.f, 0.f );
674 if( mPaintState == UnselectedState )
676 unsigned int index = FindChildIndex( mLabel );
678 Self().Insert( index, mButtonContent );
684 Actor Button::GetButtonImage() const
686 return mButtonContent;
689 Actor& Button::GetButtonImage()
691 return mButtonContent;
694 void Button::SetSelectedImage( Actor image )
698 if( mSelectedContent && mSelectedContent.GetParent() )
700 Self().Remove( mSelectedContent );
703 mSelectedContent = image;
705 mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
706 mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
707 mSelectedContent.SetPosition( 0.f, 0.f );
709 if( mPaintState == SelectedState )
711 unsigned int index = FindChildIndex( mLabel );
713 Self().Insert( index, mSelectedContent );
716 OnSelectedImageSet();
719 Actor Button::GetSelectedImage() const
721 return mSelectedContent;
724 Actor& Button::GetSelectedImage()
726 return mSelectedContent;
729 void Button::SetBackgroundImage( Actor image )
733 if( mBackgroundContent && mBackgroundContent.GetParent() )
735 Self().Remove( mBackgroundContent );
738 mBackgroundContent = image;
740 mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
741 mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
742 mBackgroundContent.SetPosition( 0.f, 0.f );
744 if( mPaintState == UnselectedState || mPaintState == SelectedState )
746 Self().Insert( 0, mBackgroundContent );
749 OnBackgroundImageSet();
752 Actor Button::GetBackgroundImage() const
754 return mBackgroundContent;
757 Actor& Button::GetBackgroundImage()
759 return mBackgroundContent;
762 void Button::SetSelectedBackgroundImage( Actor image )
766 if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
768 Self().Remove( mSelectedBackgroundContent );
771 mSelectedBackgroundContent = image;
773 mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
774 mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
775 mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
777 if( mPaintState == SelectedState )
779 if( mBackgroundContent )
781 Self().Insert( 1, mSelectedBackgroundContent );
785 Self().Insert( 0, mSelectedBackgroundContent );
789 OnSelectedBackgroundImageSet();
792 Actor Button::GetSelectedBackgroundImage() const
794 return mSelectedBackgroundContent;
797 Actor& Button::GetSelectedBackgroundImage()
799 return mSelectedBackgroundContent;
802 void Button::SetDisabledImage( Actor image )
806 if( mDisabledContent && mDisabledContent.GetParent() )
808 Self().Remove( mDisabledContent );
811 mDisabledContent = image;
813 mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
814 mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
815 mDisabledContent.SetPosition( 0.f, 0.f );
817 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
819 unsigned int index = FindChildIndex( mLabel );
821 Self().Insert( index, mDisabledContent );
824 OnDisabledImageSet();
827 Actor Button::GetDisabledImage() const
829 return mDisabledContent;
832 Actor& Button::GetDisabledImage()
834 return mDisabledContent;
837 void Button::SetDisabledSelectedImage( Actor image )
841 if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
843 Self().Remove( mDisabledSelectedContent );
846 mDisabledSelectedContent = image;
848 mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
849 mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
850 mDisabledSelectedContent.SetPosition( 0.f, 0.f );
852 if( mPaintState == DisabledSelectedState )
854 unsigned int index = FindChildIndex( mLabel );
856 Self().Insert( index, mDisabledSelectedContent );
859 OnDisabledSelectedImageSet();
862 Actor Button::GetDisabledSelectedImage() const
864 return mDisabledSelectedContent;
867 Actor& Button::GetDisabledSelectedImage()
869 return mDisabledSelectedContent;
872 void Button::SetDisabledBackgroundImage( Actor image )
876 if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
878 Self().Remove( mDisabledBackgroundContent );
881 mDisabledBackgroundContent = image;
883 mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
884 mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
885 mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
887 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
889 Self().Insert( 0, mDisabledBackgroundContent );
892 OnDisabledBackgroundImageSet();
895 Actor Button::GetDisabledBackgroundImage() const
897 return mDisabledBackgroundContent;
900 Actor& Button::GetDisabledBackgroundImage()
902 return mDisabledBackgroundContent;
905 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
909 Dali::BaseHandle handle( object );
911 Toolkit::Button button = Toolkit::Button::DownCast( handle );
913 DALI_ASSERT_ALWAYS( button );
915 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
917 GetImplementation( button ).DoClickAction( attributes );
924 void Button::DoClickAction( const PropertyValueContainer& attributes )
926 // Prevents the button signals from doing a recursive loop by sending an action
927 // and re-emitting the signals.
928 if( !mClickActionPerforming )
930 mClickActionPerforming = true;
934 mClickActionPerforming = false;
938 void Button::UpdatePaintTransitionState()
940 switch( mPaintState )
942 case UnselectedSelectedTransition:
944 RemoveChild( mButtonContent );
945 mPaintState = SelectedState;
948 case SelectedUnselectedTransition:
950 RemoveChild( mSelectedBackgroundContent );
951 RemoveChild( mSelectedContent );
952 mPaintState = UnselectedState;
955 case UnselectedDisabledTransition:
957 RemoveChild( mBackgroundContent );
958 RemoveChild( mButtonContent );
959 mPaintState = DisabledUnselectedState;
962 case DisabledUnselectedTransition:
964 RemoveChild( mDisabledBackgroundContent );
965 RemoveChild( mDisabledContent );
966 mPaintState = UnselectedState;
969 case SelectedDisabledTransition:
971 RemoveChild( mBackgroundContent );
972 RemoveChild( mSelectedBackgroundContent );
973 RemoveChild( mSelectedContent );
974 mPaintState = DisabledSelectedState;
977 case DisabledSelectedTransition:
979 RemoveChild( mDisabledBackgroundContent );
980 RemoveChild( mDisabledSelectedContent );
981 mPaintState = SelectedState;
991 void Button::OnButtonStageDisconnection()
993 if( ButtonDown == mState )
995 if( !mTogglableButton )
1001 mAutoRepeatingTimer.Reset();
1007 void Button::OnButtonDown()
1009 if( !mTogglableButton )
1011 Toolkit::Button handle( GetOwner() );
1015 if( mAutoRepeating )
1017 SetUpTimer( mInitialAutoRepeatingDelay );
1021 mPressedSignal.Emit( handle );
1025 void Button::OnButtonUp()
1027 if( ButtonDown == mState )
1029 if( mTogglableButton )
1031 SetSelected( !mSelected );
1037 if( mAutoRepeating )
1039 mAutoRepeatingTimer.Reset();
1042 Toolkit::Button handle( GetOwner() );
1045 mReleasedSignal.Emit( handle );
1046 mClickedSignal.Emit( handle );
1051 void Button::OnTouchPointLeave()
1053 if( ButtonDown == mState )
1055 if( !mTogglableButton )
1057 Toolkit::Button handle( GetOwner() );
1061 if( mAutoRepeating )
1063 mAutoRepeatingTimer.Reset();
1067 mReleasedSignal.Emit( handle );
1072 void Button::OnTouchPointInterrupted()
1074 OnTouchPointLeave();
1077 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
1079 return mPressedSignal;
1082 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
1084 return mReleasedSignal;
1087 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
1089 return mClickedSignal;
1092 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
1094 return mStateChangedSignal;
1097 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1099 Dali::BaseHandle handle( object );
1101 bool connected( true );
1102 Toolkit::Button button = Toolkit::Button::DownCast( handle );
1104 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
1106 button.PressedSignal().Connect( tracker, functor );
1108 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
1110 button.ReleasedSignal().Connect( tracker, functor );
1112 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
1114 button.ClickedSignal().Connect( tracker, functor );
1116 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
1118 button.StateChangedSignal().Connect( tracker, functor );
1122 // signalName does not match any signal
1129 bool Button::OnTouchEvent(const TouchEvent& event)
1131 // Only events are processed when the button is not disabled and the touch event has only
1133 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
1135 switch( event.GetPoint(0).state )
1137 case TouchPoint::Down:
1139 OnButtonDown(); // Notification for derived classes.
1141 // Sets the button state to ButtonDown.
1142 mState = ButtonDown;
1145 case TouchPoint::Up:
1147 OnButtonUp(); // Notification for derived classes.
1149 // Sets the button state to ButtonUp.
1153 case TouchPoint::Interrupted:
1155 OnTouchPointInterrupted(); // Notification for derived classes.
1157 // Sets the button state to the default (ButtonUp).
1161 case TouchPoint::Leave:
1163 OnTouchPointLeave(); // Notification for derived classes.
1165 // Sets the button state to the default (ButtonUp).
1169 case TouchPoint::Motion:
1170 case TouchPoint::Stationary: // FALLTHROUGH
1177 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
1182 else if( 1 < event.GetPointCount() )
1184 OnTouchPointLeave(); // Notification for derived classes.
1186 // Sets the button state to the default (ButtonUp).
1193 void Button::OnInitialize()
1195 Actor self = Self();
1197 mTapDetector = TapGestureDetector::New();
1198 mTapDetector.Attach( self );
1199 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
1201 OnButtonInitialize();
1203 self.SetDrawMode( DrawMode::OVERLAY );
1204 self.SetKeyboardFocusable( true );
1207 void Button::OnActivated()
1209 // When the button is activated, it performs the click action
1210 PropertyValueContainer attributes;
1211 DoClickAction( attributes );
1214 void Button::OnControlStageDisconnection()
1216 OnButtonStageDisconnection(); // Notification for derived classes.
1220 void Button::OnTap(Actor actor, const TapGesture& tap)
1225 void Button::SetUpTimer( float delay )
1227 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
1228 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
1229 mAutoRepeatingTimer.Start();
1232 bool Button::AutoRepeatingSlot()
1234 bool consumed = false;
1237 // Restart the autorepeat timer.
1238 SetUpTimer( mNextAutoRepeatingDelay );
1242 Toolkit::Button handle( GetOwner() );
1245 consumed = mReleasedSignal.Emit( handle );
1246 consumed |= mClickedSignal.Emit( handle );
1247 consumed |= mPressedSignal.Emit( handle );
1253 void Button::Pressed()
1255 unsigned int buttonIndex, backgroundIndex;
1256 bool animationStarted = false;
1258 switch( mPaintState )
1260 case UnselectedState:
1262 buttonIndex = FindChildIndex( mLabel );
1263 InsertChild( buttonIndex, mSelectedContent );
1265 if( mBackgroundContent )
1267 backgroundIndex = 1;
1271 backgroundIndex = 0;
1274 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1276 // Notifies the derived class the button has been pressed.
1277 animationStarted = OnPressed();
1279 if( animationStarted )
1281 mPaintState = UnselectedSelectedTransition;
1285 mPaintState = SelectedState;
1289 case SelectedUnselectedTransition:
1291 // Notifies the derived class the button has been pressed.
1292 animationStarted = OnPressed();
1294 if( animationStarted )
1296 mPaintState = UnselectedSelectedTransition;
1300 mPaintState = SelectedState;
1304 case DisabledUnselectedTransition:
1306 buttonIndex = FindChildIndex( mLabel );
1307 InsertChild( buttonIndex, mSelectedContent );
1309 if( mDisabledBackgroundContent )
1311 if( mBackgroundContent )
1313 backgroundIndex = 2;
1317 backgroundIndex = 1;
1320 else if( mBackgroundContent )
1322 backgroundIndex = 1;
1326 backgroundIndex = 0;
1329 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1331 // Notifies the derived class the button has been pressed.
1332 animationStarted = OnPressed();
1334 if( animationStarted )
1336 mPaintState = UnselectedSelectedTransition;
1340 mPaintState = SelectedState;
1349 void Button::Released()
1351 unsigned int buttonIndex;
1352 bool animationStarted = false;
1354 switch( mPaintState )
1358 buttonIndex = FindChildIndex( mLabel );
1359 InsertChild( buttonIndex, mButtonContent );
1361 // Notifies the derived class the button has been released.
1362 animationStarted = OnReleased();
1364 if( animationStarted )
1366 mPaintState = SelectedUnselectedTransition;
1370 mPaintState = UnselectedState;
1374 case UnselectedSelectedTransition:
1376 // Notifies the derived class the button has been released.
1377 animationStarted = OnReleased();
1379 if( animationStarted )
1381 mPaintState = SelectedUnselectedTransition;
1385 mPaintState = UnselectedState;
1389 case DisabledSelectedTransition:
1391 buttonIndex = FindChildIndex( mLabel );
1392 InsertChild( buttonIndex, mButtonContent );
1394 // Notifies the derived class the button has been released.
1395 animationStarted = OnReleased();
1397 if( animationStarted )
1399 mPaintState = SelectedUnselectedTransition;
1403 mPaintState = UnselectedState;
1414 Button::ButtonState Button::GetState()
1419 Button::PaintState Button::GetPaintState()
1424 void Button::InsertChild( unsigned int index, Actor& actor )
1428 Self().Insert( index, actor);
1432 void Button::RemoveChild( Actor& actor )
1434 if( actor && actor.GetParent() )
1436 Self().Remove( actor );
1440 unsigned int Button::FindChildIndex( Actor& actor )
1442 Actor self = Self();
1443 unsigned int childrenNum = self.GetChildCount();
1445 for( unsigned int i = 0; i < childrenNum; i++ )
1447 Actor child = self.GetChildAt( i );
1448 if( child == actor )
1457 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1459 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1465 case Toolkit::Button::Property::DISABLED:
1467 GetImplementation( button ).SetDisabled( value.Get<bool>() );
1471 case Toolkit::Button::Property::AUTO_REPEATING:
1473 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
1477 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1479 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
1483 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1485 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
1489 case Toolkit::Button::Property::TOGGLABLE:
1491 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
1495 case Toolkit::Button::Property::SELECTED:
1497 GetImplementation( button ).SetSelected( value.Get< bool >() );
1501 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1503 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1507 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1509 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1513 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1515 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1519 case Toolkit::Button::Property::LABEL_ACTOR:
1521 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
1528 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
1530 Property::Value value;
1532 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1536 switch ( propertyIndex )
1538 case Toolkit::Button::Property::DISABLED:
1540 value = GetImplementation( button ).mDisabled;
1544 case Toolkit::Button::Property::AUTO_REPEATING:
1546 value = GetImplementation( button ).mAutoRepeating;
1550 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1552 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
1556 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1558 value = GetImplementation( button ).mNextAutoRepeatingDelay;
1562 case Toolkit::Button::Property::TOGGLABLE:
1564 value = GetImplementation( button ).mTogglableButton;
1568 case Toolkit::Button::Property::SELECTED:
1570 value = GetImplementation( button ).mSelected;
1574 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1577 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonContent, map );
1582 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1585 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedContent, map );
1590 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1593 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledContent, map );
1598 case Toolkit::Button::Property::LABEL_ACTOR:
1601 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
1611 } // namespace Internal
1613 } // namespace Toolkit