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 SetLabel( textLabel );
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