From 936832d59c4732b18b241ddd2377555eb27cd264 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Thu, 11 Nov 2021 18:23:37 +0800 Subject: [PATCH] Add methods to remove theme overrides --- doc/classes/Control.xml | 62 ++++++++++++++++++++++++++---- scene/gui/control.cpp | 83 +++++++++++++++++++++++++++++------------ scene/gui/control.h | 7 ++++ 3 files changed, 120 insertions(+), 32 deletions(-) diff --git a/doc/classes/Control.xml b/doc/classes/Control.xml index 7daba821d41..8853b206c55 100644 --- a/doc/classes/Control.xml +++ b/doc/classes/Control.xml @@ -90,8 +90,8 @@ - Creates a local override for a theme [Color] with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. An override cannot be removed, but it can be overridden with the corresponding default value. - See also [method get_color]. + Creates a local override for a theme [Color] with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. + See also [method get_color], [method remove_color_override]. [b]Example of overriding a label's color and resetting it later:[/b] [codeblock] # Given the child Label node "MyLabel", override its font color with a custom value. @@ -106,8 +106,8 @@ - Creates a local override for a theme constant with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. An override cannot be removed, but it can be overridden with the corresponding default value. - See also [method get_constant]. + Creates a local override for a theme constant with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. + See also [method get_constant], [method remove_constant_override]. @@ -115,7 +115,8 @@ - Creates a local override for a theme [Font] with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. An override can be removed by assigning it a [code]null[/code] value. + Creates a local override for a theme [Font] with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. + [b]Note:[/b] An override can be removed by assigning it a [code]null[/code] value. This behavior is deprecated and will be removed in 4.0, use [method remove_font_override] instead. See also [method get_font]. @@ -124,7 +125,8 @@ - Creates a local override for a theme icon with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. An override can be removed by assigning it a [code]null[/code] value. + Creates a local override for a theme icon with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. + [b]Note:[/b] An override can be removed by assigning it a [code]null[/code] value. This behavior is deprecated and will be removed in 4.0, use [method remove_icon_override] instead. See also [method get_icon]. @@ -133,7 +135,8 @@ - Creates a local override for a theme shader with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. An override can be removed by assigning it a [code]null[/code] value. + Creates a local override for a theme shader with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. + [b]Note:[/b] An override can be removed by assigning it a [code]null[/code] value. This behavior is deprecated and will be removed in 4.0, use [method remove_shader_override] instead. @@ -141,7 +144,8 @@ - Creates a local override for a theme [StyleBox] with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. An override can be removed by assigning it a [code]null[/code] value. + Creates a local override for a theme [StyleBox] with the specified [code]name[/code]. Local overrides always take precedence when fetching theme items for the control. + [b]Note:[/b] An override can be removed by assigning it a [code]null[/code] value. This behavior is deprecated and will be removed in 4.0, use [method remove_stylebox_override] instead. See also [method get_stylebox]. [b]Example of modifying a property in a StyleBox by duplicating it:[/b] [codeblock] @@ -512,6 +516,48 @@ 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 an icon with the given [code]name[/code]. + + + + + + + Removes a theme override for a shader 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 a36ea3b41b9..fccca47b3b6 100644 --- a/scene/gui/control.cpp +++ b/scene/gui/control.cpp @@ -223,40 +223,22 @@ bool Control::_set(const StringName &p_name, const Variant &p_value) { if (p_value.get_type() == Variant::NIL) { if (name.begins_with("custom_icons/")) { String dname = name.get_slicec('/', 1); - if (data.icon_override.has(dname)) { - data.icon_override[dname]->disconnect("changed", this, "_override_changed"); - } - data.icon_override.erase(dname); - notification(NOTIFICATION_THEME_CHANGED); + remove_icon_override(dname); } else if (name.begins_with("custom_shaders/")) { String dname = name.get_slicec('/', 1); - if (data.shader_override.has(dname)) { - data.shader_override[dname]->disconnect("changed", this, "_override_changed"); - } - data.shader_override.erase(dname); - notification(NOTIFICATION_THEME_CHANGED); + remove_shader_override(dname); } else if (name.begins_with("custom_styles/")) { String dname = name.get_slicec('/', 1); - if (data.style_override.has(dname)) { - data.style_override[dname]->disconnect("changed", this, "_override_changed"); - } - data.style_override.erase(dname); - notification(NOTIFICATION_THEME_CHANGED); + remove_stylebox_override(dname); } else if (name.begins_with("custom_fonts/")) { String dname = name.get_slicec('/', 1); - if (data.font_override.has(dname)) { - data.font_override[dname]->disconnect("changed", this, "_override_changed"); - } - data.font_override.erase(dname); - notification(NOTIFICATION_THEME_CHANGED); + remove_font_override(dname); } else if (name.begins_with("custom_colors/")) { String dname = name.get_slicec('/', 1); - data.color_override.erase(dname); - notification(NOTIFICATION_THEME_CHANGED); + remove_color_override(dname); } else if (name.begins_with("custom_constants/")) { String dname = name.get_slicec('/', 1); - data.constant_override.erase(dname); - notification(NOTIFICATION_THEME_CHANGED); + remove_constant_override(dname); } else { return false; } @@ -1927,6 +1909,52 @@ void Control::add_constant_override(const StringName &p_name, int p_constant) { notification(NOTIFICATION_THEME_CHANGED); } +void Control::remove_icon_override(const StringName &p_name) { + if (data.icon_override.has(p_name)) { + data.icon_override[p_name]->disconnect("changed", this, "_override_changed"); + } + + data.icon_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_shader_override(const StringName &p_name) { + if (data.shader_override.has(p_name)) { + data.shader_override[p_name]->disconnect("changed", this, "_override_changed"); + } + + data.shader_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_stylebox_override(const StringName &p_name) { + if (data.style_override.has(p_name)) { + data.style_override[p_name]->disconnect("changed", this, "_override_changed"); + } + + data.style_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_font_override(const StringName &p_name) { + if (data.font_override.has(p_name)) { + data.font_override[p_name]->disconnect("changed", this, "_override_changed"); + } + + data.font_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_color_override(const StringName &p_name) { + data.color_override.erase(p_name); + notification(NOTIFICATION_THEME_CHANGED); +} + +void Control::remove_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); @@ -2789,6 +2817,13 @@ void Control::_bind_methods() { ClassDB::bind_method(D_METHOD("add_color_override", "name", "color"), &Control::add_color_override); ClassDB::bind_method(D_METHOD("add_constant_override", "name", "constant"), &Control::add_constant_override); + ClassDB::bind_method(D_METHOD("remove_icon_override", "name"), &Control::remove_icon_override); + ClassDB::bind_method(D_METHOD("remove_shader_override", "name"), &Control::remove_shader_override); + ClassDB::bind_method(D_METHOD("remove_stylebox_override", "name"), &Control::remove_stylebox_override); + ClassDB::bind_method(D_METHOD("remove_font_override", "name"), &Control::remove_font_override); + ClassDB::bind_method(D_METHOD("remove_color_override", "name"), &Control::remove_color_override); + ClassDB::bind_method(D_METHOD("remove_constant_override", "name"), &Control::remove_constant_override); + ClassDB::bind_method(D_METHOD("get_icon", "name", "theme_type"), &Control::get_icon, DEFVAL("")); ClassDB::bind_method(D_METHOD("get_stylebox", "name", "theme_type"), &Control::get_stylebox, DEFVAL("")); ClassDB::bind_method(D_METHOD("get_font", "name", "theme_type"), &Control::get_font, DEFVAL("")); diff --git a/scene/gui/control.h b/scene/gui/control.h index 51ef20f3fec..a52dfcfbfa7 100644 --- a/scene/gui/control.h +++ b/scene/gui/control.h @@ -425,6 +425,13 @@ public: void add_color_override(const StringName &p_name, const Color &p_color); void add_constant_override(const StringName &p_name, int p_constant); + void remove_icon_override(const StringName &p_name); + void remove_shader_override(const StringName &p_name); + void remove_stylebox_override(const StringName &p_name); + void remove_font_override(const StringName &p_name); + void remove_color_override(const StringName &p_name); + void remove_constant_override(const StringName &p_name); + Ref get_icon(const StringName &p_name, const StringName &p_theme_type = StringName()) const; Ref get_shader(const StringName &p_name, const StringName &p_theme_type = StringName()) const; Ref get_stylebox(const StringName &p_name, const StringName &p_theme_type = StringName()) const;