Merge branch 'tizen' into devel/new_mesh
[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 FOREGROUND_DEPTH( 0.5f );
38 const float BACKGROUND_DEPTH( 0.25f );
39
40 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
41
42 const Vector3 DISTANCE_BETWEEN_IMAGE_AND_LABEL(5.0f, 0.0f, 0.0f);
43
44 BaseHandle Create()
45 {
46   return Toolkit::CheckBoxButton::New();
47 }
48
49 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
50
51 }
52
53 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
54 {
55   // Create the implementation, temporarily owned on stack
56   IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
57
58   // Pass ownership to CustomActor
59   Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
60
61   // Second-phase init of the implementation
62   // This can only be done after the CustomActor connection has been made...
63   internalCheckBoxButton->Initialize();
64
65   return checkBoxButton;
66 }
67
68 CheckBoxButton::CheckBoxButton()
69 : Button(),
70   mPaintState( UnselectedState )
71 {
72   SetTogglableButton( true );
73
74   SetAnimationTime( ANIMATION_TIME );
75 }
76
77 CheckBoxButton::~CheckBoxButton()
78 {
79   if( mCheckInAnimation )
80   {
81     mCheckInAnimation.Clear();
82   }
83 }
84
85 void CheckBoxButton::OnButtonInitialize()
86 {
87   // Wrap around all children
88   Self().SetResizePolicy( FIT_TO_CHILDREN, ALL_DIMENSIONS );
89 }
90
91 void CheckBoxButton::SetSelectedImage( Actor image )
92 {
93   Actor& selectedImage = GetSelectedImage();
94
95   switch( mPaintState )
96   {
97     case SelectedState:
98     {
99       if( selectedImage && selectedImage.GetParent() )
100       {
101         Self().Remove( selectedImage );
102       }
103
104       selectedImage = image;
105       Self().Add( selectedImage );
106       break;
107     }
108     case UnselectedSelectedTransition:
109     {
110       StopCheckInAnimation();
111       Self().Remove( selectedImage );
112
113       selectedImage = image;
114       Self().Add( selectedImage );
115
116       mPaintState = SelectedState;
117       break;
118     }
119     default:
120     {
121       selectedImage = image;
122       break;
123     }
124   }
125
126   selectedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
127   selectedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
128   selectedImage.SetZ( FOREGROUND_DEPTH );
129 }
130
131 void CheckBoxButton::SetBackgroundImage( Actor image )
132 {
133   Actor& backgroundImage = GetBackgroundImage();
134
135   switch( mPaintState )
136   {
137     case UnselectedState:             // FALLTHROUGH
138     case SelectedState:
139     case UnselectedSelectedTransition:
140     {
141       if( backgroundImage && backgroundImage.GetParent() )
142       {
143         Self().Remove( backgroundImage );
144
145         Actor& label = GetLabel();
146
147         if( label )
148         {
149           backgroundImage.Remove( label );
150           image.Add( label );
151         }
152       }
153
154       backgroundImage = image;
155       Self().Add( backgroundImage );
156       break;
157     }
158     default:
159     {
160       backgroundImage = image;
161       break;
162     }
163   }
164
165   backgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
166   backgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
167   backgroundImage.SetZ( BACKGROUND_DEPTH );
168 }
169
170 void CheckBoxButton::SetDisabledSelectedImage( Actor image )
171 {
172   Actor& disabledSelectedImage = GetDisabledSelectedImage();
173
174   switch( mPaintState )
175   {
176     case DisabledSelectedState:
177     {
178       if( disabledSelectedImage && disabledSelectedImage.GetParent() )
179       {
180         Self().Remove( disabledSelectedImage );
181       }
182
183       disabledSelectedImage = image;
184       Self().Add( disabledSelectedImage );
185       break;
186     }
187     default:
188     {
189       disabledSelectedImage = image;
190       break;
191     }
192   }
193
194   disabledSelectedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
195   disabledSelectedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
196   disabledSelectedImage.SetZ( FOREGROUND_DEPTH );
197 }
198
199 void CheckBoxButton::SetDisabledBackgroundImage( Actor image )
200 {
201   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
202
203   switch( mPaintState )
204   {
205     case DisabledSelectedState:
206     case DisabledUnselectedState:
207     {
208       if( disabledBackgroundImage && disabledBackgroundImage.GetParent() )
209       {
210         Self().Remove( disabledBackgroundImage );
211
212         Actor& label = GetLabel();
213
214         if( label )
215         {
216           disabledBackgroundImage.Remove( label );
217           image.Add( label );
218         }
219       }
220
221       disabledBackgroundImage = image;
222       Self().Add( disabledBackgroundImage );
223       break;
224     }
225     default:
226     {
227       disabledBackgroundImage = image;
228       break;
229     }
230   }
231
232   disabledBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
233   disabledBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
234   disabledBackgroundImage.SetZ( BACKGROUND_DEPTH );
235 }
236
237 void CheckBoxButton::OnLabelSet()
238 {
239   Actor& label = GetLabel();
240
241   if( label )
242   {
243     label.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
244     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
245     label.TranslateBy( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
246
247     if( IsDisabled() && GetDisabledBackgroundImage() )
248     {
249       GetDisabledBackgroundImage().Add( label );
250     }
251     else if ( GetBackgroundImage() )
252     {
253       GetBackgroundImage().Add( label );
254     }
255     else
256     {
257       Self().Add( label );
258     }
259   }
260 }
261
262 void CheckBoxButton::OnSelected( bool selected )
263 {
264   Actor& selectedImage = GetSelectedImage();
265
266   switch( mPaintState )
267   {
268     case UnselectedState:
269     {
270       AddChild( selectedImage );
271       StartCheckInAnimation( selectedImage );    // Animate in the check actor
272
273       mPaintState = UnselectedSelectedTransition;
274       break;
275     }
276     case SelectedState:
277     {
278       RemoveChild( selectedImage );
279
280       mPaintState = UnselectedState;
281       break;
282     }
283     case UnselectedSelectedTransition:
284     {
285       StopCheckInAnimation();
286       RemoveChild( selectedImage );
287
288       mPaintState = UnselectedState;
289       break;
290     }
291     default:
292       break;
293   }
294 }
295
296 void CheckBoxButton::OnDisabled( bool disabled )
297 {
298   Actor& backgroundImage = GetBackgroundImage();
299   Actor& selectedImage = GetSelectedImage();
300   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
301   Actor& disabledSelectedImage = GetDisabledSelectedImage();
302
303   switch( mPaintState )
304   {
305     case UnselectedState:
306     {
307       if( disabled )
308       {
309         RemoveChild( backgroundImage );
310         AddChild( disabledBackgroundImage );
311         mPaintState = DisabledUnselectedState;
312       }
313       break;
314     }
315     case SelectedState:
316     {
317       if( disabled )
318       {
319         RemoveChild( backgroundImage );
320         RemoveChild( selectedImage );
321         AddChild( disabledBackgroundImage );
322         AddChild( disabledSelectedImage );
323
324         mPaintState = DisabledSelectedState;
325       }
326       break;
327     }
328     case DisabledUnselectedState:
329     {
330       if( !disabled )
331       {
332         RemoveChild( disabledBackgroundImage );
333         AddChild( backgroundImage );
334
335         mPaintState = UnselectedState;
336       }
337       break;
338     }
339     case DisabledSelectedState:
340     {
341       if( !disabled )
342       {
343         RemoveChild( disabledBackgroundImage );
344         RemoveChild( disabledSelectedImage );
345         AddChild( backgroundImage );
346         AddChild( selectedImage );
347
348         mPaintState = SelectedState;
349       }
350       break;
351     }
352     case UnselectedSelectedTransition:
353     {
354       if( disabled )
355       {
356         StopCheckInAnimation();
357
358         RemoveChild( backgroundImage );
359         RemoveChild( selectedImage );
360         AddChild( disabledBackgroundImage );
361         AddChild( disabledSelectedImage );
362
363         mPaintState = DisabledSelectedState;
364       }
365       break;
366     }
367     default:
368       break;
369   }
370
371   Actor& label = GetLabel();
372
373   if( label )
374   {
375     if( label.GetParent() )
376     {
377       label.GetParent().Remove( label );
378     }
379
380     if( disabled && disabledBackgroundImage)
381     {
382       disabledBackgroundImage.Add( label );
383     }
384     else if( backgroundImage )
385     {
386       backgroundImage.Add( label );
387     }
388   }
389 }
390
391 void CheckBoxButton::AddChild( Actor& actor )
392 {
393   if( actor )
394   {
395     Self().Add( actor);
396   }
397 }
398
399 void CheckBoxButton::RemoveChild( Actor& actor )
400 {
401   if( actor )
402   {
403     Self().Remove( actor );
404   }
405 }
406
407 void CheckBoxButton::StartCheckInAnimation( Actor& actor )
408 {
409   if( actor )
410   {
411     if( !mTickUVEffect )
412     {
413       ImageActor imageActor = ImageActor::DownCast( actor );
414       mTickUVEffect = ImageRegionEffect::New();
415       imageActor.SetShaderEffect( mTickUVEffect );
416     }
417
418     actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
419
420     mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
421
422     if( !mCheckInAnimation )
423     {
424       mCheckInAnimation = Dali::Animation::New( GetAnimationTime()  );
425     }
426
427     // UV anim
428     mCheckInAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
429
430     // Actor size anim
431     mCheckInAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
432
433     mCheckInAnimation.FinishedSignal().Connect( this, &CheckBoxButton::CheckInAnimationFinished );
434     mCheckInAnimation.Play();
435   }
436 }
437
438 void CheckBoxButton::StopCheckInAnimation()
439 {
440   if( mCheckInAnimation )
441   {
442     mCheckInAnimation.Clear();
443     mCheckInAnimation.Reset();
444   }
445 }
446
447 void CheckBoxButton::CheckInAnimationFinished( Dali::Animation& source )
448 {
449   switch( mPaintState )
450   {
451     case UnselectedSelectedTransition:
452     {
453       mPaintState = SelectedState;
454       break;
455     }
456     default:
457     {
458       break;
459     }
460   }
461
462   StopCheckInAnimation();
463 }
464
465 } // namespace Internal
466
467 } // namespace Toolkit
468
469 } // namespace Dali