2 * Copyright (c) 2016 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 "border-visual.h"
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/object/handle-devel.h>
26 #include <dali-toolkit/public-api/visuals/border-visual-properties.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
29 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
30 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
31 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
44 const char * const COLOR_NAME("borderColor");
45 const char * const SIZE_NAME("borderSize");
46 const char * const ANTI_ALIASING("antiAliasing");
48 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
49 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
50 const char * const INDEX_NAME("indices");
53 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
54 attribute mediump vec2 aPosition;\n
55 attribute mediump vec2 aDrift;\n
56 uniform mediump mat4 uMvpMatrix;\n
57 uniform mediump vec3 uSize;\n
58 uniform mediump float borderSize;\n
61 //Visual size and offset
62 uniform mediump vec2 offset;\n
63 uniform mediump vec2 size;\n
64 uniform mediump vec4 offsetSizeMode;\n
65 uniform mediump vec2 origin;\n
66 uniform mediump vec2 anchorPoint;\n
68 vec2 ComputeVertexPosition()\n
70 vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
71 vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
72 return (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy;\n
77 vec2 position = ComputeVertexPosition() + aDrift*borderSize;\n
78 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
82 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
83 uniform lowp vec4 uColor;\n
84 uniform lowp vec4 borderColor;\n
85 uniform lowp vec4 mixColor;\n
89 gl_FragColor = mixColor*borderColor*uColor;\n
93 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
94 attribute mediump vec2 aPosition;\n
95 attribute mediump vec2 aDrift;\n
96 uniform mediump mat4 uMvpMatrix;\n
97 uniform mediump vec3 uSize;\n
98 uniform mediump float borderSize;\n
99 varying mediump float vAlpha;\n
103 vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
104 gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
105 vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
109 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
110 uniform lowp vec4 uColor;\n
111 uniform lowp vec4 borderColor;\n
112 uniform lowp vec4 mixColor;\n
113 uniform mediump float borderSize;\n
114 varying mediump float vAlpha;\n
118 gl_FragColor = mixColor*borderColor*uColor;\n
119 gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
124 BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
126 BorderVisualPtr borderVisualPtr( new BorderVisual( factoryCache ) );
127 borderVisualPtr->SetProperties( properties );
128 return borderVisualPtr;
131 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
132 : Visual::Base( factoryCache ),
133 mBorderColor( Color::TRANSPARENT ),
135 mBorderColorIndex( Property::INVALID_INDEX ),
136 mBorderSizeIndex( Property::INVALID_INDEX ),
137 mAntiAliasing( false )
141 BorderVisual::~BorderVisual()
145 void BorderVisual::DoSetProperties( const Property::Map& propertyMap )
147 for( Property::Map::SizeType iter = 0; iter < propertyMap.Count(); ++iter )
149 KeyValuePair keyValue = propertyMap.GetKeyValue( iter );
150 if( keyValue.first.type == Property::Key::INDEX )
152 DoSetProperty( keyValue.first.indexKey, keyValue.second );
156 if( keyValue.first == COLOR_NAME )
158 DoSetProperty( Toolkit::BorderVisual::Property::COLOR, keyValue.second );
160 else if( keyValue.first == SIZE_NAME )
162 DoSetProperty( Toolkit::BorderVisual::Property::SIZE, keyValue.second );
164 else if( keyValue.first == ANTI_ALIASING )
166 DoSetProperty( Toolkit::BorderVisual::Property::ANTI_ALIASING, keyValue.second );
172 void BorderVisual::DoSetProperty( Dali::Property::Index index,
173 const Dali::Property::Value& value )
177 case Toolkit::BorderVisual::Property::COLOR:
179 if( !value.Get( mBorderColor ) )
181 DALI_LOG_ERROR("BorderVisual: borderColor property has incorrect type\n");
185 case Toolkit::BorderVisual::Property::SIZE:
187 if( !value.Get( mBorderSize ) )
189 DALI_LOG_ERROR("BorderVisual: borderSize property has incorrect type\n");
193 case Toolkit::BorderVisual::Property::ANTI_ALIASING:
195 if( !value.Get( mAntiAliasing ) )
197 DALI_LOG_ERROR("BorderVisual: antiAliasing property has incorrect type\n");
204 void BorderVisual::DoSetOnStage( Actor& actor )
206 InitializeRenderer();
208 mBorderColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
209 if( mBorderColor.a < 1.f || mAntiAliasing)
211 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
213 mBorderSizeIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
215 actor.AddRenderer( mImpl->mRenderer );
218 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
221 map.Insert( Toolkit::DevelVisual::Property::TYPE, Toolkit::Visual::BORDER );
222 map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
223 map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
224 map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
227 void BorderVisual::OnSetTransform()
229 if( mImpl->mRenderer )
231 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
235 void BorderVisual::InitializeRenderer()
237 Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
240 geometry = CreateBorderGeometry();
241 mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
244 Shader shader = GetBorderShader();
245 mImpl->mRenderer = Renderer::New( geometry, shader );
247 //Register transform properties
248 mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
251 void BorderVisual::SetBorderColor(const Vector4& color)
253 mBorderColor = color;
255 if( mImpl->mRenderer )
257 (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
260 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
265 void BorderVisual::SetBorderSize( float size )
269 if( mImpl->mRenderer )
271 (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
275 void BorderVisual::RequireAntiAliasing( bool antiAliasing )
277 if( mAntiAliasing != antiAliasing )
279 mAntiAliasing = antiAliasing;
280 if( mImpl->mRenderer )
282 Shader borderShader( GetBorderShader() );
283 mImpl->mRenderer.SetShader( borderShader );
286 mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
292 Shader BorderVisual::GetBorderShader()
297 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
300 shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
301 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
306 shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
309 shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
310 mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
318 * Vertices and triangles of the border geometry:
320 * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
333 Geometry BorderVisual::CreateBorderGeometry()
335 const float halfWidth = 0.5f;
336 const float halfHeight = 0.5f;
337 struct BorderVertex { Vector2 position; Vector2 drift;};
338 BorderVertex borderVertexData[16] =
340 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
341 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
342 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 0.f) },
343 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 0.f) },
345 { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
346 { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
347 { Vector2(halfWidth, -halfHeight), Vector2(-1.f, 1.f) },
348 { Vector2(halfWidth, -halfHeight), Vector2(0.f, 1.f) },
350 { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
351 { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
352 { Vector2(halfWidth, halfHeight), Vector2(-1.f, -1.f) },
353 { Vector2(halfWidth, halfHeight), Vector2(0.f, -1.f) },
355 { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
356 { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
357 { Vector2(halfWidth, halfHeight), Vector2(-1.f, 0.f) },
358 { Vector2(halfWidth, halfHeight), Vector2(0.f, 0.f) },
361 Property::Map borderVertexFormat;
362 borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
363 borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
364 PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
365 borderVertices.SetData( borderVertexData, 16 );
368 unsigned short indexData[24] = { 1,5,2,6,3,7,7,6,11,10,15,14,14,10,13,9,12,8,8,9,4,5,0,1};
370 // Create the geometry object
371 Geometry geometry = Geometry::New();
372 geometry.AddVertexBuffer( borderVertices );
373 geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
374 geometry.SetType( Geometry::TRIANGLE_STRIP );
379 } // namespace Internal
381 } // namespace Toolkit