Debian Codegraph @

scene/resources/theme.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* theme.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 "theme.h"
32
33 #include "scene/theme/theme_db.h"
34
35 // Dynamic properties.
36 bool Theme::_set(const StringName &p_name, const Variant &p_value) {
37 String sname = p_name;
38
39 if (sname.contains_char('/')) {
40 String type = sname.get_slicec('/', 1);
41 String theme_type = sname.get_slicec('/', 0);
42 String prop_name = sname.get_slicec('/', 2);
43
44 if (type == "icons") {
45 set_icon(prop_name, theme_type, p_value);
46 } else if (type == "styles") {
47 set_stylebox(prop_name, theme_type, p_value);
48 } else if (type == "fonts") {
49 set_font(prop_name, theme_type, p_value);
50 } else if (type == "font_sizes") {
51 set_font_size(prop_name, theme_type, p_value);
52 } else if (type == "colors") {
53 set_color(prop_name, theme_type, p_value);
54 } else if (type == "constants") {
55 set_constant(prop_name, theme_type, p_value);
56 } else if (type == "base_type") {
57 set_type_variation(theme_type, p_value);
58 } else {
59 return false;
60 }
61
62 return true;
63 }
64
65 return false;
66 }
67
68 bool Theme::_get(const StringName &p_name, Variant &r_ret) const {
69 String sname = p_name;
70
71 if (sname.contains_char('/')) {
72 String type = sname.get_slicec('/', 1);
73 String theme_type = sname.get_slicec('/', 0);
74 String prop_name = sname.get_slicec('/', 2);
75
76 if (type == "icons") {
77 if (!has_icon(prop_name, theme_type)) {
78 r_ret = Ref<Texture2D>();
79 } else {
80 r_ret = get_icon(prop_name, theme_type);
81 }
82 } else if (type == "styles") {
83 if (!has_stylebox(prop_name, theme_type)) {
84 r_ret = Ref<StyleBox>();
85 } else {
86 r_ret = get_stylebox(prop_name, theme_type);
87 }
88 } else if (type == "fonts") {
89 if (!has_font(prop_name, theme_type)) {
90 r_ret = Ref<Font>();
91 } else {
92 r_ret = get_font(prop_name, theme_type);
93 }
94 } else if (type == "font_sizes") {
95 r_ret = get_font_size(prop_name, theme_type);
96 } else if (type == "colors") {
97 r_ret = get_color(prop_name, theme_type);
98 } else if (type == "constants") {
99 r_ret = get_constant(prop_name, theme_type);
100 } else if (type == "base_type") {
101 r_ret = get_type_variation_base(theme_type);
102 } else {
103 return false;
104 }
105
106 return true;
107 }
108
109 return false;
110 }
111
112 void Theme::_get_property_list(List<PropertyInfo> *p_list) const {
113 List<PropertyInfo> list;
114
115 // Type variations.
116 for (const KeyValue<StringName, StringName> &E : variation_map) {
117 list.push_back(PropertyInfo(Variant::STRING_NAME, String() + E.key + "/base_type"));
118 }
119
120 // Icons.
121 for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
122 for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
123 list.push_back(PropertyInfo(Variant::OBJECT, String() + E.key + "/icons/" + F.key, PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
124 }
125 }
126
127 // Styles.
128 for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
129 for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
130 list.push_back(PropertyInfo(Variant::OBJECT, String() + E.key + "/styles/" + F.key, PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
131 }
132 }
133
134 // Fonts.
135 for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
136 for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
137 list.push_back(PropertyInfo(Variant::OBJECT, String() + E.key + "/fonts/" + F.key, PROPERTY_HINT_RESOURCE_TYPE, "Font", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_STORE_IF_NULL));
138 }
139 }
140
141 // Font sizes.
142 for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
143 for (const KeyValue<StringName, int> &F : E.value) {
144 list.push_back(PropertyInfo(Variant::INT, String() + E.key + "/font_sizes/" + F.key, PROPERTY_HINT_RANGE, "0,256,1,or_greater,suffix:px"));
145 }
146 }
147
148 // Colors.
149 for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
150 for (const KeyValue<StringName, Color> &F : E.value) {
151 list.push_back(PropertyInfo(Variant::COLOR, String() + E.key + "/colors/" + F.key));
152 }
153 }
154
155 // Constants.
156 for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
157 for (const KeyValue<StringName, int> &F : E.value) {
158 list.push_back(PropertyInfo(Variant::INT, String() + E.key + "/constants/" + F.key));
159 }
160 }
161
162 // Sort and store properties.
163 list.sort();
164 String prev_type;
165 for (const PropertyInfo &E : list) {
166 // Add groups for types so that their names are left unchanged in the inspector.
167 String current_type = E.name.get_slicec('/', 0);
168 if (prev_type != current_type) {
169 p_list->push_back(PropertyInfo(Variant::NIL, current_type, PROPERTY_HINT_NONE, current_type + "/", PROPERTY_USAGE_GROUP));
170 prev_type = current_type;
171 }
172
173 p_list->push_back(E);
174 }
175 }
176
177 // Static helpers.
178 bool Theme::is_valid_type_name(const String &p_name) {
179 int len = p_name.length();
180 const char32_t *str = p_name.ptr();
181 for (int i = 0; i < len; i++) {
182 if (!is_ascii_identifier_char(str[i])) {
183 return false;
184 }
185 }
186 return true;
187 }
188
189 bool Theme::is_valid_item_name(const String &p_name) {
190 if (p_name.is_empty()) {
191 return false;
192 }
193 int len = p_name.length();
194 const char32_t *str = p_name.ptr();
195 for (int i = 0; i < len; i++) {
196 if (!is_ascii_identifier_char(str[i])) {
197 return false;
198 }
199 }
200 return true;
201 }
202
203 String Theme::validate_type_name(const String &p_name) {
204 String type_name = p_name.strip_edges();
205 int len = type_name.length();
206 char32_t *buffer = type_name.ptrw();
207 for (int i = 0; i < len; i++) {
208 if (!is_ascii_identifier_char(buffer[i])) {
209 buffer[i] = '_';
210 }
211 }
212 return type_name;
213 }
214
215 // Fallback values for theme item types, configurable per theme.
216 void Theme::set_default_base_scale(float p_base_scale) {
217 if (default_base_scale == p_base_scale) {
218 return;
219 }
220
221 default_base_scale = p_base_scale;
222
223 _emit_theme_changed();
224 }
225
226 float Theme::get_default_base_scale() const {
227 return default_base_scale;
228 }
229
230 bool Theme::has_default_base_scale() const {
231 return default_base_scale > 0.0;
232 }
233
234 void Theme::set_default_font(const Ref<Font> &p_default_font) {
235 if (default_font == p_default_font) {
236 return;
237 }
238
239 if (default_font.is_valid()) {
240 default_font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
241 }
242
243 default_font = p_default_font;
244
245 if (default_font.is_valid()) {
246 default_font->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
247 }
248
249 _emit_theme_changed();
250 }
251
252 Ref<Font> Theme::get_default_font() const {
253 return default_font;
254 }
255
256 bool Theme::has_default_font() const {
257 return default_font.is_valid();
258 }
259
260 void Theme::set_default_font_size(int p_font_size) {
261 if (default_font_size == p_font_size) {
262 return;
263 }
264
265 default_font_size = p_font_size;
266
267 _emit_theme_changed();
268 }
269
270 int Theme::get_default_font_size() const {
271 return default_font_size;
272 }
273
274 bool Theme::has_default_font_size() const {
275 return default_font_size > 0;
276 }
277
278 // Icons.
279 void Theme::set_icon(const StringName &p_name, const StringName &p_theme_type, const Ref<Texture2D> &p_icon) {
280 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
281 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
282
283 bool existing = false;
284 if (icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid()) {
285 existing = true;
286 icon_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
287 }
288
289 icon_map[p_theme_type][p_name] = p_icon;
290
291 if (p_icon.is_valid()) {
292 icon_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
293 }
294
295 _emit_theme_changed(!existing);
296 }
297
298 Ref<Texture2D> Theme::get_icon(const StringName &p_name, const StringName &p_theme_type) const {
299 if (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid()) {
300 return icon_map[p_theme_type][p_name];
301 } else {
302 return ThemeDB::get_singleton()->get_fallback_icon();
303 }
304 }
305
306 bool Theme::has_icon(const StringName &p_name, const StringName &p_theme_type) const {
307 return (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid());
308 }
309
310 bool Theme::has_icon_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
311 return (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name));
312 }
313
314 void Theme::rename_icon(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
315 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
316 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
317 ERR_FAIL_COND_MSG(!icon_map.has(p_theme_type), "Cannot rename the icon '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
318 ERR_FAIL_COND_MSG(icon_map[p_theme_type].has(p_name), "Cannot rename the icon '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
319 ERR_FAIL_COND_MSG(!icon_map[p_theme_type].has(p_old_name), "Cannot rename the icon '" + String(p_old_name) + "' because it does not exist.");
320
321 icon_map[p_theme_type][p_name] = icon_map[p_theme_type][p_old_name];
322 icon_map[p_theme_type].erase(p_old_name);
323
324 _emit_theme_changed(true);
325 }
326
327 void Theme::clear_icon(const StringName &p_name, const StringName &p_theme_type) {
328 ERR_FAIL_COND_MSG(!icon_map.has(p_theme_type), "Cannot clear the icon '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
329 ERR_FAIL_COND_MSG(!icon_map[p_theme_type].has(p_name), "Cannot clear the icon '" + String(p_name) + "' because it does not exist.");
330
331 if (icon_map[p_theme_type][p_name].is_valid()) {
332 icon_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
333 }
334
335 icon_map[p_theme_type].erase(p_name);
336
337 _emit_theme_changed(true);
338 }
339
340 void Theme::get_icon_list(const StringName &p_theme_type, List<StringName> *p_list) const {
341 ERR_FAIL_NULL(p_list);
342
343 if (!icon_map.has(p_theme_type)) {
344 return;
345 }
346
347 for (const KeyValue<StringName, Ref<Texture2D>> &E : icon_map[p_theme_type]) {
348 p_list->push_back(E.key);
349 }
350 }
351
352 void Theme::add_icon_type(const StringName &p_theme_type) {
353 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
354
355 if (icon_map.has(p_theme_type)) {
356 return;
357 }
358 icon_map[p_theme_type] = ThemeIconMap();
359 }
360
361 void Theme::remove_icon_type(const StringName &p_theme_type) {
362 if (!icon_map.has(p_theme_type)) {
363 return;
364 }
365
366 _freeze_change_propagation();
367
368 for (const KeyValue<StringName, Ref<Texture2D>> &E : icon_map[p_theme_type]) {
369 Ref<Texture2D> icon = E.value;
370 if (icon.is_valid()) {
371 icon->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
372 }
373 }
374
375 icon_map.erase(p_theme_type);
376
377 _unfreeze_and_propagate_changes();
378 }
379
380 void Theme::rename_icon_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
381 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
382
383 if (!icon_map.has(p_old_theme_type) || icon_map.has(p_theme_type)) {
384 return;
385 }
386
387 icon_map[p_theme_type] = icon_map[p_old_theme_type];
388 icon_map.erase(p_old_theme_type);
389 }
390
391 void Theme::get_icon_type_list(List<StringName> *p_list) const {
392 ERR_FAIL_NULL(p_list);
393
394 for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
395 p_list->push_back(E.key);
396 }
397 }
398
399 // Styleboxes.
400 void Theme::set_stylebox(const StringName &p_name, const StringName &p_theme_type, const Ref<StyleBox> &p_style) {
401 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
402 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
403
404 bool existing = false;
405 if (style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) {
406 existing = true;
407 style_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
408 }
409
410 style_map[p_theme_type][p_name] = p_style;
411
412 if (p_style.is_valid()) {
413 style_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
414 }
415
416 _emit_theme_changed(!existing);
417 }
418
419 Ref<StyleBox> Theme::get_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
420 if (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) {
421 return style_map[p_theme_type][p_name];
422 } else {
423 return ThemeDB::get_singleton()->get_fallback_stylebox();
424 }
425 }
426
427 bool Theme::has_stylebox(const StringName &p_name, const StringName &p_theme_type) const {
428 return (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid());
429 }
430
431 bool Theme::has_stylebox_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
432 return (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name));
433 }
434
435 void Theme::rename_stylebox(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
436 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
437 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
438 ERR_FAIL_COND_MSG(!style_map.has(p_theme_type), "Cannot rename the stylebox '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
439 ERR_FAIL_COND_MSG(style_map[p_theme_type].has(p_name), "Cannot rename the stylebox '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
440 ERR_FAIL_COND_MSG(!style_map[p_theme_type].has(p_old_name), "Cannot rename the stylebox '" + String(p_old_name) + "' because it does not exist.");
441
442 style_map[p_theme_type][p_name] = style_map[p_theme_type][p_old_name];
443 style_map[p_theme_type].erase(p_old_name);
444
445 _emit_theme_changed(true);
446 }
447
448 void Theme::clear_stylebox(const StringName &p_name, const StringName &p_theme_type) {
449 ERR_FAIL_COND_MSG(!style_map.has(p_theme_type), "Cannot clear the stylebox '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
450 ERR_FAIL_COND_MSG(!style_map[p_theme_type].has(p_name), "Cannot clear the stylebox '" + String(p_name) + "' because it does not exist.");
451
452 if (style_map[p_theme_type][p_name].is_valid()) {
453 style_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
454 }
455
456 style_map[p_theme_type].erase(p_name);
457
458 _emit_theme_changed(true);
459 }
460
461 void Theme::get_stylebox_list(const StringName &p_theme_type, List<StringName> *p_list) const {
462 ERR_FAIL_NULL(p_list);
463
464 if (!style_map.has(p_theme_type)) {
465 return;
466 }
467
468 for (const KeyValue<StringName, Ref<StyleBox>> &E : style_map[p_theme_type]) {
469 p_list->push_back(E.key);
470 }
471 }
472
473 void Theme::add_stylebox_type(const StringName &p_theme_type) {
474 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
475
476 if (style_map.has(p_theme_type)) {
477 return;
478 }
479 style_map[p_theme_type] = ThemeStyleMap();
480 }
481
482 void Theme::remove_stylebox_type(const StringName &p_theme_type) {
483 if (!style_map.has(p_theme_type)) {
484 return;
485 }
486
487 _freeze_change_propagation();
488
489 for (const KeyValue<StringName, Ref<StyleBox>> &E : style_map[p_theme_type]) {
490 Ref<StyleBox> style = E.value;
491 if (style.is_valid()) {
492 style->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
493 }
494 }
495
496 style_map.erase(p_theme_type);
497
498 _unfreeze_and_propagate_changes();
499 }
500
501 void Theme::rename_stylebox_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
502 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
503
504 if (!style_map.has(p_old_theme_type) || style_map.has(p_theme_type)) {
505 return;
506 }
507
508 style_map[p_theme_type] = style_map[p_old_theme_type];
509 style_map.erase(p_old_theme_type);
510 }
511
512 void Theme::get_stylebox_type_list(List<StringName> *p_list) const {
513 ERR_FAIL_NULL(p_list);
514
515 for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
516 p_list->push_back(E.key);
517 }
518 }
519
520 // Fonts.
521 void Theme::set_font(const StringName &p_name, const StringName &p_theme_type, const Ref<Font> &p_font) {
522 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
523 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
524
525 bool existing = false;
526 if (font_map[p_theme_type][p_name].is_valid()) {
527 existing = true;
528 font_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
529 }
530
531 font_map[p_theme_type][p_name] = p_font;
532
533 if (p_font.is_valid()) {
534 font_map[p_theme_type][p_name]->connect_changed(callable_mp(this, &Theme::_emit_theme_changed).bind(false), CONNECT_REFERENCE_COUNTED);
535 }
536
537 _emit_theme_changed(!existing);
538 }
539
540 Ref<Font> Theme::get_font(const StringName &p_name, const StringName &p_theme_type) const {
541 if (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) {
542 return font_map[p_theme_type][p_name];
543 } else if (has_default_font()) {
544 return default_font;
545 } else {
546 return ThemeDB::get_singleton()->get_fallback_font();
547 }
548 }
549
550 bool Theme::has_font(const StringName &p_name, const StringName &p_theme_type) const {
551 return ((font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) || has_default_font());
552 }
553
554 bool Theme::has_font_no_default(const StringName &p_name, const StringName &p_theme_type) const {
555 return (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid());
556 }
557
558 bool Theme::has_font_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
559 return (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name));
560 }
561
562 void Theme::rename_font(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
563 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
564 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
565 ERR_FAIL_COND_MSG(!font_map.has(p_theme_type), "Cannot rename the font '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
566 ERR_FAIL_COND_MSG(font_map[p_theme_type].has(p_name), "Cannot rename the font '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
567 ERR_FAIL_COND_MSG(!font_map[p_theme_type].has(p_old_name), "Cannot rename the font '" + String(p_old_name) + "' because it does not exist.");
568
569 font_map[p_theme_type][p_name] = font_map[p_theme_type][p_old_name];
570 font_map[p_theme_type].erase(p_old_name);
571
572 _emit_theme_changed(true);
573 }
574
575 void Theme::clear_font(const StringName &p_name, const StringName &p_theme_type) {
576 ERR_FAIL_COND_MSG(!font_map.has(p_theme_type), "Cannot clear the font '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
577 ERR_FAIL_COND_MSG(!font_map[p_theme_type].has(p_name), "Cannot clear the font '" + String(p_name) + "' because it does not exist.");
578
579 if (font_map[p_theme_type][p_name].is_valid()) {
580 font_map[p_theme_type][p_name]->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
581 }
582
583 font_map[p_theme_type].erase(p_name);
584
585 _emit_theme_changed(true);
586 }
587
588 void Theme::get_font_list(const StringName &p_theme_type, List<StringName> *p_list) const {
589 ERR_FAIL_NULL(p_list);
590
591 if (!font_map.has(p_theme_type)) {
592 return;
593 }
594
595 for (const KeyValue<StringName, Ref<Font>> &E : font_map[p_theme_type]) {
596 p_list->push_back(E.key);
597 }
598 }
599
600 void Theme::add_font_type(const StringName &p_theme_type) {
601 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
602
603 if (font_map.has(p_theme_type)) {
604 return;
605 }
606 font_map[p_theme_type] = ThemeFontMap();
607 }
608
609 void Theme::remove_font_type(const StringName &p_theme_type) {
610 if (!font_map.has(p_theme_type)) {
611 return;
612 }
613
614 _freeze_change_propagation();
615
616 for (const KeyValue<StringName, Ref<Font>> &E : font_map[p_theme_type]) {
617 Ref<Font> font = E.value;
618 if (font.is_valid()) {
619 font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
620 }
621 }
622
623 font_map.erase(p_theme_type);
624
625 _unfreeze_and_propagate_changes();
626 }
627
628 void Theme::rename_font_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
629 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
630
631 if (!font_map.has(p_old_theme_type) || font_map.has(p_theme_type)) {
632 return;
633 }
634
635 font_map[p_theme_type] = font_map[p_old_theme_type];
636 font_map.erase(p_old_theme_type);
637 }
638
639 void Theme::get_font_type_list(List<StringName> *p_list) const {
640 ERR_FAIL_NULL(p_list);
641
642 for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
643 p_list->push_back(E.key);
644 }
645 }
646
647 // Font sizes.
648 void Theme::set_font_size(const StringName &p_name, const StringName &p_theme_type, int p_font_size) {
649 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
650 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
651
652 bool existing = has_font_size_nocheck(p_name, p_theme_type);
653 font_size_map[p_theme_type][p_name] = p_font_size;
654
655 _emit_theme_changed(!existing);
656 }
657
658 int Theme::get_font_size(const StringName &p_name, const StringName &p_theme_type) const {
659 if (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name) && (font_size_map[p_theme_type][p_name] > 0)) {
660 return font_size_map[p_theme_type][p_name];
661 } else if (has_default_font_size()) {
662 return default_font_size;
663 } else {
664 return ThemeDB::get_singleton()->get_fallback_font_size();
665 }
666 }
667
668 bool Theme::has_font_size(const StringName &p_name, const StringName &p_theme_type) const {
669 return ((font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name) && (font_size_map[p_theme_type][p_name] > 0)) || has_default_font_size());
670 }
671
672 bool Theme::has_font_size_no_default(const StringName &p_name, const StringName &p_theme_type) const {
673 return (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name) && (font_size_map[p_theme_type][p_name] > 0));
674 }
675
676 bool Theme::has_font_size_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
677 return (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name));
678 }
679
680 void Theme::rename_font_size(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
681 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
682 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
683 ERR_FAIL_COND_MSG(!font_size_map.has(p_theme_type), "Cannot rename the font size '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
684 ERR_FAIL_COND_MSG(font_size_map[p_theme_type].has(p_name), "Cannot rename the font size '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
685 ERR_FAIL_COND_MSG(!font_size_map[p_theme_type].has(p_old_name), "Cannot rename the font size '" + String(p_old_name) + "' because it does not exist.");
686
687 font_size_map[p_theme_type][p_name] = font_size_map[p_theme_type][p_old_name];
688 font_size_map[p_theme_type].erase(p_old_name);
689
690 _emit_theme_changed(true);
691 }
692
693 void Theme::clear_font_size(const StringName &p_name, const StringName &p_theme_type) {
694 ERR_FAIL_COND_MSG(!font_size_map.has(p_theme_type), "Cannot clear the font size '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
695 ERR_FAIL_COND_MSG(!font_size_map[p_theme_type].has(p_name), "Cannot clear the font size '" + String(p_name) + "' because it does not exist.");
696
697 font_size_map[p_theme_type].erase(p_name);
698
699 _emit_theme_changed(true);
700 }
701
702 void Theme::get_font_size_list(const StringName &p_theme_type, List<StringName> *p_list) const {
703 ERR_FAIL_NULL(p_list);
704
705 if (!font_size_map.has(p_theme_type)) {
706 return;
707 }
708
709 for (const KeyValue<StringName, int> &E : font_size_map[p_theme_type]) {
710 p_list->push_back(E.key);
711 }
712 }
713
714 void Theme::add_font_size_type(const StringName &p_theme_type) {
715 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
716
717 if (font_size_map.has(p_theme_type)) {
718 return;
719 }
720 font_size_map[p_theme_type] = ThemeFontSizeMap();
721 }
722
723 void Theme::remove_font_size_type(const StringName &p_theme_type) {
724 if (!font_size_map.has(p_theme_type)) {
725 return;
726 }
727
728 font_size_map.erase(p_theme_type);
729 }
730
731 void Theme::rename_font_size_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
732 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
733
734 if (!font_size_map.has(p_old_theme_type) || font_size_map.has(p_theme_type)) {
735 return;
736 }
737
738 font_size_map[p_theme_type] = font_size_map[p_old_theme_type];
739 font_size_map.erase(p_old_theme_type);
740 }
741
742 void Theme::get_font_size_type_list(List<StringName> *p_list) const {
743 ERR_FAIL_NULL(p_list);
744
745 for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
746 p_list->push_back(E.key);
747 }
748 }
749
750 // Colors.
751 void Theme::set_color(const StringName &p_name, const StringName &p_theme_type, const Color &p_color) {
752 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
753 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
754
755 bool existing = has_color_nocheck(p_name, p_theme_type);
756 color_map[p_theme_type][p_name] = p_color;
757
758 _emit_theme_changed(!existing);
759 }
760
761 Color Theme::get_color(const StringName &p_name, const StringName &p_theme_type) const {
762 if (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name)) {
763 return color_map[p_theme_type][p_name];
764 } else {
765 return Color();
766 }
767 }
768
769 bool Theme::has_color(const StringName &p_name, const StringName &p_theme_type) const {
770 return (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name));
771 }
772
773 bool Theme::has_color_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
774 return (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name));
775 }
776
777 void Theme::rename_color(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
778 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
779 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
780 ERR_FAIL_COND_MSG(!color_map.has(p_theme_type), "Cannot rename the color '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
781 ERR_FAIL_COND_MSG(color_map[p_theme_type].has(p_name), "Cannot rename the color '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
782 ERR_FAIL_COND_MSG(!color_map[p_theme_type].has(p_old_name), "Cannot rename the color '" + String(p_old_name) + "' because it does not exist.");
783
784 color_map[p_theme_type][p_name] = color_map[p_theme_type][p_old_name];
785 color_map[p_theme_type].erase(p_old_name);
786
787 _emit_theme_changed(true);
788 }
789
790 void Theme::clear_color(const StringName &p_name, const StringName &p_theme_type) {
791 ERR_FAIL_COND_MSG(!color_map.has(p_theme_type), "Cannot clear the color '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
792 ERR_FAIL_COND_MSG(!color_map[p_theme_type].has(p_name), "Cannot clear the color '" + String(p_name) + "' because it does not exist.");
793
794 color_map[p_theme_type].erase(p_name);
795
796 _emit_theme_changed(true);
797 }
798
799 void Theme::get_color_list(const StringName &p_theme_type, List<StringName> *p_list) const {
800 ERR_FAIL_NULL(p_list);
801
802 if (!color_map.has(p_theme_type)) {
803 return;
804 }
805
806 for (const KeyValue<StringName, Color> &E : color_map[p_theme_type]) {
807 p_list->push_back(E.key);
808 }
809 }
810
811 void Theme::add_color_type(const StringName &p_theme_type) {
812 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
813
814 if (color_map.has(p_theme_type)) {
815 return;
816 }
817 color_map[p_theme_type] = ThemeColorMap();
818 }
819
820 void Theme::remove_color_type(const StringName &p_theme_type) {
821 if (!color_map.has(p_theme_type)) {
822 return;
823 }
824
825 color_map.erase(p_theme_type);
826 }
827
828 void Theme::rename_color_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
829 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
830
831 if (!color_map.has(p_old_theme_type) || color_map.has(p_theme_type)) {
832 return;
833 }
834
835 color_map[p_theme_type] = color_map[p_old_theme_type];
836 color_map.erase(p_old_theme_type);
837 }
838
839 void Theme::get_color_type_list(List<StringName> *p_list) const {
840 ERR_FAIL_NULL(p_list);
841
842 for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
843 p_list->push_back(E.key);
844 }
845 }
846
847 // Theme constants.
848 void Theme::set_constant(const StringName &p_name, const StringName &p_theme_type, int p_constant) {
849 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
850 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
851
852 bool existing = has_constant_nocheck(p_name, p_theme_type);
853 constant_map[p_theme_type][p_name] = p_constant;
854
855 _emit_theme_changed(!existing);
856 }
857
858 int Theme::get_constant(const StringName &p_name, const StringName &p_theme_type) const {
859 if (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name)) {
860 return constant_map[p_theme_type][p_name];
861 } else {
862 return 0;
863 }
864 }
865
866 bool Theme::has_constant(const StringName &p_name, const StringName &p_theme_type) const {
867 return (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name));
868 }
869
870 bool Theme::has_constant_nocheck(const StringName &p_name, const StringName &p_theme_type) const {
871 return (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name));
872 }
873
874 void Theme::rename_constant(const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
875 ERR_FAIL_COND_MSG(!is_valid_item_name(p_name), vformat("Invalid item name: '%s'", p_name));
876 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
877 ERR_FAIL_COND_MSG(!constant_map.has(p_theme_type), "Cannot rename the constant '" + String(p_old_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
878 ERR_FAIL_COND_MSG(constant_map[p_theme_type].has(p_name), "Cannot rename the constant '" + String(p_old_name) + "' because the new name '" + String(p_name) + "' already exists.");
879 ERR_FAIL_COND_MSG(!constant_map[p_theme_type].has(p_old_name), "Cannot rename the constant '" + String(p_old_name) + "' because it does not exist.");
880
881 constant_map[p_theme_type][p_name] = constant_map[p_theme_type][p_old_name];
882 constant_map[p_theme_type].erase(p_old_name);
883
884 _emit_theme_changed(true);
885 }
886
887 void Theme::clear_constant(const StringName &p_name, const StringName &p_theme_type) {
888 ERR_FAIL_COND_MSG(!constant_map.has(p_theme_type), "Cannot clear the constant '" + String(p_name) + "' because the node type '" + String(p_theme_type) + "' does not exist.");
889 ERR_FAIL_COND_MSG(!constant_map[p_theme_type].has(p_name), "Cannot clear the constant '" + String(p_name) + "' because it does not exist.");
890
891 constant_map[p_theme_type].erase(p_name);
892
893 _emit_theme_changed(true);
894 }
895
896 void Theme::get_constant_list(const StringName &p_theme_type, List<StringName> *p_list) const {
897 ERR_FAIL_NULL(p_list);
898
899 if (!constant_map.has(p_theme_type)) {
900 return;
901 }
902
903 for (const KeyValue<StringName, int> &E : constant_map[p_theme_type]) {
904 p_list->push_back(E.key);
905 }
906 }
907
908 void Theme::add_constant_type(const StringName &p_theme_type) {
909 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
910
911 if (constant_map.has(p_theme_type)) {
912 return;
913 }
914 constant_map[p_theme_type] = ThemeConstantMap();
915 }
916
917 void Theme::remove_constant_type(const StringName &p_theme_type) {
918 if (!constant_map.has(p_theme_type)) {
919 return;
920 }
921
922 constant_map.erase(p_theme_type);
923 }
924
925 void Theme::rename_constant_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
926 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
927
928 if (!constant_map.has(p_old_theme_type) || constant_map.has(p_theme_type)) {
929 return;
930 }
931
932 constant_map[p_theme_type] = constant_map[p_old_theme_type];
933 constant_map.erase(p_old_theme_type);
934 }
935
936 void Theme::get_constant_type_list(List<StringName> *p_list) const {
937 ERR_FAIL_NULL(p_list);
938
939 for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
940 p_list->push_back(E.key);
941 }
942 }
943
944 // Generic methods for managing theme items.
945 void Theme::set_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type, const Variant &p_value) {
946 switch (p_data_type) {
947 case DATA_TYPE_COLOR: {
948 ERR_FAIL_COND_MSG(p_value.get_type() != Variant::COLOR, "Theme item's data type (Color) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
949
950 Color color_value = p_value;
951 set_color(p_name, p_theme_type, color_value);
952 } break;
953 case DATA_TYPE_CONSTANT: {
954 ERR_FAIL_COND_MSG(p_value.get_type() != Variant::INT, "Theme item's data type (int) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
955
956 int constant_value = p_value;
957 set_constant(p_name, p_theme_type, constant_value);
958 } break;
959 case DATA_TYPE_FONT: {
960 ERR_FAIL_COND_MSG(p_value.get_type() != Variant::OBJECT, "Theme item's data type (Object) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
961
962 Ref<Font> font_value = Object::cast_to<Font>(p_value.get_validated_object());
963 set_font(p_name, p_theme_type, font_value);
964 } break;
965 case DATA_TYPE_FONT_SIZE: {
966 ERR_FAIL_COND_MSG(p_value.get_type() != Variant::INT, "Theme item's data type (int) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
967
968 int font_size_value = p_value;
969 set_font_size(p_name, p_theme_type, font_size_value);
970 } break;
971 case DATA_TYPE_ICON: {
972 ERR_FAIL_COND_MSG(p_value.get_type() != Variant::OBJECT, "Theme item's data type (Object) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
973
974 Ref<Texture2D> icon_value = Object::cast_to<Texture2D>(p_value.get_validated_object());
975 set_icon(p_name, p_theme_type, icon_value);
976 } break;
977 case DATA_TYPE_STYLEBOX: {
978 ERR_FAIL_COND_MSG(p_value.get_type() != Variant::OBJECT, "Theme item's data type (Object) does not match Variant's type (" + Variant::get_type_name(p_value.get_type()) + ").");
979
980 Ref<StyleBox> stylebox_value = Object::cast_to<StyleBox>(p_value.get_validated_object());
981 set_stylebox(p_name, p_theme_type, stylebox_value);
982 } break;
983 case DATA_TYPE_MAX:
984 break; // Can't happen, but silences warning.
985 }
986 }
987
988 Variant Theme::get_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
989 switch (p_data_type) {
990 case DATA_TYPE_COLOR:
991 return get_color(p_name, p_theme_type);
992 case DATA_TYPE_CONSTANT:
993 return get_constant(p_name, p_theme_type);
994 case DATA_TYPE_FONT:
995 return get_font(p_name, p_theme_type);
996 case DATA_TYPE_FONT_SIZE:
997 return get_font_size(p_name, p_theme_type);
998 case DATA_TYPE_ICON:
999 return get_icon(p_name, p_theme_type);
1000 case DATA_TYPE_STYLEBOX:
1001 return get_stylebox(p_name, p_theme_type);
1002 case DATA_TYPE_MAX:
1003 break; // Can't happen, but silences warning.
1004 }
1005
1006 return Variant();
1007 }
1008
1009 bool Theme::has_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
1010 switch (p_data_type) {
1011 case DATA_TYPE_COLOR:
1012 return has_color(p_name, p_theme_type);
1013 case DATA_TYPE_CONSTANT:
1014 return has_constant(p_name, p_theme_type);
1015 case DATA_TYPE_FONT:
1016 if (!variation_map.has(p_theme_type)) {
1017 return has_font(p_name, p_theme_type);
1018 } else {
1019 return has_font_no_default(p_name, p_theme_type);
1020 }
1021 case DATA_TYPE_FONT_SIZE:
1022 if (!variation_map.has(p_theme_type)) {
1023 return has_font_size(p_name, p_theme_type);
1024 } else {
1025 return has_font_size_no_default(p_name, p_theme_type);
1026 }
1027 case DATA_TYPE_ICON:
1028 return has_icon(p_name, p_theme_type);
1029 case DATA_TYPE_STYLEBOX:
1030 return has_stylebox(p_name, p_theme_type);
1031 case DATA_TYPE_MAX:
1032 break; // Can't happen, but silences warning.
1033 }
1034
1035 return false;
1036 }
1037
1038 bool Theme::has_theme_item_nocheck(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) const {
1039 switch (p_data_type) {
1040 case DATA_TYPE_COLOR:
1041 return has_color_nocheck(p_name, p_theme_type);
1042 case DATA_TYPE_CONSTANT:
1043 return has_constant_nocheck(p_name, p_theme_type);
1044 case DATA_TYPE_FONT:
1045 return has_font_nocheck(p_name, p_theme_type);
1046 case DATA_TYPE_FONT_SIZE:
1047 return has_font_size_nocheck(p_name, p_theme_type);
1048 case DATA_TYPE_ICON:
1049 return has_icon_nocheck(p_name, p_theme_type);
1050 case DATA_TYPE_STYLEBOX:
1051 return has_stylebox_nocheck(p_name, p_theme_type);
1052 case DATA_TYPE_MAX:
1053 break; // Can't happen, but silences warning.
1054 }
1055
1056 return false;
1057 }
1058
1059 void Theme::rename_theme_item(DataType p_data_type, const StringName &p_old_name, const StringName &p_name, const StringName &p_theme_type) {
1060 switch (p_data_type) {
1061 case DATA_TYPE_COLOR:
1062 rename_color(p_old_name, p_name, p_theme_type);
1063 break;
1064 case DATA_TYPE_CONSTANT:
1065 rename_constant(p_old_name, p_name, p_theme_type);
1066 break;
1067 case DATA_TYPE_FONT:
1068 rename_font(p_old_name, p_name, p_theme_type);
1069 break;
1070 case DATA_TYPE_FONT_SIZE:
1071 rename_font_size(p_old_name, p_name, p_theme_type);
1072 break;
1073 case DATA_TYPE_ICON:
1074 rename_icon(p_old_name, p_name, p_theme_type);
1075 break;
1076 case DATA_TYPE_STYLEBOX:
1077 rename_stylebox(p_old_name, p_name, p_theme_type);
1078 break;
1079 case DATA_TYPE_MAX:
1080 break; // Can't happen, but silences warning.
1081 }
1082 }
1083
1084 void Theme::clear_theme_item(DataType p_data_type, const StringName &p_name, const StringName &p_theme_type) {
1085 switch (p_data_type) {
1086 case DATA_TYPE_COLOR:
1087 clear_color(p_name, p_theme_type);
1088 break;
1089 case DATA_TYPE_CONSTANT:
1090 clear_constant(p_name, p_theme_type);
1091 break;
1092 case DATA_TYPE_FONT:
1093 clear_font(p_name, p_theme_type);
1094 break;
1095 case DATA_TYPE_FONT_SIZE:
1096 clear_font_size(p_name, p_theme_type);
1097 break;
1098 case DATA_TYPE_ICON:
1099 clear_icon(p_name, p_theme_type);
1100 break;
1101 case DATA_TYPE_STYLEBOX:
1102 clear_stylebox(p_name, p_theme_type);
1103 break;
1104 case DATA_TYPE_MAX:
1105 break; // Can't happen, but silences warning.
1106 }
1107 }
1108
1109 void Theme::get_theme_item_list(DataType p_data_type, const StringName &p_theme_type, List<StringName> *p_list) const {
1110 switch (p_data_type) {
1111 case DATA_TYPE_COLOR:
1112 get_color_list(p_theme_type, p_list);
1113 break;
1114 case DATA_TYPE_CONSTANT:
1115 get_constant_list(p_theme_type, p_list);
1116 break;
1117 case DATA_TYPE_FONT:
1118 get_font_list(p_theme_type, p_list);
1119 break;
1120 case DATA_TYPE_FONT_SIZE:
1121 get_font_size_list(p_theme_type, p_list);
1122 break;
1123 case DATA_TYPE_ICON:
1124 get_icon_list(p_theme_type, p_list);
1125 break;
1126 case DATA_TYPE_STYLEBOX:
1127 get_stylebox_list(p_theme_type, p_list);
1128 break;
1129 case DATA_TYPE_MAX:
1130 break; // Can't happen, but silences warning.
1131 }
1132 }
1133
1134 void Theme::add_theme_item_type(DataType p_data_type, const StringName &p_theme_type) {
1135 switch (p_data_type) {
1136 case DATA_TYPE_COLOR:
1137 add_color_type(p_theme_type);
1138 break;
1139 case DATA_TYPE_CONSTANT:
1140 add_constant_type(p_theme_type);
1141 break;
1142 case DATA_TYPE_FONT:
1143 add_font_type(p_theme_type);
1144 break;
1145 case DATA_TYPE_FONT_SIZE:
1146 add_font_size_type(p_theme_type);
1147 break;
1148 case DATA_TYPE_ICON:
1149 add_icon_type(p_theme_type);
1150 break;
1151 case DATA_TYPE_STYLEBOX:
1152 add_stylebox_type(p_theme_type);
1153 break;
1154 case DATA_TYPE_MAX:
1155 break; // Can't happen, but silences warning.
1156 }
1157 }
1158
1159 void Theme::remove_theme_item_type(DataType p_data_type, const StringName &p_theme_type) {
1160 switch (p_data_type) {
1161 case DATA_TYPE_COLOR:
1162 remove_color_type(p_theme_type);
1163 break;
1164 case DATA_TYPE_CONSTANT:
1165 remove_constant_type(p_theme_type);
1166 break;
1167 case DATA_TYPE_FONT:
1168 remove_font_type(p_theme_type);
1169 break;
1170 case DATA_TYPE_FONT_SIZE:
1171 remove_font_size_type(p_theme_type);
1172 break;
1173 case DATA_TYPE_ICON:
1174 remove_icon_type(p_theme_type);
1175 break;
1176 case DATA_TYPE_STYLEBOX:
1177 remove_stylebox_type(p_theme_type);
1178 break;
1179 case DATA_TYPE_MAX:
1180 break; // Can't happen, but silences warning.
1181 }
1182 }
1183
1184 void Theme::rename_theme_item_type(DataType p_data_type, const StringName &p_old_theme_type, const StringName &p_theme_type) {
1185 switch (p_data_type) {
1186 case DATA_TYPE_COLOR:
1187 rename_color_type(p_old_theme_type, p_theme_type);
1188 break;
1189 case DATA_TYPE_CONSTANT:
1190 rename_constant_type(p_old_theme_type, p_theme_type);
1191 break;
1192 case DATA_TYPE_FONT:
1193 rename_font_type(p_old_theme_type, p_theme_type);
1194 break;
1195 case DATA_TYPE_FONT_SIZE:
1196 rename_font_size_type(p_old_theme_type, p_theme_type);
1197 break;
1198 case DATA_TYPE_ICON:
1199 rename_icon_type(p_old_theme_type, p_theme_type);
1200 break;
1201 case DATA_TYPE_STYLEBOX:
1202 rename_stylebox_type(p_old_theme_type, p_theme_type);
1203 break;
1204 case DATA_TYPE_MAX:
1205 break; // Can't happen, but silences warning.
1206 }
1207 }
1208
1209 void Theme::get_theme_item_type_list(DataType p_data_type, List<StringName> *p_list) const {
1210 switch (p_data_type) {
1211 case DATA_TYPE_COLOR:
1212 get_color_type_list(p_list);
1213 break;
1214 case DATA_TYPE_CONSTANT:
1215 get_constant_type_list(p_list);
1216 break;
1217 case DATA_TYPE_FONT:
1218 get_font_type_list(p_list);
1219 break;
1220 case DATA_TYPE_FONT_SIZE:
1221 get_font_size_type_list(p_list);
1222 break;
1223 case DATA_TYPE_ICON:
1224 get_icon_type_list(p_list);
1225 break;
1226 case DATA_TYPE_STYLEBOX:
1227 get_stylebox_type_list(p_list);
1228 break;
1229 case DATA_TYPE_MAX:
1230 break; // Can't happen, but silences warning.
1231 }
1232 }
1233
1234 // Theme type variations.
1235 void Theme::set_type_variation(const StringName &p_theme_type, const StringName &p_base_type) {
1236 ERR_FAIL_COND_MSG(!is_valid_type_name(p_theme_type), vformat("Invalid type name: '%s'", p_theme_type));
1237 ERR_FAIL_COND_MSG(!is_valid_type_name(p_base_type), vformat("Invalid type name: '%s'", p_base_type));
1238 ERR_FAIL_COND_MSG(p_theme_type == StringName(), vformat("An empty theme type cannot be marked as a variation of another type (\"%s\").", p_base_type));
1239 ERR_FAIL_COND_MSG(ClassDB::class_exists(p_theme_type), vformat("A type associated with a built-in class cannot be marked as a variation of another type (variation: \"%s\", base: \"%s\").", p_theme_type, p_base_type));
1240 ERR_FAIL_COND_MSG(p_base_type == StringName(), "An empty theme type cannot be the base type of a variation. Use clear_type_variation() instead if you want to unmark '" + String(p_theme_type) + "' as a variation.");
1241
1242 if (variation_map.has(p_theme_type)) {
1243 StringName old_base = variation_map[p_theme_type];
1244 variation_base_map[old_base].erase(p_theme_type);
1245 }
1246
1247 variation_map[p_theme_type] = p_base_type;
1248 variation_base_map[p_base_type].push_back(p_theme_type);
1249
1250 _emit_theme_changed(true);
1251 }
1252
1253 bool Theme::is_type_variation(const StringName &p_theme_type, const StringName &p_base_type) const {
1254 return (variation_map.has(p_theme_type) && variation_map[p_theme_type] == p_base_type);
1255 }
1256
1257 void Theme::clear_type_variation(const StringName &p_theme_type) {
1258 ERR_FAIL_COND_MSG(!variation_map.has(p_theme_type), "Cannot clear the type variation '" + String(p_theme_type) + "' because it does not exist.");
1259
1260 StringName base_type = variation_map[p_theme_type];
1261 variation_base_map[base_type].erase(p_theme_type);
1262 variation_map.erase(p_theme_type);
1263
1264 _emit_theme_changed(true);
1265 }
1266
1267 StringName Theme::get_type_variation_base(const StringName &p_theme_type) const {
1268 if (!variation_map.has(p_theme_type)) {
1269 return StringName();
1270 }
1271
1272 return variation_map[p_theme_type];
1273 }
1274
1275 void Theme::get_type_variation_list(const StringName &p_base_type, List<StringName> *p_list) const {
1276 ERR_FAIL_NULL(p_list);
1277
1278 if (!variation_base_map.has(p_base_type)) {
1279 return;
1280 }
1281
1282 for (const StringName &E : variation_base_map[p_base_type]) {
1283 // Prevent infinite loops if variants were set to be cross-dependent (that's still invalid usage, but handling for stability sake).
1284 if (p_list->find(E)) {
1285 continue;
1286 }
1287
1288 p_list->push_back(E);
1289 // Continue looking for sub-variations.
1290 get_type_variation_list(E, p_list);
1291 }
1292 }
1293
1294 // Theme types.
1295 void Theme::add_type(const StringName &p_theme_type) {
1296 // Add a record to every data type map.
1297 for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
1298 Theme::DataType dt = (Theme::DataType)i;
1299 add_theme_item_type(dt, p_theme_type);
1300 }
1301
1302 _emit_theme_changed(true);
1303 }
1304
1305 void Theme::remove_type(const StringName &p_theme_type) {
1306 // Gracefully remove the record from every data type map.
1307 for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
1308 Theme::DataType dt = (Theme::DataType)i;
1309 remove_theme_item_type(dt, p_theme_type);
1310 }
1311
1312 // If type is a variation, remove that connection.
1313 if (get_type_variation_base(p_theme_type) != StringName()) {
1314 clear_type_variation(p_theme_type);
1315 }
1316
1317 // If type is a variation base, remove all those connections.
1318 List<StringName> names;
1319 get_type_variation_list(p_theme_type, &names);
1320 for (const StringName &E : names) {
1321 clear_type_variation(E);
1322 }
1323
1324 _emit_theme_changed(true);
1325 }
1326
1327 void Theme::rename_type(const StringName &p_old_theme_type, const StringName &p_theme_type) {
1328 // Gracefully rename the record in every data type map.
1329 for (int i = 0; i < Theme::DATA_TYPE_MAX; i++) {
1330 Theme::DataType dt = (Theme::DataType)i;
1331 rename_theme_item_type(dt, p_old_theme_type, p_theme_type);
1332 }
1333
1334 // If type is a variation, replace that connection.
1335 const StringName base_type = get_type_variation_base(p_old_theme_type);
1336 if (base_type != StringName()) {
1337 clear_type_variation(p_old_theme_type);
1338 if (p_theme_type != StringName() && !ClassDB::class_exists(p_theme_type)) {
1339 set_type_variation(p_theme_type, base_type);
1340 }
1341 }
1342
1343 // If type is a variation base, replace all those connections.
1344 List<StringName> names;
1345 get_type_variation_list(p_old_theme_type, &names);
1346 for (const StringName &E : names) {
1347 clear_type_variation(E);
1348 if (p_theme_type != StringName()) {
1349 set_type_variation(E, p_theme_type);
1350 }
1351 }
1352
1353 _emit_theme_changed(true);
1354 }
1355
1356 void Theme::get_type_list(List<StringName> *p_list) const {
1357 ERR_FAIL_NULL(p_list);
1358
1359 // This Set guarantees uniqueness.
1360 // Because each map can have the same type defined, but for this method
1361 // we only want one occurrence of each type.
1362 HashSet<StringName> types;
1363
1364 // Icons.
1365 for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
1366 types.insert(E.key);
1367 }
1368
1369 // Styles.
1370 for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
1371 types.insert(E.key);
1372 }
1373
1374 // Fonts.
1375 for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
1376 types.insert(E.key);
1377 }
1378
1379 // Font sizes.
1380 for (const KeyValue<StringName, ThemeFontSizeMap> &E : font_size_map) {
1381 types.insert(E.key);
1382 }
1383
1384 // Colors.
1385 for (const KeyValue<StringName, ThemeColorMap> &E : color_map) {
1386 types.insert(E.key);
1387 }
1388
1389 // Constants.
1390 for (const KeyValue<StringName, ThemeConstantMap> &E : constant_map) {
1391 types.insert(E.key);
1392 }
1393
1394 // Variations.
1395 for (const KeyValue<StringName, StringName> &E : variation_map) {
1396 types.insert(E.key);
1397 }
1398
1399 for (const StringName &E : types) {
1400 p_list->push_back(E);
1401 }
1402 }
1403
1404 void Theme::get_type_dependencies(const StringName &p_base_type, const StringName &p_type_variation, Vector<StringName> &r_result) {
1405 // Build the dependency chain for type variations.
1406 if (p_type_variation != StringName()) {
1407 StringName variation_name = p_type_variation;
1408 while (variation_name != StringName()) {
1409 r_result.push_back(variation_name);
1410 variation_name = get_type_variation_base(variation_name);
1411
1412 // If we have reached the base type dependency, it's safe to stop (assuming no funny business was done to the Theme).
1413 if (variation_name == p_base_type) {
1414 break;
1415 }
1416 }
1417 }
1418
1419 // Continue building the chain using native class hierarchy.
1420 ThemeDB::get_singleton()->get_native_type_dependencies(p_base_type, r_result);
1421 }
1422
1423 // Internal methods for getting lists as a Vector of String (compatible with public API).
1424 Vector<String> Theme::_get_icon_list(const String &p_theme_type) const {
1425 Vector<String> ilret;
1426 List<StringName> il;
1427
1428 get_icon_list(p_theme_type, &il);
1429 ilret.resize(il.size());
1430
1431 int i = 0;
1432 String *w = ilret.ptrw();
1433 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1434 w[i] = E->get();
1435 }
1436 return ilret;
1437 }
1438
1439 Vector<String> Theme::_get_icon_type_list() const {
1440 Vector<String> ilret;
1441 List<StringName> il;
1442
1443 get_icon_type_list(&il);
1444 ilret.resize(il.size());
1445
1446 int i = 0;
1447 String *w = ilret.ptrw();
1448 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1449 w[i] = E->get();
1450 }
1451 return ilret;
1452 }
1453
1454 Vector<String> Theme::_get_stylebox_list(const String &p_theme_type) const {
1455 Vector<String> ilret;
1456 List<StringName> il;
1457
1458 get_stylebox_list(p_theme_type, &il);
1459 ilret.resize(il.size());
1460
1461 int i = 0;
1462 String *w = ilret.ptrw();
1463 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1464 w[i] = E->get();
1465 }
1466 return ilret;
1467 }
1468
1469 Vector<String> Theme::_get_stylebox_type_list() const {
1470 Vector<String> ilret;
1471 List<StringName> il;
1472
1473 get_stylebox_type_list(&il);
1474 ilret.resize(il.size());
1475
1476 int i = 0;
1477 String *w = ilret.ptrw();
1478 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1479 w[i] = E->get();
1480 }
1481 return ilret;
1482 }
1483
1484 Vector<String> Theme::_get_font_list(const String &p_theme_type) const {
1485 Vector<String> ilret;
1486 List<StringName> il;
1487
1488 get_font_list(p_theme_type, &il);
1489 ilret.resize(il.size());
1490
1491 int i = 0;
1492 String *w = ilret.ptrw();
1493 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1494 w[i] = E->get();
1495 }
1496 return ilret;
1497 }
1498
1499 Vector<String> Theme::_get_font_type_list() const {
1500 Vector<String> ilret;
1501 List<StringName> il;
1502
1503 get_font_type_list(&il);
1504 ilret.resize(il.size());
1505
1506 int i = 0;
1507 String *w = ilret.ptrw();
1508 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1509 w[i] = E->get();
1510 }
1511 return ilret;
1512 }
1513
1514 Vector<String> Theme::_get_font_size_list(const String &p_theme_type) const {
1515 Vector<String> ilret;
1516 List<StringName> il;
1517
1518 get_font_size_list(p_theme_type, &il);
1519 ilret.resize(il.size());
1520
1521 int i = 0;
1522 String *w = ilret.ptrw();
1523 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1524 w[i] = E->get();
1525 }
1526 return ilret;
1527 }
1528
1529 Vector<String> Theme::_get_font_size_type_list() const {
1530 Vector<String> ilret;
1531 List<StringName> il;
1532
1533 get_font_size_type_list(&il);
1534 ilret.resize(il.size());
1535
1536 int i = 0;
1537 String *w = ilret.ptrw();
1538 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1539 w[i] = E->get();
1540 }
1541 return ilret;
1542 }
1543
1544 Vector<String> Theme::_get_color_list(const String &p_theme_type) const {
1545 Vector<String> ilret;
1546 List<StringName> il;
1547
1548 get_color_list(p_theme_type, &il);
1549 ilret.resize(il.size());
1550
1551 int i = 0;
1552 String *w = ilret.ptrw();
1553 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1554 w[i] = E->get();
1555 }
1556 return ilret;
1557 }
1558
1559 Vector<String> Theme::_get_color_type_list() const {
1560 Vector<String> ilret;
1561 List<StringName> il;
1562
1563 get_color_type_list(&il);
1564 ilret.resize(il.size());
1565
1566 int i = 0;
1567 String *w = ilret.ptrw();
1568 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1569 w[i] = E->get();
1570 }
1571 return ilret;
1572 }
1573
1574 Vector<String> Theme::_get_constant_list(const String &p_theme_type) const {
1575 Vector<String> ilret;
1576 List<StringName> il;
1577
1578 get_constant_list(p_theme_type, &il);
1579 ilret.resize(il.size());
1580
1581 int i = 0;
1582 String *w = ilret.ptrw();
1583 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1584 w[i] = E->get();
1585 }
1586 return ilret;
1587 }
1588
1589 Vector<String> Theme::_get_constant_type_list() const {
1590 Vector<String> ilret;
1591 List<StringName> il;
1592
1593 get_constant_type_list(&il);
1594 ilret.resize(il.size());
1595
1596 int i = 0;
1597 String *w = ilret.ptrw();
1598 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1599 w[i] = E->get();
1600 }
1601 return ilret;
1602 }
1603
1604 Vector<String> Theme::_get_theme_item_list(DataType p_data_type, const String &p_theme_type) const {
1605 switch (p_data_type) {
1606 case DATA_TYPE_COLOR:
1607 return _get_color_list(p_theme_type);
1608 case DATA_TYPE_CONSTANT:
1609 return _get_constant_list(p_theme_type);
1610 case DATA_TYPE_FONT:
1611 return _get_font_list(p_theme_type);
1612 case DATA_TYPE_FONT_SIZE:
1613 return _get_font_size_list(p_theme_type);
1614 case DATA_TYPE_ICON:
1615 return _get_icon_list(p_theme_type);
1616 case DATA_TYPE_STYLEBOX:
1617 return _get_stylebox_list(p_theme_type);
1618 case DATA_TYPE_MAX:
1619 break; // Can't happen, but silences warning.
1620 }
1621
1622 return Vector<String>();
1623 }
1624
1625 Vector<String> Theme::_get_theme_item_type_list(DataType p_data_type) const {
1626 switch (p_data_type) {
1627 case DATA_TYPE_COLOR:
1628 return _get_color_type_list();
1629 case DATA_TYPE_CONSTANT:
1630 return _get_constant_type_list();
1631 case DATA_TYPE_FONT:
1632 return _get_font_type_list();
1633 case DATA_TYPE_FONT_SIZE:
1634 return _get_font_size_type_list();
1635 case DATA_TYPE_ICON:
1636 return _get_icon_type_list();
1637 case DATA_TYPE_STYLEBOX:
1638 return _get_stylebox_type_list();
1639 case DATA_TYPE_MAX:
1640 break; // Can't happen, but silences warning.
1641 }
1642
1643 return Vector<String>();
1644 }
1645
1646 Vector<String> Theme::_get_type_variation_list(const StringName &p_theme_type) const {
1647 Vector<String> ilret;
1648 List<StringName> il;
1649
1650 get_type_variation_list(p_theme_type, &il);
1651 ilret.resize(il.size());
1652
1653 int i = 0;
1654 String *w = ilret.ptrw();
1655 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1656 w[i] = E->get();
1657 }
1658 return ilret;
1659 }
1660
1661 Vector<String> Theme::_get_type_list() const {
1662 Vector<String> ilret;
1663 List<StringName> il;
1664
1665 get_type_list(&il);
1666 ilret.resize(il.size());
1667
1668 int i = 0;
1669 String *w = ilret.ptrw();
1670 for (List<StringName>::Element *E = il.front(); E; E = E->next(), i++) {
1671 w[i] = E->get();
1672 }
1673 return ilret;
1674 }
1675
1676 // Theme bulk manipulations.
1677 void Theme::_emit_theme_changed(bool p_notify_list_changed) {
1678 if (no_change_propagation) {
1679 return;
1680 }
1681
1682 if (p_notify_list_changed) {
1683 notify_property_list_changed();
1684 }
1685 emit_changed();
1686 }
1687
1688 void Theme::_freeze_change_propagation() {
1689 no_change_propagation = true;
1690 }
1691
1692 void Theme::_unfreeze_and_propagate_changes() {
1693 no_change_propagation = false;
1694 _emit_theme_changed(true);
1695 }
1696
1697 void Theme::merge_with(const Ref<Theme> &p_other) {
1698 if (p_other.is_null()) {
1699 return;
1700 }
1701
1702 _freeze_change_propagation();
1703
1704 // Colors.
1705 {
1706 for (const KeyValue<StringName, ThemeColorMap> &E : p_other->color_map) {
1707 for (const KeyValue<StringName, Color> &F : E.value) {
1708 set_color(F.key, E.key, F.value);
1709 }
1710 }
1711 }
1712
1713 // Constants.
1714 {
1715 for (const KeyValue<StringName, ThemeConstantMap> &E : p_other->constant_map) {
1716 for (const KeyValue<StringName, int> &F : E.value) {
1717 set_constant(F.key, E.key, F.value);
1718 }
1719 }
1720 }
1721
1722 // Fonts.
1723 {
1724 for (const KeyValue<StringName, ThemeFontMap> &E : p_other->font_map) {
1725 for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
1726 set_font(F.key, E.key, F.value);
1727 }
1728 }
1729 }
1730
1731 // Font sizes.
1732 {
1733 for (const KeyValue<StringName, ThemeFontSizeMap> &E : p_other->font_size_map) {
1734 for (const KeyValue<StringName, int> &F : E.value) {
1735 set_font_size(F.key, E.key, F.value);
1736 }
1737 }
1738 }
1739
1740 // Icons.
1741 {
1742 for (const KeyValue<StringName, ThemeIconMap> &E : p_other->icon_map) {
1743 for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
1744 set_icon(F.key, E.key, F.value);
1745 }
1746 }
1747 }
1748
1749 // Styleboxes.
1750 {
1751 for (const KeyValue<StringName, ThemeStyleMap> &E : p_other->style_map) {
1752 for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
1753 set_stylebox(F.key, E.key, F.value);
1754 }
1755 }
1756 }
1757
1758 // Type variations.
1759 {
1760 for (const KeyValue<StringName, StringName> &E : p_other->variation_map) {
1761 set_type_variation(E.key, E.value);
1762 }
1763 }
1764
1765 // Defaults.
1766 if (p_other->has_default_font()) {
1767 set_default_font(p_other->default_font);
1768 }
1769 if (p_other->has_default_font_size()) {
1770 set_default_font_size(p_other->default_font_size);
1771 }
1772 if (p_other->has_default_base_scale()) {
1773 set_default_base_scale(p_other->default_base_scale);
1774 }
1775
1776 _unfreeze_and_propagate_changes();
1777 }
1778
1779 void Theme::clear() {
1780 // These items need disconnecting.
1781 {
1782 for (const KeyValue<StringName, ThemeIconMap> &E : icon_map) {
1783 for (const KeyValue<StringName, Ref<Texture2D>> &F : E.value) {
1784 if (F.value.is_valid()) {
1785 Ref<Texture2D> icon = F.value;
1786 icon->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
1787 }
1788 }
1789 }
1790 }
1791
1792 {
1793 for (const KeyValue<StringName, ThemeStyleMap> &E : style_map) {
1794 for (const KeyValue<StringName, Ref<StyleBox>> &F : E.value) {
1795 if (F.value.is_valid()) {
1796 Ref<StyleBox> style = F.value;
1797 style->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
1798 }
1799 }
1800 }
1801 }
1802
1803 {
1804 for (const KeyValue<StringName, ThemeFontMap> &E : font_map) {
1805 for (const KeyValue<StringName, Ref<Font>> &F : E.value) {
1806 if (F.value.is_valid()) {
1807 Ref<Font> font = F.value;
1808 font->disconnect_changed(callable_mp(this, &Theme::_emit_theme_changed));
1809 }
1810 }
1811 }
1812 }
1813
1814 icon_map.clear();
1815 style_map.clear();
1816 font_map.clear();
1817 font_size_map.clear();
1818 color_map.clear();
1819 constant_map.clear();
1820
1821 variation_map.clear();
1822 variation_base_map.clear();
1823
1824 _emit_theme_changed(true);
1825 }
1826
1827 void Theme::reset_state() {
1828 clear();
1829 }
1830
1831 void Theme::_bind_methods() {
1832 ClassDB::bind_method(D_METHOD("set_icon", "name", "theme_type", "texture"), &Theme::set_icon);
1833 ClassDB::bind_method(D_METHOD("get_icon", "name", "theme_type"), &Theme::get_icon);
1834 ClassDB::bind_method(D_METHOD("has_icon", "name", "theme_type"), &Theme::has_icon);
1835 ClassDB::bind_method(D_METHOD("rename_icon", "old_name", "name", "theme_type"), &Theme::rename_icon);
1836 ClassDB::bind_method(D_METHOD("clear_icon", "name", "theme_type"), &Theme::clear_icon);
1837 ClassDB::bind_method(D_METHOD("get_icon_list", "theme_type"), &Theme::_get_icon_list);
1838 ClassDB::bind_method(D_METHOD("get_icon_type_list"), &Theme::_get_icon_type_list);
1839
1840 ClassDB::bind_method(D_METHOD("set_stylebox", "name", "theme_type", "texture"), &Theme::set_stylebox);
1841 ClassDB::bind_method(D_METHOD("get_stylebox", "name", "theme_type"), &Theme::get_stylebox);
1842 ClassDB::bind_method(D_METHOD("has_stylebox", "name", "theme_type"), &Theme::has_stylebox);
1843 ClassDB::bind_method(D_METHOD("rename_stylebox", "old_name", "name", "theme_type"), &Theme::rename_stylebox);
1844 ClassDB::bind_method(D_METHOD("clear_stylebox", "name", "theme_type"), &Theme::clear_stylebox);
1845 ClassDB::bind_method(D_METHOD("get_stylebox_list", "theme_type"), &Theme::_get_stylebox_list);
1846 ClassDB::bind_method(D_METHOD("get_stylebox_type_list"), &Theme::_get_stylebox_type_list);
1847
1848 ClassDB::bind_method(D_METHOD("set_font", "name", "theme_type", "font"), &Theme::set_font);
1849 ClassDB::bind_method(D_METHOD("get_font", "name", "theme_type"), &Theme::get_font);
1850 ClassDB::bind_method(D_METHOD("has_font", "name", "theme_type"), &Theme::has_font);
1851 ClassDB::bind_method(D_METHOD("rename_font", "old_name", "name", "theme_type"), &Theme::rename_font);
1852 ClassDB::bind_method(D_METHOD("clear_font", "name", "theme_type"), &Theme::clear_font);
1853 ClassDB::bind_method(D_METHOD("get_font_list", "theme_type"), &Theme::_get_font_list);
1854 ClassDB::bind_method(D_METHOD("get_font_type_list"), &Theme::_get_font_type_list);
1855
1856 ClassDB::bind_method(D_METHOD("set_font_size", "name", "theme_type", "font_size"), &Theme::set_font_size);
1857 ClassDB::bind_method(D_METHOD("get_font_size", "name", "theme_type"), &Theme::get_font_size);
1858 ClassDB::bind_method(D_METHOD("has_font_size", "name", "theme_type"), &Theme::has_font_size);
1859 ClassDB::bind_method(D_METHOD("rename_font_size", "old_name", "name", "theme_type"), &Theme::rename_font_size);
1860 ClassDB::bind_method(D_METHOD("clear_font_size", "name", "theme_type"), &Theme::clear_font_size);
1861 ClassDB::bind_method(D_METHOD("get_font_size_list", "theme_type"), &Theme::_get_font_size_list);
1862 ClassDB::bind_method(D_METHOD("get_font_size_type_list"), &Theme::_get_font_size_type_list);
1863
1864 ClassDB::bind_method(D_METHOD("set_color", "name", "theme_type", "color"), &Theme::set_color);
1865 ClassDB::bind_method(D_METHOD("get_color", "name", "theme_type"), &Theme::get_color);
1866 ClassDB::bind_method(D_METHOD("has_color", "name", "theme_type"), &Theme::has_color);
1867 ClassDB::bind_method(D_METHOD("rename_color", "old_name", "name", "theme_type"), &Theme::rename_color);
1868 ClassDB::bind_method(D_METHOD("clear_color", "name", "theme_type"), &Theme::clear_color);
1869 ClassDB::bind_method(D_METHOD("get_color_list", "theme_type"), &Theme::_get_color_list);
1870 ClassDB::bind_method(D_METHOD("get_color_type_list"), &Theme::_get_color_type_list);
1871
1872 ClassDB::bind_method(D_METHOD("set_constant", "name", "theme_type", "constant"), &Theme::set_constant);
1873 ClassDB::bind_method(D_METHOD("get_constant", "name", "theme_type"), &Theme::get_constant);
1874 ClassDB::bind_method(D_METHOD("has_constant", "name", "theme_type"), &Theme::has_constant);
1875 ClassDB::bind_method(D_METHOD("rename_constant", "old_name", "name", "theme_type"), &Theme::rename_constant);
1876 ClassDB::bind_method(D_METHOD("clear_constant", "name", "theme_type"), &Theme::clear_constant);
1877 ClassDB::bind_method(D_METHOD("get_constant_list", "theme_type"), &Theme::_get_constant_list);
1878 ClassDB::bind_method(D_METHOD("get_constant_type_list"), &Theme::_get_constant_type_list);
1879
1880 ClassDB::bind_method(D_METHOD("set_default_base_scale", "base_scale"), &Theme::set_default_base_scale);
1881 ClassDB::bind_method(D_METHOD("get_default_base_scale"), &Theme::get_default_base_scale);
1882 ClassDB::bind_method(D_METHOD("has_default_base_scale"), &Theme::has_default_base_scale);
1883
1884 ClassDB::bind_method(D_METHOD("set_default_font", "font"), &Theme::set_default_font);
1885 ClassDB::bind_method(D_METHOD("get_default_font"), &Theme::get_default_font);
1886 ClassDB::bind_method(D_METHOD("has_default_font"), &Theme::has_default_font);
1887
1888 ClassDB::bind_method(D_METHOD("set_default_font_size", "font_size"), &Theme::set_default_font_size);
1889 ClassDB::bind_method(D_METHOD("get_default_font_size"), &Theme::get_default_font_size);
1890 ClassDB::bind_method(D_METHOD("has_default_font_size"), &Theme::has_default_font_size);
1891
1892 ClassDB::bind_method(D_METHOD("set_theme_item", "data_type", "name", "theme_type", "value"), &Theme::set_theme_item);
1893 ClassDB::bind_method(D_METHOD("get_theme_item", "data_type", "name", "theme_type"), &Theme::get_theme_item);
1894 ClassDB::bind_method(D_METHOD("has_theme_item", "data_type", "name", "theme_type"), &Theme::has_theme_item);
1895 ClassDB::bind_method(D_METHOD("rename_theme_item", "data_type", "old_name", "name", "theme_type"), &Theme::rename_theme_item);
1896 ClassDB::bind_method(D_METHOD("clear_theme_item", "data_type", "name", "theme_type"), &Theme::clear_theme_item);
1897 ClassDB::bind_method(D_METHOD("get_theme_item_list", "data_type", "theme_type"), &Theme::_get_theme_item_list);
1898 ClassDB::bind_method(D_METHOD("get_theme_item_type_list", "data_type"), &Theme::_get_theme_item_type_list);
1899
1900 ClassDB::bind_method(D_METHOD("set_type_variation", "theme_type", "base_type"), &Theme::set_type_variation);
1901 ClassDB::bind_method(D_METHOD("is_type_variation", "theme_type", "base_type"), &Theme::is_type_variation);
1902 ClassDB::bind_method(D_METHOD("clear_type_variation", "theme_type"), &Theme::clear_type_variation);
1903 ClassDB::bind_method(D_METHOD("get_type_variation_base", "theme_type"), &Theme::get_type_variation_base);
1904 ClassDB::bind_method(D_METHOD("get_type_variation_list", "base_type"), &Theme::_get_type_variation_list);
1905
1906 ClassDB::bind_method(D_METHOD("add_type", "theme_type"), &Theme::add_type);
1907 ClassDB::bind_method(D_METHOD("remove_type", "theme_type"), &Theme::remove_type);
1908 ClassDB::bind_method(D_METHOD("rename_type", "old_theme_type", "theme_type"), &Theme::rename_type);
1909 ClassDB::bind_method(D_METHOD("get_type_list"), &Theme::_get_type_list);
1910
1911 ClassDB::bind_method(D_METHOD("merge_with", "other"), &Theme::merge_with);
1912 ClassDB::bind_method(D_METHOD("clear"), &Theme::clear);
1913
1914 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "default_base_scale", PROPERTY_HINT_RANGE, "0.0,2.0,0.01,or_greater"), "set_default_base_scale", "get_default_base_scale");
1915 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font");
1916 ADD_PROPERTY(PropertyInfo(Variant::INT, "default_font_size", PROPERTY_HINT_RANGE, "0,256,1,or_greater,suffix:px"), "set_default_font_size", "get_default_font_size");
1917
1918 BIND_ENUM_CONSTANT(DATA_TYPE_COLOR);
1919 BIND_ENUM_CONSTANT(DATA_TYPE_CONSTANT);
1920 BIND_ENUM_CONSTANT(DATA_TYPE_FONT);
1921 BIND_ENUM_CONSTANT(DATA_TYPE_FONT_SIZE);
1922 BIND_ENUM_CONSTANT(DATA_TYPE_ICON);
1923 BIND_ENUM_CONSTANT(DATA_TYPE_STYLEBOX);
1924 BIND_ENUM_CONSTANT(DATA_TYPE_MAX);
1925 }
1926
1927 Theme::Theme() {
1928 }
1929
1930 Theme::~Theme() {
1931 }