diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-08-29 14:02:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-29 14:02:21 +0200 |
commit | e60086f98b07365d6174677df84c9a22bfb56d15 (patch) | |
tree | b1ff19a2fda6a735d883340a389d85e03c6febf5 /scene/resources | |
parent | 0024ba8daddbc7d49458d438ccf578242d900e57 (diff) | |
parent | 6320a0fc18fa84363494df9013b65011a3018a62 (diff) |
Merge pull request #64119 from YuriSizov/theme-init-database
Diffstat (limited to 'scene/resources')
-rw-r--r-- | scene/resources/default_theme/default_theme.cpp | 20 | ||||
-rw-r--r-- | scene/resources/default_theme/default_theme.h | 1 | ||||
-rw-r--r-- | scene/resources/font.cpp | 37 | ||||
-rw-r--r-- | scene/resources/primitive_meshes.cpp | 17 | ||||
-rw-r--r-- | scene/resources/theme.cpp | 79 | ||||
-rw-r--r-- | scene/resources/theme.h | 29 |
6 files changed, 41 insertions, 142 deletions
diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 5bfa1adfe5..410f35e597 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -35,6 +35,7 @@ #include "default_theme_icons.gen.h" #include "scene/resources/font.h" #include "scene/resources/theme.h" +#include "scene/theme/theme_db.h" #include "servers/text_server.h" #include "modules/modules_enabled.gen.h" // For svg. @@ -1101,18 +1102,11 @@ void make_default_theme(float p_scale, Ref<Font> p_font, TextServer::SubpixelPos fill_default_theme(t, default_font, bold_font, bold_italics_font, italics_font, default_icon, default_style, default_scale); - Theme::set_default(t); - Theme::set_fallback_base_scale(default_scale); - Theme::set_fallback_icon(default_icon); - Theme::set_fallback_style(default_style); - Theme::set_fallback_font(default_font); - Theme::set_fallback_font_size(default_font_size * default_scale); -} + ThemeDB::get_singleton()->set_default_theme(t); -void clear_default_theme() { - Theme::set_project_default(nullptr); - Theme::set_default(nullptr); - Theme::set_fallback_icon(nullptr); - Theme::set_fallback_style(nullptr); - Theme::set_fallback_font(nullptr); + ThemeDB::get_singleton()->set_fallback_base_scale(default_scale); + ThemeDB::get_singleton()->set_fallback_icon(default_icon); + ThemeDB::get_singleton()->set_fallback_stylebox(default_style); + ThemeDB::get_singleton()->set_fallback_font(default_font); + ThemeDB::get_singleton()->set_fallback_font_size(default_font_size * default_scale); } diff --git a/scene/resources/default_theme/default_theme.h b/scene/resources/default_theme/default_theme.h index 15be5e676f..003934ce90 100644 --- a/scene/resources/default_theme/default_theme.h +++ b/scene/resources/default_theme/default_theme.h @@ -37,6 +37,5 @@ const int default_font_size = 16; void fill_default_theme(Ref<Theme> &theme, const Ref<Font> &default_font, const Ref<Font> &bold_font, const Ref<Font> &bold_italics_font, const Ref<Font> &italics_font, Ref<Texture2D> &default_icon, Ref<StyleBox> &default_style, float p_scale); void make_default_theme(float p_scale, Ref<Font> p_font, TextServer::SubpixelPositioning p_font_subpixel = TextServer::SUBPIXEL_POSITIONING_AUTO, TextServer::Hinting p_font_hinting = TextServer::HINTING_LIGHT, TextServer::FontAntialiasing p_font_antialiased = TextServer::FONT_ANTIALIASING_GRAY, bool p_font_msdf = false, bool p_font_generate_mipmaps = false); -void clear_default_theme(); #endif // DEFAULT_THEME_H diff --git a/scene/resources/font.cpp b/scene/resources/font.cpp index be57d9c965..8ec5678f0a 100644 --- a/scene/resources/font.cpp +++ b/scene/resources/font.cpp @@ -39,6 +39,7 @@ #include "scene/resources/text_line.h" #include "scene/resources/text_paragraph.h" #include "scene/resources/theme.h" +#include "scene/theme/theme_db.h" /*************************************************************************/ /* Font */ @@ -2552,13 +2553,13 @@ Ref<Font> FontVariation::_get_base_font_or_default() const { } // Check the project-defined Theme resource. - if (Theme::get_project_default().is_valid()) { + if (ThemeDB::get_singleton()->get_project_theme().is_valid()) { List<StringName> theme_types; - Theme::get_project_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types); + ThemeDB::get_singleton()->get_project_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types); for (const StringName &E : theme_types) { - if (Theme::get_project_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { - Ref<Font> f = Theme::get_project_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); + if (ThemeDB::get_singleton()->get_project_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { + Ref<Font> f = ThemeDB::get_singleton()->get_project_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); if (f.is_valid()) { theme_font = f; theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED); @@ -2569,13 +2570,13 @@ Ref<Font> FontVariation::_get_base_font_or_default() const { } // Lastly, fall back on the items defined in the default Theme, if they exist. - if (Theme::get_default().is_valid()) { + if (ThemeDB::get_singleton()->get_default_theme().is_valid()) { List<StringName> theme_types; - Theme::get_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types); + ThemeDB::get_singleton()->get_default_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types); for (const StringName &E : theme_types) { - if (Theme::get_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { - Ref<Font> f = Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); + if (ThemeDB::get_singleton()->get_default_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { + Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); if (f.is_valid()) { theme_font = f; theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED); @@ -2585,7 +2586,7 @@ Ref<Font> FontVariation::_get_base_font_or_default() const { } // If they don't exist, use any type to return the default/empty value. - Ref<Font> f = Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName()); + Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName()); if (f.is_valid()) { theme_font = f; theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED); @@ -2859,13 +2860,13 @@ Ref<Font> SystemFont::_get_base_font_or_default() const { } // Check the project-defined Theme resource. - if (Theme::get_project_default().is_valid()) { + if (ThemeDB::get_singleton()->get_project_theme().is_valid()) { List<StringName> theme_types; - Theme::get_project_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types); + ThemeDB::get_singleton()->get_project_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types); for (const StringName &E : theme_types) { - if (Theme::get_project_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { - Ref<Font> f = Theme::get_project_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); + if (ThemeDB::get_singleton()->get_project_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { + Ref<Font> f = ThemeDB::get_singleton()->get_project_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); if (f.is_valid()) { theme_font = f; theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED); @@ -2876,13 +2877,13 @@ Ref<Font> SystemFont::_get_base_font_or_default() const { } // Lastly, fall back on the items defined in the default Theme, if they exist. - if (Theme::get_default().is_valid()) { + if (ThemeDB::get_singleton()->get_default_theme().is_valid()) { List<StringName> theme_types; - Theme::get_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types); + ThemeDB::get_singleton()->get_default_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types); for (const StringName &E : theme_types) { - if (Theme::get_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { - Ref<Font> f = Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); + if (ThemeDB::get_singleton()->get_default_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { + Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); if (f.is_valid()) { theme_font = f; theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED); @@ -2892,7 +2893,7 @@ Ref<Font> SystemFont::_get_base_font_or_default() const { } // If they don't exist, use any type to return the default/empty value. - Ref<Font> f = Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName()); + Ref<Font> f = ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName()); if (f.is_valid()) { theme_font = f; theme_font->connect(CoreStringNames::get_singleton()->changed, callable_mp(reinterpret_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED); diff --git a/scene/resources/primitive_meshes.cpp b/scene/resources/primitive_meshes.cpp index e993936350..fc5cf2a028 100644 --- a/scene/resources/primitive_meshes.cpp +++ b/scene/resources/primitive_meshes.cpp @@ -32,6 +32,7 @@ #include "core/core_string_names.h" #include "scene/resources/theme.h" +#include "scene/theme/theme_db.h" #include "servers/rendering_server.h" #include "thirdparty/misc/clipper.hpp" #include "thirdparty/misc/polypartition.h" @@ -2984,13 +2985,13 @@ Ref<Font> TextMesh::_get_font_or_default() const { } // Check the project-defined Theme resource. - if (Theme::get_project_default().is_valid()) { + if (ThemeDB::get_singleton()->get_project_theme().is_valid()) { List<StringName> theme_types; - Theme::get_project_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types); + ThemeDB::get_singleton()->get_project_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types); for (const StringName &E : theme_types) { - if (Theme::get_project_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { - return Theme::get_project_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); + if (ThemeDB::get_singleton()->get_project_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { + return ThemeDB::get_singleton()->get_project_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); } } } @@ -2998,17 +2999,17 @@ Ref<Font> TextMesh::_get_font_or_default() const { // Lastly, fall back on the items defined in the default Theme, if they exist. { List<StringName> theme_types; - Theme::get_default()->get_type_dependencies(get_class_name(), StringName(), &theme_types); + ThemeDB::get_singleton()->get_default_theme()->get_type_dependencies(get_class_name(), StringName(), &theme_types); for (const StringName &E : theme_types) { - if (Theme::get_default()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { - return Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); + if (ThemeDB::get_singleton()->get_default_theme()->has_theme_item(Theme::DATA_TYPE_FONT, "font", E)) { + return ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", E); } } } // If they don't exist, use any type to return the default/empty value. - return Theme::get_default()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName()); + return ThemeDB::get_singleton()->get_default_theme()->get_theme_item(Theme::DATA_TYPE_FONT, "font", StringName()); } void TextMesh::set_font_size(int p_size) { diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp index 3f6eec8497..3321392821 100644 --- a/scene/resources/theme.cpp +++ b/scene/resources/theme.cpp @@ -31,17 +31,7 @@ #include "theme.h" #include "core/string/print_string.h" - -// Universal Theme resources used when no other theme has the item. -Ref<Theme> Theme::default_theme; -Ref<Theme> Theme::project_default_theme; - -// Universal default values, final fallback for every theme. -float Theme::fallback_base_scale = 1.0; -Ref<Texture2D> Theme::fallback_icon; -Ref<StyleBox> Theme::fallback_style; -Ref<Font> Theme::fallback_font; -int Theme::fallback_font_size = 16; +#include "scene/theme/theme_db.h" // Dynamic properties. bool Theme::_set(const StringName &p_name, const Variant &p_value) { @@ -185,64 +175,7 @@ void Theme::_get_property_list(List<PropertyInfo> *p_list) const { } } -// Universal fallback Theme resources. -Ref<Theme> Theme::get_default() { - return default_theme; -} - -void Theme::set_default(const Ref<Theme> &p_default) { - default_theme = p_default; -} - -Ref<Theme> Theme::get_project_default() { - return project_default_theme; -} - -void Theme::set_project_default(const Ref<Theme> &p_project_default) { - project_default_theme = p_project_default; -} - -// Universal fallback values for theme item types. -void Theme::set_fallback_base_scale(float p_base_scale) { - fallback_base_scale = p_base_scale; -} - -void Theme::set_fallback_icon(const Ref<Texture2D> &p_icon) { - fallback_icon = p_icon; -} - -void Theme::set_fallback_style(const Ref<StyleBox> &p_style) { - fallback_style = p_style; -} - -void Theme::set_fallback_font(const Ref<Font> &p_font) { - fallback_font = p_font; -} - -void Theme::set_fallback_font_size(int p_font_size) { - fallback_font_size = p_font_size; -} - -float Theme::get_fallback_base_scale() { - return fallback_base_scale; -} - -Ref<Texture2D> Theme::get_fallback_icon() { - return fallback_icon; -} - -Ref<StyleBox> Theme::get_fallback_style() { - return fallback_style; -} - -Ref<Font> Theme::get_fallback_font() { - return fallback_font; -} - -int Theme::get_fallback_font_size() { - return fallback_font_size; -} - +// Static helpers. bool Theme::is_valid_type_name(const String &p_name) { for (int i = 0; i < p_name.length(); i++) { if (!is_ascii_identifier_char(p_name[i])) { @@ -351,7 +284,7 @@ Ref<Texture2D> Theme::get_icon(const StringName &p_name, const StringName &p_the if (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid()) { return icon_map[p_theme_type][p_name]; } else { - return fallback_icon; + return ThemeDB::get_singleton()->get_fallback_icon(); } } @@ -461,7 +394,7 @@ Ref<StyleBox> Theme::get_stylebox(const StringName &p_name, const StringName &p_ if (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) { return style_map[p_theme_type][p_name]; } else { - return fallback_style; + return ThemeDB::get_singleton()->get_fallback_stylebox(); } } @@ -573,7 +506,7 @@ Ref<Font> Theme::get_font(const StringName &p_name, const StringName &p_theme_ty } else if (has_default_font()) { return default_font; } else { - return fallback_font; + return ThemeDB::get_singleton()->get_fallback_font(); } } @@ -676,7 +609,7 @@ int Theme::get_font_size(const StringName &p_name, const StringName &p_theme_typ } else if (has_default_font_size()) { return default_font_size; } else { - return fallback_font_size; + return ThemeDB::get_singleton()->get_fallback_font_size(); } } diff --git a/scene/resources/theme.h b/scene/resources/theme.h index a2aca5e61f..ed1dc7c938 100644 --- a/scene/resources/theme.h +++ b/scene/resources/theme.h @@ -102,17 +102,6 @@ protected: bool _get(const StringName &p_name, Variant &r_ret) const; void _get_property_list(List<PropertyInfo> *p_list) const; - // Universal Theme resources used when no other theme has the item. - static Ref<Theme> default_theme; - static Ref<Theme> project_default_theme; - - // Universal default values, final fallback for every theme. - static float fallback_base_scale; - static Ref<Texture2D> fallback_icon; - static Ref<StyleBox> fallback_style; - static Ref<Font> fallback_font; - static int fallback_font_size; - // Default values configurable for each individual theme. float default_base_scale = 0.0; Ref<Font> default_font; @@ -126,24 +115,6 @@ protected: virtual void reset_state() override; public: - static Ref<Theme> get_default(); - static void set_default(const Ref<Theme> &p_default); - - static Ref<Theme> get_project_default(); - static void set_project_default(const Ref<Theme> &p_project_default); - - static void set_fallback_base_scale(float p_base_scale); - static void set_fallback_icon(const Ref<Texture2D> &p_icon); - static void set_fallback_style(const Ref<StyleBox> &p_style); - static void set_fallback_font(const Ref<Font> &p_font); - static void set_fallback_font_size(int p_font_size); - - static float get_fallback_base_scale(); - static Ref<Texture2D> get_fallback_icon(); - static Ref<StyleBox> get_fallback_style(); - static Ref<Font> get_fallback_font(); - static int get_fallback_font_size(); - static bool is_valid_type_name(const String &p_name); static bool is_valid_item_name(const String &p_name); |