Merge "Changed button to use ImageView instead of ImageActor and enabled custom fragm...
[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/public-api/controls/control-depth-index-ranges.h>
27 #include <dali-toolkit/internal/controls/image-view/image-view-impl.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( DECORATION_DEPTH_INDEX );
97   }
98 }
99
100 void CheckBoxButton::OnButtonInitialize()
101 {
102   // Wrap around all children
103   Self().SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
104
105   SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
106   SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
107   SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
108   SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
109
110   mSelectedImage = GetSelectedImage();
111   SetTickUVEffect();
112 }
113
114 void CheckBoxButton::OnLabelSet( bool noPadding )
115 {
116   Actor& label = GetLabelActor();
117
118   if( label )
119   {
120     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
121     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
122
123     if( IsDisabled() && GetDisabledBackgroundImage() )
124     {
125       label.SetX( GetDisabledBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
126     }
127     else if ( GetBackgroundImage() )
128     {
129       label.SetX( GetBackgroundImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
130     }
131     else if( IsSelected() && GetSelectedImage())
132     {
133       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
134     }
135     else if( GetUnselectedImage() )
136     {
137       label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
138     }
139     else
140     {
141       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
142     }
143   }
144 }
145
146 void CheckBoxButton::OnDisabled()
147 {
148   Actor& backgroundImage = GetBackgroundImage();
149   Actor& disabledBackgroundImage = GetDisabledBackgroundImage();
150
151   Actor& label = GetLabelActor();
152   if( label )
153   {
154     if( IsDisabled() && disabledBackgroundImage )
155     {
156       label.SetX( disabledBackgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
157     }
158     else if( backgroundImage )
159     {
160       label.SetX( backgroundImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
161     }
162     else if( IsSelected() && GetSelectedImage())
163     {
164       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
165     }
166     else if( GetUnselectedImage() )
167     {
168       label.SetX( GetUnselectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
169     }
170     else
171     {
172       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
173     }
174   }
175 }
176
177 void CheckBoxButton::PrepareForTranstionIn( Actor actor )
178 {
179   Actor& selectedImage = GetSelectedImage();
180   if( actor == selectedImage )
181   {
182     actor.SetScale( Vector3( 0.0f, 1.0f, 1.0f ) );
183     actor.RegisterProperty( "uBottomRight", Vector2( 0.0f, 1.0f ) );
184
185     if( mSelectedImage != selectedImage )
186     {
187       mSelectedImage = selectedImage;
188       SetTickUVEffect();
189     }
190   }
191 }
192
193 void CheckBoxButton::PrepareForTranstionOut( Actor actor )
194 {
195   Actor& selectedImage = GetSelectedImage();
196   if( actor == selectedImage )
197   {
198     actor.SetScale( Vector3::ONE );
199     actor.RegisterProperty( "uBottomRight", Vector2::ONE );
200
201     if( mSelectedImage != selectedImage )
202     {
203       mSelectedImage = selectedImage;
204       SetTickUVEffect();
205     }
206   }
207 }
208
209 void CheckBoxButton::OnTransitionIn( Actor actor )
210 {
211   Actor& selectedImage = GetSelectedImage();
212   if( actor && actor == selectedImage )
213   {
214     if( GetPaintState() == UnselectedState )
215     {
216       Dali::Animation transitionAnimation = GetTransitionAnimation();
217       if( transitionAnimation )
218       {
219         // UV anim
220         transitionAnimation.AnimateTo( Property( actor, "uBottomRight" ), Vector2::ONE );
221
222         // Actor size anim
223         transitionAnimation.AnimateTo( Property( actor, Actor::Property::SCALE_X ), 1.0f );
224       }
225     }
226     else
227     {
228       //explicitly end the swipe animation
229       actor.SetScale( Vector3::ONE );
230       if( mSelectedImage == selectedImage  )
231       {
232         actor.RegisterProperty( "uBottomRight", Vector2::ONE );
233       }
234     }
235   }
236 }
237
238 } // namespace Internal
239
240 } // namespace Toolkit
241
242 } // namespace Dali