Debian Codegraph @

scene/resources/gradient.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* gradient.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "gradient.h"
32
33 #include "core/config/engine.h"
34
35 Gradient::Gradient() {
36 //Set initial gradient transition from black to white
37 points.resize(2);
38 points[0].color = Color(0, 0, 0, 1);
39 points[0].offset = 0;
40 points[1].color = Color(1, 1, 1, 1);
41 points[1].offset = 1;
42 }
43
44 Gradient::~Gradient() {
45 }
46
47 void Gradient::_bind_methods() {
48 ClassDB::bind_method(D_METHOD("add_point", "offset", "color"), &Gradient::add_point);
49 ClassDB::bind_method(D_METHOD("remove_point", "point"), &Gradient::remove_point);
50
51 ClassDB::bind_method(D_METHOD("set_offset", "point", "offset"), &Gradient::set_offset);
52 ClassDB::bind_method(D_METHOD("get_offset", "point"), &Gradient::get_offset);
53
54 ClassDB::bind_method(D_METHOD("reverse"), &Gradient::reverse);
55
56 ClassDB::bind_method(D_METHOD("set_color", "point", "color"), &Gradient::set_color);
57 ClassDB::bind_method(D_METHOD("get_color", "point"), &Gradient::get_color);
58
59 ClassDB::bind_method(D_METHOD("sample", "offset"), &Gradient::get_color_at_offset);
60
61 ClassDB::bind_method(D_METHOD("get_point_count"), &Gradient::get_point_count);
62
63 ClassDB::bind_method(D_METHOD("set_offsets", "offsets"), &Gradient::set_offsets);
64 ClassDB::bind_method(D_METHOD("get_offsets"), &Gradient::get_offsets);
65
66 ClassDB::bind_method(D_METHOD("set_colors", "colors"), &Gradient::set_colors);
67 ClassDB::bind_method(D_METHOD("get_colors"), &Gradient::get_colors);
68
69 ClassDB::bind_method(D_METHOD("set_interpolation_mode", "interpolation_mode"), &Gradient::set_interpolation_mode);
70 ClassDB::bind_method(D_METHOD("get_interpolation_mode"), &Gradient::get_interpolation_mode);
71
72 ClassDB::bind_method(D_METHOD("set_interpolation_color_space", "interpolation_color_space"), &Gradient::set_interpolation_color_space);
73 ClassDB::bind_method(D_METHOD("get_interpolation_color_space"), &Gradient::get_interpolation_color_space);
74
75 ADD_GROUP("Interpolation", "interpolation_");
76 ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_mode", PROPERTY_HINT_ENUM, "Linear,Constant,Cubic"), "set_interpolation_mode", "get_interpolation_mode");
77 ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_color_space", PROPERTY_HINT_ENUM, "sRGB,Linear sRGB,Oklab"), "set_interpolation_color_space", "get_interpolation_color_space");
78
79 ADD_GROUP("Raw Data", "");
80 ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "offsets"), "set_offsets", "get_offsets");
81 ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "colors"), "set_colors", "get_colors");
82
83 BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_LINEAR);
84 BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CONSTANT);
85 BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CUBIC);
86
87 BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_SRGB);
88 BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_LINEAR_SRGB);
89 BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_OKLAB);
90 }
91
92 void Gradient::_validate_property(PropertyInfo &p_property) const {
93 if (!Engine::get_singleton()->is_editor_hint()) {
94 return;
95 }
96 if (p_property.name == "interpolation_color_space" && interpolation_mode == GRADIENT_INTERPOLATE_CONSTANT) {
97 p_property.usage = PROPERTY_USAGE_NO_EDITOR;
98 }
99 }
100
101 Vector<float> Gradient::get_offsets() const {
102 Vector<float> offsets;
103 offsets.resize(points.size());
104 for (uint32_t i = 0; i < points.size(); i++) {
105 offsets.write[i] = points[i].offset;
106 }
107 return offsets;
108 }
109
110 Vector<Color> Gradient::get_colors() const {
111 Vector<Color> colors;
112 colors.resize(points.size());
113 for (uint32_t i = 0; i < points.size(); i++) {
114 colors.write[i] = points[i].color;
115 }
116 return colors;
117 }
118
119 void Gradient::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
120 if (p_interp_mode == interpolation_mode) {
121 return;
122 }
123
124 interpolation_mode = p_interp_mode;
125 emit_changed();
126 notify_property_list_changed();
127 }
128
129 Gradient::InterpolationMode Gradient::get_interpolation_mode() {
130 return interpolation_mode;
131 }
132
133 void Gradient::set_interpolation_color_space(Gradient::ColorSpace p_color_space) {
134 if (p_color_space == interpolation_color_space) {
135 return;
136 }
137
138 interpolation_color_space = p_color_space;
139 emit_changed();
140 }
141
142 Gradient::ColorSpace Gradient::get_interpolation_color_space() {
143 return interpolation_color_space;
144 }
145
146 void Gradient::set_offsets(const Vector<float> &p_offsets) {
147 points.resize(p_offsets.size());
148 for (uint32_t i = 0; i < points.size(); i++) {
149 points[i].offset = p_offsets[i];
150 }
151 is_sorted = false;
152 emit_changed();
153 }
154
155 void Gradient::set_colors(const Vector<Color> &p_colors) {
156 if (points.size() < p_colors.size()) {
157 is_sorted = false;
158 }
159 points.resize(p_colors.size());
160 for (uint32_t i = 0; i < points.size(); i++) {
161 points[i].color = p_colors[i];
162 }
163 emit_changed();
164 }
165
166 void Gradient::add_point(float p_offset, const Color &p_color) {
167 Point p;
168 p.offset = p_offset;
169 p.color = p_color;
170 is_sorted = false;
171 points.push_back(p);
172
173 emit_changed();
174 }
175
176 void Gradient::remove_point(int p_index) {
177 ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_index, points.size());
178 ERR_FAIL_COND(points.size() <= 1);
179 points.remove_at(p_index);
180 emit_changed();
181 }
182
183 void Gradient::reverse() {
184 for (uint32_t i = 0; i < points.size(); i++) {
185 points[i].offset = 1.0 - points[i].offset;
186 }
187
188 is_sorted = false;
189 _update_sorting();
190 emit_changed();
191 }
192
193 void Gradient::set_offset(int pos, const float offset) {
194 ERR_FAIL_UNSIGNED_INDEX((uint32_t)pos, points.size());
195 _update_sorting();
196 points[pos].offset = offset;
197 is_sorted = false;
198 emit_changed();
199 }
200
201 float Gradient::get_offset(int pos) {
202 ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)pos, points.size(), 0.0);
203 _update_sorting();
204 return points[pos].offset;
205 }
206
207 void Gradient::set_color(int pos, const Color &color) {
208 ERR_FAIL_UNSIGNED_INDEX((uint32_t)pos, points.size());
209 _update_sorting();
210 points[pos].color = color;
211 emit_changed();
212 }
213
214 Color Gradient::get_color(int pos) {
215 ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)pos, points.size(), Color());
216 _update_sorting();
217 return points[pos].color;
218 }
219
220 int Gradient::get_point_count() const {
221 return points.size();
222 }