2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-toolkit/internal/visuals/arc/arc-visual.h>
22 #include <dali/integration-api/debug.h>
25 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
26 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
27 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
28 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
29 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
44 DALI_ENUM_TO_STRING_TABLE_BEGIN( CAP )
45 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, BUTT )
46 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, ROUND )
47 DALI_ENUM_TO_STRING_TABLE_END( CAP )
49 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
50 attribute mediump vec2 aPosition;\n
51 uniform highp mat4 uMvpMatrix;\n
52 uniform mediump vec3 uSize;\n
54 varying mediump vec2 vPosition;\n
56 //Visual size and offset
57 uniform mediump vec2 offset;\n
58 uniform mediump vec2 size;\n
59 uniform mediump vec4 offsetSizeMode;\n
60 uniform mediump vec2 origin;\n
61 uniform mediump vec2 anchorPoint;\n
63 vec4 ComputeVertexPosition()\n
65 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
66 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
67 vPosition = aPosition* visualSize;\n
68 return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
73 gl_Position = uMvpMatrix * ComputeVertexPosition();\n
77 const char* FRAGMENT_SHADER_BUTT_CAP = DALI_COMPOSE_SHADER(
78 varying mediump vec2 vPosition;\n
79 uniform lowp vec4 uColor;\n
80 uniform lowp vec3 mixColor;\n
81 uniform mediump float thickness;\n
82 uniform mediump float radius;\n
83 uniform mediump float startAngle;\n
84 uniform mediump float sweepAngle;\n
86 const mediump float M_PI_OVER_2 = 1.57079632679;\n
87 const mediump float M_PI = 3.14159265359;\n
88 const mediump float M_PI_2 = 6.28318530718;\n
90 mediump float GetOpacity()\n
92 mediump float start = radians( mod( startAngle, 360.0 ) );\n
93 mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n
94 mediump float dist = length( vPosition );\n
95 if( angle <= radians( sweepAngle ) )\n
97 return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n
99 mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n
100 mediump vec2 q0 = vec2( dist * cos( start - M_PI_OVER_2 ), dist * sin( start - M_PI_OVER_2 ) );\n
101 mediump vec2 q1 = vec2( dist * cos( end - M_PI_OVER_2 ), dist * sin( end - M_PI_OVER_2 ) );\n
102 mediump float opacity = 1.0 - smoothstep( 0.0, 2.0, min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n
103 opacity *= step( 0.0, thickness / 2.0 - abs( dist - radius ) );\n
108 gl_FragColor = vec4( mixColor, 1.0 ) * uColor;\n
109 gl_FragColor.a *= GetOpacity();\n
113 const char* FRAGMENT_SHADER_ROUND_CAP = DALI_COMPOSE_SHADER(
114 varying mediump vec2 vPosition;\n
115 uniform lowp vec4 uColor;\n
116 uniform lowp vec3 mixColor;\n
117 uniform mediump float thickness;\n
118 uniform mediump float radius;\n
119 uniform mediump float startAngle;\n
120 uniform mediump float sweepAngle;\n
122 const mediump float M_PI_OVER_2 = 1.57079632679;\n
123 const mediump float M_PI_2 = 6.28318530718;\n
125 mediump float GetOpacity()\n
127 mediump float start = radians( mod( startAngle, 360.0 ) );\n
128 mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n
129 mediump float dist = length( vPosition );\n
130 if( angle <= radians( sweepAngle ) )\n
132 return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n
134 mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n
135 mediump vec2 q0 = vec2( radius * cos( start - M_PI_OVER_2 ), radius * sin( start - M_PI_OVER_2 ) );\n
136 mediump vec2 q1 = vec2( radius * cos( end - M_PI_OVER_2 ), radius * sin( end - M_PI_OVER_2 ) );\n
137 return smoothstep( -1.0, 1.0, thickness / 2.0 - min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n
141 gl_FragColor = vec4( mixColor, 1.0 ) * uColor;\n
142 gl_FragColor.a *= GetOpacity();\n
148 ArcVisualPtr ArcVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
150 ArcVisualPtr arcVisualPtr( new ArcVisual( factoryCache ) );
151 arcVisualPtr->SetProperties( properties );
155 ArcVisual::ArcVisual( VisualFactoryCache& factoryCache )
156 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
160 mSweepAngle( 360.0f ),
161 mRadiusIndex( Property::INVALID_INDEX ),
162 mCapType( DevelArcVisual::Cap::BUTT )
166 ArcVisual::~ArcVisual()
170 void ArcVisual::DoSetProperties( const Property::Map& propertyMap )
172 Property::Value* thicknessValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME );
175 if( !thicknessValue->Get( mThickness ) )
177 DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType() );
181 Property::Value* startAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME );
182 if( startAngleValue )
184 if( !startAngleValue->Get( mStartAngle ) )
186 DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType() );
190 Property::Value* sweepAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME );
191 if( sweepAngleValue )
193 if( !sweepAngleValue->Get( mSweepAngle ) )
195 DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType() );
199 Property::Value* capValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::CAP, CAP_NAME );
203 Scripting::GetEnumerationProperty( *capValue, CAP_TABLE, CAP_TABLE_COUNT, capType );
204 mCapType = Toolkit::DevelArcVisual::Cap::Type( capType );
208 void ArcVisual::DoSetOnStage( Actor& actor )
210 InitializeRenderer();
212 actor.AddRenderer( mImpl->mRenderer );
214 // Arc Visual generated and ready to display
215 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
218 void ArcVisual::DoCreatePropertyMap( Property::Map& map ) const
221 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ARC );
222 map.Insert( Toolkit::DevelArcVisual::Property::THICKNESS, mThickness );
223 map.Insert( Toolkit::DevelArcVisual::Property::START_ANGLE, mStartAngle );
224 map.Insert( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, mSweepAngle );
225 map.Insert( Toolkit::DevelArcVisual::Property::CAP, mCapType );
228 void ArcVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
233 void ArcVisual::OnSetTransform()
235 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
236 mRadius = ( std::min( visualSize.width, visualSize.height ) - mThickness ) / 2.0f;
238 if( mImpl->mRenderer )
240 mImpl->mRenderer.SetProperty( mRadiusIndex, mRadius );
244 void ArcVisual::InitializeRenderer()
246 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
249 if( mCapType == DevelArcVisual::Cap::BUTT )
251 shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER );
254 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_BUTT_CAP );
255 mFactoryCache.SaveShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER, shader );
260 shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER );
263 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER_ROUND_CAP );
264 mFactoryCache.SaveShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER, shader );
268 mImpl->mRenderer = Renderer::New( geometry, shader );
270 mImpl->mRenderer.RegisterProperty( THICKNESS_NAME, mThickness );
271 mImpl->mRenderer.RegisterProperty( START_ANGLE_NAME, mStartAngle );
272 mImpl->mRenderer.RegisterProperty( SWEEP_ANGLE_NAME, mSweepAngle );
273 mImpl->mRenderer.RegisterProperty( CAP_NAME, 0.0f );
275 mRadiusIndex = mImpl->mRenderer.RegisterProperty( RADIUS_NAME, mRadius );
277 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
279 // Register transform properties
280 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
283 } // namespace Internal
285 } // namespace Toolkit