d694235dfc6160bda9e52620f9e92e8c3e5e258c
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / arc / arc-visual.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/visuals/arc/arc-visual.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 //INTERNAL INCLUDES
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>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43
44 // cap
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 )
49
50 const char* VERTEX_SHADER =
51   "INPUT mediump vec2 aPosition;\n"
52   "OUTPUT mediump vec2 vPosition;\n"
53
54   "uniform highp mat4 uMvpMatrix;\n"
55   "uniform highp vec3 uSize;\n"
56
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"
63
64   "vec4 ComputeVertexPosition()\n"
65   "{\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"
70   "}\n"
71
72   "void main()\n"
73   "{\n"
74   "  gl_Position = uMvpMatrix * ComputeVertexPosition();\n"
75   "}\n";
76
77 const char* FRAGMENT_SHADER_BUTT_CAP =
78   "INPUT mediump vec2 vPosition;\n"
79
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"
86
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"
90
91   "mediump float GetOpacity()\n"
92   "{\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"
97   "  {\n"
98   "    return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n"
99   "  }\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"
105   "  return opacity;\n"
106   "}\n"
107
108   "void main()\n"
109   "{\n"
110   "  OUT_COLOR = vec4( mixColor, 1.0 ) * uColor;\n"
111   "  OUT_COLOR.a *= GetOpacity();\n"
112   "}\n";
113
114 const char* FRAGMENT_SHADER_ROUND_CAP =
115   "INPUT mediump vec2 vPosition;\n"
116
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"
123
124   "const mediump float M_PI_OVER_2 = 1.57079632679;\n"
125   "const mediump float M_PI_2 = 6.28318530718;\n"
126
127   "mediump float GetOpacity()\n"
128   "{\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"
133   "  {\n"
134   "    return smoothstep( -1.0, 1.0, thickness / 2.0 - ( abs( dist - radius ) ) );\n"
135   "  }\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"
140   "}\n"
141
142   "void main()\n"
143   "{\n"
144   "  OUT_COLOR = vec4( mixColor, 1.0 ) * uColor;\n"
145   "  OUT_COLOR.a *= GetOpacity();\n"
146   "}\n";
147
148 }
149
150 ArcVisualPtr ArcVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
151 {
152   ArcVisualPtr arcVisualPtr( new ArcVisual( factoryCache ) );
153   arcVisualPtr->SetProperties( properties );
154   return arcVisualPtr;
155 }
156
157 ArcVisual::ArcVisual( VisualFactoryCache& factoryCache )
158 : Visual::Base( factoryCache, Visual::FittingMode::FILL, static_cast<Toolkit::Visual::Type>( Toolkit::DevelVisual::ARC ) ),
159   mThickness( 0.0f ),
160   mRadius( 0.0f ),
161   mStartAngle( 0.0f ),
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 )
168 {
169 }
170
171 ArcVisual::~ArcVisual()
172 {
173 }
174
175 void ArcVisual::DoSetProperties( const Property::Map& propertyMap )
176 {
177   Property::Value* thicknessValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::THICKNESS, THICKNESS_NAME );
178   if( thicknessValue )
179   {
180     if( !thicknessValue->Get( mThickness ) )
181     {
182       DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: THICKNESS property has incorrect type: %d\n", thicknessValue->GetType() );
183     }
184     else
185     {
186       if( mImpl->mRenderer )
187       {
188         mImpl->mRenderer.SetProperty( mThicknessIndex, mThickness );
189
190         // Need to calculate radius again
191         OnSetTransform();
192       }
193     }
194   }
195
196   Property::Value* startAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::START_ANGLE, START_ANGLE_NAME );
197   if( startAngleValue )
198   {
199     if( !startAngleValue->Get( mStartAngle ) )
200     {
201       DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: START_ANGLE property has incorrect type: %d\n", startAngleValue->GetType() );
202     }
203     else
204     {
205       if( mImpl->mRenderer )
206       {
207         mImpl->mRenderer.SetProperty( mStartAngleIndex, mStartAngle );
208       }
209     }
210   }
211
212   Property::Value* sweepAngleValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::SWEEP_ANGLE, SWEEP_ANGLE_NAME );
213   if( sweepAngleValue )
214   {
215     if( !sweepAngleValue->Get( mSweepAngle ) )
216     {
217       DALI_LOG_ERROR( "ArcVisual:DoSetProperties:: SWEEP_ANGLE property has incorrect type: %d\n", sweepAngleValue->GetType() );
218     }
219     else
220     {
221       if( mImpl->mRenderer )
222       {
223         mImpl->mRenderer.SetProperty( mSweepAngleIndex, mSweepAngle );
224       }
225     }
226   }
227
228   Property::Value* capValue = propertyMap.Find( Toolkit::DevelArcVisual::Property::CAP, CAP_NAME );
229   if( capValue )
230   {
231     int capType = 0;
232     Scripting::GetEnumerationProperty( *capValue, CAP_TABLE, CAP_TABLE_COUNT, capType );
233     mCapType = Toolkit::DevelArcVisual::Cap::Type( capType );
234   }
235 }
236
237 void ArcVisual::DoSetOnScene( Actor& actor )
238 {
239   InitializeRenderer();
240
241   actor.AddRenderer( mImpl->mRenderer );
242
243   // Arc Visual generated and ready to display
244   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
245 }
246
247 void ArcVisual::DoSetOffScene(Actor& actor)
248 {
249   if(mImpl->mRenderer)
250   {
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);
255   }
256
257   actor.RemoveRenderer(mImpl->mRenderer);
258   mImpl->mRenderer.Reset();
259
260   mThicknessIndex  = Property::INVALID_INDEX;
261   mStartAngleIndex = Property::INVALID_INDEX;
262   mSweepAngleIndex = Property::INVALID_INDEX;
263 }
264
265 void ArcVisual::DoCreatePropertyMap( Property::Map& map ) const
266 {
267   float thickness, startAngle, sweepAngle;
268   if(mImpl->mRenderer)
269   {
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);
274   }
275   else
276   {
277     thickness  = mThickness;
278     startAngle = mStartAngle;
279     sweepAngle = mSweepAngle;
280   }
281
282   map.Clear();
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 );
288 }
289
290 void ArcVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
291 {
292   // Do nothing
293 }
294
295 void ArcVisual::OnSetTransform()
296 {
297   Vector2 visualSize = mImpl->mTransform.GetVisualSize( mImpl->mControlSize );
298   mRadius = ( std::min( visualSize.width, visualSize.height ) - mThickness ) / 2.0f;
299
300   if( mImpl->mRenderer )
301   {
302     mImpl->mRenderer.SetProperty( mRadiusIndex, mRadius );
303   }
304 }
305
306 void ArcVisual::OnDoAction( const Property::Index actionId, const Property::Value& attributes )
307 {
308   // Check if action is valid for this visual type and perform action if possible
309   switch( actionId )
310   {
311     case DevelArcVisual::Action::UPDATE_PROPERTY:
312     {
313       const Property::Map* map = attributes.GetMap();
314       if( map )
315       {
316         DoSetProperties( *map );
317       }
318       break;
319     }
320   }
321 }
322
323 void ArcVisual::InitializeRenderer()
324 {
325   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::QUAD_GEOMETRY );
326
327   Shader shader;
328   if( mCapType == DevelArcVisual::Cap::BUTT )
329   {
330     shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_BUTT_CAP_SHADER );
331     if( !shader )
332     {
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 );
335     }
336   }
337   else
338   {
339     shader = mFactoryCache.GetShader( VisualFactoryCache::ARC_ROUND_CAP_SHADER );
340     if( !shader )
341     {
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 );
344     }
345   }
346
347   mImpl->mRenderer = Renderer::New( geometry, shader );
348
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);
352
353   mRadiusIndex = mImpl->mRenderer.RegisterProperty( RADIUS_NAME, mRadius );
354
355   mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
356
357   // Register transform properties
358   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
359 }
360
361 } // namespace Internal
362
363 } // namespace Toolkit
364
365 } // namespace Dali