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