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>
26 #include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
40 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
41 const float ANIMATION_TIME( 0.26f ); // EFL checkbox tick time
45 return Toolkit::CheckBoxButton::New();
48 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
52 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
54 // Create the implementation, temporarily owned on stack
55 IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
57 // Pass ownership to CustomActor
58 Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
60 // Second-phase init of the implementation
61 // This can only be done after the CustomActor connection has been made...
62 internalCheckBoxButton->Initialize();
64 return checkBoxButton;
67 CheckBoxButton::CheckBoxButton()
70 SetTogglableButton( true );
72 SetAnimationTime( ANIMATION_TIME );
75 CheckBoxButton::~CheckBoxButton()
77 if( mTransitionAnimation )
79 mTransitionAnimation.Clear();
83 void CheckBoxButton::OnButtonInitialize()
85 // Wrap around all children
86 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
89 void CheckBoxButton::OnLabelSet()
91 Actor& label = GetLabel();
95 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
96 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
98 if( IsDisabled() && GetDisabledBackgroundImage() )
100 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
102 else if ( GetBackgroundImage() )
104 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
108 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
113 bool CheckBoxButton::OnSelected()
115 Actor& selectedImage = GetSelectedImage();
117 PaintState paintState = GetPaintState();
121 case UnselectedState:
123 StartTransitionAnimation( selectedImage );
128 RemoveChild( selectedImage );
131 case UnselectedSelectedTransition:
133 StopTransitionAnimation( false );
134 RemoveChild( selectedImage );
143 if( mTransitionAnimation )
151 bool CheckBoxButton::OnDisabled()
153 Actor& backgroundImage = GetBackgroundImage();
154 Actor& selectedImage = GetSelectedImage();
155 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
156 Actor& disabledSelectedImage = GetDisabledSelectedImage();
158 PaintState paintState = GetPaintState();
162 case UnselectedState:
164 RemoveChild( backgroundImage );
169 RemoveChild( backgroundImage );
170 RemoveChild( selectedImage );
173 case DisabledUnselectedState:
175 RemoveChild( disabledBackgroundImage );
178 case DisabledSelectedState:
180 RemoveChild( disabledBackgroundImage );
181 RemoveChild( disabledSelectedImage );
184 case UnselectedSelectedTransition:
186 StopTransitionAnimation();
188 RemoveChild( backgroundImage );
189 RemoveChild( selectedImage );
198 Actor& label = GetLabel();
202 if( IsDisabled() && disabledBackgroundImage)
204 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
206 else if( backgroundImage )
208 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
212 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
216 if( mTransitionAnimation )
224 void CheckBoxButton::StopAllAnimations()
226 StopTransitionAnimation();
229 void CheckBoxButton::StartTransitionAnimation( Actor& actor )
235 ImageActor imageActor = ImageActor::DownCast( actor );
236 mTickUVEffect = CreateImageRegionEffect();
237 imageActor.SetShaderEffect( mTickUVEffect );
240 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
242 mTickUVEffect.SetUniform("uBottomRight", Vector2( 0.0f, 1.0f ) );
244 if( !mTransitionAnimation )
246 mTransitionAnimation = Dali::Animation::New( GetAnimationTime() );
250 mTransitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2( 1.0f, 1.0f ) );
253 mTransitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
255 mTransitionAnimation.FinishedSignal().Connect( this, &CheckBoxButton::TransitionAnimationFinished );
256 mTransitionAnimation.Play();
260 void CheckBoxButton::StopTransitionAnimation( bool remove )
262 if( mTransitionAnimation )
264 mTransitionAnimation.Clear();
265 mTransitionAnimation.Reset();
270 UpdatePaintTransitionState();
274 void CheckBoxButton::TransitionAnimationFinished( Dali::Animation& source )
276 StopTransitionAnimation();
279 } // namespace Internal
281 } // namespace Toolkit