From 8b008757db4f84293f123d3a47978b8d799d714f Mon Sep 17 00:00:00 2001 From: kilojool Date: Wed, 25 Jan 2023 11:39:09 +0100 Subject: [PATCH] OptionButton: allow reselection of selected item --- doc/classes/OptionButton.xml | 4 ++++ scene/gui/option_button.cpp | 13 ++++++++++++- scene/gui/option_button.h | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/doc/classes/OptionButton.xml b/doc/classes/OptionButton.xml index fdf0fff0fb0..8f67e569083 100644 --- a/doc/classes/OptionButton.xml +++ b/doc/classes/OptionButton.xml @@ -204,6 +204,9 @@ + + If [code]true[/code], the currently selected item can be selected again. + If [code]true[/code], minimum size will be determined by the longest item's text, instead of the currently selected one's. [b]Note:[/b] For performance reasons, the minimum size doesn't update immediately when adding, removing or modifying items. @@ -227,6 +230,7 @@ Emitted when the current item has been changed by the user. The index of the item selected is passed as argument. + [member allow_reselect] must be enabled to reselect an item. diff --git a/scene/gui/option_button.cpp b/scene/gui/option_button.cpp index 027c97b3834..24802025349 100644 --- a/scene/gui/option_button.cpp +++ b/scene/gui/option_button.cpp @@ -383,6 +383,14 @@ bool OptionButton::is_fit_to_longest_item() const { return fit_to_longest_item; } +void OptionButton::set_allow_reselect(bool p_allow) { + allow_reselect = p_allow; +} + +bool OptionButton::get_allow_reselect() const { + return allow_reselect; +} + void OptionButton::add_separator(const String &p_text) { popup->add_separator(p_text); } @@ -395,7 +403,7 @@ void OptionButton::clear() { } void OptionButton::_select(int p_which, bool p_emit) { - if (p_which == current) { + if (p_which == current && !allow_reselect) { return; } @@ -560,11 +568,14 @@ void OptionButton::_bind_methods() { ClassDB::bind_method(D_METHOD("get_selectable_item", "from_last"), &OptionButton::get_selectable_item, DEFVAL(false)); ClassDB::bind_method(D_METHOD("set_fit_to_longest_item", "fit"), &OptionButton::set_fit_to_longest_item); ClassDB::bind_method(D_METHOD("is_fit_to_longest_item"), &OptionButton::is_fit_to_longest_item); + ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &OptionButton::set_allow_reselect); + ClassDB::bind_method(D_METHOD("get_allow_reselect"), &OptionButton::get_allow_reselect); // "selected" property must come after "item_count", otherwise GH-10213 occurs. ADD_ARRAY_COUNT("Items", "item_count", "set_item_count", "get_item_count", "popup/item_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "selected"), "_select_int", "get_selected"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_to_longest_item"), "set_fit_to_longest_item", "is_fit_to_longest_item"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect"); ADD_SIGNAL(MethodInfo("item_selected", PropertyInfo(Variant::INT, "index"))); ADD_SIGNAL(MethodInfo("item_focused", PropertyInfo(Variant::INT, "index"))); } diff --git a/scene/gui/option_button.h b/scene/gui/option_button.h index d8e2f3209d5..b741ebeeddf 100644 --- a/scene/gui/option_button.h +++ b/scene/gui/option_button.h @@ -42,6 +42,7 @@ class OptionButton : public Button { bool fit_to_longest_item = true; Vector2 _cached_size; bool cache_refresh_pending = false; + bool allow_reselect = false; struct ThemeCache { Ref normal; @@ -111,6 +112,9 @@ public: void set_fit_to_longest_item(bool p_fit); bool is_fit_to_longest_item() const; + void set_allow_reselect(bool p_allow); + bool get_allow_reselect() const; + void add_separator(const String &p_text = ""); void clear();