| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  shortcut.cpp                                                          */ | 
|---|
| 3 | /**************************************************************************/ | 
|---|
| 4 | /*                         This file is part of:                          */ | 
|---|
| 5 | /*                             GODOT ENGINE                               */ | 
|---|
| 6 | /*                        https://godotengine.org                         */ | 
|---|
| 7 | /**************************************************************************/ | 
|---|
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
|---|
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
|---|
| 10 | /*                                                                        */ | 
|---|
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
|---|
| 12 | /* a copy of this software and associated documentation files (the        */ | 
|---|
| 13 | /* "Software"), to deal in the Software without restriction, including    */ | 
|---|
| 14 | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
|---|
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
|---|
| 16 | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
|---|
| 17 | /* the following conditions:                                              */ | 
|---|
| 18 | /*                                                                        */ | 
|---|
| 19 | /* The above copyright notice and this permission notice shall be         */ | 
|---|
| 20 | /* included in all copies or substantial portions of the Software.        */ | 
|---|
| 21 | /*                                                                        */ | 
|---|
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
|---|
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
|---|
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
|---|
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
|---|
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
|---|
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
|---|
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
|---|
| 29 | /**************************************************************************/ | 
|---|
| 30 |  | 
|---|
| 31 | #include "shortcut.h" | 
|---|
| 32 | #include "core/os/keyboard.h" | 
|---|
| 33 |  | 
|---|
| 34 | void Shortcut::set_events(const Array &p_events) { | 
|---|
| 35 | for (int i = 0; i < p_events.size(); i++) { | 
|---|
| 36 | Ref<InputEventShortcut> ies = p_events[i]; | 
|---|
| 37 | ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut."); | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 | events = p_events; | 
|---|
| 41 | emit_changed(); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) { | 
|---|
| 45 | events.clear(); | 
|---|
| 46 |  | 
|---|
| 47 | for (const Ref<InputEvent> &ie : *p_events) { | 
|---|
| 48 | events.push_back(ie); | 
|---|
| 49 | } | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | Array Shortcut::get_events() const { | 
|---|
| 53 | return events; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const { | 
|---|
| 57 | Ref<InputEventShortcut> ies = p_event; | 
|---|
| 58 | if (ies.is_valid()) { | 
|---|
| 59 | if (ies->get_shortcut().ptr() == this) { | 
|---|
| 60 | return true; | 
|---|
| 61 | } | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | for (int i = 0; i < events.size(); i++) { | 
|---|
| 65 | Ref<InputEvent> ie = events[i]; | 
|---|
| 66 | bool valid = ie.is_valid() && ie->is_match(p_event); | 
|---|
| 67 |  | 
|---|
| 68 | // Stop on first valid event - don't need to check further. | 
|---|
| 69 | if (valid) { | 
|---|
| 70 | return true; | 
|---|
| 71 | } | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | return false; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | String Shortcut::get_as_text() const { | 
|---|
| 78 | for (int i = 0; i < events.size(); i++) { | 
|---|
| 79 | Ref<InputEvent> ie = events[i]; | 
|---|
| 80 | // Return first shortcut which is valid | 
|---|
| 81 | if (ie.is_valid()) { | 
|---|
| 82 | return ie->as_text(); | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | return "None"; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | bool Shortcut::has_valid_event() const { | 
|---|
| 90 | // Tests if there is ANY input event which is valid. | 
|---|
| 91 | for (int i = 0; i < events.size(); i++) { | 
|---|
| 92 | Ref<InputEvent> ie = events[i]; | 
|---|
| 93 | if (ie.is_valid()) { | 
|---|
| 94 | return true; | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | return false; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void Shortcut::_bind_methods() { | 
|---|
| 102 | ClassDB::bind_method(D_METHOD( "set_events", "events"), &Shortcut::set_events); | 
|---|
| 103 | ClassDB::bind_method(D_METHOD( "get_events"), &Shortcut::get_events); | 
|---|
| 104 |  | 
|---|
| 105 | ClassDB::bind_method(D_METHOD( "has_valid_event"), &Shortcut::has_valid_event); | 
|---|
| 106 |  | 
|---|
| 107 | ClassDB::bind_method(D_METHOD( "matches_event", "event"), &Shortcut::matches_event); | 
|---|
| 108 | ClassDB::bind_method(D_METHOD( "get_as_text"), &Shortcut::get_as_text); | 
|---|
| 109 |  | 
|---|
| 110 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT( "InputEvent")), "set_events", "get_events"); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) { | 
|---|
| 114 | if (p_event_array1.size() != p_event_array2.size()) { | 
|---|
| 115 | return false; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | bool is_same = true; | 
|---|
| 119 | for (int i = 0; i < p_event_array1.size(); i++) { | 
|---|
| 120 | Ref<InputEvent> ie_1 = p_event_array1[i]; | 
|---|
| 121 | Ref<InputEvent> ie_2 = p_event_array2[i]; | 
|---|
| 122 |  | 
|---|
| 123 | is_same = ie_1->is_match(ie_2); | 
|---|
| 124 |  | 
|---|
| 125 | // Break on the first that doesn't match - don't need to check further. | 
|---|
| 126 | if (!is_same) { | 
|---|
| 127 | break; | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | return is_same; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|