Skip to content

Commit

Permalink
Fix InputMap.action_erase_event() failing to erase events correctly.
Browse files Browse the repository at this point in the history
Fixes godotengine#52733

(3.x backport of 7104229)
  • Loading branch information
EIREXE authored and lekoder committed Dec 18, 2021
1 parent fb9cd3d commit 155f438
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
81 changes: 46 additions & 35 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ bool InputEvent::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, f
return false;
}

bool InputEvent::shortcut_match(const Ref<InputEvent> &p_event) const {
bool InputEvent::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
return false;
}

Expand All @@ -114,7 +114,7 @@ void InputEvent::_bind_methods() {

ClassDB::bind_method(D_METHOD("as_text"), &InputEvent::as_text);

ClassDB::bind_method(D_METHOD("shortcut_match", "event"), &InputEvent::shortcut_match);
ClassDB::bind_method(D_METHOD("shortcut_match", "event", "exact_match"), &InputEvent::shortcut_match, DEFVAL(true));

ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type);

Expand Down Expand Up @@ -174,6 +174,23 @@ void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModif
set_metakey(event->get_metakey());
}

uint32_t InputEventWithModifiers::get_modifiers_mask() const {
uint32_t mask = 0;
if (get_control()) {
mask |= KEY_MASK_CTRL;
}
if (get_shift()) {
mask |= KEY_MASK_SHIFT;
}
if (get_alt()) {
mask |= KEY_MASK_ALT;
}
if (get_metakey()) {
mask |= KEY_MASK_META;
}
return mask;
}

void InputEventWithModifiers::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_alt", "enable"), &InputEventWithModifiers::set_alt);
ClassDB::bind_method(D_METHOD("get_alt"), &InputEventWithModifiers::get_alt);
Expand Down Expand Up @@ -243,35 +260,11 @@ bool InputEventKey::is_echo() const {
}

uint32_t InputEventKey::get_scancode_with_modifiers() const {
uint32_t sc = scancode;
if (get_control()) {
sc |= KEY_MASK_CTRL;
}
if (get_alt()) {
sc |= KEY_MASK_ALT;
}
if (get_shift()) {
sc |= KEY_MASK_SHIFT;
}
if (get_metakey()) {
sc |= KEY_MASK_META;
}

return sc;
return scancode | get_modifiers_mask();
}

uint32_t InputEventKey::get_physical_scancode_with_modifiers() const {
uint32_t sc = physical_scancode;
if (get_control())
sc |= KEY_MASK_CTRL;
if (get_alt())
sc |= KEY_MASK_ALT;
if (get_shift())
sc |= KEY_MASK_SHIFT;
if (get_metakey())
sc |= KEY_MASK_META;

return sc;
return physical_scancode | get_modifiers_mask();
}

String InputEventKey::as_text() const {
Expand Down Expand Up @@ -330,16 +323,14 @@ bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed
return match;
}

bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event) const {
bool InputEventKey::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
Ref<InputEventKey> key = p_event;
if (key.is_null()) {
return false;
}

uint32_t code = get_scancode_with_modifiers();
uint32_t event_code = key->get_scancode_with_modifiers();

return code == event_code;
return scancode == key->scancode &&
(!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
}

void InputEventKey::_bind_methods() {
Expand Down Expand Up @@ -494,6 +485,16 @@ bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool *p
return match;
}

bool InputEventMouseButton::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
Ref<InputEventMouseButton> mb = p_event;
if (mb.is_null()) {
return false;
}

return button_index == mb->button_index &&
(!p_exact_match || get_modifiers_mask() == mb->get_modifiers_mask());
}

String InputEventMouseButton::as_text() const {
String button_index_string = "";
switch (get_button_index()) {
Expand Down Expand Up @@ -760,6 +761,16 @@ bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool *
return match;
}

bool InputEventJoypadMotion::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
Ref<InputEventJoypadMotion> jm = p_event;
if (jm.is_null()) {
return false;
}

return axis == jm->axis &&
(!p_exact_match || ((axis_value < 0) == (jm->axis_value < 0)));
}

String InputEventJoypadMotion::as_text() const {
return "InputEventJoypadMotion : axis=" + itos(axis) + ", axis_value=" + String(Variant(axis_value));
}
Expand Down Expand Up @@ -827,7 +838,7 @@ bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool *
return match;
}

bool InputEventJoypadButton::shortcut_match(const Ref<InputEvent> &p_event) const {
bool InputEventJoypadButton::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
Ref<InputEventJoypadButton> button = p_event;
if (button.is_null()) {
return false;
Expand Down Expand Up @@ -1031,7 +1042,7 @@ float InputEventAction::get_strength() const {
return strength;
}

bool InputEventAction::shortcut_match(const Ref<InputEvent> &p_event) const {
bool InputEventAction::shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
if (p_event.is_null()) {
return false;
}
Expand Down
12 changes: 8 additions & 4 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class InputEvent : public Resource {
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;

virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const;

virtual bool accumulate(const Ref<InputEvent> &p_event) { return false; }
Expand Down Expand Up @@ -263,6 +263,8 @@ class InputEventWithModifiers : public InputEvent {

void set_modifiers_from_event(const InputEventWithModifiers *event);

uint32_t get_modifiers_mask() const;

InputEventWithModifiers();
};

Expand Down Expand Up @@ -300,7 +302,7 @@ class InputEventKey : public InputEventWithModifiers {
uint32_t get_physical_scancode_with_modifiers() const;

virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }

Expand Down Expand Up @@ -359,6 +361,7 @@ class InputEventMouseButton : public InputEventMouse {

virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
virtual String as_text() const;
Expand Down Expand Up @@ -416,6 +419,7 @@ class InputEventJoypadMotion : public InputEvent {
virtual bool is_pressed() const;

virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
virtual String as_text() const;
Expand Down Expand Up @@ -443,7 +447,7 @@ class InputEventJoypadButton : public InputEvent {
float get_pressure() const;

virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
virtual String as_text() const;
Expand Down Expand Up @@ -531,7 +535,7 @@ class InputEventAction : public InputEvent {

virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;

virtual bool shortcut_match(const Ref<InputEvent> &p_event) const;
virtual bool shortcut_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const { return true; }
virtual String as_text() const;

Expand Down
4 changes: 3 additions & 1 deletion doc/classes/InputEvent.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@
<method name="shortcut_match" qualifiers="const">
<return type="bool" />
<argument index="0" name="event" type="InputEvent" />
<argument index="1" name="exact_match" type="bool" default="true" />
<description>
Returns [code]true[/code] if the given input event is checking for the same key ([InputEventKey]), button ([InputEventJoypadButton]) or action ([InputEventAction]).
Returns [code]true[/code] if the specified [code]event[/code] matches this event. Only valid for action events i.e key ([InputEventKey]), button ([InputEventMouseButton] or [InputEventJoypadButton]), axis [InputEventJoypadMotion] or action ([InputEventAction]) events.
If [code]exact_match[/code] is [code]false[/code], it ignores the input modifiers for [InputEventKey] and [InputEventMouseButton] events, and the direction for [InputEventJoypadMotion] events.
</description>
</method>
<method name="xformed_by" qualifiers="const">
Expand Down

0 comments on commit 155f438

Please sign in to comment.