action_map_editor.cpp 43 KB

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