Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/input/shortcut.cpp
9903 views
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
bool Shortcut::has_valid_event() const {
89
// Tests if there is ANY input event which is valid.
90
for (int i = 0; i < events.size(); i++) {
91
Ref<InputEvent> ie = events[i];
92
if (ie.is_valid()) {
93
return true;
94
}
95
}
96
97
return false;
98
}
99
100
void Shortcut::_bind_methods() {
101
ClassDB::bind_method(D_METHOD("set_events", "events"), &Shortcut::set_events);
102
ClassDB::bind_method(D_METHOD("get_events"), &Shortcut::get_events);
103
104
ClassDB::bind_method(D_METHOD("has_valid_event"), &Shortcut::has_valid_event);
105
106
ClassDB::bind_method(D_METHOD("matches_event", "event"), &Shortcut::matches_event);
107
ClassDB::bind_method(D_METHOD("get_as_text"), &Shortcut::get_as_text);
108
109
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "events", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("InputEvent")), "set_events", "get_events");
110
}
111
112
bool Shortcut::is_event_array_equal(const Array &p_event_array1, const Array &p_event_array2) {
113
if (p_event_array1.size() != p_event_array2.size()) {
114
return false;
115
}
116
117
bool is_same = true;
118
for (int i = 0; i < p_event_array1.size(); i++) {
119
Ref<InputEvent> ie_1 = p_event_array1[i];
120
Ref<InputEvent> ie_2 = p_event_array2[i];
121
122
is_same = ie_1->is_match(ie_2);
123
124
// Break on the first that doesn't match - don't need to check further.
125
if (!is_same) {
126
break;
127
}
128
}
129
130
return is_same;
131
}
132
133