connections_dialog.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  1. /**************************************************************************/
  2. /* connections_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "connections_dialog.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/templates/hash_set.h"
  33. #include "editor/editor_help.h"
  34. #include "editor/editor_inspector.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/scene_tree_editor.h"
  41. #include "editor/node_dock.h"
  42. #include "editor/scene_tree_dock.h"
  43. #include "plugins/script_editor_plugin.h"
  44. #include "scene/gui/button.h"
  45. #include "scene/gui/check_box.h"
  46. #include "scene/gui/label.h"
  47. #include "scene/gui/line_edit.h"
  48. #include "scene/gui/option_button.h"
  49. #include "scene/gui/popup_menu.h"
  50. #include "scene/gui/spin_box.h"
  51. #include "scene/resources/packed_scene.h"
  52. static Node *_find_first_script(Node *p_root, Node *p_node) {
  53. if (p_node != p_root && p_node->get_owner() != p_root) {
  54. return nullptr;
  55. }
  56. if (!p_node->get_script().is_null()) {
  57. return p_node;
  58. }
  59. for (int i = 0; i < p_node->get_child_count(); i++) {
  60. Node *ret = _find_first_script(p_root, p_node->get_child(i));
  61. if (ret) {
  62. return ret;
  63. }
  64. }
  65. return nullptr;
  66. }
  67. class ConnectDialogBinds : public Object {
  68. GDCLASS(ConnectDialogBinds, Object);
  69. public:
  70. Vector<Variant> params;
  71. bool _set(const StringName &p_name, const Variant &p_value) {
  72. String name = p_name;
  73. if (name.begins_with("bind/argument_")) {
  74. int which = name.get_slice("_", 1).to_int() - 1;
  75. ERR_FAIL_INDEX_V(which, params.size(), false);
  76. params.write[which] = p_value;
  77. } else {
  78. return false;
  79. }
  80. return true;
  81. }
  82. bool _get(const StringName &p_name, Variant &r_ret) const {
  83. String name = p_name;
  84. if (name.begins_with("bind/argument_")) {
  85. int which = name.get_slice("_", 1).to_int() - 1;
  86. ERR_FAIL_INDEX_V(which, params.size(), false);
  87. r_ret = params[which];
  88. } else {
  89. return false;
  90. }
  91. return true;
  92. }
  93. void _get_property_list(List<PropertyInfo> *p_list) const {
  94. for (int i = 0; i < params.size(); i++) {
  95. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));
  96. }
  97. }
  98. void notify_changed() {
  99. notify_property_list_changed();
  100. }
  101. ConnectDialogBinds() {
  102. }
  103. };
  104. /*
  105. * Signal automatically called by parent dialog.
  106. */
  107. void ConnectDialog::ok_pressed() {
  108. String method_name = dst_method->get_text();
  109. if (method_name.is_empty()) {
  110. error->set_text(TTR("Method in target node must be specified."));
  111. error->popup_centered();
  112. return;
  113. }
  114. if (!TS->is_valid_identifier(method_name.strip_edges())) {
  115. error->set_text(TTR("Method name must be a valid identifier."));
  116. error->popup_centered();
  117. return;
  118. }
  119. Node *target = tree->get_selected();
  120. if (!target) {
  121. return; // Nothing selected in the tree, not an error.
  122. }
  123. if (target->get_script().is_null()) {
  124. if (!target->has_method(method_name)) {
  125. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  126. error->popup_centered();
  127. return;
  128. }
  129. }
  130. emit_signal(SNAME("connected"));
  131. hide();
  132. }
  133. void ConnectDialog::_cancel_pressed() {
  134. hide();
  135. }
  136. void ConnectDialog::_item_activated() {
  137. _ok_pressed(); // From AcceptDialog.
  138. }
  139. void ConnectDialog::_text_submitted(const String &p_text) {
  140. _ok_pressed(); // From AcceptDialog.
  141. }
  142. /*
  143. * Called each time a target node is selected within the target node tree.
  144. */
  145. void ConnectDialog::_tree_node_selected() {
  146. Node *current = tree->get_selected();
  147. if (!current) {
  148. return;
  149. }
  150. dst_path = source->get_path_to(current);
  151. if (!edit_mode) {
  152. set_dst_method(generate_method_callback_name(source, signal, current));
  153. }
  154. _update_method_tree();
  155. _update_ok_enabled();
  156. }
  157. void ConnectDialog::_focus_currently_connected() {
  158. tree->set_selected(source);
  159. }
  160. void ConnectDialog::_unbind_count_changed(double p_count) {
  161. for (Control *control : bind_controls) {
  162. BaseButton *b = Object::cast_to<BaseButton>(control);
  163. if (b) {
  164. b->set_disabled(p_count > 0);
  165. }
  166. EditorInspector *e = Object::cast_to<EditorInspector>(control);
  167. if (e) {
  168. e->set_read_only(p_count > 0);
  169. }
  170. }
  171. }
  172. void ConnectDialog::_method_selected() {
  173. TreeItem *selected_item = method_tree->get_selected();
  174. dst_method->set_text(selected_item->get_metadata(0));
  175. }
  176. /*
  177. * Adds a new parameter bind to connection.
  178. */
  179. void ConnectDialog::_add_bind() {
  180. Variant::Type type = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  181. Variant value;
  182. Callable::CallError err;
  183. Variant::construct(type, value, nullptr, 0, err);
  184. cdbinds->params.push_back(value);
  185. cdbinds->notify_changed();
  186. }
  187. /*
  188. * Remove parameter bind from connection.
  189. */
  190. void ConnectDialog::_remove_bind() {
  191. String st = bind_editor->get_selected_path();
  192. if (st.is_empty()) {
  193. return;
  194. }
  195. int idx = st.get_slice("/", 1).to_int() - 1;
  196. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  197. cdbinds->params.remove_at(idx);
  198. cdbinds->notify_changed();
  199. }
  200. /*
  201. * Automatically generates a name for the callback method.
  202. */
  203. StringName ConnectDialog::generate_method_callback_name(Node *p_source, String p_signal_name, Node *p_target) {
  204. String node_name = p_source->get_name();
  205. for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.
  206. char32_t c = node_name[i];
  207. if ((i == 0 && !is_unicode_identifier_start(c)) || (i > 0 && !is_unicode_identifier_continue(c))) {
  208. if (c == ' ') {
  209. // Replace spaces with underlines.
  210. c = '_';
  211. } else {
  212. // Remove any other characters.
  213. node_name.remove_at(i);
  214. i--;
  215. continue;
  216. }
  217. }
  218. node_name[i] = c;
  219. }
  220. Dictionary subst;
  221. subst["NodeName"] = node_name.to_pascal_case();
  222. subst["nodeName"] = node_name.to_camel_case();
  223. subst["node_name"] = node_name.to_snake_case();
  224. subst["SignalName"] = p_signal_name.to_pascal_case();
  225. subst["signalName"] = p_signal_name.to_camel_case();
  226. subst["signal_name"] = p_signal_name.to_snake_case();
  227. String dst_method;
  228. if (p_source == p_target) {
  229. dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_to_self_name")).format(subst);
  230. } else {
  231. dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_name")).format(subst);
  232. }
  233. return dst_method;
  234. }
  235. void ConnectDialog::_create_method_tree_items(const List<MethodInfo> &p_methods, TreeItem *p_parent_item) {
  236. for (const MethodInfo &mi : p_methods) {
  237. TreeItem *method_item = method_tree->create_item(p_parent_item);
  238. method_item->set_text(0, get_signature(mi));
  239. method_item->set_metadata(0, mi.name);
  240. }
  241. }
  242. List<MethodInfo> ConnectDialog::_filter_method_list(const List<MethodInfo> &p_methods, const MethodInfo &p_signal, const String &p_search_string) const {
  243. bool check_signal = compatible_methods_only->is_pressed();
  244. List<MethodInfo> ret;
  245. for (const MethodInfo &mi : p_methods) {
  246. if (!p_search_string.is_empty() && !mi.name.contains(p_search_string)) {
  247. continue;
  248. }
  249. if (check_signal) {
  250. if (mi.arguments.size() != p_signal.arguments.size()) {
  251. continue;
  252. }
  253. bool type_mismatch = false;
  254. const List<PropertyInfo>::Element *E = p_signal.arguments.front();
  255. for (const List<PropertyInfo>::Element *F = mi.arguments.front(); F; F = F->next(), E = E->next()) {
  256. Variant::Type stype = E->get().type;
  257. Variant::Type mtype = F->get().type;
  258. if (stype != Variant::NIL && mtype != Variant::NIL && stype != mtype) {
  259. type_mismatch = true;
  260. break;
  261. }
  262. if (stype == Variant::OBJECT && mtype == Variant::OBJECT && !ClassDB::is_parent_class(E->get().class_name, F->get().class_name)) {
  263. type_mismatch = true;
  264. break;
  265. }
  266. }
  267. if (type_mismatch) {
  268. continue;
  269. }
  270. }
  271. ret.push_back(mi);
  272. }
  273. return ret;
  274. }
  275. void ConnectDialog::_update_method_tree() {
  276. method_tree->clear();
  277. Color disabled_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)) * 0.7;
  278. String search_string = method_search->get_text();
  279. Node *target = tree->get_selected();
  280. if (!target) {
  281. return;
  282. }
  283. MethodInfo signal_info;
  284. if (compatible_methods_only->is_pressed()) {
  285. List<MethodInfo> signals;
  286. source->get_signal_list(&signals);
  287. for (const MethodInfo &mi : signals) {
  288. if (mi.name == signal) {
  289. signal_info = mi;
  290. break;
  291. }
  292. }
  293. }
  294. TreeItem *root_item = method_tree->create_item();
  295. root_item->set_text(0, TTR("Methods"));
  296. root_item->set_selectable(0, false);
  297. // If a script is attached, get methods from it.
  298. ScriptInstance *si = target->get_script_instance();
  299. if (si) {
  300. if (si->get_script()->is_built_in()) {
  301. si->get_script()->reload();
  302. }
  303. List<MethodInfo> methods;
  304. si->get_method_list(&methods);
  305. methods = _filter_method_list(methods, signal_info, search_string);
  306. if (!methods.is_empty()) {
  307. TreeItem *si_item = method_tree->create_item(root_item);
  308. si_item->set_text(0, TTR("Attached Script"));
  309. si_item->set_icon(0, get_editor_theme_icon(SNAME("Script")));
  310. si_item->set_selectable(0, false);
  311. _create_method_tree_items(methods, si_item);
  312. }
  313. }
  314. if (script_methods_only->is_pressed()) {
  315. empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
  316. return;
  317. }
  318. // Get methods from each class in the hierarchy.
  319. StringName current_class = target->get_class_name();
  320. do {
  321. TreeItem *class_item = method_tree->create_item(root_item);
  322. class_item->set_text(0, current_class);
  323. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Node"));
  324. if (has_theme_icon(current_class, EditorStringName(EditorIcons))) {
  325. icon = get_editor_theme_icon(current_class);
  326. }
  327. class_item->set_icon(0, icon);
  328. class_item->set_selectable(0, false);
  329. List<MethodInfo> methods;
  330. ClassDB::get_method_list(current_class, &methods, true);
  331. methods = _filter_method_list(methods, signal_info, search_string);
  332. if (methods.is_empty()) {
  333. class_item->set_custom_color(0, disabled_color);
  334. } else {
  335. _create_method_tree_items(methods, class_item);
  336. }
  337. current_class = ClassDB::get_parent_class_nocheck(current_class);
  338. } while (current_class != StringName());
  339. empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
  340. }
  341. void ConnectDialog::_method_check_button_pressed(const CheckButton *p_button) {
  342. if (p_button == script_methods_only) {
  343. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_script_methods_only", p_button->is_pressed());
  344. } else if (p_button == compatible_methods_only) {
  345. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_compatible_methods_only", p_button->is_pressed());
  346. }
  347. _update_method_tree();
  348. }
  349. void ConnectDialog::_open_method_popup() {
  350. method_popup->popup_centered();
  351. method_search->clear();
  352. method_search->grab_focus();
  353. }
  354. /*
  355. * Enables or disables the connect button. The connect button is enabled if a
  356. * node is selected and valid in the selected mode.
  357. */
  358. void ConnectDialog::_update_ok_enabled() {
  359. Node *target = tree->get_selected();
  360. if (target == nullptr) {
  361. get_ok_button()->set_disabled(true);
  362. return;
  363. }
  364. if (dst_method->get_text().is_empty()) {
  365. get_ok_button()->set_disabled(true);
  366. return;
  367. }
  368. get_ok_button()->set_disabled(false);
  369. }
  370. void ConnectDialog::_notification(int p_what) {
  371. switch (p_what) {
  372. case NOTIFICATION_ENTER_TREE: {
  373. bind_editor->edit(cdbinds);
  374. [[fallthrough]];
  375. }
  376. case NOTIFICATION_THEME_CHANGED: {
  377. for (int i = 0; i < type_list->get_item_count(); i++) {
  378. String type_name = Variant::get_type_name((Variant::Type)type_list->get_item_id(i));
  379. type_list->set_item_icon(i, get_editor_theme_icon(type_name));
  380. }
  381. Ref<StyleBox> style = get_theme_stylebox("normal", "LineEdit")->duplicate();
  382. if (style.is_valid()) {
  383. style->set_content_margin(SIDE_TOP, style->get_content_margin(SIDE_TOP) + 1.0);
  384. from_signal->add_theme_style_override("normal", style);
  385. }
  386. method_search->set_right_icon(get_editor_theme_icon("Search"));
  387. open_method_tree->set_icon(get_editor_theme_icon("Edit"));
  388. } break;
  389. }
  390. }
  391. void ConnectDialog::_bind_methods() {
  392. ADD_SIGNAL(MethodInfo("connected"));
  393. }
  394. Node *ConnectDialog::get_source() const {
  395. return source;
  396. }
  397. ConnectDialog::ConnectionData ConnectDialog::get_source_connection_data() const {
  398. return source_connection_data;
  399. }
  400. StringName ConnectDialog::get_signal_name() const {
  401. return signal;
  402. }
  403. PackedStringArray ConnectDialog::get_signal_args() const {
  404. return signal_args;
  405. }
  406. NodePath ConnectDialog::get_dst_path() const {
  407. return dst_path;
  408. }
  409. void ConnectDialog::set_dst_node(Node *p_node) {
  410. tree->set_selected(p_node);
  411. }
  412. StringName ConnectDialog::get_dst_method_name() const {
  413. String txt = dst_method->get_text();
  414. if (txt.contains("(")) {
  415. txt = txt.left(txt.find("(")).strip_edges();
  416. }
  417. return txt;
  418. }
  419. void ConnectDialog::set_dst_method(const StringName &p_method) {
  420. dst_method->set_text(p_method);
  421. }
  422. int ConnectDialog::get_unbinds() const {
  423. return int(unbind_count->get_value());
  424. }
  425. Vector<Variant> ConnectDialog::get_binds() const {
  426. return cdbinds->params;
  427. }
  428. String ConnectDialog::get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names) {
  429. PackedStringArray signature;
  430. signature.append(p_method.name);
  431. signature.append("(");
  432. for (int i = 0; i < p_method.arguments.size(); i++) {
  433. if (i > 0) {
  434. signature.append(", ");
  435. }
  436. const PropertyInfo &pi = p_method.arguments[i];
  437. String type_name;
  438. switch (pi.type) {
  439. case Variant::NIL:
  440. type_name = "Variant";
  441. break;
  442. case Variant::INT:
  443. if ((pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && pi.class_name != StringName() && !String(pi.class_name).begins_with("res://")) {
  444. type_name = pi.class_name;
  445. } else {
  446. type_name = "int";
  447. }
  448. break;
  449. case Variant::ARRAY:
  450. if (pi.hint == PROPERTY_HINT_ARRAY_TYPE && !pi.hint_string.is_empty() && !pi.hint_string.begins_with("res://")) {
  451. type_name = "Array[" + pi.hint_string + "]";
  452. } else {
  453. type_name = "Array";
  454. }
  455. break;
  456. case Variant::OBJECT:
  457. if (pi.class_name != StringName()) {
  458. type_name = pi.class_name;
  459. } else {
  460. type_name = "Object";
  461. }
  462. break;
  463. default:
  464. type_name = Variant::get_type_name(pi.type);
  465. break;
  466. }
  467. String arg_name = pi.name.is_empty() ? "arg" + itos(i) : pi.name;
  468. signature.append(arg_name + ": " + type_name);
  469. if (r_arg_names) {
  470. r_arg_names->push_back(arg_name + ":" + type_name);
  471. }
  472. }
  473. signature.append(")");
  474. return String().join(signature);
  475. }
  476. bool ConnectDialog::get_deferred() const {
  477. return deferred->is_pressed();
  478. }
  479. bool ConnectDialog::get_one_shot() const {
  480. return one_shot->is_pressed();
  481. }
  482. /*
  483. * Returns true if ConnectDialog is being used to edit an existing connection.
  484. */
  485. bool ConnectDialog::is_editing() const {
  486. return edit_mode;
  487. }
  488. /*
  489. * Initialize ConnectDialog and populate fields with expected data.
  490. * If creating a connection from scratch, sensible defaults are used.
  491. * If editing an existing connection, previous data is retained.
  492. */
  493. void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit) {
  494. set_hide_on_ok(false);
  495. source = static_cast<Node *>(p_cd.source);
  496. signal = p_cd.signal;
  497. signal_args = p_signal_args;
  498. tree->set_selected(nullptr);
  499. tree->set_marked(source, true);
  500. if (p_cd.target) {
  501. set_dst_node(static_cast<Node *>(p_cd.target));
  502. set_dst_method(p_cd.method);
  503. }
  504. _update_ok_enabled();
  505. bool b_deferred = (p_cd.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  506. bool b_oneshot = (p_cd.flags & CONNECT_ONE_SHOT) == CONNECT_ONE_SHOT;
  507. deferred->set_pressed(b_deferred);
  508. one_shot->set_pressed(b_oneshot);
  509. unbind_count->set_max(p_signal_args.size());
  510. unbind_count->set_value(p_cd.unbinds);
  511. _unbind_count_changed(p_cd.unbinds);
  512. cdbinds->params.clear();
  513. cdbinds->params = p_cd.binds;
  514. cdbinds->notify_changed();
  515. edit_mode = p_edit;
  516. source_connection_data = p_cd;
  517. }
  518. void ConnectDialog::popup_dialog(const String p_for_signal) {
  519. from_signal->set_text(p_for_signal);
  520. error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  521. filter_nodes->clear();
  522. if (!advanced->is_pressed()) {
  523. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  524. }
  525. if (first_popup) {
  526. first_popup = false;
  527. _advanced_pressed();
  528. }
  529. popup_centered();
  530. }
  531. void ConnectDialog::_advanced_pressed() {
  532. if (advanced->is_pressed()) {
  533. connect_to_label->set_text(TTR("Connect to Node:"));
  534. tree->set_connect_to_script_mode(false);
  535. vbc_right->show();
  536. error_label->hide();
  537. } else {
  538. reset_size();
  539. connect_to_label->set_text(TTR("Connect to Script:"));
  540. tree->set_connect_to_script_mode(true);
  541. vbc_right->hide();
  542. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  543. }
  544. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "use_advanced_connections", advanced->is_pressed());
  545. popup_centered();
  546. }
  547. ConnectDialog::ConnectDialog() {
  548. set_min_size(Size2(0, 500) * EDSCALE);
  549. HBoxContainer *main_hb = memnew(HBoxContainer);
  550. add_child(main_hb);
  551. VBoxContainer *vbc_left = memnew(VBoxContainer);
  552. main_hb->add_child(vbc_left);
  553. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  554. vbc_left->set_custom_minimum_size(Vector2(400 * EDSCALE, 0));
  555. from_signal = memnew(LineEdit);
  556. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  557. from_signal->set_editable(false);
  558. tree = memnew(SceneTreeEditor(false));
  559. tree->set_connecting_signal(true);
  560. tree->set_show_enabled_subscene(true);
  561. tree->set_v_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
  562. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  563. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  564. tree->set_connect_to_script_mode(true);
  565. HBoxContainer *hbc_filter = memnew(HBoxContainer);
  566. filter_nodes = memnew(LineEdit);
  567. hbc_filter->add_child(filter_nodes);
  568. filter_nodes->set_h_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
  569. filter_nodes->set_placeholder(TTR("Filter Nodes"));
  570. filter_nodes->set_clear_button_enabled(true);
  571. filter_nodes->connect("text_changed", callable_mp(tree, &SceneTreeEditor::set_filter));
  572. Button *focus_current = memnew(Button);
  573. hbc_filter->add_child(focus_current);
  574. focus_current->set_text(TTR("Go to Source"));
  575. focus_current->connect("pressed", callable_mp(this, &ConnectDialog::_focus_currently_connected));
  576. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), hbc_filter, false);
  577. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  578. vbc_left->add_child(tree);
  579. error_label = memnew(Label);
  580. error_label->set_text(TTR("Scene does not contain any script."));
  581. vbc_left->add_child(error_label);
  582. error_label->hide();
  583. method_popup = memnew(AcceptDialog);
  584. method_popup->set_title(TTR("Select Method"));
  585. method_popup->set_min_size(Vector2(400, 600) * EDSCALE);
  586. add_child(method_popup);
  587. VBoxContainer *method_vbc = memnew(VBoxContainer);
  588. method_popup->add_child(method_vbc);
  589. method_search = memnew(LineEdit);
  590. method_vbc->add_child(method_search);
  591. method_search->set_placeholder(TTR("Filter Methods"));
  592. method_search->set_clear_button_enabled(true);
  593. method_search->connect("text_changed", callable_mp(this, &ConnectDialog::_update_method_tree).unbind(1));
  594. method_tree = memnew(Tree);
  595. method_vbc->add_child(method_tree);
  596. method_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  597. method_tree->set_hide_root(true);
  598. method_tree->connect("item_selected", callable_mp(this, &ConnectDialog::_method_selected));
  599. method_tree->connect("item_activated", callable_mp((Window *)method_popup, &Window::hide));
  600. empty_tree_label = memnew(Label(TTR("No method found matching given filters.")));
  601. method_popup->add_child(empty_tree_label);
  602. empty_tree_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  603. empty_tree_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  604. empty_tree_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  605. script_methods_only = memnew(CheckButton(TTR("Script Methods Only")));
  606. method_vbc->add_child(script_methods_only);
  607. script_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
  608. script_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_script_methods_only", true));
  609. script_methods_only->connect("pressed", callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(script_methods_only));
  610. compatible_methods_only = memnew(CheckButton(TTR("Compatible Methods Only")));
  611. method_vbc->add_child(compatible_methods_only);
  612. compatible_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
  613. compatible_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_compatible_methods_only", true));
  614. compatible_methods_only->connect("pressed", callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(compatible_methods_only));
  615. vbc_right = memnew(VBoxContainer);
  616. main_hb->add_child(vbc_right);
  617. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  618. vbc_right->set_custom_minimum_size(Vector2(150 * EDSCALE, 0));
  619. vbc_right->hide();
  620. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  621. type_list = memnew(OptionButton);
  622. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  623. add_bind_hb->add_child(type_list);
  624. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  625. if (i == Variant::NIL || i == Variant::OBJECT || i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  626. // These types can't be constructed or serialized properly, so skip them.
  627. continue;
  628. }
  629. type_list->add_item(Variant::get_type_name(Variant::Type(i)), i);
  630. }
  631. bind_controls.push_back(type_list);
  632. Button *add_bind = memnew(Button);
  633. add_bind->set_text(TTR("Add"));
  634. add_bind_hb->add_child(add_bind);
  635. add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind));
  636. bind_controls.push_back(add_bind);
  637. Button *del_bind = memnew(Button);
  638. del_bind->set_text(TTR("Remove"));
  639. add_bind_hb->add_child(del_bind);
  640. del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind));
  641. bind_controls.push_back(del_bind);
  642. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  643. bind_editor = memnew(EditorInspector);
  644. bind_controls.push_back(bind_editor);
  645. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  646. unbind_count = memnew(SpinBox);
  647. unbind_count->set_tooltip_text(TTR("Allows to drop arguments sent by signal emitter."));
  648. unbind_count->connect("value_changed", callable_mp(this, &ConnectDialog::_unbind_count_changed));
  649. vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);
  650. HBoxContainer *hbc_method = memnew(HBoxContainer);
  651. vbc_left->add_margin_child(TTR("Receiver Method:"), hbc_method);
  652. dst_method = memnew(LineEdit);
  653. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  654. dst_method->connect("text_changed", callable_mp(method_tree, &Tree::deselect_all).unbind(1));
  655. dst_method->connect("text_submitted", callable_mp(this, &ConnectDialog::_text_submitted));
  656. hbc_method->add_child(dst_method);
  657. open_method_tree = memnew(Button);
  658. hbc_method->add_child(open_method_tree);
  659. open_method_tree->set_text("Pick");
  660. open_method_tree->connect("pressed", callable_mp(this, &ConnectDialog::_open_method_popup));
  661. advanced = memnew(CheckButton(TTR("Advanced")));
  662. vbc_left->add_child(advanced);
  663. advanced->set_h_size_flags(Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND);
  664. advanced->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "use_advanced_connections", false));
  665. advanced->connect("pressed", callable_mp(this, &ConnectDialog::_advanced_pressed));
  666. HBoxContainer *hbox = memnew(HBoxContainer);
  667. vbc_right->add_child(hbox);
  668. deferred = memnew(CheckBox);
  669. deferred->set_h_size_flags(0);
  670. deferred->set_text(TTR("Deferred"));
  671. deferred->set_tooltip_text(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  672. hbox->add_child(deferred);
  673. one_shot = memnew(CheckBox);
  674. one_shot->set_h_size_flags(0);
  675. one_shot->set_text(TTR("One Shot"));
  676. one_shot->set_tooltip_text(TTR("Disconnects the signal after its first emission."));
  677. hbox->add_child(one_shot);
  678. cdbinds = memnew(ConnectDialogBinds);
  679. error = memnew(AcceptDialog);
  680. add_child(error);
  681. error->set_title(TTR("Cannot connect signal"));
  682. error->set_ok_button_text(TTR("Close"));
  683. set_ok_button_text(TTR("Connect"));
  684. }
  685. ConnectDialog::~ConnectDialog() {
  686. memdelete(cdbinds);
  687. }
  688. //////////////////////////////////////////
  689. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  690. // If it's not a doc tooltip, fallback to the default one.
  691. return p_text.contains("::") ? nullptr : memnew(EditorHelpTooltip(p_text));
  692. }
  693. struct _ConnectionsDockMethodInfoSort {
  694. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  695. return a.name < b.name;
  696. }
  697. };
  698. void ConnectionsDock::_filter_changed(const String &p_text) {
  699. update_tree();
  700. }
  701. /*
  702. * Post-ConnectDialog callback for creating/editing connections.
  703. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  704. */
  705. void ConnectionsDock::_make_or_edit_connection() {
  706. NodePath dst_path = connect_dialog->get_dst_path();
  707. Node *target = selected_node->get_node(dst_path);
  708. ERR_FAIL_NULL(target);
  709. ConnectDialog::ConnectionData cd;
  710. cd.source = connect_dialog->get_source();
  711. cd.target = target;
  712. cd.signal = connect_dialog->get_signal_name();
  713. cd.method = connect_dialog->get_dst_method_name();
  714. cd.unbinds = connect_dialog->get_unbinds();
  715. if (cd.unbinds == 0) {
  716. cd.binds = connect_dialog->get_binds();
  717. }
  718. bool b_deferred = connect_dialog->get_deferred();
  719. bool b_oneshot = connect_dialog->get_one_shot();
  720. cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONE_SHOT : 0);
  721. // Conditions to add function: must have a script and must not have the method already
  722. // (in the class, the script itself, or inherited).
  723. bool add_script_function = false;
  724. Ref<Script> scr = target->get_script();
  725. if (!scr.is_null() && !ClassDB::has_method(target->get_class(), cd.method)) {
  726. // There is a chance that the method is inherited from another script.
  727. bool found_inherited_function = false;
  728. Ref<Script> inherited_scr = scr->get_base_script();
  729. while (!inherited_scr.is_null()) {
  730. int line = inherited_scr->get_language()->find_function(cd.method, inherited_scr->get_source_code());
  731. if (line != -1) {
  732. found_inherited_function = true;
  733. break;
  734. }
  735. inherited_scr = inherited_scr->get_base_script();
  736. }
  737. add_script_function = !found_inherited_function;
  738. }
  739. if (connect_dialog->is_editing()) {
  740. _disconnect(connect_dialog->get_source_connection_data());
  741. _connect(cd);
  742. } else {
  743. _connect(cd);
  744. }
  745. if (add_script_function) {
  746. PackedStringArray script_function_args = connect_dialog->get_signal_args();
  747. script_function_args.resize(script_function_args.size() - cd.unbinds);
  748. for (int i = 0; i < cd.binds.size(); i++) {
  749. script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cd.binds[i].get_type()));
  750. }
  751. EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args);
  752. hide();
  753. }
  754. update_tree();
  755. }
  756. /*
  757. * Creates single connection w/ undo-redo functionality.
  758. */
  759. void ConnectionsDock::_connect(const ConnectDialog::ConnectionData &p_cd) {
  760. Node *source = Object::cast_to<Node>(p_cd.source);
  761. Node *target = Object::cast_to<Node>(p_cd.target);
  762. if (!source || !target) {
  763. return;
  764. }
  765. Callable callable = p_cd.get_callable();
  766. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  767. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));
  768. undo_redo->add_do_method(source, "connect", p_cd.signal, callable, p_cd.flags);
  769. undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);
  770. undo_redo->add_do_method(this, "update_tree");
  771. undo_redo->add_undo_method(this, "update_tree");
  772. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  773. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  774. undo_redo->commit_action();
  775. }
  776. /*
  777. * Break single connection w/ undo-redo functionality.
  778. */
  779. void ConnectionsDock::_disconnect(const ConnectDialog::ConnectionData &p_cd) {
  780. ERR_FAIL_COND(p_cd.source != selected_node); // Shouldn't happen but... Bugcheck.
  781. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  782. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), p_cd.signal, p_cd.method));
  783. Callable callable = p_cd.get_callable();
  784. undo_redo->add_do_method(selected_node, "disconnect", p_cd.signal, callable);
  785. undo_redo->add_undo_method(selected_node, "connect", p_cd.signal, callable, p_cd.flags);
  786. undo_redo->add_do_method(this, "update_tree");
  787. undo_redo->add_undo_method(this, "update_tree");
  788. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  789. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  790. undo_redo->commit_action();
  791. }
  792. /*
  793. * Break all connections of currently selected signal.
  794. * Can undo-redo as a single action.
  795. */
  796. void ConnectionsDock::_disconnect_all() {
  797. TreeItem *item = tree->get_selected();
  798. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
  799. return;
  800. }
  801. TreeItem *child = item->get_first_child();
  802. String signal_name = item->get_metadata(0).operator Dictionary()["name"];
  803. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  804. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));
  805. while (child) {
  806. Connection connection = child->get_metadata(0);
  807. if (!_is_connection_inherited(connection)) {
  808. ConnectDialog::ConnectionData cd = connection;
  809. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable());
  810. undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.flags);
  811. }
  812. child = child->get_next();
  813. }
  814. undo_redo->add_do_method(this, "update_tree");
  815. undo_redo->add_undo_method(this, "update_tree");
  816. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  817. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  818. undo_redo->commit_action();
  819. }
  820. void ConnectionsDock::_tree_item_selected() {
  821. TreeItem *item = tree->get_selected();
  822. if (item && _get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
  823. connect_button->set_text(TTR("Connect..."));
  824. connect_button->set_icon(get_editor_theme_icon(SNAME("Instance")));
  825. connect_button->set_disabled(false);
  826. } else if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
  827. connect_button->set_text(TTR("Disconnect"));
  828. connect_button->set_icon(get_editor_theme_icon(SNAME("Unlinked")));
  829. Object::Connection connection = item->get_metadata(0);
  830. connect_button->set_disabled(_is_connection_inherited(connection));
  831. } else {
  832. connect_button->set_text(TTR("Connect..."));
  833. connect_button->set_icon(get_editor_theme_icon(SNAME("Instance")));
  834. connect_button->set_disabled(true);
  835. }
  836. }
  837. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  838. TreeItem *item = tree->get_selected();
  839. if (!item) {
  840. return;
  841. }
  842. if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
  843. _open_connection_dialog(*item);
  844. } else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
  845. _go_to_method(*item);
  846. }
  847. }
  848. ConnectionsDock::TreeItemType ConnectionsDock::_get_item_type(const TreeItem &p_item) const {
  849. if (&p_item == tree->get_root()) {
  850. return TREE_ITEM_TYPE_ROOT;
  851. } else if (p_item.get_parent() == tree->get_root()) {
  852. return TREE_ITEM_TYPE_CLASS;
  853. } else if (p_item.get_parent()->get_parent() == tree->get_root()) {
  854. return TREE_ITEM_TYPE_SIGNAL;
  855. } else {
  856. return TREE_ITEM_TYPE_CONNECTION;
  857. }
  858. }
  859. bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {
  860. return bool(p_connection.flags & CONNECT_INHERITED);
  861. }
  862. /*
  863. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  864. */
  865. void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
  866. Dictionary sinfo = p_item.get_metadata(0);
  867. String signal_name = sinfo["name"];
  868. PackedStringArray signal_args = sinfo["args"];
  869. Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
  870. if (!dst_node || dst_node->get_script().is_null()) {
  871. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  872. }
  873. ConnectDialog::ConnectionData cd;
  874. cd.source = selected_node;
  875. cd.signal = StringName(signal_name);
  876. cd.target = dst_node;
  877. cd.method = ConnectDialog::generate_method_callback_name(cd.source, signal_name, cd.target);
  878. connect_dialog->popup_dialog(signal_name + "(" + String(", ").join(signal_args) + ")");
  879. connect_dialog->init(cd, signal_args);
  880. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  881. }
  882. /*
  883. * Open connection dialog with Connection data to EDIT an existing connection.
  884. */
  885. void ConnectionsDock::_open_edit_connection_dialog(TreeItem &p_item) {
  886. TreeItem *signal_item = p_item.get_parent();
  887. ERR_FAIL_NULL(signal_item);
  888. Connection connection = p_item.get_metadata(0);
  889. ConnectDialog::ConnectionData cd = connection;
  890. Node *src = Object::cast_to<Node>(cd.source);
  891. Node *dst = Object::cast_to<Node>(cd.target);
  892. if (src && dst) {
  893. const String &signal_name_ref = cd.signal;
  894. PackedStringArray signal_args = signal_item->get_metadata(0).operator Dictionary()["args"];
  895. connect_dialog->set_title(vformat(TTR("Edit Connection: '%s'"), cd.signal));
  896. connect_dialog->popup_dialog(signal_name_ref);
  897. connect_dialog->init(cd, signal_args, true);
  898. }
  899. }
  900. /*
  901. * Open slot method location in script editor.
  902. */
  903. void ConnectionsDock::_go_to_method(TreeItem &p_item) {
  904. if (_get_item_type(p_item) != TREE_ITEM_TYPE_CONNECTION) {
  905. return;
  906. }
  907. Connection connection = p_item.get_metadata(0);
  908. ConnectDialog::ConnectionData cd = connection;
  909. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck.
  910. if (!cd.target) {
  911. return;
  912. }
  913. Ref<Script> scr = cd.target->get_script();
  914. if (scr.is_null()) {
  915. return;
  916. }
  917. if (scr.is_valid() && ScriptEditor::get_singleton()->script_goto_method(scr, cd.method)) {
  918. EditorNode::get_singleton()->editor_select(EditorNode::EDITOR_SCRIPT);
  919. }
  920. }
  921. void ConnectionsDock::_handle_class_menu_option(int p_option) {
  922. switch (p_option) {
  923. case CLASS_MENU_OPEN_DOCS:
  924. ScriptEditor::get_singleton()->goto_help("class:" + class_menu_doc_class_name);
  925. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  926. break;
  927. }
  928. }
  929. void ConnectionsDock::_class_menu_about_to_popup() {
  930. class_menu->set_item_disabled(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), class_menu_doc_class_name.is_empty());
  931. }
  932. void ConnectionsDock::_handle_signal_menu_option(int p_option) {
  933. TreeItem *item = tree->get_selected();
  934. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
  935. return;
  936. }
  937. Dictionary meta = item->get_metadata(0);
  938. switch (p_option) {
  939. case SIGNAL_MENU_CONNECT: {
  940. _open_connection_dialog(*item);
  941. } break;
  942. case SIGNAL_MENU_DISCONNECT_ALL: {
  943. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), meta["name"]));
  944. disconnect_all_dialog->popup_centered();
  945. } break;
  946. case SIGNAL_MENU_COPY_NAME: {
  947. DisplayServer::get_singleton()->clipboard_set(meta["name"]);
  948. } break;
  949. case SIGNAL_MENU_OPEN_DOCS: {
  950. ScriptEditor::get_singleton()->goto_help("class_signal:" + String(meta["class"]) + ":" + String(meta["name"]));
  951. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  952. } break;
  953. }
  954. }
  955. void ConnectionsDock::_signal_menu_about_to_popup() {
  956. TreeItem *item = tree->get_selected();
  957. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
  958. return;
  959. }
  960. Dictionary meta = item->get_metadata(0);
  961. bool disable_disconnect_all = true;
  962. for (int i = 0; i < item->get_child_count(); i++) {
  963. if (!item->get_child(i)->has_meta("_inherited_connection")) {
  964. disable_disconnect_all = false;
  965. }
  966. }
  967. signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), disable_disconnect_all);
  968. signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), String(meta["class"]).is_empty());
  969. }
  970. void ConnectionsDock::_handle_slot_menu_option(int p_option) {
  971. TreeItem *item = tree->get_selected();
  972. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
  973. return;
  974. }
  975. switch (p_option) {
  976. case SLOT_MENU_EDIT: {
  977. _open_edit_connection_dialog(*item);
  978. } break;
  979. case SLOT_MENU_GO_TO_METHOD: {
  980. _go_to_method(*item);
  981. } break;
  982. case SLOT_MENU_DISCONNECT: {
  983. Connection connection = item->get_metadata(0);
  984. _disconnect(connection);
  985. update_tree();
  986. } break;
  987. }
  988. }
  989. void ConnectionsDock::_slot_menu_about_to_popup() {
  990. TreeItem *item = tree->get_selected();
  991. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
  992. return;
  993. }
  994. bool connection_is_inherited = item->has_meta("_inherited_connection");
  995. slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_EDIT), connection_is_inherited);
  996. slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), connection_is_inherited);
  997. }
  998. void ConnectionsDock::_rmb_pressed(const Ref<InputEvent> &p_event) {
  999. const Ref<InputEventMouseButton> &mb_event = p_event;
  1000. if (mb_event.is_null() || !mb_event->is_pressed() || mb_event->get_button_index() != MouseButton::RIGHT) {
  1001. return;
  1002. }
  1003. TreeItem *item = tree->get_item_at_position(mb_event->get_position());
  1004. if (!item) {
  1005. return;
  1006. }
  1007. if (item->is_selectable(0)) {
  1008. // Update selection now, before `about_to_popup` signal. Needed for SIGNAL and CONNECTION context menus.
  1009. tree->set_selected(item);
  1010. }
  1011. Vector2 screen_position = tree->get_screen_position() + mb_event->get_position();
  1012. switch (_get_item_type(*item)) {
  1013. case TREE_ITEM_TYPE_ROOT:
  1014. break;
  1015. case TREE_ITEM_TYPE_CLASS:
  1016. class_menu_doc_class_name = item->get_metadata(0);
  1017. class_menu->set_position(screen_position);
  1018. class_menu->reset_size();
  1019. class_menu->popup();
  1020. accept_event(); // Don't collapse item.
  1021. break;
  1022. case TREE_ITEM_TYPE_SIGNAL:
  1023. signal_menu->set_position(screen_position);
  1024. signal_menu->reset_size();
  1025. signal_menu->popup();
  1026. break;
  1027. case TREE_ITEM_TYPE_CONNECTION:
  1028. slot_menu->set_position(screen_position);
  1029. slot_menu->reset_size();
  1030. slot_menu->popup();
  1031. break;
  1032. }
  1033. }
  1034. void ConnectionsDock::_close() {
  1035. hide();
  1036. }
  1037. void ConnectionsDock::_connect_pressed() {
  1038. TreeItem *item = tree->get_selected();
  1039. if (!item) {
  1040. connect_button->set_disabled(true);
  1041. return;
  1042. }
  1043. if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
  1044. _open_connection_dialog(*item);
  1045. } else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
  1046. Connection connection = item->get_metadata(0);
  1047. _disconnect(connection);
  1048. update_tree();
  1049. }
  1050. }
  1051. void ConnectionsDock::_notification(int p_what) {
  1052. switch (p_what) {
  1053. case NOTIFICATION_ENTER_TREE:
  1054. case NOTIFICATION_THEME_CHANGED: {
  1055. search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  1056. class_menu->set_item_icon(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
  1057. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_CONNECT), get_editor_theme_icon(SNAME("Instance")));
  1058. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), get_editor_theme_icon(SNAME("Unlinked")));
  1059. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_COPY_NAME), get_editor_theme_icon(SNAME("ActionCopy")));
  1060. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
  1061. slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_EDIT), get_editor_theme_icon(SNAME("Edit")));
  1062. slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_GO_TO_METHOD), get_editor_theme_icon(SNAME("ArrowRight")));
  1063. slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), get_editor_theme_icon(SNAME("Unlinked")));
  1064. tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  1065. } break;
  1066. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1067. update_tree();
  1068. } break;
  1069. }
  1070. }
  1071. void ConnectionsDock::_bind_methods() {
  1072. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  1073. }
  1074. void ConnectionsDock::set_node(Node *p_node) {
  1075. selected_node = p_node;
  1076. update_tree();
  1077. }
  1078. void ConnectionsDock::update_tree() {
  1079. String prev_selected;
  1080. if (tree->is_anything_selected()) {
  1081. prev_selected = tree->get_selected()->get_text(0);
  1082. }
  1083. tree->clear();
  1084. if (!selected_node) {
  1085. return;
  1086. }
  1087. TreeItem *root = tree->create_item();
  1088. DocTools *doc_data = EditorHelp::get_doc_data();
  1089. EditorData &editor_data = EditorNode::get_editor_data();
  1090. StringName native_base = selected_node->get_class();
  1091. Ref<Script> script_base = selected_node->get_script();
  1092. while (native_base != StringName()) {
  1093. String class_name;
  1094. String doc_class_name;
  1095. Ref<Texture2D> class_icon;
  1096. List<MethodInfo> class_signals;
  1097. if (script_base.is_valid()) {
  1098. class_name = script_base->get_global_name();
  1099. if (class_name.is_empty()) {
  1100. class_name = script_base->get_path().get_file();
  1101. }
  1102. doc_class_name = script_base->get_global_name();
  1103. if (doc_class_name.is_empty()) {
  1104. doc_class_name = script_base->get_path().trim_prefix("res://").quote();
  1105. }
  1106. if (!doc_class_name.is_empty() && !doc_data->class_list.find(doc_class_name)) {
  1107. doc_class_name = String();
  1108. }
  1109. class_icon = editor_data.get_script_icon(script_base);
  1110. if (class_icon.is_null() && has_theme_icon(native_base, EditorStringName(EditorIcons))) {
  1111. class_icon = get_editor_theme_icon(native_base);
  1112. }
  1113. script_base->get_script_signal_list(&class_signals);
  1114. // TODO: Core: Add optional parameter to ignore base classes (no_inheritance like in ClassDB).
  1115. Ref<Script> base = script_base->get_base_script();
  1116. if (base.is_valid()) {
  1117. List<MethodInfo> base_signals;
  1118. base->get_script_signal_list(&base_signals);
  1119. HashSet<String> base_signal_names;
  1120. for (List<MethodInfo>::Element *F = base_signals.front(); F; F = F->next()) {
  1121. base_signal_names.insert(F->get().name);
  1122. }
  1123. for (List<MethodInfo>::Element *F = class_signals.front(); F; F = F->next()) {
  1124. if (base_signal_names.has(F->get().name)) {
  1125. class_signals.erase(F);
  1126. }
  1127. }
  1128. }
  1129. script_base = base;
  1130. } else {
  1131. class_name = native_base;
  1132. doc_class_name = native_base;
  1133. if (!doc_data->class_list.find(doc_class_name)) {
  1134. doc_class_name = String();
  1135. }
  1136. if (has_theme_icon(native_base, EditorStringName(EditorIcons))) {
  1137. class_icon = get_editor_theme_icon(native_base);
  1138. }
  1139. ClassDB::get_signal_list(native_base, &class_signals, true);
  1140. native_base = ClassDB::get_parent_class(native_base);
  1141. }
  1142. if (class_icon.is_null()) {
  1143. class_icon = get_editor_theme_icon(SNAME("Object"));
  1144. }
  1145. TreeItem *section_item = nullptr;
  1146. // Create subsections.
  1147. if (!class_signals.is_empty()) {
  1148. class_signals.sort();
  1149. section_item = tree->create_item(root);
  1150. section_item->set_text(0, class_name);
  1151. // `|` separators used in `EditorHelpTooltip` for formatting.
  1152. section_item->set_tooltip_text(0, "class|" + doc_class_name + "||");
  1153. section_item->set_icon(0, class_icon);
  1154. section_item->set_selectable(0, false);
  1155. section_item->set_editable(0, false);
  1156. section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  1157. section_item->set_metadata(0, doc_class_name);
  1158. }
  1159. for (MethodInfo &mi : class_signals) {
  1160. const StringName &signal_name = mi.name;
  1161. if (!search_box->get_text().is_subsequence_ofn(signal_name)) {
  1162. continue;
  1163. }
  1164. PackedStringArray argnames;
  1165. // Create the children of the subsection - the actual list of signals.
  1166. TreeItem *signal_item = tree->create_item(section_item);
  1167. String signame = connect_dialog->get_signature(mi, &argnames);
  1168. signal_item->set_text(0, signame);
  1169. if (signame == prev_selected) {
  1170. signal_item->select(0);
  1171. prev_selected = "";
  1172. }
  1173. Dictionary sinfo;
  1174. sinfo["class"] = doc_class_name;
  1175. sinfo["name"] = signal_name;
  1176. sinfo["args"] = argnames;
  1177. signal_item->set_metadata(0, sinfo);
  1178. signal_item->set_icon(0, get_editor_theme_icon(SNAME("Signal")));
  1179. // `|` separators used in `EditorHelpTooltip` for formatting.
  1180. signal_item->set_tooltip_text(0, "signal|" + doc_class_name + "|" + String(signal_name) + "|" + signame.trim_prefix(mi.name));
  1181. // List existing connections.
  1182. List<Object::Connection> existing_connections;
  1183. selected_node->get_signal_connection_list(signal_name, &existing_connections);
  1184. for (const Object::Connection &F : existing_connections) {
  1185. Connection connection = F;
  1186. if (!(connection.flags & CONNECT_PERSIST)) {
  1187. continue;
  1188. }
  1189. ConnectDialog::ConnectionData cd = connection;
  1190. Node *target = Object::cast_to<Node>(cd.target);
  1191. if (!target) {
  1192. continue;
  1193. }
  1194. String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()";
  1195. if (cd.flags & CONNECT_DEFERRED) {
  1196. path += " (deferred)";
  1197. }
  1198. if (cd.flags & CONNECT_ONE_SHOT) {
  1199. path += " (one-shot)";
  1200. }
  1201. if (cd.unbinds > 0) {
  1202. path += " unbinds(" + itos(cd.unbinds) + ")";
  1203. } else if (!cd.binds.is_empty()) {
  1204. path += " binds(";
  1205. for (int i = 0; i < cd.binds.size(); i++) {
  1206. if (i > 0) {
  1207. path += ", ";
  1208. }
  1209. path += cd.binds[i].operator String();
  1210. }
  1211. path += ")";
  1212. }
  1213. TreeItem *connection_item = tree->create_item(signal_item);
  1214. connection_item->set_text(0, path);
  1215. connection_item->set_metadata(0, connection);
  1216. connection_item->set_icon(0, get_editor_theme_icon(SNAME("Slot")));
  1217. if (_is_connection_inherited(connection)) {
  1218. // The scene inherits this connection.
  1219. connection_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  1220. connection_item->set_meta("_inherited_connection", true);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. connect_button->set_text(TTR("Connect..."));
  1226. connect_button->set_icon(get_editor_theme_icon(SNAME("Instance")));
  1227. connect_button->set_disabled(true);
  1228. }
  1229. ConnectionsDock::ConnectionsDock() {
  1230. set_name(TTR("Signals"));
  1231. VBoxContainer *vbc = this;
  1232. search_box = memnew(LineEdit);
  1233. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1234. search_box->set_placeholder(TTR("Filter Signals"));
  1235. search_box->set_clear_button_enabled(true);
  1236. search_box->connect("text_changed", callable_mp(this, &ConnectionsDock::_filter_changed));
  1237. vbc->add_child(search_box);
  1238. tree = memnew(ConnectionsDockTree);
  1239. tree->set_columns(1);
  1240. tree->set_select_mode(Tree::SELECT_ROW);
  1241. tree->set_hide_root(true);
  1242. tree->set_column_clip_content(0, true);
  1243. vbc->add_child(tree);
  1244. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1245. tree->set_allow_rmb_select(true);
  1246. connect_button = memnew(Button);
  1247. HBoxContainer *hb = memnew(HBoxContainer);
  1248. vbc->add_child(hb);
  1249. hb->add_spacer();
  1250. hb->add_child(connect_button);
  1251. connect_button->connect("pressed", callable_mp(this, &ConnectionsDock::_connect_pressed));
  1252. connect_dialog = memnew(ConnectDialog);
  1253. connect_dialog->connect("connected", callable_mp(NodeDock::get_singleton(), &NodeDock::restore_last_valid_node), CONNECT_DEFERRED);
  1254. add_child(connect_dialog);
  1255. disconnect_all_dialog = memnew(ConfirmationDialog);
  1256. add_child(disconnect_all_dialog);
  1257. disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
  1258. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  1259. class_menu = memnew(PopupMenu);
  1260. class_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_class_menu_option));
  1261. class_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_class_menu_about_to_popup));
  1262. class_menu->add_item(TTR("Open Documentation"), CLASS_MENU_OPEN_DOCS);
  1263. add_child(class_menu);
  1264. signal_menu = memnew(PopupMenu);
  1265. signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  1266. signal_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_signal_menu_about_to_popup));
  1267. signal_menu->add_item(TTR("Connect..."), SIGNAL_MENU_CONNECT);
  1268. signal_menu->add_item(TTR("Disconnect All"), SIGNAL_MENU_DISCONNECT_ALL);
  1269. signal_menu->add_item(TTR("Copy Name"), SIGNAL_MENU_COPY_NAME);
  1270. signal_menu->add_separator();
  1271. signal_menu->add_item(TTR("Open Documentation"), SIGNAL_MENU_OPEN_DOCS);
  1272. add_child(signal_menu);
  1273. slot_menu = memnew(PopupMenu);
  1274. slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  1275. slot_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_slot_menu_about_to_popup));
  1276. slot_menu->add_item(TTR("Edit..."), SLOT_MENU_EDIT);
  1277. slot_menu->add_item(TTR("Go to Method"), SLOT_MENU_GO_TO_METHOD);
  1278. slot_menu->add_item(TTR("Disconnect"), SLOT_MENU_DISCONNECT);
  1279. add_child(slot_menu);
  1280. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  1281. tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
  1282. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  1283. tree->connect("gui_input", callable_mp(this, &ConnectionsDock::_rmb_pressed));
  1284. add_theme_constant_override("separation", 3 * EDSCALE);
  1285. }
  1286. ConnectionsDock::~ConnectionsDock() {
  1287. }