Debian Codegraph @

scene/resources/label_settings.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* label_settings.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 "label_settings.h"
32
33 void LabelSettings::_font_changed() {
34 emit_changed();
35 }
36
37 void LabelSettings::_bind_methods() {
38 ClassDB::bind_method(D_METHOD("set_line_spacing", "spacing"), &LabelSettings::set_line_spacing);
39 ClassDB::bind_method(D_METHOD("get_line_spacing"), &LabelSettings::get_line_spacing);
40
41 ClassDB::bind_method(D_METHOD("set_paragraph_spacing", "spacing"), &LabelSettings::set_paragraph_spacing);
42 ClassDB::bind_method(D_METHOD("get_paragraph_spacing"), &LabelSettings::get_paragraph_spacing);
43
44 ClassDB::bind_method(D_METHOD("set_font", "font"), &LabelSettings::set_font);
45 ClassDB::bind_method(D_METHOD("get_font"), &LabelSettings::get_font);
46
47 ClassDB::bind_method(D_METHOD("set_font_size", "size"), &LabelSettings::set_font_size);
48 ClassDB::bind_method(D_METHOD("get_font_size"), &LabelSettings::get_font_size);
49
50 ClassDB::bind_method(D_METHOD("set_font_color", "color"), &LabelSettings::set_font_color);
51 ClassDB::bind_method(D_METHOD("get_font_color"), &LabelSettings::get_font_color);
52
53 ClassDB::bind_method(D_METHOD("set_outline_size", "size"), &LabelSettings::set_outline_size);
54 ClassDB::bind_method(D_METHOD("get_outline_size"), &LabelSettings::get_outline_size);
55
56 ClassDB::bind_method(D_METHOD("set_outline_color", "color"), &LabelSettings::set_outline_color);
57 ClassDB::bind_method(D_METHOD("get_outline_color"), &LabelSettings::get_outline_color);
58
59 ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &LabelSettings::set_shadow_size);
60 ClassDB::bind_method(D_METHOD("get_shadow_size"), &LabelSettings::get_shadow_size);
61
62 ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &LabelSettings::set_shadow_color);
63 ClassDB::bind_method(D_METHOD("get_shadow_color"), &LabelSettings::get_shadow_color);
64
65 ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &LabelSettings::set_shadow_offset);
66 ClassDB::bind_method(D_METHOD("get_shadow_offset"), &LabelSettings::get_shadow_offset);
67
68 // Stacked outlines
69 ClassDB::bind_method(D_METHOD("get_stacked_outline_count"), &LabelSettings::get_stacked_outline_count);
70 ClassDB::bind_method(D_METHOD("set_stacked_outline_count", "count"), &LabelSettings::set_stacked_outline_count);
71 ClassDB::bind_method(D_METHOD("add_stacked_outline", "index"), &LabelSettings::add_stacked_outline, DEFVAL(-1));
72 ClassDB::bind_method(D_METHOD("move_stacked_outline", "from_index", "to_position"), &LabelSettings::move_stacked_outline);
73 ClassDB::bind_method(D_METHOD("remove_stacked_outline", "index"), &LabelSettings::remove_stacked_outline);
74 ClassDB::bind_method(D_METHOD("set_stacked_outline_size", "index", "size"), &LabelSettings::set_stacked_outline_size);
75 ClassDB::bind_method(D_METHOD("get_stacked_outline_size", "index"), &LabelSettings::get_stacked_outline_size);
76 ClassDB::bind_method(D_METHOD("set_stacked_outline_color", "index", "color"), &LabelSettings::set_stacked_outline_color);
77 ClassDB::bind_method(D_METHOD("get_stacked_outline_color", "index"), &LabelSettings::get_stacked_outline_color);
78
79 // Stacked shadows
80 ClassDB::bind_method(D_METHOD("get_stacked_shadow_count"), &LabelSettings::get_stacked_shadow_count);
81 ClassDB::bind_method(D_METHOD("set_stacked_shadow_count", "count"), &LabelSettings::set_stacked_shadow_count);
82 ClassDB::bind_method(D_METHOD("add_stacked_shadow", "index"), &LabelSettings::add_stacked_shadow, DEFVAL(-1));
83 ClassDB::bind_method(D_METHOD("move_stacked_shadow", "from_index", "to_position"), &LabelSettings::move_stacked_shadow);
84 ClassDB::bind_method(D_METHOD("remove_stacked_shadow", "index"), &LabelSettings::remove_stacked_shadow);
85 ClassDB::bind_method(D_METHOD("set_stacked_shadow_offset", "index", "offset"), &LabelSettings::set_stacked_shadow_offset);
86 ClassDB::bind_method(D_METHOD("get_stacked_shadow_offset", "index"), &LabelSettings::get_stacked_shadow_offset);
87 ClassDB::bind_method(D_METHOD("set_stacked_shadow_color", "index", "color"), &LabelSettings::set_stacked_shadow_color);
88 ClassDB::bind_method(D_METHOD("get_stacked_shadow_color", "index"), &LabelSettings::get_stacked_shadow_color);
89 ClassDB::bind_method(D_METHOD("set_stacked_shadow_outline_size", "index", "size"), &LabelSettings::set_stacked_shadow_outline_size);
90 ClassDB::bind_method(D_METHOD("get_stacked_shadow_outline_size", "index"), &LabelSettings::get_stacked_shadow_outline_size);
91
92 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_line_spacing", "get_line_spacing");
93 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "paragraph_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_paragraph_spacing", "get_paragraph_spacing");
94
95 ADD_GROUP("Font", "font_");
96 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_font", "get_font");
97 ADD_PROPERTY(PropertyInfo(Variant::INT, "font_size", PROPERTY_HINT_RANGE, "1,1024,1,or_greater,suffix:px"), "set_font_size", "get_font_size");
98 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "font_color"), "set_font_color", "get_font_color");
99
100 ADD_GROUP("Outline", "outline_");
101 ADD_PROPERTY(PropertyInfo(Variant::INT, "outline_size", PROPERTY_HINT_RANGE, "0,127,1,or_greater,suffix:px"), "set_outline_size", "get_outline_size");
102 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "outline_color"), "set_outline_color", "get_outline_color");
103
104 ADD_GROUP("Shadow", "shadow_");
105 ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size", PROPERTY_HINT_RANGE, "0,127,1,or_greater,suffix:px"), "set_shadow_size", "get_shadow_size");
106 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
107 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_shadow_offset", "get_shadow_offset");
108
109 ADD_GROUP("Stacked Effects", "");
110 ADD_ARRAY_COUNT("Stacked Outlines", "stacked_outline_count", "set_stacked_outline_count", "get_stacked_outline_count", "stacked_outline_");
111 ADD_ARRAY_COUNT("Stacked Shadows", "stacked_shadow_count", "set_stacked_shadow_count", "get_stacked_shadow_count", "stacked_shadow_");
112
113 constexpr StackedOutlineData stacked_outline_defaults;
114
115 stacked_outline_base_property_helper.set_prefix("stacked_outline_");
116 stacked_outline_base_property_helper.set_array_length_getter(&LabelSettings::get_stacked_outline_count);
117 stacked_outline_base_property_helper.register_property(PropertyInfo(Variant::INT, "size", PROPERTY_HINT_NONE, "0,127,1,or_greater,suffix:px"), stacked_outline_defaults.size, &LabelSettings::set_stacked_outline_size, &LabelSettings::get_stacked_outline_size);
118 stacked_outline_base_property_helper.register_property(PropertyInfo(Variant::COLOR, "color"), stacked_outline_defaults.color, &LabelSettings::set_stacked_outline_color, &LabelSettings::get_stacked_outline_color);
119 PropertyListHelper::register_base_helper(&stacked_outline_base_property_helper);
120
121 constexpr StackedShadowData stacked_shadow_defaults;
122
123 stacked_shadow_base_property_helper.set_prefix("stacked_shadow_");
124 stacked_shadow_base_property_helper.set_array_length_getter(&LabelSettings::get_stacked_shadow_count);
125 stacked_shadow_base_property_helper.register_property(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), stacked_shadow_defaults.offset, &LabelSettings::set_stacked_shadow_offset, &LabelSettings::get_stacked_shadow_offset);
126 stacked_shadow_base_property_helper.register_property(PropertyInfo(Variant::COLOR, "color"), stacked_shadow_defaults.color, &LabelSettings::set_stacked_shadow_color, &LabelSettings::get_stacked_shadow_color);
127 stacked_shadow_base_property_helper.register_property(PropertyInfo(Variant::INT, "outline_size", PROPERTY_HINT_NONE, "0,127,1,or_greater,suffix:px"), stacked_shadow_defaults.outline_size, &LabelSettings::set_stacked_shadow_outline_size, &LabelSettings::get_stacked_shadow_outline_size);
128 PropertyListHelper::register_base_helper(&stacked_shadow_base_property_helper);
129 }
130
131 void LabelSettings::set_line_spacing(real_t p_spacing) {
132 if (line_spacing != p_spacing) {
133 line_spacing = p_spacing;
134 emit_changed();
135 }
136 }
137
138 real_t LabelSettings::get_line_spacing() const {
139 return line_spacing;
140 }
141
142 void LabelSettings::set_paragraph_spacing(real_t p_spacing) {
143 if (paragraph_spacing != p_spacing) {
144 paragraph_spacing = p_spacing;
145 emit_changed();
146 }
147 }
148
149 real_t LabelSettings::get_paragraph_spacing() const {
150 return paragraph_spacing;
151 }
152
153 void LabelSettings::set_font(const Ref<Font> &p_font) {
154 if (font != p_font) {
155 if (font.is_valid()) {
156 font->disconnect_changed(callable_mp(this, &LabelSettings::_font_changed));
157 }
158 font = p_font;
159 if (font.is_valid()) {
160 font->connect_changed(callable_mp(this, &LabelSettings::_font_changed), CONNECT_REFERENCE_COUNTED);
161 }
162 emit_changed();
163 }
164 }
165
166 Ref<Font> LabelSettings::get_font() const {
167 return font;
168 }
169
170 void LabelSettings::set_font_size(int p_size) {
171 if (font_size != p_size) {
172 font_size = p_size;
173 emit_changed();
174 }
175 }
176
177 int LabelSettings::get_font_size() const {
178 return font_size;
179 }
180
181 void LabelSettings::set_font_color(const Color &p_color) {
182 if (font_color != p_color) {
183 font_color = p_color;
184 emit_changed();
185 }
186 }
187
188 Color LabelSettings::get_font_color() const {
189 return font_color;
190 }
191
192 void LabelSettings::set_outline_size(int p_size) {
193 if (outline_size != p_size) {
194 outline_size = p_size;
195 emit_changed();
196 }
197 }
198
199 int LabelSettings::get_outline_size() const {
200 return outline_size;
201 }
202
203 void LabelSettings::set_outline_color(const Color &p_color) {
204 if (outline_color != p_color) {
205 outline_color = p_color;
206 emit_changed();
207 }
208 }
209
210 Color LabelSettings::get_outline_color() const {
211 return outline_color;
212 }
213
214 void LabelSettings::set_shadow_size(int p_size) {
215 if (shadow_size != p_size) {
216 shadow_size = p_size;
217 emit_changed();
218 }
219 }
220
221 int LabelSettings::get_shadow_size() const {
222 return shadow_size;
223 }
224
225 void LabelSettings::set_shadow_color(const Color &p_color) {
226 if (shadow_color != p_color) {
227 shadow_color = p_color;
228 emit_changed();
229 }
230 }
231
232 Color LabelSettings::get_shadow_color() const {
233 return shadow_color;
234 }
235
236 void LabelSettings::set_shadow_offset(const Vector2 &p_offset) {
237 if (shadow_offset != p_offset) {
238 shadow_offset = p_offset;
239 emit_changed();
240 }
241 }
242
243 Vector2 LabelSettings::get_shadow_offset() const {
244 return shadow_offset;
245 }
246
247 Vector<LabelSettings::StackedOutlineData> LabelSettings::get_stacked_outline_data() const {
248 return stacked_outline_data;
249 }
250
251 int LabelSettings::get_stacked_outline_count() const {
252 return stacked_outline_data.size();
253 }
254
255 void LabelSettings::set_stacked_outline_count(int p_count) {
256 ERR_FAIL_COND(p_count < 0);
257 if (stacked_outline_data.size() != p_count) {
258 stacked_outline_data.resize(p_count);
259 notify_property_list_changed();
260 emit_changed();
261 }
262 }
263
264 void LabelSettings::add_stacked_outline(int p_index) {
265 if (p_index < 0) {
266 p_index = stacked_outline_data.size();
267 }
268 ERR_FAIL_INDEX(p_index, stacked_outline_data.size() + 1);
269 stacked_outline_data.insert(p_index, StackedOutlineData());
270 notify_property_list_changed();
271 emit_changed();
272 }
273
274 void LabelSettings::move_stacked_outline(int p_from_index, int p_to_position) {
275 ERR_FAIL_INDEX(p_from_index, stacked_outline_data.size());
276 ERR_FAIL_INDEX(p_to_position, stacked_outline_data.size() + 1);
277 stacked_outline_data.insert(p_to_position, stacked_outline_data[p_from_index]);
278 stacked_outline_data.remove_at(p_to_position < p_from_index ? p_from_index + 1 : p_from_index);
279 notify_property_list_changed();
280 emit_changed();
281 }
282
283 void LabelSettings::remove_stacked_outline(int p_index) {
284 ERR_FAIL_INDEX(p_index, stacked_outline_data.size());
285 stacked_outline_data.remove_at(p_index);
286 notify_property_list_changed();
287 emit_changed();
288 }
289
290 void LabelSettings::set_stacked_outline_size(int p_index, int p_size) {
291 ERR_FAIL_INDEX(p_index, stacked_outline_data.size());
292 if (stacked_outline_data[p_index].size != p_size) {
293 stacked_outline_data.write[p_index].size = p_size;
294 emit_changed();
295 }
296 }
297
298 int LabelSettings::get_stacked_outline_size(int p_index) const {
299 ERR_FAIL_INDEX_V(p_index, stacked_outline_data.size(), 0);
300 return stacked_outline_data[p_index].size;
301 }
302
303 void LabelSettings::set_stacked_outline_color(int p_index, const Color &p_color) {
304 ERR_FAIL_INDEX(p_index, stacked_outline_data.size());
305 if (stacked_outline_data[p_index].color != p_color) {
306 stacked_outline_data.write[p_index].color = p_color;
307 emit_changed();
308 }
309 }
310
311 Color LabelSettings::get_stacked_outline_color(int p_index) const {
312 ERR_FAIL_INDEX_V(p_index, stacked_outline_data.size(), Color());
313 return stacked_outline_data[p_index].color;
314 }
315
316 Vector<LabelSettings::StackedShadowData> LabelSettings::get_stacked_shadow_data() const {
317 return stacked_shadow_data;
318 }
319
320 int LabelSettings::get_stacked_shadow_count() const {
321 return stacked_shadow_data.size();
322 }
323
324 void LabelSettings::set_stacked_shadow_count(int p_count) {
325 ERR_FAIL_COND(p_count < 0);
326 if (stacked_shadow_data.size() != p_count) {
327 stacked_shadow_data.resize(p_count);
328 notify_property_list_changed();
329 emit_changed();
330 }
331 }
332
333 void LabelSettings::add_stacked_shadow(int p_index) {
334 if (p_index < 0) {
335 p_index = stacked_shadow_data.size();
336 }
337 ERR_FAIL_INDEX(p_index, stacked_shadow_data.size() + 1);
338 stacked_shadow_data.insert(p_index, StackedShadowData());
339 notify_property_list_changed();
340 emit_changed();
341 }
342
343 void LabelSettings::move_stacked_shadow(int p_from_index, int p_to_position) {
344 ERR_FAIL_INDEX(p_from_index, stacked_shadow_data.size());
345 ERR_FAIL_INDEX(p_to_position, stacked_shadow_data.size() + 1);
346 stacked_shadow_data.insert(p_to_position, stacked_shadow_data[p_from_index]);
347 stacked_shadow_data.remove_at(p_to_position < p_from_index ? p_from_index + 1 : p_from_index);
348 notify_property_list_changed();
349 emit_changed();
350 }
351
352 void LabelSettings::remove_stacked_shadow(int p_index) {
353 ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
354 stacked_shadow_data.remove_at(p_index);
355 notify_property_list_changed();
356 emit_changed();
357 }
358
359 void LabelSettings::set_stacked_shadow_offset(int p_index, const Vector2 &p_offset) {
360 ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
361 if (stacked_shadow_data[p_index].offset != p_offset) {
362 stacked_shadow_data.write[p_index].offset = p_offset;
363 emit_changed();
364 }
365 }
366
367 Vector2 LabelSettings::get_stacked_shadow_offset(int p_index) const {
368 ERR_FAIL_INDEX_V(p_index, stacked_shadow_data.size(), Vector2());
369 return stacked_shadow_data[p_index].offset;
370 }
371
372 void LabelSettings::set_stacked_shadow_color(int p_index, const Color &p_color) {
373 ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
374 if (stacked_shadow_data[p_index].color != p_color) {
375 stacked_shadow_data.write[p_index].color = p_color;
376 emit_changed();
377 }
378 }
379
380 Color LabelSettings::get_stacked_shadow_color(int p_index) const {
381 ERR_FAIL_INDEX_V(p_index, stacked_shadow_data.size(), Color());
382 return stacked_shadow_data[p_index].color;
383 }
384
385 void LabelSettings::set_stacked_shadow_outline_size(int p_index, int p_size) {
386 ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
387 if (stacked_shadow_data[p_index].outline_size != p_size) {
388 stacked_shadow_data.write[p_index].outline_size = p_size;
389 emit_changed();
390 }
391 }
392
393 int LabelSettings::get_stacked_shadow_outline_size(int p_index) const {
394 ERR_FAIL_INDEX_V(p_index, stacked_shadow_data.size(), 0);
395 return stacked_shadow_data[p_index].outline_size;
396 }