summaryrefslogtreecommitdiff
path: root/core/input
diff options
context:
space:
mode:
authorMarcel Admiraal <madmiraal@users.noreply.github.com>2021-05-13 18:13:24 +0100
committerMarcel Admiraal <madmiraal@users.noreply.github.com>2021-05-19 11:43:02 +0100
commit7104229a85acbc30bf5dcc9c8a0ada8571910456 (patch)
tree06ea7e0bd69e93d88d32039ece8375720f3e33e9 /core/input
parentd0aaf4a1fd9e9943436ab6255a6d52257a794ce6 (diff)
Fix InputMap.action_erase_event() failing to erase events correctly.
Diffstat (limited to 'core/input')
-rw-r--r--core/input/input_event.cpp85
-rw-r--r--core/input/input_event.h16
-rw-r--r--core/input/input_map.cpp5
3 files changed, 59 insertions, 47 deletions
diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp
index 6f063c217f..9b962ff37c 100644
--- a/core/input/input_event.cpp
+++ b/core/input/input_event.cpp
@@ -88,7 +88,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::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
return false;
}
@@ -110,7 +110,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("is_match", "event", "exact_match"), &InputEvent::is_match, DEFVAL(true));
ClassDB::bind_method(D_METHOD("is_action_type"), &InputEvent::is_action_type);
@@ -194,6 +194,23 @@ void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModif
set_meta_pressed(event->is_meta_pressed());
}
+uint32_t InputEventWithModifiers::get_modifiers_mask() const {
+ uint32_t mask = 0;
+ if (is_ctrl_pressed()) {
+ mask |= KEY_MASK_CTRL;
+ }
+ if (is_shift_pressed()) {
+ mask |= KEY_MASK_SHIFT;
+ }
+ if (is_alt_pressed()) {
+ mask |= KEY_MASK_ALT;
+ }
+ if (is_meta_pressed()) {
+ mask |= KEY_MASK_META;
+ }
+ return mask;
+}
+
String InputEventWithModifiers::as_text() const {
Vector<String> mod_names;
@@ -313,39 +330,11 @@ bool InputEventKey::is_echo() const {
}
uint32_t InputEventKey::get_keycode_with_modifiers() const {
- uint32_t sc = keycode;
- if (is_ctrl_pressed()) {
- sc |= KEY_MASK_CTRL;
- }
- if (is_alt_pressed()) {
- sc |= KEY_MASK_ALT;
- }
- if (is_shift_pressed()) {
- sc |= KEY_MASK_SHIFT;
- }
- if (is_meta_pressed()) {
- sc |= KEY_MASK_META;
- }
-
- return sc;
+ return keycode | get_modifiers_mask();
}
uint32_t InputEventKey::get_physical_keycode_with_modifiers() const {
- uint32_t sc = physical_keycode;
- if (is_ctrl_pressed()) {
- sc |= KEY_MASK_CTRL;
- }
- if (is_alt_pressed()) {
- sc |= KEY_MASK_ALT;
- }
- if (is_shift_pressed()) {
- sc |= KEY_MASK_SHIFT;
- }
- if (is_meta_pressed()) {
- sc |= KEY_MASK_META;
- }
-
- return sc;
+ return physical_keycode | get_modifiers_mask();
}
String InputEventKey::as_text() const {
@@ -442,16 +431,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::is_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_keycode_with_modifiers();
- uint32_t event_code = key->get_keycode_with_modifiers();
-
- return code == event_code;
+ return keycode == key->keycode &&
+ (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask());
}
void InputEventKey::_bind_methods() {
@@ -599,6 +586,16 @@ bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool *p
return match;
}
+bool InputEventMouseButton::is_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());
+}
+
static const char *_mouse_button_descriptions[9] = {
TTRC("Left Mouse Button"),
TTRC("Right Mouse Button"),
@@ -904,6 +901,16 @@ bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool *
return match;
}
+bool InputEventJoypadMotion::is_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)));
+}
+
static const char *_joy_axis_descriptions[JOY_AXIS_MAX] = {
TTRC("Left Stick X-Axis, Joystick 0 X-Axis"),
TTRC("Left Stick Y-Axis, Joystick 0 Y-Axis"),
@@ -987,7 +994,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::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
Ref<InputEventJoypadButton> button = p_event;
if (button.is_null()) {
return false;
@@ -1229,7 +1236,7 @@ float InputEventAction::get_strength() const {
return strength;
}
-bool InputEventAction::shortcut_match(const Ref<InputEvent> &p_event) const {
+bool InputEventAction::is_match(const Ref<InputEvent> &p_event, bool p_exact_match) const {
if (p_event.is_null()) {
return false;
}
diff --git a/core/input/input_event.h b/core/input/input_event.h
index eed0d79326..3ef135221d 100644
--- a/core/input/input_event.h
+++ b/core/input/input_event.h
@@ -142,7 +142,8 @@ public:
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 is_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; }
@@ -212,6 +213,8 @@ public:
void set_modifiers_from_event(const InputEventWithModifiers *event);
+ uint32_t get_modifiers_mask() const;
+
virtual String as_text() const override;
virtual String to_string() override;
@@ -252,7 +255,7 @@ public:
uint32_t get_physical_keycode_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 override;
- virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
+ virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
virtual bool is_action_type() const override { return true; }
@@ -313,7 +316,9 @@ public:
bool is_double_click() const;
virtual Ref<InputEvent> xformed_by(const Transform2D &p_xform, const Vector2 &p_local_ofs = Vector2()) const override;
+
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
+ virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
virtual bool is_action_type() const override { return true; }
virtual String as_text() const override;
@@ -373,6 +378,7 @@ public:
virtual bool is_pressed() const override;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
+ virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
virtual bool is_action_type() const override { return true; }
virtual String as_text() const override;
@@ -401,9 +407,10 @@ public:
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 override;
- virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
+ virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
virtual bool is_action_type() const override { return true; }
+
virtual String as_text() const override;
virtual String to_string() override;
@@ -491,9 +498,10 @@ public:
virtual bool is_action(const StringName &p_action) const;
virtual bool action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const override;
+ virtual bool is_match(const Ref<InputEvent> &p_event, bool p_exact_match = true) const override;
- virtual bool shortcut_match(const Ref<InputEvent> &p_event) const override;
virtual bool is_action_type() const override { return true; }
+
virtual String as_text() const override;
virtual String to_string() override;
diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp
index 7421909650..2cc8f35418 100644
--- a/core/input/input_map.cpp
+++ b/core/input/input_map.cpp
@@ -130,12 +130,9 @@ List<Ref<InputEvent>>::Element *InputMap::_find_event(Action &p_action, const Re
for (List<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
const Ref<InputEvent> e = E->get();
- //if (e.type != Ref<InputEvent>::KEY && e.device != p_event.device) -- unsure about the KEY comparison, why is this here?
- // continue;
-
int device = e->get_device();
if (device == ALL_DEVICES || device == p_event->get_device()) {
- if (p_exact_match && e->shortcut_match(p_event)) {
+ if (p_exact_match && e->is_match(p_event, true)) {
return E;
} else if (!p_exact_match && e->action_match(p_event, p_pressed, p_strength, p_raw_strength, p_action.deadzone)) {
return E;