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=fcd046825510e55ad8e011774ca47d97360dc7fc;hp=ff395aee9a715c80f08e2db64e18ba22aa8b4132;hb=958bd01cb1bc4cf43cbe740a7b041927d9d34ed6;hpb=28d0de15155c9923010da5c53eb1a96cbf6cd6e4 diff --git a/dali-toolkit/internal/controls/buttons/button-impl.cpp b/dali-toolkit/internal/controls/buttons/button-impl.cpp index ff395ae..fcd0468 100644 --- a/dali-toolkit/internal/controls/buttons/button-impl.cpp +++ b/dali-toolkit/internal/controls/buttons/button-impl.cpp @@ -20,16 +20,16 @@ // EXTERNAL INCLUDES #include // for strcmp -#include +#include #include #include -#include -#include +#include #include // INTERNAL INCLUDES -#include #include +#include +#include /** * Button states and contents @@ -77,36 +77,39 @@ BaseHandle Create() DALI_TYPE_REGISTRATION_BEGIN( Toolkit::Button, Toolkit::Control, Create ); DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled", BOOLEAN, DISABLED ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "auto-repeating", BOOLEAN, AUTO_REPEATING ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "initial-auto-repeating-delay", FLOAT, INITIAL_AUTO_REPEATING_DELAY ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "next-auto-repeating-delay", FLOAT, NEXT_AUTO_REPEATING_DELAY ) +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, "unselected-state-image", STRING, UNSELECTED_STATE_IMAGE ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected-state-image", STRING, SELECTED_STATE_IMAGE ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabled-state-image", STRING, DISABLED_STATE_IMAGE ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselected-color", VECTOR4, UNSELECTED_COLOR ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selected-color", VECTOR4, SELECTED_COLOR ) -DALI_PROPERTY_REGISTRATION( Toolkit, Button, "label-text", STRING, LABEL_TEXT ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselectedStateImage", STRING, UNSELECTED_STATE_IMAGE ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selectedStateImage", STRING, SELECTED_STATE_IMAGE ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "disabledStateImage", STRING, DISABLED_STATE_IMAGE ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "unselectedColor", VECTOR4, UNSELECTED_COLOR ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "selectedColor", VECTOR4, SELECTED_COLOR ) +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "label", MAP, LABEL ) +// Deprecated properties: +DALI_PROPERTY_REGISTRATION( Toolkit, Button, "labelText", STRING, LABEL_TEXT ) + +// 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, "state-changed", SIGNAL_STATE_CHANGED ) +DALI_SIGNAL_REGISTRATION( Toolkit, Button, "stateChanged", SIGNAL_STATE_CHANGED ) -DALI_ACTION_REGISTRATION( Toolkit, Button, "button-click", ACTION_BUTTON_CLICK ) +// Actions: +DALI_ACTION_REGISTRATION( Toolkit, Button, "buttonClick", ACTION_BUTTON_CLICK ) DALI_TYPE_REGISTRATION_END() const unsigned int INITIAL_AUTOREPEATING_DELAY( 0.15f ); const unsigned int NEXT_AUTOREPEATING_DELAY( 0.05f ); -const char* const STYLE_BUTTON_LABEL = "button.label"; - } // unnamed namespace Button::Button() -: Control( ControlBehaviour( REQUIRES_TOUCH_EVENTS | REQUIRES_STYLE_CHANGE_SIGNALS ) ), +: Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ), mAutoRepeatingTimer(), mUnselectedColor( Color::WHITE ), // The natural colors of the specified images will be used by default. mSelectedColor( Color::WHITE ), @@ -428,27 +431,9 @@ float Button::GetAnimationTime() const void Button::SetLabelText( const std::string& label ) { - if( !mLabel || ( label != GetLabelText() ) ) - { - // If we have a label, unparent and update it. - if( mLabel ) - { - mLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, label ); - } - else - { - // If we don't have a label, create one and set it up. - mLabel = Toolkit::TextLabel::New( label ); - mLabel.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_LABEL ); - mLabel.SetPosition( 0.f, 0.f ); - // label should be the top of the button - Self().Add( mLabel ); - } - - OnLabelSet( false ); - - RelayoutRequest(); - } + Property::Map labelProperty; + labelProperty.Insert( "text", label ); + ModifyLabel( labelProperty ); } std::string Button::GetLabelText() const @@ -461,6 +446,40 @@ std::string Button::GetLabelText() const return std::string(); } +void Button::ModifyLabel( const Property::Map& properties ) +{ + // If we don't have a label yet, create one. + if( !mLabel ) + { + // If we don't have a label, create one and set it up. + // Note: The label text is set from the passed in property map after creation. + mLabel = Toolkit::TextLabel::New(); + mLabel.SetPosition( 0.0f, 0.0f ); + // label should be the top of the button + Self().Add( mLabel ); + } + + // Set any properties specified for the label by iterating through all property key-value pairs. + for( unsigned int i = 0, mapCount = properties.Count(); i < mapCount; ++i ) + { + const StringValuePair& propertyPair( properties.GetPair( i ) ); + + // Convert the property string to a property index. + Property::Index setPropertyIndex = mLabel.GetPropertyIndex( propertyPair.first ); + if( setPropertyIndex != Property::INVALID_INDEX ) + { + // If the conversion worked, we have a valid property index, + // Set the property to the new value. + mLabel.SetProperty( setPropertyIndex, propertyPair.second ); + } + } + + // Notify derived button classes of the change. + OnLabelSet( false ); + + RelayoutRequest(); +} + Actor& Button::GetLabelActor() { return mLabel; @@ -509,42 +528,48 @@ void Button::SetupContent( Actor& actorToModify, Actor newActor ) } } -void Button::SetUnselectedColor( const Vector4& color ) +const Vector4 Button::GetUnselectedColor() const { - mUnselectedColor = color; + return mUnselectedColor; +} - if( mUnselectedContent && !GetUnselectedImageFilename().empty() ) +void Button::SetColor( const Vector4& color, Button::PaintState selectedState ) +{ + Actor& contentActor = mSelectedContent; + bool imageFileExists = false; + + if ( selectedState == SelectedState || selectedState == DisabledSelectedState ) { - // If there is existing unselected content, change the color on it directly. - mUnselectedContent.SetColor( mUnselectedColor ); + mSelectedColor = color; + imageFileExists = !GetSelectedImageFilename().empty(); } else { - // If there is no existing content, create a new actor to use for flat color. - SetupContent( mUnselectedContent, CreateSolidColorActor( mUnselectedColor ) ); - mUnselectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); + mUnselectedColor = color; + contentActor = mUnselectedContent; + imageFileExists = !GetUnselectedImageFilename().empty(); } -} -const Vector4 Button::GetUnselectedColor() const -{ - return mUnselectedColor; -} - -void Button::SetSelectedColor( const Vector4& color ) -{ - mSelectedColor = color; - - if( mSelectedContent && !GetSelectedImageFilename().empty() ) + if( contentActor && imageFileExists ) { // If there is existing unselected content, change the color on it directly. - mSelectedContent.SetColor( mSelectedColor ); + contentActor.SetColor( color ); } else { // If there is no existing content, create a new actor to use for flat color. - SetupContent( mSelectedContent, CreateSolidColorActor( mSelectedColor ) ); - mSelectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); + Actor placementActor = Actor::New(); + Toolkit::RendererFactory rendererFactory = Toolkit::RendererFactory::Get(); + Toolkit::ControlRenderer colorRenderer; + + Property::Map map; + map["rendererType"] = "color"; + map["mixColor"] = color; + + colorRenderer = rendererFactory.CreateControlRenderer( map ); + colorRenderer.SetOnStage( placementActor ); + SetupContent( contentActor, placementActor ); + contentActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS ); } } @@ -555,18 +580,14 @@ const Vector4 Button::GetSelectedColor() const void Button::SetUnselectedImage( const std::string& filename ) { - ImageActor newContent; + Toolkit::ImageView newContent; if( !filename.empty() ) { - Image resourceimage = Dali::ResourceImage::New( filename ); - if( resourceimage ) - { - newContent = ImageActor::New( resourceimage ); - } + newContent = Toolkit::ImageView::New( filename ); } else { - newContent = ImageActor::New(); + newContent = Toolkit::ImageView::New(); } if( newContent ) @@ -587,18 +608,14 @@ Actor& Button::GetUnselectedImage() void Button::SetSelectedImage( const std::string& filename ) { - ImageActor newContent; + Toolkit::ImageView newContent; if( !filename.empty() ) { - Image resourceimage = Dali::ResourceImage::New( filename ); - if( resourceimage ) - { - newContent = ImageActor::New( resourceimage ); - } + newContent = Toolkit::ImageView::New( filename ); } else { - newContent = ImageActor::New(); + newContent = Toolkit::ImageView::New(); } if( newContent ) @@ -619,14 +636,10 @@ Actor& Button::GetSelectedImage() void Button::SetBackgroundImage( const std::string& filename ) { - Image resourceimage = Dali::ResourceImage::New( filename ); - if( resourceimage ) - { - SetupContent( mBackgroundContent, ImageActor::New( resourceimage ) ); + SetupContent( mBackgroundContent, Toolkit::ImageView::New( filename ) ); - OnBackgroundImageSet(); - RelayoutRequest(); - } + OnBackgroundImageSet(); + RelayoutRequest(); } Actor& Button::GetBackgroundImage() @@ -636,14 +649,10 @@ Actor& Button::GetBackgroundImage() void Button::SetSelectedBackgroundImage( const std::string& filename ) { - Image resourceimage = Dali::ResourceImage::New( filename ); - if( resourceimage ) - { - SetupContent( mSelectedBackgroundContent, ImageActor::New( resourceimage ) ); + SetupContent( mSelectedBackgroundContent, Toolkit::ImageView::New( filename ) ); - OnSelectedBackgroundImageSet(); - RelayoutRequest(); - } + OnSelectedBackgroundImageSet(); + RelayoutRequest(); } Actor& Button::GetSelectedBackgroundImage() @@ -653,14 +662,10 @@ Actor& Button::GetSelectedBackgroundImage() void Button::SetDisabledImage( const std::string& filename ) { - Image resourceimage = Dali::ResourceImage::New( filename ); - if( resourceimage ) - { - SetupContent( mDisabledContent, ImageActor::New( resourceimage ) ); + SetupContent( mDisabledContent, Toolkit::ImageView::New( filename ) ); - OnDisabledImageSet(); - RelayoutRequest(); - } + OnDisabledImageSet(); + RelayoutRequest(); } Actor& Button::GetDisabledImage() @@ -670,14 +675,10 @@ Actor& Button::GetDisabledImage() void Button::SetDisabledSelectedImage( const std::string& filename ) { - Image resourceimage = Dali::ResourceImage::New( filename ); - if( resourceimage ) - { - SetupContent( mDisabledSelectedContent, ImageActor::New( resourceimage ) ); + SetupContent( mDisabledSelectedContent, Toolkit::ImageView::New( filename ) ); - OnDisabledSelectedImageSet(); - RelayoutRequest(); - } + OnDisabledSelectedImageSet(); + RelayoutRequest(); } Actor& Button::GetDisabledSelectedImage() @@ -687,14 +688,10 @@ Actor& Button::GetDisabledSelectedImage() void Button::SetDisabledBackgroundImage( const std::string& filename ) { - Image resourceimage = Dali::ResourceImage::New( filename ); - if( resourceimage ) - { - SetupContent( mDisabledBackgroundContent, ImageActor::New( resourceimage ) ); + SetupContent( mDisabledBackgroundContent, Toolkit::ImageView::New( filename ) ); - OnDisabledBackgroundImageSet(); - RelayoutRequest(); - } + OnDisabledBackgroundImageSet(); + RelayoutRequest(); } Actor& Button::GetDisabledBackgroundImage() @@ -728,32 +725,6 @@ std::string Button::GetSelectedImageFilename() const return std::string(); } -std::string Button::GetBackgroundImageFilename() const -{ - if( mBackgroundContent ) - { - ResourceImage image = ResourceImage::DownCast( mBackgroundContent ); - if( image ) - { - return image.GetUrl(); - } - } - return std::string(); -} - -std::string Button::GetSelectedBackgroundImageFilename() const -{ - if( mSelectedBackgroundContent ) - { - ResourceImage image = ResourceImage::DownCast( mSelectedBackgroundContent ); - if( image ) - { - return image.GetUrl(); - } - } - return std::string(); -} - std::string Button::GetDisabledImageFilename() const { if( mDisabledContent ) @@ -767,32 +738,6 @@ std::string Button::GetDisabledImageFilename() const return std::string(); } -std::string Button::GetDisabledSelectedImageFilename() const -{ - if( mDisabledSelectedContent ) - { - ResourceImage image = ResourceImage::DownCast( mDisabledSelectedContent ); - if( image ) - { - return image.GetUrl(); - } - } - return std::string(); -} - -std::string Button::GetDisabledBackgroundImageFilename() const -{ - if( mDisabledBackgroundContent ) - { - ResourceImage image = ResourceImage::DownCast( mDisabledBackgroundContent ); - if( image ) - { - return image.GetUrl(); - } - } - return std::string(); -} - bool Button::DoAction( BaseObject* object, const std::string& actionName, const Property::Map& attributes ) { bool ret = false; @@ -829,22 +774,6 @@ bool Button::DoClickAction( const Property::Map& attributes ) return false; } -void Button::OnButtonStageDisconnection() -{ - if( ButtonDown == mState ) - { - if( !mTogglableButton ) - { - Released(); - - if( mAutoRepeating ) - { - mAutoRepeatingTimer.Reset(); - } - } - } -} - void Button::OnButtonDown() { if( !mTogglableButton ) @@ -964,15 +893,62 @@ bool Button::DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tr return connected; } -bool Button::OnTouchEvent(const TouchEvent& event) +void Button::OnInitialize() +{ + 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() +{ + return OnKeyboardEnter(); +} + +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( ButtonDown == mState ) + { + if( !mTogglableButton ) + { + Released(); + + if( mAutoRepeating ) + { + mAutoRepeatingTimer.Reset(); + } + } + } + + mState = ButtonUp; + + Control::OnStageDisconnection(); +} + +bool Button::OnTouch( Actor actor, const TouchData& touch ) { // Only events are processed when the button is not disabled and the touch event has only // one touch point. - if( ( !mDisabled ) && ( 1 == event.GetPointCount() ) ) + if( ( !mDisabled ) && ( 1 == touch.GetPointCount() ) ) { - switch( event.GetPoint(0).state ) + switch( touch.GetState( 0 ) ) { - case TouchPoint::Down: + case PointState::DOWN: { OnButtonDown(); // Notification for derived classes. @@ -980,7 +956,7 @@ bool Button::OnTouchEvent(const TouchEvent& event) mState = ButtonDown; break; } - case TouchPoint::Up: + case PointState::UP: { OnButtonUp(); // Notification for derived classes. @@ -988,7 +964,7 @@ bool Button::OnTouchEvent(const TouchEvent& event) mState = ButtonUp; break; } - case TouchPoint::Interrupted: + case PointState::INTERRUPTED: { OnTouchPointInterrupted(); // Notification for derived classes. @@ -996,7 +972,7 @@ bool Button::OnTouchEvent(const TouchEvent& event) mState = ButtonUp; break; } - case TouchPoint::Leave: + case PointState::LEAVE: { OnTouchPointLeave(); // Notification for derived classes. @@ -1004,20 +980,15 @@ bool Button::OnTouchEvent(const TouchEvent& event) mState = ButtonUp; break; } - case TouchPoint::Motion: - case TouchPoint::Stationary: // FALLTHROUGH + case PointState::MOTION: + case PointState::STATIONARY: // FALLTHROUGH { // Nothing to do break; } - default: - { - DALI_ASSERT_ALWAYS( !"Point status unhandled." ); - break; - } } } - else if( 1 < event.GetPointCount() ) + else if( 1 < touch.GetPointCount() ) { OnTouchPointLeave(); // Notification for derived classes. @@ -1028,39 +999,6 @@ bool Button::OnTouchEvent(const TouchEvent& event) return false; } -void Button::OnInitialize() -{ - Actor self = Self(); - - mTapDetector = TapGestureDetector::New(); - mTapDetector.Attach( self ); - mTapDetector.DetectedSignal().Connect(this, &Button::OnTap); - - OnButtonInitialize(); - - self.SetKeyboardFocusable( true ); -} - -bool Button::OnAccessibilityActivated() -{ - return OnKeyboardEnter(); -} - -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::OnControlStageDisconnection() -{ - OnButtonStageDisconnection(); // Notification for derived classes. - mState = ButtonUp; -} - void Button::OnTap(Actor actor, const TapGesture& tap) { // Do nothing. @@ -1179,7 +1117,6 @@ void Button::PrepareAddButtonImage( Actor& actor ) { if( actor ) { - actor.Unparent(); Self().Add( actor ); PrepareForTranstionOut( actor ); } @@ -1202,7 +1139,6 @@ void Button::AddButtonImage( Actor& actor ) { if( actor ) { - actor.Unparent(); Self().Add( actor ); } } @@ -1453,13 +1389,13 @@ void Button::SetProperty( BaseObject* object, Property::Index index, const Prope case Toolkit::Button::Property::UNSELECTED_COLOR: { - GetImplementation( button ).SetUnselectedColor( value.Get< Vector4 >() ); + GetImplementation( button ).SetColor( value.Get< Vector4 >(), UnselectedState ); break; } case Toolkit::Button::Property::SELECTED_COLOR: { - GetImplementation( button ).SetSelectedColor( value.Get< Vector4 >() ); + GetImplementation( button ).SetColor( value.Get< Vector4 >(), SelectedState ); break; } @@ -1468,6 +1404,17 @@ void Button::SetProperty( BaseObject* object, Property::Index index, const Prope GetImplementation( button ).SetLabelText( value.Get< std::string >() ); break; } + + case Toolkit::Button::Property::LABEL: + { + // Get a Property::Map from the property if possible. + Property::Map setPropertyMap; + if( value.Get( setPropertyMap ) ) + { + GetImplementation( button ).ModifyLabel( setPropertyMap ); + } + } + break; } } } @@ -1553,6 +1500,13 @@ Property::Value Button::GetProperty( BaseObject* object, Property::Index propert value = GetImplementation( button ).GetLabelText(); break; } + + case Toolkit::Button::Property::LABEL: + { + Property::Map emptyMap; + value = emptyMap; + break; + } } } @@ -1571,8 +1525,7 @@ void Button::SetLabel( Actor label ) } mLabel = label; - mLabel.SetProperty( Toolkit::Control::Property::STYLE_NAME, STYLE_BUTTON_LABEL ); - mLabel.SetPosition( 0.f, 0.f ); + mLabel.SetPosition( 0.0f, 0.0f ); // label should be the top of the button Self().Add( mLabel );