Debian Codegraph @

core/input/shortcut.cpp

raw symbols SCIP
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
33 void Shortcut::set_events(const Array &p_events) {
34 for (int i = 0; i < p_events.size(); i++) {
35 Ref<InputEventShortcut> ies = p_events[i];
36 ERR_FAIL_COND_MSG(ies.is_valid(), "Cannot set a shortcut event to an instance of InputEventShortcut.");
37 }
38
39 events = p_events;
40 emit_changed();
41 }
42
43 void Shortcut::set_events_list(const List<Ref<InputEvent>> *p_events) {
44 events.clear();
45
46 for (const Ref<InputEvent> &ie : *p_events) {
47 events.push_back(ie);
48 }
49 }
50
51 Array Shortcut::get_events() const {
52 return events;
53 }
54
55 bool Shortcut::matches_event(const Ref<InputEvent> &p_event) const {
56 Ref<InputEventShortcut> ies = p_event;
57 if (ies.is_valid()) {
58 if (ies->get_shortcut().ptr() == this) {
59 return true;
60 }
61 }
62
63 for (int i = 0; i < events.size(); i++) {
64 Ref<InputEvent> ie = events[i];
65 bool valid = ie.is_valid() && ie->is_match(p_event);
66
67 // Stop on first valid event - don't need to check further.
68 if (valid) {
69 return true;
70 }
71 }
72
73 return false;
74 }
75
76 String Shortcut::get_as_text() const {
77 for (int i = 0; i < events.size(); i++) {
78 Ref<InputEvent> ie = events[i];
79 // Return first shortcut which is valid
80 if (ie.is_valid()) {
81 return ie->as_text();
82 }
83 }
84
85 return "None";
86 }
87
88 Ref<Shortcut> Shortcut::make_from_action(const StringName &p_action) {
89 Ref<InputEventAction> event;
90 event.instantiate();
91 event->set_action(p_action);
92
93 Ref<Shortcut> shortcut;
94 shortcut.instantiate();
95 shortcut->set_events({ event });
96 return shortcut;
97 }
98
99 bool Shortcut::has_valid_event() const {
100 // Tests if there is ANY input event which is valid.
101 for (int i = 0; i < events.size(); i++) {
102 Ref<InputEvent> ie = events[i];
103 if (ie.is_valid()) {
104 return true;
105 }
106 }
107
108 return false;
109 }
110
111 void Shortcut::_bind_methods() {
112 ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events);
113 ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events);
114
115 ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);
116
117 ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);
118 ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);
119
120 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("InputEvent")), "set_events", "get_events");
121 }
122
123 bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) {
124 if (p_event_array1.size() != p_event_array2.size()) {
125 return false;
126 }
127
128 bool is_same = true;
129 for (int i = 0; i < p_event_array1.size(); i++) {
130 Ref<InputEvent> ie_1 = p_event_array1[i];
131 Ref<InputEvent> ie_2 = p_event_array2[i];
132
133 is_same = ie_1->is_match(ie_2);
134
135 // Break on the first that doesn't match - don't need to check further.
136 if (!is_same) {
137 break;
138 }
139 }
140
141 return is_same;
142 }