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/devel-api/object/type-registry-helper.h>
25 #include <dali/public-api/images/resource-image.h>
26 #include <dali/devel-api/scripting/scripting.h>
29 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
30 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
44 const float ANIMATION_TIME( 0.2f );
45 const Padding DEFAULT_LABEL_PADDING( 12.0f, 12.0f, 12.0f, 12.0f );
46 const Padding DEFAULT_ICON_PADDING( 12.0f, 12.0f, 12.0f, 12.0f );
50 return Toolkit::PushButton::New();
55 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create )
57 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "unselectedIcon", STRING, UNSELECTED_ICON )
58 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "selectedIcon", STRING, SELECTED_ICON )
59 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconAlignment", STRING, ICON_ALIGNMENT )
60 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING )
61 DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING )
63 DALI_TYPE_REGISTRATION_END()
66 * Table to define Text-to-enum conversions for IconAlignment.
68 const Dali::Scripting::StringEnum IconAlignmentTable[] = {
69 { "LEFT", Toolkit::Internal::PushButton::LEFT },
70 { "RIGHT", Toolkit::Internal::PushButton::RIGHT },
71 { "TOP", Toolkit::Internal::PushButton::TOP },
72 { "BOTTOM", Toolkit::Internal::PushButton::BOTTOM },
73 }; const unsigned int IconAlignmentTableCount = sizeof( IconAlignmentTable ) / sizeof( IconAlignmentTable[0] );
75 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-up.9.png";
76 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down.9.png";
77 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-disabled.9.png";
78 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "button-down-disabled.9.png";
80 } // unnamed namespace
86 * Get size of Actor if larger than given size
87 * @param[in] root the actor to get the size of
88 * @param[out] size the greater of the given size or the size of the Actor
90 void SizeOfActorIfLarger( Actor root, Vector3& size )
94 // RelayoutSize retreived for Actor to use any padding set to it.
95 size.width = std::max( root.GetRelayoutSize( Dimension::WIDTH ), size.width );
96 size.height = std::max( root.GetRelayoutSize( Dimension::HEIGHT ), size.height );
100 } // unnamed namespace
102 Dali::Toolkit::PushButton PushButton::New()
104 // Create the implementation, temporarily owned on stack
105 IntrusivePtr< PushButton > internalPushButton = new PushButton();
107 // Pass ownership to CustomActor
108 Dali::Toolkit::PushButton pushButton( *internalPushButton );
110 // Second-phase init of the implementation
111 // This can only be done after the CustomActor connection has been made...
112 internalPushButton->Initialize();
117 PushButton::PushButton()
119 mLabelPadding( DEFAULT_LABEL_PADDING ),
120 mIconPadding( DEFAULT_ICON_PADDING ),
121 mIconAlignment( RIGHT ),
124 SetAnimationTime( ANIMATION_TIME );
127 PushButton::~PushButton()
131 void PushButton::OnButtonInitialize()
133 // Push button requires the Leave event.
135 self.SetLeaveRequired( true );
137 // Set resize policy to natural size so that buttons will resize to background images
138 self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
140 SetUnselectedImage( UNSELECTED_BUTTON_IMAGE_DIR );
141 SetSelectedImage( SELECTED_BUTTON_IMAGE_DIR );
142 SetDisabledImage( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR );
143 SetDisabledSelectedImage( DISABLED_SELECTED_BUTTON_IMAGE_DIR );
146 void PushButton::SetIcon( DecorationState state, const std::string iconFilename )
148 mIconName[ state ] = iconFilename;
149 SetDecoration( state, Toolkit::ImageView::New( iconFilename ) );
150 ConfigureSizeNegotiation();
153 std::string& PushButton::GetIcon( DecorationState state )
155 return mIconName[ state ];
158 void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment )
160 mIconAlignment = iconAlignment;
161 ConfigureSizeNegotiation();
164 const PushButton::IconAlignment PushButton::GetIconAlignment() const
166 return mIconAlignment;
169 void PushButton::SetLabelPadding( const Vector4& padding )
171 mLabelPadding = Padding( padding.x, padding.y, padding.z, padding.w );
172 ConfigureSizeNegotiation();
175 Vector4 PushButton::GetLabelPadding()
177 return Vector4( mLabelPadding.left, mLabelPadding.right, mLabelPadding.top, mLabelPadding.bottom );
180 void PushButton::SetIconPadding( const Vector4& padding )
182 mIconPadding = Padding( padding.x, padding.y, padding.z, padding.w );
183 ConfigureSizeNegotiation();
186 Vector4 PushButton::GetIconPadding()
188 return Vector4( mIconPadding.left, mIconPadding.right, mIconPadding.top, mIconPadding.bottom );
191 void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value )
193 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
197 PushButton& pushButtonImpl( GetImplementation( pushButton ) );
199 switch ( propertyIndex )
201 case Toolkit::PushButton::Property::UNSELECTED_ICON:
203 pushButtonImpl.SetIcon( UNSELECTED_DECORATION, value.Get< std::string >() );
206 case Toolkit::PushButton::Property::SELECTED_ICON:
208 pushButtonImpl.SetIcon( SELECTED_DECORATION, value.Get< std::string >() );
211 case Toolkit::PushButton::Property::ICON_ALIGNMENT:
213 IconAlignment iconAlignment;
214 if( Scripting::GetEnumeration< IconAlignment >( value.Get< std::string >().c_str(), IconAlignmentTable, IconAlignmentTableCount, iconAlignment ) )
216 pushButtonImpl.SetIconAlignment( iconAlignment );
220 case Toolkit::PushButton::Property::LABEL_PADDING:
222 pushButtonImpl.SetLabelPadding( value.Get< Vector4 >() );
225 case Toolkit::PushButton::Property::ICON_PADDING:
227 pushButtonImpl.SetIconPadding( value.Get< Vector4 >() );
234 Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex )
236 Property::Value value;
238 Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) );
242 PushButton& pushButtonImpl( GetImplementation( pushButton ) );
244 switch ( propertyIndex )
246 case Toolkit::PushButton::Property::UNSELECTED_ICON:
248 value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION );
251 case Toolkit::PushButton::Property::SELECTED_ICON:
253 value = pushButtonImpl.GetIcon( UNSELECTED_DECORATION );
256 case Toolkit::PushButton::Property::ICON_ALIGNMENT:
258 value = Scripting::GetLinearEnumerationName< IconAlignment >( pushButtonImpl.GetIconAlignment(), IconAlignmentTable, IconAlignmentTableCount );
261 case Toolkit::PushButton::Property::LABEL_PADDING:
263 value = pushButtonImpl.GetLabelPadding();
266 case Toolkit::PushButton::Property::ICON_PADDING:
268 value = pushButtonImpl.GetIconPadding();
277 void PushButton::OnLabelSet( bool noPadding )
279 Actor& label = GetLabelActor();
285 mLabelPadding = Padding( 0.0f, 0.0f, 0.0f, 0.0f );
288 Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label );
291 textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, false );
294 ConfigureSizeNegotiation();
297 void PushButton::OnButtonImageSet()
299 ConfigureSizeNegotiation();
302 void PushButton::OnSelectedImageSet()
304 ConfigureSizeNegotiation();
307 void PushButton::OnBackgroundImageSet()
309 ConfigureSizeNegotiation();
312 void PushButton::OnSelectedBackgroundImageSet()
314 ConfigureSizeNegotiation();
317 void PushButton::OnDisabledImageSet()
319 ConfigureSizeNegotiation();
322 void PushButton::OnDisabledSelectedImageSet()
324 ConfigureSizeNegotiation();
327 void PushButton::OnDisabledBackgroundImageSet()
329 ConfigureSizeNegotiation();
332 void PushButton::OnSizeSet( const Vector3& targetSize )
334 if( targetSize != mSize )
338 Actor& label = GetLabelActor();
342 label.SetSize( mSize );
347 void PushButton::PrepareForTranstionIn( Actor actor )
349 actor.SetOpacity( 0.0f );
352 void PushButton::PrepareForTranstionOut( Actor actor )
354 actor.SetOpacity( 1.0f );
357 void PushButton::OnTransitionIn( Actor actor )
359 FadeImageTo( actor, 1.f );
362 void PushButton::OnTransitionOut( Actor actor )
364 FadeImageTo( actor, 0.0f );
367 void PushButton::FadeImageTo( Actor actor, float opacity )
371 Dali::Animation transitionAnimation = GetTransitionAnimation();
372 DALI_ASSERT_DEBUG( transitionAnimation );
374 if( transitionAnimation )
376 transitionAnimation.AnimateTo( Property( actor, Actor::Property::COLOR_ALPHA ), opacity );
381 Vector3 PushButton::GetNaturalSize()
385 // If label, test against it's size
386 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabelActor() );
388 Actor icon = GetDecoration( UNSELECTED_DECORATION );
391 Vector3 labelSize( Vector3::ZERO );
392 Vector3 iconSize( Vector3::ZERO );
396 Vector3 labelNaturalSize = label.GetNaturalSize();
397 labelSize.width = labelNaturalSize.width + mLabelPadding.left + mLabelPadding.right;
398 labelSize.height = labelNaturalSize.height + mLabelPadding.top + mLabelPadding.bottom;
403 Vector3 iconNaturalSize = icon.GetNaturalSize();
404 iconSize.width = iconNaturalSize.width + mIconPadding.left + mIconPadding.right;
405 iconSize.height = iconNaturalSize.height + mIconPadding.top + mIconPadding.bottom;
407 switch( mIconAlignment )
412 size.width = labelSize.width + iconSize.width;
413 size.height = std::max( labelSize.height, iconSize.height );
419 size.width = std::max( labelSize.width, iconSize.width );
420 size.height = labelSize.height + iconSize.height;
427 // No icon, so size is the same as label size.
428 // (If there is no label this is zero).
434 // Check Image and Background image and use the largest size as the control's Natural size.
435 SizeOfActorIfLarger( GetUnselectedImage(), size );
436 SizeOfActorIfLarger( GetBackgroundImage(), size );
442 void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension )
444 ConfigureSizeNegotiation();
447 void PushButton::ConfigureSizeNegotiation()
449 std::vector< Actor > images;
452 images.push_back( GetUnselectedImage() );
453 images.push_back( GetSelectedImage() );
454 images.push_back( GetSelectedBackgroundImage() );
455 images.push_back( GetBackgroundImage() );
456 images.push_back( GetDisabledImage() );
457 images.push_back( GetDisabledSelectedImage() );
458 images.push_back( GetDisabledBackgroundImage() );
460 Actor label = GetLabelActor();
462 for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i )
464 ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label );
467 // Add any vertical padding directly to the actors.
468 Actor icon = GetDecoration( UNSELECTED_DECORATION );
469 Actor selectedIcon = GetDecoration( SELECTED_DECORATION );
470 bool iconExists = icon || selectedIcon;
474 label.SetPadding( mLabelPadding );
478 icon.SetPadding( mIconPadding );
482 selectedIcon.SetPadding( mIconPadding );
485 // Calculate and apply horizontal alignments and offsets
486 // to text and icon (depending on existence).
487 Vector3 iconPosition( Vector3::ZERO );
488 Vector3 labelPosition( Vector3::ZERO );
489 Vector3 iconAnchoring( AnchorPoint::CENTER );
490 Vector3 labelAnchoring( AnchorPoint::CENTER );
491 std::string horizontalLabelAlignment = "CENTER";
492 std::string verticalLabelAlignment = "CENTER";
494 if( iconExists && label )
496 // There is an icon and a label to lay out.
497 switch( mIconAlignment )
501 iconPosition.x = mIconPadding.left;
502 labelPosition.x = -mLabelPadding.right;
503 iconAnchoring = AnchorPoint::CENTER_LEFT;
504 labelAnchoring = AnchorPoint::CENTER_RIGHT;
505 horizontalLabelAlignment = "END";
510 iconPosition.x = -mIconPadding.right;
511 labelPosition.x = mLabelPadding.left;
512 iconAnchoring = AnchorPoint::CENTER_RIGHT;
513 labelAnchoring = AnchorPoint::CENTER_LEFT;
514 horizontalLabelAlignment = "BEGIN";
519 iconPosition.y = mIconPadding.top;
520 labelPosition.y = -mLabelPadding.bottom;
521 iconAnchoring = AnchorPoint::TOP_CENTER;
522 labelAnchoring = AnchorPoint::BOTTOM_CENTER;
523 verticalLabelAlignment = "BOTTOM";
528 iconPosition.y = -mIconPadding.bottom;
529 labelPosition.y = mLabelPadding.top;
530 iconAnchoring = AnchorPoint::BOTTOM_CENTER;
531 labelAnchoring = AnchorPoint::TOP_CENTER;
532 verticalLabelAlignment = "TOP";
538 // Note: If there is only an icon, or only a label, the default values are now correct.
539 // Setup the icon(s) with the precalculated values.
542 icon.SetPosition( iconPosition );
543 icon.SetParentOrigin( iconAnchoring );
544 icon.SetAnchorPoint( iconAnchoring );
548 selectedIcon.SetPosition( iconPosition );
549 selectedIcon.SetParentOrigin( iconAnchoring );
550 selectedIcon.SetAnchorPoint( iconAnchoring );
556 label.SetPosition( labelPosition );
557 label.SetParentOrigin( labelAnchoring );
558 label.SetAnchorPoint( labelAnchoring );
559 label.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, horizontalLabelAlignment );
560 label.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, verticalLabelAlignment );
567 void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label )
569 ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT;
570 ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT;
572 ResizePolicy::Type resizePolicy = Self().GetResizePolicy( dimension );
574 if( resizePolicy == ResizePolicy::FIT_TO_CHILDREN || resizePolicy == ResizePolicy::USE_NATURAL_SIZE )
578 labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
582 imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE;
588 label.SetResizePolicy( labelResizePolicy, dimension );
591 for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it )
596 actor.SetResizePolicy( imageResizePolicy, dimension );
602 } // namespace Internal
604 } // namespace Toolkit