X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fbuttons%2Fpush-button-impl.cpp;h=d09f7811aa23677ca964387e5d21f4965d4d49a5;hp=a63b861fdd7a9643dcf389ece4f6e96c0dce8172;hb=b1e8521ad77e7b4e62b59613b2edef64429130e9;hpb=1c5674a11a51310ee689d6daf4e6b7d94dec607e diff --git a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp index a63b861..d09f781 100644 --- a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp +++ b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,20 @@ #include "push-button-impl.h" // EXTERNAL INCLUDES -#include #include +#include +#include +#include // INTERNAL INCLUDES +#include #include +#include +#include + +#if defined(DEBUG_ENABLED) + extern Debug::Filter* gLogButtonFilter; +#endif namespace Dali { @@ -37,36 +46,25 @@ namespace Internal namespace { -const float TEXT_PADDING = 12.0f; -const float ANIMATION_TIME( 0.2f ); - BaseHandle Create() { return Toolkit::PushButton::New(); } -TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create ); +// Properties + +DALI_TYPE_REGISTRATION_BEGIN( Toolkit::PushButton, Toolkit::Button, Create ) + +DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "labelPadding", STRING, LABEL_PADDING ) +DALI_PROPERTY_REGISTRATION( Toolkit, PushButton, "iconPadding", STRING, ICON_PADDING ) + +DALI_TYPE_REGISTRATION_END() } // unnamed namespace namespace { -/** - * Get size of Actor if larger than given size - * @param[in] root the actor to get the size of - * @param[out] size the greater of the given size or the size of the Actor - */ -void SizeOfActorIfLarger( Actor root, Vector3& size ) -{ - if ( root ) - { - // RelayoutSize retreived for Actor to use any padding set to it. - size.width = std::max( root.GetRelayoutSize( Dimension::WIDTH ), size.width ); - size.height = std::max( root.GetRelayoutSize( Dimension::HEIGHT ), size.height ); - } -} - } // unnamed namespace Dali::Toolkit::PushButton PushButton::New() @@ -86,642 +84,114 @@ Dali::Toolkit::PushButton PushButton::New() PushButton::PushButton() : Button(), - mSize() + mIconAlignment( RIGHT ) { - SetAnimationTime( ANIMATION_TIME ); } PushButton::~PushButton() { } -void PushButton::OnButtonInitialize() +void PushButton::OnInitialize() { + Button::OnInitialize(); + // Push button requires the Leave event. Actor self = Self(); - self.SetLeaveRequired( true ); - - // Set resize policy to natural size so that buttons will resize to background images - self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS ); + self.SetProperty( Actor::Property::LEAVE_REQUIRED, true ); } -void PushButton::OnLabelSet() +void PushButton::SetIconAlignment( const PushButton::IconAlignment iconAlignment ) { - Actor& label = GetLabel(); - - if( label ) + mIconAlignment = iconAlignment; + Button::Align labelAlignment; + switch ( iconAlignment ) { - label.SetAnchorPoint( AnchorPoint::CENTER ); - label.SetParentOrigin( ParentOrigin::CENTER ); - - Toolkit::TextLabel textLabel = Toolkit::TextLabel::DownCast( label ); - if( textLabel ) - { - textLabel.SetProperty( Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" ); - textLabel.SetProperty( Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" ); - textLabel.SetProperty( Toolkit::TextLabel::Property::MULTI_LINE, true ); - } - - ConfigureSizeNegotiation(); - } -} - -void PushButton::OnButtonImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnSelectedImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnBackgroundImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnSelectedBackgroundImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnDisabledImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -void PushButton::OnDisabledBackgroundImageSet() -{ - ConfigureSizeNegotiation(); - RelayoutRequest(); -} - -bool PushButton::OnSelected() -{ - Actor& buttonImage = GetButtonImage(); - Actor& selectedImage = GetSelectedImage(); - Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); - - PaintState paintState = GetPaintState(); - - switch( paintState ) + case RIGHT: { - case UnselectedState: - { - FadeOutImage( buttonImage ); - FadeInImage( selectedBackgroundImage ); - FadeInImage( selectedImage ); - StartTransitionAnimation(); - break; - } - case SelectedState: - { - FadeOutImage( selectedBackgroundImage ); - FadeOutImage( selectedImage ); - FadeInImage( buttonImage ); - StartTransitionAnimation(); - break; - } - case UnselectedSelectedTransition: - { - float opacity = 1.f; - if( selectedImage ) - { - opacity = selectedImage.GetCurrentOpacity(); - } - - StopTransitionAnimation( false ); - FadeOutImage( selectedBackgroundImage, opacity ); - FadeOutImage( selectedImage, opacity ); - FadeInImage( buttonImage, 1.f - opacity ); - StartTransitionAnimation(); - break; - } - case SelectedUnselectedTransition: - { - float opacity = 0.f; - if( selectedImage ) - { - opacity = selectedImage.GetCurrentOpacity(); - } - - StopTransitionAnimation( false ); - FadeOutImage( buttonImage, 1.f - opacity ); - FadeInImage( selectedBackgroundImage, opacity ); - FadeInImage( selectedImage, opacity ); - StartTransitionAnimation(); - break; - } - case DisabledUnselectedTransition: - { - StopTransitionAnimation(); - FadeOutImage( buttonImage ); - FadeInImage( selectedBackgroundImage ); - FadeInImage( selectedImage ); - StartTransitionAnimation(); - break; - } - case DisabledSelectedTransition: - { - StopTransitionAnimation(); - FadeOutImage( selectedBackgroundImage ); - FadeOutImage( selectedImage ); - FadeInImage( buttonImage ); - StartTransitionAnimation(); - break; - } - default: - { - break; - } + labelAlignment = Button::BEGIN; + break; } - - if( mTransitionAnimation ) + case TOP: { - return true; + labelAlignment = Button::BOTTOM; + break; } - - return false; -} - -bool PushButton::OnDisabled() -{ - Actor& buttonImage = GetButtonImage(); - Actor& selectedImage = GetSelectedImage(); - Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); - Actor& backgroundImage = GetBackgroundImage(); - Actor& disabledImage = GetDisabledImage(); - Actor& disabledSelectedImage = GetDisabledSelectedImage(); - Actor& disabledBackgroundImage = GetDisabledBackgroundImage(); - - PaintState paintState = GetPaintState(); - - switch( paintState ) + case BOTTOM: { - case UnselectedState: - { - FadeOutImage( backgroundImage ); - FadeOutImage( buttonImage ); - FadeInImage( disabledBackgroundImage ); - FadeInImage( disabledImage ); - StartTransitionAnimation(); - break; - } - case SelectedState: - { - FadeOutImage( backgroundImage ); - FadeOutImage( selectedBackgroundImage ); - FadeOutImage( selectedImage ); - FadeInImage( disabledBackgroundImage ); - FadeInImage( disabledSelectedImage ); - StartTransitionAnimation(); - break; - } - case DisabledUnselectedState: - { - FadeOutImage( disabledBackgroundImage ); - FadeOutImage( disabledImage ); - FadeInImage( backgroundImage ); - FadeInImage( buttonImage ); - StartTransitionAnimation(); - break; - } - case DisabledSelectedState: - { - FadeOutImage( disabledBackgroundImage ); - FadeOutImage( disabledSelectedImage ); - FadeInImage( backgroundImage ); - FadeInImage( selectedBackgroundImage ); - FadeInImage( selectedImage ); - StartTransitionAnimation(); - break; - } - case UnselectedSelectedTransition: - { - float opacity = 1.f; - if( selectedImage ) - { - opacity = selectedImage.GetCurrentOpacity(); - } - - StopTransitionAnimation(); - FadeOutImage( backgroundImage ); - FadeOutImage( selectedBackgroundImage, opacity ); - FadeOutImage( selectedImage, opacity ); - FadeInImage( disabledBackgroundImage ); - FadeInImage( disabledSelectedImage ); - StartTransitionAnimation(); - break; - } - case SelectedUnselectedTransition: - { - float opacity = 1.f; - if( buttonImage ) - { - opacity = buttonImage.GetCurrentOpacity(); - } - - StopTransitionAnimation(); - FadeOutImage( backgroundImage ); - FadeOutImage( buttonImage, opacity ); - FadeInImage( disabledBackgroundImage ); - FadeInImage( disabledImage ); - StartTransitionAnimation(); - break; - } - case UnselectedDisabledTransition: - { - float opacity = 1.f; - if( disabledImage ) - { - opacity = disabledImage.GetCurrentOpacity(); - } - - StopTransitionAnimation( false ); - FadeOutImage( disabledBackgroundImage, opacity ); - FadeOutImage( disabledImage, opacity ); - FadeInImage( backgroundImage, 1.f - opacity ); - FadeInImage( buttonImage, 1.f - opacity ); - StartTransitionAnimation(); - break; - } - case DisabledUnselectedTransition: - { - float opacity = 1.f; - if( buttonImage ) - { - opacity = buttonImage.GetCurrentOpacity(); - } - - StopTransitionAnimation( false ); - FadeOutImage( backgroundImage, opacity ); - FadeOutImage( buttonImage, opacity ); - FadeInImage( disabledBackgroundImage, 1.f - opacity ); - FadeInImage( disabledImage, 1.f - opacity ); - StartTransitionAnimation(); - break; - } - case SelectedDisabledTransition: - { - float opacity = 1.f; - if( disabledSelectedImage ) - { - opacity = disabledSelectedImage.GetCurrentOpacity(); - } - - StopTransitionAnimation( false ); - FadeOutImage( disabledBackgroundImage, opacity ); - FadeOutImage( disabledSelectedImage, opacity ); - FadeInImage( backgroundImage, 1.f - opacity ); - FadeInImage( selectedBackgroundImage, 1.f - opacity ); - FadeInImage( selectedImage, 1.f - opacity ); - StartTransitionAnimation(); - break; - } - case DisabledSelectedTransition: - { - float opacity = 1.f; - if( selectedImage ) - { - opacity = selectedImage.GetCurrentOpacity(); - } - - StopTransitionAnimation( false ); - FadeOutImage( backgroundImage, opacity ); - FadeOutImage( selectedBackgroundImage, opacity ); - FadeOutImage( selectedImage, opacity ); - FadeInImage( disabledBackgroundImage, 1.f - opacity ); - FadeInImage( disabledSelectedImage, 1.f - opacity ); - StartTransitionAnimation(); - break; - } + labelAlignment = Button::TOP; + break; } - - if( mTransitionAnimation ) - { - return true; + case LEFT: + default: + labelAlignment = Button::END; + break; } - return false; + Button::SetLabelAlignment( labelAlignment ); } -bool PushButton::OnPressed() +const PushButton::IconAlignment PushButton::GetIconAlignment() const { - Actor& buttonImage = GetButtonImage(); - Actor& selectedImage = GetSelectedImage(); - Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); - - PaintState paintState = GetPaintState(); - - switch( paintState ) - { - case UnselectedState: - { - FadeOutImage( buttonImage ); - FadeInImage( selectedBackgroundImage ); - FadeInImage( selectedImage ); - StartTransitionAnimation(); - break; - } - case SelectedUnselectedTransition: - { - float opacity = 1.f; - if( buttonImage ) - { - opacity = buttonImage.GetCurrentOpacity(); - } - - StopTransitionAnimation( false ); - FadeOutImage( buttonImage, opacity ); - FadeInImage( selectedBackgroundImage, 1.f - opacity ); - FadeInImage( selectedImage, 1.f - opacity ); - StartTransitionAnimation(); - break; - } - case DisabledUnselectedTransition: - { - float opacity = 1.f; - if( buttonImage ) - { - opacity = buttonImage.GetCurrentOpacity(); - } - - StopTransitionAnimation(); - FadeOutImage( buttonImage, opacity ); - FadeInImage( selectedBackgroundImage ); - FadeInImage( selectedImage ); - StartTransitionAnimation(); - break; - } - default: - break; - } - - if( mTransitionAnimation ) - { - return true; - } - - return false; + return mIconAlignment; } -bool PushButton::OnReleased() +void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value ) { - Actor& buttonImage = GetButtonImage(); - Actor& selectedImage = GetSelectedImage(); - Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); - - PaintState paintState = GetPaintState(); + Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) ); - switch( paintState ) + if ( pushButton ) { - case SelectedState: - { - FadeOutImage( selectedBackgroundImage ); - FadeOutImage( selectedImage ); - FadeInImage( buttonImage ); - StartTransitionAnimation(); - break; - } - case UnselectedSelectedTransition: + PushButton& pushButtonImpl( GetImplementation( pushButton ) ); + + // Properties remain here for Tizen 3.0 legacy requirements. Are now in Button base class + + switch ( propertyIndex ) { - float opacity = 1.f; - if( selectedImage ) + case Toolkit::PushButton::Property::LABEL_PADDING: { - opacity = selectedImage.GetCurrentOpacity(); + Vector4 padding ( value.Get< Vector4 >() ); + pushButtonImpl.Button::SetLabelPadding( Padding( padding.x, padding.y, padding.z, padding.w ) ); + break; } - - StopTransitionAnimation( false ); - FadeOutImage( selectedBackgroundImage, opacity ); - FadeOutImage( selectedImage, opacity ); - FadeInImage( buttonImage, 1.f - opacity ); - StartTransitionAnimation(); - break; - } - case DisabledSelectedTransition: - { - float opacity = 1.f; - if( selectedImage ) + case Toolkit::PushButton::Property::ICON_PADDING: { - opacity = selectedImage.GetCurrentOpacity(); + Vector4 padding ( value.Get< Vector4 >() ); + pushButtonImpl.Button::SetForegroundPadding( Padding( padding.x, padding.y, padding.z, padding.w ) ); + break; } - - StopTransitionAnimation(); - FadeOutImage( selectedBackgroundImage, opacity ); - FadeOutImage( selectedImage, opacity ); - FadeInImage( buttonImage ); - StartTransitionAnimation(); - break; - } - default: - { - break; - } - } - - if( mTransitionAnimation ) - { - return true; - } - - return false; -} - -void PushButton::StopAllAnimations() -{ - StopTransitionAnimation(); -} - -void PushButton::OnSizeSet( const Vector3& targetSize ) -{ - if( targetSize != mSize ) - { - mSize = targetSize; - - Actor& label = GetLabel(); - - if( label ) - { - label.SetSize( mSize ); - } - } -} - -void PushButton::StartTransitionAnimation() -{ - if( mTransitionAnimation ) - { - mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished ); - mTransitionAnimation.Play(); - } -} - -void PushButton::StopTransitionAnimation( bool remove ) -{ - if( mTransitionAnimation ) - { - mTransitionAnimation.Clear(); - mTransitionAnimation.Reset(); - } - - if( remove ) - { - UpdatePaintTransitionState(); - } -} - -void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale ) -{ - if( image ) - { - image.SetOpacity( opacity ); - image.SetScale( scale ); - - if( !mTransitionAnimation ) - { - mTransitionAnimation = Dali::Animation::New( GetAnimationTime() ); } - - mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f ); } } -void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale ) +Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex ) { - if( image ) - { - image.SetOpacity( opacity ); - image.SetScale( scale ); + Property::Value value; - if( !mTransitionAnimation ) - { - mTransitionAnimation = Dali::Animation::New( GetAnimationTime() ); - } - - mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f ); - } -} - -void PushButton::TransitionAnimationFinished( Dali::Animation& source ) -{ - StopTransitionAnimation(); -} + Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) ); -Vector3 PushButton::GetNaturalSize() -{ - Vector3 size; - - // If label, test against it's size - Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() ); - if( label ) - { - size.width = std::max( size.width, label.GetRelayoutSize( Dimension::WIDTH ) ); - size.height = std::max( size.height, label.GetRelayoutSize( Dimension::HEIGHT ) ); - } - else + if ( pushButton ) { - // Check Image and Background image and use the largest size as the control's Natural size. - SizeOfActorIfLarger( GetButtonImage(), size ); - SizeOfActorIfLarger( GetBackgroundImage(), size ); - } - - return size; -} - -void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) -{ - ConfigureSizeNegotiation(); -} - -void PushButton::ConfigureSizeNegotiation() -{ - std::vector< Actor > images; - images.reserve( 7 ); - - images.push_back( GetButtonImage() ); - images.push_back( GetSelectedImage() ); - images.push_back( GetSelectedBackgroundImage() ); - images.push_back( GetBackgroundImage() ); - images.push_back( GetDisabledImage() ); - images.push_back( GetDisabledSelectedImage() ); - images.push_back( GetDisabledBackgroundImage() ); - - Actor label = GetLabel(); + PushButton& pushButtonImpl( GetImplementation( pushButton ) ); - for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i ) - { - ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label ); - } - - if( label ) - { - Padding padding; - - if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::USE_NATURAL_SIZE ) - { - padding.left = TEXT_PADDING; - padding.right = TEXT_PADDING; - } - - if( label.GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::USE_NATURAL_SIZE ) - { - padding.top = TEXT_PADDING; - padding.bottom = TEXT_PADDING; - } - - label.SetPadding( padding ); - } -} - -void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label ) -{ - ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT; - ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT; - - switch( Self().GetResizePolicy( dimension ) ) - { - case ResizePolicy::FIT_TO_CHILDREN: + switch ( propertyIndex ) { - imageResizePolicy = labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE; - break; - } - case ResizePolicy::USE_NATURAL_SIZE: - { - if( label ) + case Toolkit::PushButton::Property::LABEL_PADDING: { - labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + Padding padding = pushButtonImpl.Button::GetLabelPadding(); + value = Vector4( padding.x, padding.y, padding.top, padding.bottom); + break; } - else + case Toolkit::PushButton::Property::ICON_PADDING: { - imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + Padding padding = pushButtonImpl.Button::GetForegroundPadding(); + value = Vector4( padding.x, padding.y, padding.top, padding.bottom); + break; } - break; - } - default: - { - break; } } - if( label ) - { - label.SetResizePolicy( labelResizePolicy, dimension ); - } - - for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it ) - { - Actor actor = *it; - if( actor ) - { - actor.SetResizePolicy( imageResizePolicy, dimension ); - } - } + return value; } } // namespace Internal