Merge "Layout fixes" into tizen
[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
25 namespace Dali
26 {
27
28 namespace Toolkit
29 {
30
31 namespace Internal
32 {
33
34 namespace
35 {
36
37 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
38 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
39
40 BaseHandle Create()
41 {
42   return Toolkit::CheckBoxButton::New();
43 }
44
45 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
46
47 }
48
49 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
50 {
51   // Create the implementation, temporarily owned on stack
52   IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
53
54   // Pass ownership to CustomActor
55   Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
56
57   // Second-phase init of the implementation
58   // This can only be done after the CustomActor connection has been made...
59   internalCheckBoxButton->Initialize();
60
61   return checkBoxButton;
62 }
63
64 CheckBoxButton::CheckBoxButton()
65 : Button()
66 {
67   SetTogglableButton( true );
68
69   SetAnimationTime( ANIMATION_TIME );
70 }
71
72 CheckBoxButton::~CheckBoxButton()
73 {
74   if( mTransitionAnimation )
75   {
76     mTransitionAnimation.Clear();
77   }
78 }
79
80 void CheckBoxButton::OnButtonInitialize()
81 {
82   // Wrap around all children
83   Self().SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
84 }
85
86 void CheckBoxButton::OnLabelSet()
87 {
88   Actor& label = GetLabel();
89
90   if( label )
91   {
92     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
93     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
94
95     if( IsDisabled() && GetDisabledBackgroundImage() )
96     {
97       label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
98     }
99     else if ( GetBackgroundImage() )
100     {
101       label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
102     }
103     else
104     {
105       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
106     }
107   }
108 }
109
110 bool CheckBoxButton::OnSelected()
111 {
112   Actor& selectedImage = GetSelectedImage();
113
114   PaintState paintState = GetPaintState();
115
116   switch( paintState )
117   {
118     case UnselectedState:
119     {
120       StartTransitionAnimation( selectedImage );
121       break;
122     }
123     case SelectedState:
124     {
125       RemoveChild( selectedImage );
126       break;
127     }
128     case UnselectedSelectedTransition:
129     {
130       StopTransitionAnimation( false );
131       RemoveChild( selectedImage );
132       break;
133     }
134     default:
135     {
136       break;
137     }
138   }
139
140   if( mTransitionAnimation )
141   {
142     return true;
143   }
144
145   return false;
146 }
147
148 bool CheckBoxButton::OnDisabled()
149 {
150   Actor& backgroundImage = GetBackgroundImage();
151   Actor& selectedImage = GetSelectedImage();
152   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
153   Actor& disabledSelectedImage = GetDisabledSelectedImage();
154
155   PaintState paintState = GetPaintState();
156
157   switch( paintState )
158   {
159     case UnselectedState:
160     {
161       RemoveChild( backgroundImage );
162       break;
163     }
164     case SelectedState:
165     {
166       RemoveChild( backgroundImage );
167       RemoveChild( selectedImage );
168       break;
169     }
170     case DisabledUnselectedState:
171     {
172       RemoveChild( disabledBackgroundImage );
173       break;
174     }
175     case DisabledSelectedState:
176     {
177       RemoveChild( disabledBackgroundImage );
178       RemoveChild( disabledSelectedImage );
179       break;
180     }
181     case UnselectedSelectedTransition:
182     {
183       StopTransitionAnimation();
184
185       RemoveChild( backgroundImage );
186       RemoveChild( selectedImage );
187       break;
188     }
189     default:
190     {
191       break;
192     }
193   }
194
195   Actor& label = GetLabel();
196
197   if( label )
198   {
199     if( IsDisabled() && disabledBackgroundImage)
200     {
201       label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
202     }
203     else if( backgroundImage )
204     {
205       label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
206     }
207     else
208     {
209       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
210     }
211   }
212
213   if( mTransitionAnimation )
214   {
215     return true;
216   }
217
218   return false;
219 }
220
221 void CheckBoxButton::StopAllAnimations()
222 {
223   StopTransitionAnimation();
224 }
225
226 void CheckBoxButton::StartTransitionAnimation( Actor& actor )
227 {
228   if( actor )
229   {
230     if( !mTickUVEffect )
231     {
232       ImageActor imageActor = ImageActor::DownCast( actor );
233       mTickUVEffect = ImageRegionEffect::New();
234       imageActor.SetShaderEffect( mTickUVEffect );
235     }
236
237     actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
238
239     mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
240
241     if( !mTransitionAnimation )
242     {
243       mTransitionAnimation = Dali::Animation::New( GetAnimationTime()  );
244     }
245
246     // UV anim
247     mTransitionAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
248
249     // Actor size anim
250     mTransitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
251
252     mTransitionAnimation.FinishedSignal().Connect( this, &CheckBoxButton::TransitionAnimationFinished );
253     mTransitionAnimation.Play();
254   }
255 }
256
257 void CheckBoxButton::StopTransitionAnimation( bool remove )
258 {
259   if( mTransitionAnimation )
260   {
261     mTransitionAnimation.Clear();
262     mTransitionAnimation.Reset();
263   }
264
265   if( remove )
266   {
267     UpdatePaintTransitionState();
268   }
269 }
270
271 void CheckBoxButton::TransitionAnimationFinished( Dali::Animation& source )
272 {
273   StopTransitionAnimation();
274 }
275
276 } // namespace Internal
277
278 } // namespace Toolkit
279
280 } // namespace Dali