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