input_event_configuration_dialog.cpp 26 KB

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