From d829b43ab5cf032d3fb17caf2f88365aab3b0407 Mon Sep 17 00:00:00 2001 From: TheSecondReal0 <66881186+TheSecondReal0@users.noreply.github.com> Date: Wed, 19 Apr 2023 20:37:52 -0600 Subject: [PATCH] Add option for ButtonGroups to be unpressed Add an option for ButtonGroups to be unpressed Apply suggestions from code review Co-Authored-By: Tomek Co-Authored-By: Yuri Rubinsky Co-Authored-By: kleonc <9283098+kleonc@users.noreply.github.com> --- doc/classes/ButtonGroup.xml | 3 +++ scene/gui/base_button.cpp | 13 ++++++++++++- scene/gui/base_button.h | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/doc/classes/ButtonGroup.xml b/doc/classes/ButtonGroup.xml index 3e153ea1aaf..ebc902bea7d 100644 --- a/doc/classes/ButtonGroup.xml +++ b/doc/classes/ButtonGroup.xml @@ -24,6 +24,9 @@ + + If [code]true[/code], it is possible to unpress all buttons in this [ButtonGroup]. + diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index c26a00221a3..e5b77654cf0 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -40,7 +40,7 @@ void BaseButton::_unpress_group() { return; } - if (toggle_mode) { + if (toggle_mode && !button_group->is_allow_unpress()) { status.pressed = true; } @@ -537,9 +537,20 @@ BaseButton *ButtonGroup::get_pressed_button() { return nullptr; } +void ButtonGroup::set_allow_unpress(bool p_enabled) { + allow_unpress = p_enabled; +} +bool ButtonGroup::is_allow_unpress() { + return allow_unpress; +} + void ButtonGroup::_bind_methods() { ClassDB::bind_method(D_METHOD("get_pressed_button"), &ButtonGroup::get_pressed_button); ClassDB::bind_method(D_METHOD("get_buttons"), &ButtonGroup::_get_buttons); + ClassDB::bind_method(D_METHOD("set_allow_unpress", "enabled"), &ButtonGroup::set_allow_unpress); + ClassDB::bind_method(D_METHOD("is_allow_unpress"), &ButtonGroup::is_allow_unpress); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_unpress"), "set_allow_unpress", "is_allow_unpress"); ADD_SIGNAL(MethodInfo("pressed", PropertyInfo(Variant::OBJECT, "button", PROPERTY_HINT_RESOURCE_TYPE, "BaseButton"))); } diff --git a/scene/gui/base_button.h b/scene/gui/base_button.h index 962a16c4539..a8d5cee44ca 100644 --- a/scene/gui/base_button.h +++ b/scene/gui/base_button.h @@ -152,6 +152,7 @@ class ButtonGroup : public Resource { GDCLASS(ButtonGroup, Resource); friend class BaseButton; HashSet buttons; + bool allow_unpress = false; protected: static void _bind_methods(); @@ -160,6 +161,8 @@ public: BaseButton *get_pressed_button(); void get_buttons(List *r_buttons); TypedArray _get_buttons(); + void set_allow_unpress(bool p_enabled); + bool is_allow_unpress(); ButtonGroup(); };