2
0

action_map_editor.cpp 40 KB

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