Updated all files to new format
[platform/core/uifw/dali-demo.git] / examples / particles / utils.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 #include "utils.h"
18
19 using namespace Dali;
20
21 Vector3 ToHueSaturationLightness(Vector3 rgb)
22 {
23   float min = std::min(rgb.r, std::min(rgb.g, rgb.b));
24   float max = std::max(rgb.r, std::max(rgb.g, rgb.b));
25
26   Vector3 hsl(max - min, 0.f, (max + min) * .5f);
27   if(hsl.x * hsl.x > .0f)
28   {
29     hsl.y = hsl.x / max;
30     if(max == rgb.r)
31     {
32       hsl.x = (rgb.g - rgb.b) / hsl.x;
33     }
34     else if(max == rgb.g)
35     {
36       hsl.x = 2.f + (rgb.b - rgb.r) / hsl.x;
37     }
38     else
39     {
40       hsl.x = 4.f + (rgb.r - rgb.g) / hsl.x;
41     }
42     hsl.x *= 60.f;
43     if(hsl.x < 0.f)
44     {
45       hsl.x += 360.f;
46     }
47   }
48   else
49   {
50     hsl.y = 0.f;
51   }
52
53   return hsl;
54 }
55
56 Vector3 FromHueSaturationLightness(Vector3 hsl)
57 {
58   Vector3 rgb;
59   if(hsl.y * hsl.y > 0.f)
60   {
61     if(hsl.x >= 360.f)
62     {
63       hsl.x -= 360.f;
64     }
65     hsl.x /= 60.f;
66
67     int   i  = FastFloor(hsl.x);
68     float ff = hsl.x - i;
69     float p  = hsl.z * (1.0 - hsl.y);
70     float q  = hsl.z * (1.0 - (hsl.y * ff));
71     float t  = hsl.z * (1.0 - (hsl.y * (1.f - ff)));
72
73     switch(i)
74     {
75       case 0:
76         rgb.r = hsl.z;
77         rgb.g = t;
78         rgb.b = p;
79         break;
80
81       case 1:
82         rgb.r = q;
83         rgb.g = hsl.z;
84         rgb.b = p;
85         break;
86
87       case 2:
88         rgb.r = p;
89         rgb.g = hsl.z;
90         rgb.b = t;
91         break;
92
93       case 3:
94         rgb.r = p;
95         rgb.g = q;
96         rgb.b = hsl.z;
97         break;
98
99       case 4:
100         rgb.r = t;
101         rgb.g = p;
102         rgb.b = hsl.z;
103         break;
104
105       case 5:
106       default:
107         rgb.r = hsl.z;
108         rgb.g = p;
109         rgb.b = q;
110         break;
111     }
112   }
113   else
114   {
115     rgb = Vector3::ONE * hsl.z;
116   }
117
118   return rgb;
119 }
120
121 Geometry CreateCuboidWireframeGeometry()
122 {
123   //
124   // 2---3
125   // |-  |-
126   // | 6---7
127   // | | | |
128   // 0-|-1 |
129   //  -|  -|
130   //   4---5
131   //
132   Vector3 vertexData[] = {
133     Vector3(-.5, -.5, -.5),
134     Vector3(.5, -.5, -.5),
135     Vector3(-.5, .5, -.5),
136     Vector3(.5, .5, -.5),
137     Vector3(-.5, -.5, .5),
138     Vector3(.5, -.5, .5),
139     Vector3(-.5, .5, .5),
140     Vector3(.5, .5, .5),
141   };
142
143   uint16_t indices[] = {
144     0, 1, 1, 3, 3, 2, 2, 0, 0, 4, 1, 5, 3, 7, 2, 6, 4, 5, 5, 7, 7, 6, 6, 4};
145   VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
146                                                   .Add("aPosition", Property::VECTOR3));
147   vertexBuffer.SetData(vertexData, std::extent<decltype(vertexData)>::value);
148
149   Geometry geometry = Geometry::New();
150   geometry.AddVertexBuffer(vertexBuffer);
151   geometry.SetIndexBuffer(indices, std::extent<decltype(indices)>::value);
152   geometry.SetType(Geometry::LINES);
153   return geometry;
154 }
155
156 Renderer CreateRenderer(TextureSet textures, Geometry geometry, Shader shader, uint32_t options)
157 {
158   Renderer renderer = Renderer::New(geometry, shader);
159   renderer.SetProperty(Renderer::Property::BLEND_MODE,
160                        (options & OPTION_BLEND) ? BlendMode::ON : BlendMode::OFF);
161   renderer.SetProperty(Renderer::Property::DEPTH_TEST_MODE,
162                        (options & OPTION_DEPTH_TEST) ? DepthTestMode::ON : DepthTestMode::OFF);
163   renderer.SetProperty(Renderer::Property::DEPTH_WRITE_MODE,
164                        (options & OPTION_DEPTH_WRITE) ? DepthWriteMode::ON : DepthWriteMode::OFF);
165   renderer.SetProperty(Renderer::Property::FACE_CULLING_MODE, FaceCullingMode::BACK);
166
167   if(!textures)
168   {
169     textures = TextureSet::New();
170   }
171
172   renderer.SetTextures(textures);
173   return renderer;
174 }
175
176 void CenterActor(Actor actor)
177 {
178   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
179   actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
180 }
181
182 Actor CreateActor()
183 {
184   auto actor = Actor::New();
185   CenterActor(actor);
186   return actor;
187 }
188
189 Renderer CloneRenderer(Renderer original)
190 {
191   Geometry geom   = original.GetGeometry();
192   Shader   shader = original.GetShader();
193   Renderer clone  = Renderer::New(geom, shader);
194
195   // Copy properties.
196   Property::IndexContainer indices;
197   original.GetPropertyIndices(indices);
198
199   for(auto& i : indices)
200   {
201     auto actualIndex = PropertyRanges::DEFAULT_RENDERER_PROPERTY_START_INDEX + i;
202     clone.SetProperty(actualIndex, original.GetProperty(actualIndex));
203   }
204
205   // Copy texture references (and create TextureSet, if there's any textures).
206   TextureSet ts = original.GetTextures();
207   clone.SetTextures(ts);
208
209   return clone;
210 }
211
212 Actor CloneActor(Actor original)
213 {
214   using namespace Dali;
215
216   auto clone = Actor::New();
217   clone.SetProperty(Actor::Property::NAME, original.GetProperty(Actor::Property::NAME));
218
219   // Copy properties.
220   // Don't copy every single one of them.
221   // Definitely don't copy resize policy related things, which will internally enable
222   // relayout, which in turn will result in losing the ability to set Z size.
223   for(auto i : {
224         Actor::Property::PARENT_ORIGIN,
225         Actor::Property::ANCHOR_POINT,
226         Actor::Property::SIZE,
227         Actor::Property::POSITION,
228         Actor::Property::ORIENTATION,
229         Actor::Property::SCALE,
230         Actor::Property::VISIBLE,
231         Actor::Property::COLOR,
232         Actor::Property::NAME,
233       })
234   {
235     clone.SetProperty(i, original.GetProperty(i));
236   }
237
238   // Clone renderers.
239   for(unsigned int i = 0; i < original.GetRendererCount(); ++i)
240   {
241     auto rClone = CloneRenderer(original.GetRendererAt(i));
242     clone.AddRenderer(rClone);
243   }
244
245   // Recurse into children.
246   for(unsigned int i = 0; i < original.GetChildCount(); ++i)
247   {
248     Actor newChild = CloneActor(original.GetChildAt(i));
249     clone.Add(newChild);
250   }
251
252   return clone;
253 }