input_map.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*************************************************************************/
  2. /* input_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "input_map.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. InputMap *InputMap::singleton = nullptr;
  36. int InputMap::ALL_DEVICES = -1;
  37. void InputMap::_bind_methods() {
  38. ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
  39. ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::_get_actions);
  40. ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(0.5f));
  41. ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action);
  42. ClassDB::bind_method(D_METHOD("action_set_deadzone", "action", "deadzone"), &InputMap::action_set_deadzone);
  43. ClassDB::bind_method(D_METHOD("action_get_deadzone", "action"), &InputMap::action_get_deadzone);
  44. ClassDB::bind_method(D_METHOD("action_add_event", "action", "event"), &InputMap::action_add_event);
  45. ClassDB::bind_method(D_METHOD("action_has_event", "action", "event"), &InputMap::action_has_event);
  46. ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
  47. ClassDB::bind_method(D_METHOD("action_erase_events", "action"), &InputMap::action_erase_events);
  48. ClassDB::bind_method(D_METHOD("action_get_events", "action"), &InputMap::_action_get_events);
  49. ClassDB::bind_method(D_METHOD("event_is_action", "event", "action", "exact_match"), &InputMap::event_is_action, DEFVAL(false));
  50. ClassDB::bind_method(D_METHOD("load_from_project_settings"), &InputMap::load_from_project_settings);
  51. }
  52. /**
  53. * Returns an nonexistent action error message with a suggestion of the closest
  54. * matching action name (if possible).
  55. */
  56. String InputMap::suggest_actions(const StringName &p_action) const {
  57. List<StringName> actions = get_actions();
  58. StringName closest_action;
  59. float closest_similarity = 0.0;
  60. // Find the most action with the most similar name.
  61. for (const StringName &action : actions) {
  62. const float similarity = String(action).similarity(p_action);
  63. if (similarity > closest_similarity) {
  64. closest_action = action;
  65. closest_similarity = similarity;
  66. }
  67. }
  68. String error_message = vformat("The InputMap action \"%s\" doesn't exist.", p_action);
  69. if (closest_similarity >= 0.4) {
  70. // Only include a suggestion in the error message if it's similar enough.
  71. error_message += vformat(" Did you mean \"%s\"?", closest_action);
  72. }
  73. return error_message;
  74. }
  75. void InputMap::add_action(const StringName &p_action, float p_deadzone) {
  76. ERR_FAIL_COND_MSG(input_map.has(p_action), "InputMap already has action \"" + String(p_action) + "\".");
  77. input_map[p_action] = Action();
  78. static int last_id = 1;
  79. input_map[p_action].id = last_id;
  80. input_map[p_action].deadzone = p_deadzone;
  81. last_id++;
  82. }
  83. void InputMap::erase_action(const StringName &p_action) {
  84. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  85. input_map.erase(p_action);
  86. }
  87. Array InputMap::_get_actions() {
  88. Array ret;
  89. List<StringName> actions = get_actions();
  90. if (actions.is_empty()) {
  91. return ret;
  92. }
  93. for (const StringName &E : actions) {
  94. ret.push_back(E);
  95. }
  96. return ret;
  97. }
  98. List<StringName> InputMap::get_actions() const {
  99. List<StringName> actions = List<StringName>();
  100. if (input_map.is_empty()) {
  101. return actions;
  102. }
  103. for (OrderedHashMap<StringName, Action>::Element E = input_map.front(); E; E = E.next()) {
  104. actions.push_back(E.key());
  105. }
  106. return actions;
  107. }
  108. 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) const {
  109. ERR_FAIL_COND_V(!p_event.is_valid(), nullptr);
  110. for (List<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
  111. int device = E->get()->get_device();
  112. if (device == ALL_DEVICES || device == p_event->get_device()) {
  113. if (E->get()->action_match(p_event, p_exact_match, p_action.deadzone, r_pressed, r_strength, r_raw_strength)) {
  114. return E;
  115. }
  116. }
  117. }
  118. return nullptr;
  119. }
  120. bool InputMap::has_action(const StringName &p_action) const {
  121. return input_map.has(p_action);
  122. }
  123. float InputMap::action_get_deadzone(const StringName &p_action) {
  124. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, suggest_actions(p_action));
  125. return input_map[p_action].deadzone;
  126. }
  127. void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
  128. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  129. input_map[p_action].deadzone = p_deadzone;
  130. }
  131. void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  132. ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object.");
  133. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  134. if (_find_event(input_map[p_action], p_event, true)) {
  135. return; // Already added.
  136. }
  137. input_map[p_action].inputs.push_back(p_event);
  138. }
  139. bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  140. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, suggest_actions(p_action));
  141. return (_find_event(input_map[p_action], p_event, true) != nullptr);
  142. }
  143. void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  144. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  145. List<Ref<InputEvent>>::Element *E = _find_event(input_map[p_action], p_event, true);
  146. if (E) {
  147. input_map[p_action].inputs.erase(E);
  148. if (Input::get_singleton()->is_action_pressed(p_action)) {
  149. Input::get_singleton()->action_release(p_action);
  150. }
  151. }
  152. }
  153. void InputMap::action_erase_events(const StringName &p_action) {
  154. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  155. input_map[p_action].inputs.clear();
  156. }
  157. Array InputMap::_action_get_events(const StringName &p_action) {
  158. Array ret;
  159. const List<Ref<InputEvent>> *al = action_get_events(p_action);
  160. if (al) {
  161. for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) {
  162. ret.push_back(E->get());
  163. }
  164. }
  165. return ret;
  166. }
  167. const List<Ref<InputEvent>> *InputMap::action_get_events(const StringName &p_action) {
  168. const OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
  169. if (!E) {
  170. return nullptr;
  171. }
  172. return &E.get().inputs;
  173. }
  174. bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
  175. return event_get_action_status(p_event, p_action, p_exact_match);
  176. }
  177. 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) const {
  178. OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
  179. ERR_FAIL_COND_V_MSG(!E, false, suggest_actions(p_action));
  180. Ref<InputEventAction> input_event_action = p_event;
  181. if (input_event_action.is_valid()) {
  182. const bool pressed = input_event_action->is_pressed();
  183. if (r_pressed != nullptr) {
  184. *r_pressed = pressed;
  185. }
  186. const float strength = pressed ? input_event_action->get_strength() : 0.0f;
  187. if (r_strength != nullptr) {
  188. *r_strength = strength;
  189. }
  190. if (r_raw_strength != nullptr) {
  191. *r_raw_strength = strength;
  192. }
  193. return input_event_action->get_action() == p_action;
  194. }
  195. List<Ref<InputEvent>>::Element *event = _find_event(E.get(), p_event, p_exact_match, r_pressed, r_strength, r_raw_strength);
  196. return event != nullptr;
  197. }
  198. const OrderedHashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
  199. return input_map;
  200. }
  201. void InputMap::load_from_project_settings() {
  202. input_map.clear();
  203. List<PropertyInfo> pinfo;
  204. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  205. for (const PropertyInfo &pi : pinfo) {
  206. if (!pi.name.begins_with("input/")) {
  207. continue;
  208. }
  209. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  210. Dictionary action = ProjectSettings::get_singleton()->get(pi.name);
  211. float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f;
  212. Array events = action["events"];
  213. add_action(name, deadzone);
  214. for (int i = 0; i < events.size(); i++) {
  215. Ref<InputEvent> event = events[i];
  216. if (event.is_null()) {
  217. continue;
  218. }
  219. action_add_event(name, event);
  220. }
  221. }
  222. }
  223. struct _BuiltinActionDisplayName {
  224. const char *name;
  225. const char *display_name;
  226. };
  227. static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
  228. /* clang-format off */
  229. { "ui_accept", TTRC("Accept") },
  230. { "ui_select", TTRC("Select") },
  231. { "ui_cancel", TTRC("Cancel") },
  232. { "ui_focus_next", TTRC("Focus Next") },
  233. { "ui_focus_prev", TTRC("Focus Prev") },
  234. { "ui_left", TTRC("Left") },
  235. { "ui_right", TTRC("Right") },
  236. { "ui_up", TTRC("Up") },
  237. { "ui_down", TTRC("Down") },
  238. { "ui_page_up", TTRC("Page Up") },
  239. { "ui_page_down", TTRC("Page Down") },
  240. { "ui_home", TTRC("Home") },
  241. { "ui_end", TTRC("End") },
  242. { "ui_cut", TTRC("Cut") },
  243. { "ui_copy", TTRC("Copy") },
  244. { "ui_paste", TTRC("Paste") },
  245. { "ui_undo", TTRC("Undo") },
  246. { "ui_redo", TTRC("Redo") },
  247. { "ui_text_completion_query", TTRC("Completion Query") },
  248. { "ui_text_newline", TTRC("New Line") },
  249. { "ui_text_newline_blank", TTRC("New Blank Line") },
  250. { "ui_text_newline_above", TTRC("New Line Above") },
  251. { "ui_text_indent", TTRC("Indent") },
  252. { "ui_text_dedent", TTRC("Dedent") },
  253. { "ui_text_backspace", TTRC("Backspace") },
  254. { "ui_text_backspace_word", TTRC("Backspace Word") },
  255. { "ui_text_backspace_word.macos", TTRC("Backspace Word") },
  256. { "ui_text_backspace_all_to_left", TTRC("Backspace all to Left") },
  257. { "ui_text_backspace_all_to_left.macos", TTRC("Backspace all to Left") },
  258. { "ui_text_delete", TTRC("Delete") },
  259. { "ui_text_delete_word", TTRC("Delete Word") },
  260. { "ui_text_delete_word.macos", TTRC("Delete Word") },
  261. { "ui_text_delete_all_to_right", TTRC("Delete all to Right") },
  262. { "ui_text_delete_all_to_right.macos", TTRC("Delete all to Right") },
  263. { "ui_text_caret_left", TTRC("Caret Left") },
  264. { "ui_text_caret_word_left", TTRC("Caret Word Left") },
  265. { "ui_text_caret_word_left.macos", TTRC("Caret Word Left") },
  266. { "ui_text_caret_right", TTRC("Caret Right") },
  267. { "ui_text_caret_word_right", TTRC("Caret Word Right") },
  268. { "ui_text_caret_word_right.macos", TTRC("Caret Word Right") },
  269. { "ui_text_caret_up", TTRC("Caret Up") },
  270. { "ui_text_caret_down", TTRC("Caret Down") },
  271. { "ui_text_caret_line_start", TTRC("Caret Line Start") },
  272. { "ui_text_caret_line_start.macos", TTRC("Caret Line Start") },
  273. { "ui_text_caret_line_end", TTRC("Caret Line End") },
  274. { "ui_text_caret_line_end.macos", TTRC("Caret Line End") },
  275. { "ui_text_caret_page_up", TTRC("Caret Page Up") },
  276. { "ui_text_caret_page_down", TTRC("Caret Page Down") },
  277. { "ui_text_caret_document_start", TTRC("Caret Document Start") },
  278. { "ui_text_caret_document_start.macos", TTRC("Caret Document Start") },
  279. { "ui_text_caret_document_end", TTRC("Caret Document End") },
  280. { "ui_text_caret_document_end.macos", TTRC("Caret Document End") },
  281. { "ui_text_scroll_up", TTRC("Scroll Up") },
  282. { "ui_text_scroll_up.macos", TTRC("Scroll Up") },
  283. { "ui_text_scroll_down", TTRC("Scroll Down") },
  284. { "ui_text_scroll_down.macos", TTRC("Scroll Down") },
  285. { "ui_text_select_all", TTRC("Select All") },
  286. { "ui_text_select_word_under_caret", TTRC("Select Word Under Caret") },
  287. { "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") },
  288. { "ui_text_submit", TTRC("Text Submitted") },
  289. { "ui_graph_duplicate", TTRC("Duplicate Nodes") },
  290. { "ui_graph_delete", TTRC("Delete Nodes") },
  291. { "ui_filedialog_up_one_level", TTRC("Go Up One Level") },
  292. { "ui_filedialog_refresh", TTRC("Refresh") },
  293. { "ui_filedialog_show_hidden", TTRC("Show Hidden") },
  294. { "ui_swap_input_direction ", TTRC("Swap Input Direction") },
  295. { "", TTRC("")}
  296. /* clang-format on */
  297. };
  298. String InputMap::get_builtin_display_name(const String &p_name) const {
  299. int len = sizeof(_builtin_action_display_names) / sizeof(_BuiltinActionDisplayName);
  300. for (int i = 0; i < len; i++) {
  301. if (_builtin_action_display_names[i].name == p_name) {
  302. return RTR(_builtin_action_display_names[i].display_name);
  303. }
  304. }
  305. return p_name;
  306. }
  307. const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
  308. // Return cache if it has already been built.
  309. if (default_builtin_cache.size()) {
  310. return default_builtin_cache;
  311. }
  312. List<Ref<InputEvent>> inputs;
  313. inputs.push_back(InputEventKey::create_reference(Key::ENTER));
  314. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
  315. inputs.push_back(InputEventKey::create_reference(Key::SPACE));
  316. default_builtin_cache.insert("ui_accept", inputs);
  317. inputs = List<Ref<InputEvent>>();
  318. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::Y));
  319. inputs.push_back(InputEventKey::create_reference(Key::SPACE));
  320. default_builtin_cache.insert("ui_select", inputs);
  321. inputs = List<Ref<InputEvent>>();
  322. inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
  323. default_builtin_cache.insert("ui_cancel", inputs);
  324. inputs = List<Ref<InputEvent>>();
  325. inputs.push_back(InputEventKey::create_reference(Key::TAB));
  326. default_builtin_cache.insert("ui_focus_next", inputs);
  327. inputs = List<Ref<InputEvent>>();
  328. inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
  329. default_builtin_cache.insert("ui_focus_prev", inputs);
  330. inputs = List<Ref<InputEvent>>();
  331. inputs.push_back(InputEventKey::create_reference(Key::LEFT));
  332. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_LEFT));
  333. default_builtin_cache.insert("ui_left", inputs);
  334. inputs = List<Ref<InputEvent>>();
  335. inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
  336. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_RIGHT));
  337. default_builtin_cache.insert("ui_right", inputs);
  338. inputs = List<Ref<InputEvent>>();
  339. inputs.push_back(InputEventKey::create_reference(Key::UP));
  340. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_UP));
  341. default_builtin_cache.insert("ui_up", inputs);
  342. inputs = List<Ref<InputEvent>>();
  343. inputs.push_back(InputEventKey::create_reference(Key::DOWN));
  344. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_DOWN));
  345. default_builtin_cache.insert("ui_down", inputs);
  346. inputs = List<Ref<InputEvent>>();
  347. inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
  348. default_builtin_cache.insert("ui_page_up", inputs);
  349. inputs = List<Ref<InputEvent>>();
  350. inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
  351. default_builtin_cache.insert("ui_page_down", inputs);
  352. inputs = List<Ref<InputEvent>>();
  353. inputs.push_back(InputEventKey::create_reference(Key::HOME));
  354. default_builtin_cache.insert("ui_home", inputs);
  355. inputs = List<Ref<InputEvent>>();
  356. inputs.push_back(InputEventKey::create_reference(Key::END));
  357. default_builtin_cache.insert("ui_end", inputs);
  358. // ///// UI basic Shortcuts /////
  359. inputs = List<Ref<InputEvent>>();
  360. inputs.push_back(InputEventKey::create_reference(Key::X | KeyModifierMask::CMD));
  361. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::SHIFT));
  362. default_builtin_cache.insert("ui_cut", inputs);
  363. inputs = List<Ref<InputEvent>>();
  364. inputs.push_back(InputEventKey::create_reference(Key::C | KeyModifierMask::CMD));
  365. inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::CMD));
  366. default_builtin_cache.insert("ui_copy", inputs);
  367. inputs = List<Ref<InputEvent>>();
  368. inputs.push_back(InputEventKey::create_reference(Key::V | KeyModifierMask::CMD));
  369. inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::SHIFT));
  370. default_builtin_cache.insert("ui_paste", inputs);
  371. inputs = List<Ref<InputEvent>>();
  372. inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD));
  373. default_builtin_cache.insert("ui_undo", inputs);
  374. inputs = List<Ref<InputEvent>>();
  375. inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD | KeyModifierMask::SHIFT));
  376. inputs.push_back(InputEventKey::create_reference(Key::Y | KeyModifierMask::CMD));
  377. default_builtin_cache.insert("ui_redo", inputs);
  378. // ///// UI Text Input Shortcuts /////
  379. inputs = List<Ref<InputEvent>>();
  380. inputs.push_back(InputEventKey::create_reference(Key::SPACE | KeyModifierMask::CTRL));
  381. default_builtin_cache.insert("ui_text_completion_query", inputs);
  382. inputs = List<Ref<InputEvent>>();
  383. inputs.push_back(InputEventKey::create_reference(Key::ENTER));
  384. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
  385. default_builtin_cache.insert("ui_text_completion_accept", inputs);
  386. inputs = List<Ref<InputEvent>>();
  387. inputs.push_back(InputEventKey::create_reference(Key::TAB));
  388. default_builtin_cache.insert("ui_text_completion_replace", inputs);
  389. // Newlines
  390. inputs = List<Ref<InputEvent>>();
  391. inputs.push_back(InputEventKey::create_reference(Key::ENTER));
  392. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
  393. default_builtin_cache.insert("ui_text_newline", inputs);
  394. inputs = List<Ref<InputEvent>>();
  395. inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::CMD));
  396. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::CMD));
  397. default_builtin_cache.insert("ui_text_newline_blank", inputs);
  398. inputs = List<Ref<InputEvent>>();
  399. inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD));
  400. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD));
  401. default_builtin_cache.insert("ui_text_newline_above", inputs);
  402. // Indentation
  403. inputs = List<Ref<InputEvent>>();
  404. inputs.push_back(InputEventKey::create_reference(Key::TAB));
  405. default_builtin_cache.insert("ui_text_indent", inputs);
  406. inputs = List<Ref<InputEvent>>();
  407. inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
  408. default_builtin_cache.insert("ui_text_dedent", inputs);
  409. // Text Backspace and Delete
  410. inputs = List<Ref<InputEvent>>();
  411. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
  412. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::SHIFT));
  413. default_builtin_cache.insert("ui_text_backspace", inputs);
  414. inputs = List<Ref<InputEvent>>();
  415. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD));
  416. default_builtin_cache.insert("ui_text_backspace_word", inputs);
  417. inputs = List<Ref<InputEvent>>();
  418. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::ALT));
  419. default_builtin_cache.insert("ui_text_backspace_word.macos", inputs);
  420. inputs = List<Ref<InputEvent>>();
  421. default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs);
  422. inputs = List<Ref<InputEvent>>();
  423. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD));
  424. default_builtin_cache.insert("ui_text_backspace_all_to_left.macos", inputs);
  425. inputs = List<Ref<InputEvent>>();
  426. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
  427. default_builtin_cache.insert("ui_text_delete", inputs);
  428. inputs = List<Ref<InputEvent>>();
  429. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD));
  430. default_builtin_cache.insert("ui_text_delete_word", inputs);
  431. inputs = List<Ref<InputEvent>>();
  432. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::ALT));
  433. default_builtin_cache.insert("ui_text_delete_word.macos", inputs);
  434. inputs = List<Ref<InputEvent>>();
  435. default_builtin_cache.insert("ui_text_delete_all_to_right", inputs);
  436. inputs = List<Ref<InputEvent>>();
  437. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD));
  438. default_builtin_cache.insert("ui_text_delete_all_to_right.macos", inputs);
  439. // Text Caret Movement Left/Right
  440. inputs = List<Ref<InputEvent>>();
  441. inputs.push_back(InputEventKey::create_reference(Key::LEFT));
  442. default_builtin_cache.insert("ui_text_caret_left", inputs);
  443. inputs = List<Ref<InputEvent>>();
  444. inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD));
  445. default_builtin_cache.insert("ui_text_caret_word_left", inputs);
  446. inputs = List<Ref<InputEvent>>();
  447. inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::ALT));
  448. default_builtin_cache.insert("ui_text_caret_word_left.macos", inputs);
  449. inputs = List<Ref<InputEvent>>();
  450. inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
  451. default_builtin_cache.insert("ui_text_caret_right", inputs);
  452. inputs = List<Ref<InputEvent>>();
  453. inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD));
  454. default_builtin_cache.insert("ui_text_caret_word_right", inputs);
  455. inputs = List<Ref<InputEvent>>();
  456. inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::ALT));
  457. default_builtin_cache.insert("ui_text_caret_word_right.macos", inputs);
  458. // Text Caret Movement Up/Down
  459. inputs = List<Ref<InputEvent>>();
  460. inputs.push_back(InputEventKey::create_reference(Key::UP));
  461. default_builtin_cache.insert("ui_text_caret_up", inputs);
  462. inputs = List<Ref<InputEvent>>();
  463. inputs.push_back(InputEventKey::create_reference(Key::DOWN));
  464. default_builtin_cache.insert("ui_text_caret_down", inputs);
  465. // Text Caret Movement Line Start/End
  466. inputs = List<Ref<InputEvent>>();
  467. inputs.push_back(InputEventKey::create_reference(Key::HOME));
  468. default_builtin_cache.insert("ui_text_caret_line_start", inputs);
  469. inputs = List<Ref<InputEvent>>();
  470. inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CTRL));
  471. inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD));
  472. default_builtin_cache.insert("ui_text_caret_line_start.macos", inputs);
  473. inputs = List<Ref<InputEvent>>();
  474. inputs.push_back(InputEventKey::create_reference(Key::END));
  475. default_builtin_cache.insert("ui_text_caret_line_end", inputs);
  476. inputs = List<Ref<InputEvent>>();
  477. inputs.push_back(InputEventKey::create_reference(Key::E | KeyModifierMask::CTRL));
  478. inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD));
  479. default_builtin_cache.insert("ui_text_caret_line_end.macos", inputs);
  480. // Text Caret Movement Page Up/Down
  481. inputs = List<Ref<InputEvent>>();
  482. inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
  483. default_builtin_cache.insert("ui_text_caret_page_up", inputs);
  484. inputs = List<Ref<InputEvent>>();
  485. inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
  486. default_builtin_cache.insert("ui_text_caret_page_down", inputs);
  487. // Text Caret Movement Document Start/End
  488. inputs = List<Ref<InputEvent>>();
  489. inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD));
  490. default_builtin_cache.insert("ui_text_caret_document_start", inputs);
  491. inputs = List<Ref<InputEvent>>();
  492. inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD));
  493. default_builtin_cache.insert("ui_text_caret_document_start.macos", inputs);
  494. inputs = List<Ref<InputEvent>>();
  495. inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD));
  496. default_builtin_cache.insert("ui_text_caret_document_end", inputs);
  497. inputs = List<Ref<InputEvent>>();
  498. inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD));
  499. default_builtin_cache.insert("ui_text_caret_document_end.macos", inputs);
  500. // Text Scrolling
  501. inputs = List<Ref<InputEvent>>();
  502. inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD));
  503. default_builtin_cache.insert("ui_text_scroll_up", inputs);
  504. inputs = List<Ref<InputEvent>>();
  505. inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD | KeyModifierMask::ALT));
  506. default_builtin_cache.insert("ui_text_scroll_up.macos", inputs);
  507. inputs = List<Ref<InputEvent>>();
  508. inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD));
  509. default_builtin_cache.insert("ui_text_scroll_down", inputs);
  510. inputs = List<Ref<InputEvent>>();
  511. inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD | KeyModifierMask::ALT));
  512. default_builtin_cache.insert("ui_text_scroll_down.macos", inputs);
  513. // Text Misc
  514. inputs = List<Ref<InputEvent>>();
  515. inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CMD));
  516. default_builtin_cache.insert("ui_text_select_all", inputs);
  517. inputs = List<Ref<InputEvent>>();
  518. inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD));
  519. default_builtin_cache.insert("ui_text_select_word_under_caret", inputs);
  520. inputs = List<Ref<InputEvent>>();
  521. inputs.push_back(InputEventKey::create_reference(Key::INSERT));
  522. default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs);
  523. inputs = List<Ref<InputEvent>>();
  524. inputs.push_back(InputEventKey::create_reference(Key::MENU));
  525. default_builtin_cache.insert("ui_menu", inputs);
  526. inputs = List<Ref<InputEvent>>();
  527. inputs.push_back(InputEventKey::create_reference(Key::ENTER));
  528. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
  529. default_builtin_cache.insert("ui_text_submit", inputs);
  530. // ///// UI Graph Shortcuts /////
  531. inputs = List<Ref<InputEvent>>();
  532. inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD));
  533. default_builtin_cache.insert("ui_graph_duplicate", inputs);
  534. inputs = List<Ref<InputEvent>>();
  535. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
  536. default_builtin_cache.insert("ui_graph_delete", inputs);
  537. // ///// UI File Dialog Shortcuts /////
  538. inputs = List<Ref<InputEvent>>();
  539. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
  540. default_builtin_cache.insert("ui_filedialog_up_one_level", inputs);
  541. inputs = List<Ref<InputEvent>>();
  542. inputs.push_back(InputEventKey::create_reference(Key::F5));
  543. default_builtin_cache.insert("ui_filedialog_refresh", inputs);
  544. inputs = List<Ref<InputEvent>>();
  545. inputs.push_back(InputEventKey::create_reference(Key::H));
  546. default_builtin_cache.insert("ui_filedialog_show_hidden", inputs);
  547. inputs = List<Ref<InputEvent>>();
  548. inputs.push_back(InputEventKey::create_reference(Key::QUOTELEFT | KeyModifierMask::CMD));
  549. default_builtin_cache.insert("ui_swap_input_direction", inputs);
  550. return default_builtin_cache;
  551. }
  552. const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with_feature_overrides_applied() {
  553. if (default_builtin_with_overrides_cache.size() > 0) {
  554. return default_builtin_with_overrides_cache;
  555. }
  556. OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins();
  557. // Get a list of all built in inputs which are valid overrides for the OS
  558. // Key = builtin name (e.g. ui_accept)
  559. // Value = override/feature names (e.g. macos, if it was defined as "ui_accept.macos" and the platform supports that feature)
  560. Map<String, Vector<String>> builtins_with_overrides;
  561. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  562. String fullname = E.key();
  563. Vector<String> split = fullname.split(".");
  564. String name = split[0];
  565. String override_for = split.size() > 1 ? split[1] : String();
  566. if (!override_for.is_empty() && OS::get_singleton()->has_feature(override_for)) {
  567. builtins_with_overrides[name].push_back(override_for);
  568. }
  569. }
  570. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  571. String fullname = E.key();
  572. Vector<String> split = fullname.split(".");
  573. String name = split[0];
  574. String override_for = split.size() > 1 ? split[1] : String();
  575. if (builtins_with_overrides.has(name) && override_for.is_empty()) {
  576. // Builtin has an override but this particular one is not an override, so skip.
  577. continue;
  578. }
  579. if (!override_for.is_empty() && !OS::get_singleton()->has_feature(override_for)) {
  580. // OS does not support this override - skip.
  581. continue;
  582. }
  583. default_builtin_with_overrides_cache.insert(name, E.value());
  584. }
  585. return default_builtin_with_overrides_cache;
  586. }
  587. void InputMap::load_default() {
  588. OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins_with_feature_overrides_applied();
  589. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  590. String name = E.key();
  591. add_action(name);
  592. List<Ref<InputEvent>> inputs = E.get();
  593. for (List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
  594. Ref<InputEventKey> iek = I->get();
  595. // For the editor, only add keyboard actions.
  596. if (iek.is_valid()) {
  597. action_add_event(name, I->get());
  598. }
  599. }
  600. }
  601. }
  602. InputMap::InputMap() {
  603. ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist.");
  604. singleton = this;
  605. }
  606. InputMap::~InputMap() {
  607. singleton = nullptr;
  608. }