2 * Copyright (c) 2021 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 char* const POSITION_ATTRIBUTE_NAME("aPosition");
38 const char* const INDEX_NAME("indices");
41 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, const Property::Map& properties)
43 Visual::BasePtr emtptyVisual;
45 return New(factoryCache, emtptyVisual, properties);
48 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual)
50 WireframeVisualPtr wireframeVisual(new WireframeVisual(factoryCache, actualVisual));
51 wireframeVisual->Initialize();
52 return wireframeVisual;
55 WireframeVisualPtr WireframeVisual::New(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual, const Property::Map& properties)
57 WireframeVisualPtr wireframeVisual(new WireframeVisual(factoryCache, actualVisual));
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))
64 wireframeVisual->SetTransformAndSize(transformMap, Vector2::ZERO);
66 wireframeVisual->Initialize();
67 return wireframeVisual;
70 WireframeVisual::WireframeVisual(VisualFactoryCache& factoryCache, Visual::BasePtr actualVisual)
71 : Visual::Base(factoryCache, Visual::FittingMode::FILL, actualVisual ? actualVisual->GetType() : Toolkit::Visual::WIREFRAME),
72 mActualVisual(actualVisual)
76 WireframeVisual::~WireframeVisual()
80 float WireframeVisual::GetHeightForWidth(float width)
84 return mActualVisual->GetHeightForWidth(width);
88 return Visual::Base::GetHeightForWidth(width);
92 void WireframeVisual::GetNaturalSize(Vector2& naturalSize)
96 mActualVisual->GetNaturalSize(naturalSize);
100 Visual::Base::GetNaturalSize(naturalSize);
104 void WireframeVisual::DoCreatePropertyMap(Property::Map& map) const
108 mActualVisual->CreatePropertyMap(map);
113 map.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::WIREFRAME);
117 void WireframeVisual::DoCreateInstancePropertyMap(Property::Map& map) const
122 void WireframeVisual::DoSetProperties(const Property::Map& propertyMap)
124 Property::Value* mixValue = propertyMap.Find(Toolkit::Visual::Property::MIX_COLOR, MIX_COLOR);
128 mixValue->Get(mixColor);
129 SetMixColor(mixColor);
133 void WireframeVisual::DoSetOnScene(Actor& actor)
135 actor.AddRenderer(mImpl->mRenderer);
137 // Wireframe generated and ready to display
138 ResourceReady(Toolkit::Visual::ResourceStatus::READY);
141 void WireframeVisual::OnInitialize()
143 Shader shader = mFactoryCache.GetShader(VisualFactoryCache::WIREFRAME_SHADER);
146 shader = Shader::New(SHADER_WIREFRAME_VISUAL_SHADER_VERT, SHADER_WIREFRAME_VISUAL_SHADER_FRAG);
147 mFactoryCache.SaveShader(VisualFactoryCache::WIREFRAME_SHADER, shader);
150 Geometry geometry = mFactoryCache.GetGeometry(VisualFactoryCache::WIREFRAME_GEOMETRY);
153 geometry = CreateQuadWireframeGeometry();
154 mFactoryCache.SaveGeometry(VisualFactoryCache::WIREFRAME_GEOMETRY, geometry);
157 //Create the renderer
158 mImpl->mRenderer = Renderer::New(geometry, shader);
160 //Register transform properties
161 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
164 Geometry WireframeVisual::CreateQuadWireframeGeometry()
166 const float halfWidth = 0.5f;
167 const float halfHeight = 0.5f;
172 QuadVertex quadVertexData[4] =
174 {Vector2(-halfWidth, -halfHeight)},
175 {Vector2(halfWidth, -halfHeight)},
176 {Vector2(halfWidth, halfHeight)},
177 {Vector2(-halfWidth, halfHeight)}};
179 Property::Map quadVertexFormat;
180 quadVertexFormat[POSITION_ATTRIBUTE_NAME] = Property::VECTOR2;
181 VertexBuffer quadVertices = VertexBuffer::New(quadVertexFormat);
182 quadVertices.SetData(quadVertexData, 4);
185 unsigned short indexData[10] = {0, 1, 1, 2, 2, 3, 3, 0};
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);
196 void WireframeVisual::OnSetTransform()
200 //Register transform properties
201 mImpl->mTransform.RegisterUniforms(mImpl->mRenderer, Direction::LEFT_TO_RIGHT);
205 Visual::Base& WireframeVisual::GetVisualObject()
209 return *mActualVisual.Get();
215 } // namespace Internal
217 } // namespace Toolkit