From fe283fd07aa7682faf21b252c24c67f0497dc928 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Fri, 31 Dec 2021 18:53:43 +0300 Subject: Correctly use fallback Theme values as last resort --- scene/resources/default_theme/default_theme.cpp | 16 +-- scene/resources/theme.cpp | 138 ++++++++++++++---------- scene/resources/theme.h | 56 +++++----- 3 files changed, 118 insertions(+), 92 deletions(-) (limited to 'scene/resources') diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index a1d76ef352..c92fafa4e7 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -1053,17 +1053,17 @@ void make_default_theme(bool p_hidpi, Ref p_font) { fill_default_theme(t, default_font, large_font, default_icon, default_style, default_scale); Theme::set_default(t); - Theme::set_default_base_scale(default_scale); - Theme::set_default_icon(default_icon); - Theme::set_default_style(default_style); - Theme::set_default_font(default_font); - Theme::set_default_font_size(default_font_size); + 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); } void clear_default_theme() { Theme::set_project_default(nullptr); Theme::set_default(nullptr); - Theme::set_default_icon(nullptr); - Theme::set_default_style(nullptr); - Theme::set_default_font(nullptr); + Theme::set_fallback_icon(nullptr); + Theme::set_fallback_style(nullptr); + Theme::set_fallback_font(nullptr); } diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp index 99977a20f2..40bcf0cc5c 100644 --- a/scene/resources/theme.cpp +++ b/scene/resources/theme.cpp @@ -36,11 +36,11 @@ Ref Theme::default_theme; Ref Theme::project_default_theme; // Universal default values, final fallback for every theme. -float Theme::default_base_scale = 1.0; -Ref Theme::default_icon; -Ref Theme::default_style; -Ref Theme::default_font; -int Theme::default_font_size = 16; +float Theme::fallback_base_scale = 1.0; +Ref Theme::fallback_icon; +Ref Theme::fallback_style; +Ref Theme::fallback_font; +int Theme::fallback_font_size = 16; // Dynamic properties. bool Theme::_set(const StringName &p_name, const Variant &p_value) { @@ -220,87 +220,107 @@ void Theme::set_project_default(const Ref &p_project_default) { } // Universal fallback values for theme item types. -void Theme::set_default_base_scale(float p_base_scale) { - default_base_scale = p_base_scale; +void Theme::set_fallback_base_scale(float p_base_scale) { + fallback_base_scale = p_base_scale; } -void Theme::set_default_icon(const Ref &p_icon) { - default_icon = p_icon; +void Theme::set_fallback_icon(const Ref &p_icon) { + fallback_icon = p_icon; } -void Theme::set_default_style(const Ref &p_style) { - default_style = p_style; +void Theme::set_fallback_style(const Ref &p_style) { + fallback_style = p_style; } -void Theme::set_default_font(const Ref &p_font) { - default_font = p_font; +void Theme::set_fallback_font(const Ref &p_font) { + fallback_font = p_font; } -void Theme::set_default_font_size(int p_font_size) { - default_font_size = p_font_size; +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 Theme::get_fallback_icon() { + return fallback_icon; +} + +Ref Theme::get_fallback_style() { + return fallback_style; +} + +Ref Theme::get_fallback_font() { + return fallback_font; +} + +int Theme::get_fallback_font_size() { + return fallback_font_size; } // Fallback values for theme item types, configurable per theme. -void Theme::set_default_theme_base_scale(float p_base_scale) { - if (default_theme_base_scale == p_base_scale) { +void Theme::set_default_base_scale(float p_base_scale) { + if (default_base_scale == p_base_scale) { return; } - default_theme_base_scale = p_base_scale; + default_base_scale = p_base_scale; _emit_theme_changed(); } -float Theme::get_default_theme_base_scale() const { - return default_theme_base_scale; +float Theme::get_default_base_scale() const { + return default_base_scale; } -bool Theme::has_default_theme_base_scale() const { - return default_theme_base_scale > 0.0; +bool Theme::has_default_base_scale() const { + return default_base_scale > 0.0; } -void Theme::set_default_theme_font(const Ref &p_default_font) { - if (default_theme_font == p_default_font) { +void Theme::set_default_font(const Ref &p_default_font) { + if (default_font == p_default_font) { return; } - if (default_theme_font.is_valid()) { - default_theme_font->disconnect("changed", callable_mp(this, &Theme::_emit_theme_changed)); + if (default_font.is_valid()) { + default_font->disconnect("changed", callable_mp(this, &Theme::_emit_theme_changed)); } - default_theme_font = p_default_font; + default_font = p_default_font; - if (default_theme_font.is_valid()) { - default_theme_font->connect("changed", callable_mp(this, &Theme::_emit_theme_changed), varray(false), CONNECT_REFERENCE_COUNTED); + if (default_font.is_valid()) { + default_font->connect("changed", callable_mp(this, &Theme::_emit_theme_changed), varray(false), CONNECT_REFERENCE_COUNTED); } _emit_theme_changed(); } -Ref Theme::get_default_theme_font() const { - return default_theme_font; +Ref Theme::get_default_font() const { + return default_font; } -bool Theme::has_default_theme_font() const { - return default_theme_font.is_valid(); +bool Theme::has_default_font() const { + return default_font.is_valid(); } -void Theme::set_default_theme_font_size(int p_font_size) { - if (default_theme_font_size == p_font_size) { +void Theme::set_default_font_size(int p_font_size) { + if (default_font_size == p_font_size) { return; } - default_theme_font_size = p_font_size; + default_font_size = p_font_size; _emit_theme_changed(); } -int Theme::get_default_theme_font_size() const { - return default_theme_font_size; +int Theme::get_default_font_size() const { + return default_font_size; } -bool Theme::has_default_theme_font_size() const { - return default_theme_font_size > 0; +bool Theme::has_default_font_size() const { + return default_font_size > 0; } // Icons. @@ -324,7 +344,7 @@ Ref 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 default_icon; + return fallback_icon; } } @@ -411,7 +431,7 @@ Ref 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 default_style; + return fallback_style; } } @@ -497,15 +517,15 @@ void Theme::set_font(const StringName &p_name, const StringName &p_theme_type, c Ref Theme::get_font(const StringName &p_name, const StringName &p_theme_type) const { if (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) { return font_map[p_theme_type][p_name]; - } else if (has_default_theme_font()) { - return default_theme_font; - } else { + } else if (has_default_font()) { return default_font; + } else { + return fallback_font; } } bool Theme::has_font(const StringName &p_name, const StringName &p_theme_type) const { - 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_theme_font()); + 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()); } bool Theme::has_font_nocheck(const StringName &p_name, const StringName &p_theme_type) const { @@ -577,15 +597,15 @@ void Theme::set_font_size(const StringName &p_name, const StringName &p_theme_ty int Theme::get_font_size(const StringName &p_name, const StringName &p_theme_type) const { 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)) { return font_size_map[p_theme_type][p_name]; - } else if (has_default_theme_font_size()) { - return default_theme_font_size; - } else { + } else if (has_default_font_size()) { return default_font_size; + } else { + return fallback_font_size; } } bool Theme::has_font_size(const StringName &p_name, const StringName &p_theme_type) const { - 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_theme_font_size()); + 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()); } bool Theme::has_font_size_nocheck(const StringName &p_name, const StringName &p_theme_type) const { @@ -1622,17 +1642,17 @@ void Theme::_bind_methods() { ClassDB::bind_method(D_METHOD("get_constant_list", "theme_type"), &Theme::_get_constant_list); ClassDB::bind_method(D_METHOD("get_constant_type_list"), &Theme::_get_constant_type_list); - ClassDB::bind_method(D_METHOD("set_default_base_scale", "font_size"), &Theme::set_default_theme_base_scale); - ClassDB::bind_method(D_METHOD("get_default_base_scale"), &Theme::get_default_theme_base_scale); - ClassDB::bind_method(D_METHOD("has_default_base_scale"), &Theme::has_default_theme_base_scale); + ClassDB::bind_method(D_METHOD("set_default_base_scale", "base_scale"), &Theme::set_default_base_scale); + ClassDB::bind_method(D_METHOD("get_default_base_scale"), &Theme::get_default_base_scale); + ClassDB::bind_method(D_METHOD("has_default_base_scale"), &Theme::has_default_base_scale); - ClassDB::bind_method(D_METHOD("set_default_font", "font"), &Theme::set_default_theme_font); - ClassDB::bind_method(D_METHOD("get_default_font"), &Theme::get_default_theme_font); - ClassDB::bind_method(D_METHOD("has_default_font"), &Theme::has_default_theme_font); + ClassDB::bind_method(D_METHOD("set_default_font", "font"), &Theme::set_default_font); + ClassDB::bind_method(D_METHOD("get_default_font"), &Theme::get_default_font); + ClassDB::bind_method(D_METHOD("has_default_font"), &Theme::has_default_font); - ClassDB::bind_method(D_METHOD("set_default_font_size", "font_size"), &Theme::set_default_theme_font_size); - ClassDB::bind_method(D_METHOD("get_default_font_size"), &Theme::get_default_theme_font_size); - ClassDB::bind_method(D_METHOD("has_default_font_size"), &Theme::has_default_theme_font_size); + ClassDB::bind_method(D_METHOD("set_default_font_size", "font_size"), &Theme::set_default_font_size); + ClassDB::bind_method(D_METHOD("get_default_font_size"), &Theme::get_default_font_size); + ClassDB::bind_method(D_METHOD("has_default_font_size"), &Theme::has_default_font_size); ClassDB::bind_method(D_METHOD("set_theme_item", "data_type", "name", "theme_type", "value"), &Theme::set_theme_item); ClassDB::bind_method(D_METHOD("get_theme_item", "data_type", "name", "theme_type"), &Theme::get_theme_item); diff --git a/scene/resources/theme.h b/scene/resources/theme.h index d170d53ae3..51e0ec4b1b 100644 --- a/scene/resources/theme.h +++ b/scene/resources/theme.h @@ -101,16 +101,16 @@ protected: static Ref project_default_theme; // Universal default values, final fallback for every theme. - static float default_base_scale; - static Ref default_icon; - static Ref default_style; - static Ref default_font; - static int default_font_size; + static float fallback_base_scale; + static Ref fallback_icon; + static Ref fallback_style; + static Ref fallback_font; + static int fallback_font_size; // Default values configurable for each individual theme. - float default_theme_base_scale = 0.0; - Ref default_theme_font; - int default_theme_font_size = -1; + float default_base_scale = 0.0; + Ref default_font; + int default_font_size = -1; static void _bind_methods(); @@ -126,23 +126,29 @@ public: static Ref get_project_default(); static void set_project_default(const Ref &p_project_default); - static void set_default_base_scale(float p_base_scale); - static void set_default_icon(const Ref &p_icon); - static void set_default_style(const Ref &p_style); - static void set_default_font(const Ref &p_font); - static void set_default_font_size(int p_font_size); - - void set_default_theme_base_scale(float p_base_scale); - float get_default_theme_base_scale() const; - bool has_default_theme_base_scale() const; - - void set_default_theme_font(const Ref &p_default_font); - Ref get_default_theme_font() const; - bool has_default_theme_font() const; - - void set_default_theme_font_size(int p_font_size); - int get_default_theme_font_size() const; - bool has_default_theme_font_size() const; + static void set_fallback_base_scale(float p_base_scale); + static void set_fallback_icon(const Ref &p_icon); + static void set_fallback_style(const Ref &p_style); + static void set_fallback_font(const Ref &p_font); + static void set_fallback_font_size(int p_font_size); + + static float get_fallback_base_scale(); + static Ref get_fallback_icon(); + static Ref get_fallback_style(); + static Ref get_fallback_font(); + static int get_fallback_font_size(); + + void set_default_base_scale(float p_base_scale); + float get_default_base_scale() const; + bool has_default_base_scale() const; + + void set_default_font(const Ref &p_default_font); + Ref get_default_font() const; + bool has_default_font() const; + + void set_default_font_size(int p_font_size); + int get_default_font_size() const; + bool has_default_font_size() const; void set_icon(const StringName &p_name, const StringName &p_theme_type, const Ref &p_icon); Ref get_icon(const StringName &p_name, const StringName &p_theme_type) const; -- cgit v1.2.3