b1f7474b87bb1ca0b9810d58d458da09057ff334
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include "check-box-button-impl.h"
20
21 // EXTERNAL INCLUDES
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>
25
26 //INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
42 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
43
44 BaseHandle Create()
45 {
46   return Toolkit::CheckBoxButton::New();
47 }
48
49 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
50
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";
55 }
56
57 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
58 {
59   // Create the implementation, temporarily owned on stack
60   IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
61
62   // Pass ownership to CustomActor
63   Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
64
65   // Second-phase init of the implementation
66   // This can only be done after the CustomActor connection has been made...
67   internalCheckBoxButton->Initialize();
68
69   return checkBoxButton;
70 }
71
72 CheckBoxButton::CheckBoxButton()
73 : Button()
74 {
75   SetTogglableButton( true );
76
77   SetAnimationTime( ANIMATION_TIME );
78 }
79
80 CheckBoxButton::~CheckBoxButton()
81 {
82 }
83
84 void CheckBoxButton::OnButtonInitialize()
85 {
86   // Wrap around all children
87   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
88
89   Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
90   Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
91   Image disabledImage = Dali::ResourceImage::New( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
92   Image disabledSelectedImage = Dali::ResourceImage::New( DISABLED_SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
93
94   SetButtonImage( ImageActor::New( buttonImage ) );
95   SetSelectedImage( ImageActor::New( selectedImage ) );
96   SetDisabledImage( ImageActor::New( disabledImage ) );
97   SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
98 }
99
100 void CheckBoxButton::OnLabelSet()
101 {
102   Actor& label = GetLabel();
103
104   if( label )
105   {
106     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
107     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
108
109     if( IsDisabled() && GetDisabledBackgroundImage() )
110     {
111       label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
112     }
113     else if ( GetBackgroundImage() )
114     {
115       label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
116     }
117     else if( IsSelected() && GetSelectedImage())
118     {
119       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
120     }
121     else if( GetButtonImage() )
122     {
123       label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
124     }
125     else
126     {
127       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
128     }
129   }
130 }
131
132 void CheckBoxButton::OnDisabled()
133 {
134   Actor& backgroundImage = GetBackgroundImage();
135   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
136
137   Actor& label = GetLabel();
138   if( label )
139   {
140     if( IsDisabled() && disabledBackgroundImage )
141     {
142       label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
143     }
144     else if( backgroundImage )
145     {
146       label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
147     }
148     else if( IsSelected() && GetSelectedImage())
149     {
150       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
151     }
152     else if( GetButtonImage() )
153     {
154       label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
155     }
156     else
157     {
158       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
159     }
160   }
161 }
162
163 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
164 {
165   Actor& selectedImage = GetSelectedImage();
166   if( actor == selectedImage )
167   {
168     actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
169
170     if( !mTickUVEffect )
171     {
172       mTickUVEffect = CreateImageRegionEffect();
173     }
174     mTickUVEffect.SetUniform("uBottomRight", Vector2( 0.0f, 1.0f ) );
175
176     ImageActor imageActor = ImageActor::DownCast( actor );
177     if( imageActor )
178     {
179       imageActor.SetShaderEffect( mTickUVEffect );
180     }
181   }
182 }
183
184 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
185 {
186   Actor& selectedImage = GetSelectedImage();
187   if( actor == selectedImage )
188   {
189     actor.SetScale( Vector3::ONE );
190
191     if( !mTickUVEffect )
192     {
193         mTickUVEffect = CreateImageRegionEffect();
194     }
195     mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
196
197     ImageActor imageActor = ImageActor::DownCast( actor );
198     if( imageActor )
199     {
200       imageActor.SetShaderEffect( mTickUVEffect );
201     }
202   }
203 }
204
205 void CheckBoxButton::OnTransitionIn( Actor actor )
206 {
207   Actor& selectedImage = GetSelectedImage();
208   if( actor && actor == selectedImage )
209   {
210     if( GetPaintState() == UnselectedState )
211     {
212       Dali::Animation transitionAnimation = GetTransitionAnimation();
213       if( transitionAnimation )
214       {
215         DALI_ASSERT_DEBUG( mTickUVEffect );
216         if( mTickUVEffect )
217         {
218           // UV anim
219           transitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2::ONE );
220         }
221         // Actor size anim
222         transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
223       }
224     }
225     else
226     {
227       //explicitly end the swipe animation
228       actor.SetScale( Vector3::ONE );
229       if( mTickUVEffect )
230       {
231         mTickUVEffect.SetUniform("uBottomRight", Vector2::ONE );
232       }
233     }
234   }
235 }
236
237 } // namespace Internal
238
239 } // namespace Toolkit
240
241 } // namespace Dali