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