input_map.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_add_event", "action", "event"), &InputMap::action_add_event);
  43. ClassDB::bind_method(D_METHOD("action_has_event", "action", "event"), &InputMap::action_has_event);
  44. ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
  45. ClassDB::bind_method(D_METHOD("action_erase_events", "action"), &InputMap::action_erase_events);
  46. ClassDB::bind_method(D_METHOD("action_get_events", "action"), &InputMap::_action_get_events);
  47. ClassDB::bind_method(D_METHOD("event_is_action", "event", "action", "exact_match"), &InputMap::event_is_action, DEFVAL(false));
  48. ClassDB::bind_method(D_METHOD("load_from_project_settings"), &InputMap::load_from_project_settings);
  49. }
  50. /**
  51. * Returns an nonexistent action error message with a suggestion of the closest
  52. * matching action name (if possible).
  53. */
  54. String InputMap::_suggest_actions(const StringName &p_action) const {
  55. List<StringName> actions = get_actions();
  56. StringName closest_action;
  57. float closest_similarity = 0.0;
  58. // Find the most action with the most similar name.
  59. for (List<StringName>::Element *E = actions.front(); E; E = E->next()) {
  60. const float similarity = String(E->get()).similarity(p_action);
  61. if (similarity > closest_similarity) {
  62. closest_action = E->get();
  63. closest_similarity = similarity;
  64. }
  65. }
  66. String error_message = vformat("The InputMap action \"%s\" doesn't exist.", p_action);
  67. if (closest_similarity >= 0.4) {
  68. // Only include a suggestion in the error message if it's similar enough.
  69. error_message += vformat(" Did you mean \"%s\"?", closest_action);
  70. }
  71. return error_message;
  72. }
  73. void InputMap::add_action(const StringName &p_action, float p_deadzone) {
  74. ERR_FAIL_COND_MSG(input_map.has(p_action), "InputMap already has action \"" + String(p_action) + "\".");
  75. input_map[p_action] = Action();
  76. static int last_id = 1;
  77. input_map[p_action].id = last_id;
  78. input_map[p_action].deadzone = p_deadzone;
  79. last_id++;
  80. }
  81. void InputMap::erase_action(const StringName &p_action) {
  82. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  83. input_map.erase(p_action);
  84. }
  85. Array InputMap::_get_actions() {
  86. Array ret;
  87. List<StringName> actions = get_actions();
  88. if (actions.is_empty()) {
  89. return ret;
  90. }
  91. for (const List<StringName>::Element *E = actions.front(); E; E = E->next()) {
  92. ret.push_back(E->get());
  93. }
  94. return ret;
  95. }
  96. List<StringName> InputMap::get_actions() const {
  97. List<StringName> actions = List<StringName>();
  98. if (input_map.is_empty()) {
  99. return actions;
  100. }
  101. for (OrderedHashMap<StringName, Action>::Element E = input_map.front(); E; E = E.next()) {
  102. actions.push_back(E.key());
  103. }
  104. return actions;
  105. }
  106. 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 {
  107. ERR_FAIL_COND_V(!p_event.is_valid(), nullptr);
  108. for (List<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
  109. const Ref<InputEvent> e = E->get();
  110. //if (e.type != Ref<InputEvent>::KEY && e.device != p_event.device) -- unsure about the KEY comparison, why is this here?
  111. // continue;
  112. int device = e->get_device();
  113. if (device == ALL_DEVICES || device == p_event->get_device()) {
  114. if (p_exact_match && e->shortcut_match(p_event)) {
  115. return E;
  116. } else if (!p_exact_match && e->action_match(p_event, p_pressed, p_strength, p_raw_strength, p_action.deadzone)) {
  117. return E;
  118. }
  119. }
  120. }
  121. return nullptr;
  122. }
  123. bool InputMap::has_action(const StringName &p_action) const {
  124. return input_map.has(p_action);
  125. }
  126. float InputMap::action_get_deadzone(const StringName &p_action) {
  127. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, _suggest_actions(p_action));
  128. return input_map[p_action].deadzone;
  129. }
  130. void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
  131. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  132. input_map[p_action].deadzone = p_deadzone;
  133. }
  134. void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  135. ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object.");
  136. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  137. if (_find_event(input_map[p_action], p_event, true)) {
  138. return; // Already addded.
  139. }
  140. input_map[p_action].inputs.push_back(p_event);
  141. }
  142. bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  143. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, _suggest_actions(p_action));
  144. return (_find_event(input_map[p_action], p_event, true) != nullptr);
  145. }
  146. void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  147. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  148. List<Ref<InputEvent>>::Element *E = _find_event(input_map[p_action], p_event, true);
  149. if (E) {
  150. input_map[p_action].inputs.erase(E);
  151. if (Input::get_singleton()->is_action_pressed(p_action)) {
  152. Input::get_singleton()->action_release(p_action);
  153. }
  154. }
  155. }
  156. void InputMap::action_erase_events(const StringName &p_action) {
  157. ERR_FAIL_COND_MSG(!input_map.has(p_action), _suggest_actions(p_action));
  158. input_map[p_action].inputs.clear();
  159. }
  160. Array InputMap::_action_get_events(const StringName &p_action) {
  161. Array ret;
  162. const List<Ref<InputEvent>> *al = action_get_events(p_action);
  163. if (al) {
  164. for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) {
  165. ret.push_back(E->get());
  166. }
  167. }
  168. return ret;
  169. }
  170. const List<Ref<InputEvent>> *InputMap::action_get_events(const StringName &p_action) {
  171. const OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
  172. if (!E) {
  173. return nullptr;
  174. }
  175. return &E.get().inputs;
  176. }
  177. bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
  178. return event_get_action_status(p_event, p_action, p_exact_match);
  179. }
  180. 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 {
  181. OrderedHashMap<StringName, Action>::Element E = input_map.find(p_action);
  182. ERR_FAIL_COND_V_MSG(!E, false, _suggest_actions(p_action));
  183. Ref<InputEventAction> input_event_action = p_event;
  184. if (input_event_action.is_valid()) {
  185. if (p_pressed != nullptr) {
  186. *p_pressed = input_event_action->is_pressed();
  187. }
  188. if (p_strength != nullptr) {
  189. *p_strength = (p_pressed != nullptr && *p_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 (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  220. const PropertyInfo &pi = E->get();
  221. if (!pi.name.begins_with("input/")) {
  222. continue;
  223. }
  224. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  225. Dictionary action = ProjectSettings::get_singleton()->get(pi.name);
  226. float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f;
  227. Array events = action["events"];
  228. add_action(name, deadzone);
  229. for (int i = 0; i < events.size(); i++) {
  230. Ref<InputEvent> event = events[i];
  231. if (event.is_null()) {
  232. continue;
  233. }
  234. action_add_event(name, event);
  235. }
  236. }
  237. }
  238. struct _BuiltinActionDisplayName {
  239. const char *name;
  240. const char *display_name;
  241. };
  242. static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
  243. /* clang-format off */
  244. { "ui_accept", TTRC("Accept") },
  245. { "ui_select", TTRC("Select") },
  246. { "ui_cancel", TTRC("Cancel") },
  247. { "ui_focus_next", TTRC("Focus Next") },
  248. { "ui_focus_prev", TTRC("Focus Prev") },
  249. { "ui_left", TTRC("Left") },
  250. { "ui_right", TTRC("Right") },
  251. { "ui_up", TTRC("Up") },
  252. { "ui_down", TTRC("Down") },
  253. { "ui_page_up", TTRC("Page Up") },
  254. { "ui_page_down", TTRC("Page Down") },
  255. { "ui_home", TTRC("Home") },
  256. { "ui_end", TTRC("End") },
  257. { "ui_cut", TTRC("Cut") },
  258. { "ui_copy", TTRC("Copy") },
  259. { "ui_paste", TTRC("Paste") },
  260. { "ui_undo", TTRC("Undo") },
  261. { "ui_redo", TTRC("Redo") },
  262. { "ui_text_completion_query", TTRC("Completion Query") },
  263. { "ui_text_newline", TTRC("New Line") },
  264. { "ui_text_newline_blank", TTRC("New Blank Line") },
  265. { "ui_text_newline_above", TTRC("New Line Above") },
  266. { "ui_text_indent", TTRC("Indent") },
  267. { "ui_text_dedent", TTRC("Dedent") },
  268. { "ui_text_backspace", TTRC("Backspace") },
  269. { "ui_text_backspace_word", TTRC("Backspace Word") },
  270. { "ui_text_backspace_word.OSX", TTRC("Backspace Word") },
  271. { "ui_text_backspace_all_to_left", TTRC("Backspace all to Left") },
  272. { "ui_text_backspace_all_to_left.OSX", TTRC("Backspace all to Left") },
  273. { "ui_text_delete", TTRC("Delete") },
  274. { "ui_text_delete_word", TTRC("Delete Word") },
  275. { "ui_text_delete_word.OSX", TTRC("Delete Word") },
  276. { "ui_text_delete_all_to_right", TTRC("Delete all to Right") },
  277. { "ui_text_delete_all_to_right.OSX", TTRC("Delete all to Right") },
  278. { "ui_text_caret_left", TTRC("Caret Left") },
  279. { "ui_text_caret_word_left", TTRC("Caret Word Left") },
  280. { "ui_text_caret_word_left.OSX", TTRC("Caret Word Left") },
  281. { "ui_text_caret_right", TTRC("Caret Right") },
  282. { "ui_text_caret_word_right", TTRC("Caret Word Right") },
  283. { "ui_text_caret_word_right.OSX", TTRC("Caret Word Right") },
  284. { "ui_text_caret_up", TTRC("Caret Up") },
  285. { "ui_text_caret_down", TTRC("Caret Down") },
  286. { "ui_text_caret_line_start", TTRC("Caret Line Start") },
  287. { "ui_text_caret_line_start.OSX", TTRC("Caret Line Start") },
  288. { "ui_text_caret_line_end", TTRC("Caret Line End") },
  289. { "ui_text_caret_line_end.OSX", TTRC("Caret Line End") },
  290. { "ui_text_caret_page_up", TTRC("Caret Page Up") },
  291. { "ui_text_caret_page_down", TTRC("Caret Page Down") },
  292. { "ui_text_caret_document_start", TTRC("Caret Document Start") },
  293. { "ui_text_caret_document_start.OSX", TTRC("Caret Document Start") },
  294. { "ui_text_caret_document_end", TTRC("Caret Document End") },
  295. { "ui_text_caret_document_end.OSX", TTRC("Caret Document End") },
  296. { "ui_text_scroll_up", TTRC("Scroll Up") },
  297. { "ui_text_scroll_up.OSX", TTRC("Scroll Up") },
  298. { "ui_text_scroll_down", TTRC("Scroll Down") },
  299. { "ui_text_scroll_down.OSX", TTRC("Scroll Down") },
  300. { "ui_text_select_all", TTRC("Select All") },
  301. { "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") },
  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_TAB));
  397. default_builtin_cache.insert("ui_text_completion_accept", inputs);
  398. // Newlines
  399. inputs = List<Ref<InputEvent>>();
  400. inputs.push_back(InputEventKey::create_reference(KEY_ENTER));
  401. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER));
  402. default_builtin_cache.insert("ui_text_newline", inputs);
  403. inputs = List<Ref<InputEvent>>();
  404. inputs.push_back(InputEventKey::create_reference(KEY_ENTER | KEY_MASK_CMD));
  405. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER | KEY_MASK_CMD));
  406. default_builtin_cache.insert("ui_text_newline_blank", inputs);
  407. inputs = List<Ref<InputEvent>>();
  408. inputs.push_back(InputEventKey::create_reference(KEY_ENTER | KEY_MASK_SHIFT | KEY_MASK_CMD));
  409. inputs.push_back(InputEventKey::create_reference(KEY_KP_ENTER | KEY_MASK_SHIFT | KEY_MASK_CMD));
  410. default_builtin_cache.insert("ui_text_newline_above", inputs);
  411. // Indentation
  412. inputs = List<Ref<InputEvent>>();
  413. inputs.push_back(InputEventKey::create_reference(KEY_TAB));
  414. default_builtin_cache.insert("ui_text_indent", inputs);
  415. inputs = List<Ref<InputEvent>>();
  416. inputs.push_back(InputEventKey::create_reference(KEY_TAB | KEY_MASK_SHIFT));
  417. default_builtin_cache.insert("ui_text_dedent", inputs);
  418. // Text Backspace and Delete
  419. inputs = List<Ref<InputEvent>>();
  420. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE));
  421. default_builtin_cache.insert("ui_text_backspace", inputs);
  422. inputs = List<Ref<InputEvent>>();
  423. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_CMD));
  424. default_builtin_cache.insert("ui_text_backspace_word", inputs);
  425. inputs = List<Ref<InputEvent>>();
  426. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_ALT));
  427. default_builtin_cache.insert("ui_text_backspace_word.OSX", inputs);
  428. inputs = List<Ref<InputEvent>>();
  429. default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs);
  430. inputs = List<Ref<InputEvent>>();
  431. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE | KEY_MASK_CMD));
  432. default_builtin_cache.insert("ui_text_backspace_all_to_left.OSX", inputs);
  433. inputs = List<Ref<InputEvent>>();
  434. inputs.push_back(InputEventKey::create_reference(KEY_DELETE));
  435. default_builtin_cache.insert("ui_text_delete", inputs);
  436. inputs = List<Ref<InputEvent>>();
  437. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_CMD));
  438. default_builtin_cache.insert("ui_text_delete_word", inputs);
  439. inputs = List<Ref<InputEvent>>();
  440. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_ALT));
  441. default_builtin_cache.insert("ui_text_delete_word.OSX", inputs);
  442. inputs = List<Ref<InputEvent>>();
  443. default_builtin_cache.insert("ui_text_delete_all_to_right", inputs);
  444. inputs = List<Ref<InputEvent>>();
  445. inputs.push_back(InputEventKey::create_reference(KEY_DELETE | KEY_MASK_CMD));
  446. default_builtin_cache.insert("ui_text_delete_all_to_right.OSX", inputs);
  447. // Text Caret Movement Left/Right
  448. inputs = List<Ref<InputEvent>>();
  449. inputs.push_back(InputEventKey::create_reference(KEY_LEFT));
  450. default_builtin_cache.insert("ui_text_caret_left", inputs);
  451. inputs = List<Ref<InputEvent>>();
  452. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_CMD));
  453. default_builtin_cache.insert("ui_text_caret_word_left", inputs);
  454. inputs = List<Ref<InputEvent>>();
  455. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_ALT));
  456. default_builtin_cache.insert("ui_text_caret_word_left.OSX", inputs);
  457. inputs = List<Ref<InputEvent>>();
  458. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT));
  459. default_builtin_cache.insert("ui_text_caret_right", inputs);
  460. inputs = List<Ref<InputEvent>>();
  461. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_CMD));
  462. default_builtin_cache.insert("ui_text_caret_word_right", inputs);
  463. inputs = List<Ref<InputEvent>>();
  464. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_ALT));
  465. default_builtin_cache.insert("ui_text_caret_word_right.OSX", inputs);
  466. // Text Caret Movement Up/Down
  467. inputs = List<Ref<InputEvent>>();
  468. inputs.push_back(InputEventKey::create_reference(KEY_UP));
  469. default_builtin_cache.insert("ui_text_caret_up", inputs);
  470. inputs = List<Ref<InputEvent>>();
  471. inputs.push_back(InputEventKey::create_reference(KEY_DOWN));
  472. default_builtin_cache.insert("ui_text_caret_down", inputs);
  473. // Text Caret Movement Line Start/End
  474. inputs = List<Ref<InputEvent>>();
  475. inputs.push_back(InputEventKey::create_reference(KEY_HOME));
  476. default_builtin_cache.insert("ui_text_caret_line_start", inputs);
  477. inputs = List<Ref<InputEvent>>();
  478. inputs.push_back(InputEventKey::create_reference(KEY_A | KEY_MASK_CTRL));
  479. inputs.push_back(InputEventKey::create_reference(KEY_LEFT | KEY_MASK_CMD));
  480. default_builtin_cache.insert("ui_text_caret_line_start.OSX", inputs);
  481. inputs = List<Ref<InputEvent>>();
  482. inputs.push_back(InputEventKey::create_reference(KEY_END));
  483. default_builtin_cache.insert("ui_text_caret_line_end", inputs);
  484. inputs = List<Ref<InputEvent>>();
  485. inputs.push_back(InputEventKey::create_reference(KEY_E | KEY_MASK_CTRL));
  486. inputs.push_back(InputEventKey::create_reference(KEY_RIGHT | KEY_MASK_CMD));
  487. default_builtin_cache.insert("ui_text_caret_line_end.OSX", inputs);
  488. // Text Caret Movement Page Up/Down
  489. inputs = List<Ref<InputEvent>>();
  490. inputs.push_back(InputEventKey::create_reference(KEY_PAGEUP));
  491. default_builtin_cache.insert("ui_text_caret_page_up", inputs);
  492. inputs = List<Ref<InputEvent>>();
  493. inputs.push_back(InputEventKey::create_reference(KEY_PAGEDOWN));
  494. default_builtin_cache.insert("ui_text_caret_page_down", inputs);
  495. // Text Caret Movement Document Start/End
  496. inputs = List<Ref<InputEvent>>();
  497. inputs.push_back(InputEventKey::create_reference(KEY_HOME | KEY_MASK_CMD));
  498. default_builtin_cache.insert("ui_text_caret_document_start", inputs);
  499. inputs = List<Ref<InputEvent>>();
  500. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD));
  501. default_builtin_cache.insert("ui_text_caret_document_start.OSX", inputs);
  502. inputs = List<Ref<InputEvent>>();
  503. inputs.push_back(InputEventKey::create_reference(KEY_END | KEY_MASK_CMD));
  504. default_builtin_cache.insert("ui_text_caret_document_end", inputs);
  505. inputs = List<Ref<InputEvent>>();
  506. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD));
  507. default_builtin_cache.insert("ui_text_caret_document_end.OSX", inputs);
  508. // Text Scrolling
  509. inputs = List<Ref<InputEvent>>();
  510. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD));
  511. default_builtin_cache.insert("ui_text_scroll_up", inputs);
  512. inputs = List<Ref<InputEvent>>();
  513. inputs.push_back(InputEventKey::create_reference(KEY_UP | KEY_MASK_CMD | KEY_MASK_ALT));
  514. default_builtin_cache.insert("ui_text_scroll_up.OSX", inputs);
  515. inputs = List<Ref<InputEvent>>();
  516. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD));
  517. default_builtin_cache.insert("ui_text_scroll_down", inputs);
  518. inputs = List<Ref<InputEvent>>();
  519. inputs.push_back(InputEventKey::create_reference(KEY_DOWN | KEY_MASK_CMD | KEY_MASK_ALT));
  520. default_builtin_cache.insert("ui_text_scroll_down.OSX", inputs);
  521. // Text Misc
  522. inputs = List<Ref<InputEvent>>();
  523. inputs.push_back(InputEventKey::create_reference(KEY_A | KEY_MASK_CMD));
  524. default_builtin_cache.insert("ui_text_select_all", inputs);
  525. inputs = List<Ref<InputEvent>>();
  526. inputs.push_back(InputEventKey::create_reference(KEY_INSERT));
  527. default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs);
  528. inputs = List<Ref<InputEvent>>();
  529. inputs.push_back(InputEventKey::create_reference(KEY_MENU));
  530. default_builtin_cache.insert("ui_menu", inputs);
  531. // ///// UI Graph Shortcuts /////
  532. inputs = List<Ref<InputEvent>>();
  533. inputs.push_back(InputEventKey::create_reference(KEY_D | KEY_MASK_CMD));
  534. default_builtin_cache.insert("ui_graph_duplicate", inputs);
  535. inputs = List<Ref<InputEvent>>();
  536. inputs.push_back(InputEventKey::create_reference(KEY_DELETE));
  537. default_builtin_cache.insert("ui_graph_delete", inputs);
  538. // ///// UI File Dialog Shortcuts /////
  539. inputs = List<Ref<InputEvent>>();
  540. inputs.push_back(InputEventKey::create_reference(KEY_BACKSPACE));
  541. default_builtin_cache.insert("ui_filedialog_up_one_level", inputs);
  542. inputs = List<Ref<InputEvent>>();
  543. inputs.push_back(InputEventKey::create_reference(KEY_F5));
  544. default_builtin_cache.insert("ui_filedialog_refresh", inputs);
  545. inputs = List<Ref<InputEvent>>();
  546. inputs.push_back(InputEventKey::create_reference(KEY_H));
  547. default_builtin_cache.insert("ui_filedialog_show_hidden", inputs);
  548. inputs = List<Ref<InputEvent>>();
  549. inputs.push_back(InputEventKey::create_reference(KEY_QUOTELEFT | KEY_MASK_CMD));
  550. default_builtin_cache.insert("ui_swap_input_direction", inputs);
  551. return default_builtin_cache;
  552. }
  553. void InputMap::load_default() {
  554. OrderedHashMap<String, List<Ref<InputEvent>>> builtins = get_builtins();
  555. // List of Builtins which have an override for OSX.
  556. Vector<String> osx_builtins;
  557. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  558. if (String(E.key()).ends_with(".OSX")) {
  559. // Strip .OSX from name: some_input_name.OSX -> some_input_name
  560. osx_builtins.push_back(String(E.key()).split(".")[0]);
  561. }
  562. }
  563. for (OrderedHashMap<String, List<Ref<InputEvent>>>::Element E = builtins.front(); E; E = E.next()) {
  564. String fullname = E.key();
  565. String name = fullname.split(".")[0];
  566. String override_for = fullname.split(".").size() > 1 ? fullname.split(".")[1] : "";
  567. #ifdef APPLE_STYLE_KEYS
  568. if (osx_builtins.has(name) && override_for != "OSX") {
  569. // Name has osx builtin but this particular one is for non-osx systems - so skip.
  570. continue;
  571. }
  572. #else
  573. if (override_for == "OSX") {
  574. // Override for OSX - not needed on non-osx platforms.
  575. continue;
  576. }
  577. #endif
  578. add_action(name);
  579. List<Ref<InputEvent>> inputs = E.get();
  580. for (List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
  581. Ref<InputEventKey> iek = I->get();
  582. // For the editor, only add keyboard actions.
  583. if (iek.is_valid()) {
  584. action_add_event(name, I->get());
  585. }
  586. }
  587. }
  588. }
  589. InputMap::InputMap() {
  590. ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist.");
  591. singleton = this;
  592. }