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>
24 #include <dali/public-api/images/resource-image.h>
27 #include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
41 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
42 const float ANIMATION_TIME( 0.26f ); // EFL checkbox tick time
46 return Toolkit::CheckBoxButton::New();
49 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
51 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected.png";
52 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected.png";
53 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected-disabled.png";
54 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected-diabled.png";
57 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
59 // Create the implementation, temporarily owned on stack
60 IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
62 // Pass ownership to CustomActor
63 Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
65 // Second-phase init of the implementation
66 // This can only be done after the CustomActor connection has been made...
67 internalCheckBoxButton->Initialize();
69 return checkBoxButton;
72 CheckBoxButton::CheckBoxButton()
75 SetTogglableButton( true );
77 SetAnimationTime( ANIMATION_TIME );
80 CheckBoxButton::~CheckBoxButton()
84 void CheckBoxButton::OnButtonInitialize()
86 // Wrap around all children
87 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
89 Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
90 Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
91 Image disabledImage = Dali::ResourceImage::New( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
92 Image disabledSelectedImage = Dali::ResourceImage::New( DISABLED_SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
94 SetButtonImage( ImageActor::New( buttonImage ) );
95 SetSelectedImage( ImageActor::New( selectedImage ) );
96 SetDisabledImage( ImageActor::New( disabledImage ) );
97 SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
100 void CheckBoxButton::OnLabelSet()
102 Actor& label = GetLabel();
106 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
107 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
109 if( IsDisabled() && GetDisabledBackgroundImage() )
111 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
113 else if ( GetBackgroundImage() )
115 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
117 else if( IsSelected() && GetSelectedImage())
119 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
121 else if( GetButtonImage() )
123 label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
127 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
132 void CheckBoxButton::OnDisabled()
134 Actor& backgroundImage = GetBackgroundImage();
135 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
137 Actor& label = GetLabel();
140 if( IsDisabled() && disabledBackgroundImage )
142 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
144 else if( backgroundImage )
146 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
148 else if( IsSelected() && GetSelectedImage())
150 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
152 else if( GetButtonImage() )
154 label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
158 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
163 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
165 Actor& selectedImage = GetSelectedImage();
166 if( actor == selectedImage )
168 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
172 mTickUVEffect = CreateImageRegionEffect();
174 mTickUVEffect.SetUniform("uBottomRight", Vector2( 0.0f, 1.0f ) );
176 ImageActor imageActor = ImageActor::DownCast( actor );
179 imageActor.SetShaderEffect( mTickUVEffect );
184 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
186 Actor& selectedImage = GetSelectedImage();
187 if( actor == selectedImage )
189 actor.SetScale( Vector3::ONE );
193 mTickUVEffect = CreateImageRegionEffect();
195 mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
197 ImageActor imageActor = ImageActor::DownCast( actor );
200 imageActor.SetShaderEffect( mTickUVEffect );
205 void CheckBoxButton::OnTransitionIn( Actor actor )
207 Actor& selectedImage = GetSelectedImage();
208 if( actor && actor == selectedImage )
210 if( GetPaintState() == UnselectedState )
212 Dali::Animation transitionAnimation = GetTransitionAnimation();
213 if( transitionAnimation )
215 DALI_ASSERT_DEBUG( mTickUVEffect );
219 transitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2::ONE );
222 transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
227 //explicitly end the swipe animation
228 actor.SetScale( Vector3::ONE );
231 mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
237 } // namespace Internal
239 } // namespace Toolkit