2
0

input_map.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*************************************************************************/
  2. /* input_map.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 *p_pressed, float *p_strength, float *p_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 (p_exact_match && E->get()->is_match(p_event, true)) {
  114. return E;
  115. } else if (!p_exact_match && E->get()->action_match(p_event, p_pressed, p_strength, p_raw_strength, p_action.deadzone)) {
  116. return E;
  117. }
  118. }
  119. }
  120. return nullptr;
  121. }
  122. bool InputMap::has_action(const StringName &p_action) const {
  123. return input_map.has(p_action);
  124. }
  125. float InputMap::action_get_deadzone(const StringName &p_action) {
  126. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, suggest_actions(p_action));
  127. return input_map[p_action].deadzone;
  128. }
  129. void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
  130. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  131. input_map[p_action].deadzone = p_deadzone;
  132. }
  133. void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  134. ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object.");
  135. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  136. if (_find_event(input_map[p_action], p_event, true)) {
  137. return; // Already added.
  138. }
  139. input_map[p_action].inputs.push_back(p_event);
  140. }
  141. bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  142. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, suggest_actions(p_action));
  143. return (_find_event(input_map[p_action], p_event, true) != nullptr);
  144. }
  145. void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  146. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  147. List<Ref<InputEvent>>::Element *E = _find_event(input_map[p_action], p_event, true);
  148. if (E) {
  149. input_map[p_action].inputs.erase(E);
  150. if (Input::get_singleton()->is_action_pressed(p_action)) {
  151. Input::get_singleton()->action_release(p_action);
  152. }
  153. }
  154. }
  155. void InputMap::action_erase_events(const StringName &p_action) {
  156. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  157. input_map[p_action].inputs.clear();
  158. }
  159. Array InputMap::_action_get_events(const StringName &p_action) {
  160. Array ret;
  161. const List<Ref<InputEvent>> *al = action_get_events(p_action);
  162. if (al) {
  163. for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) {
  164. ret.push_back(E->get());
  165. }
  166. }
  167. return ret;
  168. }
  169. const List<Ref<InputEvent>> *InputMap::action_get_events(const StringName &p_action) {
  170. const OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
  171. if (!E) {
  172. return nullptr;
  173. }
  174. return &E.get().inputs;
  175. }
  176. bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
  177. return event_get_action_status(p_event, p_action, p_exact_match);
  178. }
  179. bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match, bool *p_pressed, float *p_strength, float *p_raw_strength) const {
  180. OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
  181. ERR_FAIL_COND_V_MSG(!E, false, suggest_actions(p_action));
  182. Ref<InputEventAction> input_event_action = p_event;
  183. if (input_event_action.is_valid()) {
  184. bool pressed = input_event_action->is_pressed();
  185. if (p_pressed != nullptr) {
  186. *p_pressed = pressed;
  187. }
  188. if (p_strength != nullptr) {
  189. *p_strength = pressed ? input_event_action->get_strength() : 0.0f;
  190. }
  191. return input_event_action->get_action() == p_action;
  192. }
  193. bool pressed;
  194. float strength;
  195. float raw_strength;
  196. List<Ref<InputEvent>>::Element *event = _find_event(E.get(), p_event, p_exact_match, &pressed, &strength, &raw_strength);
  197. if (event != nullptr) {
  198. if (p_pressed != nullptr) {
  199. *p_pressed = pressed;
  200. }
  201. if (p_strength != nullptr) {
  202. *p_strength = strength;
  203. }
  204. if (p_raw_strength != nullptr) {
  205. *p_raw_strength = raw_strength;
  206. }
  207. return true;
  208. } else {
  209. return false;
  210. }
  211. }
  212. const OrderedHashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
  213. return input_map;
  214. }
  215. void InputMap::load_from_project_settings() {
  216. input_map.clear();
  217. List<PropertyInfo> pinfo;
  218. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  219. for (const PropertyInfo &pi : pinfo) {
  220. if (!pi.name.begins_with("input/")) {
  221. continue;
  222. }
  223. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  224. Dictionary action = ProjectSettings::get_singleton()->get(pi.name);
  225. float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f;
  226. Array events = action["events"];
  227. add_action(name, deadzone);
  228. for (int i = 0; i < events.size(); i++) {
  229. Ref<InputEvent> event = events[i];
  230. if (event.is_null()) {
  231. continue;
  232. }
  233. action_add_event(name, event);
  234. }
  235. }
  236. }
  237. struct _BuiltinActionDisplayName {
  238. const char *name;
  239. const char *display_name;
  240. };
  241. static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
  242. /* clang-format off */
  243. { "ui_accept", TTRC("Accept") },
  244. { "ui_select", TTRC("Select") },
  245. { "ui_cancel", TTRC("Cancel") },
  246. { "ui_focus_next", TTRC("Focus Next") },
  247. { "ui_focus_prev", TTRC("Focus Prev") },
  248. { "ui_left", TTRC("Left") },
  249. { "ui_right", TTRC("Right") },
  250. { "ui_up", TTRC("Up") },
  251. { "ui_down", TTRC("Down") },
  252. { "ui_page_up", TTRC("Page Up") },
  253. { "ui_page_down", TTRC("Page Down") },
  254. { "ui_home", TTRC("Home") },
  255. { "ui_end", TTRC("End") },
  256. { "ui_cut", TTRC("Cut") },
  257. { "ui_copy", TTRC("Copy") },
  258. { "ui_paste", TTRC("Paste") },
  259. { "ui_undo", TTRC("Undo") },
  260. { "ui_redo", TTRC("Redo") },
  261. { "ui_text_completion_query", TTRC("Completion Query") },
  262. { "ui_text_newline", TTRC("New Line") },
  263. { "ui_text_newline_blank", TTRC("New Blank Line") },
  264. { "ui_text_newline_above", TTRC("New Line Above") },
  265. { "ui_text_indent", TTRC("Indent") },
  266. { "ui_text_dedent", TTRC("Dedent") },
  267. { "ui_text_backspace", TTRC("Backspace") },
  268. { "ui_text_backspace_word", TTRC("Backspace Word") },
  269. { "ui_text_backspace_word.macos", TTRC("Backspace Word") },
  270. { "ui_text_backspace_all_to_left", TTRC("Backspace all to Left") },
  271. { "ui_text_backspace_all_to_left.macos", TTRC("Backspace all to Left") },
  272. { "ui_text_delete", TTRC("Delete") },
  273. { "ui_text_delete_word", TTRC("Delete Word") },
  274. { "ui_text_delete_word.macos", TTRC("Delete Word") },
  275. { "ui_text_delete_all_to_right", TTRC("Delete all to Right") },
  276. { "ui_text_delete_all_to_right.macos", TTRC("Delete all to Right") },
  277. { "ui_text_caret_left", TTRC("Caret Left") },
  278. { "ui_text_caret_word_left", TTRC("Caret Word Left") },
  279. { "ui_text_caret_word_left.macos", TTRC("Caret Word Left") },
  280. { "ui_text_caret_right", TTRC("Caret Right") },
  281. { "ui_text_caret_word_right", TTRC("Caret Word Right") },
  282. { "ui_text_caret_word_right.macos", TTRC("Caret Word Right") },
  283. { "ui_text_caret_up", TTRC("Caret Up") },
  284. { "ui_text_caret_down", TTRC("Caret Down") },
  285. { "ui_text_caret_line_start", TTRC("Caret Line Start") },
  286. { "ui_text_caret_line_start.macos", TTRC("Caret Line Start") },
  287. { "ui_text_caret_line_end", TTRC("Caret Line End") },
  288. { "ui_text_caret_line_end.macos", TTRC("Caret Line End") },
  289. { "ui_text_caret_page_up", TTRC("Caret Page Up") },
  290. { "ui_text_caret_page_down", TTRC("Caret Page Down") },
  291. { "ui_text_caret_document_start", TTRC("Caret Document Start") },
  292. { "ui_text_caret_document_start.macos", TTRC("Caret Document Start") },
  293. { "ui_text_caret_document_end", TTRC("Caret Document End") },
  294. { "ui_text_caret_document_end.macos", TTRC("Caret Document End") },
  295. { "ui_text_scroll_up", TTRC("Scroll Up") },
  296. { "ui_text_scroll_up.macos", TTRC("Scroll Up") },
  297. { "ui_text_scroll_down", TTRC("Scroll Down") },
  298. { "ui_text_scroll_down.macos", TTRC("Scroll Down") },
  299. { "ui_text_select_all", TTRC("Select All") },
  300. { "ui_text_select_word_under_caret", TTRC("Select Word Under Caret") },
  301. { "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") },
  302. { "ui_text_submit", TTRC("Text Submitted") },
  303. { "ui_graph_duplicate", TTRC("Duplicate Nodes") },
  304. { "ui_graph_delete", TTRC("Delete Nodes") },
  305. { "ui_filedialog_up_one_level", TTRC("Go Up One Level") },
  306. { "ui_filedialog_refresh", TTRC("Refresh") },
  307. { "ui_filedialog_show_hidden", TTRC("Show Hidden") },
  308. { "ui_swap_input_direction ", TTRC("Swap Input Direction") },
  309. { "", TTRC("")}
  310. /* clang-format on */
  311. };
  312. String InputMap::get_builtin_display_name(const String &p_name) const {
  313. int len = sizeof(_builtin_action_display_names) / sizeof(_BuiltinActionDisplayName);
  314. for (int i = 0; i < len; i++) {
  315. if (_builtin_action_display_names[i].name == p_name) {
  316. return RTR(_builtin_action_display_names[i].display_name);
  317. }
  318. }
  319. return p_name;
  320. }
  321. const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
  322. // Return cache if it has already been built.
  323. if (default_builtin_cache.size()) {
  324. return default_builtin_cache;
  325. }
  326. List<Ref<InputEvent>> inputs;
  327. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  328. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  329. inputs.push_back(InputEventKey::create_reference(KEY_SPACE));
  330. default_builtin_cache.insert("ui_accept", inputs);
  331. inputs = List<Ref<InputEvent>>();
  332. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_Y));
  333. inputs.push_back(InputEventKey::create_reference(KEY_SPACE));
  334. default_builtin_cache.insert("ui_select", inputs);
  335. inputs = List<Ref<InputEvent>>();
  336. inputs.push_back(InputEventKey::create_reference(KEY_ESCAPE));
  337. default_builtin_cache.insert("ui_cancel", inputs);
  338. inputs = List<Ref<InputEvent>>();
  339. inputs.push_back(InputEventKey::create_reference(KEY_TAB));
  340. default_builtin_cache.insert("ui_focus_next", inputs);
  341. inputs = List<Ref<InputEvent>>();
  342. inputs.push_back(InputEventKey::create_reference(KEY_TAB | KEY_MASK_SHIFT));
  343. default_builtin_cache.insert("ui_focus_prev", inputs);
  344. inputs = List<Ref<InputEvent>>();
  345. inputs.push_back(InputEventKey::create_reference(KEY_LEFT));
  346. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_LEFT));
  347. default_builtin_cache.insert("ui_left", inputs);
  348. inputs = List<Ref<InputEvent>>();
  349. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT));
  350. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_RIGHT));
  351. default_builtin_cache.insert("ui_right", inputs);
  352. inputs = List<Ref<InputEvent>>();
  353. inputs.push_back(InputEventKey::create_reference(KEY_UP));
  354. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_UP));
  355. default_builtin_cache.insert("ui_up", inputs);
  356. inputs = List<Ref<InputEvent>>();
  357. inputs.push_back(InputEventKey::create_reference(KEY_DOWN));
  358. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_DOWN));
  359. default_builtin_cache.insert("ui_down", inputs);
  360. inputs = List<Ref<InputEvent>>();
  361. inputs.push_back(InputEventKey::create_reference(KEY_PAGEUP));
  362. default_builtin_cache.insert("ui_page_up", inputs);
  363. inputs = List<Ref<InputEvent>>();
  364. inputs.push_back(InputEventKey::create_reference(KEY_PAGEDOWN));
  365. default_builtin_cache.insert("ui_page_down", inputs);
  366. inputs = List<Ref<InputEvent>>();
  367. inputs.push_back(InputEventKey::create_reference(KEY_HOME));
  368. default_builtin_cache.insert("ui_home", inputs);
  369. inputs = List<Ref<InputEvent>>();
  370. inputs.push_back(InputEventKey::create_reference(KEY_END));
  371. default_builtin_cache.insert("ui_end", inputs);
  372. // ///// UI basic Shortcuts /////
  373. inputs = List<Ref<InputEvent>>();
  374. inputs.push_back(InputEventKey::create_reference(KEY_X | KEY_MASK_CMD));
  375. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_SHIFT));
  376. default_builtin_cache.insert("ui_cut", inputs);
  377. inputs = List<Ref<InputEvent>>();
  378. inputs.push_back(InputEventKey::create_reference(KEY_C | KEY_MASK_CMD));
  379. inputs.push_back(InputEventKey::create_reference(KEY_INSERT | KEY_MASK_CMD));
  380. default_builtin_cache.insert("ui_copy", inputs);
  381. inputs = List<Ref<InputEvent>>();
  382. inputs.push_back(InputEventKey::create_reference(KEY_V | KEY_MASK_CMD));
  383. inputs.push_back(InputEventKey::create_reference(KEY_INSERT | KEY_MASK_SHIFT));
  384. default_builtin_cache.insert("ui_paste", inputs);
  385. inputs = List<Ref<InputEvent>>();
  386. inputs.push_back(InputEventKey::create_reference(KEY_Z | KEY_MASK_CMD));
  387. default_builtin_cache.insert("ui_undo", inputs);
  388. inputs = List<Ref<InputEvent>>();
  389. inputs.push_back(InputEventKey::create_reference(KEY_Z | KEY_MASK_CMD | KEY_MASK_SHIFT));
  390. inputs.push_back(InputEventKey::create_reference(KEY_Y | KEY_MASK_CMD));
  391. default_builtin_cache.insert("ui_redo", inputs);
  392. // ///// UI Text Input Shortcuts /////
  393. inputs = List<Ref<InputEvent>>();
  394. inputs.push_back(InputEventKey::create_reference(KEY_SPACE | KEY_MASK_CTRL));
  395. default_builtin_cache.insert("ui_text_completion_query", inputs);
  396. inputs = List<Ref<InputEvent>>();
  397. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  398. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  399. default_builtin_cache.insert("ui_text_completion_accept", inputs);
  400. inputs = List<Ref<InputEvent>>();
  401. inputs.push_back(InputEventKey::create_reference(KEY_TAB));
  402. default_builtin_cache.insert("ui_text_completion_replace", inputs);
  403. // Newlines
  404. inputs = List<Ref<InputEvent>>();
  405. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  406. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  407. default_builtin_cache.insert("ui_text_newline", inputs);
  408. inputs = List<Ref<InputEvent>>();
  409. inputs.push_back(InputEventKey::create_reference(KEY_ENTER | KEY_MASK_CMD));
  410. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER | KEY_MASK_CMD));
  411. default_builtin_cache.insert("ui_text_newline_blank", inputs);
  412. inputs = List<Ref<InputEvent>>();
  413. inputs.push_back(InputEventKey::create_reference(KEY_ENTER | KEY_MASK_SHIFT | KEY_MASK_CMD));
  414. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER | KEY_MASK_SHIFT | KEY_MASK_CMD));
  415. default_builtin_cache.insert("ui_text_newline_above", inputs);
  416. // Indentation
  417. inputs = List<Ref<InputEvent>>();
  418. inputs.push_back(InputEventKey::create_reference(KEY_TAB));
  419. default_builtin_cache.insert("ui_text_indent", inputs);
  420. inputs = List<Ref<InputEvent>>();
  421. inputs.push_back(InputEventKey::create_reference(KEY_TAB | KEY_MASK_SHIFT));
  422. default_builtin_cache.insert("ui_text_dedent", inputs);
  423. // Text Backspace and Delete
  424. inputs = List<Ref<InputEvent>>();
  425. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE));
  426. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_SHIFT));
  427. default_builtin_cache.insert("ui_text_backspace", inputs);
  428. inputs = List<Ref<InputEvent>>();
  429. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_CMD));
  430. default_builtin_cache.insert("ui_text_backspace_word", inputs);
  431. inputs = List<Ref<InputEvent>>();
  432. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_ALT));
  433. default_builtin_cache.insert("ui_text_backspace_word.macos", inputs);
  434. inputs = List<Ref<InputEvent>>();
  435. default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs);
  436. inputs = List<Ref<InputEvent>>();
  437. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_CMD));
  438. default_builtin_cache.insert("ui_text_backspace_all_to_left.macos", inputs);
  439. inputs = List<Ref<InputEvent>>();
  440. inputs.push_back(InputEventKey::create_reference(KEY_DELETE));
  441. default_builtin_cache.insert("ui_text_delete", inputs);
  442. inputs = List<Ref<InputEvent>>();
  443. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_CMD));
  444. default_builtin_cache.insert("ui_text_delete_word", inputs);
  445. inputs = List<Ref<InputEvent>>();
  446. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_ALT));
  447. default_builtin_cache.insert("ui_text_delete_word.macos", inputs);
  448. inputs = List<Ref<InputEvent>>();
  449. default_builtin_cache.insert("ui_text_delete_all_to_right", inputs);
  450. inputs = List<Ref<InputEvent>>();
  451. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_CMD));
  452. default_builtin_cache.insert("ui_text_delete_all_to_right.macos", inputs);
  453. // Text Caret Movement Left/Right
  454. inputs = List<Ref<InputEvent>>();
  455. inputs.push_back(InputEventKey::create_reference(KEY_LEFT));
  456. default_builtin_cache.insert("ui_text_caret_left", inputs);
  457. inputs = List<Ref<InputEvent>>();
  458. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_CMD));
  459. default_builtin_cache.insert("ui_text_caret_word_left", inputs);
  460. inputs = List<Ref<InputEvent>>();
  461. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_ALT));
  462. default_builtin_cache.insert("ui_text_caret_word_left.macos", inputs);
  463. inputs = List<Ref<InputEvent>>();
  464. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT));
  465. default_builtin_cache.insert("ui_text_caret_right", inputs);
  466. inputs = List<Ref<InputEvent>>();
  467. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_CMD));
  468. default_builtin_cache.insert("ui_text_caret_word_right", inputs);
  469. inputs = List<Ref<InputEvent>>();
  470. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_ALT));
  471. default_builtin_cache.insert("ui_text_caret_word_right.macos", inputs);
  472. // Text Caret Movement Up/Down
  473. inputs = List<Ref<InputEvent>>();
  474. inputs.push_back(InputEventKey::create_reference(KEY_UP));
  475. default_builtin_cache.insert("ui_text_caret_up", inputs);
  476. inputs = List<Ref<InputEvent>>();
  477. inputs.push_back(InputEventKey::create_reference(KEY_DOWN));
  478. default_builtin_cache.insert("ui_text_caret_down", inputs);
  479. // Text Caret Movement Line Start/End
  480. inputs = List<Ref<InputEvent>>();
  481. inputs.push_back(InputEventKey::create_reference(KEY_HOME));
  482. default_builtin_cache.insert("ui_text_caret_line_start", inputs);
  483. inputs = List<Ref<InputEvent>>();
  484. inputs.push_back(InputEventKey::create_reference(KEY_A | KEY_MASK_CTRL));
  485. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_CMD));
  486. default_builtin_cache.insert("ui_text_caret_line_start.macos", inputs);
  487. inputs = List<Ref<InputEvent>>();
  488. inputs.push_back(InputEventKey::create_reference(KEY_END));
  489. default_builtin_cache.insert("ui_text_caret_line_end", inputs);
  490. inputs = List<Ref<InputEvent>>();
  491. inputs.push_back(InputEventKey::create_reference(KEY_E | KEY_MASK_CTRL));
  492. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_CMD));
  493. default_builtin_cache.insert("ui_text_caret_line_end.macos", inputs);
  494. // Text Caret Movement Page Up/Down
  495. inputs = List<Ref<InputEvent>>();
  496. inputs.push_back(InputEventKey::create_reference(KEY_PAGEUP));
  497. default_builtin_cache.insert("ui_text_caret_page_up", inputs);
  498. inputs = List<Ref<InputEvent>>();
  499. inputs.push_back(InputEventKey::create_reference(KEY_PAGEDOWN));
  500. default_builtin_cache.insert("ui_text_caret_page_down", inputs);
  501. // Text Caret Movement Document Start/End
  502. inputs = List<Ref<InputEvent>>();
  503. inputs.push_back(InputEventKey::create_reference(KEY_HOME | KEY_MASK_CMD));
  504. default_builtin_cache.insert("ui_text_caret_document_start", inputs);
  505. inputs = List<Ref<InputEvent>>();
  506. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD));
  507. default_builtin_cache.insert("ui_text_caret_document_start.macos", inputs);
  508. inputs = List<Ref<InputEvent>>();
  509. inputs.push_back(InputEventKey::create_reference(KEY_END | KEY_MASK_CMD));
  510. default_builtin_cache.insert("ui_text_caret_document_end", inputs);
  511. inputs = List<Ref<InputEvent>>();
  512. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD));
  513. default_builtin_cache.insert("ui_text_caret_document_end.macos", inputs);
  514. // Text Scrolling
  515. inputs = List<Ref<InputEvent>>();
  516. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD));
  517. default_builtin_cache.insert("ui_text_scroll_up", inputs);
  518. inputs = List<Ref<InputEvent>>();
  519. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD | KEY_MASK_ALT));
  520. default_builtin_cache.insert("ui_text_scroll_up.macos", inputs);
  521. inputs = List<Ref<InputEvent>>();
  522. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD));
  523. default_builtin_cache.insert("ui_text_scroll_down", inputs);
  524. inputs = List<Ref<InputEvent>>();
  525. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD | KEY_MASK_ALT));
  526. default_builtin_cache.insert("ui_text_scroll_down.macos", inputs);
  527. // Text Misc
  528. inputs = List<Ref<InputEvent>>();
  529. inputs.push_back(InputEventKey::create_reference(KEY_A | KEY_MASK_CMD));
  530. default_builtin_cache.insert("ui_text_select_all", inputs);
  531. inputs = List<Ref<InputEvent>>();
  532. inputs.push_back(InputEventKey::create_reference(KEY_D | KEY_MASK_CMD));
  533. default_builtin_cache.insert("ui_text_select_word_under_caret", inputs);
  534. inputs = List<Ref<InputEvent>>();
  535. inputs.push_back(InputEventKey::create_reference(KEY_INSERT));
  536. default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs);
  537. inputs = List<Ref<InputEvent>>();
  538. inputs.push_back(InputEventKey::create_reference(KEY_MENU));
  539. default_builtin_cache.insert("ui_menu", inputs);
  540. inputs = List<Ref<InputEvent>>();
  541. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  542. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  543. default_builtin_cache.insert("ui_text_submit", inputs);
  544. // ///// UI Graph Shortcuts /////
  545. inputs = List<Ref<InputEvent>>();
  546. inputs.push_back(InputEventKey::create_reference(KEY_D | KEY_MASK_CMD));
  547. default_builtin_cache.insert("ui_graph_duplicate", inputs);
  548. inputs = List<Ref<InputEvent>>();
  549. inputs.push_back(InputEventKey::create_reference(KEY_DELETE));
  550. default_builtin_cache.insert("ui_graph_delete", inputs);
  551. // ///// UI File Dialog Shortcuts /////
  552. inputs = List<Ref<InputEvent>>();
  553. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE));
  554. default_builtin_cache.insert("ui_filedialog_up_one_level", inputs);
  555. inputs = List<Ref<InputEvent>>();
  556. inputs.push_back(InputEventKey::create_reference(KEY_F5));
  557. default_builtin_cache.insert("ui_filedialog_refresh", inputs);
  558. inputs = List<Ref<InputEvent>>();
  559. inputs.push_back(InputEventKey::create_reference(KEY_H));
  560. default_builtin_cache.insert("ui_filedialog_show_hidden", inputs);
  561. inputs = List<Ref<InputEvent>>();
  562. inputs.push_back(InputEventKey::create_reference(KEY_QUOTELEFT | KEY_MASK_CMD));
  563. default_builtin_cache.insert("ui_swap_input_direction", inputs);
  564. return default_builtin_cache;
  565. }
  566. const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with_feature_overrides_applied() {
  567. if (default_builtin_with_overrides_cache.size() > 0) {
  568. return default_builtin_with_overrides_cache;
  569. }
  570. OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins();
  571. // Get a list of all built in inputs which are valid overrides for the OS
  572. // Key = builtin name (e.g. ui_accept)
  573. // Value = override/feature names (e.g. macos, if it was defined as "ui_accept.macos" and the platform supports that feature)
  574. Map<String, Vector<String>> builtins_with_overrides;
  575. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  576. String fullname = E.key();
  577. Vector<String> split = fullname.split(".");
  578. String name = split[0];
  579. String override_for = split.size() > 1 ? split[1] : String();
  580. if (override_for != String() && OS::get_singleton()->has_feature(override_for)) {
  581. builtins_with_overrides[name].push_back(override_for);
  582. }
  583. }
  584. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  585. String fullname = E.key();
  586. Vector<String> split = fullname.split(".");
  587. String name = split[0];
  588. String override_for = split.size() > 1 ? split[1] : String();
  589. if (builtins_with_overrides.has(name) && override_for == String()) {
  590. // Builtin has an override but this particular one is not an override, so skip.
  591. continue;
  592. }
  593. if (override_for != String() && !OS::get_singleton()->has_feature(override_for)) {
  594. // OS does not support this override - skip.
  595. continue;
  596. }
  597. default_builtin_with_overrides_cache.insert(name, E.value());
  598. }
  599. return default_builtin_with_overrides_cache;
  600. }
  601. void InputMap::load_default() {
  602. OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins_with_feature_overrides_applied();
  603. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  604. String name = E.key();
  605. add_action(name);
  606. List<Ref<InputEvent>> inputs = E.get();
  607. for (List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
  608. Ref<InputEventKey> iek = I->get();
  609. // For the editor, only add keyboard actions.
  610. if (iek.is_valid()) {
  611. action_add_event(name, I->get());
  612. }
  613. }
  614. }
  615. }
  616. InputMap::InputMap() {
  617. ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist.");
  618. singleton = this;
  619. }
  620. InputMap::~InputMap() {
  621. singleton = nullptr;
  622. }