diff options
author | Eric M <itsjusteza@gmail.com> | 2021-06-21 11:34:50 +1000 |
---|---|---|
committer | Eric M <itsjusteza@gmail.com> | 2021-10-01 18:04:28 +1000 |
commit | ad30b0a8dd793efa2ca60b424f3dc1e25975de68 (patch) | |
tree | 87ace060f385d9ac8c8d6b1d6cc11520ef807296 /core/input | |
parent | 4baddc112647edf658692a484a481666a77e41b2 (diff) |
Allow shortcuts to have any number of bindings. Updated UI as required.
Diffstat (limited to 'core/input')
-rw-r--r-- | core/input/shortcut.cpp | 84 | ||||
-rw-r--r-- | core/input/shortcut.h | 12 |
2 files changed, 78 insertions, 18 deletions
diff --git a/core/input/shortcut.cpp b/core/input/shortcut.cpp index d0cb08724e..30e35190e4 100644 --- a/core/input/shortcut.cpp +++ b/core/input/shortcut.cpp @@ -31,14 +31,26 @@ #include "shortcut.h" #include "core/os/keyboard.h" -void Shortcut::set_event(const Ref<InputEvent> &p_event) { - ERR_FAIL_COND_MSG(Object::cast_to<InputEventShortcut>(*p_event), "Cannot set a shortcut event to an instance of InputEventShortcut."); - event = p_event; +void Shortcut::set_events(const Array &p_events) { + for (int i = 0; i < p_events.size(); i++) { + Ref<InputEventShortcut> ies = p_events[i]; + ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut."); + } + + events = p_events; emit_changed(); } -Ref<InputEvent> Shortcut::get_event() const { - return event; +void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) { + events.clear(); + + for (const Ref<InputEvent> &ie : *p_events) { + events.push_back(ie); + } +} + +Array Shortcut::get_events() const { + return events; } bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const { @@ -48,29 +60,73 @@ bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const { return true; } } - return event.is_valid() && event->is_match(p_event, true); + + for (int i = 0; i < events.size(); i++) { + Ref<InputEvent> ie = events[i]; + bool valid = ie.is_valid() && ie->is_match(p_event); + + // Stop on first valid event - don't need to check further. + if (valid) { + return true; + } + } + + return false; } String Shortcut::get_as_text() const { - if (event.is_valid()) { - return event->as_text(); - } else { - return "None"; + for (int i = 0; i < events.size(); i++) { + Ref<InputEvent> ie = events[i]; + // Return first shortcut which is valid + if (ie.is_valid()) { + return ie->as_text(); + } } + + return "None"; } bool Shortcut::has_valid_event() const { - return event.is_valid(); + // Tests if there is ANY input event which is valid. + for (int i = 0; i < events.size(); i++) { + Ref<InputEvent> ie = events[i]; + if (ie.is_valid()) { + return true; + } + } + + return false; } void Shortcut::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_event", "event"), &Shortcut::set_event); - ClassDB::bind_method(D_METHOD("get_event"), &Shortcut::get_event); + ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events); + ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events); ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event); ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event); ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "event", PROPERTY_HINT_RESOURCE_TYPE, "InputEvent"), "set_event", "get_event"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, "InputEvent")), "set_events", "get_events"); +} + +bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) { + if (p_event_array1.size() != p_event_array2.size()) { + return false; + } + + bool is_same = true; + for (int i = 0; i < p_event_array1.size(); i++) { + Ref<InputEvent> ie_1 = p_event_array1[i]; + Ref<InputEvent> ie_2 = p_event_array2[i]; + + is_same = ie_1->is_match(ie_2); + + // Break on the first that doesn't match - don't need to check further. + if (!is_same) { + break; + } + } + + return is_same; } diff --git a/core/input/shortcut.h b/core/input/shortcut.h index 249dd1971f..a989b10626 100644 --- a/core/input/shortcut.h +++ b/core/input/shortcut.h @@ -37,18 +37,22 @@ class Shortcut : public Resource { GDCLASS(Shortcut, Resource); - Ref<InputEvent> event; + Array events; protected: static void _bind_methods(); public: - void set_event(const Ref<InputEvent> &p_shortcut); - Ref<InputEvent> get_event() const; + void set_events(const Array &p_events); + Array get_events() const; + + void set_events_list(const List<Ref<InputEvent>> *p_events); + bool matches_event(const Ref<InputEvent> &p_event) const; bool has_valid_event() const; String get_as_text() const; -}; + static bool is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2); +}; #endif // SHORTCUT_H |