action_map_editor.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*************************************************************************/
  2. /* action_map_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "action_map_editor.h"
  31. #include "core/input/input_map.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor/editor_scale.h"
  34. #include "scene/gui/separator.h"
  35. /////////////////////////////////////////
  36. // Maps to 2*axis if value is neg, or 2*axis+1 if value is pos.
  37. static const char *_joy_axis_descriptions[(size_t)JoyAxis::MAX * 2] = {
  38. TTRC("Left Stick Left, Joystick 0 Left"),
  39. TTRC("Left Stick Right, Joystick 0 Right"),
  40. TTRC("Left Stick Up, Joystick 0 Up"),
  41. TTRC("Left Stick Down, Joystick 0 Down"),
  42. TTRC("Right Stick Left, Joystick 1 Left"),
  43. TTRC("Right Stick Right, Joystick 1 Right"),
  44. TTRC("Right Stick Up, Joystick 1 Up"),
  45. TTRC("Right Stick Down, Joystick 1 Down"),
  46. TTRC("Joystick 2 Left"),
  47. TTRC("Left Trigger, Sony L2, Xbox LT, Joystick 2 Right"),
  48. TTRC("Joystick 2 Up"),
  49. TTRC("Right Trigger, Sony R2, Xbox RT, Joystick 2 Down"),
  50. TTRC("Joystick 3 Left"),
  51. TTRC("Joystick 3 Right"),
  52. TTRC("Joystick 3 Up"),
  53. TTRC("Joystick 3 Down"),
  54. TTRC("Joystick 4 Left"),
  55. TTRC("Joystick 4 Right"),
  56. TTRC("Joystick 4 Up"),
  57. TTRC("Joystick 4 Down"),
  58. };
  59. String InputEventConfigurationDialog::get_event_text(const Ref<InputEvent> &p_event, bool p_include_device) const {
  60. ERR_FAIL_COND_V_MSG(p_event.is_null(), String(), "Provided event is not a valid instance of InputEvent");
  61. String text = p_event->as_text();
  62. Ref<InputEventMouse> mouse = p_event;
  63. Ref<InputEventJoypadMotion> jp_motion = p_event;
  64. Ref<InputEventJoypadButton> jp_button = p_event;
  65. if (jp_motion.is_valid()) {
  66. // Joypad motion events will display slightly differently than what the event->as_text() provides. See #43660.
  67. String desc = TTR("Unknown Joypad Axis");
  68. if (jp_motion->get_axis() < JoyAxis::MAX) {
  69. desc = RTR(_joy_axis_descriptions[2 * (size_t)jp_motion->get_axis() + (jp_motion->get_axis_value() < 0 ? 0 : 1)]);
  70. }
  71. text = vformat("Joypad Axis %s %s (%s)", itos((int64_t)jp_motion->get_axis()), jp_motion->get_axis_value() < 0 ? "-" : "+", desc);
  72. }
  73. if (p_include_device && (mouse.is_valid() || jp_button.is_valid() || jp_motion.is_valid())) {
  74. String device_string = _get_device_string(p_event->get_device());
  75. text += vformat(" - %s", device_string);
  76. }
  77. return text;
  78. }
  79. void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event, bool p_update_input_list_selection) {
  80. if (p_event.is_valid()) {
  81. event = p_event;
  82. // Update Label
  83. event_as_text->set_text(get_event_text(event, true));
  84. Ref<InputEventKey> k = p_event;
  85. Ref<InputEventMouseButton> mb = p_event;
  86. Ref<InputEventJoypadButton> joyb = p_event;
  87. Ref<InputEventJoypadMotion> joym = p_event;
  88. Ref<InputEventWithModifiers> mod = p_event;
  89. // Update option values and visibility
  90. bool show_mods = false;
  91. bool show_device = false;
  92. bool show_phys_key = false;
  93. if (mod.is_valid()) {
  94. show_mods = true;
  95. mod_checkboxes[MOD_ALT]->set_pressed(mod->is_alt_pressed());
  96. mod_checkboxes[MOD_SHIFT]->set_pressed(mod->is_shift_pressed());
  97. mod_checkboxes[MOD_COMMAND]->set_pressed(mod->is_command_pressed());
  98. mod_checkboxes[MOD_CTRL]->set_pressed(mod->is_ctrl_pressed());
  99. mod_checkboxes[MOD_META]->set_pressed(mod->is_meta_pressed());
  100. store_command_checkbox->set_pressed(mod->is_storing_command());
  101. }
  102. if (k.is_valid()) {
  103. show_phys_key = true;
  104. physical_key_checkbox->set_pressed(k->get_physical_keycode() != Key::NONE && k->get_keycode() == Key::NONE);
  105. } else if (joyb.is_valid() || joym.is_valid() || mb.is_valid()) {
  106. show_device = true;
  107. _set_current_device(event->get_device());
  108. }
  109. mod_container->set_visible(show_mods);
  110. device_container->set_visible(show_device);
  111. physical_key_checkbox->set_visible(show_phys_key);
  112. additional_options_container->show();
  113. // Update selected item in input list.
  114. if (p_update_input_list_selection && (k.is_valid() || joyb.is_valid() || joym.is_valid() || mb.is_valid())) {
  115. TreeItem *category = input_list_tree->get_root()->get_first_child();
  116. while (category) {
  117. TreeItem *input_item = category->get_first_child();
  118. if (input_item != nullptr) {
  119. // input_type should always be > 0, unless the tree structure has been misconfigured.
  120. int input_type = input_item->get_parent()->get_meta("__type", 0);
  121. if (input_type == 0) {
  122. return;
  123. }
  124. // If event type matches input types of this category.
  125. 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)) {
  126. // Loop through all items of this category until one matches.
  127. while (input_item) {
  128. 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"));
  129. bool joyb_match = joyb.is_valid() && Variant(joyb->get_button_index()) == input_item->get_meta("__index");
  130. 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");
  131. bool mb_match = mb.is_valid() && Variant(mb->get_button_index()) == input_item->get_meta("__index");
  132. if (key_match || joyb_match || joym_match || mb_match) {
  133. category->set_collapsed(false);
  134. input_item->select(0);
  135. input_list_tree->ensure_cursor_is_visible();
  136. return;
  137. }
  138. input_item = input_item->get_next();
  139. }
  140. }
  141. }
  142. category->set_collapsed(true); // Event not in this category, so collapse;
  143. category = category->get_next();
  144. }
  145. }
  146. } else {
  147. // Event is not valid, reset dialog
  148. event = p_event;
  149. Vector<String> strings;
  150. // Reset message, promp for input according to which input types are allowed.
  151. String text = TTR("Perform an Input (%s).");
  152. if (allowed_input_types & INPUT_KEY) {
  153. strings.append(TTR("Key"));
  154. }
  155. if (allowed_input_types & INPUT_JOY_BUTTON) {
  156. strings.append(TTR("Joypad Button"));
  157. }
  158. if (allowed_input_types & INPUT_JOY_MOTION) {
  159. strings.append(TTR("Joypad Axis"));
  160. }
  161. if (allowed_input_types & INPUT_MOUSE_BUTTON) {
  162. strings.append(TTR("Mouse Button in area below"));
  163. }
  164. if (strings.size() == 0) {
  165. text = TTR("Input Event dialog has been misconfigured: No input types are allowed.");
  166. event_as_text->set_text(text);
  167. } else {
  168. String insert_text = String(", ").join(strings);
  169. event_as_text->set_text(vformat(text, insert_text));
  170. }
  171. additional_options_container->hide();
  172. input_list_tree->deselect_all();
  173. _update_input_list();
  174. }
  175. }
  176. void InputEventConfigurationDialog::_tab_selected(int p_tab) {
  177. Callable signal_method = callable_mp(this, &InputEventConfigurationDialog::_listen_window_input);
  178. if (p_tab == 0) {
  179. // Start Listening.
  180. if (!is_connected("window_input", signal_method)) {
  181. connect("window_input", signal_method);
  182. }
  183. } else {
  184. // Stop Listening.
  185. if (is_connected("window_input", signal_method)) {
  186. disconnect("window_input", signal_method);
  187. }
  188. input_list_tree->call_deferred(SNAME("ensure_cursor_is_visible"));
  189. if (input_list_tree->get_selected() == nullptr) {
  190. // If nothing selected, scroll to top.
  191. input_list_tree->scroll_to_item(input_list_tree->get_root());
  192. }
  193. }
  194. }
  195. void InputEventConfigurationDialog::_listen_window_input(const Ref<InputEvent> &p_event) {
  196. // Ignore if echo or not pressed
  197. if (p_event->is_echo() || !p_event->is_pressed()) {
  198. return;
  199. }
  200. // Ignore mouse motion
  201. Ref<InputEventMouseMotion> mm = p_event;
  202. if (mm.is_valid()) {
  203. return;
  204. }
  205. // Ignore mouse button if not in the detection rect
  206. Ref<InputEventMouseButton> mb = p_event;
  207. if (mb.is_valid()) {
  208. Rect2 r = mouse_detection_rect->get_rect();
  209. if (!r.has_point(mouse_detection_rect->get_local_mouse_position() + r.get_position())) {
  210. return;
  211. }
  212. }
  213. // Create an editable reference
  214. Ref<InputEvent> received_event = p_event;
  215. // Check what the type is and if it is allowed.
  216. Ref<InputEventKey> k = received_event;
  217. Ref<InputEventJoypadButton> joyb = received_event;
  218. Ref<InputEventJoypadMotion> joym = received_event;
  219. int type = 0;
  220. if (k.is_valid()) {
  221. type = INPUT_KEY;
  222. } else if (joyb.is_valid()) {
  223. type = INPUT_JOY_BUTTON;
  224. } else if (joym.is_valid()) {
  225. type = INPUT_JOY_MOTION;
  226. } else if (mb.is_valid()) {
  227. type = INPUT_MOUSE_BUTTON;
  228. }
  229. if (!(allowed_input_types & type)) {
  230. return;
  231. }
  232. if (joym.is_valid()) {
  233. float axis_value = joym->get_axis_value();
  234. if (ABS(axis_value) < 0.9) {
  235. // Ignore motion below 0.9 magnitude to avoid accidental touches
  236. return;
  237. } else {
  238. // Always make the value 1 or -1 for display consistency
  239. joym->set_axis_value(SIGN(axis_value));
  240. }
  241. }
  242. if (k.is_valid()) {
  243. k->set_pressed(false); // To avoid serialisation of 'pressed' property - doesn't matter for actions anyway.
  244. // Maintain physical keycode option state
  245. if (physical_key_checkbox->is_pressed()) {
  246. k->set_keycode(Key::NONE);
  247. } else {
  248. k->set_physical_keycode(Key::NONE);
  249. }
  250. }
  251. Ref<InputEventWithModifiers> mod = received_event;
  252. if (mod.is_valid()) {
  253. // Maintain store command option state
  254. mod->set_store_command(store_command_checkbox->is_pressed());
  255. mod->set_window_id(0);
  256. }
  257. // Maintain device selection.
  258. received_event->set_device(_get_current_device());
  259. _set_event(received_event);
  260. set_input_as_handled();
  261. }
  262. void InputEventConfigurationDialog::_search_term_updated(const String &) {
  263. _update_input_list();
  264. }
  265. void InputEventConfigurationDialog::_update_input_list() {
  266. input_list_tree->clear();
  267. TreeItem *root = input_list_tree->create_item();
  268. String search_term = input_list_search->get_text();
  269. bool collapse = input_list_search->get_text().is_empty();
  270. if (allowed_input_types & INPUT_KEY) {
  271. TreeItem *kb_root = input_list_tree->create_item(root);
  272. kb_root->set_text(0, TTR("Keyboard Keys"));
  273. kb_root->set_icon(0, icon_cache.keyboard);
  274. kb_root->set_collapsed(collapse);
  275. kb_root->set_meta("__type", INPUT_KEY);
  276. for (int i = 0; i < keycode_get_count(); i++) {
  277. String name = keycode_get_name_by_index(i);
  278. if (!search_term.is_empty() && name.findn(search_term) == -1) {
  279. continue;
  280. }
  281. TreeItem *item = input_list_tree->create_item(kb_root);
  282. item->set_text(0, name);
  283. item->set_meta("__keycode", keycode_get_value_by_index(i));
  284. }
  285. }
  286. if (allowed_input_types & INPUT_MOUSE_BUTTON) {
  287. TreeItem *mouse_root = input_list_tree->create_item(root);
  288. mouse_root->set_text(0, TTR("Mouse Buttons"));
  289. mouse_root->set_icon(0, icon_cache.mouse);
  290. mouse_root->set_collapsed(collapse);
  291. mouse_root->set_meta("__type", INPUT_MOUSE_BUTTON);
  292. 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 };
  293. for (int i = 0; i < 9; i++) {
  294. Ref<InputEventMouseButton> mb;
  295. mb.instantiate();
  296. mb->set_button_index(mouse_buttons[i]);
  297. String desc = get_event_text(mb, false);
  298. if (!search_term.is_empty() && desc.findn(search_term) == -1) {
  299. continue;
  300. }
  301. TreeItem *item = input_list_tree->create_item(mouse_root);
  302. item->set_text(0, desc);
  303. item->set_meta("__index", mouse_buttons[i]);
  304. }
  305. }
  306. if (allowed_input_types & INPUT_JOY_BUTTON) {
  307. TreeItem *joyb_root = input_list_tree->create_item(root);
  308. joyb_root->set_text(0, TTR("Joypad Buttons"));
  309. joyb_root->set_icon(0, icon_cache.joypad_button);
  310. joyb_root->set_collapsed(collapse);
  311. joyb_root->set_meta("__type", INPUT_JOY_BUTTON);
  312. for (int i = 0; i < (int)JoyButton::MAX; i++) {
  313. Ref<InputEventJoypadButton> joyb;
  314. joyb.instantiate();
  315. joyb->set_button_index((JoyButton)i);
  316. String desc = get_event_text(joyb, false);
  317. if (!search_term.is_empty() && desc.findn(search_term) == -1) {
  318. continue;
  319. }
  320. TreeItem *item = input_list_tree->create_item(joyb_root);
  321. item->set_text(0, desc);
  322. item->set_meta("__index", i);
  323. }
  324. }
  325. if (allowed_input_types & INPUT_JOY_MOTION) {
  326. TreeItem *joya_root = input_list_tree->create_item(root);
  327. joya_root->set_text(0, TTR("Joypad Axes"));
  328. joya_root->set_icon(0, icon_cache.joypad_axis);
  329. joya_root->set_collapsed(collapse);
  330. joya_root->set_meta("__type", INPUT_JOY_MOTION);
  331. for (int i = 0; i < (int)JoyAxis::MAX * 2; i++) {
  332. int axis = i / 2;
  333. int direction = (i & 1) ? 1 : -1;
  334. Ref<InputEventJoypadMotion> joym;
  335. joym.instantiate();
  336. joym->set_axis((JoyAxis)axis);
  337. joym->set_axis_value(direction);
  338. String desc = get_event_text(joym, false);
  339. if (!search_term.is_empty() && desc.findn(search_term) == -1) {
  340. continue;
  341. }
  342. TreeItem *item = input_list_tree->create_item(joya_root);
  343. item->set_text(0, desc);
  344. item->set_meta("__axis", i >> 1);
  345. item->set_meta("__value", (i & 1) ? 1 : -1);
  346. }
  347. }
  348. }
  349. void InputEventConfigurationDialog::_mod_toggled(bool p_checked, int p_index) {
  350. Ref<InputEventWithModifiers> ie = event;
  351. // Not event with modifiers
  352. if (ie.is_null()) {
  353. return;
  354. }
  355. if (p_index == 0) {
  356. ie->set_alt_pressed(p_checked);
  357. } else if (p_index == 1) {
  358. ie->set_shift_pressed(p_checked);
  359. } else if (p_index == 2) {
  360. ie->set_command_pressed(p_checked);
  361. } else if (p_index == 3) {
  362. ie->set_ctrl_pressed(p_checked);
  363. } else if (p_index == 4) {
  364. ie->set_meta_pressed(p_checked);
  365. }
  366. _set_event(ie);
  367. }
  368. void InputEventConfigurationDialog::_store_command_toggled(bool p_checked) {
  369. Ref<InputEventWithModifiers> ie = event;
  370. if (ie.is_valid()) {
  371. ie->set_store_command(p_checked);
  372. _set_event(ie);
  373. }
  374. if (p_checked) {
  375. // If storing Command, show it's checkbox and hide Control (Win/Lin) or Meta (Mac)
  376. #ifdef APPLE_STYLE_KEYS
  377. mod_checkboxes[MOD_META]->hide();
  378. mod_checkboxes[MOD_COMMAND]->show();
  379. mod_checkboxes[MOD_COMMAND]->set_text("Meta (Command)");
  380. #else
  381. mod_checkboxes[MOD_CTRL]->hide();
  382. mod_checkboxes[MOD_COMMAND]->show();
  383. mod_checkboxes[MOD_COMMAND]->set_text("Control (Command)");
  384. #endif
  385. } else {
  386. // If not, hide Command, show Control and Meta.
  387. mod_checkboxes[MOD_COMMAND]->hide();
  388. mod_checkboxes[MOD_CTRL]->show();
  389. mod_checkboxes[MOD_META]->show();
  390. }
  391. }
  392. void InputEventConfigurationDialog::_physical_keycode_toggled(bool p_checked) {
  393. Ref<InputEventKey> k = event;
  394. if (k.is_null()) {
  395. return;
  396. }
  397. if (p_checked) {
  398. k->set_physical_keycode(k->get_keycode());
  399. k->set_keycode(Key::NONE);
  400. } else {
  401. k->set_keycode((Key)k->get_physical_keycode());
  402. k->set_physical_keycode(Key::NONE);
  403. }
  404. _set_event(k);
  405. }
  406. void InputEventConfigurationDialog::_input_list_item_selected() {
  407. TreeItem *selected = input_list_tree->get_selected();
  408. // Invalid tree selection - type only exists on the "category" items, which are not a valid selection.
  409. if (selected->has_meta("__type")) {
  410. return;
  411. }
  412. InputEventConfigurationDialog::InputType input_type = (InputEventConfigurationDialog::InputType)(int)selected->get_parent()->get_meta("__type");
  413. switch (input_type) {
  414. case InputEventConfigurationDialog::INPUT_KEY: {
  415. Key keycode = (Key)(int)selected->get_meta("__keycode");
  416. Ref<InputEventKey> k;
  417. k.instantiate();
  418. if (physical_key_checkbox->is_pressed()) {
  419. k->set_physical_keycode(keycode);
  420. k->set_keycode(Key::NONE);
  421. } else {
  422. k->set_physical_keycode(Key::NONE);
  423. k->set_keycode(keycode);
  424. }
  425. // Maintain modifier state from checkboxes
  426. k->set_alt_pressed(mod_checkboxes[MOD_ALT]->is_pressed());
  427. k->set_shift_pressed(mod_checkboxes[MOD_SHIFT]->is_pressed());
  428. k->set_command_pressed(mod_checkboxes[MOD_COMMAND]->is_pressed());
  429. k->set_ctrl_pressed(mod_checkboxes[MOD_CTRL]->is_pressed());
  430. k->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
  431. k->set_store_command(store_command_checkbox->is_pressed());
  432. _set_event(k, false);
  433. } break;
  434. case InputEventConfigurationDialog::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. mb->set_command_pressed(mod_checkboxes[MOD_COMMAND]->is_pressed());
  443. mb->set_ctrl_pressed(mod_checkboxes[MOD_CTRL]->is_pressed());
  444. mb->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
  445. mb->set_store_command(store_command_checkbox->is_pressed());
  446. // Maintain selected device
  447. mb->set_device(_get_current_device());
  448. _set_event(mb, false);
  449. } break;
  450. case InputEventConfigurationDialog::INPUT_JOY_BUTTON: {
  451. JoyButton idx = (JoyButton)(int)selected->get_meta("__index");
  452. Ref<InputEventJoypadButton> jb = InputEventJoypadButton::create_reference(idx);
  453. // Maintain selected device
  454. jb->set_device(_get_current_device());
  455. _set_event(jb, false);
  456. } break;
  457. case InputEventConfigurationDialog::INPUT_JOY_MOTION: {
  458. JoyAxis axis = (JoyAxis)(int)selected->get_meta("__axis");
  459. int value = selected->get_meta("__value");
  460. Ref<InputEventJoypadMotion> jm;
  461. jm.instantiate();
  462. jm->set_axis(axis);
  463. jm->set_axis_value(value);
  464. // Maintain selected device
  465. jm->set_device(_get_current_device());
  466. _set_event(jm, false);
  467. } break;
  468. }
  469. }
  470. void InputEventConfigurationDialog::_device_selection_changed(int p_option_button_index) {
  471. // Subtract 1 as option index 0 corresponds to "All Devices" (value of -1)
  472. // and option index 1 corresponds to device 0, etc...
  473. event->set_device(p_option_button_index - 1);
  474. event_as_text->set_text(get_event_text(event, true));
  475. }
  476. void InputEventConfigurationDialog::_set_current_device(int p_device) {
  477. device_id_option->select(p_device + 1);
  478. }
  479. int InputEventConfigurationDialog::_get_current_device() const {
  480. return device_id_option->get_selected() - 1;
  481. }
  482. String InputEventConfigurationDialog::_get_device_string(int p_device) const {
  483. if (p_device == InputMap::ALL_DEVICES) {
  484. return TTR("All Devices");
  485. }
  486. return TTR("Device") + " " + itos(p_device);
  487. }
  488. void InputEventConfigurationDialog::_notification(int p_what) {
  489. switch (p_what) {
  490. case NOTIFICATION_ENTER_TREE:
  491. case NOTIFICATION_THEME_CHANGED: {
  492. input_list_search->set_right_icon(input_list_search->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  493. physical_key_checkbox->set_icon(get_theme_icon(SNAME("KeyboardPhysical"), SNAME("EditorIcons")));
  494. icon_cache.keyboard = get_theme_icon(SNAME("Keyboard"), SNAME("EditorIcons"));
  495. icon_cache.mouse = get_theme_icon(SNAME("Mouse"), SNAME("EditorIcons"));
  496. icon_cache.joypad_button = get_theme_icon(SNAME("JoyButton"), SNAME("EditorIcons"));
  497. icon_cache.joypad_axis = get_theme_icon(SNAME("JoyAxis"), SNAME("EditorIcons"));
  498. _update_input_list();
  499. } break;
  500. }
  501. }
  502. void InputEventConfigurationDialog::popup_and_configure(const Ref<InputEvent> &p_event) {
  503. if (p_event.is_valid()) {
  504. _set_event(p_event);
  505. } else {
  506. // Clear Event
  507. _set_event(p_event);
  508. // Clear Checkbox Values
  509. for (int i = 0; i < MOD_MAX; i++) {
  510. mod_checkboxes[i]->set_pressed(false);
  511. }
  512. // Enable the Physical Key checkbox by default to encourage its use.
  513. // Physical Key should be used for most game inputs as it allows keys to work
  514. // on non-QWERTY layouts out of the box.
  515. // This is especially important for WASD movement layouts.
  516. physical_key_checkbox->set_pressed(true);
  517. store_command_checkbox->set_pressed(true);
  518. _set_current_device(0);
  519. // Switch to "Listen" tab
  520. tab_container->set_current_tab(0);
  521. // Select "All Devices" by default.
  522. device_id_option->select(0);
  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. }
  532. InputEventConfigurationDialog::InputEventConfigurationDialog() {
  533. allowed_input_types = INPUT_KEY | INPUT_MOUSE_BUTTON | INPUT_JOY_BUTTON | INPUT_JOY_MOTION | INPUT_MOUSE_BUTTON;
  534. set_title(TTR("Event Configuration"));
  535. set_min_size(Size2i(550 * EDSCALE, 0)); // Min width
  536. VBoxContainer *main_vbox = memnew(VBoxContainer);
  537. add_child(main_vbox);
  538. tab_container = memnew(TabContainer);
  539. tab_container->set_use_hidden_tabs_for_min_size(true);
  540. tab_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  541. tab_container->set_theme_type_variation("TabContainerOdd");
  542. tab_container->connect("tab_selected", callable_mp(this, &InputEventConfigurationDialog::_tab_selected));
  543. main_vbox->add_child(tab_container);
  544. // Listen to input tab
  545. VBoxContainer *vb = memnew(VBoxContainer);
  546. vb->set_name(TTR("Listen for Input"));
  547. event_as_text = memnew(Label);
  548. event_as_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  549. vb->add_child(event_as_text);
  550. // Mouse button detection rect (Mouse button event outside this rect will be ignored)
  551. mouse_detection_rect = memnew(Panel);
  552. mouse_detection_rect->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  553. vb->add_child(mouse_detection_rect);
  554. tab_container->add_child(vb);
  555. // List of all input options to manually select from.
  556. VBoxContainer *manual_vbox = memnew(VBoxContainer);
  557. manual_vbox->set_name(TTR("Manual Selection"));
  558. manual_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  559. tab_container->add_child(manual_vbox);
  560. input_list_search = memnew(LineEdit);
  561. input_list_search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  562. input_list_search->set_placeholder(TTR("Filter Inputs"));
  563. input_list_search->set_clear_button_enabled(true);
  564. input_list_search->connect("text_changed", callable_mp(this, &InputEventConfigurationDialog::_search_term_updated));
  565. manual_vbox->add_child(input_list_search);
  566. input_list_tree = memnew(Tree);
  567. input_list_tree->set_custom_minimum_size(Size2(0, 100 * EDSCALE)); // Min height for tree
  568. input_list_tree->connect("item_selected", callable_mp(this, &InputEventConfigurationDialog::_input_list_item_selected));
  569. input_list_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  570. manual_vbox->add_child(input_list_tree);
  571. input_list_tree->set_hide_root(true);
  572. input_list_tree->set_columns(1);
  573. _update_input_list();
  574. // Additional Options
  575. additional_options_container = memnew(VBoxContainer);
  576. additional_options_container->hide();
  577. Label *opts_label = memnew(Label);
  578. opts_label->set_theme_type_variation("HeaderSmall");
  579. opts_label->set_text(TTR("Additional Options"));
  580. additional_options_container->add_child(opts_label);
  581. // Device Selection
  582. device_container = memnew(HBoxContainer);
  583. device_container->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  584. Label *device_label = memnew(Label);
  585. device_label->set_theme_type_variation("HeaderSmall");
  586. device_label->set_text(TTR("Device:"));
  587. device_container->add_child(device_label);
  588. device_id_option = memnew(OptionButton);
  589. device_id_option->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  590. for (int i = -1; i < 8; i++) {
  591. device_id_option->add_item(_get_device_string(i));
  592. }
  593. device_id_option->connect("item_selected", callable_mp(this, &InputEventConfigurationDialog::_device_selection_changed));
  594. _set_current_device(InputMap::ALL_DEVICES);
  595. device_container->add_child(device_id_option);
  596. device_container->hide();
  597. additional_options_container->add_child(device_container);
  598. // Modifier Selection
  599. mod_container = memnew(HBoxContainer);
  600. for (int i = 0; i < MOD_MAX; i++) {
  601. String name = mods[i];
  602. mod_checkboxes[i] = memnew(CheckBox);
  603. mod_checkboxes[i]->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_mod_toggled).bind(i));
  604. mod_checkboxes[i]->set_text(name);
  605. mod_container->add_child(mod_checkboxes[i]);
  606. }
  607. mod_container->add_child(memnew(VSeparator));
  608. store_command_checkbox = memnew(CheckBox);
  609. store_command_checkbox->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_store_command_toggled));
  610. store_command_checkbox->set_pressed(true);
  611. store_command_checkbox->set_text(TTR("Store Command"));
  612. #ifdef APPLE_STYLE_KEYS
  613. store_command_checkbox->set_tooltip_text(TTR("Toggles between serializing 'command' and 'meta'. Used for compatibility with Windows/Linux style keyboard."));
  614. #else
  615. store_command_checkbox->set_tooltip_text(TTR("Toggles between serializing 'command' and 'control'. Used for compatibility with Apple Style keyboards."));
  616. #endif
  617. mod_container->add_child(store_command_checkbox);
  618. mod_container->hide();
  619. additional_options_container->add_child(mod_container);
  620. // Physical Key Checkbox
  621. physical_key_checkbox = memnew(CheckBox);
  622. physical_key_checkbox->set_text(TTR("Use Physical Keycode"));
  623. physical_key_checkbox->set_tooltip_text(TTR("Stores the physical position of the key on the keyboard rather than the key's value. Used for compatibility with non-latin layouts.\nThis should generally be enabled for most game shortcuts, but not in non-game applications."));
  624. physical_key_checkbox->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_physical_keycode_toggled));
  625. physical_key_checkbox->hide();
  626. additional_options_container->add_child(physical_key_checkbox);
  627. main_vbox->add_child(additional_options_container);
  628. // Default to first tab
  629. tab_container->set_current_tab(0);
  630. }
  631. /////////////////////////////////////////
  632. static bool _is_action_name_valid(const String &p_name) {
  633. const char32_t *cstr = p_name.get_data();
  634. for (int i = 0; cstr[i]; i++) {
  635. if (cstr[i] == '/' || cstr[i] == ':' || cstr[i] == '"' ||
  636. cstr[i] == '=' || cstr[i] == '\\' || cstr[i] < 32) {
  637. return false;
  638. }
  639. }
  640. return true;
  641. }
  642. void ActionMapEditor::_event_config_confirmed() {
  643. Ref<InputEvent> ev = event_config_dialog->get_event();
  644. Dictionary new_action = current_action.duplicate();
  645. Array events = new_action["events"].duplicate();
  646. if (current_action_event_index == -1) {
  647. // Add new event
  648. events.push_back(ev);
  649. } else {
  650. // Edit existing event
  651. events[current_action_event_index] = ev;
  652. }
  653. new_action["events"] = events;
  654. emit_signal(SNAME("action_edited"), current_action_name, new_action);
  655. }
  656. void ActionMapEditor::_add_action_pressed() {
  657. _add_action(add_edit->get_text());
  658. }
  659. String ActionMapEditor::_check_new_action_name(const String &p_name) {
  660. if (p_name.is_empty() || !_is_action_name_valid(p_name)) {
  661. return TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'");
  662. }
  663. if (_has_action(p_name)) {
  664. return vformat(TTR("An action with the name '%s' already exists."), p_name);
  665. }
  666. return "";
  667. }
  668. void ActionMapEditor::_add_edit_text_changed(const String &p_name) {
  669. String error = _check_new_action_name(p_name);
  670. add_button->set_tooltip_text(error);
  671. add_button->set_disabled(!error.is_empty());
  672. }
  673. bool ActionMapEditor::_has_action(const String &p_name) const {
  674. for (const ActionInfo &action_info : actions_cache) {
  675. if (p_name == action_info.name) {
  676. return true;
  677. }
  678. }
  679. return false;
  680. }
  681. void ActionMapEditor::_add_action(const String &p_name) {
  682. String error = _check_new_action_name(p_name);
  683. if (!error.is_empty()) {
  684. show_message(error);
  685. return;
  686. }
  687. add_edit->clear();
  688. emit_signal(SNAME("action_added"), p_name);
  689. }
  690. void ActionMapEditor::_action_edited() {
  691. TreeItem *ti = action_tree->get_edited();
  692. if (!ti) {
  693. return;
  694. }
  695. if (action_tree->get_selected_column() == 0) {
  696. // Name Edited
  697. String new_name = ti->get_text(0);
  698. String old_name = ti->get_meta("__name");
  699. if (new_name == old_name) {
  700. return;
  701. }
  702. if (new_name.is_empty() || !_is_action_name_valid(new_name)) {
  703. ti->set_text(0, old_name);
  704. show_message(TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'"));
  705. return;
  706. }
  707. if (_has_action(new_name)) {
  708. ti->set_text(0, old_name);
  709. show_message(vformat(TTR("An action with the name '%s' already exists."), new_name));
  710. return;
  711. }
  712. emit_signal(SNAME("action_renamed"), old_name, new_name);
  713. } else if (action_tree->get_selected_column() == 1) {
  714. // Deadzone Edited
  715. String name = ti->get_meta("__name");
  716. Dictionary old_action = ti->get_meta("__action");
  717. Dictionary new_action = old_action.duplicate();
  718. new_action["deadzone"] = ti->get_range(1);
  719. // Call deferred so that input can finish propagating through tree, allowing re-making of tree to occur.
  720. call_deferred(SNAME("emit_signal"), "action_edited", name, new_action);
  721. }
  722. }
  723. void ActionMapEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  724. if (p_button != MouseButton::LEFT) {
  725. return;
  726. }
  727. ItemButton option = (ItemButton)p_id;
  728. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  729. if (!item) {
  730. return;
  731. }
  732. switch (option) {
  733. case ActionMapEditor::BUTTON_ADD_EVENT: {
  734. current_action = item->get_meta("__action");
  735. current_action_name = item->get_meta("__name");
  736. current_action_event_index = -1;
  737. event_config_dialog->popup_and_configure();
  738. } break;
  739. case ActionMapEditor::BUTTON_EDIT_EVENT: {
  740. // Action and Action name is located on the parent of the event.
  741. current_action = item->get_parent()->get_meta("__action");
  742. current_action_name = item->get_parent()->get_meta("__name");
  743. current_action_event_index = item->get_meta("__index");
  744. Ref<InputEvent> ie = item->get_meta("__event");
  745. if (ie.is_valid()) {
  746. event_config_dialog->popup_and_configure(ie);
  747. }
  748. } break;
  749. case ActionMapEditor::BUTTON_REMOVE_ACTION: {
  750. // Send removed action name
  751. String name = item->get_meta("__name");
  752. emit_signal(SNAME("action_removed"), name);
  753. } break;
  754. case ActionMapEditor::BUTTON_REMOVE_EVENT: {
  755. // Remove event and send updated action
  756. Dictionary action = item->get_parent()->get_meta("__action").duplicate();
  757. String action_name = item->get_parent()->get_meta("__name");
  758. int event_index = item->get_meta("__index");
  759. Array events = action["events"].duplicate();
  760. events.remove_at(event_index);
  761. action["events"] = events;
  762. emit_signal(SNAME("action_edited"), action_name, action);
  763. } break;
  764. default:
  765. break;
  766. }
  767. }
  768. void ActionMapEditor::_tree_item_activated() {
  769. TreeItem *item = action_tree->get_selected();
  770. if (!item || !item->has_meta("__event")) {
  771. return;
  772. }
  773. _tree_button_pressed(item, 2, BUTTON_EDIT_EVENT, MouseButton::LEFT);
  774. }
  775. void ActionMapEditor::set_show_builtin_actions(bool p_show) {
  776. show_builtin_actions = p_show;
  777. show_builtin_actions_checkbutton->set_pressed(p_show);
  778. // Prevent unnecessary updates of action list when cache is empty.
  779. if (!actions_cache.is_empty()) {
  780. update_action_list();
  781. }
  782. }
  783. void ActionMapEditor::_search_term_updated(const String &) {
  784. update_action_list();
  785. }
  786. Variant ActionMapEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  787. TreeItem *selected = action_tree->get_selected();
  788. if (!selected) {
  789. return Variant();
  790. }
  791. String name = selected->get_text(0);
  792. Label *label = memnew(Label(name));
  793. label->set_theme_type_variation("HeaderSmall");
  794. label->set_modulate(Color(1, 1, 1, 1.0f));
  795. action_tree->set_drag_preview(label);
  796. Dictionary drag_data;
  797. if (selected->has_meta("__action")) {
  798. drag_data["input_type"] = "action";
  799. }
  800. if (selected->has_meta("__event")) {
  801. drag_data["input_type"] = "event";
  802. }
  803. action_tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  804. return drag_data;
  805. }
  806. bool ActionMapEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  807. Dictionary d = p_data;
  808. if (!d.has("input_type")) {
  809. return false;
  810. }
  811. TreeItem *selected = action_tree->get_selected();
  812. TreeItem *item = action_tree->get_item_at_position(p_point);
  813. if (!selected || !item || item == selected) {
  814. return false;
  815. }
  816. // Don't allow moving an action in-between events.
  817. if (d["input_type"] == "action" && item->has_meta("__event")) {
  818. return false;
  819. }
  820. // Don't allow moving an event to a different action.
  821. if (d["input_type"] == "event" && item->get_parent() != selected->get_parent()) {
  822. return false;
  823. }
  824. return true;
  825. }
  826. void ActionMapEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  827. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  828. return;
  829. }
  830. TreeItem *selected = action_tree->get_selected();
  831. TreeItem *target = action_tree->get_item_at_position(p_point);
  832. bool drop_above = action_tree->get_drop_section_at_position(p_point) == -1;
  833. if (!target) {
  834. return;
  835. }
  836. Dictionary d = p_data;
  837. if (d["input_type"] == "action") {
  838. // Change action order.
  839. String relative_to = target->get_meta("__name");
  840. String action_name = selected->get_meta("__name");
  841. emit_signal(SNAME("action_reordered"), action_name, relative_to, drop_above);
  842. } else if (d["input_type"] == "event") {
  843. // Change event order
  844. int current_index = selected->get_meta("__index");
  845. int target_index = target->get_meta("__index");
  846. // Construct new events array.
  847. Dictionary new_action = selected->get_parent()->get_meta("__action");
  848. Array events = new_action["events"];
  849. Array new_events;
  850. // The following method was used to perform the array changes since `remove` followed by `insert` was not working properly at time of writing.
  851. // Loop thought existing events
  852. for (int i = 0; i < events.size(); i++) {
  853. // If you come across the current index, just skip it, as it has been moved.
  854. if (i == current_index) {
  855. continue;
  856. } else if (i == target_index) {
  857. // We are at the target index. If drop above, add selected event there first, then target, so moved event goes on top.
  858. if (drop_above) {
  859. new_events.push_back(events[current_index]);
  860. new_events.push_back(events[target_index]);
  861. } else {
  862. new_events.push_back(events[target_index]);
  863. new_events.push_back(events[current_index]);
  864. }
  865. } else {
  866. new_events.push_back(events[i]);
  867. }
  868. }
  869. new_action["events"] = new_events;
  870. emit_signal(SNAME("action_edited"), selected->get_parent()->get_meta("__name"), new_action);
  871. }
  872. }
  873. void ActionMapEditor::_notification(int p_what) {
  874. switch (p_what) {
  875. case NOTIFICATION_ENTER_TREE:
  876. case NOTIFICATION_THEME_CHANGED: {
  877. action_list_search->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  878. if (!actions_cache.is_empty()) {
  879. update_action_list();
  880. }
  881. } break;
  882. }
  883. }
  884. void ActionMapEditor::_bind_methods() {
  885. ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &ActionMapEditor::get_drag_data_fw);
  886. ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &ActionMapEditor::can_drop_data_fw);
  887. ClassDB::bind_method(D_METHOD("_drop_data_fw"), &ActionMapEditor::drop_data_fw);
  888. ADD_SIGNAL(MethodInfo("action_added", PropertyInfo(Variant::STRING, "name")));
  889. ADD_SIGNAL(MethodInfo("action_edited", PropertyInfo(Variant::STRING, "name"), PropertyInfo(Variant::DICTIONARY, "new_action")));
  890. ADD_SIGNAL(MethodInfo("action_removed", PropertyInfo(Variant::STRING, "name")));
  891. ADD_SIGNAL(MethodInfo("action_renamed", PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name")));
  892. ADD_SIGNAL(MethodInfo("action_reordered", PropertyInfo(Variant::STRING, "action_name"), PropertyInfo(Variant::STRING, "relative_to"), PropertyInfo(Variant::BOOL, "before")));
  893. }
  894. LineEdit *ActionMapEditor::get_search_box() const {
  895. return action_list_search;
  896. }
  897. InputEventConfigurationDialog *ActionMapEditor::get_configuration_dialog() {
  898. return event_config_dialog;
  899. }
  900. void ActionMapEditor::update_action_list(const Vector<ActionInfo> &p_action_infos) {
  901. if (!p_action_infos.is_empty()) {
  902. actions_cache = p_action_infos;
  903. }
  904. action_tree->clear();
  905. TreeItem *root = action_tree->create_item();
  906. int uneditable_count = 0;
  907. for (int i = 0; i < actions_cache.size(); i++) {
  908. ActionInfo action_info = actions_cache[i];
  909. if (!action_info.editable) {
  910. uneditable_count++;
  911. }
  912. String search_term = action_list_search->get_text();
  913. if (!search_term.is_empty() && action_info.name.findn(search_term) == -1) {
  914. continue;
  915. }
  916. if (!action_info.editable && !show_builtin_actions) {
  917. continue;
  918. }
  919. const Array events = action_info.action["events"];
  920. const Variant deadzone = action_info.action["deadzone"];
  921. // Update Tree...
  922. TreeItem *action_item = action_tree->create_item(root);
  923. action_item->set_meta("__action", action_info.action);
  924. action_item->set_meta("__name", action_info.name);
  925. // First Column - Action Name
  926. action_item->set_text(0, action_info.name);
  927. action_item->set_editable(0, action_info.editable);
  928. action_item->set_icon(0, action_info.icon);
  929. // Second Column - Deadzone
  930. action_item->set_editable(1, true);
  931. action_item->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
  932. action_item->set_range_config(1, 0.0, 1.0, 0.01);
  933. action_item->set_range(1, deadzone);
  934. // Third column - buttons
  935. action_item->add_button(2, action_tree->get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), BUTTON_ADD_EVENT, false, TTR("Add Event"));
  936. action_item->add_button(2, action_tree->get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), BUTTON_REMOVE_ACTION, !action_info.editable, action_info.editable ? TTR("Remove Action") : TTR("Cannot Remove Action"));
  937. action_item->set_custom_bg_color(0, action_tree->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  938. action_item->set_custom_bg_color(1, action_tree->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  939. for (int evnt_idx = 0; evnt_idx < events.size(); evnt_idx++) {
  940. Ref<InputEvent> event = events[evnt_idx];
  941. if (event.is_null()) {
  942. continue;
  943. }
  944. TreeItem *event_item = action_tree->create_item(action_item);
  945. // First Column - Text
  946. event_item->set_text(0, event_config_dialog->get_event_text(event, true));
  947. event_item->set_meta("__event", event);
  948. event_item->set_meta("__index", evnt_idx);
  949. // First Column - Icon
  950. Ref<InputEventKey> k = event;
  951. if (k.is_valid()) {
  952. if (k->get_physical_keycode() == Key::NONE) {
  953. event_item->set_icon(0, action_tree->get_theme_icon(SNAME("Keyboard"), SNAME("EditorIcons")));
  954. } else {
  955. event_item->set_icon(0, action_tree->get_theme_icon(SNAME("KeyboardPhysical"), SNAME("EditorIcons")));
  956. }
  957. }
  958. Ref<InputEventMouseButton> mb = event;
  959. if (mb.is_valid()) {
  960. event_item->set_icon(0, action_tree->get_theme_icon(SNAME("Mouse"), SNAME("EditorIcons")));
  961. }
  962. Ref<InputEventJoypadButton> jb = event;
  963. if (jb.is_valid()) {
  964. event_item->set_icon(0, action_tree->get_theme_icon(SNAME("JoyButton"), SNAME("EditorIcons")));
  965. }
  966. Ref<InputEventJoypadMotion> jm = event;
  967. if (jm.is_valid()) {
  968. event_item->set_icon(0, action_tree->get_theme_icon(SNAME("JoyAxis"), SNAME("EditorIcons")));
  969. }
  970. // Third Column - Buttons
  971. event_item->add_button(2, action_tree->get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")), BUTTON_EDIT_EVENT, false, TTR("Edit Event"));
  972. event_item->add_button(2, action_tree->get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), BUTTON_REMOVE_EVENT, false, TTR("Remove Event"));
  973. event_item->set_button_color(2, 0, Color(1, 1, 1, 0.75));
  974. event_item->set_button_color(2, 1, Color(1, 1, 1, 0.75));
  975. }
  976. }
  977. }
  978. void ActionMapEditor::show_message(const String &p_message) {
  979. message->set_text(p_message);
  980. message->popup_centered();
  981. }
  982. void ActionMapEditor::use_external_search_box(LineEdit *p_searchbox) {
  983. memdelete(action_list_search);
  984. action_list_search = p_searchbox;
  985. action_list_search->connect("text_changed", callable_mp(this, &ActionMapEditor::_search_term_updated));
  986. }
  987. ActionMapEditor::ActionMapEditor() {
  988. // Main Vbox Container
  989. VBoxContainer *main_vbox = memnew(VBoxContainer);
  990. main_vbox->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  991. add_child(main_vbox);
  992. HBoxContainer *top_hbox = memnew(HBoxContainer);
  993. main_vbox->add_child(top_hbox);
  994. action_list_search = memnew(LineEdit);
  995. action_list_search->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  996. action_list_search->set_placeholder(TTR("Filter Actions"));
  997. action_list_search->set_clear_button_enabled(true);
  998. action_list_search->connect("text_changed", callable_mp(this, &ActionMapEditor::_search_term_updated));
  999. top_hbox->add_child(action_list_search);
  1000. show_builtin_actions_checkbutton = memnew(CheckButton);
  1001. show_builtin_actions_checkbutton->set_pressed(false);
  1002. show_builtin_actions_checkbutton->set_text(TTR("Show Built-in Actions"));
  1003. show_builtin_actions_checkbutton->connect("toggled", callable_mp(this, &ActionMapEditor::set_show_builtin_actions));
  1004. top_hbox->add_child(show_builtin_actions_checkbutton);
  1005. // Adding Action line edit + button
  1006. add_hbox = memnew(HBoxContainer);
  1007. add_hbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1008. add_edit = memnew(LineEdit);
  1009. add_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1010. add_edit->set_placeholder(TTR("Add New Action"));
  1011. add_edit->set_clear_button_enabled(true);
  1012. add_edit->connect("text_changed", callable_mp(this, &ActionMapEditor::_add_edit_text_changed));
  1013. add_edit->connect("text_submitted", callable_mp(this, &ActionMapEditor::_add_action));
  1014. add_hbox->add_child(add_edit);
  1015. add_button = memnew(Button);
  1016. add_button->set_text(TTR("Add"));
  1017. add_button->connect("pressed", callable_mp(this, &ActionMapEditor::_add_action_pressed));
  1018. add_hbox->add_child(add_button);
  1019. // Disable the button and set its tooltip.
  1020. _add_edit_text_changed(add_edit->get_text());
  1021. main_vbox->add_child(add_hbox);
  1022. // Action Editor Tree
  1023. action_tree = memnew(Tree);
  1024. action_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1025. action_tree->set_columns(3);
  1026. action_tree->set_hide_root(true);
  1027. action_tree->set_column_titles_visible(true);
  1028. action_tree->set_column_title(0, TTR("Action"));
  1029. action_tree->set_column_clip_content(0, true);
  1030. action_tree->set_column_title(1, TTR("Deadzone"));
  1031. action_tree->set_column_expand(1, false);
  1032. action_tree->set_column_custom_minimum_width(1, 80 * EDSCALE);
  1033. action_tree->set_column_expand(2, false);
  1034. action_tree->set_column_custom_minimum_width(2, 50 * EDSCALE);
  1035. action_tree->connect("item_edited", callable_mp(this, &ActionMapEditor::_action_edited));
  1036. action_tree->connect("item_activated", callable_mp(this, &ActionMapEditor::_tree_item_activated));
  1037. action_tree->connect("button_clicked", callable_mp(this, &ActionMapEditor::_tree_button_pressed));
  1038. main_vbox->add_child(action_tree);
  1039. action_tree->set_drag_forwarding(this);
  1040. // Adding event dialog
  1041. event_config_dialog = memnew(InputEventConfigurationDialog);
  1042. event_config_dialog->connect("confirmed", callable_mp(this, &ActionMapEditor::_event_config_confirmed));
  1043. add_child(event_config_dialog);
  1044. message = memnew(AcceptDialog);
  1045. add_child(message);
  1046. }