From 2232168ede9f22ccde74ed61a2a9780e57c8e202 Mon Sep 17 00:00:00 2001 From: Nathan Franke Date: Sun, 24 Oct 2021 00:10:06 -0500 Subject: [PATCH] [3.x] Fix action exact match --- core/input_map.cpp | 4 +--- core/os/input_event.cpp | 39 +++++++++++++++++++++------------------ core/os/input_event.h | 14 +++++++------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/core/input_map.cpp b/core/input_map.cpp index 13b1d44c6e17..6dd8523377ee 100644 --- a/core/input_map.cpp +++ b/core/input_map.cpp @@ -136,9 +136,7 @@ List>::Element *InputMap::_find_event(Action &p_action, const Re int device = e->get_device(); if (device == ALL_DEVICES || device == p_event->get_device()) { - if (p_exact_match && e->shortcut_match(p_event)) { - return E; - } else if (!p_exact_match && e->action_match(p_event, p_pressed, p_strength, p_raw_strength, p_action.deadzone)) { + if (e->action_match(p_event, p_exact_match, p_pressed, p_strength, p_raw_strength, p_action.deadzone)) { return E; } } diff --git a/core/os/input_event.cpp b/core/os/input_event.cpp index 87163fc511d4..851695982716 100644 --- a/core/os/input_event.cpp +++ b/core/os/input_event.cpp @@ -88,7 +88,7 @@ String InputEvent::as_text() const { return String(); } -bool InputEvent::action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { +bool InputEvent::action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { return false; } @@ -288,25 +288,21 @@ String InputEventKey::as_text() const { return kc; } -bool InputEventKey::action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { +bool InputEventKey::action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { Ref key = p_event; if (key.is_null()) { return false; } - bool match = false; - if (get_scancode() == 0) { - uint32_t code = get_physical_scancode_with_modifiers(); - uint32_t event_code = key->get_physical_scancode_with_modifiers(); - - match = get_physical_scancode() == key->get_physical_scancode() && (!key->is_pressed() || (code & event_code) == code); + bool match; + if (scancode != 0) { + match = scancode == key->scancode; } else { - uint32_t code = get_scancode_with_modifiers(); - uint32_t event_code = key->get_scancode_with_modifiers(); - - match = get_scancode() == key->get_scancode() && (!key->is_pressed() || (code & event_code) == code); + match = physical_scancode == key->physical_scancode; + } + if (p_exact_match) { + match &= get_modifiers_mask() == key->get_modifiers_mask(); } - if (match) { bool pressed = key->is_pressed(); if (p_pressed != nullptr) { @@ -466,13 +462,16 @@ Ref InputEventMouseButton::xformed_by(const Transform2D &p_xform, co return mb; } -bool InputEventMouseButton::action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { +bool InputEventMouseButton::action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { Ref mb = p_event; if (mb.is_null()) { return false; } bool match = mb->button_index == button_index; + if (p_exact_match) { + match &= get_modifiers_mask() == mb->get_modifiers_mask(); + } if (match) { bool pressed = mb->is_pressed(); if (p_pressed != nullptr) { @@ -730,13 +729,17 @@ bool InputEventJoypadMotion::is_pressed() const { return Math::abs(axis_value) >= 0.5f; } -bool InputEventJoypadMotion::action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { +bool InputEventJoypadMotion::action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { Ref jm = p_event; if (jm.is_null()) { return false; } - bool match = (axis == jm->axis); // Matches even if not in the same direction, but returns a "not pressed" event. + // Matches even if not in the same direction, but returns a "not pressed" event. + bool match = (axis == jm->axis); + if (p_exact_match) { + match &= (axis_value < 0) == (jm->axis_value < 0); + } if (match) { float jm_abs_axis_value = Math::abs(jm->get_axis_value()); bool same_direction = (((axis_value < 0) == (jm->axis_value < 0)) || jm->axis_value == 0); @@ -819,7 +822,7 @@ float InputEventJoypadButton::get_pressure() const { return pressure; } -bool InputEventJoypadButton::action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { +bool InputEventJoypadButton::action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { Ref jb = p_event; if (jb.is_null()) { return false; @@ -1059,7 +1062,7 @@ bool InputEventAction::is_action(const StringName &p_action) const { return action == p_action; } -bool InputEventAction::action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { +bool InputEventAction::action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const { Ref act = p_event; if (act.is_null()) { return false; diff --git a/core/os/input_event.h b/core/os/input_event.h index e246be55805d..0593d8f10c59 100644 --- a/core/os/input_event.h +++ b/core/os/input_event.h @@ -224,7 +224,7 @@ class InputEvent : public Resource { virtual Ref xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const; - virtual bool action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; + virtual bool action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; virtual bool shortcut_match(const Ref &p_event, bool p_exact_match = true) const; virtual bool is_action_type() const; @@ -312,7 +312,7 @@ class InputEventKey : public InputEventWithModifiers { uint32_t get_scancode_with_modifiers() const; uint32_t get_physical_scancode_with_modifiers() const; - virtual bool action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; + virtual bool action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; virtual bool shortcut_match(const Ref &p_event, bool p_exact_match = true) const; virtual bool is_action_type() const { return true; } @@ -371,7 +371,7 @@ class InputEventMouseButton : public InputEventMouse { bool is_doubleclick() const; virtual Ref xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const; - virtual bool action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; + virtual bool action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; virtual bool shortcut_match(const Ref &p_event, bool p_exact_match = true) const; virtual bool is_action_type() const { return true; } @@ -429,7 +429,7 @@ class InputEventJoypadMotion : public InputEvent { virtual bool is_pressed() const; - virtual bool action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; + virtual bool action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; virtual bool shortcut_match(const Ref &p_event, bool p_exact_match = true) const; virtual bool is_action_type() const { return true; } @@ -457,7 +457,7 @@ class InputEventJoypadButton : public InputEvent { void set_pressure(float p_pressure); float get_pressure() const; - virtual bool action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; + virtual bool action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; virtual bool shortcut_match(const Ref &p_event, bool p_exact_match = true) const; virtual bool is_action_type() const { return true; } @@ -544,9 +544,9 @@ class InputEventAction : public InputEvent { virtual bool is_action(const StringName &p_action) const; - virtual bool action_match(const Ref &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; - + virtual bool action_match(const Ref &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const; virtual bool shortcut_match(const Ref &p_event, bool p_exact_match = true) const; + virtual bool is_action_type() const { return true; } virtual String as_text() const;