From b4d349772a1e4efc51972233461c545de3c9463c Mon Sep 17 00:00:00 2001 From: Seoyeon Kim Date: Tue, 19 Jun 2018 18:47:56 +0900 Subject: [PATCH] [Tizen] Fix ImageView Padding and Margin issues - Currently padding does not apply to ImageView because the default Fitting Mode of ImageVisual have changed to FittingMode::FILL, so ImageVisual does not transform anything on ImageView::OnRelayout(). - Add Margin calculation to set Image Visual position on ImageView::OnRelayout(). - Trigger to call Relayout when setting Margin and Padding. Change-Id: Ic9090180caec59dc5a5f0d63bbb95757f18be0ed Signed-off-by: Seoyeon Kim --- .../src/dali-toolkit/utc-Dali-ImageView.cpp | 50 ++++++++++++++++ .../controls/control/control-data-impl.cpp | 6 ++ .../controls/image-view/image-view-impl.cpp | 69 ++++++++++++---------- dali-toolkit/public-api/controls/control-impl.cpp | 27 +++------ 4 files changed, 101 insertions(+), 51 deletions(-) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp index c9cbdc5..3931490 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-ImageView.cpp @@ -1619,3 +1619,53 @@ int UtcDaliImageViewResourceReadySignalWithReusedImage02(void) END_TEST; } + +int UtcDaliImageViewPaddingProperty(void) +{ + ToolkitTestApplication application; + + ImageView imageView = ImageView::New(); + Property::Map imagePropertyMap; + imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE; + imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = TEST_RESOURCE_DIR "/gallery-small-1.jpg" ; + imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = 128; + imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 128; + imageView.SetProperty( Toolkit::ImageView::Property::IMAGE , imagePropertyMap ); + imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT ); + imageView.SetParentOrigin( ParentOrigin::TOP_LEFT ); + imageView.SetProperty( Control::Property::PADDING, Extents( 15, 10, 5, 10 ) ); + imageView.SetProperty( Control::Property::MARGIN, Extents( 10, 10, 10, 10 ) ); + Stage::GetCurrent().Add( imageView ); + + application.SendNotification(); + application.Render(); + + DALI_TEST_EQUALS( imageView.GetProperty( Control::Property::PADDING ), Extents( 15, 10, 5, 10 ), TEST_LOCATION ); + + ImageView childImage = ImageView::New(); + childImage.SetBackgroundColor( Color::BLACK ); + childImage.SetSize( 10.f, 10.f ); + imageView.Add( childImage ); + + application.SendNotification(); + application.Render(); + + // Child ImageView should be positioned dependinig on Parent ImageView's Padding value + DALI_TEST_EQUALS( childImage.GetProperty( Dali::Actor::Property::POSITION ), Vector3( 25, 15, 0 ), TEST_LOCATION ); + + // Check whether Image Visual transforms on ImageVieiw::OnRelayout() + Toolkit::Internal::Control& controlImpl = Toolkit::Internal::GetImplementation( imageView ); + Toolkit::Visual::Base imageVisual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE ); + Property::Map resultMap; + imageVisual.CreatePropertyMap( resultMap ); + + Property::Value* transformValue = resultMap.Find( Visual::Property::TRANSFORM ); + DALI_TEST_CHECK( transformValue ); + Property::Map* retMap = transformValue->GetMap(); + DALI_TEST_CHECK( retMap ); + + // Image Visual should be positioned depending on ImageView's padding and margin + DALI_TEST_EQUALS( retMap->Find( Visual::Transform::Property::OFFSET )->Get< Vector2 >(), Vector2( 25, 15 ), TEST_LOCATION ); + + END_TEST; +} diff --git a/dali-toolkit/internal/controls/control/control-data-impl.cpp b/dali-toolkit/internal/controls/control/control-data-impl.cpp index 73769c8..f1eb934 100755 --- a/dali-toolkit/internal/controls/control/control-data-impl.cpp +++ b/dali-toolkit/internal/controls/control/control-data-impl.cpp @@ -1391,6 +1391,9 @@ void Control::Impl::OnStageDisconnection() void Control::Impl::SetMargin( Extents margin ) { mControlImpl.mImpl->mMargin = margin; + + // Trigger a size negotiation request that may be needed when setting a margin. + mControlImpl.RelayoutRequest(); } Extents Control::Impl::GetMargin() const @@ -1401,6 +1404,9 @@ Extents Control::Impl::GetMargin() const void Control::Impl::SetPadding( Extents padding ) { mControlImpl.mImpl->mPadding = padding; + + // Trigger a size negotiation request that may be needed when setting a padding. + mControlImpl.RelayoutRequest(); } Extents Control::Impl::GetPadding() const diff --git a/dali-toolkit/internal/controls/image-view/image-view-impl.cpp b/dali-toolkit/internal/controls/image-view/image-view-impl.cpp index e4d3677..529832f 100755 --- a/dali-toolkit/internal/controls/image-view/image-view-impl.cpp +++ b/dali-toolkit/internal/controls/image-view/image-view-impl.cpp @@ -280,45 +280,52 @@ void ImageView::OnRelayout( const Vector2& size, RelayoutContainer& container ) { Property::Map transformMap = Property::Map(); - // Don't transform if fitting mode is FILL - if(Toolkit::GetImplementation(mVisual).GetFittingMode() == Visual::FittingMode::FIT_KEEP_ASPECT_RATIO) - { - Extents padding; - padding = Self().GetProperty( Toolkit::Control::Property::PADDING ); + Extents padding; + padding = Self().GetProperty( Toolkit::Control::Property::PADDING ); - Dali::LayoutDirection::Type layoutDirection = static_cast( - Self().GetProperty(Dali::Actor::Property::LAYOUT_DIRECTION).Get()); + Dali::LayoutDirection::Type layoutDirection = static_cast( + Self().GetProperty( Dali::Actor::Property::LAYOUT_DIRECTION ).Get() ); - if (Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection) - { - std::swap(padding.start, padding.end); - } - - // remove padding from the size to know how much is left for the visual - auto paddedSize = size - Vector2(padding.start + padding.end, padding.top + padding.bottom); + if( Dali::LayoutDirection::RIGHT_TO_LEFT == layoutDirection ) + { + std::swap( padding.start, padding.end ); + } - Vector2 naturalSize; - mVisual.GetNaturalSize(naturalSize); + // remove padding from the size to know how much is left for the visual + auto paddedSize = size - Vector2( padding.start + padding.end, padding.top + padding.bottom ); - // scale to fit the padded area - auto finalSize = - naturalSize * std::min( ( naturalSize.width ? ( paddedSize.width / naturalSize.width ) : 0 ), - ( naturalSize.height ? ( paddedSize.height / naturalSize.height ) : 0 ) ); + Vector2 naturalSize; + mVisual.GetNaturalSize( naturalSize ); - // calculate final offset within the padded area - auto finalOffset = Vector2(padding.start, padding.top) + (paddedSize - finalSize) * .5f; + // scale to fit the padded area + auto finalSize = + naturalSize * std::min( ( naturalSize.width ? ( paddedSize.width / naturalSize.width ) : 0 ), + ( naturalSize.height ? ( paddedSize.height / naturalSize.height ) : 0 ) ); - // populate the transform map - transformMap.Add(Toolkit::Visual::Transform::Property::OFFSET, finalOffset) - .Add(Toolkit::Visual::Transform::Property::OFFSET_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, Toolkit::Align::TOP_BEGIN) - .Add(Toolkit::Visual::Transform::Property::SIZE, finalSize) - .Add(Toolkit::Visual::Transform::Property::SIZE_POLICY, - Vector2(Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE)); + // calculate final offset within the padded area + auto finalOffset = Vector2( padding.start, padding.top ) + ( paddedSize - finalSize ) * .5f; + // When set the margin on ImageView, the visual should be repositioned. + Extents margin = Self().GetProperty( Toolkit::Control::Property::MARGIN ); + if( ( margin.start != 0 ) || ( margin.end != 0 ) || ( margin.top != 0 ) || ( margin.bottom != 0 ) ) + { + if( Self().GetCurrentAnchorPoint() != AnchorPoint::CENTER ) + { + finalOffset.x += margin.start; + finalOffset.y += margin.top; + } } + + // populate the transform map + transformMap.Add( Toolkit::Visual::Transform::Property::OFFSET, finalOffset ) + .Add( Toolkit::Visual::Transform::Property::OFFSET_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, Toolkit::Align::TOP_BEGIN ) + .Add( Toolkit::Visual::Transform::Property::SIZE, finalSize ) + .Add( Toolkit::Visual::Transform::Property::SIZE_POLICY, + Vector2( Toolkit::Visual::Transform::Policy::ABSOLUTE, Toolkit::Visual::Transform::Policy::ABSOLUTE ) ); + // Should provide a transform that handles aspect ratio according to image size mVisual.SetTransformAndSize( transformMap, size ); } diff --git a/dali-toolkit/public-api/controls/control-impl.cpp b/dali-toolkit/public-api/controls/control-impl.cpp index df78b15..010ac04 100755 --- a/dali-toolkit/public-api/controls/control-impl.cpp +++ b/dali-toolkit/public-api/controls/control-impl.cpp @@ -139,34 +139,21 @@ void SetBackgroundVisual( Control::Impl& controlImpl, Toolkit::Visual::Base& vis { Property::Map transformMap = Property::Map(); - Vector2 newSize( 0.f, 0.f ); - newSize.width = size.width + ( controlImpl.mPadding.start + controlImpl.mPadding.end ); - newSize.height = size.height + ( controlImpl.mPadding.top + controlImpl.mPadding.bottom ); - - if( ( controlImpl.mMargin.start != 0 ) || - ( controlImpl.mMargin.end != 0 ) || - ( controlImpl.mMargin.top != 0 ) || - ( controlImpl.mMargin.bottom != 0 ) ) + if( ( ( controlImpl.mMargin.start != 0 ) || + ( controlImpl.mMargin.end != 0 ) || + ( controlImpl.mMargin.top != 0 ) || + ( controlImpl.mMargin.bottom != 0 ) ) && + ( controlImpl.mControlImpl.Self().GetCurrentAnchorPoint() != AnchorPoint::CENTER ) ) { - transformMap.Add( Toolkit::Visual::Transform::Property::SIZE, newSize ) + transformMap.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 ) ) .Add( Toolkit::Visual::Transform::Property::OFFSET, Vector2( controlImpl.mMargin.start, controlImpl.mMargin.top ) ) .Add( Toolkit::Visual::Transform::Property::OFFSET_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, Toolkit::Align::TOP_BEGIN ); } - else if( ( controlImpl.mPadding.start != 0 ) || - ( controlImpl.mPadding.end != 0 ) || - ( controlImpl.mPadding.top != 0 ) || - ( controlImpl.mPadding.bottom != 0 ) ) - { - transformMap.Add( Toolkit::Visual::Transform::Property::SIZE, newSize ) - .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, Toolkit::Align::TOP_BEGIN ); - } - visual.SetTransformAndSize( transformMap, newSize ); // Send an empty map as we do not want to modify the visual's set transform + visual.SetTransformAndSize( transformMap, size ); // Send an empty map as we do not want to modify the visual's set transform } } // unnamed namespace -- 2.7.4