2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "wireframe-visual.h"
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>
37 const int CUSTOM_PROPERTY_COUNT(5);
38 const char* const POSITION_ATTRIBUTE_NAME("aPosition");
39 const char* const INDEX_NAME("indices");
42 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
44 Visual::BasePtr emtptyVisual;
46 return New(factoryCache, emtptyVisual, properties);
49 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual)
51 WireframeVisualPtr wireframeVisual(new WireframeVisual(factoryCache, actualVisual));
52 wireframeVisual->Initialize();
53 return wireframeVisual;
56 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual, const Property::Map& properties)
58 WireframeVisualPtr wireframeVisual(new WireframeVisual(factoryCache, actualVisual));
60 // Instead of calling SetProperties, looking for the only valid property 'transform'
61 Property::Value* transformValue = properties.Find(Toolkit::Visual::Property::TRANSFORM, TRANSFORM);
62 Property::Map transformMap;
63 if(transformValue && transformValue->Get(transformMap))
65 wireframeVisual->SetTransformAndSize(transformMap, Vector2::ZERO);
67 wireframeVisual->Initialize();
68 return wireframeVisual;
71 WireframeVisual::WireframeVisual(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual)
72 : Visual::Base(factoryCache, Visual::FittingMode::FILL, actualVisual ? actualVisual->GetType() : Toolkit::Visual::WIREFRAME),
73 mActualVisual(actualVisual)
77 WireframeVisual::~WireframeVisual()
81 float WireframeVisual::GetHeightForWidth(float width)
85 return mActualVisual->GetHeightForWidth(width);
89 return Visual::Base::GetHeightForWidth(width);
93 void WireframeVisual::GetNaturalSize(Vector2& naturalSize)
97 mActualVisual->GetNaturalSize(naturalSize);
101 Visual::Base::GetNaturalSize(naturalSize);
105 void WireframeVisual::DoCreatePropertyMap(Property::Map& map) const
109 mActualVisual->CreatePropertyMap(map);
114 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::WIREFRAME);
118 void WireframeVisual::DoCreateInstancePropertyMap(Property::Map& map) const
123 void WireframeVisual::DoSetProperties(const Property::Map& propertyMap)
125 Property::Value* mixValue = propertyMap.Find(Toolkit::Visual::Property::MIX_COLOR, MIX_COLOR);
129 mixValue->Get(mixColor);
130 SetMixColor(mixColor);
134 void WireframeVisual::DoSetOnScene(Actor& actor)
136 actor.AddRenderer(mImpl->mRenderer);
138 // Wireframe generated and ready to display
139 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
142 void WireframeVisual::OnInitialize()
144 Shader shader = mFactoryCache.GetShader(VisualFactoryCache::WIREFRAME_SHADER);
147 shader = Shader::New(SHADER_WIREFRAME_VISUAL_SHADER_VERT, SHADER_WIREFRAME_VISUAL_SHADER_FRAG);
148 mFactoryCache.SaveShader(VisualFactoryCache::WIREFRAME_SHADER, shader);
151 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::WIREFRAME_GEOMETRY);
154 geometry = CreateQuadWireframeGeometry();
155 mFactoryCache.SaveGeometry(VisualFactoryCache::WIREFRAME_GEOMETRY, geometry);
158 //Create the renderer
159 mImpl->mRenderer = Renderer::New(geometry, shader);
160 mImpl->mRenderer.ReserveCustomProperties(CUSTOM_PROPERTY_COUNT);
162 //Register transform properties
163 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
166 Geometry WireframeVisual::CreateQuadWireframeGeometry()
168 const float halfWidth = 0.5f;
169 const float halfHeight = 0.5f;
174 QuadVertex quadVertexData[4] =
176 {Vector2(-halfWidth, -halfHeight)},
177 {Vector2(halfWidth, -halfHeight)},
178 {Vector2(halfWidth, halfHeight)},
179 {Vector2(-halfWidth, halfHeight)}};
181 Property::Map quadVertexFormat;
182 quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
183 VertexBuffer quadVertices = VertexBuffer::New(quadVertexFormat);
184 quadVertices.SetData(quadVertexData, 4);
187 unsigned short indexData[10] = {0, 1, 1, 2, 2, 3, 3, 0};
189 // Create the geometry object
190 Geometry geometry = Geometry::New();
191 geometry.AddVertexBuffer(quadVertices);
192 geometry.SetIndexBuffer(indexData, sizeof(indexData) / sizeof(indexData[0]));
193 geometry.SetType(Geometry::LINES);
198 void WireframeVisual::OnSetTransform()
202 //Register transform properties
203 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
207 Visual::Base& WireframeVisual::GetVisualObject()
211 return *mActualVisual.Get();
217 } // namespace Internal
219 } // namespace Toolkit