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/images/resource-image.h>
25 #include <dali/public-api/object/type-registry.h>
26 #include <dali/devel-api/object/type-registry-helper.h>
27 #include <dali/public-api/actors/image-actor.h>
28 #include <dali/devel-api/scripting/scripting.h>
31 #include <dali-toolkit/public-api/controls/default-controls/solid-color-actor.h>
32 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
35 * Button states and contents
36 * (3) mSelectedContent
37 * (2) mUnselectedContent (2) mSelectedBackgroundContent
38 * (1) mBackgroundContent (1) mBackgroundContent
39 * < unselected > ----------------------- < selected >
41 * | OnDisabled() | OnDisabled()
43 * < disabled > < disabled-selected >
44 * (2) mDisabledContent (2) mDisabledSelectedContent
45 * (1) mDisabledBackgroundContent (1) mDisabledBackgroundContent
47 * The drawing order of child actors is as follows.
50 * | mUnselectedContent / mSelectedContent / mDisabledContent / mDisabledSelectedContent
51 * | mSelectedBackgroundContent
52 * Bottom mBackgroundContent / mDisabledBackgroundContent
54 * Some of contents may be missed.
55 * 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.
72 // empty handle as we cannot create button (but type registered for clicked signal)
76 // Setup properties, signals and actions using the type-registry.
77 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create );
79 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled", BOOLEAN, DISABLED )
80 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "auto-repeating", BOOLEAN, AUTO_REPEATING )
81 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "initial-auto-repeating-delay", FLOAT, INITIAL_AUTO_REPEATING_DELAY )
82 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "next-auto-repeating-delay", FLOAT, NEXT_AUTO_REPEATING_DELAY )
83 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "togglable", BOOLEAN, TOGGLABLE )
84 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected", BOOLEAN, SELECTED )
85 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselected-state-image", STRING, UNSELECTED_STATE_IMAGE )
86 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected-state-image", STRING, SELECTED_STATE_IMAGE )
87 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled-state-image", STRING, DISABLED_STATE_IMAGE )
88 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselected-color", VECTOR4, UNSELECTED_COLOR )
89 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected-color", VECTOR4, SELECTED_COLOR )
90 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "label-text", STRING, LABEL_TEXT )
92 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "pressed", SIGNAL_PRESSED )
93 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "released", SIGNAL_RELEASED )
94 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "clicked", SIGNAL_CLICKED )
95 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "state-changed", SIGNAL_STATE_CHANGED )
97 DALI_ACTION_REGISTRATION( Toolkit, Button, "button-click", ACTION_BUTTON_CLICK )
99 DALI_TYPE_REGISTRATION_END()
101 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
102 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
104 const char* const STYLE_BUTTON_LABEL = "button.label";
106 } // unnamed namespace
109 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
110 mAutoRepeatingTimer(),
111 mUnselectedColor( Color::WHITE ), // The natural colors of the specified images will be used by default.
112 mSelectedColor( Color::WHITE ),
114 mAutoRepeating( false ),
115 mTogglableButton( false ),
117 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
118 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
119 mAnimationTime( 0.0f ),
120 mClickActionPerforming( false ),
122 mPaintState( UnselectedState )
130 void Button::SetDisabled( bool disabled )
132 if( disabled == mDisabled )
137 StopTransitionAnimation();
139 mDisabled = disabled;
141 // Notifies the derived class the button has been disabled.
144 switch( mPaintState )
146 case UnselectedState:
149 //(3) mDisabledContent (Inserted)
150 //(4) mUnselectedContent
151 //(2) mDisabledBackgroundContent (Inserted)
152 //(1) mBackgroundContent
154 AddButtonImage( mBackgroundContent );
155 TransitionButtonImage( mDisabledBackgroundContent );
156 AddButtonImage( mUnselectedContent );
157 TransitionButtonImage( mDisabledContent );
159 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
162 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
163 TransitionOut( mUnselectedContent );
164 TransitionOut( mSelectedContent );
165 TransitionOut( mBackgroundContent );
166 TransitionOut( mSelectedBackgroundContent );
167 TransitionOut( mDisabledSelectedContent );
169 mPaintState = DisabledUnselectedState;
175 //(5) mDisabledSelectedContent (Inserted)
176 //(4) mSelectedContent
177 //(3) mDisabledBackgroundContent (Inserted)
178 //(2) mSelectedBackgroundContent
179 //(1) mBackgroundContent
181 AddButtonImage( mBackgroundContent );
182 AddButtonImage( mSelectedBackgroundContent );
183 TransitionButtonImage( mDisabledBackgroundContent );
184 AddButtonImage( mSelectedContent );
185 TransitionButtonImage( mDisabledSelectedContent );
187 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
190 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
191 TransitionOut( mUnselectedContent );
192 TransitionOut( mSelectedContent );
193 TransitionOut( mBackgroundContent );
194 TransitionOut( mSelectedBackgroundContent );
195 TransitionOut( mDisabledContent );
197 mPaintState = DisabledSelectedState;
200 case DisabledUnselectedState:
203 //(3) mUnselectedContent (Inserted)
204 //(4) mDisabledContent
205 //(2) mBackgroundContent (Inserted)
206 //(1) mDisabledBackgroundContent
208 AddButtonImage( mDisabledBackgroundContent );
209 TransitionButtonImage( mBackgroundContent );
210 AddButtonImage( mDisabledContent );
211 TransitionButtonImage( mUnselectedContent );
213 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
216 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
217 TransitionOut( mSelectedContent );
218 TransitionOut( mSelectedBackgroundContent );
219 TransitionOut( mDisabledContent );
220 TransitionOut( mDisabledSelectedContent );
221 TransitionOut( mDisabledBackgroundContent );
223 mPaintState = UnselectedState;
226 case DisabledSelectedState:
229 //(4) mSelectedContent (Inserted)
230 //(5) mDisabledSelectedContent
231 //(3) mSelectedBackgroundContent (Inserted)
232 //(2) mBackgroundContent (Inserted)
233 //(1) mDisabledBackgroundContent
235 AddButtonImage( mDisabledBackgroundContent );
236 TransitionButtonImage( mBackgroundContent );
237 TransitionButtonImage( mSelectedBackgroundContent );
238 AddButtonImage( mDisabledSelectedContent );
239 TransitionButtonImage( mSelectedContent );
241 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
244 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
245 TransitionOut( mUnselectedContent );
246 TransitionOut( mDisabledContent );
247 TransitionOut( mDisabledSelectedContent );
248 TransitionOut( mDisabledBackgroundContent );
250 mPaintState = SelectedState;
255 StartTransitionAnimation();
258 bool Button::IsDisabled() const
263 void Button::SetAutoRepeating( bool autoRepeating )
265 mAutoRepeating = autoRepeating;
267 // An autorepeating button can't be a togglable button.
270 mTogglableButton = false;
274 // Emit a signal is not wanted, only change the appearance.
275 SetSelected( false, false );
280 bool Button::IsAutoRepeating() const
282 return mAutoRepeating;
285 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
287 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
288 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
291 float Button::GetInitialAutoRepeatingDelay() const
293 return mInitialAutoRepeatingDelay;
296 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
298 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
299 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
302 float Button::GetNextAutoRepeatingDelay() const
304 return mNextAutoRepeatingDelay;
307 void Button::SetTogglableButton( bool togglable )
309 mTogglableButton = togglable;
311 // A togglable button can't be an autorepeating button.
314 mAutoRepeating = false;
318 bool Button::IsTogglableButton() const
320 return mTogglableButton;
323 void Button::SetSelected( bool selected )
325 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
327 SetSelected( selected, true );
331 void Button::SetSelected( bool selected, bool emitSignal )
333 StopTransitionAnimation();
335 mSelected = selected;
337 // Notifies the derived class the button has been selected.
340 switch( mPaintState )
342 case UnselectedState:
345 //(3) mSelectedContent (Inserted)
346 //(4) mUnselectedContent
347 //(2) mSelectedBackgroundContent (Inserted)
348 //(1) mBackgroundContent
350 AddButtonImage( mBackgroundContent );
351 TransitionButtonImage( mSelectedBackgroundContent );
352 AddButtonImage( mUnselectedContent );
353 TransitionButtonImage( mSelectedContent );
355 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
356 TransitionButtonImage( mDecoration[ SELECTED_DECORATION ] );
359 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
360 TransitionOut( mUnselectedContent );
361 TransitionOut( mDisabledContent );
362 TransitionOut( mDisabledSelectedContent );
363 TransitionOut( mDisabledBackgroundContent );
365 mPaintState = SelectedState;
371 //(3) mUnselectedContent (Inserted)
372 //(2) mSelectedContent
373 //(1) mBackgroundContent
375 AddButtonImage( mBackgroundContent );
376 AddButtonImage( mSelectedContent );
377 TransitionButtonImage( mUnselectedContent );
379 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
380 TransitionButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
383 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
384 TransitionOut( mSelectedContent );
385 TransitionOut( mSelectedBackgroundContent );
386 TransitionOut( mDisabledContent );
387 TransitionOut( mDisabledSelectedContent );
388 TransitionOut( mDisabledBackgroundContent );
390 mPaintState = UnselectedState;
393 case DisabledUnselectedState:
394 case DisabledSelectedState:
396 DALI_ASSERT_DEBUG( 0 && "Shouldn't be able to change paint state if the button is disabled." );
401 StartTransitionAnimation();
405 Toolkit::Button handle( GetOwner() );
408 mStateChangedSignal.Emit( handle );
414 bool Button::IsSelected() const
416 return mTogglableButton && mSelected;
419 void Button::SetAnimationTime( float animationTime )
421 mAnimationTime = animationTime;
424 float Button::GetAnimationTime() const
426 return mAnimationTime;
429 void Button::SetLabelText( const std::string& label )
431 if( !mLabel || ( label != GetLabelText() ) )
433 // If we have a label, unparent and update it.
436 mLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, label );
440 // If we don't have a label, create one and set it up.
441 mLabel = Toolkit::TextLabel::New( label );
442 mLabel.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_LABEL );
443 mLabel.SetPosition( 0.f, 0.f );
444 // label should be the top of the button
445 Self().Add( mLabel );
454 std::string Button::GetLabelText() const
456 Toolkit::TextLabel label = Dali::Toolkit::TextLabel::DownCast( mLabel );
459 return label.GetProperty<std::string>( Dali::Toolkit::TextLabel::Property::TEXT );
461 return std::string();
464 Actor& Button::GetLabelActor()
469 void Button::SetDecoration( DecorationState state, Actor actor )
471 if( mDecoration[ state ] && mDecoration[ state ].GetParent() )
473 mDecoration[ state ].Unparent();
476 mDecoration[ state ] = actor;
477 mDecoration[ state ].SetColorMode( USE_OWN_COLOR );
483 Actor& Button::GetDecoration( DecorationState state )
485 return mDecoration[ state ];
488 void Button::SetupContent( Actor& actorToModify, Actor newActor )
492 StopTransitionAnimation();
494 if( actorToModify && actorToModify.GetParent() )
496 actorToModify.Unparent();
499 actorToModify = newActor;
503 actorToModify.SetAnchorPoint( AnchorPoint::TOP_LEFT );
504 actorToModify.SetParentOrigin( ParentOrigin::TOP_LEFT );
505 actorToModify.SetPosition( 0.f, 0.f );
512 void Button::SetUnselectedColor( const Vector4& color )
514 mUnselectedColor = color;
516 if( mUnselectedContent && !GetUnselectedImageFilename().empty() )
518 // If there is existing unselected content, change the color on it directly.
519 mUnselectedContent.SetColor( mUnselectedColor );
523 // If there is no existing content, create a new actor to use for flat color.
524 SetupContent( mUnselectedContent, CreateSolidColorActor( mUnselectedColor ) );
525 mUnselectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
529 const Vector4 Button::GetUnselectedColor() const
531 return mUnselectedColor;
534 void Button::SetSelectedColor( const Vector4& color )
536 mSelectedColor = color;
538 if( mSelectedContent && !GetSelectedImageFilename().empty() )
540 // If there is existing unselected content, change the color on it directly.
541 mSelectedContent.SetColor( mSelectedColor );
545 // If there is no existing content, create a new actor to use for flat color.
546 SetupContent( mSelectedContent, CreateSolidColorActor( mSelectedColor ) );
547 mSelectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
551 const Vector4 Button::GetSelectedColor() const
553 return mSelectedColor;
556 void Button::SetUnselectedImage( const std::string& filename )
558 ImageActor newContent;
559 if( !filename.empty() )
561 Image resourceimage = Dali::ResourceImage::New( filename );
564 newContent = ImageActor::New( resourceimage );
569 newContent = ImageActor::New();
574 SetupContent( mUnselectedContent, newContent );
576 mUnselectedContent.SetColor( mUnselectedColor );
578 OnUnselectedImageSet();
583 Actor& Button::GetUnselectedImage()
585 return mUnselectedContent;
588 void Button::SetSelectedImage( const std::string& filename )
590 ImageActor newContent;
591 if( !filename.empty() )
593 Image resourceimage = Dali::ResourceImage::New( filename );
596 newContent = ImageActor::New( resourceimage );
601 newContent = ImageActor::New();
606 SetupContent( mSelectedContent, newContent );
608 mSelectedContent.SetColor( mSelectedColor );
610 OnSelectedImageSet();
615 Actor& Button::GetSelectedImage()
617 return mSelectedContent;
620 void Button::SetBackgroundImage( const std::string& filename )
622 Image resourceimage = Dali::ResourceImage::New( filename );
625 SetupContent( mBackgroundContent, ImageActor::New( resourceimage ) );
627 OnBackgroundImageSet();
632 Actor& Button::GetBackgroundImage()
634 return mBackgroundContent;
637 void Button::SetSelectedBackgroundImage( const std::string& filename )
639 Image resourceimage = Dali::ResourceImage::New( filename );
642 SetupContent( mSelectedBackgroundContent, ImageActor::New( resourceimage ) );
644 OnSelectedBackgroundImageSet();
649 Actor& Button::GetSelectedBackgroundImage()
651 return mSelectedBackgroundContent;
654 void Button::SetDisabledImage( const std::string& filename )
656 Image resourceimage = Dali::ResourceImage::New( filename );
659 SetupContent( mDisabledContent, ImageActor::New( resourceimage ) );
661 OnDisabledImageSet();
666 Actor& Button::GetDisabledImage()
668 return mDisabledContent;
671 void Button::SetDisabledSelectedImage( const std::string& filename )
673 Image resourceimage = Dali::ResourceImage::New( filename );
676 SetupContent( mDisabledSelectedContent, ImageActor::New( resourceimage ) );
678 OnDisabledSelectedImageSet();
683 Actor& Button::GetDisabledSelectedImage()
685 return mDisabledSelectedContent;
688 void Button::SetDisabledBackgroundImage( const std::string& filename )
690 Image resourceimage = Dali::ResourceImage::New( filename );
693 SetupContent( mDisabledBackgroundContent, ImageActor::New( resourceimage ) );
695 OnDisabledBackgroundImageSet();
700 Actor& Button::GetDisabledBackgroundImage()
702 return mDisabledBackgroundContent;
705 std::string Button::GetUnselectedImageFilename() const
707 if( mUnselectedContent )
709 ResourceImage image = ResourceImage::DownCast( mUnselectedContent );
712 return image.GetUrl();
715 return std::string();
718 std::string Button::GetSelectedImageFilename() const
720 if( mSelectedContent )
722 ResourceImage image = ResourceImage::DownCast( mSelectedContent );
725 return image.GetUrl();
728 return std::string();
731 std::string Button::GetBackgroundImageFilename() const
733 if( mBackgroundContent )
735 ResourceImage image = ResourceImage::DownCast( mBackgroundContent );
738 return image.GetUrl();
741 return std::string();
744 std::string Button::GetSelectedBackgroundImageFilename() const
746 if( mSelectedBackgroundContent )
748 ResourceImage image = ResourceImage::DownCast( mSelectedBackgroundContent );
751 return image.GetUrl();
754 return std::string();
757 std::string Button::GetDisabledImageFilename() const
759 if( mDisabledContent )
761 ResourceImage image = ResourceImage::DownCast( mDisabledContent );
764 return image.GetUrl();
767 return std::string();
770 std::string Button::GetDisabledSelectedImageFilename() const
772 if( mDisabledSelectedContent )
774 ResourceImage image = ResourceImage::DownCast( mDisabledSelectedContent );
777 return image.GetUrl();
780 return std::string();
783 std::string Button::GetDisabledBackgroundImageFilename() const
785 if( mDisabledBackgroundContent )
787 ResourceImage image = ResourceImage::DownCast( mDisabledBackgroundContent );
790 return image.GetUrl();
793 return std::string();
796 bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
800 Dali::BaseHandle handle( object );
802 Toolkit::Button button = Toolkit::Button::DownCast( handle );
804 DALI_ASSERT_ALWAYS( button );
806 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
808 ret = GetImplementation( button ).DoClickAction( attributes );
814 bool Button::DoClickAction( const Property::Map& attributes )
816 // Prevents the button signals from doing a recursive loop by sending an action
817 // and re-emitting the signals.
818 if( !mClickActionPerforming )
820 mClickActionPerforming = true;
824 mClickActionPerforming = false;
832 void Button::OnButtonStageDisconnection()
834 if( ButtonDown == mState )
836 if( !mTogglableButton )
842 mAutoRepeatingTimer.Reset();
848 void Button::OnButtonDown()
850 if( !mTogglableButton )
856 SetUpTimer( mInitialAutoRepeatingDelay );
860 // The pressed signal should be emitted regardless of toggle mode.
861 Toolkit::Button handle( GetOwner() );
862 mPressedSignal.Emit( handle );
865 void Button::OnButtonUp()
867 if( ButtonDown == mState )
869 if( mTogglableButton )
871 SetSelected( !mSelected );
879 mAutoRepeatingTimer.Reset();
883 // The clicked and released signals should be emitted regardless of toggle mode.
884 Toolkit::Button handle( GetOwner() );
885 mReleasedSignal.Emit( handle );
886 mClickedSignal.Emit( handle );
890 void Button::OnTouchPointLeave()
892 if( ButtonDown == mState )
894 if( !mTogglableButton )
900 mAutoRepeatingTimer.Reset();
904 // The released signal should be emitted regardless of toggle mode.
905 Toolkit::Button handle( GetOwner() );
906 mReleasedSignal.Emit( handle );
910 void Button::OnTouchPointInterrupted()
915 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
917 return mPressedSignal;
920 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
922 return mReleasedSignal;
925 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
927 return mClickedSignal;
930 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
932 return mStateChangedSignal;
935 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
937 Dali::BaseHandle handle( object );
939 bool connected( true );
940 Toolkit::Button button = Toolkit::Button::DownCast( handle );
942 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
944 button.PressedSignal().Connect( tracker, functor );
946 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
948 button.ReleasedSignal().Connect( tracker, functor );
950 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
952 button.ClickedSignal().Connect( tracker, functor );
954 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
956 button.StateChangedSignal().Connect( tracker, functor );
960 // signalName does not match any signal
967 bool Button::OnTouchEvent(const TouchEvent& event)
969 // Only events are processed when the button is not disabled and the touch event has only
971 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
973 switch( event.GetPoint(0).state )
975 case TouchPoint::Down:
977 OnButtonDown(); // Notification for derived classes.
979 // Sets the button state to ButtonDown.
985 OnButtonUp(); // Notification for derived classes.
987 // Sets the button state to ButtonUp.
991 case TouchPoint::Interrupted:
993 OnTouchPointInterrupted(); // Notification for derived classes.
995 // Sets the button state to the default (ButtonUp).
999 case TouchPoint::Leave:
1001 OnTouchPointLeave(); // Notification for derived classes.
1003 // Sets the button state to the default (ButtonUp).
1007 case TouchPoint::Motion:
1008 case TouchPoint::Stationary: // FALLTHROUGH
1015 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
1020 else if( 1 < event.GetPointCount() )
1022 OnTouchPointLeave(); // Notification for derived classes.
1024 // Sets the button state to the default (ButtonUp).
1031 void Button::OnInitialize()
1033 Actor self = Self();
1035 mTapDetector = TapGestureDetector::New();
1036 mTapDetector.Attach( self );
1037 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
1039 OnButtonInitialize();
1041 self.SetKeyboardFocusable( true );
1044 bool Button::OnAccessibilityActivated()
1046 return OnKeyboardEnter();
1049 bool Button::OnKeyboardEnter()
1051 // When the enter key is pressed, or button is activated, the click action is performed.
1052 Property::Map attributes;
1053 bool ret = DoClickAction( attributes );
1058 void Button::OnControlStageDisconnection()
1060 OnButtonStageDisconnection(); // Notification for derived classes.
1064 void Button::OnTap(Actor actor, const TapGesture& tap)
1069 void Button::SetUpTimer( float delay )
1071 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
1072 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
1073 mAutoRepeatingTimer.Start();
1076 bool Button::AutoRepeatingSlot()
1078 bool consumed = false;
1081 // Restart the autorepeat timer.
1082 SetUpTimer( mNextAutoRepeatingDelay );
1086 Toolkit::Button handle( GetOwner() );
1089 consumed = mReleasedSignal.Emit( handle );
1090 consumed |= mClickedSignal.Emit( handle );
1091 consumed |= mPressedSignal.Emit( handle );
1097 void Button::Pressed()
1099 if( mPaintState == UnselectedState )
1101 StopTransitionAnimation();
1103 // Notifies the derived class the button has been pressed.
1107 //(4) mSelectedContent (Inserted)
1108 //(3) mUnselectedContent
1109 //(2) mSelectedBackgroundContent (Inserted)
1110 //(1) mBackgroundContent
1112 AddButtonImage( mBackgroundContent );
1113 TransitionButtonImage( mSelectedBackgroundContent );
1114 AddButtonImage( mUnselectedContent );
1115 TransitionButtonImage( mSelectedContent );
1117 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1118 TransitionButtonImage( mDecoration[ SELECTED_DECORATION ] );
1121 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
1122 TransitionOut( mUnselectedContent );
1123 TransitionOut( mDisabledContent );
1124 TransitionOut( mDisabledSelectedContent );
1125 TransitionOut( mDisabledBackgroundContent );
1127 mPaintState = SelectedState;
1129 StartTransitionAnimation();
1133 void Button::Released()
1135 if( mPaintState == SelectedState )
1137 StopTransitionAnimation();
1139 // Notifies the derived class the button has been released.
1143 //(3) mUnselectedContent (Inserted)
1144 //(2) mSelectedContent
1145 //(1) mBackgroundContent
1147 AddButtonImage( mBackgroundContent );
1148 AddButtonImage( mSelectedContent );
1149 TransitionButtonImage( mUnselectedContent );
1151 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
1152 TransitionButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1155 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
1156 TransitionOut( mSelectedContent );
1157 TransitionOut( mSelectedBackgroundContent );
1158 TransitionOut( mDisabledContent );
1159 TransitionOut( mDisabledSelectedContent );
1160 TransitionOut( mDisabledBackgroundContent );
1162 mPaintState = UnselectedState;
1164 StartTransitionAnimation();
1168 Button::ButtonState Button::GetState()
1173 Button::PaintState Button::GetPaintState()
1178 void Button::PrepareAddButtonImage( Actor& actor )
1183 Self().Add( actor );
1184 PrepareForTranstionOut( actor );
1188 void Button::TransitionButtonImage( Actor& actor )
1192 if( !actor.GetParent() )
1194 Self().Add( actor );
1197 OnTransitionIn( actor );
1201 void Button::AddButtonImage( Actor& actor )
1206 Self().Add( actor );
1210 void Button::ReAddLabel()
1215 Self().Add( mLabel );
1219 void Button::RemoveButtonImage( Actor& actor )
1223 if( actor.GetParent() )
1225 Self().Remove( actor );
1227 PrepareForTranstionIn( actor );
1231 unsigned int Button::FindChildIndex( Actor& actor )
1233 Actor self = Self();
1234 unsigned int childrenNum = self.GetChildCount();
1236 for( unsigned int i = 0; i < childrenNum; i++ )
1238 Actor child = self.GetChildAt( i );
1239 if( child == actor )
1248 void Button::TransitionOut( Actor actor )
1250 OnTransitionOut( actor );
1253 void Button::ResetImageLayers()
1255 // Ensure that all layers are in the correct order and state according to the paint state
1257 switch( mPaintState )
1259 case UnselectedState:
1262 //(2) mUnselectedContent
1263 //(1) mBackgroundContent
1265 RemoveButtonImage( mDecoration[ SELECTED_DECORATION ] );
1266 RemoveButtonImage( mSelectedContent );
1267 RemoveButtonImage( mSelectedBackgroundContent );
1268 RemoveButtonImage( mDisabledContent );
1269 RemoveButtonImage( mDisabledSelectedContent );
1270 RemoveButtonImage( mDisabledBackgroundContent );
1272 PrepareAddButtonImage( mBackgroundContent );
1273 PrepareAddButtonImage( mUnselectedContent );
1275 PrepareAddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1282 //(3) mSelectedContent
1283 //(2) mSelectedBackgroundContent
1284 //(1) mBackgroundContent
1286 RemoveButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1287 RemoveButtonImage( mUnselectedContent );
1288 RemoveButtonImage( mDisabledContent );
1289 RemoveButtonImage( mDisabledSelectedContent );
1290 RemoveButtonImage( mDisabledBackgroundContent );
1292 PrepareAddButtonImage( mBackgroundContent );
1293 PrepareAddButtonImage( mSelectedBackgroundContent );
1294 PrepareAddButtonImage( mSelectedContent );
1296 PrepareAddButtonImage( mDecoration[ SELECTED_DECORATION ] );
1300 case DisabledUnselectedState:
1303 //(2) mDisabledContent
1304 //(1) mDisabledBackgroundContent
1306 RemoveButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1307 RemoveButtonImage( mUnselectedContent );
1308 RemoveButtonImage( mBackgroundContent );
1309 RemoveButtonImage( mDecoration[ SELECTED_DECORATION ] );
1310 RemoveButtonImage( mSelectedContent );
1311 RemoveButtonImage( mDisabledSelectedContent );
1312 RemoveButtonImage( mSelectedBackgroundContent );
1314 PrepareAddButtonImage( mDisabledBackgroundContent ? mDisabledBackgroundContent : mBackgroundContent );
1315 PrepareAddButtonImage( mDisabledContent ? mDisabledContent : mUnselectedContent );
1317 PrepareAddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1321 case DisabledSelectedState:
1324 // (2) mDisabledSelectedContent
1325 // (1) mDisabledBackgroundContent
1327 RemoveButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1328 RemoveButtonImage( mUnselectedContent );
1329 RemoveButtonImage( mDecoration[ SELECTED_DECORATION ] );
1330 RemoveButtonImage( mSelectedContent );
1331 RemoveButtonImage( mBackgroundContent );
1332 RemoveButtonImage( mSelectedBackgroundContent );
1333 RemoveButtonImage( mDisabledContent );
1335 if( mDisabledBackgroundContent )
1337 PrepareAddButtonImage( mDisabledBackgroundContent );
1341 PrepareAddButtonImage( mBackgroundContent );
1342 PrepareAddButtonImage( mSelectedBackgroundContent );
1345 PrepareAddButtonImage( mDisabledSelectedContent ? mDisabledSelectedContent : mSelectedContent );
1347 PrepareAddButtonImage( mDecoration[ SELECTED_DECORATION ] );
1354 void Button::StartTransitionAnimation()
1356 if( mTransitionAnimation )
1358 mTransitionAnimation.Play();
1366 void Button::StopTransitionAnimation()
1368 if( mTransitionAnimation )
1370 mTransitionAnimation.Clear();
1371 mTransitionAnimation.Reset();
1375 Dali::Animation Button::GetTransitionAnimation()
1377 if( !mTransitionAnimation )
1379 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
1380 mTransitionAnimation.FinishedSignal().Connect( this, &Button::TransitionAnimationFinished );
1383 return mTransitionAnimation;
1386 void Button::TransitionAnimationFinished( Dali::Animation& source )
1388 StopTransitionAnimation();
1392 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1394 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1400 case Toolkit::Button::Property::DISABLED:
1402 GetImplementation( button ).SetDisabled( value.Get< bool >() );
1406 case Toolkit::Button::Property::AUTO_REPEATING:
1408 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
1412 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1414 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
1418 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1420 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
1424 case Toolkit::Button::Property::TOGGLABLE:
1426 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
1430 case Toolkit::Button::Property::SELECTED:
1432 GetImplementation( button ).SetSelected( value.Get< bool >() );
1436 case Toolkit::Button::Property::UNSELECTED_STATE_IMAGE:
1438 GetImplementation( button ).SetUnselectedImage( value.Get< std::string >() );
1442 case Toolkit::Button::Property::SELECTED_STATE_IMAGE:
1444 GetImplementation( button ).SetSelectedImage( value.Get< std::string >() );
1448 case Toolkit::Button::Property::DISABLED_STATE_IMAGE:
1450 GetImplementation( button ).SetDisabledImage( value.Get< std::string >() );
1454 case Toolkit::Button::Property::UNSELECTED_COLOR:
1456 GetImplementation( button ).SetUnselectedColor( value.Get< Vector4 >() );
1460 case Toolkit::Button::Property::SELECTED_COLOR:
1462 GetImplementation( button ).SetSelectedColor( value.Get< Vector4 >() );
1466 case Toolkit::Button::Property::LABEL_TEXT:
1468 GetImplementation( button ).SetLabelText( value.Get< std::string >() );
1475 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
1477 Property::Value value;
1479 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1483 switch ( propertyIndex )
1485 case Toolkit::Button::Property::DISABLED:
1487 value = GetImplementation( button ).mDisabled;
1491 case Toolkit::Button::Property::AUTO_REPEATING:
1493 value = GetImplementation( button ).mAutoRepeating;
1497 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1499 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
1503 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1505 value = GetImplementation( button ).mNextAutoRepeatingDelay;
1509 case Toolkit::Button::Property::TOGGLABLE:
1511 value = GetImplementation( button ).mTogglableButton;
1515 case Toolkit::Button::Property::SELECTED:
1517 value = GetImplementation( button ).mSelected;
1521 case Toolkit::Button::Property::UNSELECTED_STATE_IMAGE:
1523 value = GetImplementation( button ).GetUnselectedImageFilename();
1527 case Toolkit::Button::Property::SELECTED_STATE_IMAGE:
1529 value = GetImplementation( button ).GetSelectedImageFilename();
1533 case Toolkit::Button::Property::DISABLED_STATE_IMAGE:
1535 value = GetImplementation( button ).GetDisabledImageFilename();
1539 case Toolkit::Button::Property::UNSELECTED_COLOR:
1541 value = GetImplementation( button ).GetUnselectedColor();
1545 case Toolkit::Button::Property::SELECTED_COLOR:
1547 value = GetImplementation( button ).GetSelectedColor();
1551 case Toolkit::Button::Property::LABEL_TEXT:
1553 value = GetImplementation( button ).GetLabelText();
1564 void Button::SetLabel( Actor label )
1566 if( mLabel != label )
1568 if( mLabel && mLabel.GetParent() )
1570 mLabel.GetParent().Remove( mLabel );
1574 mLabel.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_LABEL );
1575 mLabel.SetPosition( 0.f, 0.f );
1577 // label should be the top of the button
1578 Self().Add( mLabel );
1586 void Button::SetButtonImage( Actor image )
1590 StopTransitionAnimation();
1592 SetupContent( mUnselectedContent, image );
1594 OnUnselectedImageSet();
1599 void Button::SetSelectedImage( Actor image )
1603 StopTransitionAnimation();
1605 SetupContent( mSelectedContent, image );
1607 OnSelectedImageSet();
1612 void Button::SetBackgroundImage( Actor image )
1616 StopTransitionAnimation();
1618 SetupContent( mBackgroundContent, image );
1620 OnBackgroundImageSet();
1625 void Button::SetSelectedBackgroundImage( Actor image )
1629 StopTransitionAnimation();
1631 SetupContent( mSelectedBackgroundContent, image );
1633 OnSelectedBackgroundImageSet();
1638 void Button::SetDisabledImage( Actor image )
1642 StopTransitionAnimation();
1644 SetupContent( mDisabledContent, image );
1646 OnDisabledImageSet();
1651 void Button::SetDisabledSelectedImage( Actor image )
1655 StopTransitionAnimation();
1657 SetupContent( mDisabledSelectedContent, image );
1659 OnDisabledSelectedImageSet();
1664 void Button::SetDisabledBackgroundImage( Actor image )
1668 StopTransitionAnimation();
1670 SetupContent( mDisabledBackgroundContent, image );
1672 OnDisabledBackgroundImageSet();
1677 Actor Button::GetButtonImage() const
1679 return mUnselectedContent;
1682 Actor Button::GetSelectedImage() const
1684 return mSelectedContent;
1688 } // namespace Internal
1690 } // namespace Toolkit