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>
33 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
36 * Button states and contents
37 * (3) mSelectedContent
38 * (2) mUnselectedContent (2) mSelectedBackgroundContent
39 * (1) mBackgroundContent (1) mBackgroundContent
40 * < unselected > ----------------------- < selected >
42 * | OnDisabled() | OnDisabled()
44 * < disabled > < disabled-selected >
45 * (2) mDisabledContent (2) mDisabledSelectedContent
46 * (1) mDisabledBackgroundContent (1) mDisabledBackgroundContent
48 * The drawing order of child actors is as follows.
51 * | mUnselectedContent / mSelectedContent / mDisabledContent / mDisabledSelectedContent
52 * | mSelectedBackgroundContent
53 * Bottom mBackgroundContent / mDisabledBackgroundContent
55 * Some of contents may be missed.
56 * 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.
73 // empty handle as we cannot create button (but type registered for clicked signal)
77 // Setup properties, signals and actions using the type-registry.
78 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create );
80 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled", BOOLEAN, DISABLED )
81 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "autoRepeating", BOOLEAN, AUTO_REPEATING )
82 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "initialAutoRepeatingDelay", FLOAT, INITIAL_AUTO_REPEATING_DELAY )
83 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "nextAutoRepeatingDelay", FLOAT, NEXT_AUTO_REPEATING_DELAY )
84 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "togglable", BOOLEAN, TOGGLABLE )
85 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected", BOOLEAN, SELECTED )
86 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselectedStateImage", STRING, UNSELECTED_STATE_IMAGE )
87 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selectedStateImage", STRING, SELECTED_STATE_IMAGE )
88 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabledStateImage", STRING, DISABLED_STATE_IMAGE )
89 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselectedColor", VECTOR4, UNSELECTED_COLOR )
90 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selectedColor", VECTOR4, SELECTED_COLOR )
91 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "label", MAP, LABEL )
93 // Deprecated properties:
94 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "labelText", STRING, LABEL_TEXT )
97 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "pressed", SIGNAL_PRESSED )
98 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "released", SIGNAL_RELEASED )
99 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "clicked", SIGNAL_CLICKED )
100 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "stateChanged", SIGNAL_STATE_CHANGED )
103 DALI_ACTION_REGISTRATION( Toolkit, Button, "buttonClick", ACTION_BUTTON_CLICK )
105 DALI_TYPE_REGISTRATION_END()
107 const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f );
108 const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f );
110 } // unnamed namespace
113 : Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ),
114 mAutoRepeatingTimer(),
115 mUnselectedColor( Color::WHITE ), // The natural colors of the specified images will be used by default.
116 mSelectedColor( Color::WHITE ),
118 mAutoRepeating( false ),
119 mTogglableButton( false ),
121 mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ),
122 mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ),
123 mAnimationTime( 0.0f ),
124 mClickActionPerforming( false ),
126 mPaintState( UnselectedState )
134 void Button::SetDisabled( bool disabled )
136 if( disabled == mDisabled )
141 StopTransitionAnimation();
143 mDisabled = disabled;
145 // Notifies the derived class the button has been disabled.
148 switch( mPaintState )
150 case UnselectedState:
153 //(3) mDisabledContent (Inserted)
154 //(4) mUnselectedContent
155 //(2) mDisabledBackgroundContent (Inserted)
156 //(1) mBackgroundContent
158 AddButtonImage( mBackgroundContent );
159 TransitionButtonImage( mDisabledBackgroundContent );
160 AddButtonImage( mUnselectedContent );
161 TransitionButtonImage( mDisabledContent );
163 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
166 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
167 TransitionOut( mUnselectedContent );
168 TransitionOut( mSelectedContent );
169 TransitionOut( mBackgroundContent );
170 TransitionOut( mSelectedBackgroundContent );
171 TransitionOut( mDisabledSelectedContent );
173 mPaintState = DisabledUnselectedState;
179 //(5) mDisabledSelectedContent (Inserted)
180 //(4) mSelectedContent
181 //(3) mDisabledBackgroundContent (Inserted)
182 //(2) mSelectedBackgroundContent
183 //(1) mBackgroundContent
185 AddButtonImage( mBackgroundContent );
186 AddButtonImage( mSelectedBackgroundContent );
187 TransitionButtonImage( mDisabledBackgroundContent );
188 AddButtonImage( mSelectedContent );
189 TransitionButtonImage( mDisabledSelectedContent );
191 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
194 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
195 TransitionOut( mUnselectedContent );
196 TransitionOut( mSelectedContent );
197 TransitionOut( mBackgroundContent );
198 TransitionOut( mSelectedBackgroundContent );
199 TransitionOut( mDisabledContent );
201 mPaintState = DisabledSelectedState;
204 case DisabledUnselectedState:
207 //(3) mUnselectedContent (Inserted)
208 //(4) mDisabledContent
209 //(2) mBackgroundContent (Inserted)
210 //(1) mDisabledBackgroundContent
212 AddButtonImage( mDisabledBackgroundContent );
213 TransitionButtonImage( mBackgroundContent );
214 AddButtonImage( mDisabledContent );
215 TransitionButtonImage( mUnselectedContent );
217 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
220 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
221 TransitionOut( mSelectedContent );
222 TransitionOut( mSelectedBackgroundContent );
223 TransitionOut( mDisabledContent );
224 TransitionOut( mDisabledSelectedContent );
225 TransitionOut( mDisabledBackgroundContent );
227 mPaintState = UnselectedState;
230 case DisabledSelectedState:
233 //(4) mSelectedContent (Inserted)
234 //(5) mDisabledSelectedContent
235 //(3) mSelectedBackgroundContent (Inserted)
236 //(2) mBackgroundContent (Inserted)
237 //(1) mDisabledBackgroundContent
239 AddButtonImage( mDisabledBackgroundContent );
240 TransitionButtonImage( mBackgroundContent );
241 TransitionButtonImage( mSelectedBackgroundContent );
242 AddButtonImage( mDisabledSelectedContent );
243 TransitionButtonImage( mSelectedContent );
245 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
248 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
249 TransitionOut( mUnselectedContent );
250 TransitionOut( mDisabledContent );
251 TransitionOut( mDisabledSelectedContent );
252 TransitionOut( mDisabledBackgroundContent );
254 mPaintState = SelectedState;
259 StartTransitionAnimation();
262 bool Button::IsDisabled() const
267 void Button::SetAutoRepeating( bool autoRepeating )
269 mAutoRepeating = autoRepeating;
271 // An autorepeating button can't be a togglable button.
274 mTogglableButton = false;
278 // Emit a signal is not wanted, only change the appearance.
279 SetSelected( false, false );
284 bool Button::IsAutoRepeating() const
286 return mAutoRepeating;
289 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
291 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
292 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
295 float Button::GetInitialAutoRepeatingDelay() const
297 return mInitialAutoRepeatingDelay;
300 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
302 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
303 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
306 float Button::GetNextAutoRepeatingDelay() const
308 return mNextAutoRepeatingDelay;
311 void Button::SetTogglableButton( bool togglable )
313 mTogglableButton = togglable;
315 // A togglable button can't be an autorepeating button.
318 mAutoRepeating = false;
322 bool Button::IsTogglableButton() const
324 return mTogglableButton;
327 void Button::SetSelected( bool selected )
329 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
331 SetSelected( selected, true );
335 void Button::SetSelected( bool selected, bool emitSignal )
337 StopTransitionAnimation();
339 mSelected = selected;
341 // Notifies the derived class the button has been selected.
344 switch( mPaintState )
346 case UnselectedState:
349 //(3) mSelectedContent (Inserted)
350 //(4) mUnselectedContent
351 //(2) mSelectedBackgroundContent (Inserted)
352 //(1) mBackgroundContent
354 AddButtonImage( mBackgroundContent );
355 TransitionButtonImage( mSelectedBackgroundContent );
356 AddButtonImage( mUnselectedContent );
357 TransitionButtonImage( mSelectedContent );
359 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
360 TransitionButtonImage( mDecoration[ SELECTED_DECORATION ] );
363 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
364 TransitionOut( mUnselectedContent );
365 TransitionOut( mDisabledContent );
366 TransitionOut( mDisabledSelectedContent );
367 TransitionOut( mDisabledBackgroundContent );
369 mPaintState = SelectedState;
375 //(3) mUnselectedContent (Inserted)
376 //(2) mSelectedContent
377 //(1) mBackgroundContent
379 AddButtonImage( mBackgroundContent );
380 AddButtonImage( mSelectedContent );
381 TransitionButtonImage( mUnselectedContent );
383 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
384 TransitionButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
387 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
388 TransitionOut( mSelectedContent );
389 TransitionOut( mSelectedBackgroundContent );
390 TransitionOut( mDisabledContent );
391 TransitionOut( mDisabledSelectedContent );
392 TransitionOut( mDisabledBackgroundContent );
394 mPaintState = UnselectedState;
397 case DisabledUnselectedState:
398 case DisabledSelectedState:
400 DALI_ASSERT_DEBUG( 0 && "Shouldn't be able to change paint state if the button is disabled." );
405 StartTransitionAnimation();
409 Toolkit::Button handle( GetOwner() );
412 mStateChangedSignal.Emit( handle );
418 bool Button::IsSelected() const
420 return mTogglableButton && mSelected;
423 void Button::SetAnimationTime( float animationTime )
425 mAnimationTime = animationTime;
428 float Button::GetAnimationTime() const
430 return mAnimationTime;
433 void Button::SetLabelText( const std::string& label )
435 Property::Map labelProperty;
436 labelProperty.Insert( "text", label );
437 ModifyLabel( labelProperty );
440 std::string Button::GetLabelText() const
442 Toolkit::TextLabel label = Dali::Toolkit::TextLabel::DownCast( mLabel );
445 return label.GetProperty<std::string>( Dali::Toolkit::TextLabel::Property::TEXT );
447 return std::string();
450 void Button::ModifyLabel( const Property::Map& properties )
452 // If we don't have a label yet, create one.
455 // If we don't have a label, create one and set it up.
456 // Note: The label text is set from the passed in property map after creation.
457 mLabel = Toolkit::TextLabel::New();
458 mLabel.SetPosition( 0.0f, 0.0f );
459 // label should be the top of the button
460 Self().Add( mLabel );
463 // Set any properties specified for the label by iterating through all property key-value pairs.
464 for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i )
466 const StringValuePair& propertyPair( properties.GetPair( i ) );
468 // Convert the property string to a property index.
469 Property::Index setPropertyIndex = mLabel.GetPropertyIndex( propertyPair.first );
470 if( setPropertyIndex != Property::INVALID_INDEX )
472 // If the conversion worked, we have a valid property index,
473 // Set the property to the new value.
474 mLabel.SetProperty( setPropertyIndex, propertyPair.second );
478 // Notify derived button classes of the change.
484 Actor& Button::GetLabelActor()
489 void Button::SetDecoration( DecorationState state, Actor actor )
491 if( mDecoration[ state ] && mDecoration[ state ].GetParent() )
493 mDecoration[ state ].Unparent();
496 mDecoration[ state ] = actor;
497 mDecoration[ state ].SetColorMode( USE_OWN_COLOR );
503 Actor& Button::GetDecoration( DecorationState state )
505 return mDecoration[ state ];
508 void Button::SetupContent( Actor& actorToModify, Actor newActor )
512 StopTransitionAnimation();
514 if( actorToModify && actorToModify.GetParent() )
516 actorToModify.Unparent();
519 actorToModify = newActor;
523 actorToModify.SetAnchorPoint( AnchorPoint::TOP_LEFT );
524 actorToModify.SetParentOrigin( ParentOrigin::TOP_LEFT );
525 actorToModify.SetPosition( 0.f, 0.f );
532 void Button::SetUnselectedColor( const Vector4& color )
534 mUnselectedColor = color;
536 if( mUnselectedContent && !GetUnselectedImageFilename().empty() )
538 // If there is existing unselected content, change the color on it directly.
539 mUnselectedContent.SetColor( mUnselectedColor );
543 // If there is no existing content, create a new actor to use for flat color.
544 SetupContent( mUnselectedContent, CreateSolidColorActor( mUnselectedColor ) );
545 mUnselectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
549 const Vector4 Button::GetUnselectedColor() const
551 return mUnselectedColor;
554 void Button::SetSelectedColor( const Vector4& color )
556 mSelectedColor = color;
558 if( mSelectedContent && !GetSelectedImageFilename().empty() )
560 // If there is existing unselected content, change the color on it directly.
561 mSelectedContent.SetColor( mSelectedColor );
565 // If there is no existing content, create a new actor to use for flat color.
566 SetupContent( mSelectedContent, CreateSolidColorActor( mSelectedColor ) );
567 mSelectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
571 const Vector4 Button::GetSelectedColor() const
573 return mSelectedColor;
576 void Button::SetUnselectedImage( const std::string& filename )
578 Toolkit::ImageView newContent;
579 if( !filename.empty() )
581 newContent = Toolkit::ImageView::New( filename );
585 newContent = Toolkit::ImageView::New();
590 SetupContent( mUnselectedContent, newContent );
592 mUnselectedContent.SetColor( mUnselectedColor );
594 OnUnselectedImageSet();
599 Actor& Button::GetUnselectedImage()
601 return mUnselectedContent;
604 void Button::SetSelectedImage( const std::string& filename )
606 Toolkit::ImageView newContent;
607 if( !filename.empty() )
609 newContent = Toolkit::ImageView::New( filename );
613 newContent = Toolkit::ImageView::New();
618 SetupContent( mSelectedContent, newContent );
620 mSelectedContent.SetColor( mSelectedColor );
622 OnSelectedImageSet();
627 Actor& Button::GetSelectedImage()
629 return mSelectedContent;
632 void Button::SetBackgroundImage( const std::string& filename )
634 SetupContent( mBackgroundContent, Toolkit::ImageView::New( filename ) );
636 OnBackgroundImageSet();
640 Actor& Button::GetBackgroundImage()
642 return mBackgroundContent;
645 void Button::SetSelectedBackgroundImage( const std::string& filename )
647 SetupContent( mSelectedBackgroundContent, Toolkit::ImageView::New( filename ) );
649 OnSelectedBackgroundImageSet();
653 Actor& Button::GetSelectedBackgroundImage()
655 return mSelectedBackgroundContent;
658 void Button::SetDisabledImage( const std::string& filename )
660 SetupContent( mDisabledContent, Toolkit::ImageView::New( filename ) );
662 OnDisabledImageSet();
666 Actor& Button::GetDisabledImage()
668 return mDisabledContent;
671 void Button::SetDisabledSelectedImage( const std::string& filename )
673 SetupContent( mDisabledSelectedContent, Toolkit::ImageView::New( filename ) );
675 OnDisabledSelectedImageSet();
679 Actor& Button::GetDisabledSelectedImage()
681 return mDisabledSelectedContent;
684 void Button::SetDisabledBackgroundImage( const std::string& filename )
686 SetupContent( mDisabledBackgroundContent, Toolkit::ImageView::New( filename ) );
688 OnDisabledBackgroundImageSet();
692 Actor& Button::GetDisabledBackgroundImage()
694 return mDisabledBackgroundContent;
697 std::string Button::GetUnselectedImageFilename() const
699 if( mUnselectedContent )
701 ResourceImage image = ResourceImage::DownCast( mUnselectedContent );
704 return image.GetUrl();
707 return std::string();
710 std::string Button::GetSelectedImageFilename() const
712 if( mSelectedContent )
714 ResourceImage image = ResourceImage::DownCast( mSelectedContent );
717 return image.GetUrl();
720 return std::string();
723 std::string Button::GetBackgroundImageFilename() const
725 if( mBackgroundContent )
727 ResourceImage image = ResourceImage::DownCast( mBackgroundContent );
730 return image.GetUrl();
733 return std::string();
736 std::string Button::GetSelectedBackgroundImageFilename() const
738 if( mSelectedBackgroundContent )
740 ResourceImage image = ResourceImage::DownCast( mSelectedBackgroundContent );
743 return image.GetUrl();
746 return std::string();
749 std::string Button::GetDisabledImageFilename() const
751 if( mDisabledContent )
753 ResourceImage image = ResourceImage::DownCast( mDisabledContent );
756 return image.GetUrl();
759 return std::string();
762 std::string Button::GetDisabledSelectedImageFilename() const
764 if( mDisabledSelectedContent )
766 ResourceImage image = ResourceImage::DownCast( mDisabledSelectedContent );
769 return image.GetUrl();
772 return std::string();
775 std::string Button::GetDisabledBackgroundImageFilename() const
777 if( mDisabledBackgroundContent )
779 ResourceImage image = ResourceImage::DownCast( mDisabledBackgroundContent );
782 return image.GetUrl();
785 return std::string();
788 bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
792 Dali::BaseHandle handle( object );
794 Toolkit::Button button = Toolkit::Button::DownCast( handle );
796 DALI_ASSERT_ALWAYS( button );
798 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
800 ret = GetImplementation( button ).DoClickAction( attributes );
806 bool Button::DoClickAction( const Property::Map& attributes )
808 // Prevents the button signals from doing a recursive loop by sending an action
809 // and re-emitting the signals.
810 if( !mClickActionPerforming )
812 mClickActionPerforming = true;
816 mClickActionPerforming = false;
824 void Button::OnButtonStageDisconnection()
826 if( ButtonDown == mState )
828 if( !mTogglableButton )
834 mAutoRepeatingTimer.Reset();
840 void Button::OnButtonDown()
842 if( !mTogglableButton )
848 SetUpTimer( mInitialAutoRepeatingDelay );
852 // The pressed signal should be emitted regardless of toggle mode.
853 Toolkit::Button handle( GetOwner() );
854 mPressedSignal.Emit( handle );
857 void Button::OnButtonUp()
859 if( ButtonDown == mState )
861 if( mTogglableButton )
863 SetSelected( !mSelected );
871 mAutoRepeatingTimer.Reset();
875 // The clicked and released signals should be emitted regardless of toggle mode.
876 Toolkit::Button handle( GetOwner() );
877 mReleasedSignal.Emit( handle );
878 mClickedSignal.Emit( handle );
882 void Button::OnTouchPointLeave()
884 if( ButtonDown == mState )
886 if( !mTogglableButton )
892 mAutoRepeatingTimer.Reset();
896 // The released signal should be emitted regardless of toggle mode.
897 Toolkit::Button handle( GetOwner() );
898 mReleasedSignal.Emit( handle );
902 void Button::OnTouchPointInterrupted()
907 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
909 return mPressedSignal;
912 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
914 return mReleasedSignal;
917 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
919 return mClickedSignal;
922 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
924 return mStateChangedSignal;
927 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
929 Dali::BaseHandle handle( object );
931 bool connected( true );
932 Toolkit::Button button = Toolkit::Button::DownCast( handle );
934 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
936 button.PressedSignal().Connect( tracker, functor );
938 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
940 button.ReleasedSignal().Connect( tracker, functor );
942 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
944 button.ClickedSignal().Connect( tracker, functor );
946 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
948 button.StateChangedSignal().Connect( tracker, functor );
952 // signalName does not match any signal
959 bool Button::OnTouchEvent(const TouchEvent& event)
961 // Only events are processed when the button is not disabled and the touch event has only
963 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
965 switch( event.GetPoint(0).state )
967 case TouchPoint::Down:
969 OnButtonDown(); // Notification for derived classes.
971 // Sets the button state to ButtonDown.
977 OnButtonUp(); // Notification for derived classes.
979 // Sets the button state to ButtonUp.
983 case TouchPoint::Interrupted:
985 OnTouchPointInterrupted(); // Notification for derived classes.
987 // Sets the button state to the default (ButtonUp).
991 case TouchPoint::Leave:
993 OnTouchPointLeave(); // Notification for derived classes.
995 // Sets the button state to the default (ButtonUp).
999 case TouchPoint::Motion:
1000 case TouchPoint::Stationary: // FALLTHROUGH
1007 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
1012 else if( 1 < event.GetPointCount() )
1014 OnTouchPointLeave(); // Notification for derived classes.
1016 // Sets the button state to the default (ButtonUp).
1023 void Button::OnInitialize()
1025 Actor self = Self();
1027 mTapDetector = TapGestureDetector::New();
1028 mTapDetector.Attach( self );
1029 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
1031 OnButtonInitialize();
1033 self.SetKeyboardFocusable( true );
1036 bool Button::OnAccessibilityActivated()
1038 return OnKeyboardEnter();
1041 bool Button::OnKeyboardEnter()
1043 // When the enter key is pressed, or button is activated, the click action is performed.
1044 Property::Map attributes;
1045 bool ret = DoClickAction( attributes );
1050 void Button::OnControlStageDisconnection()
1052 OnButtonStageDisconnection(); // Notification for derived classes.
1056 void Button::OnTap(Actor actor, const TapGesture& tap)
1061 void Button::SetUpTimer( float delay )
1063 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
1064 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
1065 mAutoRepeatingTimer.Start();
1068 bool Button::AutoRepeatingSlot()
1070 bool consumed = false;
1073 // Restart the autorepeat timer.
1074 SetUpTimer( mNextAutoRepeatingDelay );
1078 Toolkit::Button handle( GetOwner() );
1081 consumed = mReleasedSignal.Emit( handle );
1082 consumed |= mClickedSignal.Emit( handle );
1083 consumed |= mPressedSignal.Emit( handle );
1089 void Button::Pressed()
1091 if( mPaintState == UnselectedState )
1093 StopTransitionAnimation();
1095 // Notifies the derived class the button has been pressed.
1099 //(4) mSelectedContent (Inserted)
1100 //(3) mUnselectedContent
1101 //(2) mSelectedBackgroundContent (Inserted)
1102 //(1) mBackgroundContent
1104 AddButtonImage( mBackgroundContent );
1105 TransitionButtonImage( mSelectedBackgroundContent );
1106 AddButtonImage( mUnselectedContent );
1107 TransitionButtonImage( mSelectedContent );
1109 AddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1110 TransitionButtonImage( mDecoration[ SELECTED_DECORATION ] );
1113 TransitionOut( mDecoration[ UNSELECTED_DECORATION ] );
1114 TransitionOut( mUnselectedContent );
1115 TransitionOut( mDisabledContent );
1116 TransitionOut( mDisabledSelectedContent );
1117 TransitionOut( mDisabledBackgroundContent );
1119 mPaintState = SelectedState;
1121 StartTransitionAnimation();
1125 void Button::Released()
1127 if( mPaintState == SelectedState )
1129 StopTransitionAnimation();
1131 // Notifies the derived class the button has been released.
1135 //(3) mUnselectedContent (Inserted)
1136 //(2) mSelectedContent
1137 //(1) mBackgroundContent
1139 AddButtonImage( mBackgroundContent );
1140 AddButtonImage( mSelectedContent );
1141 TransitionButtonImage( mUnselectedContent );
1143 AddButtonImage( mDecoration[ SELECTED_DECORATION ] );
1144 TransitionButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1147 TransitionOut( mDecoration[ SELECTED_DECORATION ] );
1148 TransitionOut( mSelectedContent );
1149 TransitionOut( mSelectedBackgroundContent );
1150 TransitionOut( mDisabledContent );
1151 TransitionOut( mDisabledSelectedContent );
1152 TransitionOut( mDisabledBackgroundContent );
1154 mPaintState = UnselectedState;
1156 StartTransitionAnimation();
1160 Button::ButtonState Button::GetState()
1165 Button::PaintState Button::GetPaintState()
1170 void Button::PrepareAddButtonImage( Actor& actor )
1174 Self().Add( actor );
1175 PrepareForTranstionOut( actor );
1179 void Button::TransitionButtonImage( Actor& actor )
1183 if( !actor.GetParent() )
1185 Self().Add( actor );
1188 OnTransitionIn( actor );
1192 void Button::AddButtonImage( Actor& actor )
1196 Self().Add( actor );
1200 void Button::ReAddLabel()
1205 Self().Add( mLabel );
1209 void Button::RemoveButtonImage( Actor& actor )
1213 if( actor.GetParent() )
1215 Self().Remove( actor );
1217 PrepareForTranstionIn( actor );
1221 unsigned int Button::FindChildIndex( Actor& actor )
1223 Actor self = Self();
1224 unsigned int childrenNum = self.GetChildCount();
1226 for( unsigned int i = 0; i < childrenNum; i++ )
1228 Actor child = self.GetChildAt( i );
1229 if( child == actor )
1238 void Button::TransitionOut( Actor actor )
1240 OnTransitionOut( actor );
1243 void Button::ResetImageLayers()
1245 // Ensure that all layers are in the correct order and state according to the paint state
1247 switch( mPaintState )
1249 case UnselectedState:
1252 //(2) mUnselectedContent
1253 //(1) mBackgroundContent
1255 RemoveButtonImage( mDecoration[ SELECTED_DECORATION ] );
1256 RemoveButtonImage( mSelectedContent );
1257 RemoveButtonImage( mSelectedBackgroundContent );
1258 RemoveButtonImage( mDisabledContent );
1259 RemoveButtonImage( mDisabledSelectedContent );
1260 RemoveButtonImage( mDisabledBackgroundContent );
1262 PrepareAddButtonImage( mBackgroundContent );
1263 PrepareAddButtonImage( mUnselectedContent );
1265 PrepareAddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1272 //(3) mSelectedContent
1273 //(2) mSelectedBackgroundContent
1274 //(1) mBackgroundContent
1276 RemoveButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1277 RemoveButtonImage( mUnselectedContent );
1278 RemoveButtonImage( mDisabledContent );
1279 RemoveButtonImage( mDisabledSelectedContent );
1280 RemoveButtonImage( mDisabledBackgroundContent );
1282 PrepareAddButtonImage( mBackgroundContent );
1283 PrepareAddButtonImage( mSelectedBackgroundContent );
1284 PrepareAddButtonImage( mSelectedContent );
1286 PrepareAddButtonImage( mDecoration[ SELECTED_DECORATION ] );
1290 case DisabledUnselectedState:
1293 //(2) mDisabledContent
1294 //(1) mDisabledBackgroundContent
1296 RemoveButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1297 RemoveButtonImage( mUnselectedContent );
1298 RemoveButtonImage( mBackgroundContent );
1299 RemoveButtonImage( mDecoration[ SELECTED_DECORATION ] );
1300 RemoveButtonImage( mSelectedContent );
1301 RemoveButtonImage( mDisabledSelectedContent );
1302 RemoveButtonImage( mSelectedBackgroundContent );
1304 PrepareAddButtonImage( mDisabledBackgroundContent ? mDisabledBackgroundContent : mBackgroundContent );
1305 PrepareAddButtonImage( mDisabledContent ? mDisabledContent : mUnselectedContent );
1307 PrepareAddButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1311 case DisabledSelectedState:
1314 // (2) mDisabledSelectedContent
1315 // (1) mDisabledBackgroundContent
1317 RemoveButtonImage( mDecoration[ UNSELECTED_DECORATION ] );
1318 RemoveButtonImage( mUnselectedContent );
1319 RemoveButtonImage( mDecoration[ SELECTED_DECORATION ] );
1320 RemoveButtonImage( mSelectedContent );
1321 RemoveButtonImage( mBackgroundContent );
1322 RemoveButtonImage( mSelectedBackgroundContent );
1323 RemoveButtonImage( mDisabledContent );
1325 if( mDisabledBackgroundContent )
1327 PrepareAddButtonImage( mDisabledBackgroundContent );
1331 PrepareAddButtonImage( mBackgroundContent );
1332 PrepareAddButtonImage( mSelectedBackgroundContent );
1335 PrepareAddButtonImage( mDisabledSelectedContent ? mDisabledSelectedContent : mSelectedContent );
1337 PrepareAddButtonImage( mDecoration[ SELECTED_DECORATION ] );
1344 void Button::StartTransitionAnimation()
1346 if( mTransitionAnimation )
1348 mTransitionAnimation.Play();
1356 void Button::StopTransitionAnimation()
1358 if( mTransitionAnimation )
1360 mTransitionAnimation.Clear();
1361 mTransitionAnimation.Reset();
1365 Dali::Animation Button::GetTransitionAnimation()
1367 if( !mTransitionAnimation )
1369 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
1370 mTransitionAnimation.FinishedSignal().Connect( this, &Button::TransitionAnimationFinished );
1373 return mTransitionAnimation;
1376 void Button::TransitionAnimationFinished( Dali::Animation& source )
1378 StopTransitionAnimation();
1382 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1384 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1390 case Toolkit::Button::Property::DISABLED:
1392 GetImplementation( button ).SetDisabled( value.Get< bool >() );
1396 case Toolkit::Button::Property::AUTO_REPEATING:
1398 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
1402 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1404 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
1408 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1410 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
1414 case Toolkit::Button::Property::TOGGLABLE:
1416 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
1420 case Toolkit::Button::Property::SELECTED:
1422 GetImplementation( button ).SetSelected( value.Get< bool >() );
1426 case Toolkit::Button::Property::UNSELECTED_STATE_IMAGE:
1428 GetImplementation( button ).SetUnselectedImage( value.Get< std::string >() );
1432 case Toolkit::Button::Property::SELECTED_STATE_IMAGE:
1434 GetImplementation( button ).SetSelectedImage( value.Get< std::string >() );
1438 case Toolkit::Button::Property::DISABLED_STATE_IMAGE:
1440 GetImplementation( button ).SetDisabledImage( value.Get< std::string >() );
1444 case Toolkit::Button::Property::UNSELECTED_COLOR:
1446 GetImplementation( button ).SetUnselectedColor( value.Get< Vector4 >() );
1450 case Toolkit::Button::Property::SELECTED_COLOR:
1452 GetImplementation( button ).SetSelectedColor( value.Get< Vector4 >() );
1456 case Toolkit::Button::Property::LABEL_TEXT:
1458 GetImplementation( button ).SetLabelText( value.Get< std::string >() );
1462 case Toolkit::Button::Property::LABEL:
1464 // Get a Property::Map from the property if possible.
1465 Property::Map setPropertyMap;
1466 if( value.Get( setPropertyMap ) )
1468 GetImplementation( button ).ModifyLabel( setPropertyMap );
1476 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
1478 Property::Value value;
1480 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1484 switch ( propertyIndex )
1486 case Toolkit::Button::Property::DISABLED:
1488 value = GetImplementation( button ).mDisabled;
1492 case Toolkit::Button::Property::AUTO_REPEATING:
1494 value = GetImplementation( button ).mAutoRepeating;
1498 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1500 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
1504 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1506 value = GetImplementation( button ).mNextAutoRepeatingDelay;
1510 case Toolkit::Button::Property::TOGGLABLE:
1512 value = GetImplementation( button ).mTogglableButton;
1516 case Toolkit::Button::Property::SELECTED:
1518 value = GetImplementation( button ).mSelected;
1522 case Toolkit::Button::Property::UNSELECTED_STATE_IMAGE:
1524 value = GetImplementation( button ).GetUnselectedImageFilename();
1528 case Toolkit::Button::Property::SELECTED_STATE_IMAGE:
1530 value = GetImplementation( button ).GetSelectedImageFilename();
1534 case Toolkit::Button::Property::DISABLED_STATE_IMAGE:
1536 value = GetImplementation( button ).GetDisabledImageFilename();
1540 case Toolkit::Button::Property::UNSELECTED_COLOR:
1542 value = GetImplementation( button ).GetUnselectedColor();
1546 case Toolkit::Button::Property::SELECTED_COLOR:
1548 value = GetImplementation( button ).GetSelectedColor();
1552 case Toolkit::Button::Property::LABEL_TEXT:
1554 value = GetImplementation( button ).GetLabelText();
1558 case Toolkit::Button::Property::LABEL:
1560 Property::Map emptyMap;
1572 void Button::SetLabel( Actor label )
1574 if( mLabel != label )
1576 if( mLabel && mLabel.GetParent() )
1578 mLabel.GetParent().Remove( mLabel );
1582 mLabel.SetPosition( 0.0f, 0.0f );
1584 // label should be the top of the button
1585 Self().Add( mLabel );
1594 void Button::SetButtonImage( Actor image )
1598 StopTransitionAnimation();
1600 SetupContent( mUnselectedContent, image );
1602 OnUnselectedImageSet();
1607 void Button::SetSelectedImage( Actor image )
1611 StopTransitionAnimation();
1613 SetupContent( mSelectedContent, image );
1615 OnSelectedImageSet();
1620 void Button::SetBackgroundImage( Actor image )
1624 StopTransitionAnimation();
1626 SetupContent( mBackgroundContent, image );
1628 OnBackgroundImageSet();
1633 void Button::SetSelectedBackgroundImage( Actor image )
1637 StopTransitionAnimation();
1639 SetupContent( mSelectedBackgroundContent, image );
1641 OnSelectedBackgroundImageSet();
1646 void Button::SetDisabledImage( Actor image )
1650 StopTransitionAnimation();
1652 SetupContent( mDisabledContent, image );
1654 OnDisabledImageSet();
1659 void Button::SetDisabledSelectedImage( Actor image )
1663 StopTransitionAnimation();
1665 SetupContent( mDisabledSelectedContent, image );
1667 OnDisabledSelectedImageSet();
1672 void Button::SetDisabledBackgroundImage( Actor image )
1676 StopTransitionAnimation();
1678 SetupContent( mDisabledBackgroundContent, image );
1680 OnDisabledBackgroundImageSet();
1685 Actor Button::GetButtonImage() const
1687 return mUnselectedContent;
1690 Actor Button::GetSelectedImage() const
1692 return mSelectedContent;
1696 } // namespace Internal
1698 } // namespace Toolkit