X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fvisuals%2Farc%2Farc-visual.cpp;h=bf07a40a5380e5a887b0dd0c50820a824ccc7c90;hp=d694235dfc6160bda9e52620f9e92e8c3e5e258c;hb=1d71a8f7d7abd7729aa645ad062e530958097214;hpb=fa2cf9a483feaf77e4053f6b31a3046db8627269 diff --git a/dali-toolkit/internal/visuals/arc/arc-visual.cpp b/dali-toolkit/internal/visuals/arc/arc-visual.cpp index d694235..bf07a40 100644 --- a/dali-toolkit/internal/visuals/arc/arc-visual.cpp +++ b/dali-toolkit/internal/visuals/arc/arc-visual.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2022 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. @@ -23,148 +23,49 @@ //INTERNAL INCLUDES #include -#include -#include +#include +#include #include +#include #include -#include namespace Dali { - namespace Toolkit { - namespace Internal { - namespace { +const int CUSTOM_PROPERTY_COUNT(9); // 5 transform properties + thickness,start,sweep,radius // cap -DALI_ENUM_TO_STRING_TABLE_BEGIN( CAP ) -DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, BUTT ) -DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, ROUND ) -DALI_ENUM_TO_STRING_TABLE_END( CAP ) - -const char* VERTEX_SHADER = - "INPUT mediump vec2 aPosition;\n" - "OUTPUT mediump vec2 vPosition;\n" - - "uniform highp mat4 uMvpMatrix;\n" - "uniform highp vec3 uSize;\n" - - "//Visual size and offset\n" - "uniform mediump vec2 offset;\n" - "uniform highp vec2 size;\n" - "uniform mediump vec4 offsetSizeMode;\n" - "uniform mediump vec2 origin;\n" - "uniform mediump vec2 anchorPoint;\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" - " vPosition = aPosition* visualSize;\n" - " return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n" - "}\n" - - "void main()\n" - "{\n" - " gl_Position = uMvpMatrix * ComputeVertexPosition();\n" - "}\n"; - -const char* FRAGMENT_SHADER_BUTT_CAP = - "INPUT mediump vec2 vPosition;\n" - - "uniform lowp vec4 uColor;\n" - "uniform lowp vec3 mixColor;\n" - "uniform mediump float thickness;\n" - "uniform mediump float radius;\n" - "uniform mediump float startAngle;\n" - "uniform mediump float sweepAngle;\n" - - "const mediump float M_PI_OVER_2 = 1.57079632679;\n" - "const mediump float M_PI = 3.14159265359;\n" - "const mediump float M_PI_2 = 6.28318530718;\n" - - "mediump float GetOpacity()\n" - "{\n" - " mediump float start = radians( mod( startAngle, 360.0 ) );\n" - " mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n" - " mediump float dist = length( vPosition );\n" - " if( angle <= radians( sweepAngle ) )\n" - " {\n" - " return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n" - " }\n" - " mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n" - " mediump vec2 q0 = vec2( dist * cos( start - M_PI_OVER_2 ), dist * sin( start - M_PI_OVER_2 ) );\n" - " mediump vec2 q1 = vec2( dist * cos( end - M_PI_OVER_2 ), dist * sin( end - M_PI_OVER_2 ) );\n" - " mediump float opacity = 1.0 - smoothstep( 0.0, 2.0, min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n" - " opacity *= step( 0.0, thickness / 2.0 - abs( dist - radius ) );\n" - " return opacity;\n" - "}\n" - - "void main()\n" - "{\n" - " OUT_COLOR = vec4( mixColor, 1.0 ) * uColor;\n" - " OUT_COLOR.a *= GetOpacity();\n" - "}\n"; - -const char* FRAGMENT_SHADER_ROUND_CAP = - "INPUT mediump vec2 vPosition;\n" - - "uniform lowp vec4 uColor;\n" - "uniform lowp vec3 mixColor;\n" - "uniform mediump float thickness;\n" - "uniform mediump float radius;\n" - "uniform mediump float startAngle;\n" - "uniform mediump float sweepAngle;\n" - - "const mediump float M_PI_OVER_2 = 1.57079632679;\n" - "const mediump float M_PI_2 = 6.28318530718;\n" - - "mediump float GetOpacity()\n" - "{\n" - " mediump float start = radians( mod( startAngle, 360.0 ) );\n" - " mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n" - " mediump float dist = length( vPosition );\n" - " if( angle <= radians( sweepAngle ) )\n" - " {\n" - " return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n" - " }\n" - " mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n" - " mediump vec2 q0 = vec2( radius * cos( start - M_PI_OVER_2 ), radius * sin( start - M_PI_OVER_2 ) );\n" - " mediump vec2 q1 = vec2( radius * cos( end - M_PI_OVER_2 ), radius * sin( end - M_PI_OVER_2 ) );\n" - " return smoothstep( -1.0, 1.0, thickness / 2.0 - min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n" - "}\n" - - "void main()\n" - "{\n" - " OUT_COLOR = vec4( mixColor, 1.0 ) * uColor;\n" - " OUT_COLOR.a *= GetOpacity();\n" - "}\n"; +DALI_ENUM_TO_STRING_TABLE_BEGIN(CAP) + DALI_ENUM_TO_STRING_WITH_SCOPE(DevelArcVisual::Cap, BUTT) + DALI_ENUM_TO_STRING_WITH_SCOPE(DevelArcVisual::Cap, ROUND) +DALI_ENUM_TO_STRING_TABLE_END(CAP) -} +} // namespace -ArcVisualPtr ArcVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties ) +ArcVisualPtr ArcVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties) { - ArcVisualPtr arcVisualPtr( new ArcVisual( factoryCache ) ); - arcVisualPtr->SetProperties( properties ); + ArcVisualPtr arcVisualPtr(new ArcVisual(factoryCache)); + arcVisualPtr->SetProperties(properties); + arcVisualPtr->Initialize(); return arcVisualPtr; } -ArcVisual::ArcVisual( VisualFactoryCache& factoryCache ) -: Visual::Base( factoryCache, Visual::FittingMode::FILL, static_cast( Toolkit::DevelVisual::ARC ) ), - mThickness( 0.0f ), - mRadius( 0.0f ), - mStartAngle( 0.0f ), - mSweepAngle( 360.0f ), - mRadiusIndex( Property::INVALID_INDEX ), - mThicknessIndex( Property::INVALID_INDEX ), - mStartAngleIndex( Property::INVALID_INDEX ), - mSweepAngleIndex( Property::INVALID_INDEX ), - mCapType( DevelArcVisual::Cap::BUTT ) +ArcVisual::ArcVisual(VisualFactoryCache& factoryCache) +: Visual::Base(factoryCache, Visual::FittingMode::FILL, static_cast(Toolkit::DevelVisual::ARC)), + mThickness(0.0f), + mRadius(0.0f), + mStartAngle(0.0f), + mSweepAngle(360.0f), + mRadiusIndex(Property::INVALID_INDEX), + mThicknessIndex(Property::INVALID_INDEX), + mStartAngleIndex(Property::INVALID_INDEX), + mSweepAngleIndex(Property::INVALID_INDEX), + mCapType(DevelArcVisual::Cap::BUTT) { } @@ -172,20 +73,20 @@ ArcVisual::~ArcVisual() { } -void ArcVisual::DoSetProperties( const Property::Map& propertyMap ) +void ArcVisual::DoSetProperties(const Property::Map& propertyMap) { - Property::Value* thicknessValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME ); - if( thicknessValue ) + Property::Value* thicknessValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME); + if(thicknessValue) { - if( !thicknessValue->Get( mThickness ) ) + if(!thicknessValue->Get(mThickness)) { - DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType() ); + DALI_LOG_ERROR("ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType()); } else { - if( mImpl->mRenderer ) + if(mImpl->mRenderer) { - mImpl->mRenderer.SetProperty( mThicknessIndex, mThickness ); + mImpl->mRenderer.SetProperty(mThicknessIndex, mThickness); // Need to calculate radius again OnSetTransform(); @@ -193,76 +94,61 @@ void ArcVisual::DoSetProperties( const Property::Map& propertyMap ) } } - Property::Value* startAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME ); - if( startAngleValue ) + Property::Value* startAngleValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME); + if(startAngleValue) { - if( !startAngleValue->Get( mStartAngle ) ) + if(!startAngleValue->Get(mStartAngle)) { - DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType() ); + DALI_LOG_ERROR("ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType()); } else { - if( mImpl->mRenderer ) + if(mImpl->mRenderer) { - mImpl->mRenderer.SetProperty( mStartAngleIndex, mStartAngle ); + mImpl->mRenderer.SetProperty(mStartAngleIndex, mStartAngle); } } } - Property::Value* sweepAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME ); - if( sweepAngleValue ) + Property::Value* sweepAngleValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME); + if(sweepAngleValue) { - if( !sweepAngleValue->Get( mSweepAngle ) ) + if(!sweepAngleValue->Get(mSweepAngle)) { - DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType() ); + DALI_LOG_ERROR("ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType()); } else { - if( mImpl->mRenderer ) + if(mImpl->mRenderer) { - mImpl->mRenderer.SetProperty( mSweepAngleIndex, mSweepAngle ); + mImpl->mRenderer.SetProperty(mSweepAngleIndex, mSweepAngle); } } } - Property::Value* capValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::CAP, CAP_NAME ); - if( capValue ) + Property::Value* capValue = propertyMap.Find(Toolkit::DevelArcVisual::Property::CAP, CAP_NAME); + if(capValue) { int capType = 0; - Scripting::GetEnumerationProperty( *capValue, CAP_TABLE, CAP_TABLE_COUNT, capType ); - mCapType = Toolkit::DevelArcVisual::Cap::Type( capType ); + Scripting::GetEnumerationProperty(*capValue, CAP_TABLE, CAP_TABLE_COUNT, capType); + mCapType = Toolkit::DevelArcVisual::Cap::Type(capType); } } -void ArcVisual::DoSetOnScene( Actor& actor ) +void ArcVisual::DoSetOnScene(Actor& actor) { - InitializeRenderer(); - - actor.AddRenderer( mImpl->mRenderer ); + actor.AddRenderer(mImpl->mRenderer); // Arc Visual generated and ready to display - ResourceReady( Toolkit::Visual::ResourceStatus::READY ); + ResourceReady(Toolkit::Visual::ResourceStatus::READY); } void ArcVisual::DoSetOffScene(Actor& actor) { - if(mImpl->mRenderer) - { - // Update values from Renderer - mThickness = mImpl->mRenderer.GetProperty(mThicknessIndex); - mStartAngle = mImpl->mRenderer.GetProperty(mStartAngleIndex); - mSweepAngle = mImpl->mRenderer.GetProperty(mSweepAngleIndex); - } - actor.RemoveRenderer(mImpl->mRenderer); - mImpl->mRenderer.Reset(); - - mThicknessIndex = Property::INVALID_INDEX; - mStartAngleIndex = Property::INVALID_INDEX; - mSweepAngleIndex = Property::INVALID_INDEX; } -void ArcVisual::DoCreatePropertyMap( Property::Map& map ) const +void ArcVisual::DoCreatePropertyMap(Property::Map& map) const { float thickness, startAngle, sweepAngle; if(mImpl->mRenderer) @@ -280,82 +166,66 @@ void ArcVisual::DoCreatePropertyMap( Property::Map& map ) const } map.Clear(); - map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ARC ); + map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ARC); map.Insert(Toolkit::DevelArcVisual::Property::THICKNESS, thickness); map.Insert(Toolkit::DevelArcVisual::Property::START_ANGLE, startAngle); map.Insert(Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, sweepAngle); - map.Insert( Toolkit::DevelArcVisual::Property::CAP, mCapType ); + map.Insert(Toolkit::DevelArcVisual::Property::CAP, mCapType); } -void ArcVisual::DoCreateInstancePropertyMap( Property::Map& map ) const +void ArcVisual::DoCreateInstancePropertyMap(Property::Map& map) const { // Do nothing } void ArcVisual::OnSetTransform() { - Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize ); - mRadius = ( std::min( visualSize.width, visualSize.height ) - mThickness ) / 2.0f; - - if( mImpl->mRenderer ) - { - mImpl->mRenderer.SetProperty( mRadiusIndex, mRadius ); - } -} + Vector2 visualSize = mImpl->mTransform.GetVisualSize(mImpl->mControlSize); + mRadius = (std::min(visualSize.width, visualSize.height) - mThickness) / 2.0f; -void ArcVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes ) -{ - // Check if action is valid for this visual type and perform action if possible - switch( actionId ) + if(mImpl->mRenderer) { - case DevelArcVisual::Action::UPDATE_PROPERTY: - { - const Property::Map* map = attributes.GetMap(); - if( map ) - { - DoSetProperties( *map ); - } - break; - } + mImpl->mRenderer.SetProperty(mRadiusIndex, mRadius); } } -void ArcVisual::InitializeRenderer() +void ArcVisual::OnInitialize() { - Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY ); + Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::QUAD_GEOMETRY); Shader shader; - if( mCapType == DevelArcVisual::Cap::BUTT ) + if(mCapType == DevelArcVisual::Cap::BUTT) { - shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER ); - if( !shader ) + shader = mFactoryCache.GetShader(VisualFactoryCache::ARC_BUTT_CAP_SHADER); + if(!shader) { - shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_BUTT_CAP ); - mFactoryCache.SaveShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER, shader ); + shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_ARC_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_ARC_VISUAL_BUTT_CAP_SHADER_FRAG.data()); + mFactoryCache.SaveShader(VisualFactoryCache::ARC_BUTT_CAP_SHADER, shader); } } else { - shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER ); - if( !shader ) + shader = mFactoryCache.GetShader(VisualFactoryCache::ARC_ROUND_CAP_SHADER); + if(!shader) { - shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_ROUND_CAP ); - mFactoryCache.SaveShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER, shader ); + shader = Shader::New(Dali::Shader::GetVertexShaderPrefix() + SHADER_ARC_VISUAL_SHADER_VERT.data(), Dali::Shader::GetFragmentShaderPrefix() + SHADER_ARC_VISUAL_ROUND_CAP_SHADER_FRAG.data()); + mFactoryCache.SaveShader(VisualFactoryCache::ARC_ROUND_CAP_SHADER, shader); } } - mImpl->mRenderer = Renderer::New( geometry, shader ); + mImpl->mRenderer = Renderer::New(geometry, shader); + mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT); - mThicknessIndex = mImpl->mRenderer.RegisterProperty(DevelArcVisual::Property::THICKNESS, THICKNESS_NAME, mThickness); - mStartAngleIndex = mImpl->mRenderer.RegisterProperty(DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME, mStartAngle); - mSweepAngleIndex = mImpl->mRenderer.RegisterProperty(DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME, mSweepAngle); + mThicknessIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelArcVisual::Property::THICKNESS, THICKNESS_NAME, mThickness); + mStartAngleIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME, mStartAngle); + mSweepAngleIndex = mImpl->mRenderer.RegisterUniqueProperty(DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME, mSweepAngle); - mRadiusIndex = mImpl->mRenderer.RegisterProperty( RADIUS_NAME, mRadius ); + mRadiusIndex = mImpl->mRenderer.RegisterProperty(RADIUS_NAME, mRadius); - mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON ); + mImpl->mRenderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON); // Register transform properties - mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT ); + mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT); } } // namespace Internal