Debian Codegraph @

core/input/input_map.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* input_map.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 "input_map.h"
32 #include "input_map.compat.inc"
33
34 #include "core/config/project_settings.h"
35 #include "core/input/input.h"
36 #include "core/os/keyboard.h"
37 #include "core/os/os.h"
38 #include "core/variant/typed_array.h"
39
40 void InputMap::_bind_methods() {
41 ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
42 ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::get_actions);
43 ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(DEFAULT_DEADZONE));
44 ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action);
45
46 ClassDB::bind_method(D_METHOD("get_action_description", "action"), &InputMap::get_action_description);
47
48 ClassDB::bind_method(D_METHOD("action_set_deadzone", "action", "deadzone"), &InputMap::action_set_deadzone);
49 ClassDB::bind_method(D_METHOD("action_get_deadzone", "action"), &InputMap::action_get_deadzone);
50 ClassDB::bind_method(D_METHOD("action_add_event", "action", "event"), &InputMap::action_add_event);
51 ClassDB::bind_method(D_METHOD("action_has_event", "action", "event"), &InputMap::action_has_event);
52 ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
53 ClassDB::bind_method(D_METHOD("action_erase_events", "action"), &InputMap::action_erase_events);
54 ClassDB::bind_method(D_METHOD("action_get_events", "action"), &InputMap::_action_get_events);
55 ClassDB::bind_method(D_METHOD("event_is_action", "event", "action", "exact_match"), &InputMap::event_is_action, DEFVAL(false));
56 ClassDB::bind_method(D_METHOD("load_from_project_settings"), &InputMap::load_from_project_settings);
57
58 ADD_SIGNAL(MethodInfo("project_settings_loaded"));
59 }
60
61 /**
62 * Returns an nonexistent action error message with a suggestion of the closest
63 * matching action name (if possible).
64 */
65 String InputMap::suggest_actions(const StringName &p_action) const {
66 StringName closest_action;
67 float closest_similarity = 0.0;
68
69 // Find the most action with the most similar name.
70 for (const KeyValue<StringName, Action> &kv : input_map) {
71 const float similarity = String(kv.key).similarity(p_action);
72
73 if (similarity > closest_similarity) {
74 closest_action = kv.key;
75 closest_similarity = similarity;
76 }
77 }
78
79 String error_message = vformat("The InputMap action \"%s\" doesn't exist.", p_action);
80
81 if (closest_similarity >= 0.4) {
82 // Only include a suggestion in the error message if it's similar enough.
83 error_message += vformat(" Did you mean \"%s\"?", closest_action);
84 }
85 return error_message;
86 }
87
88 #ifdef TOOLS_ENABLED
89 void InputMap::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
90 const String pf = p_function;
91 bool first_argument_is_action = false;
92 if (p_idx == 0) {
93 first_argument_is_action = (pf == "has_action" || pf == "erase_action" ||
94 pf == "action_set_deadzone" || pf == "action_get_deadzone" ||
95 pf == "action_has_event" || pf == "action_add_event" || pf == "action_get_events" ||
96 pf == "action_erase_event" || pf == "action_erase_events");
97 }
98 if (first_argument_is_action || (p_idx == 1 && pf == "event_is_action")) {
99 // Cannot rely on `get_actions()`, otherwise the actions would be in the context of the Editor (no user-defined actions).
100 List<PropertyInfo> pinfo;
101 ProjectSettings::get_singleton()->get_property_list(&pinfo);
102
103 for (const PropertyInfo &pi : pinfo) {
104 if (!pi.name.begins_with("input/")) {
105 continue;
106 }
107
108 String name = pi.name.substr(pi.name.find_char('/') + 1);
109 r_options->push_back(name.quote());
110 }
111 }
112
113 Object::get_argument_options(p_function, p_idx, r_options);
114 }
115 #endif
116
117 void InputMap::add_action(const StringName &p_action, float p_deadzone) {
118 ERR_FAIL_COND_MSG(input_map.has(p_action), vformat("InputMap already has action \"%s\".", String(p_action)));
119 input_map[p_action] = Action();
120 static int last_id = 1;
121 input_map[p_action].id = last_id;
122 input_map[p_action].deadzone = p_deadzone;
123 last_id++;
124 }
125
126 void InputMap::erase_action(const StringName &p_action) {
127 ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
128
129 input_map.erase(p_action);
130 }
131
132 TypedArray<StringName> InputMap::get_actions() {
133 TypedArray<StringName> ret;
134
135 ret.resize(input_map.size());
136
137 uint32_t i = 0;
138 for (const KeyValue<StringName, Action> &kv : input_map) {
139 ret[i] = kv.key;
140 i++;
141 }
142
143 return ret;
144 }
145
146 List<Ref<InputEvent>>::Element *InputMap::_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const {
147 ERR_FAIL_COND_V(p_event.is_null(), nullptr);
148
149 int i = 0;
150 for (List<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
151 int device = E->get()->get_device();
152 if (device == ALL_DEVICES || device == p_event->get_device()) {
153 if (E->get()->action_match(p_event, p_exact_match, p_action.deadzone, r_pressed, r_strength, r_raw_strength)) {
154 if (r_event_index) {
155 *r_event_index = i;
156 }
157 return E;
158 }
159 }
160 i++;
161 }
162
163 return nullptr;
164 }
165
166 bool InputMap::has_action(const StringName &p_action) const {
167 return input_map.has(p_action);
168 }
169
170 String InputMap::get_action_description(const StringName &p_action) const {
171 ERR_FAIL_COND_V_MSG(!input_map.has(p_action), String(), suggest_actions(p_action));
172
173 String ret;
174 const List<Ref<InputEvent>> &inputs = input_map[p_action].inputs;
175 for (Ref<InputEventKey> iek : inputs) {
176 if (iek.is_valid()) {
177 if (!ret.is_empty()) {
178 ret += RTR(" or ");
179 }
180 ret += iek->as_text();
181 }
182 }
183 if (ret.is_empty()) {
184 ret = RTR("Action has no bound inputs");
185 }
186 return ret;
187 }
188
189 float InputMap::action_get_deadzone(const StringName &p_action) {
190 ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, suggest_actions(p_action));
191
192 return input_map[p_action].deadzone;
193 }
194
195 void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
196 ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
197
198 input_map[p_action].deadzone = p_deadzone;
199 }
200
201 void InputMap::action_add_event(const StringName &p_action, RequiredParam<InputEvent> rp_event) {
202 EXTRACT_PARAM_OR_FAIL_MSG(p_event, rp_event, "It's not a reference to a valid InputEvent object.");
203 ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
204 if (_find_event(input_map[p_action], p_event, true)) {
205 return; // Already added.
206 }
207
208 input_map[p_action].inputs.push_back(p_event);
209 }
210
211 bool InputMap::action_has_event(const StringName &p_action, RequiredParam<InputEvent> rp_event) {
212 EXTRACT_PARAM_OR_FAIL_V(p_event, rp_event, false);
213 ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, suggest_actions(p_action));
214 return (_find_event(input_map[p_action], p_event, true) != nullptr);
215 }
216
217 void InputMap::action_erase_event(const StringName &p_action, RequiredParam<InputEvent> rp_event) {
218 EXTRACT_PARAM_OR_FAIL(p_event, rp_event);
219 ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
220
221 List<Ref<InputEvent>>::Element *E = _find_event(input_map[p_action], p_event, true);
222 if (E) {
223 input_map[p_action].inputs.erase(E);
224
225 if (Input::get_singleton()->is_action_pressed(p_action)) {
226 Input::get_singleton()->action_release(p_action);
227 }
228 }
229 }
230
231 void InputMap::action_erase_events(const StringName &p_action) {
232 ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
233
234 if (Input::get_singleton()->is_action_pressed(p_action)) {
235 Input::get_singleton()->action_release(p_action);
236 }
237
238 input_map[p_action].inputs.clear();
239 }
240
241 TypedArray<InputEvent> InputMap::_action_get_events(const StringName &p_action) {
242 TypedArray<InputEvent> ret;
243 const List<Ref<InputEvent>> *al = action_get_events(p_action);
244 if (al) {
245 for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) {
246 ret.push_back(E->get());
247 }
248 }
249
250 return ret;
251 }
252
253 const List<Ref<InputEvent>> *InputMap::action_get_events(const StringName &p_action) {
254 HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
255 if (!E) {
256 return nullptr;
257 }
258
259 return &E->value.inputs;
260 }
261
262 bool InputMap::event_is_action(RequiredParam<InputEvent> rp_event, const StringName &p_action, bool p_exact_match) const {
263 EXTRACT_PARAM_OR_FAIL_V(p_event, rp_event, false);
264 return event_get_action_status(p_event, p_action, p_exact_match);
265 }
266
267 int InputMap::event_get_index(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
268 int index = -1;
269 bool valid = event_get_action_status(p_event, p_action, p_exact_match, nullptr, nullptr, nullptr, &index);
270 return valid ? index : -1;
271 }
272
273 bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const {
274 HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
275 ERR_FAIL_COND_V_MSG(!E, false, suggest_actions(p_action));
276
277 Ref<InputEventAction> input_event_action = p_event;
278 if (input_event_action.is_valid()) {
279 const bool pressed = input_event_action->is_pressed();
280 if (r_pressed != nullptr) {
281 *r_pressed = pressed;
282 }
283 const float strength = pressed ? input_event_action->get_strength() : 0.0f;
284 if (r_strength != nullptr) {
285 *r_strength = strength;
286 }
287 if (r_raw_strength != nullptr) {
288 *r_raw_strength = strength;
289 }
290 if (r_event_index) {
291 if (input_event_action->get_event_index() >= 0) {
292 *r_event_index = input_event_action->get_event_index();
293 } else {
294 *r_event_index = E->value.inputs.size();
295 }
296 }
297 return input_event_action->get_action() == p_action;
298 }
299
300 List<Ref<InputEvent>>::Element *event = _find_event(E->value, p_event, p_exact_match, r_pressed, r_strength, r_raw_strength, r_event_index);
301 return event != nullptr;
302 }
303
304 const HashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
305 return input_map;
306 }
307
308 void InputMap::load_from_project_settings() {
309 input_map.clear();
310
311 List<PropertyInfo> pinfo;
312 ProjectSettings::get_singleton()->get_property_list(&pinfo);
313
314 for (const PropertyInfo &pi : pinfo) {
315 if (!pi.name.begins_with("input/")) {
316 continue;
317 }
318
319 String name = pi.name.substr(pi.name.find_char('/') + 1);
320
321 Dictionary action = GLOBAL_GET(pi.name);
322 float deadzone = action.has("deadzone") ? (float)action["deadzone"] : DEFAULT_DEADZONE;
323 Array events = action["events"];
324
325 add_action(name, deadzone);
326 for (int i = 0; i < events.size(); i++) {
327 Ref<InputEvent> event = events[i];
328 if (event.is_null()) {
329 continue;
330 }
331 action_add_event(name, event);
332 }
333 }
334
335 emit_signal("project_settings_loaded");
336 }
337
338 struct _BuiltinActionDisplayName {
339 const char *name;
340 const char *display_name;
341 };
342
343 static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
344 /* clang-format off */
345 { "ui_accept", TTRC("Accept") },
346 { "ui_select", TTRC("Select") },
347 { "ui_cancel", TTRC("Cancel") },
348 { "ui_close_dialog", TTRC("Close Dialog") },
349 { "ui_focus_next", TTRC("Focus Next") },
350 { "ui_focus_prev", TTRC("Focus Prev") },
351 { "ui_left", TTRC("Left") },
352 { "ui_right", TTRC("Right") },
353 { "ui_up", TTRC("Up") },
354 { "ui_down", TTRC("Down") },
355 { "ui_page_up", TTRC("Page Up") },
356 { "ui_page_down", TTRC("Page Down") },
357 { "ui_home", TTRC("Home") },
358 { "ui_end", TTRC("End") },
359 { "ui_cut", TTRC("Cut") },
360 { "ui_copy", TTRC("Copy") },
361 { "ui_paste", TTRC("Paste") },
362 { "ui_focus_mode", TTRC("Toggle Tab Focus Mode") },
363 { "ui_undo", TTRC("Undo") },
364 { "ui_redo", TTRC("Redo") },
365 { "ui_text_completion_query", TTRC("Completion Query") },
366 { "ui_text_newline", TTRC("New Line") },
367 { "ui_text_newline_blank", TTRC("New Blank Line") },
368 { "ui_text_newline_above", TTRC("New Line Above") },
369 { "ui_text_indent", TTRC("Indent") },
370 { "ui_text_dedent", TTRC("Dedent") },
371 { "ui_text_backspace", TTRC("Backspace") },
372 { "ui_text_backspace_word", TTRC("Backspace Word") },
373 { "ui_text_backspace_all_to_left", TTRC("Backspace all to Left") },
374 { "ui_text_delete", TTRC("Delete") },
375 { "ui_text_delete_word", TTRC("Delete Word") },
376 { "ui_text_delete_all_to_right", TTRC("Delete all to Right") },
377 { "ui_text_caret_left", TTRC("Caret Left") },
378 { "ui_text_caret_word_left", TTRC("Caret Word Left") },
379 { "ui_text_caret_right", TTRC("Caret Right") },
380 { "ui_text_caret_word_right", TTRC("Caret Word Right") },
381 { "ui_text_caret_up", TTRC("Caret Up") },
382 { "ui_text_caret_down", TTRC("Caret Down") },
383 { "ui_text_caret_line_start", TTRC("Caret Line Start") },
384 { "ui_text_caret_line_end", TTRC("Caret Line End") },
385 { "ui_text_caret_page_up", TTRC("Caret Page Up") },
386 { "ui_text_caret_page_down", TTRC("Caret Page Down") },
387 { "ui_text_caret_document_start", TTRC("Caret Document Start") },
388 { "ui_text_caret_document_end", TTRC("Caret Document End") },
389 { "ui_text_caret_add_below", TTRC("Caret Add Below") },
390 { "ui_text_caret_add_above", TTRC("Caret Add Above") },
391 { "ui_text_scroll_up", TTRC("Scroll Up") },
392 { "ui_text_scroll_down", TTRC("Scroll Down") },
393 { "ui_text_select_all", TTRC("Select All") },
394 { "ui_text_select_word_under_caret", TTRC("Select Word Under Caret") },
395 { "ui_text_add_selection_for_next_occurrence", TTRC("Add Selection for Next Occurrence") },
396 { "ui_text_skip_selection_for_next_occurrence", TTRC("Skip Selection for Next Occurrence") },
397 { "ui_text_clear_carets_and_selection", TTRC("Clear Carets and Selection") },
398 { "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") },
399 { "ui_text_submit", TTRC("Submit Text") },
400 { "ui_graph_duplicate", TTRC("Duplicate Nodes") },
401 { "ui_graph_delete", TTRC("Delete Nodes") },
402 { "ui_graph_follow_left", TTRC("Follow Input Port Connection") },
403 { "ui_graph_follow_right", TTRC("Follow Output Port Connection") },
404 { "ui_filedialog_delete", TTRC("Delete") },
405 { "ui_filedialog_up_one_level", TTRC("Go Up One Level") },
406 { "ui_filedialog_refresh", TTRC("Refresh") },
407 { "ui_filedialog_show_hidden", TTRC("Show Hidden") },
408 { "ui_filedialog_find", TTRC("Find") },
409 { "ui_filedialog_focus_path", TTRC("Focus Path") },
410 { "ui_swap_input_direction", TTRC("Swap Input Direction") },
411 { "ui_unicode_start", TTRC("Start Unicode Character Input") },
412 { "ui_colorpicker_delete_preset", TTRC("ColorPicker: Delete Preset") },
413 { "ui_accessibility_drag_and_drop", TTRC("Accessibility: Keyboard Drag and Drop") },
414 { "", ""}
415 /* clang-format on */
416 };
417
418 String InputMap::get_builtin_display_name(const String &p_name) const {
419 const String name = p_name.get_slicec('.', 0);
420 constexpr int len = std_size(_builtin_action_display_names);
421 for (int i = 0; i < len; i++) {
422 if (_builtin_action_display_names[i].name == name) {
423 return _builtin_action_display_names[i].display_name;
424 }
425 }
426
427 return p_name;
428 }
429
430 const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
431 // Return cache if it has already been built.
432 if (default_builtin_cache.size()) {
433 return default_builtin_cache;
434 }
435
436 List<Ref<InputEvent>> inputs;
437 inputs.push_back(InputEventKey::create_reference(Key::ENTER));
438 inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
439 inputs.push_back(InputEventKey::create_reference(Key::SPACE));
440 default_builtin_cache.insert("ui_accept", inputs);
441
442 inputs = List<Ref<InputEvent>>();
443 inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::Y));
444 inputs.push_back(InputEventKey::create_reference(Key::SPACE));
445 default_builtin_cache.insert("ui_select", inputs);
446
447 inputs = List<Ref<InputEvent>>();
448 inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
449 default_builtin_cache.insert("ui_cancel", inputs);
450
451 inputs = List<Ref<InputEvent>>();
452 inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
453 default_builtin_cache.insert("ui_close_dialog", inputs);
454
455 inputs = List<Ref<InputEvent>>();
456 inputs.push_back(InputEventKey::create_reference(Key::W | KeyModifierMask::META));
457 inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
458 default_builtin_cache.insert("ui_close_dialog.macos", inputs);
459
460 inputs = List<Ref<InputEvent>>();
461 inputs.push_back(InputEventKey::create_reference(Key::TAB));
462 default_builtin_cache.insert("ui_focus_next", inputs);
463
464 inputs = List<Ref<InputEvent>>();
465 inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
466 default_builtin_cache.insert("ui_focus_prev", inputs);
467
468 inputs = List<Ref<InputEvent>>();
469 inputs.push_back(InputEventKey::create_reference(Key::LEFT));
470 inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_LEFT));
471 inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, -1.0));
472 default_builtin_cache.insert("ui_left", inputs);
473
474 inputs = List<Ref<InputEvent>>();
475 inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
476 inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_RIGHT));
477 inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, 1.0));
478 default_builtin_cache.insert("ui_right", inputs);
479
480 inputs = List<Ref<InputEvent>>();
481 inputs.push_back(InputEventKey::create_reference(Key::UP));
482 inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_UP));
483 inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, -1.0));
484 default_builtin_cache.insert("ui_up", inputs);
485
486 inputs = List<Ref<InputEvent>>();
487 inputs.push_back(InputEventKey::create_reference(Key::DOWN));
488 inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_DOWN));
489 inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, 1.0));
490 default_builtin_cache.insert("ui_down", inputs);
491
492 inputs = List<Ref<InputEvent>>();
493 inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
494 default_builtin_cache.insert("ui_page_up", inputs);
495
496 inputs = List<Ref<InputEvent>>();
497 inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
498 default_builtin_cache.insert("ui_page_down", inputs);
499
500 inputs = List<Ref<InputEvent>>();
501 inputs.push_back(InputEventKey::create_reference(Key::HOME));
502 default_builtin_cache.insert("ui_home", inputs);
503
504 inputs = List<Ref<InputEvent>>();
505 inputs.push_back(InputEventKey::create_reference(Key::END));
506 default_builtin_cache.insert("ui_end", inputs);
507
508 inputs = List<Ref<InputEvent>>();
509 default_builtin_cache.insert("ui_accessibility_drag_and_drop", inputs);
510
511 // ///// UI basic Shortcuts /////
512
513 inputs = List<Ref<InputEvent>>();
514 inputs.push_back(InputEventKey::create_reference(Key::X | KeyModifierMask::CMD_OR_CTRL));
515 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::SHIFT));
516 default_builtin_cache.insert("ui_cut", inputs);
517
518 inputs = List<Ref<InputEvent>>();
519 inputs.push_back(InputEventKey::create_reference(Key::C | KeyModifierMask::CMD_OR_CTRL));
520 inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::CMD_OR_CTRL));
521 default_builtin_cache.insert("ui_copy", inputs);
522
523 inputs = List<Ref<InputEvent>>();
524 inputs.push_back(InputEventKey::create_reference(Key::M | KeyModifierMask::CTRL));
525 default_builtin_cache.insert("ui_focus_mode", inputs);
526
527 inputs = List<Ref<InputEvent>>();
528 inputs.push_back(InputEventKey::create_reference(Key::V | KeyModifierMask::CMD_OR_CTRL));
529 inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::SHIFT));
530 default_builtin_cache.insert("ui_paste", inputs);
531
532 inputs = List<Ref<InputEvent>>();
533 inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL));
534 default_builtin_cache.insert("ui_undo", inputs);
535
536 inputs = List<Ref<InputEvent>>();
537 inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT));
538 inputs.push_back(InputEventKey::create_reference(Key::Y | KeyModifierMask::CMD_OR_CTRL));
539 default_builtin_cache.insert("ui_redo", inputs);
540
541 // ///// UI Text Input Shortcuts /////
542 inputs = List<Ref<InputEvent>>();
543 inputs.push_back(InputEventKey::create_reference(Key::SPACE | KeyModifierMask::CTRL));
544 default_builtin_cache.insert("ui_text_completion_query", inputs);
545
546 inputs = List<Ref<InputEvent>>();
547 inputs.push_back(InputEventKey::create_reference(Key::TAB));
548 inputs.push_back(InputEventKey::create_reference(Key::ENTER));
549 inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
550 default_builtin_cache.insert("ui_text_completion_accept", inputs);
551
552 inputs = List<Ref<InputEvent>>();
553 inputs.push_back(InputEventKey::create_reference(KeyModifierMask::SHIFT | Key::TAB));
554 inputs.push_back(InputEventKey::create_reference(KeyModifierMask::SHIFT | Key::ENTER));
555 inputs.push_back(InputEventKey::create_reference(KeyModifierMask::SHIFT | Key::KP_ENTER));
556 default_builtin_cache.insert("ui_text_completion_replace", inputs);
557
558 // Newlines
559 inputs = List<Ref<InputEvent>>();
560 inputs.push_back(InputEventKey::create_reference(Key::ENTER));
561 inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
562 default_builtin_cache.insert("ui_text_newline", inputs);
563
564 inputs = List<Ref<InputEvent>>();
565 inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::CMD_OR_CTRL));
566 inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::CMD_OR_CTRL));
567 default_builtin_cache.insert("ui_text_newline_blank", inputs);
568
569 inputs = List<Ref<InputEvent>>();
570 inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
571 inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
572 default_builtin_cache.insert("ui_text_newline_above", inputs);
573
574 // Indentation
575 inputs = List<Ref<InputEvent>>();
576 inputs.push_back(InputEventKey::create_reference(Key::TAB));
577 default_builtin_cache.insert("ui_text_indent", inputs);
578
579 inputs = List<Ref<InputEvent>>();
580 inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
581 default_builtin_cache.insert("ui_text_dedent", inputs);
582
583 // Text Backspace and Delete
584 inputs = List<Ref<InputEvent>>();
585 inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
586 inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::SHIFT));
587 default_builtin_cache.insert("ui_text_backspace", inputs);
588
589 inputs = List<Ref<InputEvent>>();
590 inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL));
591 default_builtin_cache.insert("ui_text_backspace_word", inputs);
592
593 inputs = List<Ref<InputEvent>>();
594 inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::ALT));
595 default_builtin_cache.insert("ui_text_backspace_word.macos", inputs);
596
597 inputs = List<Ref<InputEvent>>();
598 default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs);
599
600 inputs = List<Ref<InputEvent>>();
601 inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL));
602 default_builtin_cache.insert("ui_text_backspace_all_to_left.macos", inputs);
603
604 inputs = List<Ref<InputEvent>>();
605 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
606 default_builtin_cache.insert("ui_text_delete", inputs);
607
608 inputs = List<Ref<InputEvent>>();
609 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL));
610 default_builtin_cache.insert("ui_text_delete_word", inputs);
611
612 inputs = List<Ref<InputEvent>>();
613 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::ALT));
614 default_builtin_cache.insert("ui_text_delete_word.macos", inputs);
615
616 inputs = List<Ref<InputEvent>>();
617 default_builtin_cache.insert("ui_text_delete_all_to_right", inputs);
618
619 inputs = List<Ref<InputEvent>>();
620 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL));
621 default_builtin_cache.insert("ui_text_delete_all_to_right.macos", inputs);
622
623 // Text Caret Movement Left/Right
624
625 inputs = List<Ref<InputEvent>>();
626 inputs.push_back(InputEventKey::create_reference(Key::LEFT));
627 default_builtin_cache.insert("ui_text_caret_left", inputs);
628
629 inputs = List<Ref<InputEvent>>();
630 inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
631 default_builtin_cache.insert("ui_text_caret_word_left", inputs);
632
633 inputs = List<Ref<InputEvent>>();
634 inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::ALT));
635 default_builtin_cache.insert("ui_text_caret_word_left.macos", inputs);
636
637 inputs = List<Ref<InputEvent>>();
638 inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
639 default_builtin_cache.insert("ui_text_caret_right", inputs);
640
641 inputs = List<Ref<InputEvent>>();
642 inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
643 default_builtin_cache.insert("ui_text_caret_word_right", inputs);
644
645 inputs = List<Ref<InputEvent>>();
646 inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::ALT));
647 default_builtin_cache.insert("ui_text_caret_word_right.macos", inputs);
648
649 // Text Caret Movement Up/Down
650
651 inputs = List<Ref<InputEvent>>();
652 inputs.push_back(InputEventKey::create_reference(Key::UP));
653 default_builtin_cache.insert("ui_text_caret_up", inputs);
654
655 inputs = List<Ref<InputEvent>>();
656 inputs.push_back(InputEventKey::create_reference(Key::DOWN));
657 default_builtin_cache.insert("ui_text_caret_down", inputs);
658
659 // Text Caret Movement Line Start/End
660
661 inputs = List<Ref<InputEvent>>();
662 inputs.push_back(InputEventKey::create_reference(Key::HOME));
663 default_builtin_cache.insert("ui_text_caret_line_start", inputs);
664
665 inputs = List<Ref<InputEvent>>();
666 inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CTRL));
667 inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
668 inputs.push_back(InputEventKey::create_reference(Key::HOME));
669 default_builtin_cache.insert("ui_text_caret_line_start.macos", inputs);
670
671 inputs = List<Ref<InputEvent>>();
672 inputs.push_back(InputEventKey::create_reference(Key::END));
673 default_builtin_cache.insert("ui_text_caret_line_end", inputs);
674
675 inputs = List<Ref<InputEvent>>();
676 inputs.push_back(InputEventKey::create_reference(Key::E | KeyModifierMask::CTRL));
677 inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
678 inputs.push_back(InputEventKey::create_reference(Key::END));
679 default_builtin_cache.insert("ui_text_caret_line_end.macos", inputs);
680
681 // Text Caret Movement Page Up/Down
682
683 inputs = List<Ref<InputEvent>>();
684 inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
685 default_builtin_cache.insert("ui_text_caret_page_up", inputs);
686
687 inputs = List<Ref<InputEvent>>();
688 inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
689 default_builtin_cache.insert("ui_text_caret_page_down", inputs);
690
691 // Text Caret Movement Document Start/End
692
693 inputs = List<Ref<InputEvent>>();
694 inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD_OR_CTRL));
695 default_builtin_cache.insert("ui_text_caret_document_start", inputs);
696
697 inputs = List<Ref<InputEvent>>();
698 inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL));
699 inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD_OR_CTRL));
700 default_builtin_cache.insert("ui_text_caret_document_start.macos", inputs);
701
702 inputs = List<Ref<InputEvent>>();
703 inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD_OR_CTRL));
704 default_builtin_cache.insert("ui_text_caret_document_end", inputs);
705
706 inputs = List<Ref<InputEvent>>();
707 inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL));
708 inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD_OR_CTRL));
709 default_builtin_cache.insert("ui_text_caret_document_end.macos", inputs);
710
711 // Text Caret Addition Below/Above
712
713 inputs = List<Ref<InputEvent>>();
714 inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
715 default_builtin_cache.insert("ui_text_caret_add_below", inputs);
716
717 inputs = List<Ref<InputEvent>>();
718 inputs.push_back(InputEventKey::create_reference(Key::L | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
719 default_builtin_cache.insert("ui_text_caret_add_below.macos", inputs);
720
721 inputs = List<Ref<InputEvent>>();
722 inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
723 default_builtin_cache.insert("ui_text_caret_add_above", inputs);
724
725 inputs = List<Ref<InputEvent>>();
726 inputs.push_back(InputEventKey::create_reference(Key::O | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
727 default_builtin_cache.insert("ui_text_caret_add_above.macos", inputs);
728
729 // Text Scrolling
730
731 inputs = List<Ref<InputEvent>>();
732 inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL));
733 default_builtin_cache.insert("ui_text_scroll_up", inputs);
734
735 inputs = List<Ref<InputEvent>>();
736 inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
737 default_builtin_cache.insert("ui_text_scroll_up.macos", inputs);
738
739 inputs = List<Ref<InputEvent>>();
740 inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL));
741 default_builtin_cache.insert("ui_text_scroll_down", inputs);
742
743 inputs = List<Ref<InputEvent>>();
744 inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
745 default_builtin_cache.insert("ui_text_scroll_down.macos", inputs);
746
747 // Text Misc
748
749 inputs = List<Ref<InputEvent>>();
750 inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CMD_OR_CTRL));
751 default_builtin_cache.insert("ui_text_select_all", inputs);
752
753 inputs = List<Ref<InputEvent>>();
754 inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::ALT));
755 default_builtin_cache.insert("ui_text_select_word_under_caret", inputs);
756
757 inputs = List<Ref<InputEvent>>();
758 inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::CTRL | KeyModifierMask::META));
759 default_builtin_cache.insert("ui_text_select_word_under_caret.macos", inputs);
760
761 inputs = List<Ref<InputEvent>>();
762 inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL));
763 default_builtin_cache.insert("ui_text_add_selection_for_next_occurrence", inputs);
764
765 inputs = List<Ref<InputEvent>>();
766 inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
767 default_builtin_cache.insert("ui_text_skip_selection_for_next_occurrence", inputs);
768
769 inputs = List<Ref<InputEvent>>();
770 inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
771 default_builtin_cache.insert("ui_text_clear_carets_and_selection", inputs);
772
773 inputs = List<Ref<InputEvent>>();
774 inputs.push_back(InputEventKey::create_reference(Key::INSERT));
775 default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs);
776
777 inputs = List<Ref<InputEvent>>();
778 inputs.push_back(InputEventKey::create_reference(Key::MENU));
779 default_builtin_cache.insert("ui_menu", inputs);
780
781 inputs = List<Ref<InputEvent>>();
782 inputs.push_back(InputEventKey::create_reference(Key::ENTER));
783 inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
784 default_builtin_cache.insert("ui_text_submit", inputs);
785
786 inputs = List<Ref<InputEvent>>();
787 inputs.push_back(InputEventKey::create_reference(Key::U | KeyModifierMask::CTRL | KeyModifierMask::SHIFT));
788 default_builtin_cache.insert("ui_unicode_start", inputs);
789
790 // ///// UI Graph Shortcuts /////
791
792 inputs = List<Ref<InputEvent>>();
793 inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL));
794 default_builtin_cache.insert("ui_graph_duplicate", inputs);
795
796 inputs = List<Ref<InputEvent>>();
797 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
798 default_builtin_cache.insert("ui_graph_delete", inputs);
799
800 inputs = List<Ref<InputEvent>>();
801 inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
802 default_builtin_cache.insert("ui_graph_follow_left", inputs);
803
804 inputs = List<Ref<InputEvent>>();
805 inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::ALT));
806 default_builtin_cache.insert("ui_graph_follow_left.macos", inputs);
807
808 inputs = List<Ref<InputEvent>>();
809 inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
810 default_builtin_cache.insert("ui_graph_follow_right", inputs);
811
812 inputs = List<Ref<InputEvent>>();
813 inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::ALT));
814 default_builtin_cache.insert("ui_graph_follow_right.macos", inputs);
815
816 // ///// UI File Dialog Shortcuts /////
817 inputs = List<Ref<InputEvent>>();
818 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
819 default_builtin_cache.insert("ui_filedialog_delete", inputs);
820
821 inputs = List<Ref<InputEvent>>();
822 inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
823 default_builtin_cache.insert("ui_filedialog_up_one_level", inputs);
824
825 inputs = List<Ref<InputEvent>>();
826 inputs.push_back(InputEventKey::create_reference(Key::F5));
827 default_builtin_cache.insert("ui_filedialog_refresh", inputs);
828
829 inputs = List<Ref<InputEvent>>();
830 inputs.push_back(InputEventKey::create_reference(Key::H));
831 default_builtin_cache.insert("ui_filedialog_show_hidden", inputs);
832
833 inputs = List<Ref<InputEvent>>();
834 inputs.push_back(InputEventKey::create_reference(Key::F | KeyModifierMask::CMD_OR_CTRL));
835 default_builtin_cache.insert("ui_filedialog_find", inputs);
836
837 inputs = List<Ref<InputEvent>>();
838 // Ctrl + L (matches most Windows/Linux file managers' "focus on path bar" shortcut,
839 // plus macOS Safari's "focus on address bar" shortcut).
840 inputs.push_back(InputEventKey::create_reference(Key::L | KeyModifierMask::CMD_OR_CTRL));
841 default_builtin_cache.insert("ui_filedialog_focus_path", inputs);
842
843 inputs = List<Ref<InputEvent>>();
844 // Cmd + Shift + G (matches Finder's "Go To" shortcut).
845 inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::CMD_OR_CTRL));
846 inputs.push_back(InputEventKey::create_reference(Key::L | KeyModifierMask::CMD_OR_CTRL));
847 default_builtin_cache.insert("ui_filedialog_focus_path.macos", inputs);
848
849 inputs = List<Ref<InputEvent>>();
850 inputs.push_back(InputEventKey::create_reference(Key::QUOTELEFT | KeyModifierMask::CMD_OR_CTRL));
851 default_builtin_cache.insert("ui_swap_input_direction", inputs);
852
853 // ///// UI ColorPicker Shortcuts /////
854 inputs = List<Ref<InputEvent>>();
855 inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::X));
856 inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
857 default_builtin_cache.insert("ui_colorpicker_delete_preset", inputs);
858
859 return default_builtin_cache;
860 }
861
862 const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with_feature_overrides_applied() {
863 if (default_builtin_with_overrides_cache.size() > 0) {
864 return default_builtin_with_overrides_cache;
865 }
866
867 const HashMap<String, List<Ref<InputEvent>>> &builtins = get_builtins();
868
869 // Get a list of all built in inputs which are valid overrides for the OS
870 // Key = builtin name (e.g. ui_accept)
871 // Value = override/feature names (e.g. macos, if it was defined as "ui_accept.macos" and the platform supports that feature)
872 HashMap<String, Vector<String>> builtins_with_overrides;
873 for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
874 String fullname = E.key;
875
876 const String &name = fullname.get_slicec('.', 0);
877 String override_for = fullname.get_slice_count(".") > 1 ? fullname.get_slicec('.', 1) : String();
878
879 if (!override_for.is_empty() && OS::get_singleton()->has_feature(override_for)) {
880 builtins_with_overrides[name].push_back(override_for);
881 }
882 }
883
884 for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
885 String fullname = E.key;
886
887 const String &name = fullname.get_slicec('.', 0);
888 String override_for = fullname.get_slice_count(".") > 1 ? fullname.get_slicec('.', 1) : String();
889
890 if (builtins_with_overrides.has(name) && override_for.is_empty()) {
891 // Builtin has an override but this particular one is not an override, so skip.
892 continue;
893 }
894
895 if (!override_for.is_empty() && !OS::get_singleton()->has_feature(override_for)) {
896 // OS does not support this override - skip.
897 continue;
898 }
899
900 default_builtin_with_overrides_cache.insert(name, E.value);
901 }
902
903 return default_builtin_with_overrides_cache;
904 }
905
906 void InputMap::load_default() {
907 HashMap<String, List<Ref<InputEvent>>> builtins = get_builtins_with_feature_overrides_applied();
908
909 for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
910 String name = E.key;
911
912 add_action(name);
913
914 const List<Ref<InputEvent>> &inputs = E.value;
915 for (const List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
916 Ref<InputEventKey> iek = I->get();
917
918 // For the editor, only add keyboard actions.
919 if (iek.is_valid()) {
920 action_add_event(name, I->get());
921 }
922 }
923 }
924 }
925
926 InputMap::InputMap() {
927 ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exists.");
928 singleton = this;
929 }
930
931 InputMap::~InputMap() {
932 singleton = nullptr;
933 }