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