input_map.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. /**************************************************************************/
  2. /* input_map.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "input_map.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. #include "core/variant/typed_array.h"
  36. InputMap *InputMap::singleton = nullptr;
  37. int InputMap::ALL_DEVICES = -1;
  38. void InputMap::_bind_methods() {
  39. ClassDB::bind_method(D_METHOD("has_action", "action"), &InputMap::has_action);
  40. ClassDB::bind_method(D_METHOD("get_actions"), &InputMap::_get_actions);
  41. ClassDB::bind_method(D_METHOD("add_action", "action", "deadzone"), &InputMap::add_action, DEFVAL(0.5f));
  42. ClassDB::bind_method(D_METHOD("erase_action", "action"), &InputMap::erase_action);
  43. ClassDB::bind_method(D_METHOD("action_set_deadzone", "action", "deadzone"), &InputMap::action_set_deadzone);
  44. ClassDB::bind_method(D_METHOD("action_get_deadzone", "action"), &InputMap::action_get_deadzone);
  45. ClassDB::bind_method(D_METHOD("action_add_event", "action", "event"), &InputMap::action_add_event);
  46. ClassDB::bind_method(D_METHOD("action_has_event", "action", "event"), &InputMap::action_has_event);
  47. ClassDB::bind_method(D_METHOD("action_erase_event", "action", "event"), &InputMap::action_erase_event);
  48. ClassDB::bind_method(D_METHOD("action_erase_events", "action"), &InputMap::action_erase_events);
  49. ClassDB::bind_method(D_METHOD("action_get_events", "action"), &InputMap::_action_get_events);
  50. ClassDB::bind_method(D_METHOD("event_is_action", "event", "action", "exact_match"), &InputMap::event_is_action, DEFVAL(false));
  51. ClassDB::bind_method(D_METHOD("load_from_project_settings"), &InputMap::load_from_project_settings);
  52. }
  53. /**
  54. * Returns an nonexistent action error message with a suggestion of the closest
  55. * matching action name (if possible).
  56. */
  57. String InputMap::suggest_actions(const StringName &p_action) const {
  58. List<StringName> actions = get_actions();
  59. StringName closest_action;
  60. float closest_similarity = 0.0;
  61. // Find the most action with the most similar name.
  62. for (const StringName &action : actions) {
  63. const float similarity = String(action).similarity(p_action);
  64. if (similarity > closest_similarity) {
  65. closest_action = action;
  66. closest_similarity = similarity;
  67. }
  68. }
  69. String error_message = vformat("The InputMap action \"%s\" doesn't exist.", p_action);
  70. if (closest_similarity >= 0.4) {
  71. // Only include a suggestion in the error message if it's similar enough.
  72. error_message += vformat(" Did you mean \"%s\"?", closest_action);
  73. }
  74. return error_message;
  75. }
  76. void InputMap::add_action(const StringName &p_action, float p_deadzone) {
  77. ERR_FAIL_COND_MSG(input_map.has(p_action), "InputMap already has action \"" + String(p_action) + "\".");
  78. input_map[p_action] = Action();
  79. static int last_id = 1;
  80. input_map[p_action].id = last_id;
  81. input_map[p_action].deadzone = p_deadzone;
  82. last_id++;
  83. }
  84. void InputMap::erase_action(const StringName &p_action) {
  85. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  86. input_map.erase(p_action);
  87. }
  88. TypedArray<StringName> InputMap::_get_actions() {
  89. TypedArray<StringName> ret;
  90. List<StringName> actions = get_actions();
  91. if (actions.is_empty()) {
  92. return ret;
  93. }
  94. for (const StringName &E : actions) {
  95. ret.push_back(E);
  96. }
  97. return ret;
  98. }
  99. List<StringName> InputMap::get_actions() const {
  100. List<StringName> actions = List<StringName>();
  101. if (input_map.is_empty()) {
  102. return actions;
  103. }
  104. for (const KeyValue<StringName, Action> &E : input_map) {
  105. actions.push_back(E.key);
  106. }
  107. return actions;
  108. }
  109. List<Ref<InputEvent>>::Element *InputMap::_find_event(Action &p_action, const Ref<InputEvent> &p_event, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const {
  110. ERR_FAIL_COND_V(!p_event.is_valid(), nullptr);
  111. int i = 0;
  112. for (List<Ref<InputEvent>>::Element *E = p_action.inputs.front(); E; E = E->next()) {
  113. int device = E->get()->get_device();
  114. if (device == ALL_DEVICES || device == p_event->get_device()) {
  115. if (E->get()->action_match(p_event, p_exact_match, p_action.deadzone, r_pressed, r_strength, r_raw_strength)) {
  116. if (r_event_index) {
  117. *r_event_index = i;
  118. }
  119. return E;
  120. }
  121. }
  122. i++;
  123. }
  124. return nullptr;
  125. }
  126. bool InputMap::has_action(const StringName &p_action) const {
  127. return input_map.has(p_action);
  128. }
  129. float InputMap::action_get_deadzone(const StringName &p_action) {
  130. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), 0.0f, suggest_actions(p_action));
  131. return input_map[p_action].deadzone;
  132. }
  133. void InputMap::action_set_deadzone(const StringName &p_action, float p_deadzone) {
  134. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  135. input_map[p_action].deadzone = p_deadzone;
  136. }
  137. void InputMap::action_add_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  138. ERR_FAIL_COND_MSG(p_event.is_null(), "It's not a reference to a valid InputEvent object.");
  139. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  140. if (_find_event(input_map[p_action], p_event, true)) {
  141. return; // Already added.
  142. }
  143. input_map[p_action].inputs.push_back(p_event);
  144. }
  145. bool InputMap::action_has_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  146. ERR_FAIL_COND_V_MSG(!input_map.has(p_action), false, suggest_actions(p_action));
  147. return (_find_event(input_map[p_action], p_event, true) != nullptr);
  148. }
  149. void InputMap::action_erase_event(const StringName &p_action, const Ref<InputEvent> &p_event) {
  150. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  151. List<Ref<InputEvent>>::Element *E = _find_event(input_map[p_action], p_event, true);
  152. if (E) {
  153. input_map[p_action].inputs.erase(E);
  154. if (Input::get_singleton()->is_action_pressed(p_action)) {
  155. Input::get_singleton()->action_release(p_action);
  156. }
  157. }
  158. }
  159. void InputMap::action_erase_events(const StringName &p_action) {
  160. ERR_FAIL_COND_MSG(!input_map.has(p_action), suggest_actions(p_action));
  161. input_map[p_action].inputs.clear();
  162. }
  163. TypedArray<InputEvent> InputMap::_action_get_events(const StringName &p_action) {
  164. TypedArray<InputEvent> ret;
  165. const List<Ref<InputEvent>> *al = action_get_events(p_action);
  166. if (al) {
  167. for (const List<Ref<InputEvent>>::Element *E = al->front(); E; E = E->next()) {
  168. ret.push_back(E->get());
  169. }
  170. }
  171. return ret;
  172. }
  173. const List<Ref<InputEvent>> *InputMap::action_get_events(const StringName &p_action) {
  174. HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
  175. if (!E) {
  176. return nullptr;
  177. }
  178. return &E->value.inputs;
  179. }
  180. bool InputMap::event_is_action(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
  181. return event_get_action_status(p_event, p_action, p_exact_match);
  182. }
  183. int InputMap::event_get_index(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match) const {
  184. int index = -1;
  185. event_get_action_status(p_event, p_action, p_exact_match, nullptr, nullptr, nullptr, &index);
  186. return index;
  187. }
  188. bool InputMap::event_get_action_status(const Ref<InputEvent> &p_event, const StringName &p_action, bool p_exact_match, bool *r_pressed, float *r_strength, float *r_raw_strength, int *r_event_index) const {
  189. HashMap<StringName, Action>::Iterator E = input_map.find(p_action);
  190. ERR_FAIL_COND_V_MSG(!E, false, suggest_actions(p_action));
  191. Ref<InputEventAction> input_event_action = p_event;
  192. if (input_event_action.is_valid()) {
  193. const bool pressed = input_event_action->is_pressed();
  194. if (r_pressed != nullptr) {
  195. *r_pressed = pressed;
  196. }
  197. const float strength = pressed ? input_event_action->get_strength() : 0.0f;
  198. if (r_strength != nullptr) {
  199. *r_strength = strength;
  200. }
  201. if (r_raw_strength != nullptr) {
  202. *r_raw_strength = strength;
  203. }
  204. return input_event_action->get_action() == p_action;
  205. }
  206. List<Ref<InputEvent>>::Element *event = _find_event(E->value, p_event, p_exact_match, r_pressed, r_strength, r_raw_strength, r_event_index);
  207. return event != nullptr;
  208. }
  209. const HashMap<StringName, InputMap::Action> &InputMap::get_action_map() const {
  210. return input_map;
  211. }
  212. void InputMap::load_from_project_settings() {
  213. input_map.clear();
  214. List<PropertyInfo> pinfo;
  215. ProjectSettings::get_singleton()->get_property_list(&pinfo);
  216. for (const PropertyInfo &pi : pinfo) {
  217. if (!pi.name.begins_with("input/")) {
  218. continue;
  219. }
  220. String name = pi.name.substr(pi.name.find("/") + 1, pi.name.length());
  221. Dictionary action = GLOBAL_GET(pi.name);
  222. float deadzone = action.has("deadzone") ? (float)action["deadzone"] : 0.5f;
  223. Array events = action["events"];
  224. add_action(name, deadzone);
  225. for (int i = 0; i < events.size(); i++) {
  226. Ref<InputEvent> event = events[i];
  227. if (event.is_null()) {
  228. continue;
  229. }
  230. action_add_event(name, event);
  231. }
  232. }
  233. }
  234. struct _BuiltinActionDisplayName {
  235. const char *name;
  236. const char *display_name;
  237. };
  238. static const _BuiltinActionDisplayName _builtin_action_display_names[] = {
  239. /* clang-format off */
  240. { "ui_accept", TTRC("Accept") },
  241. { "ui_select", TTRC("Select") },
  242. { "ui_cancel", TTRC("Cancel") },
  243. { "ui_focus_next", TTRC("Focus Next") },
  244. { "ui_focus_prev", TTRC("Focus Prev") },
  245. { "ui_left", TTRC("Left") },
  246. { "ui_right", TTRC("Right") },
  247. { "ui_up", TTRC("Up") },
  248. { "ui_down", TTRC("Down") },
  249. { "ui_page_up", TTRC("Page Up") },
  250. { "ui_page_down", TTRC("Page Down") },
  251. { "ui_home", TTRC("Home") },
  252. { "ui_end", TTRC("End") },
  253. { "ui_cut", TTRC("Cut") },
  254. { "ui_copy", TTRC("Copy") },
  255. { "ui_paste", TTRC("Paste") },
  256. { "ui_undo", TTRC("Undo") },
  257. { "ui_redo", TTRC("Redo") },
  258. { "ui_text_completion_query", TTRC("Completion Query") },
  259. { "ui_text_newline", TTRC("New Line") },
  260. { "ui_text_newline_blank", TTRC("New Blank Line") },
  261. { "ui_text_newline_above", TTRC("New Line Above") },
  262. { "ui_text_indent", TTRC("Indent") },
  263. { "ui_text_dedent", TTRC("Dedent") },
  264. { "ui_text_backspace", TTRC("Backspace") },
  265. { "ui_text_backspace_word", TTRC("Backspace Word") },
  266. { "ui_text_backspace_word.macos", TTRC("Backspace Word") },
  267. { "ui_text_backspace_all_to_left", TTRC("Backspace all to Left") },
  268. { "ui_text_backspace_all_to_left.macos", TTRC("Backspace all to Left") },
  269. { "ui_text_delete", TTRC("Delete") },
  270. { "ui_text_delete_word", TTRC("Delete Word") },
  271. { "ui_text_delete_word.macos", TTRC("Delete Word") },
  272. { "ui_text_delete_all_to_right", TTRC("Delete all to Right") },
  273. { "ui_text_delete_all_to_right.macos", TTRC("Delete all to Right") },
  274. { "ui_text_caret_left", TTRC("Caret Left") },
  275. { "ui_text_caret_word_left", TTRC("Caret Word Left") },
  276. { "ui_text_caret_word_left.macos", TTRC("Caret Word Left") },
  277. { "ui_text_caret_right", TTRC("Caret Right") },
  278. { "ui_text_caret_word_right", TTRC("Caret Word Right") },
  279. { "ui_text_caret_word_right.macos", TTRC("Caret Word Right") },
  280. { "ui_text_caret_up", TTRC("Caret Up") },
  281. { "ui_text_caret_down", TTRC("Caret Down") },
  282. { "ui_text_caret_line_start", TTRC("Caret Line Start") },
  283. { "ui_text_caret_line_start.macos", TTRC("Caret Line Start") },
  284. { "ui_text_caret_line_end", TTRC("Caret Line End") },
  285. { "ui_text_caret_line_end.macos", TTRC("Caret Line End") },
  286. { "ui_text_caret_page_up", TTRC("Caret Page Up") },
  287. { "ui_text_caret_page_down", TTRC("Caret Page Down") },
  288. { "ui_text_caret_document_start", TTRC("Caret Document Start") },
  289. { "ui_text_caret_document_start.macos", TTRC("Caret Document Start") },
  290. { "ui_text_caret_document_end", TTRC("Caret Document End") },
  291. { "ui_text_caret_document_end.macos", TTRC("Caret Document End") },
  292. { "ui_text_caret_add_below", TTRC("Caret Add Below") },
  293. { "ui_text_caret_add_below.macos", TTRC("Caret Add Below") },
  294. { "ui_text_caret_add_above", TTRC("Caret Add Above") },
  295. { "ui_text_caret_add_above.macos", TTRC("Caret Add Above") },
  296. { "ui_text_scroll_up", TTRC("Scroll Up") },
  297. { "ui_text_scroll_up.macos", TTRC("Scroll Up") },
  298. { "ui_text_scroll_down", TTRC("Scroll Down") },
  299. { "ui_text_scroll_down.macos", TTRC("Scroll Down") },
  300. { "ui_text_select_all", TTRC("Select All") },
  301. { "ui_text_select_word_under_caret", TTRC("Select Word Under Caret") },
  302. { "ui_text_add_selection_for_next_occurrence", TTRC("Add Selection for Next Occurrence") },
  303. { "ui_text_clear_carets_and_selection", TTRC("Clear Carets and Selection") },
  304. { "ui_text_toggle_insert_mode", TTRC("Toggle Insert Mode") },
  305. { "ui_text_submit", TTRC("Submit Text") },
  306. { "ui_graph_duplicate", TTRC("Duplicate Nodes") },
  307. { "ui_graph_delete", TTRC("Delete Nodes") },
  308. { "ui_filedialog_up_one_level", TTRC("Go Up One Level") },
  309. { "ui_filedialog_refresh", TTRC("Refresh") },
  310. { "ui_filedialog_show_hidden", TTRC("Show Hidden") },
  311. { "ui_swap_input_direction ", TTRC("Swap Input Direction") },
  312. { "", ""}
  313. /* clang-format on */
  314. };
  315. String InputMap::get_builtin_display_name(const String &p_name) const {
  316. int len = sizeof(_builtin_action_display_names) / sizeof(_BuiltinActionDisplayName);
  317. for (int i = 0; i < len; i++) {
  318. if (_builtin_action_display_names[i].name == p_name) {
  319. return RTR(_builtin_action_display_names[i].display_name);
  320. }
  321. }
  322. return p_name;
  323. }
  324. const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins() {
  325. // Return cache if it has already been built.
  326. if (default_builtin_cache.size()) {
  327. return default_builtin_cache;
  328. }
  329. List<Ref<InputEvent>> inputs;
  330. inputs.push_back(InputEventKey::create_reference(Key::ENTER));
  331. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
  332. inputs.push_back(InputEventKey::create_reference(Key::SPACE));
  333. default_builtin_cache.insert("ui_accept", inputs);
  334. inputs = List<Ref<InputEvent>>();
  335. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::Y));
  336. inputs.push_back(InputEventKey::create_reference(Key::SPACE));
  337. default_builtin_cache.insert("ui_select", inputs);
  338. inputs = List<Ref<InputEvent>>();
  339. inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
  340. default_builtin_cache.insert("ui_cancel", inputs);
  341. inputs = List<Ref<InputEvent>>();
  342. inputs.push_back(InputEventKey::create_reference(Key::TAB));
  343. default_builtin_cache.insert("ui_focus_next", inputs);
  344. inputs = List<Ref<InputEvent>>();
  345. inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
  346. default_builtin_cache.insert("ui_focus_prev", inputs);
  347. inputs = List<Ref<InputEvent>>();
  348. inputs.push_back(InputEventKey::create_reference(Key::LEFT));
  349. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_LEFT));
  350. inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, -1.0));
  351. default_builtin_cache.insert("ui_left", inputs);
  352. inputs = List<Ref<InputEvent>>();
  353. inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
  354. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_RIGHT));
  355. inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_X, 1.0));
  356. default_builtin_cache.insert("ui_right", inputs);
  357. inputs = List<Ref<InputEvent>>();
  358. inputs.push_back(InputEventKey::create_reference(Key::UP));
  359. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_UP));
  360. inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, -1.0));
  361. default_builtin_cache.insert("ui_up", inputs);
  362. inputs = List<Ref<InputEvent>>();
  363. inputs.push_back(InputEventKey::create_reference(Key::DOWN));
  364. inputs.push_back(InputEventJoypadButton::create_reference(JoyButton::DPAD_DOWN));
  365. inputs.push_back(InputEventJoypadMotion::create_reference(JoyAxis::LEFT_Y, 1.0));
  366. default_builtin_cache.insert("ui_down", inputs);
  367. inputs = List<Ref<InputEvent>>();
  368. inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
  369. default_builtin_cache.insert("ui_page_up", inputs);
  370. inputs = List<Ref<InputEvent>>();
  371. inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
  372. default_builtin_cache.insert("ui_page_down", inputs);
  373. inputs = List<Ref<InputEvent>>();
  374. inputs.push_back(InputEventKey::create_reference(Key::HOME));
  375. default_builtin_cache.insert("ui_home", inputs);
  376. inputs = List<Ref<InputEvent>>();
  377. inputs.push_back(InputEventKey::create_reference(Key::END));
  378. default_builtin_cache.insert("ui_end", inputs);
  379. // ///// UI basic Shortcuts /////
  380. inputs = List<Ref<InputEvent>>();
  381. inputs.push_back(InputEventKey::create_reference(Key::X | KeyModifierMask::CMD_OR_CTRL));
  382. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::SHIFT));
  383. default_builtin_cache.insert("ui_cut", inputs);
  384. inputs = List<Ref<InputEvent>>();
  385. inputs.push_back(InputEventKey::create_reference(Key::C | KeyModifierMask::CMD_OR_CTRL));
  386. inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::CMD_OR_CTRL));
  387. default_builtin_cache.insert("ui_copy", inputs);
  388. inputs = List<Ref<InputEvent>>();
  389. inputs.push_back(InputEventKey::create_reference(Key::V | KeyModifierMask::CMD_OR_CTRL));
  390. inputs.push_back(InputEventKey::create_reference(Key::INSERT | KeyModifierMask::SHIFT));
  391. default_builtin_cache.insert("ui_paste", inputs);
  392. inputs = List<Ref<InputEvent>>();
  393. inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL));
  394. default_builtin_cache.insert("ui_undo", inputs);
  395. inputs = List<Ref<InputEvent>>();
  396. inputs.push_back(InputEventKey::create_reference(Key::Z | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT));
  397. inputs.push_back(InputEventKey::create_reference(Key::Y | KeyModifierMask::CMD_OR_CTRL));
  398. default_builtin_cache.insert("ui_redo", inputs);
  399. // ///// UI Text Input Shortcuts /////
  400. inputs = List<Ref<InputEvent>>();
  401. inputs.push_back(InputEventKey::create_reference(Key::SPACE | KeyModifierMask::CTRL));
  402. default_builtin_cache.insert("ui_text_completion_query", inputs);
  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_completion_accept", inputs);
  407. inputs = List<Ref<InputEvent>>();
  408. inputs.push_back(InputEventKey::create_reference(Key::TAB));
  409. default_builtin_cache.insert("ui_text_completion_replace", inputs);
  410. // Newlines
  411. inputs = List<Ref<InputEvent>>();
  412. inputs.push_back(InputEventKey::create_reference(Key::ENTER));
  413. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
  414. default_builtin_cache.insert("ui_text_newline", inputs);
  415. inputs = List<Ref<InputEvent>>();
  416. inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::CMD_OR_CTRL));
  417. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::CMD_OR_CTRL));
  418. default_builtin_cache.insert("ui_text_newline_blank", inputs);
  419. inputs = List<Ref<InputEvent>>();
  420. inputs.push_back(InputEventKey::create_reference(Key::ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
  421. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
  422. default_builtin_cache.insert("ui_text_newline_above", inputs);
  423. // Indentation
  424. inputs = List<Ref<InputEvent>>();
  425. inputs.push_back(InputEventKey::create_reference(Key::TAB));
  426. default_builtin_cache.insert("ui_text_indent", inputs);
  427. inputs = List<Ref<InputEvent>>();
  428. inputs.push_back(InputEventKey::create_reference(Key::TAB | KeyModifierMask::SHIFT));
  429. default_builtin_cache.insert("ui_text_dedent", inputs);
  430. // Text Backspace and Delete
  431. inputs = List<Ref<InputEvent>>();
  432. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
  433. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::SHIFT));
  434. default_builtin_cache.insert("ui_text_backspace", inputs);
  435. inputs = List<Ref<InputEvent>>();
  436. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL));
  437. default_builtin_cache.insert("ui_text_backspace_word", inputs);
  438. inputs = List<Ref<InputEvent>>();
  439. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::ALT));
  440. default_builtin_cache.insert("ui_text_backspace_word.macos", inputs);
  441. inputs = List<Ref<InputEvent>>();
  442. default_builtin_cache.insert("ui_text_backspace_all_to_left", inputs);
  443. inputs = List<Ref<InputEvent>>();
  444. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE | KeyModifierMask::CMD_OR_CTRL));
  445. default_builtin_cache.insert("ui_text_backspace_all_to_left.macos", inputs);
  446. inputs = List<Ref<InputEvent>>();
  447. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
  448. default_builtin_cache.insert("ui_text_delete", inputs);
  449. inputs = List<Ref<InputEvent>>();
  450. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL));
  451. default_builtin_cache.insert("ui_text_delete_word", inputs);
  452. inputs = List<Ref<InputEvent>>();
  453. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::ALT));
  454. default_builtin_cache.insert("ui_text_delete_word.macos", inputs);
  455. inputs = List<Ref<InputEvent>>();
  456. default_builtin_cache.insert("ui_text_delete_all_to_right", inputs);
  457. inputs = List<Ref<InputEvent>>();
  458. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE | KeyModifierMask::CMD_OR_CTRL));
  459. default_builtin_cache.insert("ui_text_delete_all_to_right.macos", inputs);
  460. // Text Caret Movement Left/Right
  461. inputs = List<Ref<InputEvent>>();
  462. inputs.push_back(InputEventKey::create_reference(Key::LEFT));
  463. default_builtin_cache.insert("ui_text_caret_left", inputs);
  464. inputs = List<Ref<InputEvent>>();
  465. inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
  466. default_builtin_cache.insert("ui_text_caret_word_left", inputs);
  467. inputs = List<Ref<InputEvent>>();
  468. inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::ALT));
  469. default_builtin_cache.insert("ui_text_caret_word_left.macos", inputs);
  470. inputs = List<Ref<InputEvent>>();
  471. inputs.push_back(InputEventKey::create_reference(Key::RIGHT));
  472. default_builtin_cache.insert("ui_text_caret_right", inputs);
  473. inputs = List<Ref<InputEvent>>();
  474. inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
  475. default_builtin_cache.insert("ui_text_caret_word_right", inputs);
  476. inputs = List<Ref<InputEvent>>();
  477. inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::ALT));
  478. default_builtin_cache.insert("ui_text_caret_word_right.macos", inputs);
  479. // Text Caret Movement Up/Down
  480. inputs = List<Ref<InputEvent>>();
  481. inputs.push_back(InputEventKey::create_reference(Key::UP));
  482. default_builtin_cache.insert("ui_text_caret_up", inputs);
  483. inputs = List<Ref<InputEvent>>();
  484. inputs.push_back(InputEventKey::create_reference(Key::DOWN));
  485. default_builtin_cache.insert("ui_text_caret_down", inputs);
  486. // Text Caret Movement Line Start/End
  487. inputs = List<Ref<InputEvent>>();
  488. inputs.push_back(InputEventKey::create_reference(Key::HOME));
  489. default_builtin_cache.insert("ui_text_caret_line_start", inputs);
  490. inputs = List<Ref<InputEvent>>();
  491. inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CTRL));
  492. inputs.push_back(InputEventKey::create_reference(Key::LEFT | KeyModifierMask::CMD_OR_CTRL));
  493. default_builtin_cache.insert("ui_text_caret_line_start.macos", inputs);
  494. inputs = List<Ref<InputEvent>>();
  495. inputs.push_back(InputEventKey::create_reference(Key::END));
  496. default_builtin_cache.insert("ui_text_caret_line_end", inputs);
  497. inputs = List<Ref<InputEvent>>();
  498. inputs.push_back(InputEventKey::create_reference(Key::E | KeyModifierMask::CTRL));
  499. inputs.push_back(InputEventKey::create_reference(Key::RIGHT | KeyModifierMask::CMD_OR_CTRL));
  500. default_builtin_cache.insert("ui_text_caret_line_end.macos", inputs);
  501. // Text Caret Movement Page Up/Down
  502. inputs = List<Ref<InputEvent>>();
  503. inputs.push_back(InputEventKey::create_reference(Key::PAGEUP));
  504. default_builtin_cache.insert("ui_text_caret_page_up", inputs);
  505. inputs = List<Ref<InputEvent>>();
  506. inputs.push_back(InputEventKey::create_reference(Key::PAGEDOWN));
  507. default_builtin_cache.insert("ui_text_caret_page_down", inputs);
  508. // Text Caret Movement Document Start/End
  509. inputs = List<Ref<InputEvent>>();
  510. inputs.push_back(InputEventKey::create_reference(Key::HOME | KeyModifierMask::CMD_OR_CTRL));
  511. default_builtin_cache.insert("ui_text_caret_document_start", inputs);
  512. inputs = List<Ref<InputEvent>>();
  513. inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL));
  514. default_builtin_cache.insert("ui_text_caret_document_start.macos", inputs);
  515. inputs = List<Ref<InputEvent>>();
  516. inputs.push_back(InputEventKey::create_reference(Key::END | KeyModifierMask::CMD_OR_CTRL));
  517. default_builtin_cache.insert("ui_text_caret_document_end", inputs);
  518. inputs = List<Ref<InputEvent>>();
  519. inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL));
  520. default_builtin_cache.insert("ui_text_caret_document_end.macos", inputs);
  521. // Text Caret Addition Below/Above
  522. inputs = List<Ref<InputEvent>>();
  523. inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
  524. default_builtin_cache.insert("ui_text_caret_add_below", inputs);
  525. inputs = List<Ref<InputEvent>>();
  526. inputs.push_back(InputEventKey::create_reference(Key::L | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
  527. default_builtin_cache.insert("ui_text_caret_add_below.macos", inputs);
  528. inputs = List<Ref<InputEvent>>();
  529. inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
  530. default_builtin_cache.insert("ui_text_caret_add_above", inputs);
  531. inputs = List<Ref<InputEvent>>();
  532. inputs.push_back(InputEventKey::create_reference(Key::O | KeyModifierMask::SHIFT | KeyModifierMask::CMD_OR_CTRL));
  533. default_builtin_cache.insert("ui_text_caret_add_above.macos", inputs);
  534. // Text Scrolling
  535. inputs = List<Ref<InputEvent>>();
  536. inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL));
  537. default_builtin_cache.insert("ui_text_scroll_up", inputs);
  538. inputs = List<Ref<InputEvent>>();
  539. inputs.push_back(InputEventKey::create_reference(Key::UP | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
  540. default_builtin_cache.insert("ui_text_scroll_up.macos", inputs);
  541. inputs = List<Ref<InputEvent>>();
  542. inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL));
  543. default_builtin_cache.insert("ui_text_scroll_down", inputs);
  544. inputs = List<Ref<InputEvent>>();
  545. inputs.push_back(InputEventKey::create_reference(Key::DOWN | KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT));
  546. default_builtin_cache.insert("ui_text_scroll_down.macos", inputs);
  547. // Text Misc
  548. inputs = List<Ref<InputEvent>>();
  549. inputs.push_back(InputEventKey::create_reference(Key::A | KeyModifierMask::CMD_OR_CTRL));
  550. default_builtin_cache.insert("ui_text_select_all", inputs);
  551. inputs = List<Ref<InputEvent>>();
  552. inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::ALT));
  553. default_builtin_cache.insert("ui_text_select_word_under_caret", inputs);
  554. inputs = List<Ref<InputEvent>>();
  555. inputs.push_back(InputEventKey::create_reference(Key::G | KeyModifierMask::CTRL | KeyModifierMask::META));
  556. default_builtin_cache.insert("ui_text_select_word_under_caret.macos", inputs);
  557. inputs = List<Ref<InputEvent>>();
  558. inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL));
  559. default_builtin_cache.insert("ui_text_add_selection_for_next_occurrence", inputs);
  560. inputs = List<Ref<InputEvent>>();
  561. inputs.push_back(InputEventKey::create_reference(Key::ESCAPE));
  562. default_builtin_cache.insert("ui_text_clear_carets_and_selection", inputs);
  563. inputs = List<Ref<InputEvent>>();
  564. inputs.push_back(InputEventKey::create_reference(Key::INSERT));
  565. default_builtin_cache.insert("ui_text_toggle_insert_mode", inputs);
  566. inputs = List<Ref<InputEvent>>();
  567. inputs.push_back(InputEventKey::create_reference(Key::MENU));
  568. default_builtin_cache.insert("ui_menu", inputs);
  569. inputs = List<Ref<InputEvent>>();
  570. inputs.push_back(InputEventKey::create_reference(Key::ENTER));
  571. inputs.push_back(InputEventKey::create_reference(Key::KP_ENTER));
  572. default_builtin_cache.insert("ui_text_submit", inputs);
  573. // ///// UI Graph Shortcuts /////
  574. inputs = List<Ref<InputEvent>>();
  575. inputs.push_back(InputEventKey::create_reference(Key::D | KeyModifierMask::CMD_OR_CTRL));
  576. default_builtin_cache.insert("ui_graph_duplicate", inputs);
  577. inputs = List<Ref<InputEvent>>();
  578. inputs.push_back(InputEventKey::create_reference(Key::KEY_DELETE));
  579. default_builtin_cache.insert("ui_graph_delete", inputs);
  580. // ///// UI File Dialog Shortcuts /////
  581. inputs = List<Ref<InputEvent>>();
  582. inputs.push_back(InputEventKey::create_reference(Key::BACKSPACE));
  583. default_builtin_cache.insert("ui_filedialog_up_one_level", inputs);
  584. inputs = List<Ref<InputEvent>>();
  585. inputs.push_back(InputEventKey::create_reference(Key::F5));
  586. default_builtin_cache.insert("ui_filedialog_refresh", inputs);
  587. inputs = List<Ref<InputEvent>>();
  588. inputs.push_back(InputEventKey::create_reference(Key::H));
  589. default_builtin_cache.insert("ui_filedialog_show_hidden", inputs);
  590. inputs = List<Ref<InputEvent>>();
  591. inputs.push_back(InputEventKey::create_reference(Key::QUOTELEFT | KeyModifierMask::CMD_OR_CTRL));
  592. default_builtin_cache.insert("ui_swap_input_direction", inputs);
  593. return default_builtin_cache;
  594. }
  595. const HashMap<String, List<Ref<InputEvent>>> &InputMap::get_builtins_with_feature_overrides_applied() {
  596. if (default_builtin_with_overrides_cache.size() > 0) {
  597. return default_builtin_with_overrides_cache;
  598. }
  599. const HashMap<String, List<Ref<InputEvent>>> &builtins = get_builtins();
  600. // Get a list of all built in inputs which are valid overrides for the OS
  601. // Key = builtin name (e.g. ui_accept)
  602. // Value = override/feature names (e.g. macos, if it was defined as "ui_accept.macos" and the platform supports that feature)
  603. HashMap<String, Vector<String>> builtins_with_overrides;
  604. for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
  605. String fullname = E.key;
  606. Vector<String> split = fullname.split(".");
  607. String name = split[0];
  608. String override_for = split.size() > 1 ? split[1] : String();
  609. if (!override_for.is_empty() && OS::get_singleton()->has_feature(override_for)) {
  610. builtins_with_overrides[name].push_back(override_for);
  611. }
  612. }
  613. for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
  614. String fullname = E.key;
  615. Vector<String> split = fullname.split(".");
  616. String name = split[0];
  617. String override_for = split.size() > 1 ? split[1] : String();
  618. if (builtins_with_overrides.has(name) && override_for.is_empty()) {
  619. // Builtin has an override but this particular one is not an override, so skip.
  620. continue;
  621. }
  622. if (!override_for.is_empty() && !OS::get_singleton()->has_feature(override_for)) {
  623. // OS does not support this override - skip.
  624. continue;
  625. }
  626. default_builtin_with_overrides_cache.insert(name, E.value);
  627. }
  628. return default_builtin_with_overrides_cache;
  629. }
  630. void InputMap::load_default() {
  631. HashMap<String, List<Ref<InputEvent>>> builtins = get_builtins_with_feature_overrides_applied();
  632. for (const KeyValue<String, List<Ref<InputEvent>>> &E : builtins) {
  633. String name = E.key;
  634. add_action(name);
  635. const List<Ref<InputEvent>> &inputs = E.value;
  636. for (const List<Ref<InputEvent>>::Element *I = inputs.front(); I; I = I->next()) {
  637. Ref<InputEventKey> iek = I->get();
  638. // For the editor, only add keyboard actions.
  639. if (iek.is_valid()) {
  640. action_add_event(name, I->get());
  641. }
  642. }
  643. }
  644. }
  645. InputMap::InputMap() {
  646. ERR_FAIL_COND_MSG(singleton, "Singleton in InputMap already exist.");
  647. singleton = this;
  648. }
  649. InputMap::~InputMap() {
  650. singleton = nullptr;
  651. }