[dali_1.4.18] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / wireframe / wireframe-visual.cpp
1 /*
2  * Copyright (c) 2018 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
19 // CLASS HEADER
20 #include "wireframe-visual.h"
21
22 // INTERNAL INCLUDES
23 #include <dali-toolkit/public-api/visuals/visual-properties.h>
24 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
25 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
26 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
27 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
41 const char * const INDEX_NAME("indices");
42
43 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
44 attribute mediump vec2  aPosition;\n
45 uniform   highp   mat4  uMvpMatrix;\n
46 uniform   mediump vec3  uSize;\n
47 \n
48
49 //Visual size and offset
50 uniform mediump vec2 offset;\n
51 uniform mediump vec2 size;\n
52 uniform mediump vec4 offsetSizeMode;\n
53 uniform mediump vec2 origin;\n
54 uniform mediump vec2 anchorPoint;\n
55
56 vec4 ComputeVertexPosition()\n
57 {\n
58   vec2 visualSize = mix(uSize.xy*size, size, offsetSizeMode.zw );\n
59   vec2 visualOffset = mix( offset, offset/uSize.xy, offsetSizeMode.xy);\n
60   return vec4( (aPosition + anchorPoint)*visualSize + (visualOffset + origin)*uSize.xy, 0.0, 1.0 );\n
61 }\n
62
63 void main()\n
64 {\n
65   gl_Position = uMvpMatrix * ComputeVertexPosition();\n
66 }\n
67 );
68
69 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(\n
70   uniform lowp vec4 uColor;\n
71   uniform lowp vec3 mixColor;\n
72 \n
73 void main()\n
74 {\n
75   gl_FragColor = uColor * vec4( mixColor, 1.0 );\n
76 }\n
77 );
78
79 }
80
81 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache, const Property::Map& properties )
82 {
83   Visual::BasePtr emtptyVisual;
84
85   return New(factoryCache, emtptyVisual, properties);
86 }
87
88 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual )
89 {
90   return new WireframeVisual( factoryCache, actualVisual );
91 }
92
93 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual, const Property::Map& properties )
94 {
95   WireframeVisualPtr wireframeVisual( new WireframeVisual( factoryCache, actualVisual ) );
96
97   // Instead of calling SetProperties, looking for the only valid property 'transform'
98   Property::Value* transformValue = properties.Find( Toolkit::Visual::Property::TRANSFORM, TRANSFORM );
99   Property::Map transformMap;
100   if( transformValue && transformValue->Get( transformMap ) )
101   {
102     wireframeVisual->SetTransformAndSize( transformMap, Vector2::ZERO );
103   }
104
105   return wireframeVisual;
106 }
107
108 WireframeVisual::WireframeVisual( VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual )
109 : Visual::Base( factoryCache, Visual::FittingMode::FILL ),
110   mActualVisual( actualVisual )
111 {
112 }
113
114 WireframeVisual::~WireframeVisual()
115 {
116 }
117
118 float WireframeVisual::GetHeightForWidth( float width )
119 {
120   if( mActualVisual )
121   {
122     return mActualVisual->GetHeightForWidth( width );
123   }
124   else
125   {
126     return Visual::Base::GetHeightForWidth( width );
127   }
128 }
129
130 void WireframeVisual::GetNaturalSize( Vector2& naturalSize )
131 {
132   if( mActualVisual )
133   {
134     mActualVisual->GetNaturalSize( naturalSize );
135   }
136   else
137   {
138     Visual::Base::GetNaturalSize( naturalSize );
139   }
140 }
141
142 void WireframeVisual::DoCreatePropertyMap( Property::Map& map ) const
143 {
144   if( mActualVisual )
145   {
146     mActualVisual->CreatePropertyMap( map );
147   }
148   else
149   {
150     map.Clear();
151     map.Insert( Toolkit::Visual::Property::TYPE, Toolkit::Visual::WIREFRAME );
152   }
153 }
154
155 void WireframeVisual::DoCreateInstancePropertyMap( Property::Map& map ) const
156 {
157   // Do nothing
158 }
159
160 void WireframeVisual::DoSetProperties( const Property::Map& propertyMap )
161 {
162   Property::Value* mixValue = propertyMap.Find( Toolkit::Visual::Property::MIX_COLOR, MIX_COLOR );
163   if( mixValue )
164   {
165     Vector4 mixColor;
166     mixValue->Get( mixColor );
167     SetMixColor( mixColor );
168   }
169 }
170
171 void WireframeVisual::DoSetOnStage( Actor& actor )
172 {
173   InitializeRenderer();
174
175   actor.AddRenderer( mImpl->mRenderer );
176
177   // Wireframe generated and ready to display
178   ResourceReady( Toolkit::Visual::ResourceStatus::READY );
179 }
180
181 void WireframeVisual::InitializeRenderer()
182 {
183   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::WIREFRAME_SHADER );
184   if( !shader )
185   {
186     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
187     mFactoryCache.SaveShader( VisualFactoryCache::WIREFRAME_SHADER, shader );
188   }
189
190   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY );
191   if( !geometry )
192   {
193     geometry = CreateQuadWireframeGeometry();
194     mFactoryCache.SaveGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY, geometry );
195   }
196
197   //Create the renderer
198   mImpl->mRenderer = Renderer::New( geometry, shader);
199
200   //Register transform properties
201   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
202 }
203
204 Geometry WireframeVisual::CreateQuadWireframeGeometry()
205 {
206   const float halfWidth = 0.5f;
207   const float halfHeight = 0.5f;
208   struct QuadVertex { Vector2 position;};
209   QuadVertex quadVertexData[4] =
210   {
211       { Vector2(-halfWidth, -halfHeight) },
212       { Vector2( halfWidth, -halfHeight) },
213       { Vector2( halfWidth,  halfHeight) },
214       { Vector2(-halfWidth,  halfHeight) }
215   };
216
217   Property::Map quadVertexFormat;
218   quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
219   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
220   quadVertices.SetData( quadVertexData, 4 );
221
222   // Create indices
223   unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
224
225   // Create the geometry object
226   Geometry geometry = Geometry::New();
227   geometry.AddVertexBuffer( quadVertices );
228   geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
229   geometry.SetType( Geometry::LINES );
230
231   return geometry;
232 }
233
234 void WireframeVisual::OnSetTransform()
235 {
236   if( mImpl->mRenderer )
237   {
238     //Register transform properties
239     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
240   }
241 }
242
243 Visual::Base& WireframeVisual::GetVisualObject()
244 {
245   if( mActualVisual )
246   {
247     return *mActualVisual.Get();
248   }
249
250   return *this;
251 }
252
253 } // namespace Internal
254
255 } // namespace Toolkit
256
257 } // namespace Dali