Skip to content

Commit

Permalink
Merge pull request #54174 from nathanfranke/3.x-fix-exact-match
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Feb 2, 2022
2 parents 78f2308 + 2232168 commit 98ed117
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
4 changes: 1 addition & 3 deletions core/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ List<Ref<InputEvent>>::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;
}
}
Expand Down
39 changes: 21 additions & 18 deletions core/os/input_event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ String InputEvent::as_text() const {
return String();
}

bool InputEvent::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
bool InputEvent::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
return false;
}

Expand Down Expand Up @@ -288,25 +288,21 @@ String InputEventKey::as_text() const {
return kc;
}

bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
bool InputEventKey::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
Ref<InputEventKey> 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) {
Expand Down Expand Up @@ -466,13 +462,16 @@ Ref<InputEvent> InputEventMouseButton::xformed_by(const Transform2D &p_xform, co
return mb;
}

bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
bool InputEventMouseButton::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
Ref<InputEventMouseButton> 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) {
Expand Down Expand Up @@ -730,13 +729,17 @@ bool InputEventJoypadMotion::is_pressed() const {
return Math::abs(axis_value) >= 0.5f;
}

bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
bool InputEventJoypadMotion::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
Ref<InputEventJoypadMotion> 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);
Expand Down Expand Up @@ -819,7 +822,7 @@ float InputEventJoypadButton::get_pressure() const {
return pressure;
}

bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
bool InputEventJoypadButton::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
Ref<InputEventJoypadButton> jb = p_event;
if (jb.is_null()) {
return false;
Expand Down Expand Up @@ -1059,7 +1062,7 @@ bool InputEventAction::is_action(const StringName &p_action) const {
return action == p_action;
}

bool InputEventAction::action_match(const Ref<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
bool InputEventAction::action_match(const Ref<InputEvent> &p_event, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const {
Ref<InputEventAction> act = p_event;
if (act.is_null()) {
return false;
Expand Down
14 changes: 7 additions & 7 deletions core/os/input_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,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 action_match(const Ref<InputEvent> &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<InputEvent> &p_event, bool p_exact_match = true) const;
virtual bool is_action_type() const;

Expand Down Expand Up @@ -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<InputEvent> &p_event, bool *p_pressed, float *p_strength, float *p_raw_strength, float p_deadzone) const;
virtual bool action_match(const Ref<InputEvent> &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<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
Expand Down Expand Up @@ -371,7 +371,7 @@ class InputEventMouseButton : public InputEventMouse {
bool is_doubleclick() const;

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 action_match(const Ref<InputEvent> &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<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
Expand Down Expand Up @@ -429,7 +429,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 action_match(const Ref<InputEvent> &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<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
Expand Down Expand Up @@ -457,7 +457,7 @@ class InputEventJoypadButton : public InputEvent {
void set_pressure(float p_pressure);
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 action_match(const Ref<InputEvent> &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<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
Expand Down Expand Up @@ -544,9 +544,9 @@ class InputEventAction : public InputEvent {

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;

virtual bool action_match(const Ref<InputEvent> &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<InputEvent> &p_event, bool p_exact_match = true) const;

virtual bool is_action_type() const { return true; }
virtual String as_text() const;

Expand Down

0 comments on commit 98ed117

Please sign in to comment.