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 SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
90 SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
91 SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
92 SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
95 void CheckBoxButton::OnLabelSet()
97 Actor& label = GetLabelActor();
101 label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
102 label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
104 if( IsDisabled() && GetDisabledBackgroundImage() )
106 label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
108 else if ( GetBackgroundImage() )
110 label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
112 else if( IsSelected() && GetSelectedImage())
114 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
116 else if( GetUnselectedImage() )
118 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
122 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
127 void CheckBoxButton::OnDisabled()
129 Actor& backgroundImage = GetBackgroundImage();
130 Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
132 Actor& label = GetLabelActor();
135 if( IsDisabled() && disabledBackgroundImage )
137 label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
139 else if( backgroundImage )
141 label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
143 else if( IsSelected() && GetSelectedImage())
145 label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
147 else if( GetUnselectedImage() )
149 label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
153 label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
158 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
160 Actor& selectedImage = GetSelectedImage();
161 if( actor == selectedImage )
163 actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
167 mTickUVEffect = CreateImageRegionEffect();
169 mTickUVEffect.SetUniform("uBottomRight", Vector2( 0.0f, 1.0f ) );
171 ImageActor imageActor = ImageActor::DownCast( actor );
174 imageActor.SetShaderEffect( mTickUVEffect );
179 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
181 Actor& selectedImage = GetSelectedImage();
182 if( actor == selectedImage )
184 actor.SetScale( Vector3::ONE );
188 mTickUVEffect = CreateImageRegionEffect();
190 mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
192 ImageActor imageActor = ImageActor::DownCast( actor );
195 imageActor.SetShaderEffect( mTickUVEffect );
200 void CheckBoxButton::OnTransitionIn( Actor actor )
202 Actor& selectedImage = GetSelectedImage();
203 if( actor && actor == selectedImage )
205 if( GetPaintState() == UnselectedState )
207 Dali::Animation transitionAnimation = GetTransitionAnimation();
208 if( transitionAnimation )
210 DALI_ASSERT_DEBUG( mTickUVEffect );
214 transitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2::ONE );
217 transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
222 //explicitly end the swipe animation
223 actor.SetScale( Vector3::ONE );
226 mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
232 } // namespace Internal
234 } // namespace Toolkit