X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fcontrols%2Fbuttons%2Fbutton-impl.cpp;h=1af6848989ca93f00468a95ea4aeac6ddc09bbc0;hp=eef0cd8970d93e1b7fb71b53017ac67316b3b69c;hb=refs%2Fchanges%2F99%2F210399%2F9;hpb=87f440c3d0367ca5a7027eef8d01d79b8703cf05 diff --git a/dali-toolkit/internal/controls/buttons/button-impl.cpp b/dali-toolkit/internal/controls/buttons/button-impl.cpp index eef0cd8..1af6848 100644 --- a/dali-toolkit/internal/controls/buttons/button-impl.cpp +++ b/dali-toolkit/internal/controls/buttons/button-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2019 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,34 @@ #include "button-impl.h" // EXTERNAL INCLUDES -#include +#include // for strcmp +#include +#include +#include +#include #include -#include -#include -#include +#include +#include +#include +#include + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(DEBUG_ENABLED) + Debug::Filter* gLogButtonFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_BUTTON_CONTROL"); +#endif namespace Dali { @@ -31,362 +54,373 @@ namespace Dali namespace Toolkit { -const Property::Index Button::PROPERTY_DISABLED = Internal::Button::BUTTON_PROPERTY_START_INDEX; -const Property::Index Button::PROPERTY_AUTO_REPEATING = Internal::Button::BUTTON_PROPERTY_START_INDEX + 1; -const Property::Index Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 2; -const Property::Index Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY = Internal::Button::BUTTON_PROPERTY_START_INDEX + 3; -const Property::Index Button::PROPERTY_TOGGLABLE = Internal::Button::BUTTON_PROPERTY_START_INDEX + 4; -const Property::Index Button::PROPERTY_SELECTED = Internal::Button::BUTTON_PROPERTY_START_INDEX + 5; -const Property::Index Button::PROPERTY_NORMAL_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 6; -const Property::Index Button::PROPERTY_SELECTED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 7; -const Property::Index Button::PROPERTY_DISABLED_STATE_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 8; -const Property::Index Button::PROPERTY_LABEL_ACTOR = Internal::Button::BUTTON_PROPERTY_START_INDEX + 9; - namespace Internal { namespace { -const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f ); -const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f ); - BaseHandle Create() { // empty handle as we cannot create button (but type registered for clicked signal) return BaseHandle(); } -TypeRegistration typeRegistration( typeid(Toolkit::Button), typeid(Toolkit::Control), Create ); - -SignalConnectorType signalConnector1( typeRegistration, Toolkit::Button::SIGNAL_PRESSED , &Button::DoConnectSignal ); -SignalConnectorType signalConnector2( typeRegistration, Toolkit::Button::SIGNAL_RELEASED, &Button::DoConnectSignal ); -SignalConnectorType signalConnector3( typeRegistration, Toolkit::Button::SIGNAL_CLICKED, &Button::DoConnectSignal ); -SignalConnectorType signalConnector4( typeRegistration, Toolkit::Button::SIGNAL_STATE_CHANGED, &Button::DoConnectSignal ); - -TypeAction action1( typeRegistration, Toolkit::Button::ACTION_BUTTON_CLICK, &Button::DoAction ); - -PropertyRegistration property1( typeRegistration, "disabled", Toolkit::Button::PROPERTY_DISABLED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property2( typeRegistration, "auto-repeating", Toolkit::Button::PROPERTY_AUTO_REPEATING, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property3( typeRegistration, "initial-auto-repeating-delay", Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property4( typeRegistration, "next-auto-repeating-delay", Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY, Property::FLOAT, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property5( typeRegistration, "togglable", Toolkit::Button::PROPERTY_TOGGLABLE, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property6( typeRegistration, "selected", Toolkit::Button::PROPERTY_SELECTED, Property::BOOLEAN, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property7( typeRegistration, "normal-state-actor", Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property8( typeRegistration, "selected-state-actor", Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property9( typeRegistration, "disabled-state-actor", Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty ); -PropertyRegistration property10( typeRegistration, "label-actor", Toolkit::Button::PROPERTY_LABEL_ACTOR, Property::MAP, &Button::SetProperty, &Button::GetProperty ); +// Setup properties, signals and actions using the type-registry. +DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create ) + +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled", BOOLEAN, DISABLED ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "autoRepeating", BOOLEAN, AUTO_REPEATING ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "initialAutoRepeatingDelay", FLOAT, INITIAL_AUTO_REPEATING_DELAY ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "nextAutoRepeatingDelay", FLOAT, NEXT_AUTO_REPEATING_DELAY ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "togglable", BOOLEAN, TOGGLABLE ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected", BOOLEAN, SELECTED ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselectedVisual", MAP, UNSELECTED_VISUAL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selectedVisual", MAP, SELECTED_VISUAL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabledSelectedVisual", MAP, DISABLED_SELECTED_VISUAL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabledUnselectedVisual", MAP, DISABLED_UNSELECTED_VISUAL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselectedBackgroundVisual", MAP, UNSELECTED_BACKGROUND_VISUAL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "label", MAP, LABEL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selectedBackgroundVisual", MAP, SELECTED_BACKGROUND_VISUAL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabledUnselectedBackgroundVisual", MAP, DISABLED_UNSELECTED_BACKGROUND_VISUAL ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabledSelectedBackgroundVisual", MAP, DISABLED_SELECTED_BACKGROUND_VISUAL ) +DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, Button, "labelRelativeAlignment", STRING, LABEL_RELATIVE_ALIGNMENT ) +DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, Button, "labelPadding", VECTOR4, LABEL_PADDING ) +DALI_DEVEL_PROPERTY_REGISTRATION( Toolkit, Button, "visualPadding", VECTOR4, VISUAL_PADDING ) + +// Signals: +DALI_SIGNAL_REGISTRATION( Toolkit, Button, "pressed", SIGNAL_PRESSED ) +DALI_SIGNAL_REGISTRATION( Toolkit, Button, "released", SIGNAL_RELEASED ) +DALI_SIGNAL_REGISTRATION( Toolkit, Button, "clicked", SIGNAL_CLICKED ) +DALI_SIGNAL_REGISTRATION( Toolkit, Button, "stateChanged", SIGNAL_STATE_CHANGED ) + +// Actions: +DALI_ACTION_REGISTRATION( Toolkit, Button, "buttonClick", ACTION_BUTTON_CLICK ) + +DALI_TYPE_REGISTRATION_END() + +DALI_ENUM_TO_STRING_TABLE_BEGIN( ALIGNMENT ) +DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Internal::Button, BEGIN ) +DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Internal::Button, END ) +DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Internal::Button, TOP ) +DALI_ENUM_TO_STRING_WITH_SCOPE( Toolkit::Internal::Button, BOTTOM ) +DALI_ENUM_TO_STRING_TABLE_END( ALIGNMENT ) + +const Scripting::StringEnum ALIGNMENT_STRING_TABLE[] = +{ + { "BEGIN", Button::BEGIN }, + { "END", Button::END }, + { "TOP", Button::TOP }, + { "BOTTOM", Button::BOTTOM }, +}; + +const unsigned int ALIGNMENT_STRING_TABLE_COUNT = sizeof( ALIGNMENT_STRING_TABLE ) / sizeof( ALIGNMENT_STRING_TABLE[0] ); + +const Property::Index VISUAL_INDEX_FOR_STATE[][Button::STATE_COUNT] = +{ + { Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, Toolkit::Button::Property::UNSELECTED_VISUAL }, + { Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, Toolkit::Button::Property::SELECTED_VISUAL }, + { Toolkit::Button::Property::DISABLED_UNSELECTED_BACKGROUND_VISUAL, Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL }, + { Toolkit::Button::Property::DISABLED_SELECTED_BACKGROUND_VISUAL, Toolkit::Button::Property::DISABLED_SELECTED_VISUAL } +}; + +/** + * Checks if given map contains a text string + */ +bool MapContainsTextString( Property::Map& map ) +{ + bool result = false; + Property::Value* value = map.Find( Toolkit::TextVisual::Property::TEXT ); + if ( value ) + { + std::string textString; + value->Get( textString ); + if ( !textString.empty() ) + { + result = true; + } + } + return result; +} } // unnamed namespace Button::Button() -: Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ), - mTogglableButton( false ), - mSelected( false ), - mPainter( NULL ), +: Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ), mAutoRepeatingTimer(), - mDisabled( false ), + mTextLabelAlignment( END ), mAutoRepeating( false ), -// mTogglableButton( false ), -// mSelected( false ), - mInitialAutoRepeatingDelay( INITIAL_AUTOREPEATING_DELAY ), - mNextAutoRepeatingDelay( NEXT_AUTOREPEATING_DELAY ), - mClickActionPerforming( false ), - mState( ButtonUp ) + mTogglableButton( false ), + mTextStringSetFlag( false ), + mInitialAutoRepeatingDelay( 0.0f ), + mNextAutoRepeatingDelay( 0.0f ), + mAnimationTime( 0.0f ), + mButtonPressedState( UNPRESSED ), + mButtonState( UNSELECTED_STATE ), + mPreviousButtonState( mButtonState ), + mClickActionPerforming( false ) { } Button::~Button() { - if( mAutoRepeatingTimer ) - { - mAutoRepeatingTimer.Reset(); - } -} - -void Button::SetDisabled( bool disabled ) -{ - mDisabled = disabled; - - // Notifies the painter. - Toolkit::Button handle( GetOwner() ); - if( mPainter ) - { - mPainter->SetDisabled( handle, mDisabled ); - } -} - -bool Button::IsDisabled() const -{ - return mDisabled; } void Button::SetAutoRepeating( bool autoRepeating ) { mAutoRepeating = autoRepeating; - // An autorepeating button can't be a togglable button. + // An autorepeating button can't be a toggle button. if( autoRepeating ) { - mTogglableButton = false; - if( mSelected ) + if( IsSelected() ) { - // Emit a signal is not wanted, only change the appearance. - Toolkit::Button handle( GetOwner() ); - mPainter->Selected( handle ); - mSelected = false; + SetSelected( false ); // UnSelect before switching off Toggle feature. } + mTogglableButton = false; } - - // Notifies the painter. - mPainter->SetAutoRepeating( mAutoRepeating ); -} - -bool Button::IsAutoRepeating() const -{ - return mAutoRepeating; } void Button::SetInitialAutoRepeatingDelay( float initialAutoRepeatingDelay ) { - DALI_ASSERT_ALWAYS( initialAutoRepeatingDelay > 0.f ); + DALI_ASSERT_DEBUG( initialAutoRepeatingDelay > 0.f ); mInitialAutoRepeatingDelay = initialAutoRepeatingDelay; } -float Button::GetInitialAutoRepeatingDelay() const -{ - return mInitialAutoRepeatingDelay; -} - void Button::SetNextAutoRepeatingDelay( float nextAutoRepeatingDelay ) { - DALI_ASSERT_ALWAYS( nextAutoRepeatingDelay > 0.f ); + DALI_ASSERT_DEBUG( nextAutoRepeatingDelay > 0.f ); mNextAutoRepeatingDelay = nextAutoRepeatingDelay; } -float Button::GetNextAutoRepeatingDelay() const -{ - return mNextAutoRepeatingDelay; -} - void Button::SetTogglableButton( bool togglable ) { mTogglableButton = togglable; - // A togglable button can't be an autorepeating button. + // A toggle button can't be an autorepeating button. if( togglable ) { mAutoRepeating = false; - - // Notifies the painter. - mPainter->SetAutoRepeating( mAutoRepeating ); } } -bool Button::IsTogglableButton() const -{ - return mTogglableButton; -} - void Button::SetSelected( bool selected ) { - if( !mDisabled && mTogglableButton && ( selected != mSelected ) ) + if( mTogglableButton ) { - mSelected = selected; - - Toolkit::Button handle( GetOwner() ); + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::SetSelected (%s)\n", (selected?"true":"false") ); - // Notifies the painter the button has been selected. - mPainter->Selected( handle ); - - // Emit signal. - mStateChangedSignal.Emit( handle ); + if ( selected && ( mButtonState != SELECTED_STATE ) ) + { + ChangeState( SELECTED_STATE ); + } + else if ( !selected && ( mButtonState != UNSELECTED_STATE ) ) + { + ChangeState( UNSELECTED_STATE ); + } } } -bool Button::IsSelected() const -{ - return mTogglableButton && mSelected; -} - -void Button::SetAnimationTime( float animationTime ) -{ - OnAnimationTimeSet( animationTime ); -} - -float Button::GetAnimationTime() const -{ - return OnAnimationTimeRequested(); -} - -void Button::SetLabel( const std::string& label ) -{ - Toolkit::TextView textView = Toolkit::TextView::New( label ); - textView.SetWidthExceedPolicy( Toolkit::TextView::ShrinkToFit ); // Make sure our text always fits inside the button - SetLabel( textView ); - - RelayoutRequest(); -} - -void Button::SetLabel( Actor label ) -{ - Toolkit::Button handle( GetOwner() ); - - mPainter->SetLabel( handle, label ); -} - -Actor Button::GetLabel() const -{ - return mLabel; -} - -Actor& Button::GetLabel() -{ - return mLabel; -} - -void Button::SetButtonImage( Image image ) -{ - SetButtonImage( ImageActor::New( image ) ); -} - -void Button::SetButtonImage( Actor image ) +void Button::SetDisabled( bool disabled ) { - Toolkit::Button handle( GetOwner() ); - mPainter->SetButtonImage( handle, image ); -} + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::SetDisabled(%s) state(%d)\n", (disabled)?"disabled":"active", mButtonState ); -Actor Button::GetButtonImage() const -{ - return mButtonImage; + if ( disabled ) + { + if ( mButtonState == SELECTED_STATE ) + { + ChangeState( DISABLED_SELECTED_STATE ); + } + else if ( mButtonState == UNSELECTED_STATE ) + { + ChangeState( DISABLED_UNSELECTED_STATE ); + } + } + else + { + if ( mButtonState == DISABLED_SELECTED_STATE ) + { + ChangeState( SELECTED_STATE ); + } + else if ( mButtonState == DISABLED_UNSELECTED_STATE ) + { + ChangeState( UNSELECTED_STATE ); + } + } } -Actor& Button::GetButtonImage() +bool Button::IsDisabled() const { - return mButtonImage; + return ( mButtonState == DISABLED_SELECTED_STATE || mButtonState == DISABLED_UNSELECTED_STATE ) ; } -void Button::SetSelectedImage( Image image ) +bool Button::ValidateState( State requestedState ) { - SetSelectedImage( ImageActor::New( image ) ); -} + /* Below tables shows allowed state transitions + * Match rows in first column to following columns, if true then transition allowed. + * eg UNSELECTED_STATE to DISABLED_UNSELECTED_STATE is true so state transition allowed. + * + to| UNSELECTED_STATE | SELECTED_STATE | DISABLED_UNSELECTED_STATE | DISABLED_SELECTED_STATE |*/ + /* from*/ + bool transitionTable[4][4] = { /* UNSELECTED_STATE*/ { false, true, true, false }, + /* SELECTED_STATE*/ { true, false, false, true }, + /* DISABLED_UNSELECTED_STATE*/{ true, true, false, false }, + /* DISABLED_SELECTED_STATE*/ { false, true, false, false } + }; -void Button::SetSelectedImage( Actor image ) -{ - Toolkit::Button handle( GetOwner() ); - mPainter->SetSelectedImage( handle, image ); -} + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::ValidateState ReuestedState:%d, CurrentState:%d, result:%s\n", + requestedState, mButtonState, (transitionTable[mButtonState][requestedState])?"change-accepted":"change-denied"); -Actor Button::GetSelectedImage() const -{ - return mSelectedImage; + return transitionTable[mButtonState][requestedState]; } -Actor& Button::GetSelectedImage() +void Button::ChangeState( State requestedState ) { - return mSelectedImage; -} + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::ChangeState ReuestedState(%d)\n", requestedState ); -void Button::SetBackgroundImage( Image image ) -{ - SetBackgroundImage( ImageActor::New( image ) ); -} - -void Button::SetBackgroundImage( Actor image ) -{ - Toolkit::Button handle( GetOwner() ); - mPainter->SetBackgroundImage( handle, image ); -} - -Actor Button::GetBackgroundImage() const -{ - return mBackgroundImage; -} + // Validate State before changing + if ( !ValidateState( requestedState )) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::ChangeState ReuestedState(%d) not validated\n", requestedState ); + return; + } -Actor& Button::GetBackgroundImage() -{ - return mBackgroundImage; -} + // If not on stage the button could have still been set to selected so update state + mPreviousButtonState = mButtonState; // Store previous state for visual removal (used when animations ended) + mButtonState = requestedState; // Update current state -void Button::SetDisabledImage( Image image ) -{ - SetDisabledImage( ImageActor::New( image ) ); -} + if ( Self().OnStage() ) + { + OnStateChange( mButtonState ); // Notify derived buttons + SelectRequiredVisual( VISUAL_INDEX_FOR_STATE[ mButtonState ][ BACKGROUND ] ); + SelectRequiredVisual( VISUAL_INDEX_FOR_STATE[ mButtonState ][ FOREGROUND ] ); + // If animation supported then visual removal should be performed after any transition animation has completed. + // If Required Visual is not loaded before current visual is removed then a flickering will be evident. + // Derived button can override OnButtonVisualRemoval + OnButtonVisualRemoval( VISUAL_INDEX_FOR_STATE[ mPreviousButtonState ][ BACKGROUND ] ); + OnButtonVisualRemoval( VISUAL_INDEX_FOR_STATE[ mPreviousButtonState ][ FOREGROUND ] ); + RelayoutRequest(); + } -void Button::SetDisabledImage( Actor image ) -{ Toolkit::Button handle( GetOwner() ); - mPainter->SetDisabledImage( handle, image ); + // Emit signal. + mStateChangedSignal.Emit( handle ); } -Actor Button::GetDisabledImage() const +bool Button::IsSelected() const { - return mDisabledImage; + bool selected = ( mButtonState == SELECTED_STATE ) || ( mButtonState == DISABLED_SELECTED_STATE ); + return mTogglableButton && selected; } -Actor& Button::GetDisabledImage() +void Button::MergeWithExistingLabelProperties( const Property::Map& inMap, Property::Map& outMap ) { - return mDisabledImage; -} + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "MergeLabelProperties with %d properties\n", inMap.Count() ); -void Button::SetDisabledSelectedImage( Image image ) -{ - SetDisabledSelectedImage( ImageActor::New( image ) ); -} + /** + * Properties for the Label visual could be from a style sheet but after being set the "TEXT" property could be set. + * Hence would need to create the Text Visual with the complete merged set of properties. + * + * 1) Find Label Visual + * 2) Retrieve current properties ( settings ) + * 3) Merge with new properties ( settings ) + * 4) Return new merged map + */ + Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, Toolkit::Button::Property::LABEL ); + if ( visual ) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "MergeLabelProperties Visual already exists, retrieving existing map\n"); + visual.CreatePropertyMap( outMap ); + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "MergeLabelProperties retrieved %d properties\n", outMap.Count() ); + } -void Button::SetDisabledSelectedImage( Actor image ) -{ - Toolkit::Button handle( GetOwner() ); - mPainter->SetDisabledSelectedImage( handle, image ); -} + outMap.Merge( inMap ); -Actor Button::GetDisabledSelectedImage() const -{ - return mDisabledSelectedImage; -} + // Store if a text string has been supplied. -Actor& Button::GetDisabledSelectedImage() -{ - return mDisabledSelectedImage; -} + mTextStringSetFlag = MapContainsTextString( outMap ); -void Button::SetDisabledBackgroundImage( Image image ) -{ - SetDisabledBackgroundImage( ImageActor::New( image ) ); + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "MergeLabelProperties now has %d properties\n", outMap.Count() ); } -void Button::SetDisabledBackgroundImage( Actor image ) +void Button::SetLabelAlignment( Button::Align labelAlignment) { - Toolkit::Button handle( GetOwner() ); - mPainter->SetDisabledBackgroundImage( handle, image ); + mTextLabelAlignment = labelAlignment; + RelayoutRequest(); } -Actor Button::GetDisabledBackgroundImage() const +Button::Align Button::GetLabelAlignment() { - return mDisabledBackgroundImage; + return mTextLabelAlignment; } -Actor& Button::GetDisabledBackgroundImage() +/** + * Create Visual for given index from a property map or url. + * 1) Check if value passed in is a url and create visual + * 2) Create visual from map if step (1) is false + * 3) Register visual with control with false for enable flag. Button will later enable visual when needed ( Button::SelectRequiredVisual ) + * 4) Unregister visual if empty map was provided. This is the method to remove a visual + */ +void Button::CreateVisualsForComponent( Property::Index index, const Property::Value& value, const int visualDepth ) { - return mDisabledBackgroundImage; -} + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "CreateVisualsForComponent index(%d)\n", index ); + Toolkit::VisualFactory visualFactory = Toolkit::VisualFactory::Get(); + Toolkit::Visual::Base buttonVisual; -Actor& Button::GetFadeOutButtonImage() -{ - return mFadeOutButtonImage; -} + std::string imageUrl; + if( value.Get( imageUrl ) ) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "CreateVisualsForComponent Using image URL(%d)\n", index ); + if ( !imageUrl.empty() ) + { + DALI_ASSERT_DEBUG( index != Toolkit::Button::Property::LABEL && "Creating a Image Visual instead of Text Visual " ); + buttonVisual = visualFactory.CreateVisual( imageUrl, ImageDimensions() ); + } + } + else + { + // if its not a string then get a Property::Map from the property if possible. + Property::Map *map = value.GetMap(); + if( map && !map->Empty() ) // Empty map results in current visual removal. + { + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "CreateVisualsForComponent Using Map(%d)\n", index ); + buttonVisual = visualFactory.CreateVisual( *map ); + } + } -Actor& Button::GetFadeOutSelectedImage() -{ - return mFadeOutSelectedImage; + if ( buttonVisual ) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "CreateVisualsForComponent RegisterVisual index(%d) enabled(%s)\n", + index, DevelControl::IsVisualEnabled( *this, index )?"true":"false" ); + // enable the visual if needed for current state + const bool enabled = ( ( index == VISUAL_INDEX_FOR_STATE[ mButtonState ][ BACKGROUND ] )|| + ( index == VISUAL_INDEX_FOR_STATE[ mButtonState ][ FOREGROUND ] )|| + ( index == Toolkit::Button::Property::LABEL ) ); + DevelControl::RegisterVisual( *this, index, buttonVisual, enabled, visualDepth ); + } + else + { + DevelControl::UnregisterVisual( *this, index ); + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "CreateVisualsForComponent Visual not created or empty map (clearing visual).(%d)\n", index); + } + RelayoutRequest(); } -Actor& Button::GetFadeOutBackgroundImage() +bool Button::GetPropertyMapForVisual( Property::Index visualIndex, Property::Map& retreivedMap ) const { - return mFadeOutBackgroundImage; + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "GetPropertyMapForVisual visual(%d)\n", visualIndex); + bool success = false; + Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, visualIndex ); + if ( visual ) + { + visual.CreatePropertyMap( retreivedMap ); + success = true; + } + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "GetPropertyMapForVisual %s\n", success?"Success":"Failure"); + return success; } -bool Button::DoAction( BaseObject* object, const std::string& actionName, const PropertyValueContainer& attributes ) +bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes ) { bool ret = false; @@ -394,126 +428,127 @@ bool Button::DoAction( BaseObject* object, const std::string& actionName, const Toolkit::Button button = Toolkit::Button::DownCast( handle ); - DALI_ASSERT_ALWAYS( button ); + DALI_ASSERT_DEBUG( button ); - if( Toolkit::Button::ACTION_BUTTON_CLICK == actionName ) + if( 0 == strcmp( actionName.c_str(), ACTION_BUTTON_CLICK ) ) { - GetImplementation( button ).DoClickAction( attributes ); - ret = true; + ret = GetImplementation( button ).DoClickAction( attributes ); } return ret; } -void Button::DoClickAction( const PropertyValueContainer& attributes ) +bool Button::DoClickAction( const Property::Map& 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(); + ButtonDown(); + if ( !mTogglableButton ) + { + mButtonPressedState = DEPRESSED; + } + ButtonUp(); mClickActionPerforming = false; - } -} -void Button::OnAnimationTimeSet( float animationTime ) -{ - mPainter->SetAnimationTime( animationTime ); -} + return true; + } -float Button::OnAnimationTimeRequested() const -{ - return mPainter->GetAnimationTime(); + return false; } -void Button::OnButtonStageDisconnection() +void Button::ButtonDown() { - if( ButtonDown == mState ) + if( mTogglableButton ) { - if( !mTogglableButton ) + if ( mButtonState != SELECTED_STATE ) { - Toolkit::Button handle( GetOwner() ); - - // Notifies the painter the button has been released. - mPainter->Released( handle ); - - if( mAutoRepeating ) - { - mAutoRepeatingTimer.Reset(); - } + SetSelected( true ); + mButtonPressedState = TOGGLE_DEPRESSED; + } + else + { + mButtonPressedState = DEPRESSED; } } -} - -void Button::OnButtonDown() -{ - if( !mTogglableButton ) + else { - Toolkit::Button handle( GetOwner() ); - - // Notifies the painter the button has been pressed. - mPainter->Pressed( handle ); - + Pressed(); + mButtonPressedState = DEPRESSED; if( mAutoRepeating ) { - SetUpTimer( mInitialAutoRepeatingDelay ); + SetUpTimer( mInitialAutoRepeatingDelay ); } - - //Emit signal. - mPressedSignal.Emit( handle ); } + + // The pressed signal should be emitted regardless of toggle mode. + Toolkit::Button handle( GetOwner() ); + mPressedSignal.Emit( handle ); } -void Button::OnButtonUp() +void Button::ButtonUp() { - if( ButtonDown == mState ) + bool emitSignalsForPressAndReleaseAction = false; + + if( DEPRESSED == mButtonPressedState ) { - if( mTogglableButton ) + if( mTogglableButton ) // Button up will change state { - SetSelected( !mSelected ); + emitSignalsForPressAndReleaseAction = OnToggleReleased(); // Derived toggle buttons can override this to provide custom behaviour } else { - Toolkit::Button handle( GetOwner() ); - - // Notifies the painter the button has been clicked. - mPainter->Released( handle ); - mPainter->Clicked( handle ); - + Released(); // Button up will result in unselected state if( mAutoRepeating ) { mAutoRepeatingTimer.Reset(); } - - //Emit signal. - mReleasedSignal.Emit( handle ); - mClickedSignal.Emit( handle ); + emitSignalsForPressAndReleaseAction = true; } } + else if ( TOGGLE_DEPRESSED == mButtonPressedState ) + { + emitSignalsForPressAndReleaseAction = true; // toggle released after being pressed, a click + } + + if ( emitSignalsForPressAndReleaseAction ) + { + // The clicked and released signals should be emitted regardless of toggle mode. + Toolkit::Button handle( GetOwner() ); + mReleasedSignal.Emit( handle ); + mClickedSignal.Emit( handle ); + } +} + +bool Button::OnToggleReleased() +{ + SetSelected( !IsSelected() ); + mButtonPressedState = UNPRESSED; + return true; } + void Button::OnTouchPointLeave() { - if( ButtonDown == mState ) + if( DEPRESSED == mButtonPressedState ) { if( !mTogglableButton ) { - Toolkit::Button handle( GetOwner() ); - - // Notifies the painter the button has been released. - mPainter->Released( handle ); + Released(); if( mAutoRepeating ) { mAutoRepeatingTimer.Reset(); } - - //Emit signal. - mReleasedSignal.Emit( handle ); } + + mButtonPressedState = UNPRESSED; + + // The released signal should be emitted regardless of toggle mode. + Toolkit::Button handle( GetOwner() ); + mReleasedSignal.Emit( handle ); } } @@ -547,21 +582,21 @@ bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tr Dali::BaseHandle handle( object ); bool connected( true ); - Toolkit::Button button = Toolkit::Button::DownCast(handle); + Toolkit::Button button = Toolkit::Button::DownCast( handle ); - if( Toolkit::Button::SIGNAL_PRESSED == signalName ) + if( 0 == strcmp( signalName.c_str(), SIGNAL_PRESSED ) ) { button.PressedSignal().Connect( tracker, functor ); } - else if( Toolkit::Button::SIGNAL_RELEASED == signalName ) + else if( 0 == strcmp( signalName.c_str(), SIGNAL_RELEASED ) ) { button.ReleasedSignal().Connect( tracker, functor ); } - else if( Dali::Toolkit::Button::SIGNAL_CLICKED == signalName ) + else if( 0 == strcmp( signalName.c_str(), SIGNAL_CLICKED ) ) { button.ClickedSignal().Connect( tracker, functor ); } - else if( Dali::Toolkit::Button::SIGNAL_STATE_CHANGED == signalName ) + else if( 0 == strcmp( signalName.c_str(), SIGNAL_STATE_CHANGED ) ) { button.StateChangedSignal().Connect( tracker, functor ); } @@ -574,109 +609,389 @@ bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tr return connected; } -bool Button::OnTouchEvent(const TouchEvent& event) +void Button::OnInitialize() +{ + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::OnInitialize\n" ); + + Actor self = Self(); + + mTapDetector = TapGestureDetector::New(); + mTapDetector.Attach( self ); + mTapDetector.DetectedSignal().Connect(this, &Button::OnTap); + + self.SetKeyboardFocusable( true ); + + self.TouchSignal().Connect( this, &Button::OnTouch ); +} + +bool Button::OnAccessibilityActivated() { - // Only events are processed when the button is not disabled and the touch event has only - // one touch point. - if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) ) + return OnKeyboardEnter(); +} + +bool Button::OnTouch( Actor actor, const TouchData& touch ) +{ + + // Only events are processed when the button is not disabled + auto result( false ); + + if( !IsDisabled() ) { - switch( event.GetPoint(0).state ) + if ( 1 == touch.GetPointCount() ) { - case TouchPoint::Down: + switch( touch.GetState( 0 ) ) { - OnButtonDown(); // Notification for derived classes. - - // Sets the button state to ButtonDown. - mState = ButtonDown; - break; + case PointState::DOWN: + { + ButtonDown(); + break; + } + case PointState::UP: + { + ButtonUp(); + break; + } + case PointState::INTERRUPTED: + { + OnTouchPointInterrupted(); + break; + } + case PointState::LEAVE: + { + OnTouchPointLeave(); + break; + } + case PointState::MOTION: + case PointState::STATIONARY: // FALLTHROUGH + { + // Nothing to do + break; + } } - case TouchPoint::Up: - { - OnButtonUp(); // Notification for derived classes. + } + else if( 1 < touch.GetPointCount() ) + { + OnTouchPointLeave(); // Notification for derived classes. - // Sets the button state to ButtonUp. - mState = ButtonUp; - break; - } - case TouchPoint::Interrupted: - { - OnTouchPointInterrupted(); // Notification for derived classes. + // Sets the button state to the default + mButtonPressedState = UNPRESSED; + } + result = true; + } + return result; +} - // Sets the button state to the default (ButtonUp). - mState = ButtonUp; - break; - } - case TouchPoint::Leave: +bool Button::OnKeyboardEnter() +{ + // When the enter key is pressed, or button is activated, the click action is performed. + Property::Map attributes; + bool ret = DoClickAction( attributes ); + + return ret; +} + +void Button::OnStageDisconnection() +{ + if( DEPRESSED == mButtonPressedState ) + { + if( !mTogglableButton ) + { + Released(); + + if( mAutoRepeating ) { - OnTouchPointLeave(); // Notification for derived classes. + mAutoRepeatingTimer.Reset(); + } + } + } - // Sets the button state to the default (ButtonUp). - mState = ButtonUp; - break; + mButtonPressedState = UNPRESSED; + + Control::OnStageDisconnection(); // Visuals will be set off stage +} + +void Button::OnStageConnection( int depth ) +{ + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::OnStageConnection ptr(%p) \n", this ); + OnButtonVisualRemoval( VISUAL_INDEX_FOR_STATE[ mPreviousButtonState ][ BACKGROUND ] ); + OnButtonVisualRemoval( VISUAL_INDEX_FOR_STATE[ mPreviousButtonState ][ FOREGROUND ] ); + SelectRequiredVisual( Toolkit::Button::Property::LABEL ); + SelectRequiredVisual( VISUAL_INDEX_FOR_STATE[ mButtonState ][ BACKGROUND ] ); + SelectRequiredVisual( VISUAL_INDEX_FOR_STATE[ mButtonState ][ FOREGROUND ] ); + Control::OnStageConnection( depth ); // Enabled visuals will be put on stage + RelayoutRequest(); +} + +Vector3 Button::GetNaturalSize() +{ + Vector3 size = Vector3::ZERO; + + bool horizontalAlignment = mTextLabelAlignment == BEGIN || mTextLabelAlignment == END; // label and visual side by side + + // Get natural size of foreground ( largest of the possible visuals ) + Size largestProvidedVisual; + Size labelSize = Size::ZERO; + + bool foreGroundVisualUsed = false; + + for ( int state = Button::UNSELECTED_STATE; state < Button::STATE_COUNT; state++ ) + { + Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, VISUAL_INDEX_FOR_STATE[state][FOREGROUND] ); + Size visualSize; + if ( visual ) + { + visual.GetNaturalSize( visualSize ); + largestProvidedVisual.width = std::max(largestProvidedVisual.width, visualSize.width ); + largestProvidedVisual.height = std::max(largestProvidedVisual.height, visualSize.height ); + foreGroundVisualUsed = true; + } + } + + if ( !foreGroundVisualUsed ) // If foreground visual not supplied then use the background visual to calculate Natural size + { + for ( int state = Button::UNSELECTED_STATE; state < Button::STATE_COUNT; state++ ) + { + Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, VISUAL_INDEX_FOR_STATE[state][BACKGROUND] ); + Size visualSize; + if ( visual ) + { + visual.GetNaturalSize( visualSize ); + largestProvidedVisual.width = std::max(largestProvidedVisual.width, visualSize.width ); + largestProvidedVisual.height = std::max(largestProvidedVisual.height, visualSize.height ); } - case TouchPoint::Motion: - case TouchPoint::Stationary: // FALLTHROUGH + } + } + + // Get horizontal padding total + if ( largestProvidedVisual.width > 0 ) // if visual exists + { + size.width += largestProvidedVisual.width + mForegroundPadding.left + mForegroundPadding.right; + } + // Get vertical padding total + if ( largestProvidedVisual.height > 0 ) + { + size.height += largestProvidedVisual.height + mForegroundPadding.top + mForegroundPadding.bottom; + } + + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "GetNaturalSize visual Size(%f,%f)\n", + largestProvidedVisual.width, largestProvidedVisual.height ); + + // Get natural size of label if text has been set + if ( mTextStringSetFlag ) + { + Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, Toolkit::Button::Property::LABEL ); + + if ( visual ) + { + visual.GetNaturalSize( labelSize ); + + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "GetNaturalSize labelSize(%f,%f) padding(%f,%f)\n", + labelSize.width, labelSize.height, mLabelPadding.left + mLabelPadding.right, mLabelPadding.top + mLabelPadding.bottom); + + labelSize.width += mLabelPadding.left + mLabelPadding.right; + labelSize.height += mLabelPadding.top + mLabelPadding.bottom; + + // Add label size to height or width depending on alignment position + if ( horizontalAlignment ) { - // Nothing to do - break; + size.width += labelSize.width; + size.height = std::max(size.height, labelSize.height ); } - default: + else { - DALI_ASSERT_ALWAYS( !"Point status unhandled." ); - break; + size.height += labelSize.height; + size.width = std::max(size.width, labelSize.width ); } } } - else if( 1 < event.GetPointCount() ) - { - OnTouchPointLeave(); // Notification for derived classes. - // Sets the button state to the default (ButtonUp). - mState = ButtonUp; + if( size.width < 1 && size.height < 1 ) + { + // if no image or label then use Control's natural size + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "GetNaturalSize Using control natural size\n"); + size = Control::GetNaturalSize(); } - return false; + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "Button GetNaturalSize (%f,%f)\n", size.width, size.height ); + + return size; } -void Button::OnInitialize() +void Button::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) { - // Initialize the painter and notifies subclasses. - Toolkit::Button handle( GetOwner() ); - if( mPainter ) + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnSetResizePolicy\n"); + RelayoutRequest(); +} + +/** + * Visuals are sized and positioned in this function. + * Whilst the control has it's size negotiated it has to size it's visuals explicitly here. + */ + +void Button::OnRelayout( const Vector2& size, RelayoutContainer& container ) +{ + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout targetSize(%f,%f) ptr(%p) state[%d]\n", size.width, size.height, this, mButtonState ); + + Toolkit::Visual::Base currentVisual = DevelControl::GetVisual( *this, VISUAL_INDEX_FOR_STATE[mButtonState][FOREGROUND] ); + Toolkit::Visual::Base currentBackGroundVisual = DevelControl::GetVisual( *this, VISUAL_INDEX_FOR_STATE[mButtonState][BACKGROUND] ); + + // Sizes and padding set to zero, if not present then values will no effect calculations. + Vector2 visualPosition = Vector2::ZERO; + Vector2 labelPosition = Vector2::ZERO; + Size visualSize = Size::ZERO; + Padding foregroundVisualPadding = Padding(0.0f, 0.0f, 0.0f, 0.0f ); + Padding labelVisualPadding = Padding(0.0f, 0.0f, 0.0f, 0.0f ); + + if ( mTextStringSetFlag ) { - mPainter->Initialize( handle ); + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout Label padding setting padding:%f,%f,%f,%f\n", mLabelPadding.y, mLabelPadding.x, mLabelPadding.width,mLabelPadding.height ); + labelVisualPadding = mLabelPadding; } - Actor self = Self(); + if ( currentVisual ) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout Foreground Visual setting padding:%f,%f,%f,%f\n", mForegroundPadding.y, mForegroundPadding.x, mForegroundPadding.width,mForegroundPadding.height ); + currentVisual.GetNaturalSize( visualSize ); + foregroundVisualPadding = mForegroundPadding; + } - mTapDetector = TapGestureDetector::New(); - mTapDetector.Attach( self ); - mTapDetector.DetectedSignal().Connect(this, &Button::OnTap); + Toolkit::Align::Type visualAnchorPoint = Toolkit::Align::TOP_BEGIN; - OnButtonInitialize(); + Vector2 visualAndPaddingSize = Vector2( ( foregroundVisualPadding.x + visualSize.width + foregroundVisualPadding.y ), + ( foregroundVisualPadding.width + visualSize.height + foregroundVisualPadding.height )); - self.SetKeyboardFocusable( true ); -} + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout visualAndPaddingSize(%f,%f)\n", visualAndPaddingSize.width, visualAndPaddingSize.height); -void Button::OnActivated() -{ - // When the button is activated, it performs the click action - PropertyValueContainer attributes; - DoClickAction( attributes ); -} + // Text Visual should take all space available after foreground visual size and all padding is considered. + // Remaining Space priority, Foreground padding, foreground visual, Text padding then Text visual. + Size remainingSpaceForText = Size::ZERO; -void Button::OnControlSizeSet(const Vector3& targetSize) -{ - Toolkit::Button handle( GetOwner() ); - if( mPainter ) + switch ( mTextLabelAlignment ) + { + case BEGIN : + { + visualAnchorPoint = Toolkit::Align::TOP_END; + visualPosition.x = foregroundVisualPadding.right; + visualPosition.y = foregroundVisualPadding.top; + + labelPosition.x = labelVisualPadding.x; + labelPosition.y = labelVisualPadding.top; + + remainingSpaceForText.width = size.width - visualAndPaddingSize.width - labelVisualPadding.x - labelVisualPadding.y; + remainingSpaceForText.height = size.height - labelVisualPadding.top - labelVisualPadding.bottom; + break; + } + case END : + { + visualAnchorPoint = Toolkit::Align::TOP_BEGIN; + visualPosition.x = foregroundVisualPadding.left; + visualPosition.y = foregroundVisualPadding.top; + + labelPosition.x = visualAndPaddingSize.width + labelVisualPadding.x; + labelPosition.y = labelVisualPadding.top; + + remainingSpaceForText.width = size.width - visualAndPaddingSize.width - labelVisualPadding.x - labelVisualPadding.y; + remainingSpaceForText.height = size.height - labelVisualPadding.top - labelVisualPadding.bottom; + break; + } + case TOP : + { + visualAnchorPoint = Toolkit::Align::BOTTOM_END; + visualPosition.x = foregroundVisualPadding.left; + visualPosition.y = foregroundVisualPadding.bottom; + + labelPosition.x = labelVisualPadding.left; + labelPosition.y = labelVisualPadding.top; + + remainingSpaceForText.width = size.width - labelVisualPadding.x - labelVisualPadding.y; + remainingSpaceForText.height = size.height - visualAndPaddingSize.height - labelVisualPadding.top - labelVisualPadding.bottom; + + break; + } + case BOTTOM : + { + visualAnchorPoint = Toolkit::Align::TOP_END; + visualPosition.x = foregroundVisualPadding.left; + visualPosition.y = foregroundVisualPadding.top; + + labelPosition.x = labelVisualPadding.left; + labelPosition.y = visualAndPaddingSize.height + labelVisualPadding.top; + + remainingSpaceForText.width = size.width - labelVisualPadding.x - labelVisualPadding.y; + remainingSpaceForText.height = size.height - visualAndPaddingSize.height - labelVisualPadding.top - labelVisualPadding.bottom; + + break; + } + } + + if ( currentBackGroundVisual ) { - mPainter->SetSize( handle, targetSize ); + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout Setting visual background size to(%f,%f)\n", size.width, size.height); + + Property::Map visualTransform; + + visualTransform.Add( Toolkit::Visual::Transform::Property::SIZE, size ) + .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) ); + + currentBackGroundVisual.SetTransformAndSize( visualTransform, size ); + } + + if ( currentVisual ) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout Setting visual size to(%f,%f)\n", visualSize.width, visualSize.height); + + Property::Map visualTransform; + + visualTransform.Add( Toolkit::Visual::Transform::Property::SIZE, visualSize ) + .Add( Toolkit::Visual::Transform::Property::OFFSET, visualPosition ) + .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) ) + .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) ) + .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN ) + .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, visualAnchorPoint ); + + currentVisual.SetTransformAndSize( visualTransform, size ); } + + if ( mTextStringSetFlag ) + { + Toolkit::Visual::Base textVisual = DevelControl::GetVisual( *this, Toolkit::Button::Property::LABEL ); // No need to search for Label visual if no text set. + + if ( textVisual ) + { + if ( !currentVisual ) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout Only Text\n"); + labelPosition.x = labelVisualPadding.left; + labelPosition.y = labelVisualPadding.height; + } + + Vector2 preSize = Vector2( static_cast< int >( remainingSpaceForText.x ), static_cast< int >( remainingSpaceForText.y )); + + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout text Size(%f,%f) text Position(%f,%f) \n", remainingSpaceForText.width, remainingSpaceForText.height, labelPosition.x, labelPosition.y); + + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout text Size -- (%f,%f) text Position(%f,%f) \n", preSize.width, preSize.height, labelPosition.x, labelPosition.y); + + + Property::Map textVisualTransform; + textVisualTransform.Add( Toolkit::Visual::Transform::Property::SIZE, preSize ) + .Add( Toolkit::Visual::Transform::Property::OFFSET, labelPosition ) + .Add( Toolkit::Visual::Transform::Property::OFFSET_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) ) + .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) ) + .Add( Toolkit::Visual::Transform::Property::ORIGIN, Toolkit::Align::TOP_BEGIN ) + .Add( Toolkit::Visual::Transform::Property::ANCHOR_POINT, visualAnchorPoint ); + + textVisual.SetTransformAndSize( textVisualTransform, size ); + } + } + + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout selected (%s) \n", IsSelected()?"yes":"no" ); + + DALI_LOG_INFO( gLogButtonFilter, Debug::General, "OnRelayout << \n"); } void Button::OnTap(Actor actor, const TapGesture& tap) { - // Do nothing. + // Prevents Parent getting a tap event } void Button::SetUpTimer( float delay ) @@ -689,106 +1004,196 @@ void Button::SetUpTimer( float delay ) bool Button::AutoRepeatingSlot() { bool consumed = false; - if( !mDisabled ) + if( !IsDisabled() ) { // Restart the autorepeat timer. SetUpTimer( mNextAutoRepeatingDelay ); - Toolkit::Button handle( GetOwner() ); + Pressed(); - // Notifies the painter the button has been pressed. - mPainter->Pressed( handle ); + Toolkit::Button handle( GetOwner() ); //Emit signal. consumed = mReleasedSignal.Emit( handle ); - consumed |= mClickedSignal.Emit( handle ); + consumed = mClickedSignal.Emit( handle ); consumed |= mPressedSignal.Emit( handle ); } return consumed; } -void Button::OnControlStageDisconnection() +void Button::Pressed() +{ + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::Pressed\n" ); + + if( mButtonState == UNSELECTED_STATE ) + { + ChangeState( SELECTED_STATE ); + OnPressed(); // Notifies the derived class the button has been pressed. + } +} + +void Button::Released() { - OnButtonStageDisconnection(); // Notification for derived classes. - mState = ButtonUp; + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::Released\n" ); + + if( mButtonState == SELECTED_STATE && !mTogglableButton ) + { + ChangeState( UNSELECTED_STATE ); + OnReleased(); // // Notifies the derived class the button has been released. + } + mButtonPressedState = UNPRESSED; } -void Button::SetPainter(ButtonPainterPtr painter) +void Button::SelectRequiredVisual( Property::Index visualIndex ) { - mPainter = painter; + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::SelectRequiredVisual index(%d) state(%d)\n", visualIndex, mButtonState ); + // only enable visuals that exist + if( DevelControl::GetVisual( *this, visualIndex ) ) + { + DevelControl::EnableVisual( *this, visualIndex, true ); + } } -Button::ButtonState Button::GetState() +void Button::RemoveVisual( Property::Index visualIndex ) { - return mState; + // Use OnButtonVisualRemoval if want button developer to have the option to override removal. + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::RemoveVisual index(%d) state(%d)\n", visualIndex, mButtonState ); + + Toolkit::Visual::Base visual = DevelControl::GetVisual( *this, visualIndex ); + + if( visual ) + { + DevelControl::EnableVisual( *this, visualIndex, false ); + } +} + +void Button::OnButtonVisualRemoval( Property::Index visualIndex ) +{ + // Derived Buttons can over ride this to prevent default removal. + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::OnButtonVisualRemoval index(%d)\n", visualIndex ); + RemoveVisual( visualIndex ); } void Button::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value ) { Toolkit::Button button = Toolkit::Button::DownCast( Dali::BaseHandle( object ) ); + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::SetProperty index[%d]\n", index ); + if ( button ) { switch ( index ) { - case Toolkit::Button::PROPERTY_DISABLED: + case Toolkit::Button::Property::DISABLED: { - GetImplementation( button ).SetDisabled( value.Get() ); + GetImplementation( button ).SetDisabled( value.Get< bool >() ); break; } - case Toolkit::Button::PROPERTY_AUTO_REPEATING: + case Toolkit::Button::Property::AUTO_REPEATING: { GetImplementation( button ).SetAutoRepeating( value.Get< bool >() ); break; } - case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY: + case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY: { GetImplementation( button ).SetInitialAutoRepeatingDelay( value.Get< float >() ); break; } - case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY: + case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY: { GetImplementation( button ).SetNextAutoRepeatingDelay( value.Get< float >() ); break; } - case Toolkit::Button::PROPERTY_TOGGLABLE: + case Toolkit::Button::Property::TOGGLABLE: { GetImplementation( button ).SetTogglableButton( value.Get< bool >() ); break; } - case Toolkit::Button::PROPERTY_SELECTED: + case Toolkit::Button::Property::SELECTED: { GetImplementation( button ).SetSelected( value.Get< bool >() ); break; } - case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR: + case Toolkit::Button::Property::UNSELECTED_VISUAL: + case Toolkit::Button::Property::SELECTED_VISUAL: + case Toolkit::Button::Property::DISABLED_SELECTED_VISUAL: + case Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL: { - GetImplementation( button ).SetButtonImage( Scripting::NewActor( value.Get< Property::Map >() ) ); + GetImplementation( button ).CreateVisualsForComponent( index, value, DepthIndex::CONTENT ); break; } - case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR: + case Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL: + case Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL: + case Toolkit::Button::Property::DISABLED_SELECTED_BACKGROUND_VISUAL: + case Toolkit::Button::Property::DISABLED_UNSELECTED_BACKGROUND_VISUAL: { - GetImplementation( button ).SetSelectedImage( Scripting::NewActor( value.Get< Property::Map >() ) ); + GetImplementation( button ).CreateVisualsForComponent( index , value, DepthIndex::BACKGROUND); break; } - case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR: + case Toolkit::Button::Property::LABEL: { - GetImplementation( button ).SetDisabledImage( Scripting::NewActor( value.Get< Property::Map >() ) ); + Property::Map outTextVisualProperties; + std::string textString; + + if ( value.Get( textString ) ) + { + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::SetProperty Setting TextVisual with string[%s]\n", textString.c_str() ); + + Property::Map setPropertyMap; + setPropertyMap.Add( Toolkit::Visual::Property::TYPE, Toolkit::Visual::TEXT ) + .Add( Toolkit::TextVisual::Property::TEXT, textString ); + + GetImplementation( button ).MergeWithExistingLabelProperties( setPropertyMap, outTextVisualProperties ); + } + else + { + // Get a Property::Map from the property if possible. + Property::Map* setPropertyMap = value.GetMap(); + if( setPropertyMap ) + { + TextVisual::ConvertStringKeysToIndexKeys( *setPropertyMap ); + GetImplementation( button ).MergeWithExistingLabelProperties( *setPropertyMap, outTextVisualProperties ); + } + } + + if( !outTextVisualProperties.Empty() ) + { + GetImplementation( button ).CreateVisualsForComponent( index, outTextVisualProperties, DepthIndex::CONTENT ); + } break; } - case Toolkit::Button::PROPERTY_LABEL_ACTOR: + case Toolkit::DevelButton::Property::LABEL_RELATIVE_ALIGNMENT: { - GetImplementation( button ).SetLabel( Scripting::NewActor( value.Get< Property::Map >() ) ); + Button::Align labelAlignment(END); + Scripting::GetEnumeration< Button::Align> ( value.Get< std::string >().c_str(), + ALIGNMENT_TABLE, ALIGNMENT_TABLE_COUNT, + labelAlignment ); + + GetImplementation( button ).SetLabelAlignment( labelAlignment ); + break; + } + + case Toolkit::DevelButton::Property::LABEL_PADDING: + { + Vector4 padding ( value.Get< Vector4 >() ); + GetImplementation( button ).SetLabelPadding( Padding( padding.x, padding.y, padding.z, padding.w ) ); + break; + } + + case Toolkit::DevelButton::Property::VISUAL_PADDING: + { + Vector4 padding ( value.Get< Vector4 >() ); + GetImplementation( button ).SetForegroundPadding( Padding( padding.x, padding.y, padding.z, padding.w ) ); break; } } @@ -805,72 +1210,84 @@ Property::Value Button::GetProperty( BaseObject* object, Property::Index propert { switch ( propertyIndex ) { - case Toolkit::Button::PROPERTY_DISABLED: + case Toolkit::Button::Property::DISABLED: { - value = GetImplementation( button ).mDisabled; + value = GetImplementation( button ).IsDisabled(); break; } - case Toolkit::Button::PROPERTY_AUTO_REPEATING: + case Toolkit::Button::Property::AUTO_REPEATING: { value = GetImplementation( button ).mAutoRepeating; break; } - case Toolkit::Button::PROPERTY_INITIAL_AUTO_REPEATING_DELAY: + case Toolkit::Button::Property::INITIAL_AUTO_REPEATING_DELAY: { value = GetImplementation( button ).mInitialAutoRepeatingDelay; break; } - case Toolkit::Button::PROPERTY_NEXT_AUTO_REPEATING_DELAY: + case Toolkit::Button::Property::NEXT_AUTO_REPEATING_DELAY: { value = GetImplementation( button ).mNextAutoRepeatingDelay; break; } - case Toolkit::Button::PROPERTY_TOGGLABLE: + case Toolkit::Button::Property::TOGGLABLE: { value = GetImplementation( button ).mTogglableButton; break; } - case Toolkit::Button::PROPERTY_SELECTED: + case Toolkit::Button::Property::SELECTED: { - value = GetImplementation( button ).mSelected; + value = GetImplementation( button ).IsSelected(); break; } - case Toolkit::Button::PROPERTY_NORMAL_STATE_ACTOR: + case Toolkit::Button::Property::UNSELECTED_VISUAL: + case Toolkit::Button::Property::SELECTED_VISUAL: + case Toolkit::Button::Property::DISABLED_SELECTED_VISUAL: + case Toolkit::Button::Property::DISABLED_UNSELECTED_VISUAL: + case Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL: + case Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL: + case Toolkit::Button::Property::DISABLED_SELECTED_BACKGROUND_VISUAL: + case Toolkit::Button::Property::DISABLED_UNSELECTED_BACKGROUND_VISUAL: + case Toolkit::Button::Property::LABEL: { - Property::Map map; - Scripting::CreatePropertyMap( GetImplementation( button ).mButtonImage, map ); - value = map; + Property::Map visualProperty; + if ( GetImplementation( button ).GetPropertyMapForVisual( propertyIndex, visualProperty ) ) + { + value = visualProperty; + } break; } - case Toolkit::Button::PROPERTY_SELECTED_STATE_ACTOR: + case Toolkit::DevelButton::Property::LABEL_RELATIVE_ALIGNMENT: { - Property::Map map; - Scripting::CreatePropertyMap( GetImplementation( button ).mSelectedImage, map ); - value = map; + const char* alignment = Scripting::GetEnumerationName< Button::Align >( GetImplementation( button ).GetLabelAlignment(), + ALIGNMENT_STRING_TABLE, + ALIGNMENT_STRING_TABLE_COUNT ); + if( alignment ) + { + value = std::string( alignment ); + } + break; } - case Toolkit::Button::PROPERTY_DISABLED_STATE_ACTOR: + case Toolkit::DevelButton::Property::LABEL_PADDING: { - Property::Map map; - Scripting::CreatePropertyMap( GetImplementation( button ).mDisabledImage, map ); - value = map; + Padding padding = GetImplementation( button ).GetLabelPadding(); + value = Vector4( padding.x, padding.y, padding.top, padding.bottom); break; } - case Toolkit::Button::PROPERTY_LABEL_ACTOR: + case Toolkit::DevelButton::Property::VISUAL_PADDING: { - Property::Map map; - Scripting::CreatePropertyMap( GetImplementation( button ).mLabel, map ); - value = map; - break; + Padding padding = GetImplementation( button ).GetForegroundPadding(); + value = Vector4( padding.x, padding.y, padding.top, padding.bottom); } } } @@ -878,6 +1295,30 @@ Property::Value Button::GetProperty( BaseObject* object, Property::Index propert return value; } +void Button::SetLabelPadding( const Padding& padding) +{ + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::SetLabelPadding padding(%f,%f,%f,%f)\n", padding.left, padding.right, padding.bottom, padding.top ); + mLabelPadding = Padding( padding.left, padding.right, padding.bottom, padding.top ); + RelayoutRequest(); +} + +Padding Button::GetLabelPadding() +{ + return mLabelPadding; +} + +void Button::SetForegroundPadding( const Padding& padding) +{ + DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "Button::SetForegroundPadding padding(%f,%f,%f,%f)\n", padding.left, padding.right, padding.bottom, padding.top ); + mForegroundPadding = Padding( padding.left, padding.right, padding.bottom, padding.top ); + RelayoutRequest(); +} + +Padding Button::GetForegroundPadding() +{ + return mForegroundPadding; +} + } // namespace Internal } // namespace Toolkit