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-view/text-view.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::TextView textView = Toolkit::TextView::New( label );
619 textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button
620 SetLabel( textView );
623 void Button::SetLabel( Actor label )
625 if( mLabel != label )
627 if( mLabel && mLabel.GetParent() )
629 mLabel.GetParent().Remove( mLabel );
633 mLabel.SetPosition( 0.f, 0.f );
635 // label should be the top of the button
636 Self().Add( mLabel );
644 Actor Button::GetLabel() const
649 Actor& Button::GetLabel()
654 void Button::SetButtonImage( Actor image )
658 if( mButtonContent && mButtonContent.GetParent() )
660 Self().Remove( mButtonContent );
663 mButtonContent = image;
665 mButtonContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
666 mButtonContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
667 mButtonContent.SetPosition( 0.f, 0.f );
669 if( mPaintState == UnselectedState )
671 unsigned int index = FindChildIndex( mLabel );
673 Self().Insert( index, mButtonContent );
679 Actor Button::GetButtonImage() const
681 return mButtonContent;
684 Actor& Button::GetButtonImage()
686 return mButtonContent;
689 void Button::SetSelectedImage( Actor image )
693 if( mSelectedContent && mSelectedContent.GetParent() )
695 Self().Remove( mSelectedContent );
698 mSelectedContent = image;
700 mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
701 mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
702 mSelectedContent.SetPosition( 0.f, 0.f );
704 if( mPaintState == SelectedState )
706 unsigned int index = FindChildIndex( mLabel );
708 Self().Insert( index, mSelectedContent );
711 OnSelectedImageSet();
714 Actor Button::GetSelectedImage() const
716 return mSelectedContent;
719 Actor& Button::GetSelectedImage()
721 return mSelectedContent;
724 void Button::SetBackgroundImage( Actor image )
728 if( mBackgroundContent && mBackgroundContent.GetParent() )
730 Self().Remove( mBackgroundContent );
733 mBackgroundContent = image;
735 mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
736 mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
737 mBackgroundContent.SetPosition( 0.f, 0.f );
739 if( mPaintState == UnselectedState || mPaintState == SelectedState )
741 Self().Insert( 0, mBackgroundContent );
744 OnBackgroundImageSet();
747 Actor Button::GetBackgroundImage() const
749 return mBackgroundContent;
752 Actor& Button::GetBackgroundImage()
754 return mBackgroundContent;
757 void Button::SetSelectedBackgroundImage( Actor image )
761 if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
763 Self().Remove( mSelectedBackgroundContent );
766 mSelectedBackgroundContent = image;
768 mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
769 mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
770 mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
772 if( mPaintState == SelectedState )
774 if( mBackgroundContent )
776 Self().Insert( 1, mSelectedBackgroundContent );
780 Self().Insert( 0, mSelectedBackgroundContent );
784 OnSelectedBackgroundImageSet();
787 Actor Button::GetSelectedBackgroundImage() const
789 return mSelectedBackgroundContent;
792 Actor& Button::GetSelectedBackgroundImage()
794 return mSelectedBackgroundContent;
797 void Button::SetDisabledImage( Actor image )
801 if( mDisabledContent && mDisabledContent.GetParent() )
803 Self().Remove( mDisabledContent );
806 mDisabledContent = image;
808 mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
809 mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
810 mDisabledContent.SetPosition( 0.f, 0.f );
812 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
814 unsigned int index = FindChildIndex( mLabel );
816 Self().Insert( index, mDisabledContent );
819 OnDisabledImageSet();
822 Actor Button::GetDisabledImage() const
824 return mDisabledContent;
827 Actor& Button::GetDisabledImage()
829 return mDisabledContent;
832 void Button::SetDisabledSelectedImage( Actor image )
836 if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
838 Self().Remove( mDisabledSelectedContent );
841 mDisabledSelectedContent = image;
843 mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
844 mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
845 mDisabledSelectedContent.SetPosition( 0.f, 0.f );
847 if( mPaintState == DisabledSelectedState )
849 unsigned int index = FindChildIndex( mLabel );
851 Self().Insert( index, mDisabledSelectedContent );
854 OnDisabledSelectedImageSet();
857 Actor Button::GetDisabledSelectedImage() const
859 return mDisabledSelectedContent;
862 Actor& Button::GetDisabledSelectedImage()
864 return mDisabledSelectedContent;
867 void Button::SetDisabledBackgroundImage( Actor image )
871 if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
873 Self().Remove( mDisabledBackgroundContent );
876 mDisabledBackgroundContent = image;
878 mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
879 mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
880 mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
882 if( mPaintState == DisabledUnselectedState || mPaintState == DisabledSelectedState )
884 Self().Insert( 0, mDisabledBackgroundContent );
887 OnDisabledBackgroundImageSet();
890 Actor Button::GetDisabledBackgroundImage() const
892 return mDisabledBackgroundContent;
895 Actor& Button::GetDisabledBackgroundImage()
897 return mDisabledBackgroundContent;
900 bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes )
904 Dali::BaseHandle handle( object );
906 Toolkit::Button button = Toolkit::Button::DownCast( handle );
908 DALI_ASSERT_ALWAYS( button );
910 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
912 GetImplementation( button ).DoClickAction( attributes );
919 void Button::DoClickAction( const PropertyValueContainer& attributes )
921 // Prevents the button signals from doing a recursive loop by sending an action
922 // and re-emitting the signals.
923 if( !mClickActionPerforming )
925 mClickActionPerforming = true;
929 mClickActionPerforming = false;
933 void Button::UpdatePaintTransitionState()
935 switch( mPaintState )
937 case UnselectedSelectedTransition:
939 RemoveChild( mButtonContent );
940 mPaintState = SelectedState;
943 case SelectedUnselectedTransition:
945 RemoveChild( mSelectedBackgroundContent );
946 RemoveChild( mSelectedContent );
947 mPaintState = UnselectedState;
950 case UnselectedDisabledTransition:
952 RemoveChild( mBackgroundContent );
953 RemoveChild( mButtonContent );
954 mPaintState = DisabledUnselectedState;
957 case DisabledUnselectedTransition:
959 RemoveChild( mDisabledBackgroundContent );
960 RemoveChild( mDisabledContent );
961 mPaintState = UnselectedState;
964 case SelectedDisabledTransition:
966 RemoveChild( mBackgroundContent );
967 RemoveChild( mSelectedBackgroundContent );
968 RemoveChild( mSelectedContent );
969 mPaintState = DisabledSelectedState;
972 case DisabledSelectedTransition:
974 RemoveChild( mDisabledBackgroundContent );
975 RemoveChild( mDisabledSelectedContent );
976 mPaintState = SelectedState;
986 void Button::OnButtonStageDisconnection()
988 if( ButtonDown == mState )
990 if( !mTogglableButton )
996 mAutoRepeatingTimer.Reset();
1002 void Button::OnButtonDown()
1004 if( !mTogglableButton )
1006 Toolkit::Button handle( GetOwner() );
1010 if( mAutoRepeating )
1012 SetUpTimer( mInitialAutoRepeatingDelay );
1016 mPressedSignal.Emit( handle );
1020 void Button::OnButtonUp()
1022 if( ButtonDown == mState )
1024 if( mTogglableButton )
1026 SetSelected( !mSelected );
1032 if( mAutoRepeating )
1034 mAutoRepeatingTimer.Reset();
1037 Toolkit::Button handle( GetOwner() );
1040 mReleasedSignal.Emit( handle );
1041 mClickedSignal.Emit( handle );
1046 void Button::OnTouchPointLeave()
1048 if( ButtonDown == mState )
1050 if( !mTogglableButton )
1052 Toolkit::Button handle( GetOwner() );
1056 if( mAutoRepeating )
1058 mAutoRepeatingTimer.Reset();
1062 mReleasedSignal.Emit( handle );
1067 void Button::OnTouchPointInterrupted()
1069 OnTouchPointLeave();
1072 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
1074 return mPressedSignal;
1077 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
1079 return mReleasedSignal;
1082 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
1084 return mClickedSignal;
1087 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
1089 return mStateChangedSignal;
1092 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
1094 Dali::BaseHandle handle( object );
1096 bool connected( true );
1097 Toolkit::Button button = Toolkit::Button::DownCast( handle );
1099 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
1101 button.PressedSignal().Connect( tracker, functor );
1103 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
1105 button.ReleasedSignal().Connect( tracker, functor );
1107 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
1109 button.ClickedSignal().Connect( tracker, functor );
1111 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
1113 button.StateChangedSignal().Connect( tracker, functor );
1117 // signalName does not match any signal
1124 bool Button::OnTouchEvent(const TouchEvent& event)
1126 // Only events are processed when the button is not disabled and the touch event has only
1128 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
1130 switch( event.GetPoint(0).state )
1132 case TouchPoint::Down:
1134 OnButtonDown(); // Notification for derived classes.
1136 // Sets the button state to ButtonDown.
1137 mState = ButtonDown;
1140 case TouchPoint::Up:
1142 OnButtonUp(); // Notification for derived classes.
1144 // Sets the button state to ButtonUp.
1148 case TouchPoint::Interrupted:
1150 OnTouchPointInterrupted(); // Notification for derived classes.
1152 // Sets the button state to the default (ButtonUp).
1156 case TouchPoint::Leave:
1158 OnTouchPointLeave(); // Notification for derived classes.
1160 // Sets the button state to the default (ButtonUp).
1164 case TouchPoint::Motion:
1165 case TouchPoint::Stationary: // FALLTHROUGH
1172 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
1177 else if( 1 < event.GetPointCount() )
1179 OnTouchPointLeave(); // Notification for derived classes.
1181 // Sets the button state to the default (ButtonUp).
1188 void Button::OnInitialize()
1190 Actor self = Self();
1192 mTapDetector = TapGestureDetector::New();
1193 mTapDetector.Attach( self );
1194 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
1196 OnButtonInitialize();
1198 self.SetDrawMode( DrawMode::OVERLAY );
1199 self.SetKeyboardFocusable( true );
1202 void Button::OnActivated()
1204 // When the button is activated, it performs the click action
1205 PropertyValueContainer attributes;
1206 DoClickAction( attributes );
1209 void Button::OnControlStageDisconnection()
1211 OnButtonStageDisconnection(); // Notification for derived classes.
1215 void Button::OnTap(Actor actor, const TapGesture& tap)
1220 void Button::SetUpTimer( float delay )
1222 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
1223 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
1224 mAutoRepeatingTimer.Start();
1227 bool Button::AutoRepeatingSlot()
1229 bool consumed = false;
1232 // Restart the autorepeat timer.
1233 SetUpTimer( mNextAutoRepeatingDelay );
1237 Toolkit::Button handle( GetOwner() );
1240 consumed = mReleasedSignal.Emit( handle );
1241 consumed |= mClickedSignal.Emit( handle );
1242 consumed |= mPressedSignal.Emit( handle );
1248 void Button::Pressed()
1250 unsigned int buttonIndex, backgroundIndex;
1251 bool animationStarted = false;
1253 switch( mPaintState )
1255 case UnselectedState:
1257 buttonIndex = FindChildIndex( mLabel );
1258 InsertChild( buttonIndex, mSelectedContent );
1260 if( mBackgroundContent )
1262 backgroundIndex = 1;
1266 backgroundIndex = 0;
1269 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1271 // Notifies the derived class the button has been pressed.
1272 animationStarted = OnPressed();
1274 if( animationStarted )
1276 mPaintState = UnselectedSelectedTransition;
1280 mPaintState = SelectedState;
1284 case SelectedUnselectedTransition:
1286 // Notifies the derived class the button has been pressed.
1287 animationStarted = OnPressed();
1289 if( animationStarted )
1291 mPaintState = UnselectedSelectedTransition;
1295 mPaintState = SelectedState;
1299 case DisabledUnselectedTransition:
1301 buttonIndex = FindChildIndex( mLabel );
1302 InsertChild( buttonIndex, mSelectedContent );
1304 if( mDisabledBackgroundContent )
1306 if( mBackgroundContent )
1308 backgroundIndex = 2;
1312 backgroundIndex = 1;
1315 else if( mBackgroundContent )
1317 backgroundIndex = 1;
1321 backgroundIndex = 0;
1324 InsertChild( backgroundIndex, mSelectedBackgroundContent );
1326 // Notifies the derived class the button has been pressed.
1327 animationStarted = OnPressed();
1329 if( animationStarted )
1331 mPaintState = UnselectedSelectedTransition;
1335 mPaintState = SelectedState;
1344 void Button::Released()
1346 unsigned int buttonIndex;
1347 bool animationStarted = false;
1349 switch( mPaintState )
1353 buttonIndex = FindChildIndex( mLabel );
1354 InsertChild( buttonIndex, mButtonContent );
1356 // Notifies the derived class the button has been released.
1357 animationStarted = OnReleased();
1359 if( animationStarted )
1361 mPaintState = SelectedUnselectedTransition;
1365 mPaintState = UnselectedState;
1369 case UnselectedSelectedTransition:
1371 // Notifies the derived class the button has been released.
1372 animationStarted = OnReleased();
1374 if( animationStarted )
1376 mPaintState = SelectedUnselectedTransition;
1380 mPaintState = UnselectedState;
1384 case DisabledSelectedTransition:
1386 buttonIndex = FindChildIndex( mLabel );
1387 InsertChild( buttonIndex, mButtonContent );
1389 // Notifies the derived class the button has been released.
1390 animationStarted = OnReleased();
1392 if( animationStarted )
1394 mPaintState = SelectedUnselectedTransition;
1398 mPaintState = UnselectedState;
1409 Button::ButtonState Button::GetState()
1414 Button::PaintState Button::GetPaintState()
1419 void Button::InsertChild( unsigned int index, Actor& actor )
1423 Self().Insert( index, actor);
1427 void Button::RemoveChild( Actor& actor )
1429 if( actor && actor.GetParent() )
1431 Self().Remove( actor );
1435 unsigned int Button::FindChildIndex( Actor& actor )
1437 Actor self = Self();
1438 unsigned int childrenNum = self.GetChildCount();
1440 for( unsigned int i = 0; i < childrenNum; i++ )
1442 Actor child = self.GetChildAt( i );
1443 if( child == actor )
1452 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1454 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1460 case Toolkit::Button::Property::DISABLED:
1462 GetImplementation( button ).SetDisabled( value.Get<bool>() );
1466 case Toolkit::Button::Property::AUTO_REPEATING:
1468 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
1472 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1474 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
1478 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1480 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
1484 case Toolkit::Button::Property::TOGGLABLE:
1486 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
1490 case Toolkit::Button::Property::SELECTED:
1492 GetImplementation( button ).SetSelected( value.Get< bool >() );
1496 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1498 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1502 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1504 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1508 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1510 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1514 case Toolkit::Button::Property::LABEL_ACTOR:
1516 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
1523 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
1525 Property::Value value;
1527 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1531 switch ( propertyIndex )
1533 case Toolkit::Button::Property::DISABLED:
1535 value = GetImplementation( button ).mDisabled;
1539 case Toolkit::Button::Property::AUTO_REPEATING:
1541 value = GetImplementation( button ).mAutoRepeating;
1545 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1547 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
1551 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1553 value = GetImplementation( button ).mNextAutoRepeatingDelay;
1557 case Toolkit::Button::Property::TOGGLABLE:
1559 value = GetImplementation( button ).mTogglableButton;
1563 case Toolkit::Button::Property::SELECTED:
1565 value = GetImplementation( button ).mSelected;
1569 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1572 Scripting::CreatePropertyMap( GetImplementation( button ).mButtonContent, map );
1577 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1580 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedContent, map );
1585 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1588 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledContent, map );
1593 case Toolkit::Button::Property::LABEL_ACTOR:
1596 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
1606 } // namespace Internal
1608 } // namespace Toolkit