Adding Visual namespace and Visual becomes Visual::Base
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / debug / debug-visual.cpp
1 /*
2  * Copyright (c) 2015 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 "debug-visual.h"
21
22 //INTERNAL INCLUDES
23 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
24 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
25 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
26 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39 const char * const POSITION_ATTRIBUTE_NAME("aPosition");
40 const char * const INDEX_NAME("indices");
41
42 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
43 attribute mediump vec2  aPosition;\n
44 uniform   mediump mat4  uMvpMatrix;\n
45 uniform   mediump vec3  uSize;\n
46 \n
47 void main()\n
48 {\n
49   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
50   vertexPosition.xyz *= uSize;\n
51   gl_Position = uMvpMatrix * vertexPosition;\n
52 }\n
53 );
54
55 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(\n
56 uniform lowp vec4 uColor;\n
57 \n
58 void main()\n
59 {\n
60   gl_FragColor = uColor;\n
61 }\n
62 );
63
64 }
65
66
67 DebugVisual::DebugVisual( VisualFactoryCache& factoryCache )
68 : Visual::Base( factoryCache )
69 {
70 }
71
72 DebugVisual::~DebugVisual()
73 {}
74
75 void DebugVisual::DoSetOnStage( Actor& actor )
76 {
77   InitializeRenderer();
78 }
79
80 void DebugVisual::DoCreatePropertyMap( Property::Map& map ) const
81 {
82   map.Clear();
83   map.Insert( RENDERER_TYPE, DEBUG_RENDERER );
84 }
85
86 void DebugVisual::InitializeRenderer()
87 {
88   mImpl->mRenderer = mFactoryCache.GetDebugRenderer();
89   if( !mImpl->mRenderer )
90   {
91     Geometry geometry = CreateQuadWireframeGeometry();
92     Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
93
94     mImpl->mRenderer = Renderer::New( geometry, shader);
95     mFactoryCache.CacheDebugRenderer( mImpl->mRenderer );
96   }
97 }
98
99 Geometry DebugVisual::CreateQuadWireframeGeometry()
100 {
101   const float halfWidth = 0.5f;
102   const float halfHeight = 0.5f;
103   struct QuadVertex { Vector2 position;};
104   QuadVertex quadVertexData[4] =
105   {
106       { Vector2(-halfWidth, -halfHeight) },
107       { Vector2( halfWidth, -halfHeight) },
108       { Vector2( halfWidth,  halfHeight) },
109       { Vector2(-halfWidth,  halfHeight) }
110   };
111
112   Property::Map quadVertexFormat;
113   quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
114   PropertyBuffer quadVertices = PropertyBuffer::New( quadVertexFormat );
115   quadVertices.SetData( quadVertexData, 4 );
116
117   // Create indices
118   unsigned short indexData[10] = { 0, 1, 1, 2, 2, 3, 3, 0 };
119
120   // Create the geometry object
121   Geometry geometry = Geometry::New();
122   geometry.AddVertexBuffer( quadVertices );
123   geometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
124   geometry.SetGeometryType( Geometry::LINES );
125
126   return geometry;
127 }
128
129 } // namespace Internal
130
131 } // namespace Toolkit
132
133 } // namespace Dali