2 * Copyright (c) 2020 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.
18 #include "dali-toolkit/dali-toolkit.h"
22 using namespace Dali::Toolkit;
24 Vector3 ToHueSaturationLightness(Vector3 rgb)
26 float min = std::min(rgb.r, std::min(rgb.g, rgb.b));
27 float max = std::max(rgb.r, std::max(rgb.g, rgb.b));
29 Vector3 hsl(max - min, 0.f, (max + min) * .5f);
30 if (hsl.x * hsl.x > .0f)
35 hsl.x = (rgb.g - rgb.b) / hsl.x;
39 hsl.x = 2.f + (rgb.b - rgb.r) / hsl.x;
43 hsl.x = 4.f + (rgb.r - rgb.g) / hsl.x;
59 Vector3 FromHueSaturationLightness(Vector3 hsl)
62 if (hsl.y * hsl.y > 0.f)
70 int i = FastFloor(hsl.x);
72 float p = hsl.z * (1.0 - hsl.y);
73 float q = hsl.z * (1.0 - (hsl.y * ff));
74 float t = hsl.z * (1.0 - (hsl.y * (1.f - ff)));
118 rgb = Vector3::ONE * hsl.z;
124 Geometry CreateTesselatedQuad(unsigned int xVerts, unsigned int yVerts,
125 Vector2 scale, VertexFn positionFn, VertexFn texCoordFn)
127 DALI_ASSERT_DEBUG(xVerts > 1 && yVerts > 1);
128 int numVerts = xVerts * yVerts;
134 std::vector<Vertex> vertices;
135 vertices.reserve( numVerts);
137 float dx = 1.f / (xVerts - 1);
138 float dz = 1.f / (yVerts - 1);
140 Vector2 pos{ 0.f, 0.f };
141 for (unsigned int i = 0; i < yVerts; ++i)
143 pos.x = float(int((i & 1) * 2) - 1) * dx * .25f;
144 for (unsigned int j = 0; j < xVerts; ++j)
146 auto vPos = pos + Vector2{ -.5f, -.5f };
147 vertices.push_back(Vertex{ (positionFn ? positionFn(vPos) : vPos) * scale,
148 texCoordFn ? texCoordFn(pos) : pos });
155 VertexBuffer vertexBuffer = VertexBuffer::New( Property::Map()
156 .Add( "aPosition", Property::VECTOR2 )
157 .Add( "aTexCoord", Property::VECTOR2 ));
158 vertexBuffer.SetData(vertices.data(), vertices.size());
160 int numInds = (xVerts - 1) * (yVerts - 1) * 6;
161 std::vector<uint16_t> indices;
162 indices.reserve(numInds);
164 for (unsigned int i = 1; i < yVerts; ++i)
168 for (unsigned int j = 1; j < xVerts; ++j)
170 int iBase = i * xVerts + j;
171 indices.push_back(iBase);
172 indices.push_back(iBase - 1);
173 indices.push_back(iBase - xVerts - 1);
174 indices.push_back(indices.back());
175 indices.push_back(iBase - xVerts);
176 indices.push_back(iBase);
181 for (unsigned int j = 1; j < xVerts; ++j)
183 int iBase = i * xVerts + j;
184 indices.push_back(iBase);
185 indices.push_back(iBase - 1);
186 indices.push_back(iBase - xVerts);
187 indices.push_back(indices.back());
188 indices.push_back(iBase - 1);
189 indices.push_back(iBase - xVerts - 1);
194 Geometry geom = Geometry::New();
195 geom.AddVertexBuffer(vertexBuffer);
196 geom.SetIndexBuffer(indices.data(), indices.size());
200 Texture LoadTexture(const std::string& path)
202 PixelData pixelData = SyncImageLoader::Load(path);
204 Texture texture = Texture::New(TextureType::TEXTURE_2D, pixelData.GetPixelFormat(),
205 pixelData.GetWidth(), pixelData.GetHeight());
206 texture.Upload(pixelData);
210 Renderer CreateRenderer(TextureSet textures, Geometry geometry, Shader shader, uint32_t options)
212 Renderer renderer = Renderer::New(geometry, shader);
213 renderer.SetProperty(Renderer::Property::BLEND_MODE,
214 (options & OPTION_BLEND) ? BlendMode::ON : BlendMode::OFF);
215 renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE,
216 (options & OPTION_DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF);
217 renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE,
218 (options & OPTION_DEPTH_WRITE) ? DepthWriteMode::ON : DepthWriteMode::OFF);
219 renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
223 textures = TextureSet::New();
226 renderer.SetTextures(textures);
230 void CenterActor(Actor actor)
232 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
233 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
238 auto actor = Actor::New();
243 Renderer CloneRenderer(Renderer original)
245 Geometry geom = original.GetGeometry();
246 Shader shader = original.GetShader();
247 Renderer clone = Renderer::New(geom, shader);
250 Property::IndexContainer indices;
251 original.GetPropertyIndices(indices);
253 for (auto& i: indices)
255 auto actualIndex = Dali::PropertyRanges::DEFAULT_RENDERER_PROPERTY_START_INDEX + i;
256 clone.SetProperty(actualIndex, original.GetProperty(actualIndex));
259 // Copy texture references (and create TextureSet, if there's any textures).
260 TextureSet ts = original.GetTextures();
261 clone.SetTextures(ts);
266 Actor CloneActor(Actor original)
268 using namespace Dali;
270 auto clone = Actor::New();
271 clone.SetProperty(Actor::Property::NAME, original.GetProperty(Actor::Property::NAME));
274 // Don't copy every single one of them.
275 // Definitely don't copy resize policy related things, which will internally enable
276 // relayout, which in turn will result in losing the ability to set Z size.
278 Actor::Property::PARENT_ORIGIN,
279 Actor::Property::ANCHOR_POINT,
280 Actor::Property::SIZE,
281 Actor::Property::POSITION,
282 Actor::Property::ORIENTATION,
283 Actor::Property::SCALE,
284 Actor::Property::VISIBLE,
285 Actor::Property::COLOR,
286 Actor::Property::NAME,
289 clone.SetProperty(i, original.GetProperty(i));
293 for(unsigned int i = 0; i < original.GetRendererCount(); ++i)
295 auto rClone = CloneRenderer(original.GetRendererAt(i));
296 clone.AddRenderer(rClone);
299 // Recurse into children.
300 for(unsigned int i = 0; i < original.GetChildCount(); ++i)
302 Actor newChild = CloneActor(original.GetChildAt(i));