Skip to content

Commit

Permalink
Merge pull request #63335 from Spartan322/button-shortcut-feedback
Browse files Browse the repository at this point in the history
Make `BaseButton` Shortcut feedback togglable
  • Loading branch information
akien-mga committed Nov 2, 2022
2 parents 093dea2 + d846dd6 commit 06c8e40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions doc/classes/BaseButton.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
<member name="shortcut" type="Shortcut" setter="set_shortcut" getter="get_shortcut">
[Shortcut] associated to the button.
</member>
<member name="shortcut_feedback" type="bool" setter="set_shortcut_feedback" getter="is_shortcut_feedback">
If [code]true[/code], the button will appear pressed when its shortcut is activated. If [code]false[/code] and [member toggle_mode] is [code]false[/code], the shortcut will activate the button without appearing to press the button.
</member>
<member name="shortcut_in_tooltip" type="bool" setter="set_shortcut_in_tooltip" getter="is_shortcut_in_tooltip_enabled">
If [code]true[/code], the button will add information about its shortcut in the tooltip.
</member>
Expand Down
19 changes: 18 additions & 1 deletion scene/gui/base_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ void BaseButton::_notification(int p_what) {
status.hovering = false;
status.press_attempt = false;
status.pressing_inside = false;
status.shortcut_press = false;
} break;
}
}
Expand Down Expand Up @@ -160,6 +161,7 @@ void BaseButton::on_action_event(Ref<InputEvent> p_event) {
if (action_mode == ACTION_MODE_BUTTON_PRESS) {
status.press_attempt = false;
status.pressing_inside = false;
status.shortcut_press = false;
}
status.pressed = !status.pressed;
_unpress_group();
Expand All @@ -185,6 +187,7 @@ void BaseButton::on_action_event(Ref<InputEvent> p_event) {
}
status.press_attempt = false;
status.pressing_inside = false;
status.shortcut_press = false;
emit_signal(SNAME("button_up"));
}

Expand All @@ -209,6 +212,7 @@ void BaseButton::set_disabled(bool p_disabled) {
}
status.press_attempt = false;
status.pressing_inside = false;
status.shortcut_press = false;
}
queue_redraw();
}
Expand Down Expand Up @@ -284,7 +288,7 @@ BaseButton::DrawMode BaseButton::get_draw_mode() const {
pressing = status.pressed;
}

if (pressing) {
if ((shortcut_feedback || !status.shortcut_press) && pressing) {
return DRAW_PRESSED;
} else {
return DRAW_NORMAL;
Expand Down Expand Up @@ -350,6 +354,7 @@ void BaseButton::shortcut_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null());

if (!is_disabled() && is_visible_in_tree() && !p_event->is_echo() && shortcut.is_valid() && shortcut->matches_event(p_event)) {
status.shortcut_press = true;
on_action_event(p_event);
accept_event();
}
Expand Down Expand Up @@ -389,6 +394,14 @@ bool BaseButton::_was_pressed_by_mouse() const {
return was_mouse_pressed;
}

void BaseButton::set_shortcut_feedback(bool p_feedback) {
shortcut_feedback = p_feedback;
}

bool BaseButton::is_shortcut_feedback() const {
return shortcut_feedback;
}

void BaseButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);
Expand All @@ -414,6 +427,9 @@ void BaseButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_button_group", "button_group"), &BaseButton::set_button_group);
ClassDB::bind_method(D_METHOD("get_button_group"), &BaseButton::get_button_group);

ClassDB::bind_method(D_METHOD("set_shortcut_feedback", "enabled"), &BaseButton::set_shortcut_feedback);
ClassDB::bind_method(D_METHOD("is_shortcut_feedback"), &BaseButton::is_shortcut_feedback);

GDVIRTUAL_BIND(_pressed);
GDVIRTUAL_BIND(_toggled, "button_pressed");

Expand All @@ -430,6 +446,7 @@ void BaseButton::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "button_mask", PROPERTY_HINT_FLAGS, "Mouse Left, Mouse Right, Mouse Middle"), "set_button_mask", "get_button_mask");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_pressed_outside"), "set_keep_pressed_outside", "is_keep_pressed_outside");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shortcut", PROPERTY_HINT_RESOURCE_TYPE, "Shortcut"), "set_shortcut", "get_shortcut");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_feedback"), "set_shortcut_feedback", "is_shortcut_feedback");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "button_group", PROPERTY_HINT_RESOURCE_TYPE, "ButtonGroup"), "set_button_group", "get_button_group");

BIND_ENUM_CONSTANT(DRAW_NORMAL);
Expand Down
5 changes: 5 additions & 0 deletions scene/gui/base_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ class BaseButton : public Control {
bool keep_pressed_outside = false;
Ref<Shortcut> shortcut;
ObjectID shortcut_context;
bool shortcut_feedback = true;

ActionMode action_mode = ACTION_MODE_BUTTON_RELEASE;
struct Status {
bool pressed = false;
bool hovering = false;
bool press_attempt = false;
bool pressing_inside = false;
bool shortcut_press = false;

bool disabled = false;

Expand Down Expand Up @@ -131,6 +133,9 @@ class BaseButton : public Control {
void set_button_group(const Ref<ButtonGroup> &p_group);
Ref<ButtonGroup> get_button_group() const;

void set_shortcut_feedback(bool p_feedback);
bool is_shortcut_feedback() const;

BaseButton();
~BaseButton();
};
Expand Down

0 comments on commit 06c8e40

Please sign in to comment.