input_map.cpp 38 KB

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