Removed Visual::Base::SetProperty and GetProperty
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / wireframe / wireframe-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
19 // CLASS HEADER
20 #include "wireframe-visual.h"
21
22 // INTERNAL INCLUDES
23 #include <dali-toolkit/devel-api/visual-factory/devel-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   mediump 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 \n
72 void main()\n
73 {\n
74   gl_FragColor = uColor;\n
75 }\n
76 );
77
78 }
79
80 WireframeVisualPtr WireframeVisual::New( VisualFactoryCache& factoryCache )
81 {
82   return new WireframeVisual( factoryCache );
83 }
84
85 WireframeVisual::WireframeVisual( VisualFactoryCache& factoryCache )
86 : Visual::Base( factoryCache )
87 {
88 }
89
90 WireframeVisual::~WireframeVisual()
91 {
92 }
93
94 void WireframeVisual::DoSetProperties( const Property::Map& propertyMap )
95 {
96   // no properties supported at the moment
97 }
98
99 void WireframeVisual::DoSetOnStage( Actor& actor )
100 {
101   InitializeRenderer();
102
103   actor.AddRenderer( mImpl->mRenderer );
104 }
105
106 void WireframeVisual::DoCreatePropertyMap( Property::Map& map ) const
107 {
108   map.Clear();
109   map.Insert( Toolkit::VisualProperty::TYPE, Toolkit::Visual::WIREFRAME );
110 }
111
112 void WireframeVisual::InitializeRenderer()
113 {
114   Shader shader = mFactoryCache.GetShader( VisualFactoryCache::WIREFRAME_SHADER );
115   if( !shader )
116   {
117     shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
118     mFactoryCache.SaveShader( VisualFactoryCache::WIREFRAME_SHADER, shader );
119   }
120
121   Geometry geometry = mFactoryCache.GetGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY );
122   if( !geometry )
123   {
124     geometry = CreateQuadWireframeGeometry();
125     mFactoryCache.SaveGeometry( VisualFactoryCache::WIREFRAME_GEOMETRY, geometry );
126   }
127
128   //Create the renderer
129   mImpl->mRenderer = Renderer::New( geometry, shader);
130
131   //Register transform properties
132   mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
133 }
134
135 Geometry WireframeVisual::CreateQuadWireframeGeometry()
136 {
137   const float halfWidth = 0.5f;
138   const float halfHeight = 0.5f;
139   struct QuadVertex { Vector2 position;};
140   QuadVertex quadVertexData[4] =
141   {
142       { Vector2(-halfWidth, -halfHeight) },
143       { Vector2( halfWidth, -halfHeight) },
144       { Vector2( halfWidth,  halfHeight) },
145       { Vector2(-halfWidth,  halfHeight) }
146   };
147
148   Property::Map quadVertexFormat;
149   quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
150   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
151   quadVertices.SetData( quadVertexData, 4 );
152
153   // Create indices
154   unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
155
156   // Create the geometry object
157   Geometry geometry = Geometry::New();
158   geometry.AddVertexBuffer( quadVertices );
159   geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
160   geometry.SetType( Geometry::LINES );
161
162   return geometry;
163 }
164
165 void WireframeVisual::OnSetTransform()
166 {
167   if( mImpl->mRenderer )
168   {
169     //Register transform properties
170     mImpl->mTransform.RegisterUniforms( mImpl->mRenderer, Direction::LEFT_TO_RIGHT );
171   }
172 }
173
174 } // namespace Internal
175
176 } // namespace Toolkit
177
178 } // namespace Dali