input_event_configuration_dialog.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /**************************************************************************/
  2. /* input_event_configuration_dialog.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 "editor/input_event_configuration_dialog.h"
  31. #include "core/input/input_map.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/event_listener_line_edit.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/check_box.h"
  36. #include "scene/gui/line_edit.h"
  37. #include "scene/gui/option_button.h"
  38. #include "scene/gui/separator.h"
  39. #include "scene/gui/tree.h"
  40. void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event, const Ref<InputEvent> &p_original_event, bool p_update_input_list_selection) {
  41. if (p_event.is_valid()) {
  42. event = p_event;
  43. original_event = p_original_event;
  44. // If the event is changed to something which is not the same as the listener,
  45. // clear out the event from the listener text box to avoid confusion.
  46. const Ref<InputEvent> listener_event = event_listener->get_event();
  47. if (listener_event.is_valid() && !listener_event->is_match(p_event)) {
  48. event_listener->clear_event();
  49. }
  50. // Update Label
  51. event_as_text->set_text(EventListenerLineEdit::get_event_text(event, true));
  52. Ref<InputEventKey> k = p_event;
  53. Ref<InputEventMouseButton> mb = p_event;
  54. Ref<InputEventJoypadButton> joyb = p_event;
  55. Ref<InputEventJoypadMotion> joym = p_event;
  56. Ref<InputEventWithModifiers> mod = p_event;
  57. // Update option values and visibility
  58. bool show_mods = false;
  59. bool show_device = false;
  60. bool show_key = false;
  61. bool show_location = false;
  62. if (mod.is_valid()) {
  63. show_mods = true;
  64. mod_checkboxes[MOD_ALT]->set_pressed(mod->is_alt_pressed());
  65. mod_checkboxes[MOD_SHIFT]->set_pressed(mod->is_shift_pressed());
  66. mod_checkboxes[MOD_CTRL]->set_pressed(mod->is_ctrl_pressed());
  67. mod_checkboxes[MOD_META]->set_pressed(mod->is_meta_pressed());
  68. autoremap_command_or_control_checkbox->set_pressed(mod->is_command_or_control_autoremap());
  69. }
  70. if (k.is_valid()) {
  71. show_key = true;
  72. Key phys_key = k->get_physical_keycode();
  73. if (k->get_keycode() == Key::NONE && phys_key == Key::NONE && k->get_key_label() != Key::NONE) {
  74. key_mode->select(KEYMODE_UNICODE);
  75. } else if (k->get_keycode() != Key::NONE) {
  76. key_mode->select(KEYMODE_KEYCODE);
  77. } else if (phys_key != Key::NONE) {
  78. key_mode->select(KEYMODE_PHY_KEYCODE);
  79. if (phys_key == Key::SHIFT || phys_key == Key::CTRL || phys_key == Key::ALT || phys_key == Key::META) {
  80. key_location->select((int)k->get_location());
  81. show_location = true;
  82. }
  83. } else {
  84. // Invalid key.
  85. event = Ref<InputEvent>();
  86. original_event = Ref<InputEvent>();
  87. event_listener->clear_event();
  88. event_as_text->set_text(TTR("No Event Configured"));
  89. additional_options_container->hide();
  90. input_list_tree->deselect_all();
  91. _update_input_list();
  92. return;
  93. }
  94. } else if (joyb.is_valid() || joym.is_valid() || mb.is_valid()) {
  95. show_device = true;
  96. _set_current_device(event->get_device());
  97. }
  98. mod_container->set_visible(show_mods);
  99. device_container->set_visible(show_device);
  100. key_mode->set_visible(show_key);
  101. location_container->set_visible(show_location);
  102. additional_options_container->show();
  103. // Update mode selector based on original key event.
  104. Ref<InputEventKey> ko = p_original_event;
  105. if (ko.is_valid()) {
  106. if (ko->get_keycode() == Key::NONE) {
  107. if (ko->get_physical_keycode() != Key::NONE) {
  108. ko->set_keycode(ko->get_physical_keycode());
  109. }
  110. if (ko->get_key_label() != Key::NONE) {
  111. ko->set_keycode(fix_keycode((char32_t)ko->get_key_label(), Key::NONE));
  112. }
  113. }
  114. if (ko->get_physical_keycode() == Key::NONE) {
  115. if (ko->get_keycode() != Key::NONE) {
  116. ko->set_physical_keycode(ko->get_keycode());
  117. }
  118. if (ko->get_key_label() != Key::NONE) {
  119. ko->set_physical_keycode(fix_keycode((char32_t)ko->get_key_label(), Key::NONE));
  120. }
  121. }
  122. if (ko->get_key_label() == Key::NONE) {
  123. if (ko->get_keycode() != Key::NONE) {
  124. ko->set_key_label(fix_key_label((char32_t)ko->get_keycode(), Key::NONE));
  125. }
  126. if (ko->get_physical_keycode() != Key::NONE) {
  127. ko->set_key_label(fix_key_label((char32_t)ko->get_physical_keycode(), Key::NONE));
  128. }
  129. }
  130. key_mode->set_item_disabled(KEYMODE_KEYCODE, ko->get_keycode() == Key::NONE);
  131. key_mode->set_item_disabled(KEYMODE_PHY_KEYCODE, ko->get_physical_keycode() == Key::NONE);
  132. key_mode->set_item_disabled(KEYMODE_UNICODE, ko->get_key_label() == Key::NONE);
  133. }
  134. // Update selected item in input list.
  135. if (p_update_input_list_selection && (k.is_valid() || joyb.is_valid() || joym.is_valid() || mb.is_valid())) {
  136. in_tree_update = true;
  137. TreeItem *category = input_list_tree->get_root()->get_first_child();
  138. while (category) {
  139. TreeItem *input_item = category->get_first_child();
  140. if (input_item != nullptr) {
  141. // input_type should always be > 0, unless the tree structure has been misconfigured.
  142. int input_type = input_item->get_parent()->get_meta("__type", 0);
  143. if (input_type == 0) {
  144. in_tree_update = false;
  145. return;
  146. }
  147. // If event type matches input types of this category.
  148. if ((k.is_valid() && input_type == INPUT_KEY) || (joyb.is_valid() && input_type == INPUT_JOY_BUTTON) || (joym.is_valid() && input_type == INPUT_JOY_MOTION) || (mb.is_valid() && input_type == INPUT_MOUSE_BUTTON)) {
  149. // Loop through all items of this category until one matches.
  150. while (input_item) {
  151. bool key_match = k.is_valid() && (Variant(k->get_keycode()) == input_item->get_meta("__keycode") || Variant(k->get_physical_keycode()) == input_item->get_meta("__keycode"));
  152. bool joyb_match = joyb.is_valid() && Variant(joyb->get_button_index()) == input_item->get_meta("__index");
  153. bool joym_match = joym.is_valid() && Variant(joym->get_axis()) == input_item->get_meta("__axis") && joym->get_axis_value() == (float)input_item->get_meta("__value");
  154. bool mb_match = mb.is_valid() && Variant(mb->get_button_index()) == input_item->get_meta("__index");
  155. if (key_match || joyb_match || joym_match || mb_match) {
  156. category->set_collapsed(false);
  157. input_item->select(0);
  158. input_list_tree->ensure_cursor_is_visible();
  159. in_tree_update = false;
  160. return;
  161. }
  162. input_item = input_item->get_next();
  163. }
  164. }
  165. }
  166. category->set_collapsed(true); // Event not in this category, so collapse;
  167. category = category->get_next();
  168. }
  169. in_tree_update = false;
  170. }
  171. } else {
  172. // Event is not valid, reset dialog
  173. event = Ref<InputEvent>();
  174. original_event = Ref<InputEvent>();
  175. event_listener->clear_event();
  176. event_as_text->set_text(TTR("No Event Configured"));
  177. additional_options_container->hide();
  178. input_list_tree->deselect_all();
  179. _update_input_list();
  180. }
  181. }
  182. void InputEventConfigurationDialog::_on_listen_input_changed(const Ref<InputEvent> &p_event) {
  183. // Ignore if invalid, echo or not pressed
  184. if (p_event.is_null() || p_event->is_echo() || !p_event->is_pressed()) {
  185. return;
  186. }
  187. // Create an editable reference and a copy of full event.
  188. Ref<InputEvent> received_event = p_event;
  189. Ref<InputEvent> received_original_event = received_event->duplicate();
  190. // Check what the type is and if it is allowed.
  191. Ref<InputEventKey> k = received_event;
  192. Ref<InputEventJoypadButton> joyb = received_event;
  193. Ref<InputEventJoypadMotion> joym = received_event;
  194. Ref<InputEventMouseButton> mb = received_event;
  195. int type = 0;
  196. if (k.is_valid()) {
  197. type = INPUT_KEY;
  198. } else if (joyb.is_valid()) {
  199. type = INPUT_JOY_BUTTON;
  200. } else if (joym.is_valid()) {
  201. type = INPUT_JOY_MOTION;
  202. } else if (mb.is_valid()) {
  203. type = INPUT_MOUSE_BUTTON;
  204. }
  205. if (!(allowed_input_types & type)) {
  206. return;
  207. }
  208. if (joym.is_valid()) {
  209. joym->set_axis_value(SIGN(joym->get_axis_value()));
  210. }
  211. if (k.is_valid()) {
  212. k->set_pressed(false); // To avoid serialization of 'pressed' property - doesn't matter for actions anyway.
  213. if (key_mode->get_selected_id() == KEYMODE_KEYCODE) {
  214. k->set_physical_keycode(Key::NONE);
  215. k->set_key_label(Key::NONE);
  216. } else if (key_mode->get_selected_id() == KEYMODE_PHY_KEYCODE) {
  217. k->set_keycode(Key::NONE);
  218. k->set_key_label(Key::NONE);
  219. } else if (key_mode->get_selected_id() == KEYMODE_UNICODE) {
  220. k->set_physical_keycode(Key::NONE);
  221. k->set_keycode(Key::NONE);
  222. }
  223. if (key_location->get_selected_id() == (int)KeyLocation::UNSPECIFIED) {
  224. k->set_location(KeyLocation::UNSPECIFIED);
  225. }
  226. }
  227. Ref<InputEventWithModifiers> mod = received_event;
  228. if (mod.is_valid()) {
  229. mod->set_window_id(0);
  230. }
  231. // Maintain device selection.
  232. received_event->set_device(_get_current_device());
  233. _set_event(received_event, received_original_event);
  234. }
  235. void InputEventConfigurationDialog::_search_term_updated(const String &) {
  236. _update_input_list();
  237. }
  238. void InputEventConfigurationDialog::_update_input_list() {
  239. input_list_tree->clear();
  240. TreeItem *root = input_list_tree->create_item();
  241. String search_term = input_list_search->get_text();
  242. bool collapse = input_list_search->get_text().is_empty();
  243. if (allowed_input_types & INPUT_KEY) {
  244. TreeItem *kb_root = input_list_tree->create_item(root);
  245. kb_root->set_text(0, TTR("Keyboard Keys"));
  246. kb_root->set_icon(0, icon_cache.keyboard);
  247. kb_root->set_collapsed(collapse);
  248. kb_root->set_meta("__type", INPUT_KEY);
  249. for (int i = 0; i < keycode_get_count(); i++) {
  250. String name = keycode_get_name_by_index(i);
  251. if (!search_term.is_empty() && !name.containsn(search_term)) {
  252. continue;
  253. }
  254. TreeItem *item = input_list_tree->create_item(kb_root);
  255. item->set_text(0, name);
  256. item->set_meta("__keycode", keycode_get_value_by_index(i));
  257. }
  258. }
  259. if (allowed_input_types & INPUT_MOUSE_BUTTON) {
  260. TreeItem *mouse_root = input_list_tree->create_item(root);
  261. mouse_root->set_text(0, TTR("Mouse Buttons"));
  262. mouse_root->set_icon(0, icon_cache.mouse);
  263. mouse_root->set_collapsed(collapse);
  264. mouse_root->set_meta("__type", INPUT_MOUSE_BUTTON);
  265. MouseButton mouse_buttons[9] = { MouseButton::LEFT, MouseButton::RIGHT, MouseButton::MIDDLE, MouseButton::WHEEL_UP, MouseButton::WHEEL_DOWN, MouseButton::WHEEL_LEFT, MouseButton::WHEEL_RIGHT, MouseButton::MB_XBUTTON1, MouseButton::MB_XBUTTON2 };
  266. for (int i = 0; i < 9; i++) {
  267. Ref<InputEventMouseButton> mb;
  268. mb.instantiate();
  269. mb->set_button_index(mouse_buttons[i]);
  270. String desc = EventListenerLineEdit::get_event_text(mb, false);
  271. if (!search_term.is_empty() && !desc.containsn(search_term)) {
  272. continue;
  273. }
  274. TreeItem *item = input_list_tree->create_item(mouse_root);
  275. item->set_text(0, desc);
  276. item->set_meta("__index", mouse_buttons[i]);
  277. }
  278. }
  279. if (allowed_input_types & INPUT_JOY_BUTTON) {
  280. TreeItem *joyb_root = input_list_tree->create_item(root);
  281. joyb_root->set_text(0, TTR("Joypad Buttons"));
  282. joyb_root->set_icon(0, icon_cache.joypad_button);
  283. joyb_root->set_collapsed(collapse);
  284. joyb_root->set_meta("__type", INPUT_JOY_BUTTON);
  285. for (int i = 0; i < (int)JoyButton::MAX; i++) {
  286. Ref<InputEventJoypadButton> joyb;
  287. joyb.instantiate();
  288. joyb->set_button_index((JoyButton)i);
  289. String desc = EventListenerLineEdit::get_event_text(joyb, false);
  290. if (!search_term.is_empty() && !desc.containsn(search_term)) {
  291. continue;
  292. }
  293. TreeItem *item = input_list_tree->create_item(joyb_root);
  294. item->set_text(0, desc);
  295. item->set_meta("__index", i);
  296. }
  297. }
  298. if (allowed_input_types & INPUT_JOY_MOTION) {
  299. TreeItem *joya_root = input_list_tree->create_item(root);
  300. joya_root->set_text(0, TTR("Joypad Axes"));
  301. joya_root->set_icon(0, icon_cache.joypad_axis);
  302. joya_root->set_collapsed(collapse);
  303. joya_root->set_meta("__type", INPUT_JOY_MOTION);
  304. for (int i = 0; i < (int)JoyAxis::MAX * 2; i++) {
  305. int axis = i / 2;
  306. int direction = (i & 1) ? 1 : -1;
  307. Ref<InputEventJoypadMotion> joym;
  308. joym.instantiate();
  309. joym->set_axis((JoyAxis)axis);
  310. joym->set_axis_value(direction);
  311. String desc = EventListenerLineEdit::get_event_text(joym, false);
  312. if (!search_term.is_empty() && !desc.containsn(search_term)) {
  313. continue;
  314. }
  315. TreeItem *item = input_list_tree->create_item(joya_root);
  316. item->set_text(0, desc);
  317. item->set_meta("__axis", i >> 1);
  318. item->set_meta("__value", (i & 1) ? 1 : -1);
  319. }
  320. }
  321. }
  322. void InputEventConfigurationDialog::_mod_toggled(bool p_checked, int p_index) {
  323. Ref<InputEventWithModifiers> ie = event;
  324. // Not event with modifiers
  325. if (ie.is_null()) {
  326. return;
  327. }
  328. if (p_index == 0) {
  329. ie->set_alt_pressed(p_checked);
  330. } else if (p_index == 1) {
  331. ie->set_shift_pressed(p_checked);
  332. } else if (p_index == 2) {
  333. if (!autoremap_command_or_control_checkbox->is_pressed()) {
  334. ie->set_ctrl_pressed(p_checked);
  335. }
  336. } else if (p_index == 3) {
  337. if (!autoremap_command_or_control_checkbox->is_pressed()) {
  338. ie->set_meta_pressed(p_checked);
  339. }
  340. }
  341. _set_event(ie, original_event);
  342. }
  343. void InputEventConfigurationDialog::_autoremap_command_or_control_toggled(bool p_checked) {
  344. Ref<InputEventWithModifiers> ie = event;
  345. if (ie.is_valid()) {
  346. ie->set_command_or_control_autoremap(p_checked);
  347. _set_event(ie, original_event);
  348. }
  349. if (p_checked) {
  350. mod_checkboxes[MOD_META]->hide();
  351. mod_checkboxes[MOD_CTRL]->hide();
  352. } else {
  353. mod_checkboxes[MOD_META]->show();
  354. mod_checkboxes[MOD_CTRL]->show();
  355. }
  356. }
  357. void InputEventConfigurationDialog::_key_mode_selected(int p_mode) {
  358. Ref<InputEventKey> k = event;
  359. Ref<InputEventKey> ko = original_event;
  360. if (k.is_null() || ko.is_null()) {
  361. return;
  362. }
  363. if (key_mode->get_selected_id() == KEYMODE_KEYCODE) {
  364. k->set_keycode(ko->get_keycode());
  365. k->set_physical_keycode(Key::NONE);
  366. k->set_key_label(Key::NONE);
  367. } else if (key_mode->get_selected_id() == KEYMODE_PHY_KEYCODE) {
  368. k->set_keycode(Key::NONE);
  369. k->set_physical_keycode(ko->get_physical_keycode());
  370. k->set_key_label(Key::NONE);
  371. } else if (key_mode->get_selected_id() == KEYMODE_UNICODE) {
  372. k->set_physical_keycode(Key::NONE);
  373. k->set_keycode(Key::NONE);
  374. k->set_key_label(ko->get_key_label());
  375. }
  376. _set_event(k, original_event);
  377. }
  378. void InputEventConfigurationDialog::_key_location_selected(int p_location) {
  379. Ref<InputEventKey> k = event;
  380. if (k.is_null()) {
  381. return;
  382. }
  383. k->set_location((KeyLocation)p_location);
  384. _set_event(k, original_event);
  385. }
  386. void InputEventConfigurationDialog::_input_list_item_activated() {
  387. TreeItem *selected = input_list_tree->get_selected();
  388. selected->set_collapsed(!selected->is_collapsed());
  389. }
  390. void InputEventConfigurationDialog::_input_list_item_selected() {
  391. TreeItem *selected = input_list_tree->get_selected();
  392. // Called form _set_event, do not update for a second time.
  393. if (in_tree_update) {
  394. return;
  395. }
  396. // Invalid tree selection - type only exists on the "category" items, which are not a valid selection.
  397. if (selected->has_meta("__type")) {
  398. return;
  399. }
  400. InputType input_type = (InputType)(int)selected->get_parent()->get_meta("__type");
  401. switch (input_type) {
  402. case INPUT_KEY: {
  403. Key keycode = (Key)(int)selected->get_meta("__keycode");
  404. Ref<InputEventKey> k;
  405. k.instantiate();
  406. k->set_physical_keycode(keycode);
  407. k->set_keycode(keycode);
  408. k->set_key_label(keycode);
  409. // Maintain modifier state from checkboxes.
  410. k->set_alt_pressed(mod_checkboxes[MOD_ALT]->is_pressed());
  411. k->set_shift_pressed(mod_checkboxes[MOD_SHIFT]->is_pressed());
  412. if (autoremap_command_or_control_checkbox->is_pressed()) {
  413. k->set_command_or_control_autoremap(true);
  414. } else {
  415. k->set_ctrl_pressed(mod_checkboxes[MOD_CTRL]->is_pressed());
  416. k->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
  417. }
  418. Ref<InputEventKey> ko = k->duplicate();
  419. if (key_mode->get_selected_id() == KEYMODE_UNICODE) {
  420. key_mode->select(KEYMODE_PHY_KEYCODE);
  421. }
  422. if (key_mode->get_selected_id() == KEYMODE_KEYCODE) {
  423. k->set_physical_keycode(Key::NONE);
  424. k->set_keycode(keycode);
  425. k->set_key_label(Key::NONE);
  426. } else if (key_mode->get_selected_id() == KEYMODE_PHY_KEYCODE) {
  427. k->set_physical_keycode(keycode);
  428. k->set_keycode(Key::NONE);
  429. k->set_key_label(Key::NONE);
  430. }
  431. _set_event(k, ko, false);
  432. } break;
  433. case INPUT_MOUSE_BUTTON: {
  434. MouseButton idx = (MouseButton)(int)selected->get_meta("__index");
  435. Ref<InputEventMouseButton> mb;
  436. mb.instantiate();
  437. mb->set_button_index(idx);
  438. // Maintain modifier state from checkboxes
  439. mb->set_alt_pressed(mod_checkboxes[MOD_ALT]->is_pressed());
  440. mb->set_shift_pressed(mod_checkboxes[MOD_SHIFT]->is_pressed());
  441. if (autoremap_command_or_control_checkbox->is_pressed()) {
  442. mb->set_command_or_control_autoremap(true);
  443. } else {
  444. mb->set_ctrl_pressed(mod_checkboxes[MOD_CTRL]->is_pressed());
  445. mb->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
  446. }
  447. // Maintain selected device
  448. mb->set_device(_get_current_device());
  449. _set_event(mb, mb, false);
  450. } break;
  451. case INPUT_JOY_BUTTON: {
  452. JoyButton idx = (JoyButton)(int)selected->get_meta("__index");
  453. Ref<InputEventJoypadButton> jb = InputEventJoypadButton::create_reference(idx);
  454. // Maintain selected device
  455. jb->set_device(_get_current_device());
  456. _set_event(jb, jb, false);
  457. } break;
  458. case INPUT_JOY_MOTION: {
  459. JoyAxis axis = (JoyAxis)(int)selected->get_meta("__axis");
  460. int value = selected->get_meta("__value");
  461. Ref<InputEventJoypadMotion> jm;
  462. jm.instantiate();
  463. jm->set_axis(axis);
  464. jm->set_axis_value(value);
  465. // Maintain selected device
  466. jm->set_device(_get_current_device());
  467. _set_event(jm, jm, false);
  468. } break;
  469. }
  470. }
  471. void InputEventConfigurationDialog::_device_selection_changed(int p_option_button_index) {
  472. // Subtract 1 as option index 0 corresponds to "All Devices" (value of -1)
  473. // and option index 1 corresponds to device 0, etc...
  474. event->set_device(p_option_button_index - 1);
  475. event_as_text->set_text(EventListenerLineEdit::get_event_text(event, true));
  476. }
  477. void InputEventConfigurationDialog::_set_current_device(int p_device) {
  478. device_id_option->select(p_device + 1);
  479. }
  480. int InputEventConfigurationDialog::_get_current_device() const {
  481. return device_id_option->get_selected() - 1;
  482. }
  483. void InputEventConfigurationDialog::_notification(int p_what) {
  484. switch (p_what) {
  485. case NOTIFICATION_VISIBILITY_CHANGED: {
  486. event_listener->grab_focus();
  487. } break;
  488. case NOTIFICATION_THEME_CHANGED: {
  489. input_list_search->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  490. key_mode->set_item_icon(KEYMODE_KEYCODE, get_editor_theme_icon(SNAME("Keyboard")));
  491. key_mode->set_item_icon(KEYMODE_PHY_KEYCODE, get_editor_theme_icon(SNAME("KeyboardPhysical")));
  492. key_mode->set_item_icon(KEYMODE_UNICODE, get_editor_theme_icon(SNAME("KeyboardLabel")));
  493. icon_cache.keyboard = get_editor_theme_icon(SNAME("Keyboard"));
  494. icon_cache.mouse = get_editor_theme_icon(SNAME("Mouse"));
  495. icon_cache.joypad_button = get_editor_theme_icon(SNAME("JoyButton"));
  496. icon_cache.joypad_axis = get_editor_theme_icon(SNAME("JoyAxis"));
  497. event_as_text->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));
  498. _update_input_list();
  499. } break;
  500. }
  501. }
  502. void InputEventConfigurationDialog::popup_and_configure(const Ref<InputEvent> &p_event, const String &p_current_action_name) {
  503. if (p_event.is_valid()) {
  504. _set_event(p_event->duplicate(), p_event->duplicate());
  505. } else {
  506. // Clear Event
  507. _set_event(Ref<InputEvent>(), Ref<InputEvent>());
  508. // Clear Checkbox Values
  509. for (int i = 0; i < MOD_MAX; i++) {
  510. mod_checkboxes[i]->set_pressed(false);
  511. }
  512. // Enable the Physical Key by default to encourage its use.
  513. // Physical Key should be used for most game inputs as it allows keys to work
  514. // on non-QWERTY layouts out of the box.
  515. // This is especially important for WASD movement layouts.
  516. key_mode->select(KEYMODE_PHY_KEYCODE);
  517. autoremap_command_or_control_checkbox->set_pressed(false);
  518. // Select "All Devices" by default.
  519. device_id_option->select(0);
  520. // Also "all locations".
  521. key_location->select(0);
  522. }
  523. if (!p_current_action_name.is_empty()) {
  524. set_title(vformat(TTR("Event Configuration for \"%s\""), p_current_action_name));
  525. } else {
  526. set_title(TTR("Event Configuration"));
  527. }
  528. popup_centered(Size2(0, 400) * EDSCALE);
  529. }
  530. Ref<InputEvent> InputEventConfigurationDialog::get_event() const {
  531. return event;
  532. }
  533. void InputEventConfigurationDialog::set_allowed_input_types(int p_type_masks) {
  534. allowed_input_types = p_type_masks;
  535. event_listener->set_allowed_input_types(p_type_masks);
  536. }
  537. InputEventConfigurationDialog::InputEventConfigurationDialog() {
  538. allowed_input_types = INPUT_KEY | INPUT_MOUSE_BUTTON | INPUT_JOY_BUTTON | INPUT_JOY_MOTION;
  539. set_min_size(Size2i(550, 0) * EDSCALE);
  540. VBoxContainer *main_vbox = memnew(VBoxContainer);
  541. add_child(main_vbox);
  542. event_as_text = memnew(Label);
  543. event_as_text->set_custom_minimum_size(Size2(500, 0) * EDSCALE);
  544. event_as_text->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  545. event_as_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  546. event_as_text->add_theme_font_size_override(SceneStringName(font_size), 18 * EDSCALE);
  547. main_vbox->add_child(event_as_text);
  548. event_listener = memnew(EventListenerLineEdit);
  549. event_listener->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  550. event_listener->set_stretch_ratio(0.75);
  551. event_listener->connect("event_changed", callable_mp(this, &InputEventConfigurationDialog::_on_listen_input_changed));
  552. event_listener->connect(SceneStringName(focus_entered), callable_mp((AcceptDialog *)this, &AcceptDialog::set_close_on_escape).bind(false));
  553. event_listener->connect(SceneStringName(focus_exited), callable_mp((AcceptDialog *)this, &AcceptDialog::set_close_on_escape).bind(true));
  554. main_vbox->add_child(event_listener);
  555. main_vbox->add_child(memnew(HSeparator));
  556. // List of all input options to manually select from.
  557. VBoxContainer *manual_vbox = memnew(VBoxContainer);
  558. manual_vbox->set_name(TTR("Manual Selection"));
  559. manual_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  560. main_vbox->add_child(manual_vbox);
  561. input_list_search = memnew(LineEdit);
  562. input_list_search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  563. input_list_search->set_placeholder(TTR("Filter Inputs"));
  564. input_list_search->set_clear_button_enabled(true);
  565. input_list_search->connect(SceneStringName(text_changed), callable_mp(this, &InputEventConfigurationDialog::_search_term_updated));
  566. manual_vbox->add_child(input_list_search);
  567. input_list_tree = memnew(Tree);
  568. input_list_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  569. input_list_tree->set_custom_minimum_size(Size2(0, 100 * EDSCALE)); // Min height for tree
  570. input_list_tree->connect("item_activated", callable_mp(this, &InputEventConfigurationDialog::_input_list_item_activated));
  571. input_list_tree->connect(SceneStringName(item_selected), callable_mp(this, &InputEventConfigurationDialog::_input_list_item_selected));
  572. input_list_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  573. manual_vbox->add_child(input_list_tree);
  574. input_list_tree->set_hide_root(true);
  575. input_list_tree->set_columns(1);
  576. _update_input_list();
  577. // Additional Options
  578. additional_options_container = memnew(VBoxContainer);
  579. additional_options_container->hide();
  580. Label *opts_label = memnew(Label);
  581. opts_label->set_theme_type_variation("HeaderSmall");
  582. opts_label->set_text(TTR("Additional Options"));
  583. additional_options_container->add_child(opts_label);
  584. // Device Selection
  585. device_container = memnew(HBoxContainer);
  586. device_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  587. Label *device_label = memnew(Label);
  588. device_label->set_theme_type_variation("HeaderSmall");
  589. device_label->set_text(TTR("Device:"));
  590. device_container->add_child(device_label);
  591. device_id_option = memnew(OptionButton);
  592. device_id_option->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  593. for (int i = -1; i < 8; i++) {
  594. device_id_option->add_item(EventListenerLineEdit::get_device_string(i));
  595. }
  596. device_id_option->connect(SceneStringName(item_selected), callable_mp(this, &InputEventConfigurationDialog::_device_selection_changed));
  597. _set_current_device(InputMap::ALL_DEVICES);
  598. device_container->add_child(device_id_option);
  599. device_container->hide();
  600. additional_options_container->add_child(device_container);
  601. // Modifier Selection
  602. mod_container = memnew(HBoxContainer);
  603. for (int i = 0; i < MOD_MAX; i++) {
  604. String name = mods[i];
  605. mod_checkboxes[i] = memnew(CheckBox);
  606. mod_checkboxes[i]->connect(SceneStringName(toggled), callable_mp(this, &InputEventConfigurationDialog::_mod_toggled).bind(i));
  607. mod_checkboxes[i]->set_text(name);
  608. mod_checkboxes[i]->set_tooltip_text(TTR(mods_tip[i]));
  609. mod_container->add_child(mod_checkboxes[i]);
  610. }
  611. mod_container->add_child(memnew(VSeparator));
  612. autoremap_command_or_control_checkbox = memnew(CheckBox);
  613. autoremap_command_or_control_checkbox->connect(SceneStringName(toggled), callable_mp(this, &InputEventConfigurationDialog::_autoremap_command_or_control_toggled));
  614. autoremap_command_or_control_checkbox->set_pressed(false);
  615. autoremap_command_or_control_checkbox->set_text(TTR("Command / Control (auto)"));
  616. autoremap_command_or_control_checkbox->set_tooltip_text(TTR("Automatically remaps between 'Meta' ('Command') and 'Control' depending on current platform."));
  617. mod_container->add_child(autoremap_command_or_control_checkbox);
  618. mod_container->hide();
  619. additional_options_container->add_child(mod_container);
  620. // Key Mode Selection
  621. key_mode = memnew(OptionButton);
  622. key_mode->add_item(TTR("Keycode (Latin Equivalent)"), KEYMODE_KEYCODE);
  623. key_mode->add_item(TTR("Physical Keycode (Position on US QWERTY Keyboard)"), KEYMODE_PHY_KEYCODE);
  624. key_mode->add_item(TTR("Key Label (Unicode, Case-Insensitive)"), KEYMODE_UNICODE);
  625. key_mode->connect(SceneStringName(item_selected), callable_mp(this, &InputEventConfigurationDialog::_key_mode_selected));
  626. key_mode->hide();
  627. additional_options_container->add_child(key_mode);
  628. // Key Location Selection
  629. location_container = memnew(HBoxContainer);
  630. location_container->hide();
  631. Label *location_label = memnew(Label);
  632. location_label->set_text(TTR("Physical location"));
  633. location_container->add_child(location_label);
  634. key_location = memnew(OptionButton);
  635. key_location->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  636. key_location->add_item(TTR("Any"), (int)KeyLocation::UNSPECIFIED);
  637. key_location->add_item(TTR("Left"), (int)KeyLocation::LEFT);
  638. key_location->add_item(TTR("Right"), (int)KeyLocation::RIGHT);
  639. key_location->connect(SceneStringName(item_selected), callable_mp(this, &InputEventConfigurationDialog::_key_location_selected));
  640. location_container->add_child(key_location);
  641. additional_options_container->add_child(location_container);
  642. main_vbox->add_child(additional_options_container);
  643. }