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/object/type-registry.h>
23 #include <dali/public-api/images/resource-image.h>
26 #include <dali-toolkit/internal/controls/image-view/image-view-impl.h>
27 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
28 #include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
42 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
43 const float ANIMATION_TIME( 0.26f ); // EFL checkbox tick time
47 return Toolkit::CheckBoxButton::New();
50 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
52 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected.png";
53 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected.png";
54 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected-disabled.png";
55 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected-diabled.png";
58 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
60 // Create the implementation, temporarily owned on stack
61 IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
63 // Pass ownership to CustomActor
64 Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
66 // Second-phase init of the implementation
67 // This can only be done after the CustomActor connection has been made...
68 internalCheckBoxButton->Initialize();
70 return checkBoxButton;
73 CheckBoxButton::CheckBoxButton()
76 SetTogglableButton( true );
78 SetAnimationTime( ANIMATION_TIME );
81 CheckBoxButton::~CheckBoxButton()
85 void CheckBoxButton::SetTickUVEffect()
87 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( mSelectedImage );
90 imageView.RegisterProperty( "uTextureRect", Vector4(0.f, 0.f, 1.f, 1.f ) );
91 imageView.RegisterProperty( "uTopLeft", Vector2::ZERO );
93 Property::Map shaderMap = CreateImageRegionEffect();
94 imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, shaderMap );
96 GetImpl( imageView ).SetDepthIndex( DepthIndex::DECORATION );
100 void CheckBoxButton::OnInitialize()
102 Button::OnInitialize();
104 // Wrap around all children
105 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
107 SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
108 SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
109 SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
110 SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
112 mSelectedImage = GetSelectedImage();
116 void CheckBoxButton::OnLabelSet( bool noPadding )
118 Actor& label = GetLabelActor();
122 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
123 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
125 if( IsDisabled() && GetDisabledBackgroundImage() )
127 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
129 else if ( GetBackgroundImage() )
131 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
133 else if( IsSelected() && GetSelectedImage())
135 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
137 else if( GetUnselectedImage() )
139 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
143 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
148 void CheckBoxButton::OnDisabled()
150 Actor& backgroundImage = GetBackgroundImage();
151 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
153 Actor& label = GetLabelActor();
156 if( IsDisabled() && disabledBackgroundImage )
158 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
160 else if( backgroundImage )
162 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
164 else if( IsSelected() && GetSelectedImage())
166 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
168 else if( GetUnselectedImage() )
170 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
174 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
179 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
181 Actor& selectedImage = GetSelectedImage();
182 if( actor == selectedImage )
184 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
185 actor.RegisterProperty( "uBottomRight", Vector2( 0.0f, 1.0f ) );
187 if( mSelectedImage != selectedImage )
189 mSelectedImage = selectedImage;
195 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
197 Actor& selectedImage = GetSelectedImage();
198 if( actor == selectedImage )
200 actor.SetScale( Vector3::ONE );
201 actor.RegisterProperty( "uBottomRight", Vector2::ONE );
203 if( mSelectedImage != selectedImage )
205 mSelectedImage = selectedImage;
211 void CheckBoxButton::OnTransitionIn( Actor actor )
213 Actor& selectedImage = GetSelectedImage();
214 if( actor && actor == selectedImage )
216 if( GetPaintState() == UnselectedState )
218 Dali::Animation transitionAnimation = GetTransitionAnimation();
219 if( transitionAnimation )
222 transitionAnimation.AnimateTo( Property( actor, "uBottomRight" ), Vector2::ONE );
225 transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
230 //explicitly end the swipe animation
231 actor.SetScale( Vector3::ONE );
232 if( mSelectedImage == selectedImage )
234 actor.RegisterProperty( "uBottomRight", Vector2::ONE );
240 } // namespace Internal
242 } // namespace Toolkit