Merge "Add focus indicator only when attaching physicalkeyboard" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / border / border-visual.cpp
1 /*
2  * Copyright (c) 2016 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 "border-visual.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/devel-api/object/handle-devel.h>
24
25 // INTERNAL INCLUDES
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>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 namespace
43 {
44 const char * const COLOR_NAME("borderColor");
45 const char * const SIZE_NAME("borderSize");
46 const char * const ANTI_ALIASING("antiAliasing");
47
48 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
49 const char * const DRIFT_ATTRIBUTE_NAME("aDrift");
50 const char * const INDEX_NAME("indices");
51
52
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
59   \n
60
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
67
68   vec2 ComputeVertexPosition()\n
69   {\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
73   }\n
74
75   void main()\n
76   {\n
77     vec2 position = ComputeVertexPosition() + aDrift*borderSize;\n
78     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
79   }\n
80 );
81
82 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
83   uniform lowp vec4 uColor;\n
84   uniform lowp vec4 borderColor;\n
85   \n
86   void main()\n
87   {\n
88     gl_FragColor = borderColor*uColor;\n
89   }\n
90 );
91
92 const char* VERTEX_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
93   attribute mediump vec2 aPosition;\n
94   attribute mediump vec2 aDrift;\n
95   uniform mediump mat4 uMvpMatrix;\n
96   uniform mediump vec3 uSize;\n
97   uniform mediump float borderSize;\n
98   varying mediump float vAlpha;\n
99   \n
100   void main()\n
101   {\n
102     vec2 position = aPosition*(uSize.xy+vec2(0.75)) + aDrift*(borderSize+1.5);\n
103     gl_Position = uMvpMatrix * vec4(position, 0.0, 1.0);\n
104     vAlpha = min( abs(aDrift.x), abs(aDrift.y) )*(borderSize+1.5);
105   }\n
106 );
107
108 const char* FRAGMENT_SHADER_ANTI_ALIASING = DALI_COMPOSE_SHADER(
109   uniform lowp vec4 uColor;\n
110   uniform lowp vec4 borderColor;\n
111   uniform mediump float borderSize;\n
112   varying mediump float vAlpha;\n
113   \n
114   void main()\n
115   {\n
116     gl_FragColor = borderColor*uColor;\n
117     gl_FragColor.a *= smoothstep(0.0, 1.5, vAlpha)*smoothstep( borderSize+1.5, borderSize, vAlpha );\n
118   }\n
119 );
120 }
121
122 BorderVisualPtr BorderVisual::New( VisualFactoryCache& factoryCache )
123 {
124   return new BorderVisual( factoryCache );
125 }
126
127 BorderVisual::BorderVisual( VisualFactoryCache& factoryCache )
128 : Visual::Base( factoryCache ),
129   mBorderColor( Color::TRANSPARENT ),
130   mBorderSize( 0.f ),
131   mBorderColorIndex( Property::INVALID_INDEX ),
132   mBorderSizeIndex( Property::INVALID_INDEX ),
133   mAntiAliasing( false )
134 {
135 }
136
137 BorderVisual::~BorderVisual()
138 {
139 }
140
141 void BorderVisual::DoSetProperties( const Property::Map& propertyMap )
142 {
143   Property::Value* color = propertyMap.Find( Toolkit::BorderVisual::Property::COLOR, COLOR_NAME );
144   if( !( color && color->Get(mBorderColor) ) )
145   {
146     DALI_LOG_ERROR( "Fail to provide a border color to the BorderVisual object\n" );
147   }
148
149   Property::Value* size = propertyMap.Find( Toolkit::BorderVisual::Property::SIZE, SIZE_NAME );
150   if( !( size && size->Get(mBorderSize) ) )
151   {
152     DALI_LOG_ERROR( "Fail to provide a border size to the BorderVisual object\n" );
153   }
154
155   Property::Value* antiAliasing = propertyMap.Find( Toolkit::BorderVisual::Property::ANTI_ALIASING, ANTI_ALIASING );
156   if( antiAliasing )
157   {
158     antiAliasing->Get( mAntiAliasing );
159   }
160 }
161
162 void BorderVisual::DoSetOnStage( Actor& actor )
163 {
164   InitializeRenderer();
165
166   mBorderColorIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::COLOR, COLOR_NAME, mBorderColor );
167   if( mBorderColor.a < 1.f || mAntiAliasing)
168   {
169     mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
170   }
171   mBorderSizeIndex = DevelHandle::RegisterProperty( mImpl->mRenderer, Toolkit::BorderVisual::Property::SIZE, SIZE_NAME, mBorderSize );
172
173   actor.AddRenderer( mImpl->mRenderer );
174 }
175
176 void BorderVisual::DoCreatePropertyMap( Property::Map& map ) const
177 {
178   map.Clear();
179   map.Insert( DevelVisual::Property::TYPE, Toolkit::Visual::BORDER );
180   map.Insert( Toolkit::BorderVisual::Property::COLOR, mBorderColor );
181   map.Insert( Toolkit::BorderVisual::Property::SIZE, mBorderSize );
182   map.Insert( Toolkit::BorderVisual::Property::ANTI_ALIASING, mAntiAliasing );
183 }
184
185 void BorderVisual::OnSetTransform()
186 {
187   if( mImpl->mRenderer )
188   {
189     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
190   }
191 }
192
193 void BorderVisual::InitializeRenderer()
194 {
195   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::BORDER_GEOMETRY );
196   if( !geometry )
197   {
198     geometry =  CreateBorderGeometry();
199     mFactoryCache.SaveGeometry( VisualFactoryCache::BORDER_GEOMETRY, geometry );
200   }
201
202
203   Shader shader = GetBorderShader();
204   mImpl->mRenderer = Renderer::New( geometry, shader  );
205
206   //Register transform properties
207   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
208
209 }
210
211 void BorderVisual::SetBorderColor(const Vector4& color)
212 {
213   mBorderColor = color;
214
215   if( mImpl->mRenderer )
216   {
217     (mImpl->mRenderer).SetProperty( mBorderColorIndex, color );
218     if( color.a < 1.f )
219     {
220       mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
221     }
222   }
223 }
224
225 void BorderVisual::SetBorderSize( float size )
226 {
227   mBorderSize = size;
228
229   if( mImpl->mRenderer )
230   {
231     (mImpl->mRenderer).SetProperty( mBorderSizeIndex, size );
232   }
233 }
234
235 void BorderVisual::RequireAntiAliasing( bool antiAliasing )
236 {
237   if( mAntiAliasing != antiAliasing )
238   {
239     mAntiAliasing = antiAliasing;
240     if( mImpl->mRenderer )
241     {
242       Shader borderShader( GetBorderShader() );
243       mImpl->mRenderer.SetShader( borderShader );
244       if( mAntiAliasing )
245       {
246         mImpl->mRenderer.SetProperty( Renderer::Property::BLEND_MODE, BlendMode::ON );
247       }
248     }
249   }
250 }
251
252 Shader BorderVisual::GetBorderShader()
253 {
254   Shader shader;
255   if( mAntiAliasing )
256   {
257     shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING );
258     if( !shader )
259     {
260       shader = Shader::New( VERTEX_SHADER_ANTI_ALIASING, FRAGMENT_SHADER_ANTI_ALIASING );
261       mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER_ANTI_ALIASING, shader );
262     }
263   }
264   else
265   {
266     shader = mFactoryCache.GetShader( VisualFactoryCache::BORDER_SHADER );
267     if( !shader )
268     {
269       shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
270       mFactoryCache.SaveShader( VisualFactoryCache::BORDER_SHADER, shader );
271     }
272   }
273
274   return shader;
275 }
276
277 /**
278  * Vertices and triangles of the border geometry:
279  *
280  * vertex position = aPosition*uSize.xy + aDrift*uBorderSize;
281  *
282  * 0--1--2--3
283  * |\ | /| /|
284  * | \|/ |/ |
285  * 4--5--6--7
286  * |\ |  |\ |
287  * | \|  | \|
288  * 8--9--10-11
289  * | /| /|\ |
290  * |/ |/ | \|
291  * 12-13-14-15
292  */
293 Geometry BorderVisual::CreateBorderGeometry()
294 {
295   const float halfWidth = 0.5f;
296   const float halfHeight = 0.5f;
297   struct BorderVertex { Vector2 position; Vector2 drift;};
298   BorderVertex borderVertexData[16] =
299   {
300       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 0.f) },
301       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 0.f) },
302       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 0.f) },
303       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 0.f) },
304
305       { Vector2(-halfWidth, -halfHeight), Vector2(0.f, 1.f) },
306       { Vector2(-halfWidth, -halfHeight), Vector2(1.f, 1.f) },
307       { Vector2(halfWidth, -halfHeight),  Vector2(-1.f, 1.f) },
308       { Vector2(halfWidth, -halfHeight),  Vector2(0.f, 1.f) },
309
310       { Vector2(-halfWidth, halfHeight), Vector2(0.f, -1.f) },
311       { Vector2(-halfWidth, halfHeight), Vector2(1.f, -1.f) },
312       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, -1.f) },
313       { Vector2(halfWidth, halfHeight),  Vector2(0.f, -1.f) },
314
315       { Vector2(-halfWidth, halfHeight), Vector2(0.f, 0.f) },
316       { Vector2(-halfWidth, halfHeight), Vector2(1.f, 0.f) },
317       { Vector2(halfWidth, halfHeight),  Vector2(-1.f, 0.f) },
318       { Vector2(halfWidth, halfHeight),  Vector2(0.f, 0.f) },
319   };
320
321   Property::Map borderVertexFormat;
322   borderVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
323   borderVertexFormat[DRIFT_ATTRIBUTE_NAME] = Property::VECTOR2;
324   PropertyBuffer borderVertices = PropertyBuffer::New( borderVertexFormat );
325   borderVertices.SetData( borderVertexData, 16 );
326
327   // Create indices
328   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};
329
330   // Create the geometry object
331   Geometry geometry = Geometry::New();
332   geometry.AddVertexBuffer( borderVertices );
333   geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
334   geometry.SetType( Geometry::TRIANGLE_STRIP );
335
336   return geometry;
337 }
338
339 } // namespace Internal
340
341 } // namespace Toolkit
342
343 } // namespace Dali