Debian Codegraph @

scene/theme/theme_db.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* theme_db.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_db.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/resource_loader.h"
35 #include "scene/gui/control.h"
36 #include "scene/main/node.h"
37 #include "scene/main/window.h"
38 #include "scene/resources/font.h"
39 #include "scene/resources/style_box.h"
40 #include "scene/resources/texture.h"
41 #include "scene/theme/default_theme.h"
42 #include "servers/text/text_server.h"
43
44 // Default engine theme creation and configuration.
45
46 void ThemeDB::initialize_theme() {
47 // Default theme-related project settings.
48
49 // Allow creating the default theme at a different scale to suit higher/lower base resolutions.
50 float default_theme_scale = GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/theme/default_theme_scale", PROPERTY_HINT_RANGE, "0.5,8,0.01", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), 1.0);
51
52 String project_theme_path = GLOBAL_DEF_RST_BASIC(PropertyInfo(Variant::STRING, "gui/theme/custom", PROPERTY_HINT_FILE, "*.tres,*.res,*.theme", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), "");
53 String project_font_path = GLOBAL_DEF_RST_BASIC(PropertyInfo(Variant::STRING, "gui/theme/custom_font", PROPERTY_HINT_FILE, "*.tres,*.res,*.otf,*.ttf,*.woff,*.woff2,*.fnt,*.font,*.pfb,*.pfm", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), "");
54
55 TextServer::FontAntialiasing font_antialiasing = (TextServer::FontAntialiasing)(int)GLOBAL_GET("gui/theme/default_font_antialiasing");
56 TextServer::Hinting font_hinting = (TextServer::Hinting)(int)GLOBAL_GET("gui/theme/default_font_hinting");
57 TextServer::SubpixelPositioning font_subpixel_positioning = (TextServer::SubpixelPositioning)(int)GLOBAL_GET("gui/theme/default_font_subpixel_positioning");
58
59 const bool font_msdf = GLOBAL_GET("gui/theme/default_font_multichannel_signed_distance_field");
60 const bool font_generate_mipmaps = GLOBAL_GET("gui/theme/default_font_generate_mipmaps");
61
62 // Attempt to load custom project theme and font.
63
64 if (!project_theme_path.is_empty()) {
65 Ref<Theme> theme = ResourceLoader::load(project_theme_path);
66 if (theme.is_valid()) {
67 set_project_theme(theme);
68 } else {
69 ERR_PRINT("Error loading custom project theme '" + project_theme_path + "'");
70 }
71 }
72
73 Ref<Font> project_font;
74 if (!project_font_path.is_empty()) {
75 project_font = ResourceLoader::load(project_font_path);
76 if (project_font.is_valid()) {
77 set_fallback_font(project_font);
78 } else {
79 ERR_PRINT("Error loading custom project font '" + project_font_path + "'");
80 }
81 }
82
83 // Always generate the default theme to serve as a fallback for all required theme definitions.
84
85 if (RenderingServer::get_singleton()) {
86 make_default_theme(default_theme_scale, project_font, font_subpixel_positioning, font_hinting, font_antialiasing, font_msdf, font_generate_mipmaps);
87 }
88
89 _init_default_theme_context();
90 }
91
92 void ThemeDB::initialize_theme_noproject() {
93 if (RenderingServer::get_singleton()) {
94 make_default_theme(1.0, Ref<Font>());
95 }
96
97 _init_default_theme_context();
98 }
99
100 void ThemeDB::finalize_theme() {
101 if (!RenderingServer::get_singleton()) {
102 WARN_PRINT("Finalizing theme when there is no RenderingServer is an error; check the order of operations.");
103 }
104
105 _finalize_theme_contexts();
106 default_theme.unref();
107
108 fallback_font.unref();
109 fallback_icon.unref();
110 fallback_stylebox.unref();
111 }
112
113 // Global Theme resources.
114
115 void ThemeDB::set_default_theme(const Ref<Theme> &p_default) {
116 default_theme = p_default;
117 }
118
119 Ref<Theme> ThemeDB::get_default_theme() {
120 return default_theme;
121 }
122
123 void ThemeDB::set_project_theme(const Ref<Theme> &p_project_default) {
124 project_theme = p_project_default;
125 }
126
127 Ref<Theme> ThemeDB::get_project_theme() {
128 return project_theme;
129 }
130
131 // Universal fallback values for theme item types.
132
133 void ThemeDB::set_fallback_base_scale(float p_base_scale) {
134 if (fallback_base_scale == p_base_scale) {
135 return;
136 }
137
138 fallback_base_scale = p_base_scale;
139 emit_signal(SNAME("fallback_changed"));
140 }
141
142 float ThemeDB::get_fallback_base_scale() {
143 return fallback_base_scale;
144 }
145
146 void ThemeDB::set_fallback_font(const Ref<Font> &p_font) {
147 if (fallback_font == p_font) {
148 return;
149 }
150
151 fallback_font = p_font;
152 emit_signal(SNAME("fallback_changed"));
153 }
154
155 Ref<Font> ThemeDB::get_fallback_font() {
156 return fallback_font;
157 }
158
159 void ThemeDB::set_fallback_font_size(int p_font_size) {
160 if (fallback_font_size == p_font_size) {
161 return;
162 }
163
164 fallback_font_size = p_font_size;
165 emit_signal(SNAME("fallback_changed"));
166 }
167
168 int ThemeDB::get_fallback_font_size() {
169 return fallback_font_size;
170 }
171
172 void ThemeDB::set_fallback_icon(const Ref<Texture2D> &p_icon) {
173 if (fallback_icon == p_icon) {
174 return;
175 }
176
177 fallback_icon = p_icon;
178 emit_signal(SNAME("fallback_changed"));
179 }
180
181 Ref<Texture2D> ThemeDB::get_fallback_icon() {
182 return fallback_icon;
183 }
184
185 void ThemeDB::set_fallback_stylebox(const Ref<StyleBox> &p_stylebox) {
186 if (fallback_stylebox == p_stylebox) {
187 return;
188 }
189
190 fallback_stylebox = p_stylebox;
191 emit_signal(SNAME("fallback_changed"));
192 }
193
194 Ref<StyleBox> ThemeDB::get_fallback_stylebox() {
195 return fallback_stylebox;
196 }
197
198 void ThemeDB::get_native_type_dependencies(const StringName &p_base_type, Vector<StringName> &r_result) {
199 if (p_base_type == StringName()) {
200 return;
201 }
202
203 // TODO: It may make sense to stop at Control/Window, because their parent classes cannot be used in
204 // a meaningful way.
205 if (!ClassDB::get_inheritance_chain_nocheck(p_base_type, r_result)) {
206 r_result.push_back(p_base_type);
207 }
208 }
209
210 // Global theme contexts.
211
212 ThemeContext *ThemeDB::create_theme_context(Node *p_node, Vector<Ref<Theme>> &p_themes) {
213 ERR_FAIL_COND_V(!p_node->is_inside_tree(), nullptr);
214 ERR_FAIL_COND_V(theme_contexts.has(p_node), nullptr);
215 ERR_FAIL_COND_V(p_themes.is_empty(), nullptr);
216
217 ThemeContext *context = memnew(ThemeContext);
218 context->node = p_node;
219 context->parent = get_nearest_theme_context(p_node);
220 context->set_themes(p_themes);
221
222 theme_contexts[p_node] = context;
223 _propagate_theme_context(p_node, context);
224
225 p_node->connect(SceneStringName(tree_exited), callable_mp(this, &ThemeDB::destroy_theme_context).bind(p_node));
226
227 return context;
228 }
229
230 void ThemeDB::destroy_theme_context(Node *p_node) {
231 ERR_FAIL_COND(!theme_contexts.has(p_node));
232
233 p_node->disconnect(SceneStringName(tree_exited), callable_mp(this, &ThemeDB::destroy_theme_context));
234
235 ThemeContext *context = theme_contexts[p_node];
236
237 theme_contexts.erase(p_node);
238 _propagate_theme_context(p_node, context->parent);
239
240 memdelete(context);
241 }
242
243 void ThemeDB::_propagate_theme_context(Node *p_from_node, ThemeContext *p_context) {
244 Control *from_control = Object::cast_to<Control>(p_from_node);
245 Window *from_window = from_control ? nullptr : Object::cast_to<Window>(p_from_node);
246
247 if (from_control) {
248 from_control->set_theme_context(p_context);
249 } else if (from_window) {
250 from_window->set_theme_context(p_context);
251 }
252
253 for (int i = 0; i < p_from_node->get_child_count(); i++) {
254 Node *child_node = p_from_node->get_child(i);
255
256 // If the child is the root of another global context, stop the propagation
257 // in this branch.
258 if (theme_contexts.has(child_node)) {
259 theme_contexts[child_node]->parent = p_context;
260 continue;
261 }
262
263 _propagate_theme_context(child_node, p_context);
264 }
265 }
266
267 void ThemeDB::_init_default_theme_context() {
268 default_theme_context = memnew(ThemeContext);
269
270 Vector<Ref<Theme>> themes;
271
272 // Only add the project theme to the default context when running projects.
273
274 #ifdef TOOLS_ENABLED
275 if (!Engine::get_singleton()->is_editor_hint()) {
276 themes.push_back(project_theme);
277 }
278 #else
279 themes.push_back(project_theme);
280 #endif
281
282 themes.push_back(default_theme);
283 default_theme_context->set_themes(themes);
284 }
285
286 void ThemeDB::_finalize_theme_contexts() {
287 if (default_theme_context) {
288 memdelete(default_theme_context);
289 default_theme_context = nullptr;
290 }
291 while (theme_contexts.size()) {
292 HashMap<Node *, ThemeContext *>::Iterator E = theme_contexts.begin();
293 memdelete(E->value);
294 theme_contexts.remove(E);
295 }
296 }
297
298 ThemeContext *ThemeDB::get_theme_context(Node *p_node) const {
299 if (!theme_contexts.has(p_node)) {
300 return nullptr;
301 }
302
303 return theme_contexts[p_node];
304 }
305
306 ThemeContext *ThemeDB::get_default_theme_context() const {
307 return default_theme_context;
308 }
309
310 ThemeContext *ThemeDB::get_nearest_theme_context(Node *p_for_node) const {
311 ERR_FAIL_COND_V(!p_for_node->is_inside_tree(), nullptr);
312
313 Node *parent_node = p_for_node->get_parent();
314 while (parent_node) {
315 if (theme_contexts.has(parent_node)) {
316 return theme_contexts[parent_node];
317 }
318
319 parent_node = parent_node->get_parent();
320 }
321
322 return nullptr;
323 }
324
325 // Theme item binding.
326
327 void ThemeDB::bind_class_item(Theme::DataType p_data_type, const StringName &p_class_name, const StringName &p_prop_name, const StringName &p_item_name, ThemeItemSetter p_setter) {
328 ERR_FAIL_COND_MSG(theme_item_binds[p_class_name].has(p_prop_name), vformat("Failed to bind theme item '%s' in class '%s': already bound", p_prop_name, p_class_name));
329
330 ThemeItemBind bind;
331 bind.data_type = p_data_type;
332 bind.class_name = p_class_name;
333 bind.item_name = p_item_name;
334 bind.setter = p_setter;
335
336 theme_item_binds[p_class_name][p_prop_name] = bind;
337 theme_item_binds_list[p_class_name].push_back(bind);
338 }
339
340 void ThemeDB::bind_class_external_item(Theme::DataType p_data_type, const StringName &p_class_name, const StringName &p_prop_name, const StringName &p_item_name, const StringName &p_type_name, ThemeItemSetter p_setter) {
341 ERR_FAIL_COND_MSG(theme_item_binds[p_class_name].has(p_prop_name), vformat("Failed to bind theme item '%s' in class '%s': already bound", p_prop_name, p_class_name));
342
343 ThemeItemBind bind;
344 bind.data_type = p_data_type;
345 bind.class_name = p_class_name;
346 bind.item_name = p_item_name;
347 bind.type_name = p_type_name;
348 bind.external = true;
349 bind.setter = p_setter;
350
351 theme_item_binds[p_class_name][p_prop_name] = bind;
352 theme_item_binds_list[p_class_name].push_back(bind);
353 }
354
355 void ThemeDB::update_class_instance_items(Node *p_instance) {
356 ERR_FAIL_NULL(p_instance);
357
358 // Use the hierarchy to initialize all inherited theme caches. Setters carry the necessary
359 // context and will set the values appropriately.
360 StringName class_name = p_instance->get_class();
361 while (class_name != StringName()) {
362 HashMap<StringName, HashMap<StringName, ThemeItemBind>>::Iterator E = theme_item_binds.find(class_name);
363 if (E) {
364 for (const KeyValue<StringName, ThemeItemBind> &F : E->value) {
365 F.value.setter(p_instance, F.value.item_name, F.value.type_name);
366 }
367 }
368
369 class_name = ClassDB::get_parent_class_nocheck(class_name);
370 }
371 }
372
373 void ThemeDB::get_class_items(const StringName &p_class_name, List<ThemeItemBind> *r_list, bool p_include_inherited, Theme::DataType p_filter_type) {
374 List<StringName> class_hierarchy;
375 StringName class_name = p_class_name;
376 while (class_name != StringName()) {
377 class_hierarchy.push_front(class_name); // Put parent classes in front.
378 class_name = ClassDB::get_parent_class_nocheck(class_name);
379 }
380
381 HashSet<StringName> inherited_props;
382 for (const StringName &theme_type : class_hierarchy) {
383 HashMap<StringName, List<ThemeItemBind>>::Iterator E = theme_item_binds_list.find(theme_type);
384 if (E) {
385 for (const ThemeItemBind &F : E->value) {
386 if (p_filter_type != Theme::DATA_TYPE_MAX && F.data_type != p_filter_type) {
387 continue;
388 }
389 if (inherited_props.has(F.item_name)) {
390 continue; // Skip inherited properties.
391 }
392 if (F.external || F.class_name != p_class_name) {
393 inherited_props.insert(F.item_name);
394
395 if (!p_include_inherited) {
396 continue; // Track properties defined in parent classes, and skip them.
397 }
398 }
399
400 r_list->push_back(F);
401 }
402 }
403 }
404 }
405
406 void ThemeDB::_sort_theme_items() {
407 for (KeyValue<StringName, List<ThemeDB::ThemeItemBind>> &E : theme_item_binds_list) {
408 E.value.sort_custom<ThemeItemBind::SortByType>();
409 }
410 }
411
412 // Object methods.
413
414 void ThemeDB::_bind_methods() {
415 ClassDB::bind_method(D_METHOD("get_default_theme"), &ThemeDB::get_default_theme);
416 ClassDB::bind_method(D_METHOD("get_project_theme"), &ThemeDB::get_project_theme);
417
418 ClassDB::bind_method(D_METHOD("set_fallback_base_scale", "base_scale"), &ThemeDB::set_fallback_base_scale);
419 ClassDB::bind_method(D_METHOD("get_fallback_base_scale"), &ThemeDB::get_fallback_base_scale);
420 ClassDB::bind_method(D_METHOD("set_fallback_font", "font"), &ThemeDB::set_fallback_font);
421 ClassDB::bind_method(D_METHOD("get_fallback_font"), &ThemeDB::get_fallback_font);
422 ClassDB::bind_method(D_METHOD("set_fallback_font_size", "font_size"), &ThemeDB::set_fallback_font_size);
423 ClassDB::bind_method(D_METHOD("get_fallback_font_size"), &ThemeDB::get_fallback_font_size);
424 ClassDB::bind_method(D_METHOD("set_fallback_icon", "icon"), &ThemeDB::set_fallback_icon);
425 ClassDB::bind_method(D_METHOD("get_fallback_icon"), &ThemeDB::get_fallback_icon);
426 ClassDB::bind_method(D_METHOD("set_fallback_stylebox", "stylebox"), &ThemeDB::set_fallback_stylebox);
427 ClassDB::bind_method(D_METHOD("get_fallback_stylebox"), &ThemeDB::get_fallback_stylebox);
428
429 ADD_GROUP("Fallback values", "fallback_");
430 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fallback_base_scale", PROPERTY_HINT_RANGE, "0.0,2.0,0.01,or_greater"), "set_fallback_base_scale", "get_fallback_base_scale");
431 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_font", PROPERTY_HINT_RESOURCE_TYPE, "Font", PROPERTY_USAGE_NONE), "set_fallback_font", "get_fallback_font");
432 ADD_PROPERTY(PropertyInfo(Variant::INT, "fallback_font_size", PROPERTY_HINT_RANGE, "0,256,1,or_greater,suffix:px"), "set_fallback_font_size", "get_fallback_font_size");
433 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_NONE), "set_fallback_icon", "get_fallback_icon");
434 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fallback_stylebox", PROPERTY_HINT_RESOURCE_TYPE, "StyleBox", PROPERTY_USAGE_NONE), "set_fallback_stylebox", "get_fallback_stylebox");
435
436 ADD_SIGNAL(MethodInfo("fallback_changed"));
437 }
438
439 // Memory management, reference, and initialization.
440
441 ThemeDB *ThemeDB::singleton = nullptr;
442
443 ThemeDB *ThemeDB::get_singleton() {
444 return singleton;
445 }
446
447 ThemeDB::ThemeDB() {
448 singleton = this;
449 if (MessageQueue::get_singleton()) { // May not exist in tests etc.
450 callable_mp(this, &ThemeDB::_sort_theme_items).call_deferred();
451 }
452 }
453
454 ThemeDB::~ThemeDB() {
455 // For technical reasons unit tests recreate and destroy the default
456 // theme over and over again. Make sure that finalize_theme() also
457 // frees any objects that can be recreated by initialize_theme*().
458
459 _finalize_theme_contexts();
460
461 default_theme.unref();
462 project_theme.unref();
463
464 fallback_font.unref();
465 fallback_icon.unref();
466 fallback_stylebox.unref();
467
468 singleton = nullptr;
469 }
470
471 void ThemeContext::_emit_changed() {
472 emit_signal(CoreStringName(changed));
473 }
474
475 void ThemeContext::set_themes(Vector<Ref<Theme>> &p_themes) {
476 for (const Ref<Theme> &theme : themes) {
477 theme->disconnect_changed(callable_mp(this, &ThemeContext::_emit_changed));
478 }
479
480 themes.clear();
481
482 for (const Ref<Theme> &theme : p_themes) {
483 if (theme.is_null()) {
484 continue;
485 }
486
487 themes.push_back(theme);
488 theme->connect_changed(callable_mp(this, &ThemeContext::_emit_changed));
489 }
490
491 _emit_changed();
492 }
493
494 const Vector<Ref<Theme>> ThemeContext::get_themes() const {
495 return themes;
496 }
497
498 Ref<Theme> ThemeContext::get_fallback_theme() const {
499 // We expect all contexts to be valid and non-empty, but just in case...
500 if (themes.is_empty()) {
501 return ThemeDB::get_singleton()->get_default_theme();
502 }
503
504 return themes[themes.size() - 1];
505 }
506
507 void ThemeContext::_bind_methods() {
508 ADD_SIGNAL(MethodInfo("changed"));
509 }