input_map.cpp 31 KB

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