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