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 DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
38 const float ANIMATION_TIME( 0.26f ); // EFL checkbox tick time
42 return Toolkit::CheckBoxButton::New();
45 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
49 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
51 // Create the implementation, temporarily owned on stack
52 IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
54 // Pass ownership to CustomActor
55 Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
57 // Second-phase init of the implementation
58 // This can only be done after the CustomActor connection has been made...
59 internalCheckBoxButton->Initialize();
61 return checkBoxButton;
64 CheckBoxButton::CheckBoxButton()
67 SetTogglableButton( true );
69 SetAnimationTime( ANIMATION_TIME );
72 CheckBoxButton::~CheckBoxButton()
74 if( mTransitionAnimation )
76 mTransitionAnimation.Clear();
80 void CheckBoxButton::OnButtonInitialize()
82 // Wrap around all children
83 Self().SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
86 void CheckBoxButton::OnLabelSet()
88 Actor& label = GetLabel();
92 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
93 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
95 if( IsDisabled() && GetDisabledBackgroundImage() )
97 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
99 else if ( GetBackgroundImage() )
101 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
105 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
110 bool CheckBoxButton::OnSelected()
112 Actor& selectedImage = GetSelectedImage();
114 PaintState paintState = GetPaintState();
118 case UnselectedState:
120 StartTransitionAnimation( selectedImage );
125 RemoveChild( selectedImage );
128 case UnselectedSelectedTransition:
130 StopTransitionAnimation( false );
131 RemoveChild( selectedImage );
140 if( mTransitionAnimation )
148 bool CheckBoxButton::OnDisabled()
150 Actor& backgroundImage = GetBackgroundImage();
151 Actor& selectedImage = GetSelectedImage();
152 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
153 Actor& disabledSelectedImage = GetDisabledSelectedImage();
155 PaintState paintState = GetPaintState();
159 case UnselectedState:
161 RemoveChild( backgroundImage );
166 RemoveChild( backgroundImage );
167 RemoveChild( selectedImage );
170 case DisabledUnselectedState:
172 RemoveChild( disabledBackgroundImage );
175 case DisabledSelectedState:
177 RemoveChild( disabledBackgroundImage );
178 RemoveChild( disabledSelectedImage );
181 case UnselectedSelectedTransition:
183 StopTransitionAnimation();
185 RemoveChild( backgroundImage );
186 RemoveChild( selectedImage );
195 Actor& label = GetLabel();
199 if( IsDisabled() && disabledBackgroundImage)
201 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
203 else if( backgroundImage )
205 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
209 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
213 if( mTransitionAnimation )
221 void CheckBoxButton::StopAllAnimations()
223 StopTransitionAnimation();
226 void CheckBoxButton::StartTransitionAnimation( Actor& actor )
232 ImageActor imageActor = ImageActor::DownCast( actor );
233 mTickUVEffect = ImageRegionEffect::New();
234 imageActor.SetShaderEffect( mTickUVEffect );
237 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
239 mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
241 if( !mTransitionAnimation )
243 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
247 mTransitionAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
250 mTransitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
252 mTransitionAnimation.FinishedSignal().Connect( this, &CheckBoxButton::TransitionAnimationFinished );
253 mTransitionAnimation.Play();
257 void CheckBoxButton::StopTransitionAnimation( bool remove )
259 if( mTransitionAnimation )
261 mTransitionAnimation.Clear();
262 mTransitionAnimation.Reset();
267 UpdatePaintTransitionState();
271 void CheckBoxButton::TransitionAnimationFinished( Dali::Animation& source )
273 StopTransitionAnimation();
276 } // namespace Internal
278 } // namespace Toolkit