2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "push-button-impl.h"
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>
27 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
41 const float TEXT_PADDING = 12.0f;
42 const float ANIMATION_TIME( 0.2f );
46 return Toolkit::PushButton::New();
49 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
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";
56 } // unnamed namespace
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
66 void SizeOfActorIfLarger( Actor root, Vector3& size )
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 );
76 } // unnamed namespace
78 Dali::Toolkit::PushButton PushButton::New()
80 // Create the implementation, temporarily owned on stack
81 IntrusivePtr< PushButton > internalPushButton = new PushButton();
83 // Pass ownership to CustomActor
84 Dali::Toolkit::PushButton pushButton( *internalPushButton );
86 // Second-phase init of the implementation
87 // This can only be done after the CustomActor connection has been made...
88 internalPushButton->Initialize();
93 PushButton::PushButton()
97 SetAnimationTime( ANIMATION_TIME );
100 PushButton::~PushButton()
104 void PushButton::OnButtonInitialize()
106 // Push button requires the Leave event.
108 self.SetLeaveRequired( true );
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 );
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 );
118 SetButtonImage( ImageActor::New( buttonImage ) );
119 SetSelectedImage( ImageActor::New( selectedImage ) );
120 SetDisabledImage( ImageActor::New( disabledImage ) );
121 SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
124 void PushButton::OnLabelSet()
126 Actor& label = GetLabel();
130 label.SetAnchorPoint( AnchorPoint::CENTER );
131 label.SetParentOrigin( ParentOrigin::CENTER );
133 Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label );
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 );
141 ConfigureSizeNegotiation();
145 void PushButton::OnButtonImageSet()
147 ConfigureSizeNegotiation();
151 void PushButton::OnSelectedImageSet()
153 ConfigureSizeNegotiation();
157 void PushButton::OnBackgroundImageSet()
159 ConfigureSizeNegotiation();
163 void PushButton::OnSelectedBackgroundImageSet()
165 ConfigureSizeNegotiation();
169 void PushButton::OnDisabledImageSet()
171 ConfigureSizeNegotiation();
175 void PushButton::OnDisabledSelectedImageSet()
177 ConfigureSizeNegotiation();
181 void PushButton::OnDisabledBackgroundImageSet()
183 ConfigureSizeNegotiation();
187 void PushButton::OnSizeSet( const Vector3& targetSize )
189 if( targetSize != mSize )
193 Actor& label = GetLabel();
197 label.SetSize( mSize );
202 void PushButton::PrepareForTranstionIn( Actor actor )
204 actor.SetOpacity( 0.0f );
207 void PushButton::PrepareForTranstionOut( Actor actor )
209 actor.SetOpacity( 1.0f );
212 void PushButton::OnTransitionIn( Actor actor )
214 FadeImageTo( actor, 1.f );
217 void PushButton::OnTransitionOut( Actor actor )
219 FadeImageTo( actor, 0.0f );
222 void PushButton::FadeImageTo( Actor actor, float opacity )
226 Dali::Animation transitionAnimation = GetTransitionAnimation();
227 DALI_ASSERT_DEBUG( transitionAnimation );
229 if( transitionAnimation )
231 transitionAnimation.AnimateTo( Property( actor, Actor::Property::COLOR_ALPHA ), opacity );
236 Vector3 PushButton::GetNaturalSize()
240 // If label, test against it's size
241 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() );
244 size.width = std::max( size.width, label.GetRelayoutSize( Dimension::WIDTH ) );
245 size.height = std::max( size.height, label.GetRelayoutSize( Dimension::HEIGHT ) );
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 );
257 void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
259 ConfigureSizeNegotiation();
262 void PushButton::ConfigureSizeNegotiation()
264 std::vector< Actor > images;
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() );
275 Actor label = GetLabel();
277 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
279 ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label );
286 if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::USE_NATURAL_SIZE )
288 padding.left = TEXT_PADDING;
289 padding.right = TEXT_PADDING;
292 if( label.GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::USE_NATURAL_SIZE )
294 padding.top = TEXT_PADDING;
295 padding.bottom = TEXT_PADDING;
298 label.SetPadding( padding );
302 void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label )
304 ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT;
305 ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT;
307 switch( Self().GetResizePolicy( dimension ) )
309 case ResizePolicy::FIT_TO_CHILDREN:
311 imageResizePolicy = labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
314 case ResizePolicy::USE_NATURAL_SIZE:
318 labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
322 imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
334 label.SetResizePolicy( labelResizePolicy, dimension );
337 for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it )
342 actor.SetResizePolicy( imageResizePolicy, dimension );
347 } // namespace Internal
349 } // namespace Toolkit