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 textLabel.SetResizePolicy( FILL_TO_PARENT, ALL_DIMENSIONS );
620 textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
621 textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
622 textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
624 SetLabel( textLabel );
627 void Button::SetLabel( Actor label )
629 if( mLabel != label )
631 if( mLabel && mLabel.GetParent() )
633 mLabel.GetParent().Remove( mLabel );
637 mLabel.SetPosition( 0.f, 0.f );
639 // label should be the top of the button
640 Self().Add( mLabel );
648 Actor Button::GetLabel() const
653 Actor& Button::GetLabel()
658 void Button::SetButtonImage( Actor image )
662 if( mButtonContent && mButtonContent.GetParent() )
664 Self().Remove( mButtonContent );
667 mButtonContent = image;
669 mButtonContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
670 mButtonContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
671 mButtonContent.SetPosition( 0.f, 0.f );
673 if( mPaintState == UnselectedState )
675 unsigned int index = FindChildIndex( mLabel );
677 Self().Insert( index, mButtonContent );
683 Actor Button::GetButtonImage() const
685 return mButtonContent;
688 Actor& Button::GetButtonImage()
690 return mButtonContent;
693 void Button::SetSelectedImage( Actor image )
697 if( mSelectedContent && mSelectedContent.GetParent() )
699 Self().Remove( mSelectedContent );
702 mSelectedContent = image;
704 mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
705 mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
706 mSelectedContent.SetPosition( 0.f, 0.f );
708 if( mPaintState == SelectedState )
710 unsigned int index = FindChildIndex( mLabel );
712 Self().Insert( index, mSelectedContent );
715 OnSelectedImageSet();
718 Actor Button::GetSelectedImage() const
720 return mSelectedContent;
723 Actor& Button::GetSelectedImage()
725 return mSelectedContent;
728 void Button::SetBackgroundImage( Actor image )
732 if( mBackgroundContent && mBackgroundContent.GetParent() )
734 Self().Remove( mBackgroundContent );
737 mBackgroundContent = image;
739 mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
740 mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
741 mBackgroundContent.SetPosition( 0.f, 0.f );
743 if( mPaintState == UnselectedState || mPaintState == SelectedState )
745 Self().Insert( 0, mBackgroundContent );
748 OnBackgroundImageSet();
751 Actor Button::GetBackgroundImage() const
753 return mBackgroundContent;
756 Actor& Button::GetBackgroundImage()
758 return mBackgroundContent;
761 void Button::SetSelectedBackgroundImage( Actor image )
765 if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
767 Self().Remove( mSelectedBackgroundContent );
770 mSelectedBackgroundContent = image;
772 mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
773 mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
774 mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
776 if( mPaintState == SelectedState )
778 if( mBackgroundContent )
780 Self().Insert( 1, mSelectedBackgroundContent );
784 Self().Insert( 0, mSelectedBackgroundContent );
788 OnSelectedBackgroundImageSet();
791 Actor Button::GetSelectedBackgroundImage() const
793 return mSelectedBackgroundContent;
796 Actor& Button::GetSelectedBackgroundImage()
798 return mSelectedBackgroundContent;
801 void Button::SetDisabledImage( Actor image )
805 if( mDisabledContent && mDisabledContent.GetParent() )
807 Self().Remove( mDisabledContent );
810 mDisabledContent = image;
812 mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
813 mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
814 mDisabledContent.SetPosition( 0.f, 0.f );
816 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
818 unsigned int index = FindChildIndex( mLabel );
820 Self().Insert( index, mDisabledContent );
823 OnDisabledImageSet();
826 Actor Button::GetDisabledImage() const
828 return mDisabledContent;
831 Actor& Button::GetDisabledImage()
833 return mDisabledContent;
836 void Button::SetDisabledSelectedImage( Actor image )
840 if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
842 Self().Remove( mDisabledSelectedContent );
845 mDisabledSelectedContent = image;
847 mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
848 mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
849 mDisabledSelectedContent.SetPosition( 0.f, 0.f );
851 if( mPaintState == DisabledSelectedState )
853 unsigned int index = FindChildIndex( mLabel );
855 Self().Insert( index, mDisabledSelectedContent );
858 OnDisabledSelectedImageSet();
861 Actor Button::GetDisabledSelectedImage() const
863 return mDisabledSelectedContent;
866 Actor& Button::GetDisabledSelectedImage()
868 return mDisabledSelectedContent;
871 void Button::SetDisabledBackgroundImage( Actor image )
875 if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
877 Self().Remove( mDisabledBackgroundContent );
880 mDisabledBackgroundContent = image;
882 mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
883 mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
884 mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
886 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
888 Self().Insert( 0, mDisabledBackgroundContent );
891 OnDisabledBackgroundImageSet();
894 Actor Button::GetDisabledBackgroundImage() const
896 return mDisabledBackgroundContent;
899 Actor& Button::GetDisabledBackgroundImage()
901 return mDisabledBackgroundContent;
904 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
908 Dali::BaseHandle handle( object );
910 Toolkit::Button button = Toolkit::Button::DownCast( handle );
912 DALI_ASSERT_ALWAYS( button );
914 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
916 GetImplementation( button ).DoClickAction( attributes );
923 void Button::DoClickAction( const PropertyValueContainer& attributes )
925 // Prevents the button signals from doing a recursive loop by sending an action
926 // and re-emitting the signals.
927 if( !mClickActionPerforming )
929 mClickActionPerforming = true;
933 mClickActionPerforming = false;
937 void Button::UpdatePaintTransitionState()
939 switch( mPaintState )
941 case UnselectedSelectedTransition:
943 RemoveChild( mButtonContent );
944 mPaintState = SelectedState;
947 case SelectedUnselectedTransition:
949 RemoveChild( mSelectedBackgroundContent );
950 RemoveChild( mSelectedContent );
951 mPaintState = UnselectedState;
954 case UnselectedDisabledTransition:
956 RemoveChild( mBackgroundContent );
957 RemoveChild( mButtonContent );
958 mPaintState = DisabledUnselectedState;
961 case DisabledUnselectedTransition:
963 RemoveChild( mDisabledBackgroundContent );
964 RemoveChild( mDisabledContent );
965 mPaintState = UnselectedState;
968 case SelectedDisabledTransition:
970 RemoveChild( mBackgroundContent );
971 RemoveChild( mSelectedBackgroundContent );
972 RemoveChild( mSelectedContent );
973 mPaintState = DisabledSelectedState;
976 case DisabledSelectedTransition:
978 RemoveChild( mDisabledBackgroundContent );
979 RemoveChild( mDisabledSelectedContent );
980 mPaintState = SelectedState;
990 void Button::OnButtonStageDisconnection()
992 if( ButtonDown == mState )
994 if( !mTogglableButton )
1000 mAutoRepeatingTimer.Reset();
1006 void Button::OnButtonDown()
1008 if( !mTogglableButton )
1010 Toolkit::Button handle( GetOwner() );
1014 if( mAutoRepeating )
1016 SetUpTimer( mInitialAutoRepeatingDelay );
1020 mPressedSignal.Emit( handle );
1024 void Button::OnButtonUp()
1026 if( ButtonDown == mState )
1028 if( mTogglableButton )
1030 SetSelected( !mSelected );
1036 if( mAutoRepeating )
1038 mAutoRepeatingTimer.Reset();
1041 Toolkit::Button handle( GetOwner() );
1044 mReleasedSignal.Emit( handle );
1045 mClickedSignal.Emit( handle );
1050 void Button::OnTouchPointLeave()
1052 if( ButtonDown == mState )
1054 if( !mTogglableButton )
1056 Toolkit::Button handle( GetOwner() );
1060 if( mAutoRepeating )
1062 mAutoRepeatingTimer.Reset();
1066 mReleasedSignal.Emit( handle );
1071 void Button::OnTouchPointInterrupted()
1073 OnTouchPointLeave();
1076 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
1078 return mPressedSignal;
1081 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
1083 return mReleasedSignal;
1086 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
1088 return mClickedSignal;
1091 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
1093 return mStateChangedSignal;
1096 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1098 Dali::BaseHandle handle( object );
1100 bool connected( true );
1101 Toolkit::Button button = Toolkit::Button::DownCast( handle );
1103 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
1105 button.PressedSignal().Connect( tracker, functor );
1107 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
1109 button.ReleasedSignal().Connect( tracker, functor );
1111 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
1113 button.ClickedSignal().Connect( tracker, functor );
1115 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
1117 button.StateChangedSignal().Connect( tracker, functor );
1121 // signalName does not match any signal
1128 bool Button::OnTouchEvent(const TouchEvent& event)
1130 // Only events are processed when the button is not disabled and the touch event has only
1132 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
1134 switch( event.GetPoint(0).state )
1136 case TouchPoint::Down:
1138 OnButtonDown(); // Notification for derived classes.
1140 // Sets the button state to ButtonDown.
1141 mState = ButtonDown;
1144 case TouchPoint::Up:
1146 OnButtonUp(); // Notification for derived classes.
1148 // Sets the button state to ButtonUp.
1152 case TouchPoint::Interrupted:
1154 OnTouchPointInterrupted(); // Notification for derived classes.
1156 // Sets the button state to the default (ButtonUp).
1160 case TouchPoint::Leave:
1162 OnTouchPointLeave(); // Notification for derived classes.
1164 // Sets the button state to the default (ButtonUp).
1168 case TouchPoint::Motion:
1169 case TouchPoint::Stationary: // FALLTHROUGH
1176 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
1181 else if( 1 < event.GetPointCount() )
1183 OnTouchPointLeave(); // Notification for derived classes.
1185 // Sets the button state to the default (ButtonUp).
1192 void Button::OnInitialize()
1194 Actor self = Self();
1196 mTapDetector = TapGestureDetector::New();
1197 mTapDetector.Attach( self );
1198 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
1200 OnButtonInitialize();
1202 self.SetDrawMode( DrawMode::OVERLAY );
1203 self.SetKeyboardFocusable( true );
1206 void Button::OnActivated()
1208 // When the button is activated, it performs the click action
1209 PropertyValueContainer attributes;
1210 DoClickAction( attributes );
1213 void Button::OnControlStageDisconnection()
1215 OnButtonStageDisconnection(); // Notification for derived classes.
1219 void Button::OnTap(Actor actor, const TapGesture& tap)
1224 void Button::SetUpTimer( float delay )
1226 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
1227 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
1228 mAutoRepeatingTimer.Start();
1231 bool Button::AutoRepeatingSlot()
1233 bool consumed = false;
1236 // Restart the autorepeat timer.
1237 SetUpTimer( mNextAutoRepeatingDelay );
1241 Toolkit::Button handle( GetOwner() );
1244 consumed = mReleasedSignal.Emit( handle );
1245 consumed |= mClickedSignal.Emit( handle );
1246 consumed |= mPressedSignal.Emit( handle );
1252 void Button::Pressed()
1254 unsigned int buttonIndex, backgroundIndex;
1255 bool animationStarted = false;
1257 switch( mPaintState )
1259 case UnselectedState:
1261 buttonIndex = FindChildIndex( mLabel );
1262 InsertChild( buttonIndex, mSelectedContent );
1264 if( mBackgroundContent )
1266 backgroundIndex = 1;
1270 backgroundIndex = 0;
1273 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1275 // Notifies the derived class the button has been pressed.
1276 animationStarted = OnPressed();
1278 if( animationStarted )
1280 mPaintState = UnselectedSelectedTransition;
1284 mPaintState = SelectedState;
1288 case SelectedUnselectedTransition:
1290 // Notifies the derived class the button has been pressed.
1291 animationStarted = OnPressed();
1293 if( animationStarted )
1295 mPaintState = UnselectedSelectedTransition;
1299 mPaintState = SelectedState;
1303 case DisabledUnselectedTransition:
1305 buttonIndex = FindChildIndex( mLabel );
1306 InsertChild( buttonIndex, mSelectedContent );
1308 if( mDisabledBackgroundContent )
1310 if( mBackgroundContent )
1312 backgroundIndex = 2;
1316 backgroundIndex = 1;
1319 else if( mBackgroundContent )
1321 backgroundIndex = 1;
1325 backgroundIndex = 0;
1328 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1330 // Notifies the derived class the button has been pressed.
1331 animationStarted = OnPressed();
1333 if( animationStarted )
1335 mPaintState = UnselectedSelectedTransition;
1339 mPaintState = SelectedState;
1348 void Button::Released()
1350 unsigned int buttonIndex;
1351 bool animationStarted = false;
1353 switch( mPaintState )
1357 buttonIndex = FindChildIndex( mLabel );
1358 InsertChild( buttonIndex, mButtonContent );
1360 // Notifies the derived class the button has been released.
1361 animationStarted = OnReleased();
1363 if( animationStarted )
1365 mPaintState = SelectedUnselectedTransition;
1369 mPaintState = UnselectedState;
1373 case UnselectedSelectedTransition:
1375 // Notifies the derived class the button has been released.
1376 animationStarted = OnReleased();
1378 if( animationStarted )
1380 mPaintState = SelectedUnselectedTransition;
1384 mPaintState = UnselectedState;
1388 case DisabledSelectedTransition:
1390 buttonIndex = FindChildIndex( mLabel );
1391 InsertChild( buttonIndex, mButtonContent );
1393 // Notifies the derived class the button has been released.
1394 animationStarted = OnReleased();
1396 if( animationStarted )
1398 mPaintState = SelectedUnselectedTransition;
1402 mPaintState = UnselectedState;
1413 Button::ButtonState Button::GetState()
1418 Button::PaintState Button::GetPaintState()
1423 void Button::InsertChild( unsigned int index, Actor& actor )
1427 Self().Insert( index, actor);
1431 void Button::RemoveChild( Actor& actor )
1433 if( actor && actor.GetParent() )
1435 Self().Remove( actor );
1439 unsigned int Button::FindChildIndex( Actor& actor )
1441 Actor self = Self();
1442 unsigned int childrenNum = self.GetChildCount();
1444 for( unsigned int i = 0; i < childrenNum; i++ )
1446 Actor child = self.GetChildAt( i );
1447 if( child == actor )
1456 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1458 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1464 case Toolkit::Button::Property::DISABLED:
1466 GetImplementation( button ).SetDisabled( value.Get<bool>() );
1470 case Toolkit::Button::Property::AUTO_REPEATING:
1472 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
1476 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1478 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
1482 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1484 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
1488 case Toolkit::Button::Property::TOGGLABLE:
1490 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
1494 case Toolkit::Button::Property::SELECTED:
1496 GetImplementation( button ).SetSelected( value.Get< bool >() );
1500 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1502 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1506 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1508 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1512 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1514 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1518 case Toolkit::Button::Property::LABEL_ACTOR:
1520 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
1527 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
1529 Property::Value value;
1531 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1535 switch ( propertyIndex )
1537 case Toolkit::Button::Property::DISABLED:
1539 value = GetImplementation( button ).mDisabled;
1543 case Toolkit::Button::Property::AUTO_REPEATING:
1545 value = GetImplementation( button ).mAutoRepeating;
1549 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1551 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
1555 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1557 value = GetImplementation( button ).mNextAutoRepeatingDelay;
1561 case Toolkit::Button::Property::TOGGLABLE:
1563 value = GetImplementation( button ).mTogglableButton;
1567 case Toolkit::Button::Property::SELECTED:
1569 value = GetImplementation( button ).mSelected;
1573 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1576 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonContent, map );
1581 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1584 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedContent, map );
1589 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1592 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledContent, map );
1597 case Toolkit::Button::Property::LABEL_ACTOR:
1600 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
1610 } // namespace Internal
1612 } // namespace Toolkit