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 "check-box-button-impl.h"
22 #include <dali/public-api/actors/image-actor.h>
23 #include <dali/public-api/object/type-registry.h>
37 const float FOREGROUND_DEPTH( 0.5f );
38 const float BACKGROUND_DEPTH( 0.25f );
40 const float ANIMATION_TIME( 0.26f ); // EFL checkbox tick time
42 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
46 return Toolkit::CheckBoxButton::New();
49 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
53 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
55 // Create the implementation, temporarily owned on stack
56 IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
58 // Pass ownership to CustomActor
59 Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
61 // Second-phase init of the implementation
62 // This can only be done after the CustomActor connection has been made...
63 internalCheckBoxButton->Initialize();
65 return checkBoxButton;
68 CheckBoxButton::CheckBoxButton()
70 mPaintState( UnselectedState )
72 SetTogglableButton( true );
74 SetAnimationTime( ANIMATION_TIME );
77 CheckBoxButton::~CheckBoxButton()
79 if( mCheckInAnimation )
81 mCheckInAnimation.Clear();
85 void CheckBoxButton::OnButtonInitialize()
87 // Wrap around all children
88 Self().SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
91 void CheckBoxButton::SetSelectedImage( Actor image )
93 Actor& selectedImage = GetSelectedImage();
99 if( selectedImage && selectedImage.GetParent() )
101 Self().Remove( selectedImage );
104 selectedImage = image;
105 Self().Add( selectedImage );
108 case UnselectedSelectedTransition:
110 StopCheckInAnimation();
111 Self().Remove( selectedImage );
113 selectedImage = image;
114 Self().Add( selectedImage );
116 mPaintState = SelectedState;
121 selectedImage = image;
126 selectedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
127 selectedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
128 selectedImage.SetZ( FOREGROUND_DEPTH );
131 void CheckBoxButton::SetBackgroundImage( Actor image )
133 Actor& backgroundImage = GetBackgroundImage();
135 switch( mPaintState )
137 case UnselectedState: // FALLTHROUGH
139 case UnselectedSelectedTransition:
141 if( backgroundImage && backgroundImage.GetParent() )
143 Self().Remove( backgroundImage );
145 Actor& label = GetLabel();
149 backgroundImage.Remove( label );
154 backgroundImage = image;
155 Self().Add( backgroundImage );
160 backgroundImage = image;
165 backgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
166 backgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
167 backgroundImage.SetZ( BACKGROUND_DEPTH );
170 void CheckBoxButton::SetDisabledSelectedImage( Actor image )
172 Actor& disabledSelectedImage = GetDisabledSelectedImage();
174 switch( mPaintState )
176 case DisabledSelectedState:
178 if( disabledSelectedImage && disabledSelectedImage.GetParent() )
180 Self().Remove( disabledSelectedImage );
183 disabledSelectedImage = image;
184 Self().Add( disabledSelectedImage );
189 disabledSelectedImage = image;
194 disabledSelectedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
195 disabledSelectedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
196 disabledSelectedImage.SetZ( FOREGROUND_DEPTH );
199 void CheckBoxButton::SetDisabledBackgroundImage( Actor image )
201 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
203 switch( mPaintState )
205 case DisabledSelectedState:
206 case DisabledUnselectedState:
208 if( disabledBackgroundImage && disabledBackgroundImage.GetParent() )
210 Self().Remove( disabledBackgroundImage );
212 Actor& label = GetLabel();
216 disabledBackgroundImage.Remove( label );
221 disabledBackgroundImage = image;
222 Self().Add( disabledBackgroundImage );
227 disabledBackgroundImage = image;
232 disabledBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
233 disabledBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
234 disabledBackgroundImage.SetZ( BACKGROUND_DEPTH );
237 void CheckBoxButton::OnLabelSet()
239 Actor& label = GetLabel();
243 label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
244 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
245 label.TranslateBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
247 if( IsDisabled() && GetDisabledBackgroundImage() )
249 GetDisabledBackgroundImage().Add( label );
251 else if ( GetBackgroundImage() )
253 GetBackgroundImage().Add( label );
262 void CheckBoxButton::OnSelected( bool selected )
264 Actor& selectedImage = GetSelectedImage();
266 switch( mPaintState )
268 case UnselectedState:
270 AddChild( selectedImage );
271 StartCheckInAnimation( selectedImage ); // Animate in the check actor
273 mPaintState = UnselectedSelectedTransition;
278 RemoveChild( selectedImage );
280 mPaintState = UnselectedState;
283 case UnselectedSelectedTransition:
285 StopCheckInAnimation();
286 RemoveChild( selectedImage );
288 mPaintState = UnselectedState;
296 void CheckBoxButton::OnDisabled( bool disabled )
298 Actor& backgroundImage = GetBackgroundImage();
299 Actor& selectedImage = GetSelectedImage();
300 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
301 Actor& disabledSelectedImage = GetDisabledSelectedImage();
303 switch( mPaintState )
305 case UnselectedState:
309 RemoveChild( backgroundImage );
310 AddChild( disabledBackgroundImage );
311 mPaintState = DisabledUnselectedState;
319 RemoveChild( backgroundImage );
320 RemoveChild( selectedImage );
321 AddChild( disabledBackgroundImage );
322 AddChild( disabledSelectedImage );
324 mPaintState = DisabledSelectedState;
328 case DisabledUnselectedState:
332 RemoveChild( disabledBackgroundImage );
333 AddChild( backgroundImage );
335 mPaintState = UnselectedState;
339 case DisabledSelectedState:
343 RemoveChild( disabledBackgroundImage );
344 RemoveChild( disabledSelectedImage );
345 AddChild( backgroundImage );
346 AddChild( selectedImage );
348 mPaintState = SelectedState;
352 case UnselectedSelectedTransition:
356 StopCheckInAnimation();
358 RemoveChild( backgroundImage );
359 RemoveChild( selectedImage );
360 AddChild( disabledBackgroundImage );
361 AddChild( disabledSelectedImage );
363 mPaintState = DisabledSelectedState;
371 Actor& label = GetLabel();
375 if( label.GetParent() )
377 label.GetParent().Remove( label );
380 if( disabled && disabledBackgroundImage)
382 disabledBackgroundImage.Add( label );
384 else if( backgroundImage )
386 backgroundImage.Add( label );
391 void CheckBoxButton::AddChild( Actor& actor )
399 void CheckBoxButton::RemoveChild( Actor& actor )
403 Self().Remove( actor );
407 void CheckBoxButton::StartCheckInAnimation( Actor& actor )
413 ImageActor imageActor = ImageActor::DownCast( actor );
414 mTickUVEffect = ImageRegionEffect::New();
415 imageActor.SetShaderEffect( mTickUVEffect );
418 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
420 mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
422 if( !mCheckInAnimation )
424 mCheckInAnimation = Dali::Animation::New( GetAnimationTime() );
428 mCheckInAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
431 mCheckInAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
433 mCheckInAnimation.FinishedSignal().Connect( this, &CheckBoxButton::CheckInAnimationFinished );
434 mCheckInAnimation.Play();
438 void CheckBoxButton::StopCheckInAnimation()
440 if( mCheckInAnimation )
442 mCheckInAnimation.Clear();
443 mCheckInAnimation.Reset();
447 void CheckBoxButton::CheckInAnimationFinished( Dali::Animation& source )
449 switch( mPaintState )
451 case UnselectedSelectedTransition:
453 mPaintState = SelectedState;
462 StopCheckInAnimation();
465 } // namespace Internal
467 } // namespace Toolkit