(Button) Allow button size to return natural size
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / buttons / check-box-button-default-painter-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18
19 #include "check-box-button-default-painter-impl.h"
20
21 // INTERNAL INCLUDES
22
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali/public-api/object/ref-object.h>
25 #include "check-box-button-impl.h"
26
27 // EXTERNAL INCLUDES
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40 const float FOREGROUND_DEPTH( 0.5f );
41 const float BACKGROUND_DEPTH( 0.25f );
42
43 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
44
45 const std::string PERCENTAGE_PARENT_SIZE_PROPERTY_NAME( "percentage-parent-size" );
46
47
48 /**
49  * Constraint to wrap an actor in y that is moving vertically
50  */
51 Vector3 EqualToPercentageWidthConstraint( const Vector3& current,
52                                           const PropertyInput& percentageProperty,
53                                           const PropertyInput& parentSizeProperty )
54 {
55   float percentage = percentageProperty.GetFloat();
56   const Vector3& parentSize = parentSizeProperty.GetVector3();
57
58   Vector3 size( parentSize );
59   size.x *= percentage;
60
61   return size;
62 }
63
64
65 inline Toolkit::Internal::CheckBoxButton& GetCheckBoxButtonImpl( Toolkit::Button& button )
66 {
67   DALI_ASSERT_ALWAYS( button );
68
69   Dali::RefObject& handle = button.GetImplementation();
70
71   return static_cast<Toolkit::Internal::CheckBoxButton&>( handle );
72 }
73
74 inline const Toolkit::Internal::CheckBoxButton& GetCheckBoxButtonImpl( const Toolkit::Button& button )
75 {
76   DALI_ASSERT_ALWAYS( button );
77
78   const Dali::RefObject& handle = button.GetImplementation();
79
80   return static_cast<const Toolkit::Internal::CheckBoxButton&>( handle );
81 }
82
83 }
84
85 CheckBoxButtonDefaultPainter::CheckBoxButtonDefaultPainter()
86 : CheckBoxButtonPainter(),
87   mDimmed( false ),
88   mPaintState( UncheckedState ),
89   mButton(NULL),
90   mAnimationTime( ANIMATION_TIME ),
91   mPercentageParentSizeProperty( Property::INVALID_INDEX )
92 {
93 }
94
95 CheckBoxButtonDefaultPainter::~CheckBoxButtonDefaultPainter()
96 {
97   if( mCheckInAnimation )
98   {
99     mCheckInAnimation.Clear();
100   }
101   if( mCheckOutAnimation )
102   {
103     mCheckOutAnimation.Clear();
104   }
105 }
106
107 void CheckBoxButtonDefaultPainter::SetBackgroundImage( Toolkit::CheckBoxButton& checkBox, Actor image )
108 {
109   Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetImplementation( checkBox );
110   Actor& backgroundImage = checkBoxImpl.GetBackgroundImage();
111   Actor& fadeOutBackgroundImage = checkBoxImpl.GetFadeOutBackgroundImage();
112
113   switch( mPaintState )
114   {
115     case UncheckedState:             // FALLTHROUGH
116     case CheckedState:
117     case UncheckedCheckedTransition:
118     case CheckedUncheckedTransition:
119     {
120       if( backgroundImage && backgroundImage.GetParent() )
121       {
122         StopCheckOutAnimation( checkBox );
123         FadeOutImage( checkBox, Background, backgroundImage  );
124
125         backgroundImage = image;
126
127         FadeInImage( checkBox, backgroundImage );
128
129         StartCheckOutAnimation( checkBox );
130         StartCheckInAnimation();
131       }
132       else
133       {
134         backgroundImage = image;
135         checkBox.Add( backgroundImage );
136       }
137       break;
138     }
139     case DimmedUncheckedTransition: // FALLTHROUGH
140     case DimmedCheckedTransition:
141     {
142       StopCheckInAnimation();
143       checkBox.Remove( backgroundImage );
144
145       backgroundImage = image;
146
147       FadeInImage( checkBox, backgroundImage );
148       StartCheckInAnimation();
149       break;
150     }
151     case CheckedDimmedTransition:   // FALLTHROUGH
152     case UncheckedDimmedTransition:
153     {
154       float opacity = 1.f;
155       if( fadeOutBackgroundImage )
156       {
157         opacity = fadeOutBackgroundImage.GetCurrentOpacity();
158       }
159       StopCheckOutAnimation( checkBox );
160
161       // Replaces the button image.
162       backgroundImage = image;
163
164       checkBox.Add( backgroundImage );
165       FadeOutImage( checkBox, Background, backgroundImage, opacity );
166
167       StartCheckOutAnimation( checkBox );
168       break;
169     }
170     default:
171     {
172       backgroundImage = image;
173       break;
174     }
175   }
176
177   backgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
178   backgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
179   ApplyConstraint( backgroundImage, BACKGROUND_DEPTH );
180 }
181
182 void CheckBoxButtonDefaultPainter::SetCheckedImage( Toolkit::CheckBoxButton& checkBox, Actor image )
183 {
184   Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetImplementation( checkBox );
185   Actor& checkedImage = checkBoxImpl.GetCheckedImage();
186   Actor& fadeOutCheckedImage = checkBoxImpl.GetFadeOutCheckedImage();
187
188   switch( mPaintState )
189   {
190     case CheckedState:
191     {
192       if( checkedImage && checkedImage.GetParent() )
193       {
194         StopCheckOutAnimation( checkBox );
195         FadeOutImage( checkBox, Foreground, checkedImage );
196
197         checkedImage = image;
198
199         FadeInImage( checkBox, checkedImage );
200
201         StartCheckOutAnimation( checkBox );
202         StartCheckInAnimation();
203       }
204       else
205       {
206         checkedImage = image;
207         checkBox.Add( checkedImage );
208       }
209       break;
210     }
211     case UncheckedCheckedTransition: // FALLTHROUGH
212     case DimmedCheckedTransition:
213     {
214       StopCheckInAnimation();
215       checkBox.Remove( checkedImage );
216
217       checkedImage = image;
218
219       FadeInImage( checkBox, checkedImage );
220       StartCheckInAnimation();
221       break;
222     }
223     case CheckedUncheckedTransition: // FALLTHROUGH
224     case CheckedDimmedTransition:
225     {
226       float opacity = 1.f;
227       if( fadeOutCheckedImage )
228       {
229         opacity = fadeOutCheckedImage.GetCurrentOpacity();
230       }
231       StopCheckOutAnimation( checkBox );
232
233       // Replaces the button image.
234       checkedImage = image;
235
236       checkBox.Add( checkedImage );
237       FadeOutImage( checkBox, Foreground, checkedImage, opacity );
238
239       StartCheckOutAnimation( checkBox );
240       break;
241     }
242     default:
243     {
244       checkedImage = image;
245       break;
246     }
247     }
248
249   checkedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
250   checkedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
251   ApplyCheckedConstraint( checkedImage, FOREGROUND_DEPTH );
252 }
253
254 void CheckBoxButtonDefaultPainter::SetDimmedCheckedImage( Toolkit::CheckBoxButton& checkBox, Actor image )
255 {
256   Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetImplementation( checkBox );
257   Actor& dimmedCheckedImage = checkBoxImpl.GetDimmedCheckedImage();
258   Actor& fadeOutCheckedImage = checkBoxImpl.GetFadeOutCheckedImage();
259
260   switch( mPaintState )
261   {
262     case DimmedCheckedState:
263     {
264       if( dimmedCheckedImage && dimmedCheckedImage.GetParent() )
265       {
266         StopCheckOutAnimation( checkBox );
267         FadeOutImage( checkBox, Foreground, dimmedCheckedImage );
268
269         dimmedCheckedImage = image;
270
271         FadeInImage( checkBox, dimmedCheckedImage );
272
273         StartCheckOutAnimation( checkBox );
274         StartCheckInAnimation();
275       }
276       else
277       {
278         dimmedCheckedImage = image;
279         checkBox.Add( dimmedCheckedImage );
280       }
281       break;
282     }
283     case CheckedDimmedTransition:
284     {
285       StopCheckInAnimation();
286       checkBox.Remove( dimmedCheckedImage );
287
288       dimmedCheckedImage = image;
289
290       FadeInImage( checkBox, dimmedCheckedImage );
291       StartCheckInAnimation();
292       break;
293     }
294     case DimmedCheckedTransition:
295     {
296       float opacity = 1.f;
297       if( fadeOutCheckedImage )
298       {
299         opacity = fadeOutCheckedImage.GetCurrentOpacity();
300       }
301       StopCheckOutAnimation( checkBox );
302
303       // Replaces the button image.
304       dimmedCheckedImage = image;
305
306       checkBox.Add( dimmedCheckedImage );
307       FadeOutImage( checkBox, Foreground, dimmedCheckedImage, opacity );
308
309       StartCheckOutAnimation( checkBox );
310       break;
311     }
312     default:
313     {
314       dimmedCheckedImage = image;
315       break;
316     }
317   }
318
319   dimmedCheckedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
320   dimmedCheckedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
321   ApplyConstraint( dimmedCheckedImage, FOREGROUND_DEPTH );
322 }
323
324 void CheckBoxButtonDefaultPainter::SetDimmedBackgroundImage( Toolkit::CheckBoxButton& checkBox, Actor image )
325 {
326   Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetImplementation( checkBox );
327   Actor& dimmedBackgroundImage = checkBoxImpl.GetDimmedBackgroundImage();
328   Actor& fadeOutBackgroundImage = checkBoxImpl.GetFadeOutBackgroundImage();
329
330   switch( mPaintState )
331   {
332     case DimmedCheckedState:   // FALLTHROUGH
333     case DimmedUncheckedState:
334     {
335       if( dimmedBackgroundImage && dimmedBackgroundImage.GetParent() )
336       {
337         StopCheckOutAnimation( checkBox );
338         FadeOutImage( checkBox, Background, dimmedBackgroundImage  );
339
340         dimmedBackgroundImage = image;
341
342         FadeInImage( checkBox, dimmedBackgroundImage );
343
344         StartCheckOutAnimation( checkBox );
345         StartCheckInAnimation();
346       }
347       else
348       {
349         dimmedBackgroundImage = image;
350         checkBox.Add( dimmedBackgroundImage );
351       }
352       break;
353     }
354     case UncheckedDimmedTransition: // FALLTHROUGH
355     case CheckedDimmedTransition:
356     {
357       StopCheckInAnimation();
358       checkBox.Remove( dimmedBackgroundImage );
359
360       dimmedBackgroundImage = image;
361
362       FadeInImage( checkBox, dimmedBackgroundImage );
363       StartCheckInAnimation();
364       break;
365     }
366     case DimmedUncheckedTransition: // FALLTHROUGH
367     case DimmedCheckedTransition:
368     {
369       float opacity = 1.f;
370       if( fadeOutBackgroundImage )
371       {
372         opacity = fadeOutBackgroundImage.GetCurrentOpacity();
373       }
374       StopCheckOutAnimation( checkBox );
375
376       // Replaces the button image.
377       dimmedBackgroundImage = image;
378
379       checkBox.Add( dimmedBackgroundImage );
380       FadeOutImage( checkBox, Background, dimmedBackgroundImage, opacity );
381
382       StartCheckOutAnimation( checkBox );
383       break;
384     }
385     default:
386     {
387       dimmedBackgroundImage = image;
388       break;
389     }
390   }
391
392   dimmedBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
393   dimmedBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
394   ApplyConstraint( dimmedBackgroundImage, BACKGROUND_DEPTH );
395 }
396
397 void CheckBoxButtonDefaultPainter::Initialize( Toolkit::Button& button )
398 {
399   Toolkit::Internal::CheckBoxButton& buttonImpl = GetCheckBoxButtonImpl( button );
400   Actor& backgroundImage = buttonImpl.GetBackgroundImage();
401   Actor& checkedImage = buttonImpl.GetCheckedImage();
402   Actor& dimmedBackgroundImage = buttonImpl.GetDimmedBackgroundImage();
403   Actor& dimmedCheckedImage = buttonImpl.GetDimmedCheckedImage();
404
405   Toolkit::CheckBoxButton& checkBox = static_cast<Toolkit::CheckBoxButton&>( button );
406
407   if( backgroundImage )
408   {
409     SetBackgroundImage( checkBox, backgroundImage );
410   }
411
412   if( checkedImage )
413   {
414     SetCheckedImage( checkBox, checkedImage );
415   }
416
417   if( dimmedBackgroundImage )
418   {
419     SetDimmedBackgroundImage( checkBox, dimmedBackgroundImage );
420   }
421
422   if( dimmedCheckedImage )
423   {
424     SetDimmedCheckedImage( checkBox, dimmedCheckedImage );
425   }
426
427   SetDimmed( button, mDimmed );
428 }
429
430 void CheckBoxButtonDefaultPainter::SetSize( Toolkit::Button& button, const Vector3& size )
431 {
432   Toolkit::Internal::CheckBoxButton& buttonImpl = GetCheckBoxButtonImpl( button );
433   Actor& backgroundImage = buttonImpl.GetBackgroundImage();
434   Actor& checkedImage = buttonImpl.GetCheckedImage();
435   Actor& dimmedBackgroundImage = buttonImpl.GetDimmedBackgroundImage();
436   Actor& dimmedCheckedImage = buttonImpl.GetDimmedCheckedImage();
437
438   ApplyCheckedConstraint( checkedImage, FOREGROUND_DEPTH );
439   ApplyConstraint( backgroundImage, BACKGROUND_DEPTH );
440   ApplyConstraint( dimmedCheckedImage, FOREGROUND_DEPTH );
441   ApplyConstraint( dimmedBackgroundImage, BACKGROUND_DEPTH );
442 }
443
444 void CheckBoxButtonDefaultPainter::SetDimmed( Toolkit::Button& button, bool dimmed )
445 {
446   mDimmed = dimmed;
447
448   Toolkit::Internal::CheckBoxButton& buttonImpl = GetCheckBoxButtonImpl( button );
449   Actor& backgroundImage = buttonImpl.GetBackgroundImage();
450   Actor& checkedImage = buttonImpl.GetCheckedImage();
451   Actor& dimmedBackgroundImage = buttonImpl.GetDimmedBackgroundImage();
452   Actor& dimmedCheckedImage = buttonImpl.GetDimmedCheckedImage();
453   Actor& fadeOutCheckedImage = buttonImpl.GetFadeOutCheckedImage();
454   Actor& fadeOutBackgroundImage = buttonImpl.GetFadeOutBackgroundImage();
455
456   Toolkit::CheckBoxButton& checkBox = static_cast<Toolkit::CheckBoxButton&>( button );
457
458   switch( mPaintState )
459   {
460     case UncheckedState:
461     {
462       if( dimmed )
463       {
464         StopCheckOutAnimation( checkBox );
465         FadeOutImage( checkBox, Background, backgroundImage );
466         FadeInImage( checkBox, dimmedBackgroundImage );
467         StartCheckOutAnimation( checkBox );
468         StartCheckInAnimation();
469
470         mPaintState = UncheckedDimmedTransition;
471       }
472       break;
473     }
474     case CheckedState:
475     {
476       if( dimmed )
477       {
478         StopCheckOutAnimation( checkBox );
479         FadeOutImage( checkBox, Background, backgroundImage );
480         FadeOutImage( checkBox, Foreground, checkedImage );
481         FadeInImage( checkBox, dimmedCheckedImage );
482         FadeInImage( checkBox, dimmedBackgroundImage );
483         StartCheckOutAnimation( checkBox );
484         StartCheckInAnimation();
485
486         mPaintState = CheckedDimmedTransition;
487       }
488       break;
489     }
490     case DimmedUncheckedState:
491     {
492       if( !dimmed )
493       {
494         StopCheckOutAnimation( checkBox );
495         FadeOutImage( checkBox, Background, dimmedBackgroundImage );
496         FadeInImage( checkBox, backgroundImage );
497         StartCheckOutAnimation( checkBox );
498         StartCheckInAnimation();
499
500         mPaintState = DimmedUncheckedTransition;
501       }
502       break;
503     }
504     case DimmedCheckedState:
505     {
506       if( !dimmed )
507       {
508         StopCheckOutAnimation( checkBox );
509         FadeOutImage( checkBox, Background, dimmedBackgroundImage );
510         FadeOutImage( checkBox, Foreground, dimmedCheckedImage );
511         FadeInImage( checkBox, backgroundImage );
512         FadeInImage( checkBox, checkedImage );
513         StartCheckOutAnimation( checkBox );
514         StartCheckInAnimation();
515
516         mPaintState = DimmedCheckedTransition;
517       }
518       break;
519     }
520     case UncheckedCheckedTransition:
521     {
522       if( dimmed )
523       {
524         float opacity = 1.f;
525         if( checkedImage )
526         {
527           opacity = checkedImage.GetCurrentOpacity();
528         }
529         StopCheckOutAnimation( checkBox );
530         StopCheckInAnimation();
531
532         FadeOutImage( checkBox, Foreground, checkedImage, opacity );
533         FadeOutImage( checkBox, Background, backgroundImage );
534
535         FadeInImage( checkBox, dimmedCheckedImage );
536         FadeInImage( checkBox, dimmedBackgroundImage );
537
538         StartCheckOutAnimation( checkBox );
539         StartCheckInAnimation();
540
541         mPaintState = CheckedDimmedTransition;
542       }
543       break;
544     }
545     case CheckedUncheckedTransition:
546     {
547       if( dimmed )
548       {
549         float opacity = 1.f;
550         if( fadeOutCheckedImage )
551         {
552           opacity = fadeOutCheckedImage.GetCurrentOpacity();
553         }
554         StopCheckOutAnimation( checkBox );
555         StopCheckInAnimation();
556
557         button.Add( dimmedCheckedImage );
558         FadeOutImage( checkBox, Foreground, dimmedCheckedImage, opacity );
559         FadeOutImage( checkBox, Background, backgroundImage );
560
561         FadeInImage( checkBox, dimmedBackgroundImage );
562
563         StartCheckOutAnimation( checkBox );
564         StartCheckInAnimation();
565
566         mPaintState = UncheckedDimmedTransition;
567       }
568       break;
569     }
570     case UncheckedDimmedTransition:
571     {
572       if( !dimmed )
573       {
574         float opacity = 1.f;
575         if( fadeOutBackgroundImage )
576         {
577           opacity = fadeOutBackgroundImage.GetCurrentOpacity();
578         }
579         StopCheckOutAnimation( checkBox, false );
580         StopCheckInAnimation();
581
582         FadeOutImage( checkBox, Background, dimmedBackgroundImage, 1.f - opacity );
583         FadeInImage( checkBox, backgroundImage, opacity );
584
585         StartCheckOutAnimation( checkBox );
586         StartCheckInAnimation();
587
588         mPaintState = DimmedUncheckedTransition;
589       }
590       break;
591     }
592     case DimmedUncheckedTransition:
593     {
594       if( dimmed )
595       {
596         float opacity = 1.f;
597         if( fadeOutBackgroundImage )
598         {
599           opacity = fadeOutBackgroundImage.GetCurrentOpacity();
600         }
601         StopCheckOutAnimation( checkBox, false );
602         StopCheckInAnimation();
603
604         FadeOutImage( checkBox, Background, backgroundImage, 1.f - opacity );
605         FadeInImage( checkBox, dimmedBackgroundImage, opacity );
606
607         StartCheckOutAnimation( checkBox );
608         StartCheckInAnimation();
609
610         mPaintState = UncheckedDimmedTransition;
611       }
612       break;
613     }
614     case CheckedDimmedTransition:
615     {
616       if( !dimmed )
617       {
618         float opacity = 1.f;
619         if( fadeOutBackgroundImage )
620         {
621           opacity = fadeOutBackgroundImage.GetCurrentOpacity();
622         }
623         StopCheckOutAnimation( checkBox, false );
624         StopCheckInAnimation();
625
626         FadeOutImage( checkBox, Foreground, dimmedCheckedImage, 1.f - opacity );
627         FadeOutImage( checkBox, Background, dimmedBackgroundImage, 1.f - opacity );
628         FadeInImage( checkBox, checkedImage, opacity );
629         FadeInImage( checkBox, backgroundImage, opacity );
630
631         StartCheckOutAnimation( checkBox );
632         StartCheckInAnimation();
633
634         mPaintState = DimmedCheckedTransition;
635       }
636       break;
637     }
638     case DimmedCheckedTransition:
639     {
640       if( dimmed )
641       {
642         float opacity = 1.f;
643         if( fadeOutBackgroundImage )
644         {
645           opacity = fadeOutBackgroundImage.GetCurrentOpacity();
646         }
647         StopCheckOutAnimation( checkBox, false );
648         StopCheckInAnimation();
649
650         FadeOutImage( checkBox, Foreground, checkedImage, 1.f - opacity );
651         FadeOutImage( checkBox, Background, backgroundImage, 1.f - opacity );
652         FadeInImage( checkBox, dimmedCheckedImage, opacity );
653         FadeInImage( checkBox, dimmedBackgroundImage, opacity );
654
655         StartCheckOutAnimation( checkBox );
656         StartCheckInAnimation();
657
658         mPaintState = CheckedDimmedTransition;
659       }
660       break;
661     }
662     default:
663       break;
664   }
665 }
666
667 void CheckBoxButtonDefaultPainter::SetAnimationTime( float animationTime )
668 {
669   mAnimationTime = animationTime;
670 }
671
672 float CheckBoxButtonDefaultPainter::GetAnimationTime() const
673 {
674   return mAnimationTime;
675 }
676
677 void CheckBoxButtonDefaultPainter::Checked( Toolkit::CheckBoxButton& button )
678 {
679   Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetCheckBoxButtonImpl( button );
680   Actor& checkedImage = checkBoxImpl.GetCheckedImage();
681   Actor& fadeOutCheckedImage = checkBoxImpl.GetFadeOutCheckedImage();
682
683   switch( mPaintState )
684   {
685     case UncheckedState:
686     {
687       // Fade in the 'check' actor.
688       FadeInImage( button, checkedImage );
689       SetupCheckedAnimation( button, checkedImage );    // Animate in the check actor
690       StartCheckInAnimation();
691
692       mPaintState = UncheckedCheckedTransition;
693       break;
694     }
695     case CheckedState:
696     {
697       // Fade out the 'check' actor.
698       StopCheckOutAnimation( button );
699       FadeOutImage( button, Foreground, checkedImage );
700       StartCheckOutAnimation( button );
701
702       if( button.GetProperty<bool>( button.GetPropertyIndex( Toolkit::CheckBoxButton::USE_FADE_ANIMATION_PROPERTY_NAME ) ) )
703       {
704         mPaintState = CheckedUncheckedTransition;
705       }
706       else
707       {
708         mPaintState = UncheckedState;
709       }
710       break;
711     }
712     case UncheckedCheckedTransition:
713     {
714       // Stop fade in and start fade out.
715       StopCheckOutAnimation( button );
716       StopCheckInAnimation();
717
718       float opacity = 0.f;
719       if( checkedImage )
720       {
721         opacity = checkedImage.GetCurrentOpacity();
722       }
723       FadeOutImage( button, Foreground, checkedImage, opacity );
724       StartCheckOutAnimation( button );
725
726       if( button.GetProperty<bool>( button.GetPropertyIndex( Toolkit::CheckBoxButton::USE_FADE_ANIMATION_PROPERTY_NAME ) ) )
727       {
728         mPaintState = CheckedUncheckedTransition;
729       }
730       else
731       {
732         mPaintState = UncheckedState;
733       }
734       break;
735     }
736     case CheckedUncheckedTransition:
737     {
738       // Stop fade out and start fade in.
739       float opacity = 1.f;
740       if( fadeOutCheckedImage )
741       {
742         opacity = fadeOutCheckedImage.GetCurrentOpacity();
743       }
744       StopCheckOutAnimation( button );
745
746       FadeInImage( button, checkedImage, opacity );
747       StartCheckInAnimation();
748
749       mPaintState = UncheckedCheckedTransition;
750       break;
751     }
752     default:
753       break;
754   }
755 }
756
757 void CheckBoxButtonDefaultPainter::ApplyConstraint( Actor& actor, float depth )
758 {
759   if( actor )
760   {
761     actor.RemoveConstraints();
762     actor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
763     actor.SetZ( depth );
764   }
765 }
766
767 void CheckBoxButtonDefaultPainter::ApplyCheckedConstraint( Actor& actor, float depth )
768 {
769   if( actor )
770   {
771     if( mPercentageParentSizeProperty == Property::INVALID_INDEX )
772     {
773       mPercentageParentSizeProperty = actor.RegisterProperty( PERCENTAGE_PARENT_SIZE_PROPERTY_NAME, 1.0f );
774     }
775
776     actor.RemoveConstraints();
777     actor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE,
778                                                      LocalSource( mPercentageParentSizeProperty ),
779                                                      ParentSource( Actor::SIZE ),
780                                                      EqualToPercentageWidthConstraint ) );
781     actor.SetZ( depth );
782   }
783 }
784
785 void CheckBoxButtonDefaultPainter::AddToFadeInAnimation( const Actor& actor )
786 {
787   if( !mCheckInAnimation )
788   {
789     mCheckInAnimation = Dali::Animation::New( mAnimationTime  );
790   }
791
792   mCheckInAnimation.OpacityTo( actor, 1.f );
793
794 }
795
796 void CheckBoxButtonDefaultPainter::StartCheckInAnimation()
797 {
798   if( mCheckInAnimation )
799   {
800     mCheckInAnimation.FinishedSignal().Connect( this, &CheckBoxButtonDefaultPainter::CheckInAnimationFinished );
801     mCheckInAnimation.Play();
802   }
803 }
804
805 void CheckBoxButtonDefaultPainter::StopCheckInAnimation()
806 {
807   if( mCheckInAnimation )
808   {
809     mCheckInAnimation.Clear();
810     mCheckInAnimation.Reset();
811   }
812 }
813
814 void CheckBoxButtonDefaultPainter::AddToFadeOutAnimation( const Actor& actor )
815 {
816   if( !mCheckOutAnimation )
817   {
818     mCheckOutAnimation = Dali::Animation::New( mAnimationTime );
819   }
820
821   mCheckOutAnimation.OpacityTo( actor, 0.f );
822 }
823
824 void CheckBoxButtonDefaultPainter::StartCheckOutAnimation( Toolkit::CheckBoxButton& checkBox )
825 {
826   if( mCheckOutAnimation )
827   {
828     Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetCheckBoxButtonImpl( checkBox );
829     mButton = &checkBoxImpl;
830
831     mCheckOutAnimation.FinishedSignal().Connect( this, &CheckBoxButtonDefaultPainter::CheckOutAnimationFinished );
832     mCheckOutAnimation.Play();
833   }
834 }
835
836 void CheckBoxButtonDefaultPainter::StopCheckOutAnimation( Toolkit::CheckBoxButton& checkBox, bool remove )
837 {
838   if( mCheckOutAnimation )
839   {
840     mCheckOutAnimation.Clear();
841     mCheckOutAnimation.Reset();
842   }
843
844   Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetCheckBoxButtonImpl( checkBox );
845   Actor& fadeOutCheckedImage = checkBoxImpl.GetFadeOutCheckedImage();
846   Actor& fadeOutBackgroundImage = checkBoxImpl.GetFadeOutBackgroundImage();
847
848   if( remove )
849   {
850     if( fadeOutCheckedImage && fadeOutCheckedImage.GetParent() )
851     {
852       fadeOutCheckedImage.GetParent().Remove( fadeOutCheckedImage );
853     }
854
855     if( fadeOutBackgroundImage && fadeOutBackgroundImage.GetParent() )
856     {
857       fadeOutBackgroundImage.GetParent().Remove( fadeOutBackgroundImage );
858     }
859
860     fadeOutCheckedImage.Reset();
861     fadeOutBackgroundImage.Reset();
862   }
863 }
864
865 void CheckBoxButtonDefaultPainter::FadeInImage( Toolkit::CheckBoxButton& checkBox, Actor& image, float opacity )
866 {
867   if( image )
868   {
869     if( !image.GetParent() )
870     {
871       checkBox.Add( image );
872     }
873
874     if( checkBox.GetProperty<bool>( checkBox.GetPropertyIndex( Toolkit::CheckBoxButton::USE_FADE_ANIMATION_PROPERTY_NAME ) ) )
875     {
876       image.SetOpacity( opacity );
877       AddToFadeInAnimation( image );
878     }
879     else
880     {
881       image.SetOpacity( 1.0f );
882     }
883   }
884 }
885
886 void CheckBoxButtonDefaultPainter::FadeOutImage( Toolkit::CheckBoxButton& checkBox, ImageLayer layer, Actor& image, float opacity )
887 {
888   if( image )
889   {
890     Toolkit::Internal::CheckBoxButton& checkBoxImpl = GetCheckBoxButtonImpl( checkBox );
891     Actor& fadeOutCheckedImage = checkBoxImpl.GetFadeOutCheckedImage();
892     Actor& fadeOutBackgroundImage = checkBoxImpl.GetFadeOutBackgroundImage();
893
894     Actor& actorLayer = ( ( Background == layer ) ? fadeOutBackgroundImage : fadeOutCheckedImage );
895
896     actorLayer = image;
897
898     if( checkBox.GetProperty<bool>( checkBox.GetPropertyIndex( Toolkit::CheckBoxButton::USE_FADE_ANIMATION_PROPERTY_NAME ) ) )
899     {
900       actorLayer.SetOpacity( opacity );
901       AddToFadeOutAnimation( actorLayer );
902     }
903     else
904     {
905       actorLayer.SetOpacity( 0.0f );
906     }
907   }
908 }
909
910 void CheckBoxButtonDefaultPainter::AddToCheckInAnimation( const Actor& actor )
911 {
912   if( !mCheckInAnimation )
913   {
914     mCheckInAnimation = Dali::Animation::New( mAnimationTime  );
915   }
916
917   // UV anim
918   mCheckInAnimation.AnimateTo( Property( mTickUVEffect, mTickUVEffect.GetBottomRightPropertyName() ), Vector2( 1.0f, 1.0f ) );
919
920   // Actor size anim
921   Handle handle = actor; // Get rid of const
922   mCheckInAnimation.AnimateTo( Property( handle, mPercentageParentSizeProperty ), 1.0f );
923 }
924
925 void CheckBoxButtonDefaultPainter::SetupCheckedAnimation( Toolkit::CheckBoxButton& checkBox, Actor& image )
926 {
927   if( checkBox.GetProperty<bool>( checkBox.GetPropertyIndex( Toolkit::CheckBoxButton::USE_CHECK_ANIMATION_PROPERTY_NAME ) ) && image )
928   {
929     if( !mTickUVEffect )
930     {
931       ImageActor imageActor = ImageActor::DownCast( image );
932       mTickUVEffect = ImageRegionEffect::New();
933       imageActor.SetShaderEffect( mTickUVEffect );
934     }
935
936     // Register a custom property to animate size of tick over
937     if( mPercentageParentSizeProperty != Property::INVALID_INDEX )
938     {
939       image.SetProperty( mPercentageParentSizeProperty, 0.0f );
940     }
941
942     mTickUVEffect.SetBottomRight( Vector2( 0.0f, 1.0f ) );
943
944     // Parent
945     if( !image.GetParent() )
946     {
947       checkBox.Add( image );
948     }
949
950     AddToCheckInAnimation( image );
951   }
952 }
953
954 void CheckBoxButtonDefaultPainter::EndCheckOutAnimation()
955 {
956   switch( mPaintState )
957   {
958     case UncheckedCheckedTransition:
959     {
960       mPaintState = CheckedState;
961       break;
962     }
963     case CheckedUncheckedTransition:
964     {
965       mPaintState = UncheckedState;
966       break;
967     }
968     case UncheckedDimmedTransition:
969     {
970       mPaintState = DimmedUncheckedState;
971       break;
972     }
973     case DimmedUncheckedTransition:
974     {
975       mPaintState = UncheckedState;
976       break;
977     }
978     case CheckedDimmedTransition:
979     {
980       mPaintState = DimmedCheckedState;
981       break;
982     }
983     case DimmedCheckedTransition:
984     {
985       mPaintState = CheckedState;
986       break;
987     }
988     default:
989     {
990       break;
991     }
992   }
993 }
994
995 void CheckBoxButtonDefaultPainter::CheckOutAnimationFinished( Dali::Animation& source )
996 {
997   EndCheckOutAnimation();
998
999   Toolkit::CheckBoxButton handle( mButton->GetOwner() );
1000   StopCheckOutAnimation( handle );
1001   mButton = NULL;
1002 }
1003
1004 void CheckBoxButtonDefaultPainter::CheckInAnimationFinished( Dali::Animation& source )
1005 {
1006   switch( mPaintState )
1007   {
1008     case UncheckedCheckedTransition:
1009     {
1010       mPaintState = CheckedState;
1011       break;
1012     }
1013     case CheckedUncheckedTransition:
1014     {
1015       mPaintState = UncheckedState;
1016       break;
1017     }
1018     case UncheckedDimmedTransition:
1019     {
1020       mPaintState = DimmedUncheckedState;
1021       break;
1022     }
1023     case DimmedUncheckedTransition:
1024     {
1025       mPaintState = UncheckedState;
1026       break;
1027     }
1028     case CheckedDimmedTransition:
1029     {
1030       mPaintState = DimmedCheckedState;
1031       break;
1032     }
1033     case DimmedCheckedTransition:
1034     {
1035       mPaintState = CheckedState;
1036       break;
1037     }
1038     default:
1039     {
1040       break;
1041     }
1042   }
1043
1044   StopCheckInAnimation();
1045 }
1046
1047 } // namespace Internal
1048
1049 } // namespace Toolkit
1050
1051 } // namespace Dali