Adding TextField Popup behaviour to (Programming) Guide
[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 #include <dali/public-api/images/resource-image.h>
25
26 //INTERNAL INCLUDES
27 #include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
42 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
43 // Required for the UV reveal shader to render the tick on top of the rest of the checkbox.
44 const float SHADER_DEPTH_OFFSET = 1.0f;
45
46 BaseHandle Create()
47 {
48   return Toolkit::CheckBoxButton::New();
49 }
50
51 TypeRegistration mType( typeid(Toolkit::CheckBoxButton), typeid(Toolkit::Button), Create );
52
53 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected.png";
54 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected.png";
55 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-unselected-disabled.png";
56 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "checkbox-selected-diabled.png";
57 }
58
59 Dali::Toolkit::CheckBoxButton CheckBoxButton::New()
60 {
61   // Create the implementation, temporarily owned on stack
62   IntrusivePtr< CheckBoxButton > internalCheckBoxButton = new CheckBoxButton();
63
64   // Pass ownership to CustomActor
65   Dali::Toolkit::CheckBoxButton checkBoxButton( *internalCheckBoxButton );
66
67   // Second-phase init of the implementation
68   // This can only be done after the CustomActor connection has been made...
69   internalCheckBoxButton->Initialize();
70
71   return checkBoxButton;
72 }
73
74 CheckBoxButton::CheckBoxButton()
75 : Button()
76 {
77   SetTogglableButton( true );
78
79   SetAnimationTime( ANIMATION_TIME );
80 }
81
82 CheckBoxButton::~CheckBoxButton()
83 {
84 }
85
86 void CheckBoxButton::OnButtonInitialize()
87 {
88   // Wrap around all children
89   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
90
91   SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
92   SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
93   SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
94   SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
95 }
96
97 void CheckBoxButton::OnLabelSet( bool noPadding )
98 {
99   Actor& label = GetLabelActor();
100
101   if( label )
102   {
103     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
104     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
105
106     if( IsDisabled() && GetDisabledBackgroundImage() )
107     {
108       label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
109     }
110     else if ( GetBackgroundImage() )
111     {
112       label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
113     }
114     else if( IsSelected() && GetSelectedImage())
115     {
116       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
117     }
118     else if( GetUnselectedImage() )
119     {
120       label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
121     }
122     else
123     {
124       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
125     }
126   }
127 }
128
129 void CheckBoxButton::OnDisabled()
130 {
131   Actor& backgroundImage = GetBackgroundImage();
132   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
133
134   Actor& label = GetLabelActor();
135   if( label )
136   {
137     if( IsDisabled() && disabledBackgroundImage )
138     {
139       label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
140     }
141     else if( backgroundImage )
142     {
143       label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
144     }
145     else if( IsSelected() && GetSelectedImage())
146     {
147       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
148     }
149     else if( GetUnselectedImage() )
150     {
151       label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
152     }
153     else
154     {
155       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
156     }
157   }
158 }
159
160 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
161 {
162   Actor& selectedImage = GetSelectedImage();
163   if( actor == selectedImage )
164   {
165     actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
166
167     if( !mTickUVEffect )
168     {
169       mTickUVEffect = CreateImageRegionEffect();
170     }
171     mTickUVEffect.SetUniform( "uBottomRight", Vector2( 0.0f, 1.0f ) );
172
173     ImageActor imageActor = ImageActor::DownCast( actor );
174     if( imageActor )
175     {
176       // Ensure the tick effect is rendered above the reset of the checkbox.
177       imageActor.SetSortModifier( SHADER_DEPTH_OFFSET );
178       imageActor.SetShaderEffect( mTickUVEffect );
179     }
180   }
181 }
182
183 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
184 {
185   Actor& selectedImage = GetSelectedImage();
186   if( actor == selectedImage )
187   {
188     actor.SetScale( Vector3::ONE );
189
190     if( !mTickUVEffect )
191     {
192       mTickUVEffect = CreateImageRegionEffect();
193     }
194     mTickUVEffect.SetUniform( "uBottomRight", Vector2::ONE );
195
196     ImageActor imageActor = ImageActor::DownCast( actor );
197     if( imageActor )
198     {
199       imageActor.SetShaderEffect( mTickUVEffect );
200     }
201   }
202 }
203
204 void CheckBoxButton::OnTransitionIn( Actor actor )
205 {
206   Actor& selectedImage = GetSelectedImage();
207   if( actor && actor == selectedImage )
208   {
209     if( GetPaintState() == UnselectedState )
210     {
211       Dali::Animation transitionAnimation = GetTransitionAnimation();
212       if( transitionAnimation )
213       {
214         DALI_ASSERT_DEBUG( mTickUVEffect );
215         if( mTickUVEffect )
216         {
217           // UV anim
218           transitionAnimation.AnimateTo( Property( mTickUVEffect, "uBottomRight" ), Vector2::ONE );
219         }
220         // Actor size anim
221         transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
222       }
223     }
224     else
225     {
226       //explicitly end the swipe animation
227       actor.SetScale( Vector3::ONE );
228       if( mTickUVEffect )
229       {
230         mTickUVEffect.SetUniform( "uBottomRight", Vector2::ONE );
231       }
232     }
233   }
234 }
235
236 } // namespace Internal
237
238 } // namespace Toolkit
239
240 } // namespace Dali