Skip to content

Commit

Permalink
Add option for ButtonGroups to be unpressed
Browse files Browse the repository at this point in the history
Add an option for ButtonGroups to be unpressed

Apply suggestions from code review

Co-Authored-By: Tomek <kobewi4e@gmail.com>
Co-Authored-By: Yuri Rubinsky <chaosus89@gmail.com>
Co-Authored-By: kleonc <9283098+kleonc@users.noreply.github.com>
  • Loading branch information
4 people committed Apr 20, 2023
1 parent 27253f3 commit d829b43
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/classes/ButtonGroup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
</method>
</methods>
<members>
<member name="allow_unpress" type="bool" setter="set_allow_unpress" getter="is_allow_unpress" default="false">
If [code]true[/code], it is possible to unpress all buttons in this [ButtonGroup].
</member>
<member name="resource_local_to_scene" type="bool" setter="set_local_to_scene" getter="is_local_to_scene" overrides="Resource" default="true" />
</members>
<signals>
Expand Down
13 changes: 12 additions & 1 deletion scene/gui/base_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void BaseButton::_unpress_group() {
return;
}

if (toggle_mode) {
if (toggle_mode && !button_group->is_allow_unpress()) {
status.pressed = true;
}

Expand Down Expand Up @@ -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")));
}
Expand Down
3 changes: 3 additions & 0 deletions scene/gui/base_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class ButtonGroup : public Resource {
GDCLASS(ButtonGroup, Resource);
friend class BaseButton;
HashSet<BaseButton *> buttons;
bool allow_unpress = false;

protected:
static void _bind_methods();
Expand All @@ -160,6 +161,8 @@ class ButtonGroup : public Resource {
BaseButton *get_pressed_button();
void get_buttons(List<BaseButton *> *r_buttons);
TypedArray<BaseButton> _get_buttons();
void set_allow_unpress(bool p_enabled);
bool is_allow_unpress();
ButtonGroup();
};

Expand Down

0 comments on commit d829b43

Please sign in to comment.