input_map.cpp 30 KB

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