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
43 // Required for the UV reveal shader to render the tick on top of the rest of the checkbox.
44 const float SHADER_DEPTH_OFFSET = 1.0f;
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::OnButtonInitialize()
88 // Wrap around all children
89 Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
91 SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
92 SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
93 SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
94 SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
97 void CheckBoxButton::OnLabelSet( bool noPadding )
99 Actor& label = GetLabelActor();
103 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
104 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
106 if( IsDisabled() && GetDisabledBackgroundImage() )
108 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
110 else if ( GetBackgroundImage() )
112 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
114 else if( IsSelected() && GetSelectedImage())
116 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
118 else if( GetUnselectedImage() )
120 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
124 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
129 void CheckBoxButton::OnDisabled()
131 Actor& backgroundImage = GetBackgroundImage();
132 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
134 Actor& label = GetLabelActor();
137 if( IsDisabled() && disabledBackgroundImage )
139 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
141 else if( backgroundImage )
143 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
145 else if( IsSelected() && GetSelectedImage())
147 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
149 else if( GetUnselectedImage() )
151 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
155 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
160 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
162 Actor& selectedImage = GetSelectedImage();
163 if( actor == selectedImage )
165 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
169 mTickUVEffect = CreateImageRegionEffect();
171 mTickUVEffect.SetUniform( "uBottomRight", Vector2( 0.0f, 1.0f ) );
173 ImageActor imageActor = ImageActor::DownCast( actor );
176 // Ensure the tick effect is rendered above the reset of the checkbox.
177 imageActor.SetSortModifier( SHADER_DEPTH_OFFSET );
178 imageActor.SetShaderEffect( mTickUVEffect );
183 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
185 Actor& selectedImage = GetSelectedImage();
186 if( actor == selectedImage )
188 actor.SetScale( Vector3::ONE );
192 mTickUVEffect = CreateImageRegionEffect();
194 mTickUVEffect.SetUniform( "uBottomRight", Vector2::ONE );
196 ImageActor imageActor = ImageActor::DownCast( actor );
199 imageActor.SetShaderEffect( mTickUVEffect );
204 void CheckBoxButton::OnTransitionIn( Actor actor )
206 Actor& selectedImage = GetSelectedImage();
207 if( actor && actor == selectedImage )
209 if( GetPaintState() == UnselectedState )
211 Dali::Animation transitionAnimation = GetTransitionAnimation();
212 if( transitionAnimation )
214 DALI_ASSERT_DEBUG( mTickUVEffect );
218 transitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2::ONE );
221 transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
226 //explicitly end the swipe animation
227 actor.SetScale( Vector3::ONE );
230 mTickUVEffect.SetUniform( "uBottomRight", Vector2::ONE );
236 } // namespace Internal
238 } // namespace Toolkit