X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=dali-toolkit%2Finternal%2Fcontrols%2Fbuttons%2Fpush-button-impl.cpp;h=a63b861fdd7a9643dcf389ece4f6e96c0dce8172;hb=70fc28a8c9ad04e29cb4a69c856bac3e96e2301d;hp=8b96ba537f5eb5690be8df9e42e9fb8ead0a9523;hpb=3fbada5f2ec43d9b0ea3e63eb54223cf7b3056f4;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp index 8b96ba5..a63b861 100644 --- a/dali-toolkit/internal/controls/buttons/push-button-impl.cpp +++ b/dali-toolkit/internal/controls/buttons/push-button-impl.cpp @@ -1,32 +1,29 @@ -// -// Copyright (c) 2014 Samsung Electronics Co., Ltd. -// -// Licensed under the Flora License, Version 1.0 (the License); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://floralicense.org/license/ -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an AS IS BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// +/* + * Copyright (c) 2014 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ // CLASS HEADER - #include "push-button-impl.h" // EXTERNAL INCLUDES - -#include +#include +#include // INTERNAL INCLUDES - -#include "push-button-default-painter-impl.h" - -#include +#include namespace Dali { @@ -34,22 +31,15 @@ namespace Dali namespace Toolkit { -const Property::Index PushButton::PROPERTY_AUTO_REPEATING = Internal::Button::BUTTON_PROPERTY_END_INDEX + 1; -const Property::Index PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_END_INDEX + 2; -const Property::Index PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_END_INDEX + 3; -const Property::Index PushButton::PROPERTY_TOGGLABLE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 4; -const Property::Index PushButton::PROPERTY_TOGGLE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 5; -const Property::Index PushButton::PROPERTY_BUTTON_IMAGE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 6; -const Property::Index PushButton::PROPERTY_PRESSED_IMAGE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 8; -const Property::Index PushButton::PROPERTY_DIMMED_IMAGE = Internal::Button::BUTTON_PROPERTY_END_INDEX + 9; -const Property::Index PushButton::PROPERTY_LABEL_TEXT = Internal::Button::BUTTON_PROPERTY_END_INDEX + 11; - namespace Internal { namespace { +const float TEXT_PADDING = 12.0f; +const float ANIMATION_TIME( 0.2f ); + BaseHandle Create() { return Toolkit::PushButton::New(); @@ -57,58 +47,23 @@ BaseHandle Create() TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create ); -SignalConnectorType signalConnector1( typeRegistration, Toolkit::PushButton::SIGNAL_TOGGLED , &PushButton::DoConnectSignal ); -SignalConnectorType signalConnector2( typeRegistration, Toolkit::PushButton::SIGNAL_PRESSED , &PushButton::DoConnectSignal ); -SignalConnectorType signalConnector3( typeRegistration, Toolkit::PushButton::SIGNAL_RELEASED, &PushButton::DoConnectSignal ); - -TypeAction action1( typeRegistration, Toolkit::PushButton::ACTION_PUSH_BUTTON_CLICK, &PushButton::DoAction ); - -PropertyRegistration property1( typeRegistration, "auto-repeating", Toolkit::PushButton::PROPERTY_AUTO_REPEATING, Property::BOOLEAN, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property2( typeRegistration, "initial-auto-repeating-delay", Toolkit::PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property3( typeRegistration, "next-auto-repeating-delay", Toolkit::PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY, Property::FLOAT, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property4( typeRegistration, "togglable", Toolkit::PushButton::PROPERTY_TOGGLABLE, Property::BOOLEAN, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property5( typeRegistration, "toggle", Toolkit::PushButton::PROPERTY_TOGGLE, Property::BOOLEAN, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property6( typeRegistration, "button-image", Toolkit::PushButton::PROPERTY_BUTTON_IMAGE, Property::STRING, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property7( typeRegistration, "pressed-image", Toolkit::PushButton::PROPERTY_PRESSED_IMAGE, Property::STRING, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property8( typeRegistration, "dimmed-image", Toolkit::PushButton::PROPERTY_DIMMED_IMAGE, Property::STRING, &PushButton::SetProperty, &PushButton::GetProperty ); -PropertyRegistration property9( typeRegistration, "label-text", Toolkit::PushButton::PROPERTY_LABEL_TEXT, Property::STRING, &PushButton::SetProperty, &PushButton::GetProperty ); - } // unnamed namespace namespace { -const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f ); -const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f ); - -// Helper function used to cast a ButtonPainter to PushButtonDefaultPainter -PushButtonDefaultPainterPtr GetPushButtonPainter( Dali::Toolkit::Internal::ButtonPainterPtr painter ) -{ - return static_cast( painter.Get() ); -} - /** - * Helper function to checks if the specified actor is an ImageActor and if it has an Image with a path. - * - * @param[in] actor Actor handle to check. - * @param[out] path The image path will be applied to this parameter, if available. - * If not available then this will be an empty string. + * 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 GetImageActorFilename( Actor& actor, std::string& path ) +void SizeOfActorIfLarger( Actor root, Vector3& size ) { - path = ""; // Just return an empty string if not using ImageActor with an image - - if ( actor ) + if ( root ) { - ImageActor imageActor = ImageActor::DownCast( actor ); - if ( imageActor ) - { - Image image = imageActor.GetImage(); - if ( image ) - { - path = image.GetFilename(); - } - } + // 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 ); } } @@ -129,621 +84,644 @@ Dali::Toolkit::PushButton PushButton::New() return pushButton; } -void PushButton::SetAutoRepeating( bool autoRepeating ) -{ - mAutoRepeating = autoRepeating; - - // An autorepeating button can't be a toggle button. - if( autoRepeating ) - { - mToggleButton = false; - if( mToggled ) - { - // Emit a signal is not wanted, only change the appearance. - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->Toggled( handle ); - mToggled = false; - } - } - - // Notifies the painter. - GetPushButtonPainter( mPainter )->SetAutoRepeating( mAutoRepeating ); -} - -bool PushButton::IsAutoRepeating() const -{ - return mAutoRepeating; -} - -void PushButton::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay ) -{ - DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f ); - mInitialAutoRepeatingDelay = initialAutoRepeatingDelay; -} - -float PushButton::GetInitialAutoRepeatingDelay() const -{ - return mInitialAutoRepeatingDelay; -} - -void PushButton::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay ) +PushButton::PushButton() +: Button(), + mSize() { - DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f ); - mNextAutoRepeatingDelay = nextAutoRepeatingDelay; + SetAnimationTime( ANIMATION_TIME ); } -float PushButton::GetNextAutoRepeatingDelay() const +PushButton::~PushButton() { - return mNextAutoRepeatingDelay; } -void PushButton::SetToggleButton( bool toggle ) +void PushButton::OnButtonInitialize() { - mToggleButton = toggle; - - // A toggle button can't be an autorepeating button. - if( toggle ) - { - mAutoRepeating = false; + // Push button requires the Leave event. + Actor self = Self(); + self.SetLeaveRequired( true ); - // Notifies the painter. - GetPushButtonPainter( mPainter )->SetAutoRepeating( mAutoRepeating ); - } + // Set resize policy to natural size so that buttons will resize to background images + self.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS ); } -bool PushButton::IsToggleButton() const +void PushButton::OnLabelSet() { - return mToggleButton; -} + Actor& label = GetLabel(); -void PushButton::SetToggled( bool toggle ) -{ - if( !mDimmed && mToggleButton && ( toggle != mToggled ) ) + if( label ) { - mToggled = toggle; + label.SetAnchorPoint( AnchorPoint::CENTER ); + label.SetParentOrigin( ParentOrigin::CENTER ); - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been toggled. - GetPushButtonPainter( mPainter )->Toggled( handle ); + 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 ); + } - // Emit signal. - mToggledSignalV2.Emit( handle, mToggled ); + ConfigureSizeNegotiation(); } } -bool PushButton::IsToggled() const -{ - return mToggleButton && mToggled; -} - -void PushButton::SetButtonImage( Image image ) -{ - SetButtonImage( ImageActor::New( image ) ); -} - -void PushButton::SetButtonImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetButtonImage( handle, image ); -} - -Actor& PushButton::GetButtonImage() -{ - return mButtonImage; -} - -Actor PushButton::GetButtonImage() const -{ - return mButtonImage; -} - -void PushButton::SetBackgroundImage( Image image ) -{ - SetBackgroundImage( ImageActor::New( image ) ); -} - -void PushButton::SetBackgroundImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetBackgroundImage( handle, image ); -} - -Actor& PushButton::GetBackgroundImage() -{ - return mBackgroundImage; -} - -Actor PushButton::GetBackgroundImage() const -{ - return mBackgroundImage; -} - -void PushButton::SetPressedImage( Image image ) -{ - SetPressedImage( ImageActor::New( image ) ); -} - -void PushButton::SetPressedImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetPressedImage( handle, image ); -} - -Actor& PushButton::GetPressedImage() -{ - return mPressedImage; -} - -Actor PushButton::GetPressedImage() const -{ - return mPressedImage; -} - -void PushButton::SetDimmedBackgroundImage( Image image ) -{ - SetDimmedBackgroundImage( ImageActor::New( image ) ); -} - -void PushButton::SetDimmedBackgroundImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetDimmedBackgroundImage( handle, image ); -} - -Actor& PushButton::GetDimmedBackgroundImage() -{ - return mDimmedBackgroundImage; -} - -Actor PushButton::GetDimmedBackgroundImage() const -{ - return mDimmedBackgroundImage; -} - -void PushButton::SetDimmedImage( Image image ) -{ - SetDimmedImage( ImageActor::New( image ) ); -} - -void PushButton::SetDimmedImage( Actor image ) -{ - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetDimmedImage( handle, image ); -} - -Actor& PushButton::GetDimmedImage() -{ - return mDimmedImage; -} - -Actor PushButton::GetDimmedImage() const -{ - return mDimmedImage; -} - -void PushButton::SetLabelText( const std::string& text ) +void PushButton::OnButtonImageSet() { - Toolkit::TextView textView ( Toolkit::TextView::New( text ) ); - textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button - SetLabelText( textView ); + ConfigureSizeNegotiation(); + RelayoutRequest(); } -void PushButton::SetLabelText( Actor text ) +void PushButton::OnSelectedImageSet() { - Toolkit::PushButton handle( GetOwner() ); - GetPushButtonPainter( mPainter )->SetLabelText( handle, text ); + ConfigureSizeNegotiation(); + RelayoutRequest(); } -Actor& PushButton::GetLabel() +void PushButton::OnBackgroundImageSet() { - return mLabel; + ConfigureSizeNegotiation(); + RelayoutRequest(); } -Actor PushButton::GetLabelText() const +void PushButton::OnSelectedBackgroundImageSet() { - return mLabel; + ConfigureSizeNegotiation(); + RelayoutRequest(); } -Actor& PushButton::GetFadeOutBackgroundImage() +void PushButton::OnDisabledImageSet() { - return mFadeOutBackgroundImage; + ConfigureSizeNegotiation(); + RelayoutRequest(); } -Actor& PushButton::GetFadeOutButtonImage() +void PushButton::OnDisabledBackgroundImageSet() { - return mFadeOutButtonImage; + ConfigureSizeNegotiation(); + RelayoutRequest(); } -Toolkit::PushButton::ToggledSignalV2& PushButton::ToggledSignal() +bool PushButton::OnSelected() { - return mToggledSignalV2; -} + Actor& buttonImage = GetButtonImage(); + Actor& selectedImage = GetSelectedImage(); + Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); -Toolkit::PushButton::PressedSignalV2& PushButton::PressedSignal() -{ - return mPressedSignalV2; -} + PaintState paintState = GetPaintState(); -Toolkit::PushButton::ReleasedSignalV2& PushButton::ReleasedSignal() -{ - return mReleasedSignalV2; -} - -bool PushButton::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor ) -{ - Dali::BaseHandle handle( object ); + switch( paintState ) + { + 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(); + } - bool connected( true ); - Toolkit::PushButton button = Toolkit::PushButton::DownCast(handle); + 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(); + } - if( Toolkit::PushButton::SIGNAL_TOGGLED == signalName ) - { - button.ToggledSignal().Connect( tracker, functor ); - } - else if( Toolkit::PushButton::SIGNAL_PRESSED == signalName ) - { - button.PressedSignal().Connect( tracker, functor ); - } - else if( Toolkit::PushButton::SIGNAL_RELEASED == signalName ) - { - button.ReleasedSignal().Connect( tracker, functor ); + 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; + } } - else + + if( mTransitionAnimation ) { - // signalName does not match any signal - connected = false; + return true; } - return connected; + return false; } -void PushButton::SetProperty( BaseObject* object, Property::Index propertyIndex, const Property::Value& value ) +bool PushButton::OnDisabled() { - Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) ); + Actor& buttonImage = GetButtonImage(); + Actor& selectedImage = GetSelectedImage(); + Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); + Actor& backgroundImage = GetBackgroundImage(); + Actor& disabledImage = GetDisabledImage(); + Actor& disabledSelectedImage = GetDisabledSelectedImage(); + Actor& disabledBackgroundImage = GetDisabledBackgroundImage(); - if ( pushButton ) - { - PushButton& pushButtonImpl( GetImplementation( pushButton ) ); + PaintState paintState = GetPaintState(); - switch ( propertyIndex ) + switch( paintState ) + { + case UnselectedState: { - case Toolkit::PushButton::PROPERTY_AUTO_REPEATING: - { - pushButtonImpl.SetAutoRepeating( value.Get< bool >() ); - break; - } - - case Toolkit::PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY: - { - pushButtonImpl.SetInitialAutoRepeatingDelay( value.Get< float >() ); - break; - } - - case Toolkit::PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY: + 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 ) { - pushButtonImpl.SetNextAutoRepeatingDelay( value.Get< float >() ); - break; + opacity = selectedImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_TOGGLABLE: + StopTransitionAnimation(); + FadeOutImage( backgroundImage ); + FadeOutImage( selectedBackgroundImage, opacity ); + FadeOutImage( selectedImage, opacity ); + FadeInImage( disabledBackgroundImage ); + FadeInImage( disabledSelectedImage ); + StartTransitionAnimation(); + break; + } + case SelectedUnselectedTransition: + { + float opacity = 1.f; + if( buttonImage ) { - pushButtonImpl.SetToggleButton( value.Get< bool >() ); - break; + opacity = buttonImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_TOGGLE: + StopTransitionAnimation(); + FadeOutImage( backgroundImage ); + FadeOutImage( buttonImage, opacity ); + FadeInImage( disabledBackgroundImage ); + FadeInImage( disabledImage ); + StartTransitionAnimation(); + break; + } + case UnselectedDisabledTransition: + { + float opacity = 1.f; + if( disabledImage ) { - pushButtonImpl.SetToggled( value.Get< bool >() ); - break; + opacity = disabledImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_BUTTON_IMAGE: + 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 ) { - Image image = Image::New( value.Get() ); - pushButtonImpl.SetButtonImage( image ); - break; + opacity = buttonImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_PRESSED_IMAGE: + 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 ) { - Image image = Image::New( value.Get() ); - pushButtonImpl.SetPressedImage( image ); - break; + opacity = disabledSelectedImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_DIMMED_IMAGE: + 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 ) { - Image image = Image::New( value.Get() ); - pushButtonImpl.SetDimmedImage( image ); - break; + opacity = selectedImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_LABEL_TEXT: - { - pushButtonImpl.SetLabelText( value.Get< std::string >() ); - break; - } + StopTransitionAnimation( false ); + FadeOutImage( backgroundImage, opacity ); + FadeOutImage( selectedBackgroundImage, opacity ); + FadeOutImage( selectedImage, opacity ); + FadeInImage( disabledBackgroundImage, 1.f - opacity ); + FadeInImage( disabledSelectedImage, 1.f - opacity ); + StartTransitionAnimation(); + break; } } + + if( mTransitionAnimation ) + { + return true; + } + + return false; } -Property::Value PushButton::GetProperty( BaseObject* object, Property::Index propertyIndex ) +bool PushButton::OnPressed() { - Property::Value value; + Actor& buttonImage = GetButtonImage(); + Actor& selectedImage = GetSelectedImage(); + Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); - Toolkit::PushButton pushButton = Toolkit::PushButton::DownCast( Dali::BaseHandle( object ) ); + PaintState paintState = GetPaintState(); - if ( pushButton ) + switch( paintState ) { - PushButton& pushButtonImpl( GetImplementation( pushButton ) ); - - switch ( propertyIndex ) + case UnselectedState: { - case Toolkit::PushButton::PROPERTY_AUTO_REPEATING: + FadeOutImage( buttonImage ); + FadeInImage( selectedBackgroundImage ); + FadeInImage( selectedImage ); + StartTransitionAnimation(); + break; + } + case SelectedUnselectedTransition: + { + float opacity = 1.f; + if( buttonImage ) { - value = pushButtonImpl.mAutoRepeating; - break; + opacity = buttonImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_INITIAL_AUTO_REPEATING_DELAY: + 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 ) { - value = pushButtonImpl.mInitialAutoRepeatingDelay; - break; + opacity = buttonImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_NEXT_AUTO_REPEATING_DELAY: - { - value = pushButtonImpl.mNextAutoRepeatingDelay; - break; - } + StopTransitionAnimation(); + FadeOutImage( buttonImage, opacity ); + FadeInImage( selectedBackgroundImage ); + FadeInImage( selectedImage ); + StartTransitionAnimation(); + break; + } + default: + break; + } - case Toolkit::PushButton::PROPERTY_TOGGLABLE: - { - value = pushButtonImpl.mToggleButton; - break; - } + if( mTransitionAnimation ) + { + return true; + } - case Toolkit::PushButton::PROPERTY_TOGGLE: - { - value = pushButtonImpl.mToggled; - break; - } + return false; +} - case Toolkit::PushButton::PROPERTY_BUTTON_IMAGE: - { - std::string path; - GetImageActorFilename( pushButtonImpl.mButtonImage, path ); - value = path; - break; - } +bool PushButton::OnReleased() +{ + Actor& buttonImage = GetButtonImage(); + Actor& selectedImage = GetSelectedImage(); + Actor& selectedBackgroundImage = GetSelectedBackgroundImage(); - case Toolkit::PushButton::PROPERTY_PRESSED_IMAGE: - { - std::string path; - GetImageActorFilename( pushButtonImpl.mPressedImage, path ); - value = path; - break; - } + PaintState paintState = GetPaintState(); - case Toolkit::PushButton::PROPERTY_DIMMED_IMAGE: + switch( paintState ) + { + case SelectedState: + { + FadeOutImage( selectedBackgroundImage ); + FadeOutImage( selectedImage ); + FadeInImage( buttonImage ); + StartTransitionAnimation(); + break; + } + case UnselectedSelectedTransition: + { + float opacity = 1.f; + if( selectedImage ) { - std::string path; - GetImageActorFilename( pushButtonImpl.mDimmedImage, path ); - value = path; - break; + opacity = selectedImage.GetCurrentOpacity(); } - case Toolkit::PushButton::PROPERTY_LABEL_TEXT: + StopTransitionAnimation( false ); + FadeOutImage( selectedBackgroundImage, opacity ); + FadeOutImage( selectedImage, opacity ); + FadeInImage( buttonImage, 1.f - opacity ); + StartTransitionAnimation(); + break; + } + case DisabledSelectedTransition: + { + float opacity = 1.f; + if( selectedImage ) { - value = ""; // Just return an empty string if not using a TextView - - if ( pushButtonImpl.mLabel ) - { - Toolkit::TextView textView = Toolkit::TextView::DownCast( pushButtonImpl.mLabel ); - if ( textView ) - { - value = textView.GetText(); - } - } - break; + opacity = selectedImage.GetCurrentOpacity(); } + + StopTransitionAnimation(); + FadeOutImage( selectedBackgroundImage, opacity ); + FadeOutImage( selectedImage, opacity ); + FadeInImage( buttonImage ); + StartTransitionAnimation(); + break; } + default: + { + break; + } + } + + if( mTransitionAnimation ) + { + return true; } - return value; + return false; } -void PushButton::OnButtonInitialize() +void PushButton::StopAllAnimations() { - // Push button requires the Leave event. - Actor root = Self(); - root.SetLeaveRequired( true ); + StopTransitionAnimation(); } -void PushButton::OnButtonDown() +void PushButton::OnSizeSet( const Vector3& targetSize ) { - if( !mToggleButton ) + if( targetSize != mSize ) { - Toolkit::PushButton handle( GetOwner() ); + mSize = targetSize; - // Notifies the painter the button has been pressed. - GetPushButtonPainter( mPainter )->Pressed( handle ); + Actor& label = GetLabel(); - if( mAutoRepeating ) + if( label ) { - SetUpTimer( mInitialAutoRepeatingDelay ); + label.SetSize( mSize ); } + } +} - //Emit signal. - mPressedSignalV2.Emit( handle ); +void PushButton::StartTransitionAnimation() +{ + if( mTransitionAnimation ) + { + mTransitionAnimation.FinishedSignal().Connect( this, &PushButton::TransitionAnimationFinished ); + mTransitionAnimation.Play(); } } -void PushButton::OnButtonUp() +void PushButton::StopTransitionAnimation( bool remove ) { - if( ButtonDown == mState ) + if( mTransitionAnimation ) { - if( mToggleButton ) - { - mToggled = !mToggled; + mTransitionAnimation.Clear(); + mTransitionAnimation.Reset(); + } - Toolkit::PushButton handle( GetOwner() ); + if( remove ) + { + UpdatePaintTransitionState(); + } +} - // Notifies the painter the button has been toggled. - GetPushButtonPainter( mPainter )->Toggled( handle ); +void PushButton::FadeInImage( Actor& image, float opacity, Vector3 scale ) +{ + if( image ) + { + image.SetOpacity( opacity ); + image.SetScale( scale ); - //Emit signal. - mToggledSignalV2.Emit( handle, mToggled ); - } - else + if( !mTransitionAnimation ) { - Toolkit::PushButton handle( GetOwner() ); - - // Notifies the painter the button has been clicked. - GetPushButtonPainter( mPainter )->Released( handle ); - GetPushButtonPainter( mPainter )->Clicked( handle ); - - if( mAutoRepeating ) - { - mAutoRepeatingTimer.Reset(); - } - - //Emit signal. - mReleasedSignalV2.Emit( handle ); - mClickedSignalV2.Emit( handle ); + mTransitionAnimation = Dali::Animation::New( GetAnimationTime() ); } + + mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 1.f ); } } -void PushButton::OnTouchPointLeave() +void PushButton::FadeOutImage( Actor& image, float opacity, Vector3 scale ) { - if( ButtonDown == mState ) + if( image ) { - if( !mToggleButton ) - { - Toolkit::PushButton handle( GetOwner() ); + image.SetOpacity( opacity ); + image.SetScale( scale ); - // Notifies the painter the button has been released. - GetPushButtonPainter( mPainter )->Released( handle ); - - if( mAutoRepeating ) - { - mAutoRepeatingTimer.Reset(); - } - - //Emit signal. - mReleasedSignalV2.Emit( handle ); + if( !mTransitionAnimation ) + { + mTransitionAnimation = Dali::Animation::New( GetAnimationTime() ); } - } -} -void PushButton::OnTouchPointInterrupted() -{ - OnTouchPointLeave(); + mTransitionAnimation.AnimateTo( Property( image, Actor::Property::COLOR_ALPHA ), 0.f ); + } } -void PushButton::OnAnimationTimeSet( float animationTime ) +void PushButton::TransitionAnimationFinished( Dali::Animation& source ) { - GetPushButtonPainter( mPainter )->SetAnimationTime( animationTime ); + StopTransitionAnimation(); } -float PushButton::OnAnimationTimeRequested() const +Vector3 PushButton::GetNaturalSize() { - return GetPushButtonPainter( mPainter )->GetAnimationTime(); -} + Vector3 size; -PushButton::PushButton() -: Button(), - mAutoRepeating( false ), - mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ), - mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ), - mToggleButton( false ), - mAutoRepeatingTimer(), - mToggled( false ), - mClickActionPerforming(false) -{ - // Creates specific painter. - mPainter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() ); -} - -PushButton::~PushButton() -{ - if( mAutoRepeatingTimer ) + // If label, test against it's size + Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( GetLabel() ); + if( label ) { - mAutoRepeatingTimer.Reset(); + size.width = std::max( size.width, label.GetRelayoutSize( Dimension::WIDTH ) ); + size.height = std::max( size.height, label.GetRelayoutSize( Dimension::HEIGHT ) ); + } + else + { + // Check Image and Background image and use the largest size as the control's Natural size. + SizeOfActorIfLarger( GetButtonImage(), size ); + SizeOfActorIfLarger( GetBackgroundImage(), size ); } - mPainter = NULL; + return size; } -void PushButton::SetUpTimer( float delay ) +void PushButton::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) { - mAutoRepeatingTimer = Dali::Timer::New( static_cast( 1000.f * delay ) ); - mAutoRepeatingTimer.TickSignal().Connect( this, &PushButton::AutoRepeatingSlot ); - mAutoRepeatingTimer.Start(); + ConfigureSizeNegotiation(); } -bool PushButton::AutoRepeatingSlot() +void PushButton::ConfigureSizeNegotiation() { - bool consumed = false; - if( !mDimmed ) - { - // Restart the autorepeat timer. - SetUpTimer( mNextAutoRepeatingDelay ); + std::vector< Actor > images; + images.reserve( 7 ); - Toolkit::PushButton handle( GetOwner() ); + 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() ); - // Notifies the painter the button has been pressed. - GetPushButtonPainter( mPainter )->Pressed( handle ); + Actor label = GetLabel(); - //Emit signal. - consumed = mReleasedSignalV2.Emit( handle ); - consumed |= mClickedSignalV2.Emit( handle ); - consumed |= mPressedSignalV2.Emit( handle ); - } + for( unsigned int i = 0; i < Dimension::DIMENSION_COUNT; ++i ) + { + ConfigureSizeNegotiationDimension( static_cast< Dimension::Type >( 1 << i ), images, label ); + } - return consumed; -} + if( label ) + { + Padding padding; -void PushButton::OnActivated() -{ - // When the button is activated, it performs the click action - std::vector attributes; - DoClickAction(attributes); -} + if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::USE_NATURAL_SIZE ) + { + padding.left = TEXT_PADDING; + padding.right = TEXT_PADDING; + } -void PushButton::DoClickAction(const PropertyValueContainer& attributes) -{ - // Prevents the button signals from doing a recursive loop by sending an action - // and re-emitting the signals. - if(!mClickActionPerforming) - { - mClickActionPerforming = true; - OnButtonDown(); - mState = ButtonDown; - OnButtonUp(); - mClickActionPerforming = false; + if( label.GetResizePolicy( Dimension::HEIGHT ) == ResizePolicy::USE_NATURAL_SIZE ) + { + padding.top = TEXT_PADDING; + padding.bottom = TEXT_PADDING; + } + + label.SetPadding( padding ); } } -bool PushButton::DoAction(BaseObject* object, const std::string& actionName, const std::vector& attributes) +void PushButton::ConfigureSizeNegotiationDimension( Dimension::Type dimension, const std::vector< Actor >& images, Actor& label ) { - bool ret = false; + ResizePolicy::Type imageResizePolicy = ResizePolicy::FILL_TO_PARENT; + ResizePolicy::Type labelResizePolicy = ResizePolicy::FILL_TO_PARENT; - Dali::BaseHandle handle(object); - - Toolkit::PushButton button = Toolkit::PushButton::DownCast(handle); - - DALI_ASSERT_ALWAYS(button); + switch( Self().GetResizePolicy( dimension ) ) + { + case ResizePolicy::FIT_TO_CHILDREN: + { + imageResizePolicy = labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + break; + } + case ResizePolicy::USE_NATURAL_SIZE: + { + if( label ) + { + labelResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + } + else + { + imageResizePolicy = ResizePolicy::USE_NATURAL_SIZE; + } + break; + } + default: + { + break; + } + } - if(Toolkit::PushButton::ACTION_PUSH_BUTTON_CLICK == actionName) + if( label ) { - GetImplementation(button).DoClickAction(attributes); - ret = true; + label.SetResizePolicy( labelResizePolicy, dimension ); } - return ret; + for( std::vector< Actor >::const_iterator it = images.begin(), itEnd = images.end(); it != itEnd; ++it ) + { + Actor actor = *it; + if( actor ) + { + actor.SetResizePolicy( imageResizePolicy, dimension ); + } + } } } // namespace Internal