From 088b32fd39a074d6ffc7d93bfa41ca3dee7f3eb6 Mon Sep 17 00:00:00 2001 From: Heeyong Song Date: Thu, 25 Jun 2020 16:25:55 +0900 Subject: [PATCH] (Visual) Support CORNER_RADIUS_POLICY Change-Id: I83b9f75f040da83d776e6987cb0450d527348eb9 --- .../src/dali-toolkit/utc-Dali-Visual.cpp | 74 +++++++++++++++++++++- .../devel-api/visuals/visual-properties-devel.h | 9 +++ .../internal/visuals/color/color-visual.cpp | 11 +++- .../internal/visuals/gradient/gradient-visual.cpp | 22 +++++-- .../visuals/image-visual-shader-factory.cpp | 11 +++- .../internal/visuals/visual-base-data-impl.cpp | 1 + .../internal/visuals/visual-base-data-impl.h | 1 + dali-toolkit/internal/visuals/visual-base-impl.cpp | 28 ++++++++ .../internal/visuals/visual-string-constants.cpp | 1 + .../internal/visuals/visual-string-constants.h | 1 + 10 files changed, 146 insertions(+), 13 deletions(-) diff --git a/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp b/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp index 4309d9b..3e7513e 100644 --- a/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp +++ b/automated-tests/src/dali-toolkit/utc-Dali-Visual.cpp @@ -511,6 +511,7 @@ int UtcDaliVisualGetPropertyMap1(void) propertyMap.Insert(Visual::Property::TYPE, Visual::COLOR); propertyMap.Insert(Visual::Property::MIX_COLOR, Color::BLUE); propertyMap.Insert( DevelVisual::Property::CORNER_RADIUS, 10.0f ); + propertyMap.Insert( DevelVisual::Property::CORNER_RADIUS_POLICY, Toolkit::Visual::Transform::Policy::RELATIVE ); propertyMap.Insert( DevelColorVisual::Property::BLUR_RADIUS, 20.0f ); Visual::Base colorVisual = factory.CreateVisual( propertyMap ); @@ -529,6 +530,10 @@ int UtcDaliVisualGetPropertyMap1(void) DALI_TEST_CHECK( cornerRadiusValue ); DALI_TEST_CHECK( cornerRadiusValue->Get< float >() == 10.0f ); + Property::Value* cornerRadiusPolicyValue = resultMap.Find( DevelVisual::Property::CORNER_RADIUS_POLICY, Property::INTEGER ); + DALI_TEST_CHECK( cornerRadiusPolicyValue ); + DALI_TEST_CHECK( cornerRadiusPolicyValue->Get< int >() == Toolkit::Visual::Transform::Policy::RELATIVE ); + Property::Value* blurRadiusValue = resultMap.Find( DevelColorVisual::Property::BLUR_RADIUS, Property::FLOAT ); DALI_TEST_CHECK( blurRadiusValue ); DALI_TEST_CHECK( blurRadiusValue->Get< float >() == 20.0f ); @@ -3661,7 +3666,7 @@ int UtcDaliVisualRoundedCorner(void) DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue< float >( "cornerRadius", cornerRadius ), true, TEST_LOCATION ); } - // color visual + // color visual 1 { VisualFactory factory = VisualFactory::Get(); Property::Map properties; @@ -3670,6 +3675,71 @@ int UtcDaliVisualRoundedCorner(void) properties[Visual::Property::TYPE] = Visual::COLOR; properties[ColorVisual::Property::MIX_COLOR] = Color::BLUE; properties["cornerRadius"] = cornerRadius; + properties["cornerRadiusPolicy"] = Toolkit::Visual::Transform::Policy::ABSOLUTE; + + Visual::Base visual = factory.CreateVisual( properties ); + + // trigger creation through setting on stage + DummyControl dummy = DummyControl::New( true ); + Impl::DummyControl& dummyImpl = static_cast< Impl::DummyControl& >( dummy.GetImplementation() ); + dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual ); + + dummy.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) ); + dummy.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + Stage::GetCurrent().Add( dummy ); + + application.SendNotification(); + application.Render(); + + application.SendNotification(); + application.Render(); + + DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue< float >( "cornerRadius", cornerRadius ), true, TEST_LOCATION ); + DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue< float >( "cornerRadiusPolicy", Toolkit::Visual::Transform::Policy::ABSOLUTE ), true, TEST_LOCATION ); + } + + // color visual 2 + { + VisualFactory factory = VisualFactory::Get(); + Property::Map properties; + float cornerRadius = 0.5f; + + properties[Visual::Property::TYPE] = Visual::COLOR; + properties[ColorVisual::Property::MIX_COLOR] = Color::BLUE; + properties[DevelVisual::Property::CORNER_RADIUS] = cornerRadius; + properties[DevelVisual::Property::CORNER_RADIUS_POLICY] = Toolkit::Visual::Transform::Policy::RELATIVE; + + Visual::Base visual = factory.CreateVisual( properties ); + + // trigger creation through setting on stage + DummyControl dummy = DummyControl::New( true ); + Impl::DummyControl& dummyImpl = static_cast< Impl::DummyControl& >( dummy.GetImplementation() ); + dummyImpl.RegisterVisual( DummyControl::Property::TEST_VISUAL, visual ); + + dummy.SetProperty( Actor::Property::SIZE, Vector2( 200.f, 200.f ) ); + dummy.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER ); + Stage::GetCurrent().Add( dummy ); + + application.SendNotification(); + application.Render(); + + application.SendNotification(); + application.Render(); + + DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue< float >( "cornerRadius", cornerRadius ), true, TEST_LOCATION ); + DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue< float >( "cornerRadiusPolicy", Toolkit::Visual::Transform::Policy::RELATIVE ), true, TEST_LOCATION ); + } + + // color visual 3 - invalid value + { + VisualFactory factory = VisualFactory::Get(); + Property::Map properties; + float cornerRadius = 30.0f; + + properties[Visual::Property::TYPE] = Visual::COLOR; + properties[ColorVisual::Property::MIX_COLOR] = Color::BLUE; + properties[DevelVisual::Property::CORNER_RADIUS] = cornerRadius; + properties[DevelVisual::Property::CORNER_RADIUS_POLICY] = -1; // Set an invalid value Visual::Base visual = factory.CreateVisual( properties ); @@ -3689,6 +3759,8 @@ int UtcDaliVisualRoundedCorner(void) application.Render(); DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue< float >( "cornerRadius", cornerRadius ), true, TEST_LOCATION ); + // Default corner radius policy is absolute. + DALI_TEST_EQUALS( application.GetGlAbstraction().CheckUniformValue< float >( "cornerRadiusPolicy", Toolkit::Visual::Transform::Policy::ABSOLUTE ), true, TEST_LOCATION ); } // gradient visual diff --git a/dali-toolkit/devel-api/visuals/visual-properties-devel.h b/dali-toolkit/devel-api/visuals/visual-properties-devel.h index e30f241..8164c2b 100644 --- a/dali-toolkit/devel-api/visuals/visual-properties-devel.h +++ b/dali-toolkit/devel-api/visuals/visual-properties-devel.h @@ -80,6 +80,15 @@ enum Type * @note Optional. */ CORNER_RADIUS = OPACITY + 2, + + /** + * @brief Whether the corner radius value is relative (percentage [0.0f to 1.0f] of the visual size) or absolute (in world units). + * @details Name "cornerRadiusPolicy", type Property::INTEGER. + * @see Policy::Type + * @note By default, it is ABSOLUTE to the visual's size. + * If it it RELATIVE, the corner radius value is relative to the smaller of the visual width and visual height. + */ + CORNER_RADIUS_POLICY = OPACITY + 3, }; } // namespace Property diff --git a/dali-toolkit/internal/visuals/color/color-visual.cpp b/dali-toolkit/internal/visuals/color/color-visual.cpp index cb2b447..74ad4d2 100644 --- a/dali-toolkit/internal/visuals/color/color-visual.cpp +++ b/dali-toolkit/internal/visuals/color/color-visual.cpp @@ -86,6 +86,7 @@ const char* VERTEX_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER( uniform mediump vec3 uSize;\n varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n \n //Visual size and offset uniform mediump vec2 offset;\n @@ -95,12 +96,16 @@ const char* VERTEX_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER( uniform mediump vec2 origin;\n uniform mediump vec2 anchorPoint;\n uniform mediump float cornerRadius;\n + uniform mediump float cornerRadiusPolicy;\n \n vec4 ComputeVertexPosition()\n {\n vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n - vRectSize = visualSize / 2.0 - cornerRadius;\n + mediump float minSize = min( visualSize.x, visualSize.y );\n + vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);\n + vCornerRadius = min( vCornerRadius, minSize * 0.5 );\n + vRectSize = visualSize / 2.0 - vCornerRadius;\n vPosition = aPosition* visualSize;\n return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n }\n @@ -115,13 +120,13 @@ const char* VERTEX_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER( const char* FRAGMENT_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER( varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n uniform lowp vec4 uColor;\n uniform lowp vec3 mixColor;\n - uniform mediump float cornerRadius;\n \n void main()\n {\n - mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - cornerRadius;\n + mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;\n gl_FragColor = uColor * vec4( mixColor, 1.0 );\n gl_FragColor.a *= 1.0 - smoothstep( -1.0, 1.0, dist );\n }\n diff --git a/dali-toolkit/internal/visuals/gradient/gradient-visual.cpp b/dali-toolkit/internal/visuals/gradient/gradient-visual.cpp index 1e0e46a..d6573e2 100644 --- a/dali-toolkit/internal/visuals/gradient/gradient-visual.cpp +++ b/dali-toolkit/internal/visuals/gradient/gradient-visual.cpp @@ -159,6 +159,7 @@ DALI_COMPOSE_SHADER( varying mediump vec2 vTexCoord;\n varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n \n //Visual size and offset uniform mediump vec2 offset;\n @@ -167,12 +168,16 @@ DALI_COMPOSE_SHADER( uniform mediump vec2 origin;\n uniform mediump vec2 anchorPoint;\n uniform mediump float cornerRadius;\n + uniform mediump float cornerRadiusPolicy;\n vec4 ComputeVertexPosition()\n {\n vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n - vRectSize = visualSize * 0.5 - cornerRadius;\n + mediump float minSize = min( visualSize.x, visualSize.y );\n + vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);\n + vCornerRadius = min( vCornerRadius, minSize * 0.5 );\n + vRectSize = visualSize * 0.5 - vCornerRadius;\n vPosition = aPosition * visualSize;\n return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n }\n @@ -195,6 +200,7 @@ DALI_COMPOSE_SHADER( varying mediump vec2 vTexCoord;\n varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n \n //Visual size and offset uniform mediump vec2 offset;\n @@ -203,12 +209,16 @@ DALI_COMPOSE_SHADER( uniform mediump vec2 origin;\n uniform mediump vec2 anchorPoint;\n uniform mediump float cornerRadius;\n + uniform mediump float cornerRadiusPolicy;\n vec4 ComputeVertexPosition()\n {\n vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n - vRectSize = visualSize * 0.5 - cornerRadius;\n + mediump float minSize = min( visualSize.x, visualSize.y );\n + vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);\n + vCornerRadius = min( vCornerRadius, minSize * 0.5 );\n + vRectSize = visualSize * 0.5 - vCornerRadius;\n vPosition = aPosition * visualSize;\n return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n }\n @@ -257,14 +267,14 @@ DALI_COMPOSE_SHADER( uniform sampler2D sTexture;\n // sampler1D? uniform lowp vec4 uColor;\n uniform lowp vec3 mixColor;\n - uniform mediump float cornerRadius;\n varying mediump vec2 vTexCoord;\n varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n \n void main()\n {\n - mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - cornerRadius;\n + mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;\n gl_FragColor = texture2D( sTexture, vec2( vTexCoord.y, 0.5 ) ) * vec4(mixColor, 1.0) * uColor;\n gl_FragColor *= 1.0 - smoothstep( -1.0, 1.0, dist );\n }\n @@ -275,14 +285,14 @@ DALI_COMPOSE_SHADER( uniform sampler2D sTexture;\n // sampler1D? uniform lowp vec4 uColor;\n uniform lowp vec3 mixColor;\n - uniform mediump float cornerRadius;\n varying mediump vec2 vTexCoord;\n varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n \n void main()\n {\n - mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - cornerRadius;\n + mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;\n gl_FragColor = texture2D( sTexture, vec2( length(vTexCoord), 0.5 ) ) * vec4(mixColor, 1.0) * uColor;\n gl_FragColor *= 1.0 - smoothstep( -1.0, 1.0, dist );\n }\n diff --git a/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp b/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp index d9f273c..beb4f09 100644 --- a/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp +++ b/dali-toolkit/internal/visuals/image-visual-shader-factory.cpp @@ -129,6 +129,7 @@ const char* VERTEX_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER( varying mediump vec2 vTexCoord;\n varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n \n //Visual size and offset uniform mediump vec2 offset;\n @@ -137,13 +138,17 @@ const char* VERTEX_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER( uniform mediump vec2 origin;\n uniform mediump vec2 anchorPoint;\n uniform mediump float cornerRadius;\n + uniform mediump float cornerRadiusPolicy;\n uniform mediump vec2 extraSize;\n \n vec4 ComputeVertexPosition()\n {\n vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw ) + extraSize;\n vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n - vRectSize = visualSize * 0.5 - cornerRadius;\n + mediump float minSize = min( visualSize.x, visualSize.y );\n + vCornerRadius = mix( cornerRadius * minSize, cornerRadius, cornerRadiusPolicy);\n + vCornerRadius = min( vCornerRadius, minSize * 0.5 );\n + vRectSize = visualSize * 0.5 - vCornerRadius;\n vPosition = aPosition* visualSize;\n return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n }\n @@ -160,15 +165,15 @@ const char* FRAGMENT_SHADER_ROUNDED_CORNER = DALI_COMPOSE_SHADER( varying mediump vec2 vTexCoord;\n varying mediump vec2 vPosition;\n varying mediump vec2 vRectSize;\n + varying mediump float vCornerRadius;\n uniform sampler2D sTexture;\n uniform lowp vec4 uColor;\n uniform lowp vec3 mixColor;\n - uniform mediump float cornerRadius;\n uniform lowp float preMultipliedAlpha;\n \n void main()\n {\n - mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - cornerRadius;\n + mediump float dist = length( max( abs( vPosition ), vRectSize ) - vRectSize ) - vCornerRadius;\n mediump float opacity = 1.0 - smoothstep( -1.0, 1.0, dist );\n gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4( mixColor, 1.0 );\n gl_FragColor.a *= opacity;\n diff --git a/dali-toolkit/internal/visuals/visual-base-data-impl.cpp b/dali-toolkit/internal/visuals/visual-base-data-impl.cpp index 41040ef..ba9f925 100644 --- a/dali-toolkit/internal/visuals/visual-base-data-impl.cpp +++ b/dali-toolkit/internal/visuals/visual-base-data-impl.cpp @@ -123,6 +123,7 @@ Internal::Visual::Base::Impl::Impl( FittingMode fittingMode, Toolkit::Visual::Ty mMixColor( Color::WHITE ), mControlSize( Vector2::ZERO ), mCornerRadius( 0.0f ), + mCornerRadiusPolicy( 1.0f ), mDepthIndex( 0.0f ), mMixColorIndex( Property::INVALID_INDEX ), mCornerRadiusIndex( Property::INVALID_INDEX ), diff --git a/dali-toolkit/internal/visuals/visual-base-data-impl.h b/dali-toolkit/internal/visuals/visual-base-data-impl.h index 1b6afa3..23bcda9 100644 --- a/dali-toolkit/internal/visuals/visual-base-data-impl.h +++ b/dali-toolkit/internal/visuals/visual-base-data-impl.h @@ -127,6 +127,7 @@ struct Base::Impl Vector4 mMixColor; Size mControlSize; float mCornerRadius; + float mCornerRadiusPolicy; int mDepthIndex; Property::Index mMixColorIndex; Property::Index mCornerRadiusIndex; diff --git a/dali-toolkit/internal/visuals/visual-base-impl.cpp b/dali-toolkit/internal/visuals/visual-base-impl.cpp index c63a9e2..2a21570 100755 --- a/dali-toolkit/internal/visuals/visual-base-impl.cpp +++ b/dali-toolkit/internal/visuals/visual-base-impl.cpp @@ -129,6 +129,10 @@ void Visual::Base::SetProperties( const Property::Map& propertyMap ) { matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS ); } + else if( matchKey == CORNER_RADIUS_POLICY ) + { + matchKey = Property::Key( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY ); + } } switch( matchKey.indexKey ) @@ -205,6 +209,28 @@ void Visual::Base::SetProperties( const Property::Map& propertyMap ) } break; } + case Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY: + { + int policy; + if( value.Get( policy ) ) + { + switch( policy ) + { + case Toolkit::Visual::Transform::Policy::RELATIVE: + case Toolkit::Visual::Transform::Policy::ABSOLUTE: + { + mImpl->mCornerRadiusPolicy = policy; + break; + } + default: + { + DALI_LOG_ERROR( "Unsupported policy: %d\n", policy ); + break; + } + } + } + break; + } } } @@ -299,6 +325,7 @@ void Visual::Base::SetOnStage( Actor& actor ) if( IsRoundedCornerRequired() ) { mImpl->mCornerRadiusIndex = mImpl->mRenderer.RegisterProperty( CORNER_RADIUS, mImpl->mCornerRadius ); + mImpl->mRenderer.RegisterProperty( CORNER_RADIUS_POLICY, mImpl->mCornerRadiusPolicy ); mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON ); } @@ -348,6 +375,7 @@ void Visual::Base::CreatePropertyMap( Property::Map& map ) const map.Insert( Toolkit::DevelVisual::Property::VISUAL_FITTING_MODE, fittingModeString ); map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS, mImpl->mCornerRadius ); + map.Insert( Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, static_cast< int >( mImpl->mCornerRadiusPolicy ) ); } void Visual::Base::CreateInstancePropertyMap( Property::Map& map ) const diff --git a/dali-toolkit/internal/visuals/visual-string-constants.cpp b/dali-toolkit/internal/visuals/visual-string-constants.cpp index 293628e..fa1b1fd 100644 --- a/dali-toolkit/internal/visuals/visual-string-constants.cpp +++ b/dali-toolkit/internal/visuals/visual-string-constants.cpp @@ -80,6 +80,7 @@ const char * const VISUAL_FITTING_MODE( "visualFittingMode" ); // Corner radius const char * const CORNER_RADIUS( "cornerRadius" ); +const char * const CORNER_RADIUS_POLICY( "cornerRadiusPolicy" ); // Color visual const char * const RENDER_IF_TRANSPARENT_NAME( "renderIfTransparent" ); diff --git a/dali-toolkit/internal/visuals/visual-string-constants.h b/dali-toolkit/internal/visuals/visual-string-constants.h index d402e7f..f972f37 100644 --- a/dali-toolkit/internal/visuals/visual-string-constants.h +++ b/dali-toolkit/internal/visuals/visual-string-constants.h @@ -64,6 +64,7 @@ extern const char * const VISUAL_FITTING_MODE; // Corner radius extern const char * const CORNER_RADIUS; +extern const char * const CORNER_RADIUS_POLICY; // Color visual extern const char * const RENDER_IF_TRANSPARENT_NAME; -- 2.7.4