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/devel-api/visuals/arc-visual-actions-devel.h>
27 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
28 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
29 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
30 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
45 DALI_ENUM_TO_STRING_TABLE_BEGIN( CAP )
46 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, BUTT )
47 DALI_ENUM_TO_STRING_WITH_SCOPE( DevelArcVisual::Cap, ROUND )
48 DALI_ENUM_TO_STRING_TABLE_END( CAP )
50 const char* VERTEX_SHADER =
51 "INPUT mediump vec2 aPosition;\n"
52 "OUTPUT mediump vec2 vPosition;\n"
54 "uniform highp mat4 uMvpMatrix;\n"
55 "uniform highp vec3 uSize;\n"
57 "//Visual size and offset\n"
58 "uniform mediump vec2 offset;\n"
59 "uniform highp vec2 size;\n"
60 "uniform mediump vec4 offsetSizeMode;\n"
61 "uniform mediump vec2 origin;\n"
62 "uniform mediump vec2 anchorPoint;\n"
64 "vec4 ComputeVertexPosition()\n"
66 " vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n"
67 " vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n"
68 " vPosition = aPosition* visualSize;\n"
69 " return vec4( vPosition + anchorPoint*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n"
74 " gl_Position = uMvpMatrix * ComputeVertexPosition();\n"
77 const char* FRAGMENT_SHADER_BUTT_CAP =
78 "INPUT mediump vec2 vPosition;\n"
80 "uniform lowp vec4 uColor;\n"
81 "uniform lowp vec3 mixColor;\n"
82 "uniform mediump float thickness;\n"
83 "uniform mediump float radius;\n"
84 "uniform mediump float startAngle;\n"
85 "uniform mediump float sweepAngle;\n"
87 "const mediump float M_PI_OVER_2 = 1.57079632679;\n"
88 "const mediump float M_PI = 3.14159265359;\n"
89 "const mediump float M_PI_2 = 6.28318530718;\n"
91 "mediump float GetOpacity()\n"
93 " mediump float start = radians( mod( startAngle, 360.0 ) );\n"
94 " mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n"
95 " mediump float dist = length( vPosition );\n"
96 " if( angle <= radians( sweepAngle ) )\n"
98 " return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n"
100 " mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n"
101 " mediump vec2 q0 = vec2( dist * cos( start - M_PI_OVER_2 ), dist * sin( start - M_PI_OVER_2 ) );\n"
102 " mediump vec2 q1 = vec2( dist * cos( end - M_PI_OVER_2 ), dist * sin( end - M_PI_OVER_2 ) );\n"
103 " mediump float opacity = 1.0 - smoothstep( 0.0, 2.0, min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n"
104 " opacity *= step( 0.0, thickness / 2.0 - abs( dist - radius ) );\n"
110 " OUT_COLOR = vec4( mixColor, 1.0 ) * uColor;\n"
111 " OUT_COLOR.a *= GetOpacity();\n"
114 const char* FRAGMENT_SHADER_ROUND_CAP =
115 "INPUT mediump vec2 vPosition;\n"
117 "uniform lowp vec4 uColor;\n"
118 "uniform lowp vec3 mixColor;\n"
119 "uniform mediump float thickness;\n"
120 "uniform mediump float radius;\n"
121 "uniform mediump float startAngle;\n"
122 "uniform mediump float sweepAngle;\n"
124 "const mediump float M_PI_OVER_2 = 1.57079632679;\n"
125 "const mediump float M_PI_2 = 6.28318530718;\n"
127 "mediump float GetOpacity()\n"
129 " mediump float start = radians( mod( startAngle, 360.0 ) );\n"
130 " mediump float angle = mod( atan( vPosition.y, vPosition.x ) + M_PI_OVER_2 - start, M_PI_2 );\n"
131 " mediump float dist = length( vPosition );\n"
132 " if( angle <= radians( sweepAngle ) )\n"
134 " return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n"
136 " mediump float end = radians( mod( startAngle + sweepAngle, 360.0 ) );\n"
137 " mediump vec2 q0 = vec2( radius * cos( start - M_PI_OVER_2 ), radius * sin( start - M_PI_OVER_2 ) );\n"
138 " mediump vec2 q1 = vec2( radius * cos( end - M_PI_OVER_2 ), radius * sin( end - M_PI_OVER_2 ) );\n"
139 " return smoothstep( -1.0, 1.0, thickness / 2.0 - min( length( vPosition - q0 ), length( vPosition - q1 ) ) );\n"
144 " OUT_COLOR = vec4( mixColor, 1.0 ) * uColor;\n"
145 " OUT_COLOR.a *= GetOpacity();\n"
150 ArcVisualPtr ArcVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
152 ArcVisualPtr arcVisualPtr( new ArcVisual( factoryCache ) );
153 arcVisualPtr->SetProperties( properties );
157 ArcVisual::ArcVisual( VisualFactoryCache& factoryCache )
158 : Visual::Base( factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>( Toolkit::DevelVisual::ARC ) ),
162 mSweepAngle( 360.0f ),
163 mRadiusIndex( Property::INVALID_INDEX ),
164 mThicknessIndex( Property::INVALID_INDEX ),
165 mStartAngleIndex( Property::INVALID_INDEX ),
166 mSweepAngleIndex( Property::INVALID_INDEX ),
167 mCapType( DevelArcVisual::Cap::BUTT )
171 ArcVisual::~ArcVisual()
175 void ArcVisual::DoSetProperties( const Property::Map& propertyMap )
177 Property::Value* thicknessValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME );
180 if( !thicknessValue->Get( mThickness ) )
182 DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType() );
186 if( mImpl->mRenderer )
188 mImpl->mRenderer.SetProperty( mThicknessIndex, mThickness );
190 // Need to calculate radius again
196 Property::Value* startAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME );
197 if( startAngleValue )
199 if( !startAngleValue->Get( mStartAngle ) )
201 DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType() );
205 if( mImpl->mRenderer )
207 mImpl->mRenderer.SetProperty( mStartAngleIndex, mStartAngle );
212 Property::Value* sweepAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME );
213 if( sweepAngleValue )
215 if( !sweepAngleValue->Get( mSweepAngle ) )
217 DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType() );
221 if( mImpl->mRenderer )
223 mImpl->mRenderer.SetProperty( mSweepAngleIndex, mSweepAngle );
228 Property::Value* capValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::CAP, CAP_NAME );
232 Scripting::GetEnumerationProperty( *capValue, CAP_TABLE, CAP_TABLE_COUNT, capType );
233 mCapType = Toolkit::DevelArcVisual::Cap::Type( capType );
237 void ArcVisual::DoSetOnScene( Actor& actor )
239 InitializeRenderer();
241 actor.AddRenderer( mImpl->mRenderer );
243 // Arc Visual generated and ready to display
244 ResourceReady( Toolkit::Visual::ResourceStatus::READY );
247 void ArcVisual::DoSetOffScene(Actor& actor)
251 // Update values from Renderer
252 mThickness = mImpl->mRenderer.GetProperty<float>(mThicknessIndex);
253 mStartAngle = mImpl->mRenderer.GetProperty<float>(mStartAngleIndex);
254 mSweepAngle = mImpl->mRenderer.GetProperty<float>(mSweepAngleIndex);
257 actor.RemoveRenderer(mImpl->mRenderer);
258 mImpl->mRenderer.Reset();
260 mThicknessIndex = Property::INVALID_INDEX;
261 mStartAngleIndex = Property::INVALID_INDEX;
262 mSweepAngleIndex = Property::INVALID_INDEX;
265 void ArcVisual::DoCreatePropertyMap( Property::Map& map ) const
267 float thickness, startAngle, sweepAngle;
270 // Update values from Renderer
271 thickness = mImpl->mRenderer.GetProperty<float>(mThicknessIndex);
272 startAngle = mImpl->mRenderer.GetProperty<float>(mStartAngleIndex);
273 sweepAngle = mImpl->mRenderer.GetProperty<float>(mSweepAngleIndex);
277 thickness = mThickness;
278 startAngle = mStartAngle;
279 sweepAngle = mSweepAngle;
283 map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::DevelVisual::ARC );
284 map.Insert(Toolkit::DevelArcVisual::Property::THICKNESS, thickness);
285 map.Insert(Toolkit::DevelArcVisual::Property::START_ANGLE, startAngle);
286 map.Insert(Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, sweepAngle);
287 map.Insert( Toolkit::DevelArcVisual::Property::CAP, mCapType );
290 void ArcVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
295 void ArcVisual::OnSetTransform()
297 Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
298 mRadius = ( std::min( visualSize.width, visualSize.height ) - mThickness ) / 2.0f;
300 if( mImpl->mRenderer )
302 mImpl->mRenderer.SetProperty( mRadiusIndex, mRadius );
306 void ArcVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
308 // Check if action is valid for this visual type and perform action if possible
311 case DevelArcVisual::Action::UPDATE_PROPERTY:
313 const Property::Map* map = attributes.GetMap();
316 DoSetProperties( *map );
323 void ArcVisual::InitializeRenderer()
325 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
328 if( mCapType == DevelArcVisual::Cap::BUTT )
330 shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER );
333 shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_BUTT_CAP );
334 mFactoryCache.SaveShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER, shader );
339 shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER );
342 shader = Shader::New( Dali::Shader::GetVertexShaderPrefix() + VERTEX_SHADER, Dali::Shader::GetFragmentShaderPrefix() + FRAGMENT_SHADER_ROUND_CAP );
343 mFactoryCache.SaveShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER, shader );
347 mImpl->mRenderer = Renderer::New( geometry, shader );
349 mThicknessIndex = mImpl->mRenderer.RegisterProperty(DevelArcVisual::Property::THICKNESS, THICKNESS_NAME, mThickness);
350 mStartAngleIndex = mImpl->mRenderer.RegisterProperty(DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME, mStartAngle);
351 mSweepAngleIndex = mImpl->mRenderer.RegisterProperty(DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME, mSweepAngle);
353 mRadiusIndex = mImpl->mRenderer.RegisterProperty( RADIUS_NAME, mRadius );
355 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
357 // Register transform properties
358 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
361 } // namespace Internal
363 } // namespace Toolkit