2 * Copyright (c) 2016 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>
29 #include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
43 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
44 const float ANIMATION_TIME( 0.26f ); // EFL checkbox tick time
48 return Toolkit::CheckBoxButton::New();
51 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
53 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected.png";
54 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected.png";
55 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected-disabled.png";
56 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected-diabled.png";
59 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
61 // Create the implementation, temporarily owned on stack
62 IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
64 // Pass ownership to CustomActor
65 Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
67 // Second-phase init of the implementation
68 // This can only be done after the CustomActor connection has been made...
69 internalCheckBoxButton->Initialize();
71 return checkBoxButton;
74 CheckBoxButton::CheckBoxButton()
77 SetTogglableButton( true );
79 SetAnimationTime( ANIMATION_TIME );
82 CheckBoxButton::~CheckBoxButton()
86 void CheckBoxButton::SetTickUVEffect()
88 Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( mSelectedImage );
91 imageView.RegisterProperty( "uTextureRect", Vector4(0.f, 0.f, 1.f, 1.f ) );
92 imageView.RegisterProperty( "uTopLeft", Vector2::ZERO );
94 Property::Map shaderMap = CreateImageRegionEffect();
95 imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, shaderMap );
97 GetImpl( imageView ).SetDepthIndex( DepthIndex::DECORATION );
101 void CheckBoxButton::OnInitialize()
103 Button::OnInitialize();
105 // Wrap around all children
106 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
108 SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
109 SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
110 SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
111 SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
113 mSelectedImage = GetSelectedImage();
117 void CheckBoxButton::OnLabelSet( bool noPadding )
119 Actor& label = GetLabelActor();
123 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
124 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
126 if( IsDisabled() && GetDisabledBackgroundImage() )
128 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
130 else if ( GetBackgroundImage() )
132 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
134 else if( IsSelected() && GetSelectedImage())
136 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
138 else if( GetUnselectedImage() )
140 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
144 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
149 void CheckBoxButton::OnDisabled()
151 Actor& backgroundImage = GetBackgroundImage();
152 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
154 Actor& label = GetLabelActor();
157 if( IsDisabled() && disabledBackgroundImage )
159 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
161 else if( backgroundImage )
163 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
165 else if( IsSelected() && GetSelectedImage())
167 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
169 else if( GetUnselectedImage() )
171 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
175 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
180 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
182 Actor& selectedImage = GetSelectedImage();
183 if( actor == selectedImage )
185 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
186 actor.RegisterProperty( "uBottomRight", Vector2( 0.0f, 1.0f ) );
188 if( mSelectedImage != selectedImage )
190 mSelectedImage = selectedImage;
196 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
198 Actor& selectedImage = GetSelectedImage();
199 if( actor == selectedImage )
201 actor.SetScale( Vector3::ONE );
202 actor.RegisterProperty( "uBottomRight", Vector2::ONE );
204 if( mSelectedImage != selectedImage )
206 mSelectedImage = selectedImage;
212 void CheckBoxButton::OnTransitionIn( Actor actor )
214 Actor& selectedImage = GetSelectedImage();
215 if( actor && actor == selectedImage )
217 if( GetPaintState() == UnselectedState )
219 Dali::Animation transitionAnimation = GetTransitionAnimation();
220 if( transitionAnimation )
223 transitionAnimation.AnimateTo( Property( actor, "uBottomRight" ), Vector2::ONE );
226 transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
231 //explicitly end the swipe animation
232 actor.SetScale( Vector3::ONE );
233 if( mSelectedImage == selectedImage )
235 actor.RegisterProperty( "uBottomRight", Vector2::ONE );
241 } // namespace Internal
243 } // namespace Toolkit