From 98cf645258c4ee3b9d13f2b1016b15ea212724c6 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Mon, 4 Oct 2021 17:44:53 +0300 Subject: [PATCH] Expose API to retrieve Theme's default font --- doc/classes/Control.xml | 7 +++++++ doc/classes/Theme.xml | 9 ++++++++- scene/gui/control.cpp | 34 ++++++++++++++++++++++++++++++++++ scene/gui/control.h | 2 ++ scene/resources/theme.cpp | 10 +++++++--- scene/resources/theme.h | 1 + 6 files changed, 59 insertions(+), 4 deletions(-) diff --git a/doc/classes/Control.xml b/doc/classes/Control.xml index 72993e2db1b..18c2db33bd6 100644 --- a/doc/classes/Control.xml +++ b/doc/classes/Control.xml @@ -362,6 +362,13 @@ See [method get_color] for details. + + + + Returns the default font from the first matching [Theme] in the tree if that [Theme] has a valid [member Theme.default_font] value. + See [method get_color] for details. + + diff --git a/doc/classes/Theme.xml b/doc/classes/Theme.xml index ca1ff60c687..74b0f187a6a 100644 --- a/doc/classes/Theme.xml +++ b/doc/classes/Theme.xml @@ -238,6 +238,12 @@ Returns [code]false[/code] if the theme does not have [code]node_type[/code]. + + + + Returns [code]true[/code] if this theme has a valid [member default_font] value. + + @@ -403,7 +409,8 @@ - The theme's default font. + The default font of this [Theme] resource. Used as a fallback value for font items defined in this theme, but having invalid values. If this value is also invalid, the global default value is used. + Use [method has_default_font] to check if this value is valid. diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index 50bf20970be..84f0180c363 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -1299,6 +1299,37 @@ bool Control::has_constant(const StringName &p_name, const StringName &p_theme_t return Theme::get_default()->has_constant(p_name, type); } +Ref Control::get_theme_default_font() const { + // First, look through each control or window node in the branch, until no valid parent can be found. + // Only nodes with a theme resource attached are considered. + // For each theme resource see if their assigned theme has the default value defined and valid. + Control *theme_owner = data.theme_owner; + + while (theme_owner) { + if (theme_owner && theme_owner->data.theme->has_default_theme_font()) { + return theme_owner->data.theme->get_default_theme_font(); + } + + Node *parent = theme_owner->get_parent(); + Control *parent_c = Object::cast_to(parent); + if (parent_c) { + theme_owner = parent_c->data.theme_owner; + } else { + theme_owner = nullptr; + } + } + + // Secondly, check the project-defined Theme resource. + if (Theme::get_project_default().is_valid()) { + if (Theme::get_project_default()->has_default_theme_font()) { + return Theme::get_project_default()->get_default_theme_font(); + } + } + + // Lastly, fall back on the default Theme. + return Theme::get_default()->get_default_theme_font(); +} + Rect2 Control::get_parent_anchorable_rect() const { if (!is_inside_tree()) { return Rect2(); @@ -2793,6 +2824,8 @@ void Control::_bind_methods() { ClassDB::bind_method(D_METHOD("has_color", "name", "theme_type"), &Control::has_color, DEFVAL("")); ClassDB::bind_method(D_METHOD("has_constant", "name", "theme_type"), &Control::has_constant, DEFVAL("")); + ClassDB::bind_method(D_METHOD("get_theme_default_font"), &Control::get_theme_default_font); + ClassDB::bind_method(D_METHOD("get_parent_control"), &Control::get_parent_control); ClassDB::bind_method(D_METHOD("set_h_grow_direction", "direction"), &Control::set_h_grow_direction); @@ -2991,6 +3024,7 @@ void Control::_bind_methods() { BIND_VMETHOD(MethodInfo(Variant::BOOL, "has_point", PropertyInfo(Variant::VECTOR2, "point"))); } + Control::Control() { data.parent = nullptr; diff --git a/scene/gui/control.h b/scene/gui/control.h index 745897e878c..51ef20f3fec 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -446,6 +446,8 @@ public: bool has_color(const StringName &p_name, const StringName &p_theme_type = StringName()) const; bool has_constant(const StringName &p_name, const StringName &p_theme_type = StringName()) const; + Ref get_theme_default_font() const; + /* TOOLTIP */ void set_tooltip(const String &p_tooltip); diff --git a/scene/resources/theme.cpp b/scene/resources/theme.cpp index 58d305a2f78..c1add622da1 100644 --- a/scene/resources/theme.cpp +++ b/scene/resources/theme.cpp @@ -221,6 +221,10 @@ Ref Theme::get_default_theme_font() const { return default_theme_font; } +bool Theme::has_default_theme_font() const { + return default_theme_font.is_valid(); +} + // Icons. void Theme::set_icon(const StringName &p_name, const StringName &p_node_type, const Ref &p_icon) { if (icon_map[p_node_type].has(p_name) && icon_map[p_node_type][p_name].is_valid()) { @@ -451,7 +455,7 @@ void Theme::set_font(const StringName &p_name, const StringName &p_node_type, co Ref Theme::get_font(const StringName &p_name, const StringName &p_node_type) const { if (font_map.has(p_node_type) && font_map[p_node_type].has(p_name) && font_map[p_node_type][p_name].is_valid()) { return font_map[p_node_type][p_name]; - } else if (default_theme_font.is_valid()) { + } else if (has_default_theme_font()) { return default_theme_font; } else { return default_font; @@ -1354,10 +1358,9 @@ void Theme::_bind_methods() { ClassDB::bind_method(D_METHOD("get_constant_list", "node_type"), &Theme::_get_constant_list); ClassDB::bind_method(D_METHOD("get_constant_types"), &Theme::_get_constant_types); - ClassDB::bind_method(D_METHOD("clear"), &Theme::clear); - 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_theme_item", "data_type", "name", "node_type", "value"), &Theme::set_theme_item); ClassDB::bind_method(D_METHOD("get_theme_item", "data_type", "name", "node_type"), &Theme::get_theme_item); @@ -1374,6 +1377,7 @@ void Theme::_bind_methods() { ClassDB::bind_method("copy_default_theme", &Theme::copy_default_theme); ClassDB::bind_method(D_METHOD("copy_theme", "other"), &Theme::copy_theme); ClassDB::bind_method(D_METHOD("merge_with", "other"), &Theme::merge_with); + ClassDB::bind_method(D_METHOD("clear"), &Theme::clear); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font"); diff --git a/scene/resources/theme.h b/scene/resources/theme.h index 7e24d1e0b5a..69c582ee6e9 100644 --- a/scene/resources/theme.h +++ b/scene/resources/theme.h @@ -120,6 +120,7 @@ public: void set_default_theme_font(const Ref &p_default_font); Ref get_default_theme_font() const; + bool has_default_theme_font() const; void set_icon(const StringName &p_name, const StringName &p_node_type, const Ref &p_icon); Ref get_icon(const StringName &p_name, const StringName &p_node_type) const;