From ecff5bc42fe036cd01b480ef09222370df7eeae7 Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 22 Mar 2021 02:43:01 +0100 Subject: [PATCH 1/2] Add methods to remove theme overrides --- doc/classes/Control.xml | 55 ++++++++++++++++++++++++++++++++++++++++- scene/gui/control.cpp | 49 ++++++++++++++++++++++++++++++++++++ scene/gui/control.h | 7 ++++++ 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/doc/classes/Control.xml b/doc/classes/Control.xml index c5e820e9fe3..35196ff98bd 100644 --- a/doc/classes/Control.xml +++ b/doc/classes/Control.xml @@ -150,7 +150,6 @@ Overrides the [Color] with given [code]name[/code] in the [member theme] resource the control uses. - [b]Note:[/b] Unlike other theme overrides, there is no way to undo a color override without manually assigning the previous color. [b]Example of overriding a label's color and resetting it later:[/b] [codeblocks] [gdscript] @@ -730,6 +729,60 @@ Give up the focus. No other control will be able to receive keyboard input. + + + + + + + Removes a theme override for a [Color] with the given [code]name[/code]. + + + + + + + + + Removes a theme override for a constant with the given [code]name[/code]. + + + + + + + + + Removes a theme override for a [Font] with the given [code]name[/code]. + + + + + + + + + Removes a theme override for a font size with the given [code]name[/code]. + + + + + + + + + Removes a theme override for an icon with the given [code]name[/code]. + + + + + + + + + Removes a theme override for a [StyleBox] with the given [code]name[/code]. + + diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index c13bce5e0c9..e3e4e316acd 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -1845,6 +1845,48 @@ void Control::add_theme_constant_override(const StringName &p_name, int p_consta notification(NOTIFICATION_THEME_CHANGED); } +void Control::remove_theme_icon_override(const StringName &p_name) { + if (data.icon_override.has(p_name)) { + data.icon_override[p_name]->disconnect("changed", callable_mp(this, &Control::_override_changed)); + } + + data.icon_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_theme_style_override(const StringName &p_name) { + if (data.style_override.has(p_name)) { + data.style_override[p_name]->disconnect("changed", callable_mp(this, &Control::_override_changed)); + } + + data.style_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_theme_font_override(const StringName &p_name) { + if (data.font_override.has(p_name)) { + data.font_override[p_name]->disconnect("changed", callable_mp(this, &Control::_override_changed)); + } + + data.font_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_theme_font_size_override(const StringName &p_name) { + data.font_size_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_theme_color_override(const StringName &p_name) { + data.color_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_theme_constant_override(const StringName &p_name) { + data.constant_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + void Control::set_focus_mode(FocusMode p_focus_mode) { ERR_FAIL_INDEX((int)p_focus_mode, 3); @@ -2799,6 +2841,13 @@ void Control::_bind_methods() { ClassDB::bind_method(D_METHOD("add_theme_color_override", "name", "color"), &Control::add_theme_color_override); ClassDB::bind_method(D_METHOD("add_theme_constant_override", "name", "constant"), &Control::add_theme_constant_override); + ClassDB::bind_method(D_METHOD("remove_theme_icon_override", "name"), &Control::remove_theme_icon_override); + ClassDB::bind_method(D_METHOD("remove_theme_stylebox_override", "name"), &Control::remove_theme_style_override); + ClassDB::bind_method(D_METHOD("remove_theme_font_override", "name"), &Control::remove_theme_font_override); + ClassDB::bind_method(D_METHOD("remove_theme_font_size_override", "name"), &Control::remove_theme_font_size_override); + ClassDB::bind_method(D_METHOD("remove_theme_color_override", "name"), &Control::remove_theme_color_override); + ClassDB::bind_method(D_METHOD("remove_theme_constant_override", "name"), &Control::remove_theme_constant_override); + ClassDB::bind_method(D_METHOD("get_theme_icon", "name", "node_type"), &Control::get_theme_icon, DEFVAL("")); ClassDB::bind_method(D_METHOD("get_theme_stylebox", "name", "node_type"), &Control::get_theme_stylebox, DEFVAL("")); ClassDB::bind_method(D_METHOD("get_theme_font", "name", "node_type"), &Control::get_theme_font, DEFVAL("")); diff --git a/scene/gui/control.h b/scene/gui/control.h index 8981e058726..422bc12aa33 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -459,6 +459,13 @@ public: void add_theme_color_override(const StringName &p_name, const Color &p_color); void add_theme_constant_override(const StringName &p_name, int p_constant); + void remove_theme_icon_override(const StringName &p_name); + void remove_theme_style_override(const StringName &p_name); + void remove_theme_font_override(const StringName &p_name); + void remove_theme_font_size_override(const StringName &p_name); + void remove_theme_color_override(const StringName &p_name); + void remove_theme_constant_override(const StringName &p_name); + Ref get_theme_icon(const StringName &p_name, const StringName &p_node_type = StringName()) const; Ref get_theme_stylebox(const StringName &p_name, const StringName &p_node_type = StringName()) const; Ref get_theme_font(const StringName &p_name, const StringName &p_node_type = StringName()) const; From 5950482b86d3b16be2e738465c24f105b8dcdd10 Mon Sep 17 00:00:00 2001 From: kobewi Date: Tue, 23 Mar 2021 00:55:02 +0100 Subject: [PATCH 2/2] Remove the clearing behavior from add_override --- doc/classes/Control.xml | 10 +++++----- scene/gui/control.cpp | 40 ++++++++++++---------------------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/doc/classes/Control.xml b/doc/classes/Control.xml index 35196ff98bd..5b8fcb64c64 100644 --- a/doc/classes/Control.xml +++ b/doc/classes/Control.xml @@ -177,7 +177,7 @@ - Overrides an integer constant with given [code]name[/code] in the [member theme] resource the control uses. If the [code]constant[/code] is [code]0[/code], the override is cleared and the constant from assigned [Theme] is used. + Overrides an integer constant with given [code]name[/code] in the [member theme] resource the control uses. @@ -188,7 +188,7 @@ - Overrides the font with given [code]name[/code] in the [member theme] resource the control uses. If [code]font[/code] is [code]null[/code] or invalid, the override is cleared and the font from assigned [Theme] is used. + Overrides the font with given [code]name[/code] in the [member theme] resource the control uses. @@ -199,7 +199,7 @@ - Overrides the font size with given [code]name[/code] in the [member theme] resource the control uses. If [code]font_size[/code] is [code]-1[/code], the override is cleared and the font size from assigned [Theme] is used. + Overrides the font size with given [code]name[/code] in the [member theme] resource the control uses. @@ -210,7 +210,7 @@ - Overrides the icon with given [code]name[/code] in the [member theme] resource the control uses. If [code]icon[/code] is [code]null[/code] or invalid, the override is cleared and the icon from assigned [Theme] is used. + Overrides the icon with given [code]name[/code] in the [member theme] resource the control uses. @@ -221,7 +221,7 @@ - Overrides the [StyleBox] with given [code]name[/code] in the [member theme] resource the control uses. If [code]stylebox[/code] is empty or invalid, the override is cleared and the [StyleBox] from assigned [Theme] is used. + Overrides the [StyleBox] with given [code]name[/code] in the [member theme] resource the control uses. [b]Example of modifying a property in a StyleBox by duplicating it:[/b] [codeblocks] [gdscript] diff --git a/scene/gui/control.cpp b/scene/gui/control.cpp index e3e4e316acd..5e937722b59 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -327,7 +327,6 @@ bool Control::_get(const StringName &p_name, Variant &r_ret) const { r_ret = data.color_override.has(name) ? Variant(data.color_override[name]) : Variant(); } else if (sname.begins_with("custom_constants/")) { String name = sname.get_slicec('/', 1); - r_ret = data.constant_override.has(name) ? Variant(data.constant_override[name]) : Variant(); } else { return false; @@ -1780,53 +1779,38 @@ Rect2 Control::get_anchorable_rect() const { } void Control::add_theme_icon_override(const StringName &p_name, const Ref &p_icon) { + ERR_FAIL_COND(!p_icon.is_valid()); + if (data.icon_override.has(p_name)) { data.icon_override[p_name]->disconnect("changed", callable_mp(this, &Control::_override_changed)); } - // clear if "null" is passed instead of an icon - if (p_icon.is_null()) { - data.icon_override.erase(p_name); - } else { - data.icon_override[p_name] = p_icon; - if (data.icon_override[p_name].is_valid()) { - data.icon_override[p_name]->connect("changed", callable_mp(this, &Control::_override_changed), Vector(), CONNECT_REFERENCE_COUNTED); - } - } + data.icon_override[p_name] = p_icon; + data.icon_override[p_name]->connect("changed", callable_mp(this, &Control::_override_changed), Vector(), CONNECT_REFERENCE_COUNTED); notification(NOTIFICATION_THEME_CHANGED); } void Control::add_theme_style_override(const StringName &p_name, const Ref &p_style) { + ERR_FAIL_COND(!p_style.is_valid()); + if (data.style_override.has(p_name)) { data.style_override[p_name]->disconnect("changed", callable_mp(this, &Control::_override_changed)); } - // clear if "null" is passed instead of a style - if (p_style.is_null()) { - data.style_override.erase(p_name); - } else { - data.style_override[p_name] = p_style; - if (data.style_override[p_name].is_valid()) { - data.style_override[p_name]->connect("changed", callable_mp(this, &Control::_override_changed), Vector(), CONNECT_REFERENCE_COUNTED); - } - } + data.style_override[p_name] = p_style; + data.style_override[p_name]->connect("changed", callable_mp(this, &Control::_override_changed), Vector(), CONNECT_REFERENCE_COUNTED); notification(NOTIFICATION_THEME_CHANGED); } void Control::add_theme_font_override(const StringName &p_name, const Ref &p_font) { + ERR_FAIL_COND(!p_font.is_valid()); + if (data.font_override.has(p_name)) { data.font_override[p_name]->disconnect("changed", callable_mp(this, &Control::_override_changed)); } - // clear if "null" is passed instead of a font - if (p_font.is_null()) { - data.font_override.erase(p_name); - } else { - data.font_override[p_name] = p_font; - if (data.font_override[p_name].is_valid()) { - data.font_override[p_name]->connect("changed", callable_mp(this, &Control::_override_changed), Vector(), CONNECT_REFERENCE_COUNTED); - } - } + data.font_override[p_name] = p_font; + data.font_override[p_name]->connect("changed", callable_mp(this, &Control::_override_changed), Vector(), CONNECT_REFERENCE_COUNTED); notification(NOTIFICATION_THEME_CHANGED); }