input_event_configuration_dialog.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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_selected() {
  387. TreeItem *selected = input_list_tree->get_selected();
  388. // Called form _set_event, do not update for a second time.
  389. if (in_tree_update) {
  390. return;
  391. }
  392. // Invalid tree selection - type only exists on the "category" items, which are not a valid selection.
  393. if (selected->has_meta("__type")) {
  394. return;
  395. }
  396. InputType input_type = (InputType)(int)selected->get_parent()->get_meta("__type");
  397. switch (input_type) {
  398. case INPUT_KEY: {
  399. Key keycode = (Key)(int)selected->get_meta("__keycode");
  400. Ref<InputEventKey> k;
  401. k.instantiate();
  402. k->set_physical_keycode(keycode);
  403. k->set_keycode(keycode);
  404. k->set_key_label(keycode);
  405. // Maintain modifier state from checkboxes.
  406. k->set_alt_pressed(mod_checkboxes[MOD_ALT]->is_pressed());
  407. k->set_shift_pressed(mod_checkboxes[MOD_SHIFT]->is_pressed());
  408. if (autoremap_command_or_control_checkbox->is_pressed()) {
  409. k->set_command_or_control_autoremap(true);
  410. } else {
  411. k->set_ctrl_pressed(mod_checkboxes[MOD_CTRL]->is_pressed());
  412. k->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
  413. }
  414. Ref<InputEventKey> ko = k->duplicate();
  415. if (key_mode->get_selected_id() == KEYMODE_UNICODE) {
  416. key_mode->select(KEYMODE_PHY_KEYCODE);
  417. }
  418. if (key_mode->get_selected_id() == KEYMODE_KEYCODE) {
  419. k->set_physical_keycode(Key::NONE);
  420. k->set_keycode(keycode);
  421. k->set_key_label(Key::NONE);
  422. } else if (key_mode->get_selected_id() == KEYMODE_PHY_KEYCODE) {
  423. k->set_physical_keycode(keycode);
  424. k->set_keycode(Key::NONE);
  425. k->set_key_label(Key::NONE);
  426. }
  427. _set_event(k, ko, false);
  428. } break;
  429. case INPUT_MOUSE_BUTTON: {
  430. MouseButton idx = (MouseButton)(int)selected->get_meta("__index");
  431. Ref<InputEventMouseButton> mb;
  432. mb.instantiate();
  433. mb->set_button_index(idx);
  434. // Maintain modifier state from checkboxes
  435. mb->set_alt_pressed(mod_checkboxes[MOD_ALT]->is_pressed());
  436. mb->set_shift_pressed(mod_checkboxes[MOD_SHIFT]->is_pressed());
  437. if (autoremap_command_or_control_checkbox->is_pressed()) {
  438. mb->set_command_or_control_autoremap(true);
  439. } else {
  440. mb->set_ctrl_pressed(mod_checkboxes[MOD_CTRL]->is_pressed());
  441. mb->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
  442. }
  443. // Maintain selected device
  444. mb->set_device(_get_current_device());
  445. _set_event(mb, mb, false);
  446. } break;
  447. case INPUT_JOY_BUTTON: {
  448. JoyButton idx = (JoyButton)(int)selected->get_meta("__index");
  449. Ref<InputEventJoypadButton> jb = InputEventJoypadButton::create_reference(idx);
  450. // Maintain selected device
  451. jb->set_device(_get_current_device());
  452. _set_event(jb, jb, false);
  453. } break;
  454. case INPUT_JOY_MOTION: {
  455. JoyAxis axis = (JoyAxis)(int)selected->get_meta("__axis");
  456. int value = selected->get_meta("__value");
  457. Ref<InputEventJoypadMotion> jm;
  458. jm.instantiate();
  459. jm->set_axis(axis);
  460. jm->set_axis_value(value);
  461. // Maintain selected device
  462. jm->set_device(_get_current_device());
  463. _set_event(jm, jm, false);
  464. } break;
  465. }
  466. }
  467. void InputEventConfigurationDialog::_device_selection_changed(int p_option_button_index) {
  468. // Subtract 1 as option index 0 corresponds to "All Devices" (value of -1)
  469. // and option index 1 corresponds to device 0, etc...
  470. event->set_device(p_option_button_index - 1);
  471. event_as_text->set_text(EventListenerLineEdit::get_event_text(event, true));
  472. }
  473. void InputEventConfigurationDialog::_set_current_device(int p_device) {
  474. device_id_option->select(p_device + 1);
  475. }
  476. int InputEventConfigurationDialog::_get_current_device() const {
  477. return device_id_option->get_selected() - 1;
  478. }
  479. void InputEventConfigurationDialog::_notification(int p_what) {
  480. switch (p_what) {
  481. case NOTIFICATION_VISIBILITY_CHANGED: {
  482. event_listener->grab_focus();
  483. } break;
  484. case NOTIFICATION_THEME_CHANGED: {
  485. input_list_search->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  486. key_mode->set_item_icon(KEYMODE_KEYCODE, get_editor_theme_icon(SNAME("Keyboard")));
  487. key_mode->set_item_icon(KEYMODE_PHY_KEYCODE, get_editor_theme_icon(SNAME("KeyboardPhysical")));
  488. key_mode->set_item_icon(KEYMODE_UNICODE, get_editor_theme_icon(SNAME("KeyboardLabel")));
  489. icon_cache.keyboard = get_editor_theme_icon(SNAME("Keyboard"));
  490. icon_cache.mouse = get_editor_theme_icon(SNAME("Mouse"));
  491. icon_cache.joypad_button = get_editor_theme_icon(SNAME("JoyButton"));
  492. icon_cache.joypad_axis = get_editor_theme_icon(SNAME("JoyAxis"));
  493. event_as_text->add_theme_font_override("font", get_theme_font(SNAME("bold"), EditorStringName(EditorFonts)));
  494. _update_input_list();
  495. } break;
  496. }
  497. }
  498. void InputEventConfigurationDialog::popup_and_configure(const Ref<InputEvent> &p_event, const String &p_current_action_name) {
  499. if (p_event.is_valid()) {
  500. _set_event(p_event->duplicate(), p_event->duplicate());
  501. } else {
  502. // Clear Event
  503. _set_event(Ref<InputEvent>(), Ref<InputEvent>());
  504. // Clear Checkbox Values
  505. for (int i = 0; i < MOD_MAX; i++) {
  506. mod_checkboxes[i]->set_pressed(false);
  507. }
  508. // Enable the Physical Key by default to encourage its use.
  509. // Physical Key should be used for most game inputs as it allows keys to work
  510. // on non-QWERTY layouts out of the box.
  511. // This is especially important for WASD movement layouts.
  512. key_mode->select(KEYMODE_PHY_KEYCODE);
  513. autoremap_command_or_control_checkbox->set_pressed(false);
  514. // Select "All Devices" by default.
  515. device_id_option->select(0);
  516. // Also "all locations".
  517. key_location->select(0);
  518. }
  519. if (!p_current_action_name.is_empty()) {
  520. set_title(vformat(TTR("Event Configuration for \"%s\""), p_current_action_name));
  521. } else {
  522. set_title(TTR("Event Configuration"));
  523. }
  524. popup_centered(Size2(0, 400) * EDSCALE);
  525. }
  526. Ref<InputEvent> InputEventConfigurationDialog::get_event() const {
  527. return event;
  528. }
  529. void InputEventConfigurationDialog::set_allowed_input_types(int p_type_masks) {
  530. allowed_input_types = p_type_masks;
  531. event_listener->set_allowed_input_types(p_type_masks);
  532. }
  533. InputEventConfigurationDialog::InputEventConfigurationDialog() {
  534. allowed_input_types = INPUT_KEY | INPUT_MOUSE_BUTTON | INPUT_JOY_BUTTON | INPUT_JOY_MOTION;
  535. set_min_size(Size2i(550, 0) * EDSCALE);
  536. VBoxContainer *main_vbox = memnew(VBoxContainer);
  537. add_child(main_vbox);
  538. event_as_text = memnew(Label);
  539. event_as_text->set_custom_minimum_size(Size2(500, 0) * EDSCALE);
  540. event_as_text->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  541. event_as_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  542. event_as_text->add_theme_font_size_override("font_size", 18 * EDSCALE);
  543. main_vbox->add_child(event_as_text);
  544. event_listener = memnew(EventListenerLineEdit);
  545. event_listener->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  546. event_listener->set_stretch_ratio(0.75);
  547. event_listener->connect("event_changed", callable_mp(this, &InputEventConfigurationDialog::_on_listen_input_changed));
  548. event_listener->connect(SceneStringName(focus_entered), callable_mp((AcceptDialog *)this, &AcceptDialog::set_close_on_escape).bind(false));
  549. event_listener->connect(SceneStringName(focus_exited), callable_mp((AcceptDialog *)this, &AcceptDialog::set_close_on_escape).bind(true));
  550. main_vbox->add_child(event_listener);
  551. main_vbox->add_child(memnew(HSeparator));
  552. // List of all input options to manually select from.
  553. VBoxContainer *manual_vbox = memnew(VBoxContainer);
  554. manual_vbox->set_name(TTR("Manual Selection"));
  555. manual_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  556. main_vbox->add_child(manual_vbox);
  557. input_list_search = memnew(LineEdit);
  558. input_list_search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  559. input_list_search->set_placeholder(TTR("Filter Inputs"));
  560. input_list_search->set_clear_button_enabled(true);
  561. input_list_search->connect("text_changed", callable_mp(this, &InputEventConfigurationDialog::_search_term_updated));
  562. manual_vbox->add_child(input_list_search);
  563. input_list_tree = memnew(Tree);
  564. input_list_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  565. input_list_tree->set_custom_minimum_size(Size2(0, 100 * EDSCALE)); // Min height for tree
  566. input_list_tree->connect("item_selected", callable_mp(this, &InputEventConfigurationDialog::_input_list_item_selected));
  567. input_list_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  568. manual_vbox->add_child(input_list_tree);
  569. input_list_tree->set_hide_root(true);
  570. input_list_tree->set_columns(1);
  571. _update_input_list();
  572. // Additional Options
  573. additional_options_container = memnew(VBoxContainer);
  574. additional_options_container->hide();
  575. Label *opts_label = memnew(Label);
  576. opts_label->set_theme_type_variation("HeaderSmall");
  577. opts_label->set_text(TTR("Additional Options"));
  578. additional_options_container->add_child(opts_label);
  579. // Device Selection
  580. device_container = memnew(HBoxContainer);
  581. device_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  582. Label *device_label = memnew(Label);
  583. device_label->set_theme_type_variation("HeaderSmall");
  584. device_label->set_text(TTR("Device:"));
  585. device_container->add_child(device_label);
  586. device_id_option = memnew(OptionButton);
  587. device_id_option->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  588. for (int i = -1; i < 8; i++) {
  589. device_id_option->add_item(EventListenerLineEdit::get_device_string(i));
  590. }
  591. device_id_option->connect("item_selected", callable_mp(this, &InputEventConfigurationDialog::_device_selection_changed));
  592. _set_current_device(InputMap::ALL_DEVICES);
  593. device_container->add_child(device_id_option);
  594. device_container->hide();
  595. additional_options_container->add_child(device_container);
  596. // Modifier Selection
  597. mod_container = memnew(HBoxContainer);
  598. for (int i = 0; i < MOD_MAX; i++) {
  599. String name = mods[i];
  600. mod_checkboxes[i] = memnew(CheckBox);
  601. mod_checkboxes[i]->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_mod_toggled).bind(i));
  602. mod_checkboxes[i]->set_text(name);
  603. mod_checkboxes[i]->set_tooltip_text(TTR(mods_tip[i]));
  604. mod_container->add_child(mod_checkboxes[i]);
  605. }
  606. mod_container->add_child(memnew(VSeparator));
  607. autoremap_command_or_control_checkbox = memnew(CheckBox);
  608. autoremap_command_or_control_checkbox->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_autoremap_command_or_control_toggled));
  609. autoremap_command_or_control_checkbox->set_pressed(false);
  610. autoremap_command_or_control_checkbox->set_text(TTR("Command / Control (auto)"));
  611. autoremap_command_or_control_checkbox->set_tooltip_text(TTR("Automatically remaps between 'Meta' ('Command') and 'Control' depending on current platform."));
  612. mod_container->add_child(autoremap_command_or_control_checkbox);
  613. mod_container->hide();
  614. additional_options_container->add_child(mod_container);
  615. // Key Mode Selection
  616. key_mode = memnew(OptionButton);
  617. key_mode->add_item(TTR("Keycode (Latin Equivalent)"), KEYMODE_KEYCODE);
  618. key_mode->add_item(TTR("Physical Keycode (Position on US QWERTY Keyboard)"), KEYMODE_PHY_KEYCODE);
  619. key_mode->add_item(TTR("Key Label (Unicode, Case-Insensitive)"), KEYMODE_UNICODE);
  620. key_mode->connect("item_selected", callable_mp(this, &InputEventConfigurationDialog::_key_mode_selected));
  621. key_mode->hide();
  622. additional_options_container->add_child(key_mode);
  623. // Key Location Selection
  624. location_container = memnew(HBoxContainer);
  625. location_container->hide();
  626. Label *location_label = memnew(Label);
  627. location_label->set_text(TTR("Physical location"));
  628. location_container->add_child(location_label);
  629. key_location = memnew(OptionButton);
  630. key_location->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  631. key_location->add_item(TTR("Any"), (int)KeyLocation::UNSPECIFIED);
  632. key_location->add_item(TTR("Left"), (int)KeyLocation::LEFT);
  633. key_location->add_item(TTR("Right"), (int)KeyLocation::RIGHT);
  634. key_location->connect("item_selected", callable_mp(this, &InputEventConfigurationDialog::_key_location_selected));
  635. location_container->add_child(key_location);
  636. additional_options_container->add_child(location_container);
  637. main_vbox->add_child(additional_options_container);
  638. }