Updated all cpp files to new format
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / wireframe / wireframe-visual.cpp
1 /*
2  * Copyright (c) 2021 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 "wireframe-visual.h"
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
23 #include <dali-toolkit/internal/visuals/visual-base-data-impl.h>
24 #include <dali-toolkit/internal/visuals/visual-factory-cache.h>
25 #include <dali-toolkit/internal/visuals/visual-factory-impl.h>
26 #include <dali-toolkit/internal/visuals/visual-string-constants.h>
27 #include <dali-toolkit/public-api/visuals/visual-properties.h>
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33 namespace Internal
34 {
35 namespace
36 {
37 const char* const POSITION_ATTRIBUTE_NAME("aPosition");
38 const char* const INDEX_NAME("indices");
39 } // namespace
40
41 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
42 {
43   Visual::BasePtr emtptyVisual;
44
45   return New(factoryCache, emtptyVisual, properties);
46 }
47
48 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual)
49 {
50   WireframeVisualPtr wireframeVisual(new WireframeVisual(factoryCache, actualVisual));
51   wireframeVisual->Initialize();
52   return wireframeVisual;
53 }
54
55 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual, const Property::Map& properties)
56 {
57   WireframeVisualPtr wireframeVisual(new WireframeVisual(factoryCache, actualVisual));
58
59   // Instead of calling SetProperties, looking for the only valid property 'transform'
60   Property::Value* transformValue = properties.Find(Toolkit::Visual::Property::TRANSFORM, TRANSFORM);
61   Property::Map    transformMap;
62   if(transformValue && transformValue->Get(transformMap))
63   {
64     wireframeVisual->SetTransformAndSize(transformMap, Vector2::ZERO);
65   }
66   wireframeVisual->Initialize();
67   return wireframeVisual;
68 }
69
70 WireframeVisual::WireframeVisual(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual)
71 : Visual::Base(factoryCache, Visual::FittingMode::FILL, actualVisual ? actualVisual->GetType() : Toolkit::Visual::WIREFRAME),
72   mActualVisual(actualVisual)
73 {
74 }
75
76 WireframeVisual::~WireframeVisual()
77 {
78 }
79
80 float WireframeVisual::GetHeightForWidth(float width)
81 {
82   if(mActualVisual)
83   {
84     return mActualVisual->GetHeightForWidth(width);
85   }
86   else
87   {
88     return Visual::Base::GetHeightForWidth(width);
89   }
90 }
91
92 void WireframeVisual::GetNaturalSize(Vector2& naturalSize)
93 {
94   if(mActualVisual)
95   {
96     mActualVisual->GetNaturalSize(naturalSize);
97   }
98   else
99   {
100     Visual::Base::GetNaturalSize(naturalSize);
101   }
102 }
103
104 void WireframeVisual::DoCreatePropertyMap(Property::Map& map) const
105 {
106   if(mActualVisual)
107   {
108     mActualVisual->CreatePropertyMap(map);
109   }
110   else
111   {
112     map.Clear();
113     map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::WIREFRAME);
114   }
115 }
116
117 void WireframeVisual::DoCreateInstancePropertyMap(Property::Map& map) const
118 {
119   // Do nothing
120 }
121
122 void WireframeVisual::DoSetProperties(const Property::Map& propertyMap)
123 {
124   Property::Value* mixValue = propertyMap.Find(Toolkit::Visual::Property::MIX_COLOR, MIX_COLOR);
125   if(mixValue)
126   {
127     Vector4 mixColor;
128     mixValue->Get(mixColor);
129     SetMixColor(mixColor);
130   }
131 }
132
133 void WireframeVisual::DoSetOnScene(Actor& actor)
134 {
135   actor.AddRenderer(mImpl->mRenderer);
136
137   // Wireframe generated and ready to display
138   ResourceReady(Toolkit::Visual::ResourceStatus::READY);
139 }
140
141 void WireframeVisual::OnInitialize()
142 {
143   Shader shader = mFactoryCache.GetShader(VisualFactoryCache::WIREFRAME_SHADER);
144   if(!shader)
145   {
146     shader = Shader::New(SHADER_WIREFRAME_VISUAL_SHADER_VERT, SHADER_WIREFRAME_VISUAL_SHADER_FRAG);
147     mFactoryCache.SaveShader(VisualFactoryCache::WIREFRAME_SHADER, shader);
148   }
149
150   Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::WIREFRAME_GEOMETRY);
151   if(!geometry)
152   {
153     geometry = CreateQuadWireframeGeometry();
154     mFactoryCache.SaveGeometry(VisualFactoryCache::WIREFRAME_GEOMETRY, geometry);
155   }
156
157   //Create the renderer
158   mImpl->mRenderer = Renderer::New(geometry, shader);
159
160   //Register transform properties
161   mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
162 }
163
164 Geometry WireframeVisual::CreateQuadWireframeGeometry()
165 {
166   const float halfWidth  = 0.5f;
167   const float halfHeight = 0.5f;
168   struct QuadVertex
169   {
170     Vector2 position;
171   };
172   QuadVertex quadVertexData[4] =
173     {
174       {Vector2(-halfWidth, -halfHeight)},
175       {Vector2(halfWidth, -halfHeight)},
176       {Vector2(halfWidth, halfHeight)},
177       {Vector2(-halfWidth, halfHeight)}};
178
179   Property::Map quadVertexFormat;
180   quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
181   VertexBuffer quadVertices                 = VertexBuffer::New(quadVertexFormat);
182   quadVertices.SetData(quadVertexData, 4);
183
184   // Create indices
185   unsigned short indexData[10] = {0, 1, 1, 2, 2, 3, 3, 0};
186
187   // Create the geometry object
188   Geometry geometry = Geometry::New();
189   geometry.AddVertexBuffer(quadVertices);
190   geometry.SetIndexBuffer(indexData, sizeof(indexData) / sizeof(indexData[0]));
191   geometry.SetType(Geometry::LINES);
192
193   return geometry;
194 }
195
196 void WireframeVisual::OnSetTransform()
197 {
198   if(mImpl->mRenderer)
199   {
200     //Register transform properties
201     mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
202   }
203 }
204
205 Visual::Base& WireframeVisual::GetVisualObject()
206 {
207   if(mActualVisual)
208   {
209     return *mActualVisual.Get();
210   }
211
212   return *this;
213 }
214
215 } // namespace Internal
216
217 } // namespace Toolkit
218
219 } // namespace Dali