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/devel-api/object/type-registry-helper.h>
26 #include <dali/public-api/actors/image-actor.h>
27 #include <dali/devel-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) mUnselectedContent (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 * | mUnselectedContent / 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( Toolkit, Button, "disabled", BOOLEAN, DISABLED )
78 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "auto-repeating", BOOLEAN, AUTO_REPEATING )
79 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "initial-auto-repeating-delay", FLOAT, INITIAL_AUTO_REPEATING_DELAY )
80 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "next-auto-repeating-delay", FLOAT, NEXT_AUTO_REPEATING_DELAY )
81 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "togglable", BOOLEAN, TOGGLABLE )
82 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected", BOOLEAN, SELECTED )
83 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "normal-state-actor", MAP, NORMAL_STATE_ACTOR )
84 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected-state-actor", MAP, SELECTED_STATE_ACTOR )
85 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled-state-actor", MAP, DISABLED_STATE_ACTOR )
86 DALI_PROPERTY_REGISTRATION( Toolkit, Button, "label-actor", MAP, LABEL_ACTOR )
88 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "pressed", SIGNAL_PRESSED )
89 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "released", SIGNAL_RELEASED )
90 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "clicked", SIGNAL_CLICKED )
91 DALI_SIGNAL_REGISTRATION( Toolkit, Button, "state-changed", SIGNAL_STATE_CHANGED )
93 DALI_ACTION_REGISTRATION( Toolkit, 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 )
122 void Button::SetDisabled( bool disabled )
124 if( disabled == mDisabled )
129 StopTransitionAnimation();
131 mDisabled = disabled;
133 // Notifies the derived class the button has been disabled.
136 switch( mPaintState )
138 case UnselectedState:
141 //(3) mDisabledContent (Inserted)
142 //(4) mUnselectedContent
143 //(2) mDisabledBackgroundContent (Inserted)
144 //(1) mBackgroundContent
146 TransitionInBetween( mUnselectedContent, mLabel, mDisabledContent );
147 TransitionInAbove( mBackgroundContent, mDisabledBackgroundContent );
149 TransitionOut( mUnselectedContent );
150 TransitionOut( mSelectedContent );
151 TransitionOut( mBackgroundContent );
152 TransitionOut( mSelectedBackgroundContent );
153 TransitionOut( mDisabledSelectedContent );
155 mPaintState = DisabledUnselectedState;
161 //(5) mDisabledSelectedContent (Inserted)
162 //(4) mSelectedContent
163 //(3) mDisabledBackgroundContent (Inserted)
164 //(2) mSelectedBackgroundContent
165 //(1) mBackgroundContent
167 TransitionInBetween( mSelectedContent, mLabel, mDisabledSelectedContent );
168 TransitionInAbove( mSelectedBackgroundContent, mDisabledBackgroundContent );
170 TransitionOut( mUnselectedContent );
171 TransitionOut( mSelectedContent );
172 TransitionOut( mBackgroundContent );
173 TransitionOut( mSelectedBackgroundContent );
174 TransitionOut( mDisabledContent );
176 mPaintState = DisabledSelectedState;
179 case DisabledUnselectedState:
182 //(3) mUnselectedContent (Inserted)
183 //(4) mDisabledContent
184 //(2) mBackgroundContent (Inserted)
185 //(1) mDisabledBackgroundContent
187 TransitionInBetween( mDisabledContent, mLabel, mUnselectedContent );
188 TransitionInAbove( mDisabledBackgroundContent, mBackgroundContent );
190 TransitionOut( mSelectedContent );
191 TransitionOut( mSelectedBackgroundContent );
192 TransitionOut( mDisabledContent );
193 TransitionOut( mDisabledSelectedContent );
194 TransitionOut( mDisabledBackgroundContent );
196 mPaintState = UnselectedState;
199 case DisabledSelectedState:
202 //(4) mSelectedContent (Inserted)
203 //(5) mDisabledSelectedContent
204 //(3) mSelectedBackgroundContent (Inserted)
205 //(2) mBackgroundContent (Inserted)
206 //(1) mDisabledBackgroundContent
208 TransitionInBetween( mDisabledSelectedContent, mLabel, mSelectedContent );
209 TransitionInAbove( mDisabledBackgroundContent, mSelectedBackgroundContent );
210 TransitionInAbove( mDisabledBackgroundContent, mBackgroundContent );
212 TransitionOut( mUnselectedContent );
213 TransitionOut( mDisabledContent );
214 TransitionOut( mDisabledSelectedContent );
215 TransitionOut( mDisabledBackgroundContent );
217 mPaintState = SelectedState;
222 StartTransitionAnimation();
225 bool Button::IsDisabled() const
230 void Button::SetAutoRepeating( bool autoRepeating )
232 mAutoRepeating = autoRepeating;
234 // An autorepeating button can't be a togglable button.
237 mTogglableButton = false;
241 // Emit a signal is not wanted, only change the appearance.
242 SetSelected( false, false );
247 bool Button::IsAutoRepeating() const
249 return mAutoRepeating;
252 void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay )
254 DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f );
255 mInitialAutoRepeatingDelay = initialAutoRepeatingDelay;
258 float Button::GetInitialAutoRepeatingDelay() const
260 return mInitialAutoRepeatingDelay;
263 void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay )
265 DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f );
266 mNextAutoRepeatingDelay = nextAutoRepeatingDelay;
269 float Button::GetNextAutoRepeatingDelay() const
271 return mNextAutoRepeatingDelay;
274 void Button::SetTogglableButton( bool togglable )
276 mTogglableButton = togglable;
278 // A togglable button can't be an autorepeating button.
281 mAutoRepeating = false;
285 bool Button::IsTogglableButton() const
287 return mTogglableButton;
290 void Button::SetSelected( bool selected )
292 if( !mDisabled && mTogglableButton && ( selected != mSelected ) )
294 SetSelected( selected, true );
298 void Button::SetSelected( bool selected, bool emitSignal )
300 StopTransitionAnimation();
302 mSelected = selected;
304 // Notifies the derived class the button has been selected.
307 switch( mPaintState )
309 case UnselectedState:
312 //(3) mSelectedContent (Inserted)
313 //(4) mUnselectedContent
314 //(2) mSelectedBackgroundContent (Inserted)
315 //(1) mBackgroundContent
317 TransitionInBetween( mUnselectedContent, mLabel, mSelectedContent );
318 TransitionInAbove( mBackgroundContent, mSelectedBackgroundContent );
319 TransitionInAtIndex( 0, mBackgroundContent );
321 TransitionOut( mUnselectedContent );
322 TransitionOut( mDisabledContent );
323 TransitionOut( mDisabledSelectedContent );
324 TransitionOut( mDisabledBackgroundContent );
326 mPaintState = SelectedState;
332 //(3) mUnselectedContent (Inserted)
333 //(2) mSelectedContent
334 //(1) mBackgroundContent
336 TransitionInBetween( mSelectedContent, mLabel, mUnselectedContent );
337 TransitionInAtIndex( 0, mBackgroundContent );
339 TransitionOut( mSelectedContent );
340 TransitionOut( mSelectedBackgroundContent );
341 TransitionOut( mDisabledContent );
342 TransitionOut( mDisabledSelectedContent );
343 TransitionOut( mDisabledBackgroundContent );
345 mPaintState = UnselectedState;
348 case DisabledUnselectedState:
349 case DisabledSelectedState:
351 DALI_ASSERT_DEBUG( 0 && "Shouldn't be able to change paint state if the button is disabled." );
356 StartTransitionAnimation();
360 Toolkit::Button handle( GetOwner() );
363 mStateChangedSignal.Emit( handle );
369 bool Button::IsSelected() const
371 return mTogglableButton && mSelected;
374 void Button::SetAnimationTime( float animationTime )
376 mAnimationTime = animationTime;
379 float Button::GetAnimationTime() const
381 return mAnimationTime;
384 void Button::SetLabel( const std::string& label )
386 Toolkit::TextLabel textLabel = Toolkit::TextLabel::New( label );
387 SetLabel( textLabel );
390 void Button::SetLabel( Actor label )
392 if( mLabel != label )
394 if( mLabel && mLabel.GetParent() )
396 mLabel.GetParent().Remove( mLabel );
400 mLabel.SetPosition( 0.f, 0.f );
402 // label should be the top of the button
403 Self().Add( mLabel );
411 Actor Button::GetLabel() const
416 Actor& Button::GetLabel()
421 void Button::SetButtonImage( Actor image )
423 StopTransitionAnimation();
425 if( mUnselectedContent && mUnselectedContent.GetParent() )
427 Self().Remove( mUnselectedContent );
430 mUnselectedContent = image;
431 if( mUnselectedContent )
433 mUnselectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
434 mUnselectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
435 mUnselectedContent.SetPosition( 0.f, 0.f );
443 Actor Button::GetButtonImage() const
445 return mUnselectedContent;
448 Actor& Button::GetButtonImage()
450 return mUnselectedContent;
453 void Button::SetSelectedImage( Actor image )
455 StopTransitionAnimation();
457 if( mSelectedContent && mSelectedContent.GetParent() )
459 Self().Remove( mSelectedContent );
462 mSelectedContent = image;
463 if( mSelectedContent )
465 mSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
466 mSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
467 mSelectedContent.SetPosition( 0.f, 0.f );
470 OnSelectedImageSet();
475 Actor Button::GetSelectedImage() const
477 return mSelectedContent;
480 Actor& Button::GetSelectedImage()
482 return mSelectedContent;
485 void Button::SetBackgroundImage( Actor image )
487 StopTransitionAnimation();
489 if( mBackgroundContent && mBackgroundContent.GetParent() )
491 Self().Remove( mBackgroundContent );
494 mBackgroundContent = image;
495 if( mBackgroundContent )
497 mBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
498 mBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
499 mBackgroundContent.SetPosition( 0.f, 0.f );
502 OnBackgroundImageSet();
507 Actor Button::GetBackgroundImage() const
509 return mBackgroundContent;
512 Actor& Button::GetBackgroundImage()
514 return mBackgroundContent;
517 void Button::SetSelectedBackgroundImage( Actor image )
519 StopTransitionAnimation();
521 if( mSelectedBackgroundContent && mSelectedBackgroundContent.GetParent() )
523 Self().Remove( mSelectedBackgroundContent );
526 mSelectedBackgroundContent = image;
527 if( mSelectedBackgroundContent )
529 mSelectedBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
530 mSelectedBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
531 mSelectedBackgroundContent.SetPosition( 0.f, 0.f );
534 OnSelectedBackgroundImageSet();
539 Actor Button::GetSelectedBackgroundImage() const
541 return mSelectedBackgroundContent;
544 Actor& Button::GetSelectedBackgroundImage()
546 return mSelectedBackgroundContent;
549 void Button::SetDisabledImage( Actor image )
551 StopTransitionAnimation();
553 if( mDisabledContent && mDisabledContent.GetParent() )
555 Self().Remove( mDisabledContent );
558 mDisabledContent = image;
559 if( mDisabledContent )
561 mDisabledContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
562 mDisabledContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
563 mDisabledContent.SetPosition( 0.f, 0.f );
567 OnDisabledImageSet();
570 Actor Button::GetDisabledImage() const
572 return mDisabledContent;
575 Actor& Button::GetDisabledImage()
577 return mDisabledContent;
580 void Button::SetDisabledSelectedImage( Actor image )
582 StopTransitionAnimation();
584 if( mDisabledSelectedContent && mDisabledSelectedContent.GetParent() )
586 Self().Remove( mDisabledSelectedContent );
589 mDisabledSelectedContent = image;
590 if( mDisabledSelectedContent )
592 mDisabledSelectedContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
593 mDisabledSelectedContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
594 mDisabledSelectedContent.SetPosition( 0.f, 0.f );
598 OnDisabledSelectedImageSet();
601 Actor Button::GetDisabledSelectedImage() const
603 return mDisabledSelectedContent;
606 Actor& Button::GetDisabledSelectedImage()
608 return mDisabledSelectedContent;
611 void Button::SetDisabledBackgroundImage( Actor image )
613 StopTransitionAnimation();
615 if( mDisabledBackgroundContent && mDisabledBackgroundContent.GetParent() )
617 Self().Remove( mDisabledBackgroundContent );
620 mDisabledBackgroundContent = image;
621 if( mDisabledBackgroundContent )
623 mDisabledBackgroundContent.SetAnchorPoint( AnchorPoint::TOP_LEFT );
624 mDisabledBackgroundContent.SetParentOrigin( ParentOrigin::TOP_LEFT );
625 mDisabledBackgroundContent.SetPosition( 0.f, 0.f );
628 OnDisabledBackgroundImageSet();
631 Actor Button::GetDisabledBackgroundImage() const
633 return mDisabledBackgroundContent;
636 Actor& Button::GetDisabledBackgroundImage()
638 return mDisabledBackgroundContent;
641 bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes )
645 Dali::BaseHandle handle( object );
647 Toolkit::Button button = Toolkit::Button::DownCast( handle );
649 DALI_ASSERT_ALWAYS( button );
651 if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) )
653 ret = GetImplementation( button ).DoClickAction( attributes );
659 bool Button::DoClickAction( const Property::Map& attributes )
661 // Prevents the button signals from doing a recursive loop by sending an action
662 // and re-emitting the signals.
663 if( !mClickActionPerforming )
665 mClickActionPerforming = true;
669 mClickActionPerforming = false;
677 void Button::OnButtonStageDisconnection()
679 if( ButtonDown == mState )
681 if( !mTogglableButton )
687 mAutoRepeatingTimer.Reset();
693 void Button::OnButtonDown()
695 if( !mTogglableButton )
701 SetUpTimer( mInitialAutoRepeatingDelay );
705 // The pressed signal should be emitted regardless of toggle mode.
706 Toolkit::Button handle( GetOwner() );
707 mPressedSignal.Emit( handle );
710 void Button::OnButtonUp()
712 if( ButtonDown == mState )
714 if( mTogglableButton )
716 SetSelected( !mSelected );
724 mAutoRepeatingTimer.Reset();
728 // The clicked and released signals should be emitted regardless of toggle mode.
729 Toolkit::Button handle( GetOwner() );
730 mReleasedSignal.Emit( handle );
731 mClickedSignal.Emit( handle );
735 void Button::OnTouchPointLeave()
737 if( ButtonDown == mState )
739 if( !mTogglableButton )
745 mAutoRepeatingTimer.Reset();
749 // The released signal should be emitted regardless of toggle mode.
750 Toolkit::Button handle( GetOwner() );
751 mReleasedSignal.Emit( handle );
755 void Button::OnTouchPointInterrupted()
760 Toolkit::Button::ButtonSignalType& Button::PressedSignal()
762 return mPressedSignal;
765 Toolkit::Button::ButtonSignalType& Button::ReleasedSignal()
767 return mReleasedSignal;
770 Toolkit::Button::ButtonSignalType& Button::ClickedSignal()
772 return mClickedSignal;
775 Toolkit::Button::ButtonSignalType& Button::StateChangedSignal()
777 return mStateChangedSignal;
780 bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor )
782 Dali::BaseHandle handle( object );
784 bool connected( true );
785 Toolkit::Button button = Toolkit::Button::DownCast( handle );
787 if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) )
789 button.PressedSignal().Connect( tracker, functor );
791 else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) )
793 button.ReleasedSignal().Connect( tracker, functor );
795 else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) )
797 button.ClickedSignal().Connect( tracker, functor );
799 else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) )
801 button.StateChangedSignal().Connect( tracker, functor );
805 // signalName does not match any signal
812 bool Button::OnTouchEvent(const TouchEvent& event)
814 // Only events are processed when the button is not disabled and the touch event has only
816 if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) )
818 switch( event.GetPoint(0).state )
820 case TouchPoint::Down:
822 OnButtonDown(); // Notification for derived classes.
824 // Sets the button state to ButtonDown.
830 OnButtonUp(); // Notification for derived classes.
832 // Sets the button state to ButtonUp.
836 case TouchPoint::Interrupted:
838 OnTouchPointInterrupted(); // Notification for derived classes.
840 // Sets the button state to the default (ButtonUp).
844 case TouchPoint::Leave:
846 OnTouchPointLeave(); // Notification for derived classes.
848 // Sets the button state to the default (ButtonUp).
852 case TouchPoint::Motion:
853 case TouchPoint::Stationary: // FALLTHROUGH
860 DALI_ASSERT_ALWAYS( !"Point status unhandled." );
865 else if( 1 < event.GetPointCount() )
867 OnTouchPointLeave(); // Notification for derived classes.
869 // Sets the button state to the default (ButtonUp).
876 void Button::OnInitialize()
880 mTapDetector = TapGestureDetector::New();
881 mTapDetector.Attach( self );
882 mTapDetector.DetectedSignal().Connect(this, &Button::OnTap);
884 OnButtonInitialize();
886 self.SetKeyboardFocusable( true );
889 bool Button::OnAccessibilityActivated()
891 return OnKeyboardEnter();
894 bool Button::OnKeyboardEnter()
896 // When the enter key is pressed, or button is activated, the click action is performed.
897 Property::Map attributes;
898 bool ret = DoClickAction( attributes );
903 void Button::OnControlStageDisconnection()
905 OnButtonStageDisconnection(); // Notification for derived classes.
909 void Button::OnTap(Actor actor, const TapGesture& tap)
914 void Button::SetUpTimer( float delay )
916 mAutoRepeatingTimer = Dali::Timer::New( static_cast<unsigned int>( 1000.f * delay ) );
917 mAutoRepeatingTimer.TickSignal().Connect( this, &Button::AutoRepeatingSlot );
918 mAutoRepeatingTimer.Start();
921 bool Button::AutoRepeatingSlot()
923 bool consumed = false;
926 // Restart the autorepeat timer.
927 SetUpTimer( mNextAutoRepeatingDelay );
931 Toolkit::Button handle( GetOwner() );
934 consumed = mReleasedSignal.Emit( handle );
935 consumed |= mClickedSignal.Emit( handle );
936 consumed |= mPressedSignal.Emit( handle );
942 void Button::Pressed()
944 if( mPaintState == UnselectedState )
946 StopTransitionAnimation();
948 // Notifies the derived class the button has been pressed.
952 //(4) mSelectedContent (Inserted)
953 //(3) mUnselectedContent
954 //(2) mSelectedBackgroundContent (Inserted)
955 //(1) mBackgroundContent
957 TransitionInBetween( mUnselectedContent, mLabel, mSelectedContent );
958 TransitionInAbove( mBackgroundContent, mSelectedBackgroundContent );
959 TransitionInAtIndex( 0, mBackgroundContent );
961 TransitionOut( mUnselectedContent );
962 TransitionOut( mDisabledContent );
963 TransitionOut( mDisabledSelectedContent );
964 TransitionOut( mDisabledBackgroundContent );
966 mPaintState = SelectedState;
968 StartTransitionAnimation();
972 void Button::Released()
974 if( mPaintState == SelectedState )
976 StopTransitionAnimation();
978 // Notifies the derived class the button has been released.
982 //(3) mUnselectedContent (Inserted)
983 //(2) mSelectedContent
984 //(1) mBackgroundContent
986 TransitionInBetween( mSelectedContent, mLabel, mUnselectedContent );
987 TransitionInAtIndex( 0, mBackgroundContent );
989 TransitionOut( mSelectedContent );
990 TransitionOut( mSelectedBackgroundContent );
991 TransitionOut( mDisabledContent );
992 TransitionOut( mDisabledSelectedContent );
993 TransitionOut( mDisabledBackgroundContent );
995 mPaintState = UnselectedState;
997 StartTransitionAnimation();
1001 Button::ButtonState Button::GetState()
1006 Button::PaintState Button::GetPaintState()
1011 bool Button::InsertButtonImage( unsigned int index, Actor& actor )
1015 Self().Insert( index, actor );
1016 PrepareForTranstionOut( actor );
1023 void Button::RemoveButtonImage( Actor& actor )
1027 if( actor.GetParent() )
1029 Self().Remove( actor );
1031 PrepareForTranstionIn( actor );
1035 unsigned int Button::FindChildIndex( Actor& actor )
1037 Actor self = Self();
1038 unsigned int childrenNum = self.GetChildCount();
1040 for( unsigned int i = 0; i < childrenNum; i++ )
1042 Actor child = self.GetChildAt( i );
1043 if( child == actor )
1052 void Button::TransitionInBetween( Actor childLower, Actor childUpper, Actor actor )
1054 unsigned int index = childLower ? FindChildIndex( childLower ) + 1 : FindChildIndex( childUpper );
1055 TransitionInAtIndex( index, actor );
1058 void Button::TransitionInAbove( Actor child, Actor actor )
1060 unsigned int index = child ? FindChildIndex( child ) + 1 : 0;
1061 TransitionInAtIndex( index, actor );
1064 void Button::TransitionInAtIndex( unsigned int index, Actor actor )
1068 if( !actor.GetParent() )
1070 Self().Insert( index, actor );
1073 OnTransitionIn( actor );
1077 void Button::TransitionOut( Actor actor )
1079 OnTransitionOut( actor );
1082 void Button::ResetImageLayers()
1084 //ensure that all layers are in the correct order and state according to the paint state
1087 switch( mPaintState )
1089 case UnselectedState:
1092 //(2) mUnselectedContent
1093 //(1) mBackgroundContent
1095 RemoveButtonImage( mSelectedContent );
1096 RemoveButtonImage( mSelectedBackgroundContent );
1097 RemoveButtonImage( mDisabledContent );
1098 RemoveButtonImage( mDisabledSelectedContent );
1099 RemoveButtonImage( mDisabledBackgroundContent );
1101 if( InsertButtonImage( index, mBackgroundContent ) )
1105 if( InsertButtonImage( index, mUnselectedContent ) )
1114 //(3) mSelectedContent
1115 //(2) mSelectedBackgroundContent
1116 //(1) mBackgroundContent
1118 RemoveButtonImage( mUnselectedContent );
1119 RemoveButtonImage( mDisabledContent );
1120 RemoveButtonImage( mDisabledSelectedContent );
1121 RemoveButtonImage( mDisabledBackgroundContent );
1123 if( InsertButtonImage( index, mBackgroundContent ) )
1127 if( InsertButtonImage( index, mSelectedBackgroundContent ) )
1131 if( InsertButtonImage( index, mSelectedContent ) )
1137 case DisabledUnselectedState:
1140 //(2) mDisabledContent
1141 //(1) mDisabledBackgroundContent
1143 RemoveButtonImage( mUnselectedContent );
1144 RemoveButtonImage( mBackgroundContent );
1145 RemoveButtonImage( mSelectedContent );
1146 RemoveButtonImage( mDisabledSelectedContent );
1147 RemoveButtonImage( mSelectedBackgroundContent );
1149 if( InsertButtonImage( index, mDisabledBackgroundContent ? mDisabledBackgroundContent : mBackgroundContent ) )
1153 if( InsertButtonImage( index, mDisabledContent ? mDisabledContent : mUnselectedContent ) )
1159 case DisabledSelectedState:
1162 // (2) mDisabledSelectedContent
1163 // (1) mDisabledBackgroundContent
1165 RemoveButtonImage( mUnselectedContent );
1166 RemoveButtonImage( mSelectedContent );
1167 RemoveButtonImage( mBackgroundContent );
1168 RemoveButtonImage( mSelectedBackgroundContent );
1169 RemoveButtonImage( mDisabledContent );
1171 if( mDisabledBackgroundContent )
1173 if( InsertButtonImage( index, mDisabledBackgroundContent) )
1180 if( InsertButtonImage( index, mBackgroundContent ) )
1184 if( InsertButtonImage( index, mSelectedBackgroundContent ) )
1190 if( InsertButtonImage( index, mDisabledSelectedContent ? mDisabledSelectedContent : mSelectedContent) )
1199 void Button::StartTransitionAnimation()
1201 if( mTransitionAnimation )
1203 mTransitionAnimation.Play();
1211 void Button::StopTransitionAnimation()
1213 if( mTransitionAnimation )
1215 mTransitionAnimation.Clear();
1216 mTransitionAnimation.Reset();
1220 Dali::Animation Button::GetTransitionAnimation()
1222 if( !mTransitionAnimation )
1224 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
1225 mTransitionAnimation.FinishedSignal().Connect( this, &Button::TransitionAnimationFinished );
1228 return mTransitionAnimation;
1231 void Button::TransitionAnimationFinished( Dali::Animation& source )
1233 StopTransitionAnimation();
1237 void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
1239 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1245 case Toolkit::Button::Property::DISABLED:
1247 GetImplementation( button ).SetDisabled( value.Get< bool >() );
1251 case Toolkit::Button::Property::AUTO_REPEATING:
1253 GetImplementation( button ).SetAutoRepeating( value.Get< bool >() );
1257 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1259 GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() );
1263 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1265 GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() );
1269 case Toolkit::Button::Property::TOGGLABLE:
1271 GetImplementation( button ).SetTogglableButton( value.Get< bool >() );
1275 case Toolkit::Button::Property::SELECTED:
1277 GetImplementation( button ).SetSelected( value.Get< bool >() );
1281 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1283 GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1287 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1289 GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1293 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1295 GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) );
1299 case Toolkit::Button::Property::LABEL_ACTOR:
1301 GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) );
1308 Property::Value Button::GetProperty( BaseObject* object, Property::Index propertyIndex )
1310 Property::Value value;
1312 Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) );
1316 switch ( propertyIndex )
1318 case Toolkit::Button::Property::DISABLED:
1320 value = GetImplementation( button ).mDisabled;
1324 case Toolkit::Button::Property::AUTO_REPEATING:
1326 value = GetImplementation( button ).mAutoRepeating;
1330 case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY:
1332 value = GetImplementation( button ).mInitialAutoRepeatingDelay;
1336 case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY:
1338 value = GetImplementation( button ).mNextAutoRepeatingDelay;
1342 case Toolkit::Button::Property::TOGGLABLE:
1344 value = GetImplementation( button ).mTogglableButton;
1348 case Toolkit::Button::Property::SELECTED:
1350 value = GetImplementation( button ).mSelected;
1354 case Toolkit::Button::Property::NORMAL_STATE_ACTOR:
1357 Scripting::CreatePropertyMap( GetImplementation( button ).mUnselectedContent, map );
1362 case Toolkit::Button::Property::SELECTED_STATE_ACTOR:
1365 Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedContent, map );
1370 case Toolkit::Button::Property::DISABLED_STATE_ACTOR:
1373 Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledContent, map );
1378 case Toolkit::Button::Property::LABEL_ACTOR:
1381 Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map );
1391 } // namespace Internal
1393 } // namespace Toolkit