input_event_configuration_dialog.cpp 29 KB

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