Changed Toolkit shader effects to be a static function returning a
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "check-box-button-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/image-actor.h>
23 #include <dali/public-api/object/type-registry.h>
24
25 //INTERNAL INCLUDES
26 #include <dali-toolkit/public-api/shader-effects/image-region-effect.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
41 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
42
43 BaseHandle Create()
44 {
45   return Toolkit::CheckBoxButton::New();
46 }
47
48 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
49
50 }
51
52 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
53 {
54   // Create the implementation, temporarily owned on stack
55   IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
56
57   // Pass ownership to CustomActor
58   Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
59
60   // Second-phase init of the implementation
61   // This can only be done after the CustomActor connection has been made...
62   internalCheckBoxButton->Initialize();
63
64   return checkBoxButton;
65 }
66
67 CheckBoxButton::CheckBoxButton()
68 : Button()
69 {
70   SetTogglableButton( true );
71
72   SetAnimationTime( ANIMATION_TIME );
73 }
74
75 CheckBoxButton::~CheckBoxButton()
76 {
77   if( mTransitionAnimation )
78   {
79     mTransitionAnimation.Clear();
80   }
81 }
82
83 void CheckBoxButton::OnButtonInitialize()
84 {
85   // Wrap around all children
86   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
87 }
88
89 void CheckBoxButton::OnLabelSet()
90 {
91   Actor& label = GetLabel();
92
93   if( label )
94   {
95     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
96     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
97
98     if( IsDisabled() && GetDisabledBackgroundImage() )
99     {
100       label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
101     }
102     else if ( GetBackgroundImage() )
103     {
104       label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
105     }
106     else
107     {
108       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
109     }
110   }
111 }
112
113 bool CheckBoxButton::OnSelected()
114 {
115   Actor& selectedImage = GetSelectedImage();
116
117   PaintState paintState = GetPaintState();
118
119   switch( paintState )
120   {
121     case UnselectedState:
122     {
123       StartTransitionAnimation( selectedImage );
124       break;
125     }
126     case SelectedState:
127     {
128       RemoveChild( selectedImage );
129       break;
130     }
131     case UnselectedSelectedTransition:
132     {
133       StopTransitionAnimation( false );
134       RemoveChild( selectedImage );
135       break;
136     }
137     default:
138     {
139       break;
140     }
141   }
142
143   if( mTransitionAnimation )
144   {
145     return true;
146   }
147
148   return false;
149 }
150
151 bool CheckBoxButton::OnDisabled()
152 {
153   Actor& backgroundImage = GetBackgroundImage();
154   Actor& selectedImage = GetSelectedImage();
155   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
156   Actor& disabledSelectedImage = GetDisabledSelectedImage();
157
158   PaintState paintState = GetPaintState();
159
160   switch( paintState )
161   {
162     case UnselectedState:
163     {
164       RemoveChild( backgroundImage );
165       break;
166     }
167     case SelectedState:
168     {
169       RemoveChild( backgroundImage );
170       RemoveChild( selectedImage );
171       break;
172     }
173     case DisabledUnselectedState:
174     {
175       RemoveChild( disabledBackgroundImage );
176       break;
177     }
178     case DisabledSelectedState:
179     {
180       RemoveChild( disabledBackgroundImage );
181       RemoveChild( disabledSelectedImage );
182       break;
183     }
184     case UnselectedSelectedTransition:
185     {
186       StopTransitionAnimation();
187
188       RemoveChild( backgroundImage );
189       RemoveChild( selectedImage );
190       break;
191     }
192     default:
193     {
194       break;
195     }
196   }
197
198   Actor& label = GetLabel();
199
200   if( label )
201   {
202     if( IsDisabled() && disabledBackgroundImage)
203     {
204       label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
205     }
206     else if( backgroundImage )
207     {
208       label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
209     }
210     else
211     {
212       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
213     }
214   }
215
216   if( mTransitionAnimation )
217   {
218     return true;
219   }
220
221   return false;
222 }
223
224 void CheckBoxButton::StopAllAnimations()
225 {
226   StopTransitionAnimation();
227 }
228
229 void CheckBoxButton::StartTransitionAnimation( Actor& actor )
230 {
231   if( actor )
232   {
233     if( !mTickUVEffect )
234     {
235       ImageActor imageActor = ImageActor::DownCast( actor );
236       mTickUVEffect = CreateImageRegionEffect();
237       imageActor.SetShaderEffect( mTickUVEffect );
238     }
239
240     actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
241
242     mTickUVEffect.SetUniform("uBottomRight", Vector2( 0.0f, 1.0f ) );
243
244     if( !mTransitionAnimation )
245     {
246       mTransitionAnimation = Dali::Animation::New( GetAnimationTime()  );
247     }
248
249     // UV anim
250     mTransitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2( 1.0f, 1.0f ) );
251
252     // Actor size anim
253     mTransitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
254
255     mTransitionAnimation.FinishedSignal().Connect( this, &CheckBoxButton::TransitionAnimationFinished );
256     mTransitionAnimation.Play();
257   }
258 }
259
260 void CheckBoxButton::StopTransitionAnimation( bool remove )
261 {
262   if( mTransitionAnimation )
263   {
264     mTransitionAnimation.Clear();
265     mTransitionAnimation.Reset();
266   }
267
268   if( remove )
269   {
270     UpdatePaintTransitionState();
271   }
272 }
273
274 void CheckBoxButton::TransitionAnimationFinished( Dali::Animation& source )
275 {
276   StopTransitionAnimation();
277 }
278
279 } // namespace Internal
280
281 } // namespace Toolkit
282
283 } // namespace Dali