input_map.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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->get());
  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. bool pressed = input_event_action->is_pressed();
  184. if (p_pressed != nullptr) {
  185. *p_pressed = pressed;
  186. }
  187. if (p_strength != nullptr) {
  188. *p_strength = pressed ? input_event_action->get_strength() : 0.0f;
  189. }
  190. return input_event_action->get_action() == p_action;
  191. }
  192. bool pressed;
  193. float strength;
  194. float raw_strength;
  195. List<Ref<InputEvent>>::Element *event = _find_event(E.get(), p_event, p_exact_match, &pressed, &strength, &raw_strength);
  196. if (event != nullptr) {
  197. if (p_pressed != nullptr) {
  198. *p_pressed = pressed;
  199. }
  200. if (p_strength != nullptr) {
  201. *p_strength = strength;
  202. }
  203. if (p_raw_strength != nullptr) {
  204. *p_raw_strength = raw_strength;
  205. }
  206. return true;
  207. } else {
  208. return false;
  209. }
  210. }
  211. const OrderedHashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
  212. return input_map;
  213. }
  214. void InputMap::load_from_project_settings() {
  215. input_map.clear();
  216. List<PropertyInfo> pinfo;
  217. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  218. for (const PropertyInfo &pi : pinfo) {
  219. if (!pi.name.begins_with("input/")) {
  220. continue;
  221. }
  222. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  223. Dictionary action = ProjectSettings::get_singleton()->get(pi.name);
  224. float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f;
  225. Array events = action["events"];
  226. add_action(name, deadzone);
  227. for (int i = 0; i < events.size(); i++) {
  228. Ref<InputEvent> event = events[i];
  229. if (event.is_null()) {
  230. continue;
  231. }
  232. action_add_event(name, event);
  233. }
  234. }
  235. }
  236. struct _BuiltinActionDisplayName {
  237. const char *name;
  238. const char *display_name;
  239. };
  240. static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
  241. /* clang-format off */
  242. { "ui_accept", TTRC("Accept") },
  243. { "ui_select", TTRC("Select") },
  244. { "ui_cancel", TTRC("Cancel") },
  245. { "ui_focus_next", TTRC("Focus Next") },
  246. { "ui_focus_prev", TTRC("Focus Prev") },
  247. { "ui_left", TTRC("Left") },
  248. { "ui_right", TTRC("Right") },
  249. { "ui_up", TTRC("Up") },
  250. { "ui_down", TTRC("Down") },
  251. { "ui_page_up", TTRC("Page Up") },
  252. { "ui_page_down", TTRC("Page Down") },
  253. { "ui_home", TTRC("Home") },
  254. { "ui_end", TTRC("End") },
  255. { "ui_cut", TTRC("Cut") },
  256. { "ui_copy", TTRC("Copy") },
  257. { "ui_paste", TTRC("Paste") },
  258. { "ui_undo", TTRC("Undo") },
  259. { "ui_redo", TTRC("Redo") },
  260. { "ui_text_completion_query", TTRC("Completion Query") },
  261. { "ui_text_newline", TTRC("New Line") },
  262. { "ui_text_newline_blank", TTRC("New Blank Line") },
  263. { "ui_text_newline_above", TTRC("New Line Above") },
  264. { "ui_text_indent", TTRC("Indent") },
  265. { "ui_text_dedent", TTRC("Dedent") },
  266. { "ui_text_backspace", TTRC("Backspace") },
  267. { "ui_text_backspace_word", TTRC("Backspace Word") },
  268. { "ui_text_backspace_word.OSX", TTRC("Backspace Word") },
  269. { "ui_text_backspace_all_to_left", TTRC("Backspace all to Left") },
  270. { "ui_text_backspace_all_to_left.OSX", TTRC("Backspace all to Left") },
  271. { "ui_text_delete", TTRC("Delete") },
  272. { "ui_text_delete_word", TTRC("Delete Word") },
  273. { "ui_text_delete_word.OSX", TTRC("Delete Word") },
  274. { "ui_text_delete_all_to_right", TTRC("Delete all to Right") },
  275. { "ui_text_delete_all_to_right.OSX", TTRC("Delete all to Right") },
  276. { "ui_text_caret_left", TTRC("Caret Left") },
  277. { "ui_text_caret_word_left", TTRC("Caret Word Left") },
  278. { "ui_text_caret_word_left.OSX", TTRC("Caret Word Left") },
  279. { "ui_text_caret_right", TTRC("Caret Right") },
  280. { "ui_text_caret_word_right", TTRC("Caret Word Right") },
  281. { "ui_text_caret_word_right.OSX", TTRC("Caret Word Right") },
  282. { "ui_text_caret_up", TTRC("Caret Up") },
  283. { "ui_text_caret_down", TTRC("Caret Down") },
  284. { "ui_text_caret_line_start", TTRC("Caret Line Start") },
  285. { "ui_text_caret_line_start.OSX", TTRC("Caret Line Start") },
  286. { "ui_text_caret_line_end", TTRC("Caret Line End") },
  287. { "ui_text_caret_line_end.OSX", TTRC("Caret Line End") },
  288. { "ui_text_caret_page_up", TTRC("Caret Page Up") },
  289. { "ui_text_caret_page_down", TTRC("Caret Page Down") },
  290. { "ui_text_caret_document_start", TTRC("Caret Document Start") },
  291. { "ui_text_caret_document_start.OSX", TTRC("Caret Document Start") },
  292. { "ui_text_caret_document_end", TTRC("Caret Document End") },
  293. { "ui_text_caret_document_end.OSX", TTRC("Caret Document End") },
  294. { "ui_text_scroll_up", TTRC("Scroll Up") },
  295. { "ui_text_scroll_up.OSX", TTRC("Scroll Up") },
  296. { "ui_text_scroll_down", TTRC("Scroll Down") },
  297. { "ui_text_scroll_down.OSX", TTRC("Scroll Down") },
  298. { "ui_text_select_all", TTRC("Select All") },
  299. { "ui_text_select_word_under_caret", TTRC("Select Word Under Caret") },
  300. { "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") },
  301. { "ui_text_submit", TTRC("Text Submitted") },
  302. { "ui_graph_duplicate", TTRC("Duplicate Nodes") },
  303. { "ui_graph_delete", TTRC("Delete Nodes") },
  304. { "ui_filedialog_up_one_level", TTRC("Go Up One Level") },
  305. { "ui_filedialog_refresh", TTRC("Refresh") },
  306. { "ui_filedialog_show_hidden", TTRC("Show Hidden") },
  307. { "ui_swap_input_direction ", TTRC("Swap Input Direction") },
  308. { "", TTRC("")}
  309. /* clang-format on */
  310. };
  311. String InputMap::get_builtin_display_name(const String &p_name) const {
  312. int len = sizeof(_builtin_action_display_names) / sizeof(_BuiltinActionDisplayName);
  313. for (int i = 0; i < len; i++) {
  314. if (_builtin_action_display_names[i].name == p_name) {
  315. return RTR(_builtin_action_display_names[i].display_name);
  316. }
  317. }
  318. return p_name;
  319. }
  320. const OrderedHashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
  321. // Return cache if it has already been built.
  322. if (default_builtin_cache.size()) {
  323. return default_builtin_cache;
  324. }
  325. List<Ref<InputEvent>> inputs;
  326. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  327. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  328. inputs.push_back(InputEventKey::create_reference(KEY_SPACE));
  329. default_builtin_cache.insert("ui_accept", inputs);
  330. inputs = List<Ref<InputEvent>>();
  331. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_Y));
  332. inputs.push_back(InputEventKey::create_reference(KEY_SPACE));
  333. default_builtin_cache.insert("ui_select", inputs);
  334. inputs = List<Ref<InputEvent>>();
  335. inputs.push_back(InputEventKey::create_reference(KEY_ESCAPE));
  336. default_builtin_cache.insert("ui_cancel", inputs);
  337. inputs = List<Ref<InputEvent>>();
  338. inputs.push_back(InputEventKey::create_reference(KEY_TAB));
  339. default_builtin_cache.insert("ui_focus_next", inputs);
  340. inputs = List<Ref<InputEvent>>();
  341. inputs.push_back(InputEventKey::create_reference(KEY_TAB | KEY_MASK_SHIFT));
  342. default_builtin_cache.insert("ui_focus_prev", inputs);
  343. inputs = List<Ref<InputEvent>>();
  344. inputs.push_back(InputEventKey::create_reference(KEY_LEFT));
  345. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_LEFT));
  346. default_builtin_cache.insert("ui_left", inputs);
  347. inputs = List<Ref<InputEvent>>();
  348. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT));
  349. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_RIGHT));
  350. default_builtin_cache.insert("ui_right", inputs);
  351. inputs = List<Ref<InputEvent>>();
  352. inputs.push_back(InputEventKey::create_reference(KEY_UP));
  353. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_UP));
  354. default_builtin_cache.insert("ui_up", inputs);
  355. inputs = List<Ref<InputEvent>>();
  356. inputs.push_back(InputEventKey::create_reference(KEY_DOWN));
  357. inputs.push_back(InputEventJoypadButton::create_reference(JOY_BUTTON_DPAD_DOWN));
  358. default_builtin_cache.insert("ui_down", inputs);
  359. inputs = List<Ref<InputEvent>>();
  360. inputs.push_back(InputEventKey::create_reference(KEY_PAGEUP));
  361. default_builtin_cache.insert("ui_page_up", inputs);
  362. inputs = List<Ref<InputEvent>>();
  363. inputs.push_back(InputEventKey::create_reference(KEY_PAGEDOWN));
  364. default_builtin_cache.insert("ui_page_down", inputs);
  365. inputs = List<Ref<InputEvent>>();
  366. inputs.push_back(InputEventKey::create_reference(KEY_HOME));
  367. default_builtin_cache.insert("ui_home", inputs);
  368. inputs = List<Ref<InputEvent>>();
  369. inputs.push_back(InputEventKey::create_reference(KEY_END));
  370. default_builtin_cache.insert("ui_end", inputs);
  371. // ///// UI basic Shortcuts /////
  372. inputs = List<Ref<InputEvent>>();
  373. inputs.push_back(InputEventKey::create_reference(KEY_X | KEY_MASK_CMD));
  374. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_SHIFT));
  375. default_builtin_cache.insert("ui_cut", inputs);
  376. inputs = List<Ref<InputEvent>>();
  377. inputs.push_back(InputEventKey::create_reference(KEY_C | KEY_MASK_CMD));
  378. inputs.push_back(InputEventKey::create_reference(KEY_INSERT | KEY_MASK_CMD));
  379. default_builtin_cache.insert("ui_copy", inputs);
  380. inputs = List<Ref<InputEvent>>();
  381. inputs.push_back(InputEventKey::create_reference(KEY_V | KEY_MASK_CMD));
  382. inputs.push_back(InputEventKey::create_reference(KEY_INSERT | KEY_MASK_SHIFT));
  383. default_builtin_cache.insert("ui_paste", inputs);
  384. inputs = List<Ref<InputEvent>>();
  385. inputs.push_back(InputEventKey::create_reference(KEY_Z | KEY_MASK_CMD));
  386. default_builtin_cache.insert("ui_undo", inputs);
  387. inputs = List<Ref<InputEvent>>();
  388. inputs.push_back(InputEventKey::create_reference(KEY_Z | KEY_MASK_CMD | KEY_MASK_SHIFT));
  389. inputs.push_back(InputEventKey::create_reference(KEY_Y | KEY_MASK_CMD));
  390. default_builtin_cache.insert("ui_redo", inputs);
  391. // ///// UI Text Input Shortcuts /////
  392. inputs = List<Ref<InputEvent>>();
  393. inputs.push_back(InputEventKey::create_reference(KEY_SPACE | KEY_MASK_CMD));
  394. default_builtin_cache.insert("ui_text_completion_query", inputs);
  395. inputs = List<Ref<InputEvent>>();
  396. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  397. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  398. default_builtin_cache.insert("ui_text_completion_accept", inputs);
  399. inputs = List<Ref<InputEvent>>();
  400. inputs.push_back(InputEventKey::create_reference(KEY_TAB));
  401. default_builtin_cache.insert("ui_text_completion_replace", inputs);
  402. // Newlines
  403. inputs = List<Ref<InputEvent>>();
  404. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  405. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  406. default_builtin_cache.insert("ui_text_newline", inputs);
  407. inputs = List<Ref<InputEvent>>();
  408. inputs.push_back(InputEventKey::create_reference(KEY_ENTER | KEY_MASK_CMD));
  409. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER | KEY_MASK_CMD));
  410. default_builtin_cache.insert("ui_text_newline_blank", inputs);
  411. inputs = List<Ref<InputEvent>>();
  412. inputs.push_back(InputEventKey::create_reference(KEY_ENTER | KEY_MASK_SHIFT | KEY_MASK_CMD));
  413. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER | KEY_MASK_SHIFT | KEY_MASK_CMD));
  414. default_builtin_cache.insert("ui_text_newline_above", inputs);
  415. // Indentation
  416. inputs = List<Ref<InputEvent>>();
  417. inputs.push_back(InputEventKey::create_reference(KEY_TAB));
  418. default_builtin_cache.insert("ui_text_indent", inputs);
  419. inputs = List<Ref<InputEvent>>();
  420. inputs.push_back(InputEventKey::create_reference(KEY_TAB | KEY_MASK_SHIFT));
  421. default_builtin_cache.insert("ui_text_dedent", inputs);
  422. // Text Backspace and Delete
  423. inputs = List<Ref<InputEvent>>();
  424. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE));
  425. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_SHIFT));
  426. default_builtin_cache.insert("ui_text_backspace", inputs);
  427. inputs = List<Ref<InputEvent>>();
  428. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_CMD));
  429. default_builtin_cache.insert("ui_text_backspace_word", inputs);
  430. inputs = List<Ref<InputEvent>>();
  431. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_ALT));
  432. default_builtin_cache.insert("ui_text_backspace_word.OSX", inputs);
  433. inputs = List<Ref<InputEvent>>();
  434. default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs);
  435. inputs = List<Ref<InputEvent>>();
  436. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_CMD));
  437. default_builtin_cache.insert("ui_text_backspace_all_to_left.OSX", inputs);
  438. inputs = List<Ref<InputEvent>>();
  439. inputs.push_back(InputEventKey::create_reference(KEY_DELETE));
  440. default_builtin_cache.insert("ui_text_delete", inputs);
  441. inputs = List<Ref<InputEvent>>();
  442. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_CMD));
  443. default_builtin_cache.insert("ui_text_delete_word", inputs);
  444. inputs = List<Ref<InputEvent>>();
  445. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_ALT));
  446. default_builtin_cache.insert("ui_text_delete_word.OSX", inputs);
  447. inputs = List<Ref<InputEvent>>();
  448. default_builtin_cache.insert("ui_text_delete_all_to_right", inputs);
  449. inputs = List<Ref<InputEvent>>();
  450. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_CMD));
  451. default_builtin_cache.insert("ui_text_delete_all_to_right.OSX", inputs);
  452. // Text Caret Movement Left/Right
  453. inputs = List<Ref<InputEvent>>();
  454. inputs.push_back(InputEventKey::create_reference(KEY_LEFT));
  455. default_builtin_cache.insert("ui_text_caret_left", inputs);
  456. inputs = List<Ref<InputEvent>>();
  457. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_CMD));
  458. default_builtin_cache.insert("ui_text_caret_word_left", inputs);
  459. inputs = List<Ref<InputEvent>>();
  460. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_ALT));
  461. default_builtin_cache.insert("ui_text_caret_word_left.OSX", inputs);
  462. inputs = List<Ref<InputEvent>>();
  463. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT));
  464. default_builtin_cache.insert("ui_text_caret_right", inputs);
  465. inputs = List<Ref<InputEvent>>();
  466. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_CMD));
  467. default_builtin_cache.insert("ui_text_caret_word_right", inputs);
  468. inputs = List<Ref<InputEvent>>();
  469. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_ALT));
  470. default_builtin_cache.insert("ui_text_caret_word_right.OSX", inputs);
  471. // Text Caret Movement Up/Down
  472. inputs = List<Ref<InputEvent>>();
  473. inputs.push_back(InputEventKey::create_reference(KEY_UP));
  474. default_builtin_cache.insert("ui_text_caret_up", inputs);
  475. inputs = List<Ref<InputEvent>>();
  476. inputs.push_back(InputEventKey::create_reference(KEY_DOWN));
  477. default_builtin_cache.insert("ui_text_caret_down", inputs);
  478. // Text Caret Movement Line Start/End
  479. inputs = List<Ref<InputEvent>>();
  480. inputs.push_back(InputEventKey::create_reference(KEY_HOME));
  481. default_builtin_cache.insert("ui_text_caret_line_start", inputs);
  482. inputs = List<Ref<InputEvent>>();
  483. inputs.push_back(InputEventKey::create_reference(KEY_A | KEY_MASK_CTRL));
  484. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_CMD));
  485. default_builtin_cache.insert("ui_text_caret_line_start.OSX", inputs);
  486. inputs = List<Ref<InputEvent>>();
  487. inputs.push_back(InputEventKey::create_reference(KEY_END));
  488. default_builtin_cache.insert("ui_text_caret_line_end", inputs);
  489. inputs = List<Ref<InputEvent>>();
  490. inputs.push_back(InputEventKey::create_reference(KEY_E | KEY_MASK_CTRL));
  491. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_CMD));
  492. default_builtin_cache.insert("ui_text_caret_line_end.OSX", inputs);
  493. // Text Caret Movement Page Up/Down
  494. inputs = List<Ref<InputEvent>>();
  495. inputs.push_back(InputEventKey::create_reference(KEY_PAGEUP));
  496. default_builtin_cache.insert("ui_text_caret_page_up", inputs);
  497. inputs = List<Ref<InputEvent>>();
  498. inputs.push_back(InputEventKey::create_reference(KEY_PAGEDOWN));
  499. default_builtin_cache.insert("ui_text_caret_page_down", inputs);
  500. // Text Caret Movement Document Start/End
  501. inputs = List<Ref<InputEvent>>();
  502. inputs.push_back(InputEventKey::create_reference(KEY_HOME | KEY_MASK_CMD));
  503. default_builtin_cache.insert("ui_text_caret_document_start", inputs);
  504. inputs = List<Ref<InputEvent>>();
  505. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD));
  506. default_builtin_cache.insert("ui_text_caret_document_start.OSX", inputs);
  507. inputs = List<Ref<InputEvent>>();
  508. inputs.push_back(InputEventKey::create_reference(KEY_END | KEY_MASK_CMD));
  509. default_builtin_cache.insert("ui_text_caret_document_end", inputs);
  510. inputs = List<Ref<InputEvent>>();
  511. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD));
  512. default_builtin_cache.insert("ui_text_caret_document_end.OSX", inputs);
  513. // Text Scrolling
  514. inputs = List<Ref<InputEvent>>();
  515. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD));
  516. default_builtin_cache.insert("ui_text_scroll_up", inputs);
  517. inputs = List<Ref<InputEvent>>();
  518. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD | KEY_MASK_ALT));
  519. default_builtin_cache.insert("ui_text_scroll_up.OSX", inputs);
  520. inputs = List<Ref<InputEvent>>();
  521. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD));
  522. default_builtin_cache.insert("ui_text_scroll_down", inputs);
  523. inputs = List<Ref<InputEvent>>();
  524. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD | KEY_MASK_ALT));
  525. default_builtin_cache.insert("ui_text_scroll_down.OSX", inputs);
  526. // Text Misc
  527. inputs = List<Ref<InputEvent>>();
  528. inputs.push_back(InputEventKey::create_reference(KEY_A | KEY_MASK_CMD));
  529. default_builtin_cache.insert("ui_text_select_all", inputs);
  530. inputs = List<Ref<InputEvent>>();
  531. inputs.push_back(InputEventKey::create_reference(KEY_D | KEY_MASK_CMD));
  532. default_builtin_cache.insert("ui_text_select_word_under_caret", inputs);
  533. inputs = List<Ref<InputEvent>>();
  534. inputs.push_back(InputEventKey::create_reference(KEY_INSERT));
  535. default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs);
  536. inputs = List<Ref<InputEvent>>();
  537. inputs.push_back(InputEventKey::create_reference(KEY_MENU));
  538. default_builtin_cache.insert("ui_menu", inputs);
  539. inputs = List<Ref<InputEvent>>();
  540. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  541. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  542. default_builtin_cache.insert("ui_text_submit", inputs);
  543. // ///// UI Graph Shortcuts /////
  544. inputs = List<Ref<InputEvent>>();
  545. inputs.push_back(InputEventKey::create_reference(KEY_D | KEY_MASK_CMD));
  546. default_builtin_cache.insert("ui_graph_duplicate", inputs);
  547. inputs = List<Ref<InputEvent>>();
  548. inputs.push_back(InputEventKey::create_reference(KEY_DELETE));
  549. default_builtin_cache.insert("ui_graph_delete", inputs);
  550. // ///// UI File Dialog Shortcuts /////
  551. inputs = List<Ref<InputEvent>>();
  552. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE));
  553. default_builtin_cache.insert("ui_filedialog_up_one_level", inputs);
  554. inputs = List<Ref<InputEvent>>();
  555. inputs.push_back(InputEventKey::create_reference(KEY_F5));
  556. default_builtin_cache.insert("ui_filedialog_refresh", inputs);
  557. inputs = List<Ref<InputEvent>>();
  558. inputs.push_back(InputEventKey::create_reference(KEY_H));
  559. default_builtin_cache.insert("ui_filedialog_show_hidden", inputs);
  560. inputs = List<Ref<InputEvent>>();
  561. inputs.push_back(InputEventKey::create_reference(KEY_QUOTELEFT | KEY_MASK_CMD));
  562. default_builtin_cache.insert("ui_swap_input_direction", inputs);
  563. return default_builtin_cache;
  564. }
  565. void InputMap::load_default() {
  566. OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins();
  567. // List of Builtins which have an override for OSX.
  568. Vector<String> osx_builtins;
  569. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  570. if (String(E.key()).ends_with(".OSX")) {
  571. // Strip .OSX from name: some_input_name.OSX -> some_input_name
  572. osx_builtins.push_back(String(E.key()).split(".")[0]);
  573. }
  574. }
  575. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  576. String fullname = E.key();
  577. String name = fullname.split(".")[0];
  578. String override_for = fullname.split(".").size() > 1 ? fullname.split(".")[1] : "";
  579. #ifdef APPLE_STYLE_KEYS
  580. if (osx_builtins.has(name) && override_for != "OSX") {
  581. // Name has osx builtin but this particular one is for non-osx systems - so skip.
  582. continue;
  583. }
  584. #else
  585. if (override_for == "OSX") {
  586. // Override for OSX - not needed on non-osx platforms.
  587. continue;
  588. }
  589. #endif
  590. add_action(name);
  591. List<Ref<InputEvent>> inputs = E.get();
  592. for (List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
  593. Ref<InputEventKey> iek = I->get();
  594. // For the editor, only add keyboard actions.
  595. if (iek.is_valid()) {
  596. action_add_event(name, I->get());
  597. }
  598. }
  599. }
  600. }
  601. InputMap::InputMap() {
  602. ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist.");
  603. singleton = this;
  604. }