input_event_configuration_dialog.cpp 25 KB

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