Updated all files to new format
[platform/core/uifw/dali-demo.git] / examples / waves / utils.h
1 #ifndef WAVES_UTILS_H_
2 #define WAVES_UTILS_H_
3 /*
4  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #include <cmath>
20 #include "dali/public-api/actors/actor.h"
21 #include "dali/public-api/math/vector3.h"
22 #include "dali/public-api/rendering/geometry.h"
23 #include "dali/public-api/rendering/renderer.h"
24 #include "dali/public-api/rendering/shader.h"
25 #include "dali/public-api/rendering/texture.h"
26
27 //
28 // Maths
29 //
30 inline float FastFloor(float x)
31 {
32   return static_cast<int>(x) - static_cast<int>(x < 0);
33 }
34
35 inline float Sign(float x)
36 {
37   return float(x > 0.f) - float(x < .0f);
38 }
39
40 template<typename T>
41 inline
42   typename std::decay<T>::type
43   Lerp(
44     const T& min, const T& max, float alpha)
45 {
46   return min + (max - min) * alpha;
47 }
48
49 template<typename T>
50 T Normalized(T v)
51 {
52   v.Normalize();
53   return v;
54 }
55
56 //
57 // Files
58 //
59 ///@brief Converts RGB values (in the 0..1 range) to HSL, where hue is in degrees,
60 /// in the 0..360 range, and saturation and lightness are in the 0..1 range.
61 Dali::Vector3 ToHueSaturationLightness(Dali::Vector3 rgb);
62
63 ///@brief Converts HSL values, where hue is in degrees, in the 0..360 range, and
64 /// saturation and lightness are in 0..1  to RGB (in the 0..1 range)
65 Dali::Vector3 FromHueSaturationLightness(Dali::Vector3 hsl);
66
67 //
68 // Dali entities
69 //
70 using VertexFn = Dali::Vector2 (*)(const Dali::Vector2&);
71
72 ///@brief Creates a tesselated quad with @a xVerts vertices horizontally and @a yVerts
73 /// vertices vertically. Allows the use of an optional @a shaderFn, which can be used to
74 /// modify the vertex positions - these will be in the [{ 0.f, 0.f}, { 1.f, 1.f}] range.
75 /// After returning from the shader, they're transformed
76 Dali::Geometry CreateTesselatedQuad(unsigned int xVerts, unsigned int yVerts, Dali::Vector2 scale, VertexFn positionFn = nullptr, VertexFn texCoordFn = nullptr);
77
78 Dali::Texture LoadTexture(const std::string& path);
79
80 enum RendererOptions
81 {
82   OPTION_NONE        = 0x0,
83   OPTION_BLEND       = 0x01,
84   OPTION_DEPTH_TEST  = 0x02,
85   OPTION_DEPTH_WRITE = 0x04
86 };
87
88 ///@brief Creates a renderer with the given @a textures set, @a geometry, @a shader
89 /// and @a options from above.
90 ///@note Back face culling is on.
91 ///@note If textures is not a valid handle, an empty texture set will be created.
92 Dali::Renderer CreateRenderer(Dali::TextureSet textures, Dali::Geometry geometry, Dali::Shader shader, uint32_t options = OPTION_NONE);
93
94 ///@brief Sets @a actor's anchor point and parent origin to center.
95 void CenterActor(Dali::Actor actor);
96
97 ///@brief Creates an empty and centered actor.
98 Dali::Actor CreateActor();
99
100 ///@brief Creates a copy of @a original, sharing the same geometry and shader and
101 /// copying each properties.
102 ///@note Breaks if @a original has any custom properties. TODO: fix.
103 Dali::Renderer CloneRenderer(Dali::Renderer original);
104
105 ///@brief Creates a copy of @a original, cloning each renderer, and a select set
106 /// of properties: parent origin, anchor point, size, position, orientation, scale,
107 /// visible, color and name.
108 ///@note Does not copy resize policy related properties, as setting those, even if
109 /// default, will break the ability to specify a size for the actor in Z.
110 Dali::Actor CloneActor(Dali::Actor original);
111
112 #endif /* EXAMPLES_PARTICLES_UTILS_H_ */