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