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 "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, Vector2 scale, VertexFn positionFn, VertexFn texCoordFn)
126 DALI_ASSERT_DEBUG(xVerts > 1 && yVerts > 1);
127 int numVerts = xVerts * yVerts;
133 std::vector<Vertex> vertices;
134 vertices.reserve(numVerts);
136 float dx = 1.f / (xVerts - 1);
137 float dz = 1.f / (yVerts - 1);
139 Vector2 pos{0.f, 0.f};
140 for(unsigned int i = 0; i < yVerts; ++i)
142 pos.x = float(int((i & 1) * 2) - 1) * dx * .25f;
143 for(unsigned int j = 0; j < xVerts; ++j)
145 auto vPos = pos + Vector2{-.5f, -.5f};
146 vertices.push_back(Vertex{(positionFn ? positionFn(vPos) : vPos) * scale,
147 texCoordFn ? texCoordFn(pos) : pos});
154 VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
155 .Add("aPosition", Property::VECTOR2)
156 .Add("aTexCoord", Property::VECTOR2));
157 vertexBuffer.SetData(vertices.data(), vertices.size());
159 int numInds = (xVerts - 1) * (yVerts - 1) * 6;
160 std::vector<uint16_t> indices;
161 indices.reserve(numInds);
163 for(unsigned int i = 1; i < yVerts; ++i)
167 for(unsigned int j = 1; j < xVerts; ++j)
169 int iBase = i * xVerts + j;
170 indices.push_back(iBase);
171 indices.push_back(iBase - 1);
172 indices.push_back(iBase - xVerts - 1);
173 indices.push_back(indices.back());
174 indices.push_back(iBase - xVerts);
175 indices.push_back(iBase);
180 for(unsigned int j = 1; j < xVerts; ++j)
182 int iBase = i * xVerts + j;
183 indices.push_back(iBase);
184 indices.push_back(iBase - 1);
185 indices.push_back(iBase - xVerts);
186 indices.push_back(indices.back());
187 indices.push_back(iBase - 1);
188 indices.push_back(iBase - xVerts - 1);
193 Geometry geom = Geometry::New();
194 geom.AddVertexBuffer(vertexBuffer);
195 geom.SetIndexBuffer(indices.data(), indices.size());
199 Texture LoadTexture(const std::string& path)
201 PixelData pixelData = SyncImageLoader::Load(path);
203 Texture texture = Texture::New(TextureType::TEXTURE_2D, pixelData.GetPixelFormat(), pixelData.GetWidth(), pixelData.GetHeight());
204 texture.Upload(pixelData);
208 Renderer CreateRenderer(TextureSet textures, Geometry geometry, Shader shader, uint32_t options)
210 Renderer renderer = Renderer::New(geometry, shader);
211 renderer.SetProperty(Renderer::Property::BLEND_MODE,
212 (options & OPTION_BLEND) ? BlendMode::ON : BlendMode::OFF);
213 renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE,
214 (options & OPTION_DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF);
215 renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE,
216 (options & OPTION_DEPTH_WRITE) ? DepthWriteMode::ON : DepthWriteMode::OFF);
217 renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
221 textures = TextureSet::New();
224 renderer.SetTextures(textures);
228 void CenterActor(Actor actor)
230 actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
231 actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
236 auto actor = Actor::New();
241 Renderer CloneRenderer(Renderer original)
243 Geometry geom = original.GetGeometry();
244 Shader shader = original.GetShader();
245 Renderer clone = Renderer::New(geom, shader);
248 Property::IndexContainer indices;
249 original.GetPropertyIndices(indices);
251 for(auto& i : indices)
253 auto actualIndex = Dali::PropertyRanges::DEFAULT_RENDERER_PROPERTY_START_INDEX + i;
254 clone.SetProperty(actualIndex, original.GetProperty(actualIndex));
257 // Copy texture references (and create TextureSet, if there's any textures).
258 TextureSet ts = original.GetTextures();
259 clone.SetTextures(ts);
264 Actor CloneActor(Actor original)
266 using namespace Dali;
268 auto clone = Actor::New();
269 clone.SetProperty(Actor::Property::NAME, original.GetProperty(Actor::Property::NAME));
272 // Don't copy every single one of them.
273 // Definitely don't copy resize policy related things, which will internally enable
274 // relayout, which in turn will result in losing the ability to set Z size.
276 Actor::Property::PARENT_ORIGIN,
277 Actor::Property::ANCHOR_POINT,
278 Actor::Property::SIZE,
279 Actor::Property::POSITION,
280 Actor::Property::ORIENTATION,
281 Actor::Property::SCALE,
282 Actor::Property::VISIBLE,
283 Actor::Property::COLOR,
284 Actor::Property::NAME,
287 clone.SetProperty(i, original.GetProperty(i));
291 for(unsigned int i = 0; i < original.GetRendererCount(); ++i)
293 auto rClone = CloneRenderer(original.GetRendererAt(i));
294 clone.AddRenderer(rClone);
297 // Recurse into children.
298 for(unsigned int i = 0; i < original.GetChildCount(); ++i)
300 Actor newChild = CloneActor(original.GetChildAt(i));