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::OnButtonInitialize()
102 // Wrap around all children
103 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
105 SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
106 SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
107 SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
108 SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
110 mSelectedImage = GetSelectedImage();
114 void CheckBoxButton::OnLabelSet( bool noPadding )
116 Actor& label = GetLabelActor();
120 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
121 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
123 if( IsDisabled() && GetDisabledBackgroundImage() )
125 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
127 else if ( GetBackgroundImage() )
129 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
131 else if( IsSelected() && GetSelectedImage())
133 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
135 else if( GetUnselectedImage() )
137 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
141 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
146 void CheckBoxButton::OnDisabled()
148 Actor& backgroundImage = GetBackgroundImage();
149 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
151 Actor& label = GetLabelActor();
154 if( IsDisabled() && disabledBackgroundImage )
156 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
158 else if( backgroundImage )
160 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
162 else if( IsSelected() && GetSelectedImage())
164 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
166 else if( GetUnselectedImage() )
168 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
172 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
177 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
179 Actor& selectedImage = GetSelectedImage();
180 if( actor == selectedImage )
182 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
183 actor.RegisterProperty( "uBottomRight", Vector2( 0.0f, 1.0f ) );
185 if( mSelectedImage != selectedImage )
187 mSelectedImage = selectedImage;
193 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
195 Actor& selectedImage = GetSelectedImage();
196 if( actor == selectedImage )
198 actor.SetScale( Vector3::ONE );
199 actor.RegisterProperty( "uBottomRight", Vector2::ONE );
201 if( mSelectedImage != selectedImage )
203 mSelectedImage = selectedImage;
209 void CheckBoxButton::OnTransitionIn( Actor actor )
211 Actor& selectedImage = GetSelectedImage();
212 if( actor && actor == selectedImage )
214 if( GetPaintState() == UnselectedState )
216 Dali::Animation transitionAnimation = GetTransitionAnimation();
217 if( transitionAnimation )
220 transitionAnimation.AnimateTo( Property( actor, "uBottomRight" ), Vector2::ONE );
223 transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
228 //explicitly end the swipe animation
229 actor.SetScale( Vector3::ONE );
230 if( mSelectedImage == selectedImage )
232 actor.RegisterProperty( "uBottomRight", Vector2::ONE );
238 } // namespace Internal
240 } // namespace Toolkit