Merge "Refactored Button and derived classes, moving state change and transition...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-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 "push-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/public-api/controls/text-controls/text-label.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 const float TEXT_PADDING = 12.0f;
42 const float ANIMATION_TIME( 0.2f );
43
44 BaseHandle Create()
45 {
46   return Toolkit::PushButton::New();
47 }
48
49 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
50
51 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-up.9.png";
52 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down.9.png";
53 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-disabled.9.png";
54 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down-disabled.9.png";
55
56 } // unnamed namespace
57
58 namespace
59 {
60
61 /**
62  * Get size of Actor if larger than given size
63  * @param[in] root the actor to get the size of
64  * @param[out] size the greater of the given size or the size of the Actor
65  */
66 void SizeOfActorIfLarger( Actor root, Vector3& size )
67 {
68   if ( root )
69   {
70     // RelayoutSize retreived for Actor to use any padding set to it.
71     size.width = std::max( root.GetRelayoutSize( Dimension::WIDTH ), size.width );
72     size.height = std::max( root.GetRelayoutSize( Dimension::HEIGHT ), size.height );
73   }
74 }
75
76 } // unnamed namespace
77
78 Dali::Toolkit::PushButton PushButton::New()
79 {
80   // Create the implementation, temporarily owned on stack
81   IntrusivePtr< PushButton > internalPushButton = new PushButton();
82
83   // Pass ownership to CustomActor
84   Dali::Toolkit::PushButton pushButton( *internalPushButton );
85
86   // Second-phase init of the implementation
87   // This can only be done after the CustomActor connection has been made...
88   internalPushButton->Initialize();
89
90   return pushButton;
91 }
92
93 PushButton::PushButton()
94 : Button(),
95   mSize()
96 {
97   SetAnimationTime( ANIMATION_TIME );
98 }
99
100 PushButton::~PushButton()
101 {
102 }
103
104 void PushButton::OnButtonInitialize()
105 {
106   // Push button requires the Leave event.
107   Actor self = Self();
108   self.SetLeaveRequired( true );
109
110   // Set resize policy to natural size so that buttons will resize to background images
111   self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
112
113   Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
114   Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
115   Image disabledImage = Dali::ResourceImage::New( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
116   Image disabledSelectedImage = Dali::ResourceImage::New( DISABLED_SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
117
118   SetButtonImage( ImageActor::New( buttonImage ) );
119   SetSelectedImage( ImageActor::New( selectedImage ) );
120   SetDisabledImage( ImageActor::New( disabledImage ) );
121   SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
122 }
123
124 void PushButton::OnLabelSet()
125 {
126   Actor& label = GetLabel();
127
128   if( label )
129   {
130     label.SetAnchorPoint( AnchorPoint::CENTER );
131     label.SetParentOrigin( ParentOrigin::CENTER );
132
133     Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label );
134     if( textLabel )
135     {
136       textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
137       textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
138       textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true );
139     }
140
141     ConfigureSizeNegotiation();
142   }
143 }
144
145 void PushButton::OnButtonImageSet()
146 {
147   ConfigureSizeNegotiation();
148   RelayoutRequest();
149 }
150
151 void PushButton::OnSelectedImageSet()
152 {
153   ConfigureSizeNegotiation();
154   RelayoutRequest();
155 }
156
157 void PushButton::OnBackgroundImageSet()
158 {
159   ConfigureSizeNegotiation();
160   RelayoutRequest();
161 }
162
163 void PushButton::OnSelectedBackgroundImageSet()
164 {
165   ConfigureSizeNegotiation();
166   RelayoutRequest();
167 }
168
169 void PushButton::OnDisabledImageSet()
170 {
171   ConfigureSizeNegotiation();
172   RelayoutRequest();
173 }
174
175 void PushButton::OnDisabledSelectedImageSet()
176 {
177   ConfigureSizeNegotiation();
178   RelayoutRequest();
179 }
180
181 void PushButton::OnDisabledBackgroundImageSet()
182 {
183   ConfigureSizeNegotiation();
184   RelayoutRequest();
185 }
186
187 void PushButton::OnSizeSet( const Vector3& targetSize )
188 {
189   if( targetSize != mSize )
190   {
191     mSize = targetSize;
192
193     Actor& label = GetLabel();
194
195     if( label )
196     {
197       label.SetSize( mSize );
198     }
199   }
200 }
201
202 void PushButton::PrepareForTranstionIn( Actor actor )
203 {
204   actor.SetOpacity( 0.0f );
205 }
206
207 void PushButton::PrepareForTranstionOut( Actor actor )
208 {
209   actor.SetOpacity( 1.0f );
210 }
211
212 void PushButton::OnTransitionIn( Actor actor )
213 {
214   FadeImageTo( actor, 1.f );
215 }
216
217 void PushButton::OnTransitionOut( Actor actor )
218 {
219   FadeImageTo( actor, 0.0f );
220 }
221
222 void PushButton::FadeImageTo( Actor actor, float opacity )
223 {
224   if( actor )
225   {
226     Dali::Animation transitionAnimation = GetTransitionAnimation();
227     DALI_ASSERT_DEBUG( transitionAnimation );
228
229     if( transitionAnimation )
230     {
231       transitionAnimation.AnimateTo( Property( actor, Actor::Property::COLOR_ALPHA ), opacity );
232     }
233   }
234 }
235
236 Vector3 PushButton::GetNaturalSize()
237 {
238   Vector3 size;
239
240   // If label, test against it's size
241   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() );
242   if( label )
243   {
244     size.width  = std::max( size.width,  label.GetRelayoutSize( Dimension::WIDTH ) );
245     size.height = std::max( size.height, label.GetRelayoutSize( Dimension::HEIGHT ) );
246   }
247   else
248   {
249     // Check Image and Background image and use the largest size as the control's Natural size.
250     SizeOfActorIfLarger( GetButtonImage(), size );
251     SizeOfActorIfLarger( GetBackgroundImage(), size );
252   }
253
254   return size;
255 }
256
257 void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
258 {
259   ConfigureSizeNegotiation();
260 }
261
262 void PushButton::ConfigureSizeNegotiation()
263 {
264   std::vector< Actor > images;
265   images.reserve( 7 );
266
267   images.push_back( GetButtonImage() );
268   images.push_back( GetSelectedImage() );
269   images.push_back( GetSelectedBackgroundImage() );
270   images.push_back( GetBackgroundImage() );
271   images.push_back( GetDisabledImage() );
272   images.push_back( GetDisabledSelectedImage() );
273   images.push_back( GetDisabledBackgroundImage() );
274
275   Actor label = GetLabel();
276
277   for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
278   {
279     ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label );
280   }
281
282   if( label )
283   {
284     Padding padding;
285
286     if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::USE_NATURAL_SIZE )
287     {
288       padding.left = TEXT_PADDING;
289       padding.right = TEXT_PADDING;
290     }
291
292     if( label.GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::USE_NATURAL_SIZE )
293     {
294       padding.top = TEXT_PADDING;
295       padding.bottom = TEXT_PADDING;
296     }
297
298     label.SetPadding( padding );
299   }
300 }
301
302 void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label )
303 {
304   ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT;
305   ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT;
306
307   switch( Self().GetResizePolicy( dimension ) )
308   {
309     case ResizePolicy::FIT_TO_CHILDREN:
310     {
311       imageResizePolicy = labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
312       break;
313     }
314     case ResizePolicy::USE_NATURAL_SIZE:
315     {
316       if( label )
317       {
318         labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
319       }
320       else
321       {
322         imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
323       }
324       break;
325     }
326     default:
327     {
328       break;
329     }
330   }
331
332   if( label )
333   {
334     label.SetResizePolicy( labelResizePolicy, dimension );
335   }
336
337   for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it )
338   {
339     Actor actor = *it;
340     if( actor )
341     {
342       actor.SetResizePolicy( imageResizePolicy, dimension );
343     }
344   }
345 }
346
347 } // namespace Internal
348
349 } // namespace Toolkit
350
351 } // namespace Dali