Updated control transitions to work with visual transform
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / check-box-button-impl.cpp
1 /*
2  * Copyright (c) 2016 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 #include <dali-toolkit/devel-api/shader-effects/image-region-effect.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace
41 {
42
43 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
44 const float ANIMATION_TIME( 0.26f );  // EFL checkbox tick time
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::SetTickUVEffect()
87 {
88   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( mSelectedImage );
89   if( imageView )
90   {
91     imageView.RegisterProperty( "uTextureRect", Vector4(0.f, 0.f, 1.f, 1.f ) );
92     imageView.RegisterProperty( "uTopLeft", Vector2::ZERO );
93
94     Property::Map shaderMap = CreateImageRegionEffect();
95     imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, shaderMap );
96
97     GetImpl( imageView ).SetDepthIndex( DepthIndex::DECORATION );
98   }
99 }
100
101 void CheckBoxButton::OnInitialize()
102 {
103   Button::OnInitialize();
104
105   // Wrap around all children
106   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
107
108   SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
109   SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
110   SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
111   SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
112
113   mSelectedImage = GetSelectedImage();
114   SetTickUVEffect();
115 }
116
117 void CheckBoxButton::OnLabelSet( bool noPadding )
118 {
119   Actor& label = GetLabelActor();
120
121   if( label )
122   {
123     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
124     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
125
126     if( IsDisabled() && GetDisabledBackgroundImage() )
127     {
128       label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
129     }
130     else if ( GetBackgroundImage() )
131     {
132       label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
133     }
134     else if( IsSelected() && GetSelectedImage())
135     {
136       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
137     }
138     else if( GetUnselectedImage() )
139     {
140       label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
141     }
142     else
143     {
144       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
145     }
146   }
147 }
148
149 void CheckBoxButton::OnDisabled()
150 {
151   Actor& backgroundImage = GetBackgroundImage();
152   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
153
154   Actor& label = GetLabelActor();
155   if( label )
156   {
157     if( IsDisabled() && disabledBackgroundImage )
158     {
159       label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
160     }
161     else if( backgroundImage )
162     {
163       label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
164     }
165     else if( IsSelected() && GetSelectedImage())
166     {
167       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
168     }
169     else if( GetUnselectedImage() )
170     {
171       label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
172     }
173     else
174     {
175       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
176     }
177   }
178 }
179
180 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
181 {
182   Actor& selectedImage = GetSelectedImage();
183   if( actor == selectedImage )
184   {
185     actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
186     actor.RegisterProperty( "uBottomRight", Vector2( 0.0f, 1.0f ) );
187
188     if( mSelectedImage != selectedImage )
189     {
190       mSelectedImage = selectedImage;
191       SetTickUVEffect();
192     }
193   }
194 }
195
196 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
197 {
198   Actor& selectedImage = GetSelectedImage();
199   if( actor == selectedImage )
200   {
201     actor.SetScale( Vector3::ONE );
202     actor.RegisterProperty( "uBottomRight", Vector2::ONE );
203
204     if( mSelectedImage != selectedImage )
205     {
206       mSelectedImage = selectedImage;
207       SetTickUVEffect();
208     }
209   }
210 }
211
212 void CheckBoxButton::OnTransitionIn( Actor actor )
213 {
214   Actor& selectedImage = GetSelectedImage();
215   if( actor && actor == selectedImage )
216   {
217     if( GetPaintState() == UnselectedState )
218     {
219       Dali::Animation transitionAnimation = GetTransitionAnimation();
220       if( transitionAnimation )
221       {
222         // UV anim
223         transitionAnimation.AnimateTo( Property( actor, "uBottomRight" ), Vector2::ONE );
224
225         // Actor size anim
226         transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
227       }
228     }
229     else
230     {
231       //explicitly end the swipe animation
232       actor.SetScale( Vector3::ONE );
233       if( mSelectedImage == selectedImage  )
234       {
235         actor.RegisterProperty( "uBottomRight", Vector2::ONE );
236       }
237     }
238   }
239 }
240
241 } // namespace Internal
242
243 } // namespace Toolkit
244
245 } // namespace Dali