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>
32 * Button states and contents
33 * (3) mSelectedContent
34 * (2) mButtonContent (2) mSelectedBackgroundContent
35 * (1) mBackgroundContent (1) mBackgroundContent
36 * < unselected > ----------------------- < selected >
38 * | OnDisabled() | OnDisabled()
40 * < disabled > < disabled-selected >
41 * (2) mDisabledContent (2) mDisabledSelectedContent
42 * (1) mDisabledBackgroundContent (1) mDisabledBackgroundContent
44 * The drawing order of child actors is as follows.
47 * | mButtonContent / mSelectedContent / mDisabledContent / mDisabledSelectedContent
48 * | mSelectedBackgroundContent
49 * Bottom mBackgroundContent / mDisabledBackgroundContent
51 * Some of contents may be missed.
52 * 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.
69 // empty handle as we cannot create button (but type registered for clicked signal)
73 // Setup properties, signals and actions using the type-registry.
74 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create );
76 DALI_PROPERTY_REGISTRATION( Button, "disabled", BOOLEAN, DISABLED )
77 DALI_PROPERTY_REGISTRATION( Button, "auto-repeating", BOOLEAN, AUTO_REPEATING )
78 DALI_PROPERTY_REGISTRATION( Button, "initial-auto-repeating-delay", FLOAT, INITIAL_AUTO_REPEATING_DELAY )
79 DALI_PROPERTY_REGISTRATION( Button, "next-auto-repeating-delay", FLOAT, NEXT_AUTO_REPEATING_DELAY )
80 DALI_PROPERTY_REGISTRATION( Button, "togglable", BOOLEAN, TOGGLABLE )
81 DALI_PROPERTY_REGISTRATION( Button, "selected", BOOLEAN, SELECTED )
82 DALI_PROPERTY_REGISTRATION( Button, "normal-state-actor", MAP, NORMAL_STATE_ACTOR )
83 DALI_PROPERTY_REGISTRATION( Button, "selected-state-actor", MAP, SELECTED_STATE_ACTOR )
84 DALI_PROPERTY_REGISTRATION( Button, "disabled-state-actor", MAP, DISABLED_STATE_ACTOR )
85 DALI_PROPERTY_REGISTRATION( Button, "label-actor", MAP, LABEL_ACTOR )
87 DALI_SIGNAL_REGISTRATION( Button, "pressed", SIGNAL_PRESSED )
88 DALI_SIGNAL_REGISTRATION( Button, "released", SIGNAL_RELEASED )
89 DALI_SIGNAL_REGISTRATION( Button, "clicked", SIGNAL_CLICKED )
90 DALI_SIGNAL_REGISTRATION( Button, "state-changed", SIGNAL_STATE_CHANGED )
92 DALI_ACTION_REGISTRATION( Button, "button-click", ACTION_BUTTON_CLICK )
94 DALI_TYPE_REGISTRATION_END()
96 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
97 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
99 } // unnamed namespace
102 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
103 mAutoRepeatingTimer(),
105 mAutoRepeating( false ),
106 mTogglableButton( false ),
108 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
109 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
110 mAnimationTime( 0.0f ),
111 mClickActionPerforming( false ),
113 mPaintState( UnselectedState )
119 if( mAutoRepeatingTimer )
121 mAutoRepeatingTimer.Reset();
125 void Button::SetDisabled( bool disabled )
127 if( disabled != mDisabled )
129 unsigned int backgroundIndex;
130 unsigned int buttonIndex;
132 bool animationStarted = false;
134 mDisabled = disabled;
136 switch( mPaintState )
138 case UnselectedState:
140 buttonIndex = FindChildIndex( mLabel );
141 InsertChild( buttonIndex, mDisabledContent );
143 if( mBackgroundContent )
152 InsertChild( backgroundIndex, mDisabledBackgroundContent );
154 animationStarted = OnDisabled();
156 if( animationStarted )
158 mPaintState = UnselectedDisabledTransition;
162 mPaintState = DisabledUnselectedState;
168 buttonIndex = FindChildIndex( mLabel );
169 InsertChild( buttonIndex, mDisabledSelectedContent );
171 if( mBackgroundContent )
180 InsertChild( backgroundIndex, mDisabledBackgroundContent );
182 animationStarted = OnDisabled();
184 if( animationStarted )
186 mPaintState = SelectedDisabledTransition;
190 mPaintState = DisabledSelectedState;
194 case DisabledUnselectedState:
196 buttonIndex = FindChildIndex( mLabel );
197 InsertChild( buttonIndex, mButtonContent );
199 if( mDisabledBackgroundContent )
208 InsertChild( backgroundIndex, mBackgroundContent );
210 animationStarted = OnDisabled();
212 if( animationStarted )
214 mPaintState = DisabledUnselectedTransition;
218 mPaintState = UnselectedState;
222 case DisabledSelectedState:
224 buttonIndex = FindChildIndex( mLabel );
225 InsertChild( buttonIndex, mSelectedContent );
227 if( mDisabledBackgroundContent )
236 InsertChild( backgroundIndex, mSelectedBackgroundContent );
237 InsertChild( backgroundIndex, mBackgroundContent );
239 animationStarted = OnDisabled();
241 if( animationStarted )
243 mPaintState = DisabledSelectedTransition;
247 mPaintState = SelectedState;
251 case UnselectedSelectedTransition:
253 buttonIndex = FindChildIndex( mLabel );
254 InsertChild( buttonIndex, mDisabledSelectedContent );
256 if( mBackgroundContent )
265 InsertChild( backgroundIndex, mDisabledBackgroundContent );
267 animationStarted = OnDisabled();
269 if( animationStarted )
271 mPaintState = SelectedDisabledTransition;
275 mPaintState = DisabledSelectedState;
279 case SelectedUnselectedTransition:
281 buttonIndex = FindChildIndex( mLabel );
282 InsertChild( buttonIndex, mDisabledContent );
284 if( mBackgroundContent )
293 InsertChild( backgroundIndex, mDisabledBackgroundContent );
295 animationStarted = OnDisabled();
297 if( animationStarted )
299 mPaintState = UnselectedDisabledTransition;
303 mPaintState = DisabledUnselectedState;
307 case UnselectedDisabledTransition:
309 animationStarted = OnDisabled();
311 if( animationStarted )
313 mPaintState = DisabledUnselectedTransition;
317 mPaintState = UnselectedState;
321 case DisabledUnselectedTransition:
323 animationStarted = OnDisabled();
325 if( animationStarted )
327 mPaintState = UnselectedDisabledTransition;
331 mPaintState = DisabledUnselectedState;
335 case SelectedDisabledTransition:
337 animationStarted = OnDisabled();
339 if( animationStarted )
341 mPaintState = DisabledSelectedTransition;
345 mPaintState = SelectedState;
349 case DisabledSelectedTransition:
351 animationStarted = OnDisabled();
353 if( animationStarted )
355 mPaintState = SelectedDisabledTransition;
359 mPaintState = DisabledSelectedState;
367 bool Button::IsDisabled() const
372 void Button::SetAutoRepeating( bool autoRepeating )
374 mAutoRepeating = autoRepeating;
376 // An autorepeating button can't be a togglable button.
379 mTogglableButton = false;
383 // Emit a signal is not wanted, only change the appearance.
384 SetSelected( false, false );
389 bool Button::IsAutoRepeating() const
391 return mAutoRepeating;
394 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
396 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
397 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
400 float Button::GetInitialAutoRepeatingDelay() const
402 return mInitialAutoRepeatingDelay;
405 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
407 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
408 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
411 float Button::GetNextAutoRepeatingDelay() const
413 return mNextAutoRepeatingDelay;
416 void Button::SetTogglableButton( bool togglable )
418 mTogglableButton = togglable;
420 // A togglable button can't be an autorepeating button.
423 mAutoRepeating = false;
427 bool Button::IsTogglableButton() const
429 return mTogglableButton;
432 void Button::SetSelected( bool selected )
434 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
436 SetSelected( selected, true );
440 void Button::SetSelected( bool selected, bool emitSignal )
442 unsigned int buttonIndex, backgroundIndex;
443 bool animationStarted = false;
445 mSelected = selected;
447 switch( mPaintState )
449 case UnselectedState:
451 buttonIndex = FindChildIndex( mLabel );
452 InsertChild( buttonIndex, mSelectedContent );
454 if( mBackgroundContent )
463 InsertChild( backgroundIndex, mSelectedBackgroundContent );
465 // Notifies the derived class the button has been selected.
466 animationStarted = OnSelected();
468 if( animationStarted )
470 mPaintState = UnselectedSelectedTransition;
474 mPaintState = SelectedState;
480 buttonIndex = FindChildIndex( mLabel );
481 InsertChild( buttonIndex, mButtonContent );
483 // Notifies the derived class the button has been selected.
484 animationStarted = OnSelected();
486 if( animationStarted )
488 mPaintState = SelectedUnselectedTransition;
492 mPaintState = UnselectedState;
496 case UnselectedSelectedTransition:
498 // Notifies the derived class the button has been selected.
499 animationStarted = OnSelected();
501 if( animationStarted )
503 mPaintState = SelectedUnselectedTransition;
507 mPaintState = UnselectedState;
511 case SelectedUnselectedTransition:
513 // Notifies the derived class the button has been selected.
514 animationStarted = OnSelected();
516 if( animationStarted )
518 mPaintState = UnselectedSelectedTransition;
522 mPaintState = SelectedState;
526 case DisabledUnselectedTransition:
528 buttonIndex = FindChildIndex( mLabel );
529 InsertChild( buttonIndex, mSelectedContent );
531 if( mDisabledBackgroundContent )
533 if( mBackgroundContent )
542 else if( mBackgroundContent )
551 InsertChild( backgroundIndex, mSelectedBackgroundContent );
553 // Notifies the derived class the button has been selected.
554 animationStarted = OnSelected();
556 if( animationStarted )
558 mPaintState = UnselectedSelectedTransition;
562 mPaintState = SelectedState;
566 case DisabledSelectedTransition:
568 buttonIndex = FindChildIndex( mLabel );
569 InsertChild( buttonIndex, mButtonContent );
571 // Notifies the derived class the button has been selected.
572 animationStarted = OnSelected();
574 if( animationStarted )
576 mPaintState = SelectedUnselectedTransition;
580 mPaintState = UnselectedState;
592 Toolkit::Button handle( GetOwner() );
595 mStateChangedSignal.Emit( handle );
601 bool Button::IsSelected() const
603 return mTogglableButton && mSelected;
606 void Button::SetAnimationTime( float animationTime )
608 mAnimationTime = animationTime;
611 float Button::GetAnimationTime() const
613 return mAnimationTime;
616 void Button::SetLabel( const std::string& label )
618 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( label );
619 SetLabel( textLabel );
622 void Button::SetLabel( Actor label )
624 if( mLabel != label )
626 if( mLabel && mLabel.GetParent() )
628 mLabel.GetParent().Remove( mLabel );
632 mLabel.SetPosition( 0.f, 0.f );
634 // label should be the top of the button
635 Self().Add( mLabel );
643 Actor Button::GetLabel() const
648 Actor& Button::GetLabel()
653 void Button::SetButtonImage( Actor image )
657 if( mButtonContent && mButtonContent.GetParent() )
659 Self().Remove( mButtonContent );
662 mButtonContent = image;
664 mButtonContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
665 mButtonContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
666 mButtonContent.SetPosition( 0.f, 0.f );
668 if( mPaintState == UnselectedState )
670 unsigned int index = FindChildIndex( mLabel );
672 Self().Insert( index, mButtonContent );
678 Actor Button::GetButtonImage() const
680 return mButtonContent;
683 Actor& Button::GetButtonImage()
685 return mButtonContent;
688 void Button::SetSelectedImage( Actor image )
692 if( mSelectedContent && mSelectedContent.GetParent() )
694 Self().Remove( mSelectedContent );
697 mSelectedContent = image;
699 mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
700 mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
701 mSelectedContent.SetPosition( 0.f, 0.f );
703 if( mPaintState == SelectedState )
705 unsigned int index = FindChildIndex( mLabel );
707 Self().Insert( index, mSelectedContent );
710 OnSelectedImageSet();
713 Actor Button::GetSelectedImage() const
715 return mSelectedContent;
718 Actor& Button::GetSelectedImage()
720 return mSelectedContent;
723 void Button::SetBackgroundImage( Actor image )
727 if( mBackgroundContent && mBackgroundContent.GetParent() )
729 Self().Remove( mBackgroundContent );
732 mBackgroundContent = image;
734 mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
735 mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
736 mBackgroundContent.SetPosition( 0.f, 0.f );
738 if( mPaintState == UnselectedState || mPaintState == SelectedState )
740 Self().Insert( 0, mBackgroundContent );
743 OnBackgroundImageSet();
746 Actor Button::GetBackgroundImage() const
748 return mBackgroundContent;
751 Actor& Button::GetBackgroundImage()
753 return mBackgroundContent;
756 void Button::SetSelectedBackgroundImage( Actor image )
760 if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
762 Self().Remove( mSelectedBackgroundContent );
765 mSelectedBackgroundContent = image;
767 mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
768 mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
769 mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
771 if( mPaintState == SelectedState )
773 if( mBackgroundContent )
775 Self().Insert( 1, mSelectedBackgroundContent );
779 Self().Insert( 0, mSelectedBackgroundContent );
783 OnSelectedBackgroundImageSet();
786 Actor Button::GetSelectedBackgroundImage() const
788 return mSelectedBackgroundContent;
791 Actor& Button::GetSelectedBackgroundImage()
793 return mSelectedBackgroundContent;
796 void Button::SetDisabledImage( Actor image )
800 if( mDisabledContent && mDisabledContent.GetParent() )
802 Self().Remove( mDisabledContent );
805 mDisabledContent = image;
807 mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
808 mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
809 mDisabledContent.SetPosition( 0.f, 0.f );
811 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
813 unsigned int index = FindChildIndex( mLabel );
815 Self().Insert( index, mDisabledContent );
818 OnDisabledImageSet();
821 Actor Button::GetDisabledImage() const
823 return mDisabledContent;
826 Actor& Button::GetDisabledImage()
828 return mDisabledContent;
831 void Button::SetDisabledSelectedImage( Actor image )
835 if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
837 Self().Remove( mDisabledSelectedContent );
840 mDisabledSelectedContent = image;
842 mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
843 mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
844 mDisabledSelectedContent.SetPosition( 0.f, 0.f );
846 if( mPaintState == DisabledSelectedState )
848 unsigned int index = FindChildIndex( mLabel );
850 Self().Insert( index, mDisabledSelectedContent );
853 OnDisabledSelectedImageSet();
856 Actor Button::GetDisabledSelectedImage() const
858 return mDisabledSelectedContent;
861 Actor& Button::GetDisabledSelectedImage()
863 return mDisabledSelectedContent;
866 void Button::SetDisabledBackgroundImage( Actor image )
870 if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
872 Self().Remove( mDisabledBackgroundContent );
875 mDisabledBackgroundContent = image;
877 mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
878 mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
879 mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
881 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
883 Self().Insert( 0, mDisabledBackgroundContent );
886 OnDisabledBackgroundImageSet();
889 Actor Button::GetDisabledBackgroundImage() const
891 return mDisabledBackgroundContent;
894 Actor& Button::GetDisabledBackgroundImage()
896 return mDisabledBackgroundContent;
899 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
903 Dali::BaseHandle handle( object );
905 Toolkit::Button button = Toolkit::Button::DownCast( handle );
907 DALI_ASSERT_ALWAYS( button );
909 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
911 GetImplementation( button ).DoClickAction( attributes );
918 void Button::DoClickAction( const PropertyValueContainer& attributes )
920 // Prevents the button signals from doing a recursive loop by sending an action
921 // and re-emitting the signals.
922 if( !mClickActionPerforming )
924 mClickActionPerforming = true;
928 mClickActionPerforming = false;
932 void Button::UpdatePaintTransitionState()
934 switch( mPaintState )
936 case UnselectedSelectedTransition:
938 RemoveChild( mButtonContent );
939 mPaintState = SelectedState;
942 case SelectedUnselectedTransition:
944 RemoveChild( mSelectedBackgroundContent );
945 RemoveChild( mSelectedContent );
946 mPaintState = UnselectedState;
949 case UnselectedDisabledTransition:
951 RemoveChild( mBackgroundContent );
952 RemoveChild( mButtonContent );
953 mPaintState = DisabledUnselectedState;
956 case DisabledUnselectedTransition:
958 RemoveChild( mDisabledBackgroundContent );
959 RemoveChild( mDisabledContent );
960 mPaintState = UnselectedState;
963 case SelectedDisabledTransition:
965 RemoveChild( mBackgroundContent );
966 RemoveChild( mSelectedBackgroundContent );
967 RemoveChild( mSelectedContent );
968 mPaintState = DisabledSelectedState;
971 case DisabledSelectedTransition:
973 RemoveChild( mDisabledBackgroundContent );
974 RemoveChild( mDisabledSelectedContent );
975 mPaintState = SelectedState;
985 void Button::OnButtonStageDisconnection()
987 if( ButtonDown == mState )
989 if( !mTogglableButton )
995 mAutoRepeatingTimer.Reset();
1001 void Button::OnButtonDown()
1003 if( !mTogglableButton )
1005 Toolkit::Button handle( GetOwner() );
1009 if( mAutoRepeating )
1011 SetUpTimer( mInitialAutoRepeatingDelay );
1015 mPressedSignal.Emit( handle );
1019 void Button::OnButtonUp()
1021 if( ButtonDown == mState )
1023 if( mTogglableButton )
1025 SetSelected( !mSelected );
1031 if( mAutoRepeating )
1033 mAutoRepeatingTimer.Reset();
1036 Toolkit::Button handle( GetOwner() );
1039 mReleasedSignal.Emit( handle );
1040 mClickedSignal.Emit( handle );
1045 void Button::OnTouchPointLeave()
1047 if( ButtonDown == mState )
1049 if( !mTogglableButton )
1051 Toolkit::Button handle( GetOwner() );
1055 if( mAutoRepeating )
1057 mAutoRepeatingTimer.Reset();
1061 mReleasedSignal.Emit( handle );
1066 void Button::OnTouchPointInterrupted()
1068 OnTouchPointLeave();
1071 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
1073 return mPressedSignal;
1076 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
1078 return mReleasedSignal;
1081 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
1083 return mClickedSignal;
1086 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
1088 return mStateChangedSignal;
1091 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1093 Dali::BaseHandle handle( object );
1095 bool connected( true );
1096 Toolkit::Button button = Toolkit::Button::DownCast( handle );
1098 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
1100 button.PressedSignal().Connect( tracker, functor );
1102 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
1104 button.ReleasedSignal().Connect( tracker, functor );
1106 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
1108 button.ClickedSignal().Connect( tracker, functor );
1110 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
1112 button.StateChangedSignal().Connect( tracker, functor );
1116 // signalName does not match any signal
1123 bool Button::OnTouchEvent(const TouchEvent& event)
1125 // Only events are processed when the button is not disabled and the touch event has only
1127 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
1129 switch( event.GetPoint(0).state )
1131 case TouchPoint::Down:
1133 OnButtonDown(); // Notification for derived classes.
1135 // Sets the button state to ButtonDown.
1136 mState = ButtonDown;
1139 case TouchPoint::Up:
1141 OnButtonUp(); // Notification for derived classes.
1143 // Sets the button state to ButtonUp.
1147 case TouchPoint::Interrupted:
1149 OnTouchPointInterrupted(); // Notification for derived classes.
1151 // Sets the button state to the default (ButtonUp).
1155 case TouchPoint::Leave:
1157 OnTouchPointLeave(); // Notification for derived classes.
1159 // Sets the button state to the default (ButtonUp).
1163 case TouchPoint::Motion:
1164 case TouchPoint::Stationary: // FALLTHROUGH
1171 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
1176 else if( 1 < event.GetPointCount() )
1178 OnTouchPointLeave(); // Notification for derived classes.
1180 // Sets the button state to the default (ButtonUp).
1187 void Button::OnInitialize()
1189 Actor self = Self();
1191 mTapDetector = TapGestureDetector::New();
1192 mTapDetector.Attach( self );
1193 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
1195 OnButtonInitialize();
1197 self.SetDrawMode( DrawMode::OVERLAY );
1198 self.SetKeyboardFocusable( true );
1201 void Button::OnActivated()
1203 // When the button is activated, it performs the click action
1204 PropertyValueContainer attributes;
1205 DoClickAction( attributes );
1208 void Button::OnControlStageDisconnection()
1210 OnButtonStageDisconnection(); // Notification for derived classes.
1214 void Button::OnTap(Actor actor, const TapGesture& tap)
1219 void Button::SetUpTimer( float delay )
1221 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
1222 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
1223 mAutoRepeatingTimer.Start();
1226 bool Button::AutoRepeatingSlot()
1228 bool consumed = false;
1231 // Restart the autorepeat timer.
1232 SetUpTimer( mNextAutoRepeatingDelay );
1236 Toolkit::Button handle( GetOwner() );
1239 consumed = mReleasedSignal.Emit( handle );
1240 consumed |= mClickedSignal.Emit( handle );
1241 consumed |= mPressedSignal.Emit( handle );
1247 void Button::Pressed()
1249 unsigned int buttonIndex, backgroundIndex;
1250 bool animationStarted = false;
1252 switch( mPaintState )
1254 case UnselectedState:
1256 buttonIndex = FindChildIndex( mLabel );
1257 InsertChild( buttonIndex, mSelectedContent );
1259 if( mBackgroundContent )
1261 backgroundIndex = 1;
1265 backgroundIndex = 0;
1268 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1270 // Notifies the derived class the button has been pressed.
1271 animationStarted = OnPressed();
1273 if( animationStarted )
1275 mPaintState = UnselectedSelectedTransition;
1279 mPaintState = SelectedState;
1283 case SelectedUnselectedTransition:
1285 // Notifies the derived class the button has been pressed.
1286 animationStarted = OnPressed();
1288 if( animationStarted )
1290 mPaintState = UnselectedSelectedTransition;
1294 mPaintState = SelectedState;
1298 case DisabledUnselectedTransition:
1300 buttonIndex = FindChildIndex( mLabel );
1301 InsertChild( buttonIndex, mSelectedContent );
1303 if( mDisabledBackgroundContent )
1305 if( mBackgroundContent )
1307 backgroundIndex = 2;
1311 backgroundIndex = 1;
1314 else if( mBackgroundContent )
1316 backgroundIndex = 1;
1320 backgroundIndex = 0;
1323 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1325 // Notifies the derived class the button has been pressed.
1326 animationStarted = OnPressed();
1328 if( animationStarted )
1330 mPaintState = UnselectedSelectedTransition;
1334 mPaintState = SelectedState;
1343 void Button::Released()
1345 unsigned int buttonIndex;
1346 bool animationStarted = false;
1348 switch( mPaintState )
1352 buttonIndex = FindChildIndex( mLabel );
1353 InsertChild( buttonIndex, mButtonContent );
1355 // Notifies the derived class the button has been released.
1356 animationStarted = OnReleased();
1358 if( animationStarted )
1360 mPaintState = SelectedUnselectedTransition;
1364 mPaintState = UnselectedState;
1368 case UnselectedSelectedTransition:
1370 // Notifies the derived class the button has been released.
1371 animationStarted = OnReleased();
1373 if( animationStarted )
1375 mPaintState = SelectedUnselectedTransition;
1379 mPaintState = UnselectedState;
1383 case DisabledSelectedTransition:
1385 buttonIndex = FindChildIndex( mLabel );
1386 InsertChild( buttonIndex, mButtonContent );
1388 // Notifies the derived class the button has been released.
1389 animationStarted = OnReleased();
1391 if( animationStarted )
1393 mPaintState = SelectedUnselectedTransition;
1397 mPaintState = UnselectedState;
1408 Button::ButtonState Button::GetState()
1413 Button::PaintState Button::GetPaintState()
1418 void Button::InsertChild( unsigned int index, Actor& actor )
1422 Self().Insert( index, actor);
1426 void Button::RemoveChild( Actor& actor )
1428 if( actor && actor.GetParent() )
1430 Self().Remove( actor );
1434 unsigned int Button::FindChildIndex( Actor& actor )
1436 Actor self = Self();
1437 unsigned int childrenNum = self.GetChildCount();
1439 for( unsigned int i = 0; i < childrenNum; i++ )
1441 Actor child = self.GetChildAt( i );
1442 if( child == actor )
1451 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1453 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1459 case Toolkit::Button::Property::DISABLED:
1461 GetImplementation( button ).SetDisabled( value.Get<bool>() );
1465 case Toolkit::Button::Property::AUTO_REPEATING:
1467 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
1471 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1473 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
1477 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1479 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
1483 case Toolkit::Button::Property::TOGGLABLE:
1485 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
1489 case Toolkit::Button::Property::SELECTED:
1491 GetImplementation( button ).SetSelected( value.Get< bool >() );
1495 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1497 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1501 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1503 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1507 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1509 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1513 case Toolkit::Button::Property::LABEL_ACTOR:
1515 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
1522 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
1524 Property::Value value;
1526 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1530 switch ( propertyIndex )
1532 case Toolkit::Button::Property::DISABLED:
1534 value = GetImplementation( button ).mDisabled;
1538 case Toolkit::Button::Property::AUTO_REPEATING:
1540 value = GetImplementation( button ).mAutoRepeating;
1544 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1546 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
1550 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1552 value = GetImplementation( button ).mNextAutoRepeatingDelay;
1556 case Toolkit::Button::Property::TOGGLABLE:
1558 value = GetImplementation( button ).mTogglableButton;
1562 case Toolkit::Button::Property::SELECTED:
1564 value = GetImplementation( button ).mSelected;
1568 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1571 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonContent, map );
1576 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1579 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedContent, map );
1584 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1587 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledContent, map );
1592 case Toolkit::Button::Property::LABEL_ACTOR:
1595 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
1605 } // namespace Internal
1607 } // namespace Toolkit