Add 'ExclusiveArch: armv7l' limit build to arm architecture
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-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 "push-button-default-painter-impl.h"
20
21 // INTERNAL INCLUDES
22
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
25 #include <dali-toolkit/public-api/controls/buttons/push-button.h>
26 #include <dali/public-api/object/ref-object.h>
27
28 #include "push-button-impl.h"
29
30 // EXTERNAL INCLUDES
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43 const float LABEL_DEPTH( 0.75f );
44 const float FOREGROUND_DEPTH( 0.5f );
45 const float BACKGROUND_DEPTH( 0.25f );
46
47 const float ANIMATION_TIME( 0.2f );
48
49 inline Toolkit::Internal::PushButton& GetPushButtonImpl( Toolkit::Button& button )
50 {
51   DALI_ASSERT_ALWAYS( button );
52
53   Dali::RefObject& handle = button.GetImplementation();
54
55   return static_cast<Toolkit::Internal::PushButton&>( handle );
56 }
57
58 inline const Toolkit::Internal::PushButton& GetPushButtonImpl( const Toolkit::Button& button )
59 {
60   DALI_ASSERT_ALWAYS( button );
61
62   const Dali::RefObject& handle = button.GetImplementation();
63
64   return static_cast<const Toolkit::Internal::PushButton&>( handle );
65 }
66
67 }
68
69 PushButtonDefaultPainter::PushButtonDefaultPainter()
70 : PushButtonPainter(),
71   mAutoRepeating( false ),
72   mDimmed( false ),
73   mPaintState( ReleasedState ),
74   mButton( NULL ),
75   mAnimationTime( ANIMATION_TIME ),
76   mSize()
77 {
78 }
79
80 PushButtonDefaultPainter::~PushButtonDefaultPainter()
81 {
82   if( mFadeInAnimation )
83   {
84     mFadeInAnimation.Clear();
85   }
86   if( mFadeOutAnimation )
87   {
88     mFadeOutAnimation.Clear();
89   }
90 }
91
92 void PushButtonDefaultPainter::SetButtonImage( Toolkit::PushButton& pushButton, Actor image )
93 {
94   Toolkit::Internal::PushButton& pushButtonImpl = GetImplementation( pushButton );
95   Actor& buttonImage = pushButtonImpl.GetButtonImage();
96   Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
97
98   switch( mPaintState )
99   {
100     case ReleasedState:
101     {
102       if( buttonImage && buttonImage.GetParent() )
103       {
104         StopFadeOutAnimation( pushButton );
105         FadeOutImage( pushButton, Foreground, buttonImage );
106
107         buttonImage = image;
108
109         FadeInImage( pushButton, buttonImage );
110
111         StartFadeOutAnimation( pushButton );
112         StartFadeInAnimation();
113       }
114       else
115       {
116         buttonImage = image;
117         pushButton.Add( buttonImage );
118       }
119       break;
120     }
121     case ReleasedPressedTransition: // FALLTHROUGH
122     case ReleasedDimmedTransition:
123     {
124       float opacity = 1.f;
125       if( fadeOutButtonImage )
126       {
127         opacity = fadeOutButtonImage.GetCurrentOpacity();
128       }
129       StopFadeOutAnimation( pushButton );
130
131       // Replaces the button image.
132       buttonImage = image;
133
134       pushButton.Add( buttonImage );
135       FadeOutImage( pushButton, Foreground, buttonImage, opacity );
136
137       StartFadeOutAnimation( pushButton );
138       break;
139     }
140     case PressedReleasedTransition: // FALLTHROUGH
141     case DimmedReleasedTransition:
142     {
143       StopFadeInAnimation();
144       pushButton.Remove( buttonImage );
145
146       buttonImage = image;
147
148       FadeInImage( pushButton, buttonImage );
149       StartFadeInAnimation();
150       break;
151     }
152     default:
153       buttonImage = image;
154       break;
155   }
156
157   buttonImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
158   buttonImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
159   ApplyConstraint( buttonImage, FOREGROUND_DEPTH );
160 }
161
162 void PushButtonDefaultPainter::SetBackgroundImage( Toolkit::PushButton& pushButton, Actor image )
163 {
164   Toolkit::Internal::PushButton& pushButtonImpl = GetImplementation( pushButton );
165   Actor& backgroundImage = pushButtonImpl.GetBackgroundImage();
166   Actor& fadeOutBackgroundImage = pushButtonImpl.GetFadeOutBackgroundImage();
167
168   switch( mPaintState )
169   {
170     case ReleasedState:             // FALLTHROUGH
171     case PressedState:
172     case ReleasedPressedTransition:
173     case PressedReleasedTransition:
174     {
175       if( backgroundImage && backgroundImage.GetParent() )
176       {
177         StopFadeOutAnimation( pushButton );
178         FadeOutImage( pushButton, Background, backgroundImage  );
179
180         backgroundImage = image;
181
182         FadeInImage( pushButton, backgroundImage );
183
184         StartFadeOutAnimation( pushButton );
185         StartFadeInAnimation();
186       }
187       else
188       {
189         backgroundImage = image;
190         pushButton.Add( backgroundImage );
191       }
192       break;
193     }
194     case ReleasedDimmedTransition: // FALLTHROUGH
195     case PressedDimmedTransition:
196     {
197       float opacity = 1.f;
198       if( fadeOutBackgroundImage )
199       {
200         opacity = fadeOutBackgroundImage.GetCurrentOpacity();
201       }
202       StopFadeOutAnimation( pushButton );
203
204       // Replaces the button image.
205       backgroundImage = image;
206
207       pushButton.Add( backgroundImage );
208       FadeOutImage( pushButton, Background, backgroundImage, opacity );
209
210       StartFadeOutAnimation( pushButton );
211       break;
212     }
213     case DimmedReleasedTransition: // FALLTHROUGH
214     case DimmedPressedTransition:
215     {
216       StopFadeInAnimation();
217       pushButton.Remove( backgroundImage );
218
219       backgroundImage = image;
220
221       FadeInImage( pushButton, backgroundImage );
222       StartFadeInAnimation();
223       break;
224     }
225     default:
226       backgroundImage = image;
227       break;
228     }
229
230   backgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
231   backgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
232   ApplyConstraint( backgroundImage, BACKGROUND_DEPTH );
233 }
234
235 void PushButtonDefaultPainter::SetPressedImage( Toolkit::PushButton& pushButton, Actor image )
236 {
237   Toolkit::Internal::PushButton& pushButtonImpl = GetImplementation( pushButton );
238   Actor& pressedImage = pushButtonImpl.GetPressedImage();
239   Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
240
241   switch( mPaintState )
242   {
243     case PressedState:
244     {
245       if( pressedImage && pressedImage.GetParent() )
246       {
247         StopFadeOutAnimation( pushButton );
248         FadeOutImage( pushButton, Foreground, pressedImage  );
249
250         pressedImage = image;
251
252         FadeInImage( pushButton, pressedImage );
253
254         StartFadeOutAnimation( pushButton );
255         StartFadeInAnimation();
256       }
257       else
258       {
259         pressedImage = image;
260         pushButton.Add( pressedImage );
261       }
262       break;
263     }
264     case PressedReleasedTransition: // FALLTHROUGH
265     case PressedDimmedTransition:
266     {
267       float opacity = 1.f;
268       if( fadeOutButtonImage )
269       {
270         opacity = fadeOutButtonImage.GetCurrentOpacity();
271       }
272       StopFadeOutAnimation( pushButton );
273
274       // Replaces the button image.
275       pressedImage = image;
276
277       pushButton.Add( pressedImage );
278       FadeOutImage( pushButton, Foreground, pressedImage, opacity );
279
280       StartFadeOutAnimation( pushButton );
281       break;
282     }
283     case ReleasedPressedTransition: // FALLTHROUGH
284     case DimmedPressedTransition:
285     {
286       StopFadeInAnimation();
287       pushButton.Remove( pressedImage );
288
289       pressedImage = image;
290
291       FadeInImage( pushButton, pressedImage );
292       StartFadeInAnimation();
293       break;
294     }
295     default:
296       pressedImage = image;
297       break;
298   }
299
300   pressedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
301   pressedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
302   ApplyConstraint( pressedImage, FOREGROUND_DEPTH );
303 }
304
305 void PushButtonDefaultPainter::SetDimmedImage( Toolkit::PushButton& pushButton, Actor image )
306 {
307   Toolkit::Internal::PushButton& pushButtonImpl = GetImplementation( pushButton );
308   Actor& dimmedImage = pushButtonImpl.GetDimmedImage();
309   Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
310
311   switch( mPaintState )
312   {
313     case DimmedReleasedState: // FALLTHROUGH
314     case DimmedPressedState:
315     {
316       if( dimmedImage && dimmedImage.GetParent() )
317       {
318         StopFadeOutAnimation( pushButton );
319         FadeOutImage( pushButton, Foreground, dimmedImage  );
320
321         dimmedImage = image;
322
323         FadeInImage( pushButton, dimmedImage );
324
325         StartFadeOutAnimation( pushButton );
326         StartFadeInAnimation();
327       }
328       else
329       {
330         dimmedImage = image;
331         pushButton.Add( dimmedImage );
332       }
333       break;
334     }
335     case ReleasedDimmedTransition: // FALLTHROUGH
336     case PressedDimmedTransition:
337     {
338       StopFadeInAnimation();
339       pushButton.Remove( dimmedImage );
340
341       dimmedImage = image;
342
343       FadeInImage( pushButton, dimmedImage );
344       StartFadeInAnimation();
345       break;
346     }
347     case DimmedReleasedTransition: // FALLTHROUGH
348     case DimmedPressedTransition:
349     {
350       float opacity = 1.f;
351       if( fadeOutButtonImage )
352       {
353         opacity = fadeOutButtonImage.GetCurrentOpacity();
354       }
355       StopFadeOutAnimation( pushButton );
356
357       // Replaces the button image.
358       dimmedImage = image;
359
360       pushButton.Add( dimmedImage );
361       FadeOutImage( pushButton, Foreground, dimmedImage, opacity );
362
363       StartFadeOutAnimation( pushButton );
364       break;
365     }
366     default:
367       dimmedImage = image;
368       break;
369   }
370
371   dimmedImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
372   dimmedImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
373   ApplyConstraint( dimmedImage, FOREGROUND_DEPTH );
374 }
375
376 void PushButtonDefaultPainter::SetDimmedBackgroundImage( Toolkit::PushButton& pushButton, Actor image )
377 {
378   Toolkit::Internal::PushButton& pushButtonImpl = GetImplementation( pushButton );
379   Actor& dimmedBackgroundImage = pushButtonImpl.GetDimmedBackgroundImage();
380   Actor& fadeOutBackgroundImage = pushButtonImpl.GetFadeOutBackgroundImage();
381
382   switch( mPaintState )
383   {
384     case DimmedReleasedState: // FALLTHROUGH
385     case DimmedPressedState:
386     {
387       if( dimmedBackgroundImage && dimmedBackgroundImage.GetParent() )
388       {
389         StopFadeOutAnimation( pushButton );
390         FadeOutImage( pushButton, Background, dimmedBackgroundImage  );
391
392         dimmedBackgroundImage = image;
393
394         FadeInImage( pushButton, dimmedBackgroundImage );
395
396         StartFadeOutAnimation( pushButton );
397         StartFadeInAnimation();
398       }
399       else
400       {
401         dimmedBackgroundImage = image;
402         pushButton.Add( dimmedBackgroundImage );
403       }
404       break;
405     }
406     case ReleasedDimmedTransition: // FALLTHROUGH
407     case PressedDimmedTransition:
408     {
409       StopFadeInAnimation();
410       pushButton.Remove( dimmedBackgroundImage );
411
412       dimmedBackgroundImage = image;
413
414       FadeInImage( pushButton, dimmedBackgroundImage );
415       StartFadeInAnimation();
416       break;
417     }
418     case DimmedReleasedTransition: // FALLTHROUGH
419     case DimmedPressedTransition:
420     {
421       float opacity = 1.f;
422       if( fadeOutBackgroundImage )
423       {
424         opacity = fadeOutBackgroundImage.GetCurrentOpacity();
425       }
426       StopFadeOutAnimation( pushButton );
427
428       // Replaces the button image.
429       dimmedBackgroundImage = image;
430
431       pushButton.Add( dimmedBackgroundImage );
432       FadeOutImage( pushButton, Background, dimmedBackgroundImage, opacity );
433
434       StartFadeOutAnimation( pushButton );
435       break;
436     }
437     default:
438       dimmedBackgroundImage = image;
439       break;
440   }
441
442   dimmedBackgroundImage.SetAnchorPoint( AnchorPoint::TOP_LEFT );
443   dimmedBackgroundImage.SetParentOrigin( ParentOrigin::TOP_LEFT );
444   ApplyConstraint( dimmedBackgroundImage, BACKGROUND_DEPTH );
445 }
446
447 void PushButtonDefaultPainter::SetLabelText( Toolkit::PushButton& pushButton, Actor text )
448 {
449   Toolkit::Internal::PushButton& pushButtonImpl = GetImplementation( pushButton );
450   Actor& label = pushButtonImpl.GetLabel();
451
452   if( label && label.GetParent() )
453   {
454     label.GetParent().Remove( label );
455   }
456
457   label = text;
458   label.SetAnchorPoint( AnchorPoint::CENTER );
459   label.SetParentOrigin( ParentOrigin::CENTER );
460
461   label.SetPosition( 0.f, 0.f, LABEL_DEPTH );
462   label.SetSize( mSize );
463
464   pushButton.Add( label  );
465 }
466
467 void PushButtonDefaultPainter::Initialize( Toolkit::Button& button )
468 {
469   Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( button );
470   Actor& buttonImage = pushButtonImpl.GetButtonImage();
471   Actor& pressedImage = pushButtonImpl.GetPressedImage();
472   Actor& backgroundImage = pushButtonImpl.GetBackgroundImage();
473   Actor& dimmedImage = pushButtonImpl.GetDimmedImage();
474   Actor& dimmedBackgroundImage = pushButtonImpl.GetDimmedBackgroundImage();
475   Actor& label = pushButtonImpl.GetLabel();
476
477   Toolkit::PushButton& pushButton = static_cast<Toolkit::PushButton&>( button );
478
479   if( buttonImage )
480   {
481     SetButtonImage( pushButton, buttonImage );
482   }
483
484   if( backgroundImage )
485   {
486     SetBackgroundImage( pushButton, backgroundImage );
487   }
488
489   if( pressedImage )
490   {
491     SetPressedImage( pushButton, pressedImage );
492   }
493
494   if( dimmedImage )
495   {
496     SetDimmedImage( pushButton, dimmedImage );
497   }
498
499   if( dimmedBackgroundImage )
500   {
501     SetDimmedBackgroundImage( pushButton, dimmedBackgroundImage );
502   }
503
504   if( label )
505   {
506     SetLabelText( pushButton, label );
507   }
508
509   SetDimmed( pushButton, mDimmed );
510 }
511
512 void PushButtonDefaultPainter::SetSize( Toolkit::Button& button, const Vector3& size )
513 {
514   if( size != mSize )
515   {
516     mSize = size;
517
518     Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( button );
519     Actor& buttonImage = pushButtonImpl.GetButtonImage();
520     Actor& pressedImage = pushButtonImpl.GetPressedImage();
521     Actor& backgroundImage = pushButtonImpl.GetBackgroundImage();
522     Actor& dimmedImage = pushButtonImpl.GetDimmedImage();
523     Actor& dimmedBackgroundImage = pushButtonImpl.GetDimmedBackgroundImage();
524     Actor& label = pushButtonImpl.GetLabel();
525
526     ApplyConstraint( buttonImage, FOREGROUND_DEPTH );
527     ApplyConstraint( backgroundImage, BACKGROUND_DEPTH );
528     ApplyConstraint( pressedImage, FOREGROUND_DEPTH );
529     ApplyConstraint( dimmedImage, FOREGROUND_DEPTH );
530     ApplyConstraint( dimmedBackgroundImage, BACKGROUND_DEPTH );
531
532     if( label )
533     {
534       label.SetPosition( 0.f, 0.f, LABEL_DEPTH );
535       label.SetSize( mSize );
536     }
537   }
538 }
539
540 void PushButtonDefaultPainter::SetDimmed( Toolkit::Button& button, bool dimmed )
541 {
542   Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( button );
543   Actor& buttonImage = pushButtonImpl.GetButtonImage();
544   Actor& pressedImage = pushButtonImpl.GetPressedImage();
545   Actor& backgroundImage = pushButtonImpl.GetBackgroundImage();
546   Actor& dimmedImage = pushButtonImpl.GetDimmedImage();
547   Actor& dimmedBackgroundImage = pushButtonImpl.GetDimmedBackgroundImage();
548   Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
549
550   Toolkit::PushButton& pushButton = static_cast<Toolkit::PushButton&>( button );
551
552   mDimmed = dimmed;
553
554   switch( mPaintState )
555   {
556   case ReleasedState:
557   {
558     if( dimmed )
559     {
560       StopFadeOutAnimation( pushButton );
561       FadeOutImage( pushButton, Background, backgroundImage );
562       FadeOutImage( pushButton, Foreground, buttonImage );
563       FadeInImage( pushButton, dimmedBackgroundImage );
564       FadeInImage( pushButton, dimmedImage );
565       StartFadeOutAnimation( pushButton );
566       StartFadeInAnimation();
567
568       if( buttonImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
569       {
570         mPaintState = ReleasedDimmedTransition;
571       }
572       else
573       {
574         mPaintState = DimmedReleasedState;
575       }
576     }
577     break;
578   }
579   case PressedState:
580   {
581     if( dimmed )
582     {
583       StopFadeOutAnimation( pushButton );
584       FadeOutImage( pushButton, Background, backgroundImage );
585       FadeOutImage( pushButton, Foreground, pressedImage );
586       FadeInImage( pushButton, dimmedBackgroundImage );
587       FadeInImage( pushButton, dimmedImage );
588       StartFadeOutAnimation( pushButton );
589       StartFadeInAnimation();
590
591       if( pressedImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
592       {
593         mPaintState = PressedDimmedTransition;
594       }
595       else
596       {
597         mPaintState = DimmedPressedState;
598       }
599     }
600     break;
601   }
602   case DimmedReleasedState:
603   {
604     if( !dimmed )
605     {
606       StopFadeOutAnimation( pushButton );
607       FadeOutImage( pushButton, Background, dimmedBackgroundImage );
608       FadeOutImage( pushButton, Foreground, dimmedImage );
609       FadeInImage( pushButton, backgroundImage );
610       FadeInImage( pushButton, buttonImage );
611       StartFadeOutAnimation( pushButton );
612       StartFadeInAnimation();
613
614       if( buttonImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
615       {
616         mPaintState = DimmedReleasedTransition;
617       }
618       else
619       {
620         mPaintState = ReleasedState;
621       }
622     }
623     break;
624   }
625   case DimmedPressedState:
626   {
627     if( !dimmed )
628     {
629       StopFadeOutAnimation( pushButton );
630       FadeOutImage( pushButton, Background, dimmedBackgroundImage );
631       FadeOutImage( pushButton, Foreground, dimmedImage );
632       FadeInImage( pushButton, backgroundImage );
633       FadeInImage( pushButton, pressedImage );
634       StartFadeOutAnimation( pushButton );
635       StartFadeInAnimation();
636
637       if( pressedImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
638       {
639         mPaintState = DimmedPressedTransition;
640       }
641       else
642       {
643         mPaintState = PressedState;
644       }
645     }
646     break;
647   }
648   case ReleasedPressedTransition:
649   {
650     if( dimmed )
651     {
652       float opacity = 1.f;
653       if( fadeOutButtonImage )
654       {
655         opacity = fadeOutButtonImage.GetCurrentOpacity();
656       }
657       StopFadeOutAnimation( pushButton );
658       StopFadeInAnimation();
659
660       FadeOutImage( pushButton, Foreground, pressedImage, 1.f - opacity );
661       FadeOutImage( pushButton, Background, backgroundImage );
662
663       FadeInImage( pushButton, dimmedImage );
664       FadeInImage( pushButton, dimmedBackgroundImage );
665
666       StartFadeOutAnimation( pushButton );
667       StartFadeInAnimation();
668
669       if( pressedImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
670       {
671         mPaintState = PressedDimmedTransition;
672       }
673       else
674       {
675         mPaintState = DimmedPressedState;
676       }
677     }
678     break;
679   }
680   case PressedReleasedTransition:
681   {
682     if( dimmed )
683     {
684       float opacity = 1.f;
685       if( fadeOutButtonImage )
686       {
687         opacity = fadeOutButtonImage.GetCurrentOpacity();
688       }
689       StopFadeOutAnimation( pushButton );
690       StopFadeInAnimation();
691
692       FadeOutImage( pushButton, Foreground, buttonImage, 1.f - opacity );
693       FadeOutImage( pushButton, Background, backgroundImage );
694
695       FadeInImage( pushButton, dimmedImage );
696       FadeInImage( pushButton, dimmedBackgroundImage );
697
698       StartFadeOutAnimation( pushButton );
699       StartFadeInAnimation();
700
701       if( buttonImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
702       {
703         mPaintState = ReleasedDimmedTransition;
704       }
705       else
706       {
707         mPaintState = DimmedReleasedState;
708       }
709     }
710     break;
711   }
712   case ReleasedDimmedTransition:
713   {
714     if( !dimmed )
715     {
716       float opacity = 1.f;
717       if( fadeOutButtonImage )
718       {
719         opacity = fadeOutButtonImage.GetCurrentOpacity();
720       }
721       StopFadeOutAnimation( pushButton, false );
722       StopFadeInAnimation();
723
724       FadeOutImage( pushButton, Foreground, dimmedImage, 1.f - opacity );
725       FadeOutImage( pushButton, Background, dimmedBackgroundImage, 1.f - opacity );
726       FadeInImage( pushButton, buttonImage, opacity );
727       FadeInImage( pushButton, backgroundImage, opacity );
728
729       StartFadeOutAnimation( pushButton );
730       StartFadeInAnimation();
731
732       if( buttonImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
733       {
734         mPaintState = DimmedReleasedTransition;
735       }
736       else
737       {
738         mPaintState = ReleasedState;
739       }
740     }
741     break;
742   }
743   case DimmedReleasedTransition:
744   {
745     if( dimmed )
746     {
747       float opacity = 1.f;
748       if( fadeOutButtonImage )
749       {
750         opacity = fadeOutButtonImage.GetCurrentOpacity();
751       }
752       StopFadeOutAnimation( pushButton, false );
753       StopFadeInAnimation();
754
755       FadeOutImage( pushButton, Foreground, buttonImage, 1.f - opacity );
756       FadeOutImage( pushButton, Background, backgroundImage, 1.f - opacity );
757       FadeInImage( pushButton, dimmedImage, opacity );
758       FadeInImage( pushButton, dimmedBackgroundImage, opacity );
759
760       StartFadeOutAnimation( pushButton );
761       StartFadeInAnimation();
762
763       if( buttonImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
764       {
765         mPaintState = ReleasedDimmedTransition;
766       }
767       else
768       {
769         mPaintState = DimmedReleasedState;
770       }
771     }
772     break;
773   }
774   case PressedDimmedTransition:
775   {
776     if( !dimmed )
777     {
778       float opacity = 1.f;
779       if( fadeOutButtonImage )
780       {
781         opacity = fadeOutButtonImage.GetCurrentOpacity();
782       }
783       StopFadeOutAnimation( pushButton, false );
784       StopFadeInAnimation();
785
786       FadeOutImage( pushButton, Foreground, dimmedImage, 1.f - opacity );
787       FadeOutImage( pushButton, Background, dimmedBackgroundImage, 1.f - opacity );
788       FadeInImage( pushButton, pressedImage, opacity );
789       FadeInImage( pushButton, backgroundImage, opacity );
790
791       StartFadeOutAnimation( pushButton );
792       StartFadeInAnimation();
793
794       if( pressedImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
795       {
796         mPaintState = DimmedPressedTransition;
797       }
798       else
799       {
800         mPaintState = PressedState;
801       }
802     }
803     break;
804   }
805   case DimmedPressedTransition:
806   {
807     if( dimmed )
808     {
809       float opacity = 1.f;
810       if( fadeOutButtonImage )
811       {
812         opacity = fadeOutButtonImage.GetCurrentOpacity();
813       }
814       StopFadeOutAnimation( pushButton, false );
815       StopFadeInAnimation();
816
817       FadeOutImage( pushButton, Foreground, pressedImage, 1.f - opacity );
818       FadeOutImage( pushButton, Background, backgroundImage, 1.f - opacity );
819       FadeInImage( pushButton, dimmedImage, opacity );
820       FadeInImage( pushButton, dimmedBackgroundImage, opacity );
821
822       StartFadeOutAnimation( pushButton );
823       StartFadeInAnimation();
824
825       if( pressedImage || dimmedImage || backgroundImage || dimmedBackgroundImage )
826       {
827         mPaintState = PressedDimmedTransition;
828       }
829       else
830       {
831         mPaintState = DimmedPressedState;
832       }
833     }
834     break;
835   }
836   default:
837     break;
838   }
839 }
840
841 void PushButtonDefaultPainter::SetAnimationTime( float animationTime )
842 {
843   mAnimationTime = animationTime;
844 }
845
846 float PushButtonDefaultPainter::GetAnimationTime() const
847 {
848   return mAnimationTime;
849 }
850
851 void PushButtonDefaultPainter::SetAutoRepeating( bool autorepeating )
852 {
853   mAutoRepeating = autorepeating;
854 }
855
856 void PushButtonDefaultPainter::Pressed( Toolkit::PushButton& button )
857 {
858   Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( button );
859   Actor& pressedImage = pushButtonImpl.GetPressedImage();
860   Actor& buttonImage = pushButtonImpl.GetButtonImage();
861   Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
862
863   switch( mPaintState )
864   {
865     case ReleasedState:
866     {
867       StopFadeOutAnimation( button );
868       FadeOutImage( button, Foreground, buttonImage );
869       FadeInImage( button, pressedImage );
870       StartFadeOutAnimation( button );
871       StartFadeInAnimation();
872
873       if( buttonImage || pressedImage )
874       {
875         mPaintState = ReleasedPressedTransition;
876       }
877       else
878       {
879         mPaintState = PressedState;
880       }
881       break;
882     }
883     case ReleasedPressedTransition:
884     {
885       if( !mAutoRepeating )
886       {
887         mPaintState = PressedReleasedTransition;
888       }
889       break;
890     }
891     case PressedReleasedTransition:
892     {
893       float opacity = 1.f;
894       if( fadeOutButtonImage )
895       {
896         opacity = fadeOutButtonImage.GetCurrentOpacity();
897       }
898       StopFadeOutAnimation( button, false );
899       StopFadeInAnimation();
900
901       FadeOutImage( button, Foreground, buttonImage, 1.f - opacity );
902       FadeInImage( button, pressedImage, opacity );
903
904       StartFadeOutAnimation( button );
905       StartFadeInAnimation();
906
907       if( buttonImage || pressedImage )
908       {
909         mPaintState = ReleasedPressedTransition;
910       }
911       else
912       {
913         mPaintState = PressedState;
914       }
915       break;
916     }
917     default:
918       break;
919   }
920 }
921
922 void PushButtonDefaultPainter::Released( Toolkit::PushButton& button )
923 {
924   Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( button );
925   Actor& pressedImage = pushButtonImpl.GetPressedImage();
926   Actor& buttonImage = pushButtonImpl.GetButtonImage();
927   Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
928
929   switch( mPaintState )
930   {
931     case PressedState:
932     {
933       StopFadeOutAnimation( button );
934       FadeOutImage( button, Foreground, pressedImage );
935       FadeInImage( button, buttonImage );
936       StartFadeOutAnimation( button );
937       StartFadeInAnimation();
938
939       if( buttonImage || pressedImage )
940       {
941         mPaintState = PressedReleasedTransition;
942       }
943       else
944       {
945         mPaintState = ReleasedState;
946       }
947       break;
948     }
949     case ReleasedPressedTransition:
950     {
951       float opacity = 1.f;
952       if( fadeOutButtonImage )
953       {
954         opacity = fadeOutButtonImage.GetCurrentOpacity();
955       }
956       StopFadeOutAnimation( button, false );
957       StopFadeInAnimation();
958
959       FadeOutImage( button, Foreground, pressedImage, 1.f - opacity );
960       FadeInImage( button, buttonImage, opacity );
961
962       StartFadeOutAnimation( button );
963       StartFadeInAnimation();
964
965       if( buttonImage || pressedImage )
966       {
967         mPaintState = PressedReleasedTransition;
968       }
969       else
970       {
971         mPaintState = ReleasedState;
972       }
973       break;
974     }
975     default:
976     {
977       break;
978     }
979   }
980 }
981
982 void PushButtonDefaultPainter::Clicked( Toolkit::PushButton& button )
983 {
984   Released( button );
985 }
986
987 void PushButtonDefaultPainter::Toggled( Toolkit::PushButton& button )
988 {
989   Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( button );
990   Actor& pressedImage = pushButtonImpl.GetPressedImage();
991   Actor& buttonImage = pushButtonImpl.GetButtonImage();
992   Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
993
994   switch( mPaintState )
995   {
996     case ReleasedState:
997     {
998       StopFadeOutAnimation( button );
999       FadeOutImage( button, Foreground, buttonImage );
1000       FadeInImage( button, pressedImage );
1001       StartFadeOutAnimation( button );
1002       StartFadeInAnimation();
1003
1004       if( buttonImage || pressedImage )
1005       {
1006         mPaintState = ReleasedPressedTransition;
1007       }
1008       else
1009       {
1010         mPaintState = PressedState;
1011       }
1012       break;
1013     }
1014     case PressedState:
1015     {
1016       StopFadeOutAnimation( button );
1017       FadeOutImage( button, Foreground, pressedImage );
1018       FadeInImage( button, buttonImage );
1019       StartFadeOutAnimation( button );
1020       StartFadeInAnimation();
1021
1022       if( buttonImage || pressedImage )
1023       {
1024         mPaintState = PressedReleasedTransition;
1025       }
1026       else
1027       {
1028         mPaintState = ReleasedState;
1029       }
1030       break;
1031     }
1032     case ReleasedPressedTransition:
1033     {
1034       float opacity = 1.f;
1035       if( fadeOutButtonImage )
1036       {
1037         opacity = fadeOutButtonImage.GetCurrentOpacity();
1038       }
1039       StopFadeOutAnimation( button, false );
1040       StopFadeInAnimation();
1041
1042       FadeOutImage( button, Foreground, pressedImage, 1.f - opacity );
1043       FadeInImage( button, buttonImage, opacity );
1044
1045       StartFadeOutAnimation( button );
1046       StartFadeInAnimation();
1047
1048       if( buttonImage || pressedImage )
1049       {
1050         mPaintState = PressedReleasedTransition;
1051       }
1052       else
1053       {
1054         mPaintState = ReleasedState;
1055       }
1056       break;
1057     }
1058     case PressedReleasedTransition:
1059     {
1060       float opacity = 0.f;
1061       if( fadeOutButtonImage )
1062       {
1063         opacity = 1.f - fadeOutButtonImage.GetCurrentOpacity();
1064       }
1065       StopFadeOutAnimation( button, false );
1066       StopFadeInAnimation();
1067
1068       FadeOutImage( button, Foreground, buttonImage, 1.f - opacity );
1069       FadeInImage( button, pressedImage, opacity );
1070
1071       StartFadeOutAnimation( button );
1072       StartFadeInAnimation();
1073
1074       if( buttonImage || pressedImage )
1075       {
1076         mPaintState = ReleasedPressedTransition;
1077       }
1078       else
1079       {
1080         mPaintState = PressedState;
1081       }
1082       break;
1083     }
1084     default:
1085     {
1086       break;
1087     }
1088   }
1089 }
1090
1091 void PushButtonDefaultPainter::ApplyConstraint( Actor& actor, float depth )
1092 {
1093   if( actor )
1094   {
1095     actor.SetPosition( 0.f, 0.f, depth );
1096     actor.RemoveConstraints();
1097     actor.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
1098   }
1099 }
1100
1101 void PushButtonDefaultPainter::AddToFadeInAnimation( const Actor& actor )
1102 {
1103   if( !mFadeInAnimation )
1104   {
1105     mFadeInAnimation = Dali::Animation::New( mAnimationTime );
1106   }
1107
1108   mFadeInAnimation.OpacityTo( actor, 1.f );
1109 }
1110
1111 void PushButtonDefaultPainter::StartFadeInAnimation()
1112 {
1113   if( mFadeInAnimation )
1114   {
1115     mFadeInAnimation.FinishedSignal().Connect( this, &PushButtonDefaultPainter::PressedReleasedFadeInAnimationFinished );
1116     mFadeInAnimation.Play();
1117   }
1118 }
1119
1120 void PushButtonDefaultPainter::StopFadeInAnimation()
1121 {
1122   if( mFadeInAnimation )
1123   {
1124     mFadeInAnimation.Clear();
1125     mFadeInAnimation.Reset();
1126   }
1127 }
1128
1129 void PushButtonDefaultPainter::AddToFadeOutAnimation( const Actor& actor )
1130 {
1131   if( !mFadeOutAnimation )
1132   {
1133     mFadeOutAnimation = Dali::Animation::New( mAnimationTime );
1134   }
1135
1136   mFadeOutAnimation.OpacityTo( actor, 0.f );
1137 }
1138
1139 void PushButtonDefaultPainter::StartFadeOutAnimation( Toolkit::PushButton& pushButton )
1140 {
1141   if( mFadeOutAnimation )
1142   {
1143     Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( pushButton );
1144     mButton = &pushButtonImpl;
1145
1146     mFadeOutAnimation.FinishedSignal().Connect( this, &PushButtonDefaultPainter::PressedReleasedFadeOutAnimationFinished );
1147     mFadeOutAnimation.Play();
1148   }
1149 }
1150
1151 void PushButtonDefaultPainter::StopFadeOutAnimation( Toolkit::PushButton& pushButton, bool remove )
1152 {
1153   if( mFadeOutAnimation )
1154   {
1155     mFadeOutAnimation.Clear();
1156   }
1157
1158   mFadeOutAnimation.Reset();
1159
1160   if( remove )
1161   {
1162     Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( pushButton );
1163     Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
1164     Actor& fadeOutBackgroundImage = pushButtonImpl.GetFadeOutBackgroundImage();
1165
1166     if( fadeOutButtonImage && fadeOutButtonImage.GetParent() )
1167     {
1168       fadeOutButtonImage.GetParent().Remove( fadeOutButtonImage );
1169     }
1170
1171     if( fadeOutBackgroundImage && fadeOutBackgroundImage.GetParent() )
1172     {
1173       fadeOutBackgroundImage.GetParent().Remove( fadeOutBackgroundImage );
1174     }
1175
1176     fadeOutButtonImage.Reset();
1177     fadeOutBackgroundImage.Reset();
1178   }
1179 }
1180
1181 void PushButtonDefaultPainter::FadeInImage( Toolkit::PushButton& pushButton, Actor& image, float opacity )
1182 {
1183   if( image )
1184   {
1185     image.SetOpacity( opacity );
1186     if( !image.GetParent() )
1187     {
1188       pushButton.Add( image );
1189     }
1190
1191     AddToFadeInAnimation( image );
1192   }
1193 }
1194
1195 void PushButtonDefaultPainter::FadeOutImage( Toolkit::PushButton& pushButton, ImageLayer layer, Actor& image, float opacity )
1196 {
1197   if( image )
1198   {
1199     Toolkit::Internal::PushButton& pushButtonImpl = GetPushButtonImpl( pushButton );
1200     Actor& fadeOutButtonImage = pushButtonImpl.GetFadeOutButtonImage();
1201     Actor& fadeOutBackgroundImage = pushButtonImpl.GetFadeOutBackgroundImage();
1202
1203     Actor& actorLayer = ( ( Background == layer ) ? fadeOutBackgroundImage : fadeOutButtonImage );
1204
1205     actorLayer = image;
1206     actorLayer.SetOpacity( opacity );
1207
1208     AddToFadeOutAnimation( actorLayer );
1209   }
1210 }
1211
1212 void PushButtonDefaultPainter::PressedReleasedFadeOutAnimationFinished( Dali::Animation& source )
1213 {
1214   switch( mPaintState )
1215   {
1216     case ReleasedPressedTransition:
1217     {
1218       mPaintState = PressedState;
1219       break;
1220     }
1221     case PressedReleasedTransition:
1222     {
1223       mPaintState = ReleasedState;
1224       break;
1225     }
1226     case ReleasedDimmedTransition:
1227     {
1228       mPaintState = DimmedReleasedState;
1229       break;
1230     }
1231     case DimmedReleasedTransition:
1232     {
1233       mPaintState = ReleasedState;
1234       break;
1235     }
1236     case PressedDimmedTransition:
1237     {
1238       mPaintState = DimmedPressedState;
1239       break;
1240     }
1241     case DimmedPressedTransition:
1242     {
1243       mPaintState = PressedState;
1244       break;
1245     }
1246     default:
1247     {
1248       break;
1249     }
1250   }
1251
1252   Toolkit::PushButton handle( mButton->GetOwner() );
1253   StopFadeOutAnimation( handle );
1254   mButton = NULL;
1255 }
1256
1257 void PushButtonDefaultPainter::PressedReleasedFadeInAnimationFinished( Dali::Animation& source )
1258 {
1259   switch( mPaintState )
1260   {
1261     case ReleasedPressedTransition:
1262     {
1263       mPaintState = PressedState;
1264       break;
1265     }
1266     case PressedReleasedTransition:
1267     {
1268       mPaintState = ReleasedState;
1269       break;
1270     }
1271     case ReleasedDimmedTransition:
1272     {
1273       mPaintState = DimmedReleasedState;
1274       break;
1275     }
1276     case DimmedReleasedTransition:
1277     {
1278       mPaintState = ReleasedState;
1279       break;
1280     }
1281     case PressedDimmedTransition:
1282     {
1283       mPaintState = DimmedPressedState;
1284       break;
1285     }
1286     case DimmedPressedTransition:
1287     {
1288       mPaintState = PressedState;
1289       break;
1290     }
1291     default:
1292     {
1293       break;
1294     }
1295   }
1296
1297   StopFadeInAnimation();
1298 }
1299
1300 } // namespace Internal
1301
1302 } // namespace Toolkit
1303
1304 } // namespace Dali