connections_dialog.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  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/doc/editor_help.h"
  34. #include "editor/docks/node_dock.h"
  35. #include "editor/docks/scene_tree_dock.h"
  36. #include "editor/editor_main_screen.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/editor_variant_type_selectors.h"
  41. #include "editor/inspector/editor_inspector.h"
  42. #include "editor/scene/scene_tree_editor.h"
  43. #include "editor/script/script_editor_plugin.h"
  44. #include "editor/settings/editor_settings.h"
  45. #include "editor/themes/editor_scale.h"
  46. #include "scene/gui/button.h"
  47. #include "scene/gui/check_box.h"
  48. #include "scene/gui/check_button.h"
  49. #include "scene/gui/flow_container.h"
  50. #include "scene/gui/label.h"
  51. #include "scene/gui/line_edit.h"
  52. #include "scene/gui/margin_container.h"
  53. #include "scene/gui/popup_menu.h"
  54. #include "scene/gui/spin_box.h"
  55. static Node *_find_first_script(Node *p_root, Node *p_node) {
  56. if (p_node != p_root && p_node->get_owner() != p_root) {
  57. return nullptr;
  58. }
  59. if (!p_node->get_script().is_null()) {
  60. return p_node;
  61. }
  62. for (int i = 0; i < p_node->get_child_count(); i++) {
  63. Node *ret = _find_first_script(p_root, p_node->get_child(i));
  64. if (ret) {
  65. return ret;
  66. }
  67. }
  68. return nullptr;
  69. }
  70. class ConnectDialogBinds : public Object {
  71. GDCLASS(ConnectDialogBinds, Object);
  72. public:
  73. Vector<Variant> params;
  74. bool _set(const StringName &p_name, const Variant &p_value) {
  75. String name = p_name;
  76. if (name.begins_with("bind/argument_")) {
  77. int which = name.get_slicec('_', 1).to_int() - 1;
  78. ERR_FAIL_INDEX_V(which, params.size(), false);
  79. params.write[which] = p_value;
  80. } else {
  81. return false;
  82. }
  83. return true;
  84. }
  85. bool _get(const StringName &p_name, Variant &r_ret) const {
  86. String name = p_name;
  87. if (name.begins_with("bind/argument_")) {
  88. int which = name.get_slicec('_', 1).to_int() - 1;
  89. ERR_FAIL_INDEX_V(which, params.size(), false);
  90. r_ret = params[which];
  91. } else {
  92. return false;
  93. }
  94. return true;
  95. }
  96. void _get_property_list(List<PropertyInfo> *p_list) const {
  97. for (int i = 0; i < params.size(); i++) {
  98. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));
  99. }
  100. }
  101. void update_base_node_relative(Node *p_node) {
  102. Node *old_base = nullptr;
  103. if (has_meta("__base_node_relative")) {
  104. old_base = Object::cast_to<Node>(get_meta("__base_node_relative"));
  105. }
  106. if (old_base == p_node) {
  107. return;
  108. }
  109. // The cdbinds is a proxy object, so we want the node path to be relative to the target node.
  110. set_meta("__base_node_relative", p_node);
  111. if (!old_base) {
  112. return;
  113. }
  114. // Update existing outdated node paths.
  115. for (int i = 0; i < params.size(); i++) {
  116. if (params[i].get_type() != Variant::NODE_PATH) {
  117. continue;
  118. }
  119. StringName property_name = "bind/argument_" + itos(i + 1);
  120. Node *n = old_base->get_node(get(property_name));
  121. set(property_name, p_node ? p_node->get_path_to(n) : NodePath());
  122. }
  123. }
  124. void notify_changed() {
  125. notify_property_list_changed();
  126. }
  127. ConnectDialogBinds() {
  128. }
  129. };
  130. /*
  131. * Signal automatically called by parent dialog.
  132. */
  133. void ConnectDialog::ok_pressed() {
  134. String method_name = dst_method->get_text();
  135. if (method_name.is_empty()) {
  136. error->set_text(TTR("Method in target node must be specified."));
  137. error->popup_centered();
  138. return;
  139. }
  140. if (!TS->is_valid_identifier(method_name.strip_edges())) {
  141. error->set_text(TTR("Method name must be a valid identifier."));
  142. error->popup_centered();
  143. return;
  144. }
  145. Node *target = tree->get_selected();
  146. if (!target) {
  147. return; // Nothing selected in the tree, not an error.
  148. }
  149. if (target->get_script().is_null()) {
  150. if (!target->has_method(method_name)) {
  151. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  152. error->popup_centered();
  153. return;
  154. }
  155. }
  156. emit_signal(SNAME("connected"));
  157. hide();
  158. }
  159. void ConnectDialog::_cancel_pressed() {
  160. hide();
  161. }
  162. void ConnectDialog::_item_activated() {
  163. _ok_pressed(); // From AcceptDialog.
  164. }
  165. /*
  166. * Called each time a target node is selected within the target node tree.
  167. */
  168. void ConnectDialog::_tree_node_selected() {
  169. Node *current = tree->get_selected();
  170. if (!current) {
  171. return;
  172. }
  173. Node *source_node = Object::cast_to<Node>(source);
  174. if (source_node) {
  175. dst_path = source_node->get_path_to(current);
  176. }
  177. if (!edit_mode) {
  178. set_dst_method(generate_method_callback_name(source, signal, current));
  179. }
  180. cdbinds->update_base_node_relative(current);
  181. _update_method_tree();
  182. _update_warning_label();
  183. _update_ok_enabled();
  184. }
  185. void ConnectDialog::_focus_currently_connected() {
  186. tree->set_selected(Object::cast_to<Node>(source));
  187. }
  188. void ConnectDialog::_method_selected() {
  189. TreeItem *selected_item = method_tree->get_selected();
  190. dst_method->set_text(selected_item->get_metadata(0));
  191. }
  192. /*
  193. * Adds a new parameter bind to connection.
  194. */
  195. void ConnectDialog::_add_bind() {
  196. Variant::Type type = type_list->get_selected_type();
  197. Variant value;
  198. Callable::CallError err;
  199. Variant::construct(type, value, nullptr, 0, err);
  200. cdbinds->params.push_back(value);
  201. cdbinds->notify_changed();
  202. }
  203. /*
  204. * Remove parameter bind from connection.
  205. */
  206. void ConnectDialog::_remove_bind() {
  207. String st = bind_editor->get_selected_path();
  208. if (st.is_empty()) {
  209. return;
  210. }
  211. int idx = st.get_slicec('/', 1).to_int() - 1;
  212. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  213. cdbinds->params.remove_at(idx);
  214. cdbinds->notify_changed();
  215. }
  216. /*
  217. * Automatically generates a name for the callback method.
  218. */
  219. StringName ConnectDialog::generate_method_callback_name(Object *p_source, const String &p_signal_name, Object *p_target) {
  220. String node_name = p_source->call("get_name");
  221. for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.
  222. char32_t c = node_name[i];
  223. if ((i == 0 && !is_unicode_identifier_start(c)) || (i > 0 && !is_unicode_identifier_continue(c))) {
  224. if (c == ' ') {
  225. // Replace spaces with underlines.
  226. c = '_';
  227. } else {
  228. // Remove any other characters.
  229. node_name.remove_at(i);
  230. i--;
  231. continue;
  232. }
  233. }
  234. node_name[i] = c;
  235. }
  236. Dictionary subst;
  237. subst["NodeName"] = node_name.to_pascal_case();
  238. subst["nodeName"] = node_name.to_camel_case();
  239. subst["node_name"] = node_name.to_snake_case();
  240. subst["node-name"] = node_name.to_kebab_case();
  241. subst["SignalName"] = p_signal_name.to_pascal_case();
  242. subst["signalName"] = p_signal_name.to_camel_case();
  243. subst["signal_name"] = p_signal_name.to_snake_case();
  244. subst["signal-name"] = p_signal_name.to_kebab_case();
  245. String dst_method;
  246. if (p_source == p_target) {
  247. dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_to_self_name")).format(subst);
  248. } else {
  249. dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_name")).format(subst);
  250. }
  251. return dst_method;
  252. }
  253. void ConnectDialog::_create_method_tree_items(const List<MethodInfo> &p_methods, TreeItem *p_parent_item) {
  254. for (const MethodInfo &mi : p_methods) {
  255. TreeItem *method_item = method_tree->create_item(p_parent_item);
  256. method_item->set_text(0, get_signature(mi));
  257. method_item->set_metadata(0, mi.name);
  258. }
  259. }
  260. List<MethodInfo> ConnectDialog::_filter_method_list(const List<MethodInfo> &p_methods, const MethodInfo &p_signal, const String &p_search_string) const {
  261. bool check_signal = compatible_methods_only->is_pressed();
  262. List<MethodInfo> ret;
  263. LocalVector<Pair<Variant::Type, StringName>> effective_args;
  264. int unbind = get_unbinds();
  265. effective_args.reserve(MAX(p_signal.arguments.size() - unbind, 0));
  266. for (int64_t i = 0; i < p_signal.arguments.size() - unbind; i++) {
  267. PropertyInfo pi = p_signal.arguments[i];
  268. effective_args.push_back(Pair(pi.type, pi.class_name));
  269. }
  270. for (const Variant &variant : get_binds()) {
  271. effective_args.push_back(Pair(variant.get_type(), StringName()));
  272. }
  273. for (const MethodInfo &mi : p_methods) {
  274. if (mi.name.begins_with("@")) {
  275. // GH-92782. GDScript inline setters/getters are historically present in `get_method_list()`
  276. // and can be called using `Object.call()`. However, these functions are meant to be internal
  277. // and their names are not valid identifiers, so let's hide them from the user.
  278. continue;
  279. }
  280. if (!p_search_string.is_empty() && !mi.name.containsn(p_search_string)) {
  281. continue;
  282. }
  283. if (check_signal) {
  284. const unsigned min_argc = mi.arguments.size() - mi.default_arguments.size();
  285. const unsigned max_argc = (mi.flags & METHOD_FLAG_VARARG) ? UINT_MAX : mi.arguments.size();
  286. if (effective_args.size() < min_argc || effective_args.size() > max_argc) {
  287. continue;
  288. }
  289. bool type_mismatch = false;
  290. for (int64_t i = 0; i < effective_args.size() && i < mi.arguments.size(); ++i) {
  291. Variant::Type stype = effective_args[i].first;
  292. Variant::Type mtype = mi.arguments[i].type;
  293. if (stype != Variant::NIL && mtype != Variant::NIL && stype != mtype) {
  294. type_mismatch = true;
  295. break;
  296. }
  297. if (stype == Variant::OBJECT && mtype == Variant::OBJECT && !ClassDB::is_parent_class(effective_args[i].second, mi.arguments[i].class_name)) {
  298. type_mismatch = true;
  299. break;
  300. }
  301. }
  302. if (type_mismatch) {
  303. continue;
  304. }
  305. }
  306. ret.push_back(mi);
  307. }
  308. return ret;
  309. }
  310. void ConnectDialog::_update_method_tree() {
  311. method_tree->clear();
  312. Color disabled_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)) * 0.7;
  313. String search_string = method_search->get_text();
  314. Node *target = tree->get_selected();
  315. if (!target) {
  316. return;
  317. }
  318. MethodInfo signal_info;
  319. if (compatible_methods_only->is_pressed()) {
  320. List<MethodInfo> signals;
  321. source->get_signal_list(&signals);
  322. for (const MethodInfo &mi : signals) {
  323. if (mi.name == signal) {
  324. signal_info = mi;
  325. break;
  326. }
  327. }
  328. }
  329. TreeItem *root_item = method_tree->create_item();
  330. root_item->set_text(0, TTR("Methods"));
  331. root_item->set_selectable(0, false);
  332. // If a script is attached, get methods from it.
  333. ScriptInstance *si = target->get_script_instance();
  334. if (si) {
  335. if (si->get_script()->is_built_in()) {
  336. si->get_script()->reload();
  337. }
  338. List<MethodInfo> methods;
  339. si->get_method_list(&methods);
  340. methods = _filter_method_list(methods, signal_info, search_string);
  341. if (!methods.is_empty()) {
  342. TreeItem *si_item = method_tree->create_item(root_item);
  343. si_item->set_text(0, TTR("Attached Script"));
  344. si_item->set_icon(0, get_editor_theme_icon(SNAME("Script")));
  345. si_item->set_selectable(0, false);
  346. _create_method_tree_items(methods, si_item);
  347. }
  348. }
  349. if (script_methods_only->is_pressed()) {
  350. empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
  351. return;
  352. }
  353. // Get methods from each class in the hierarchy.
  354. StringName current_class = target->get_class_name();
  355. do {
  356. TreeItem *class_item = method_tree->create_item(root_item);
  357. class_item->set_text(0, current_class);
  358. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Node"));
  359. if (has_theme_icon(current_class, EditorStringName(EditorIcons))) {
  360. icon = get_editor_theme_icon(current_class);
  361. }
  362. class_item->set_icon(0, icon);
  363. class_item->set_selectable(0, false);
  364. List<MethodInfo> methods;
  365. ClassDB::get_method_list(current_class, &methods, true);
  366. methods = _filter_method_list(methods, signal_info, search_string);
  367. if (methods.is_empty()) {
  368. class_item->set_custom_color(0, disabled_color);
  369. } else {
  370. _create_method_tree_items(methods, class_item);
  371. }
  372. current_class = ClassDB::get_parent_class_nocheck(current_class);
  373. } while (current_class != StringName());
  374. empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
  375. }
  376. void ConnectDialog::_method_check_button_pressed(const CheckButton *p_button) {
  377. if (p_button == script_methods_only) {
  378. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_script_methods_only", p_button->is_pressed());
  379. } else if (p_button == compatible_methods_only) {
  380. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_compatible_methods_only", p_button->is_pressed());
  381. }
  382. _update_method_tree();
  383. }
  384. void ConnectDialog::_open_method_popup() {
  385. method_popup->popup_centered();
  386. method_search->clear();
  387. method_search->grab_focus();
  388. }
  389. /*
  390. * Enables or disables the connect button. The connect button is enabled if a
  391. * node is selected and valid in the selected mode.
  392. */
  393. void ConnectDialog::_update_ok_enabled() {
  394. Node *target = tree->get_selected();
  395. if (target == nullptr) {
  396. get_ok_button()->set_disabled(true);
  397. return;
  398. }
  399. if (dst_method->get_text().is_empty()) {
  400. get_ok_button()->set_disabled(true);
  401. return;
  402. }
  403. get_ok_button()->set_disabled(false);
  404. }
  405. void ConnectDialog::_update_warning_label() {
  406. Node *dst = Object::cast_to<Node>(source)->get_node(dst_path);
  407. if (dst == nullptr) {
  408. warning_label->set_visible(false);
  409. return;
  410. }
  411. Ref<Script> scr = dst->get_script();
  412. if (scr.is_null()) {
  413. warning_label->set_visible(false);
  414. return;
  415. }
  416. ScriptLanguage *language = scr->get_language();
  417. if (language->can_make_function()) {
  418. warning_label->set_visible(false);
  419. return;
  420. }
  421. warning_label->set_text(vformat(TTR("%s: Callback code won't be generated, please add it manually."), language->get_name()));
  422. warning_label->set_visible(true);
  423. }
  424. void ConnectDialog::_post_popup() {
  425. callable_mp((Control *)dst_method, &Control::grab_focus).call_deferred(false);
  426. callable_mp(dst_method, &LineEdit::select_all).call_deferred();
  427. }
  428. void ConnectDialog::_notification(int p_what) {
  429. switch (p_what) {
  430. case NOTIFICATION_ENTER_TREE: {
  431. bind_editor->edit(cdbinds);
  432. [[fallthrough]];
  433. }
  434. case NOTIFICATION_THEME_CHANGED: {
  435. method_search->set_right_icon(get_editor_theme_icon("Search"));
  436. open_method_tree->set_button_icon(get_editor_theme_icon("Edit"));
  437. } break;
  438. }
  439. }
  440. void ConnectDialog::_bind_methods() {
  441. ADD_SIGNAL(MethodInfo("connected"));
  442. }
  443. Object *ConnectDialog::get_source() const {
  444. return source;
  445. }
  446. ConnectDialog::ConnectionData ConnectDialog::get_source_connection_data() const {
  447. return source_connection_data;
  448. }
  449. StringName ConnectDialog::get_signal_name() const {
  450. return signal;
  451. }
  452. PackedStringArray ConnectDialog::get_signal_args() const {
  453. return signal_args;
  454. }
  455. NodePath ConnectDialog::get_dst_path() const {
  456. return dst_path;
  457. }
  458. void ConnectDialog::set_dst_node(Node *p_node) {
  459. tree->set_selected(p_node);
  460. }
  461. StringName ConnectDialog::get_dst_method_name() const {
  462. String txt = dst_method->get_text();
  463. if (txt.contains_char('(')) {
  464. txt = txt.left(txt.find_char('(')).strip_edges();
  465. }
  466. return txt;
  467. }
  468. void ConnectDialog::set_dst_method(const StringName &p_method) {
  469. dst_method->set_text(p_method);
  470. }
  471. int ConnectDialog::get_unbinds() const {
  472. return int(unbind_count->get_value());
  473. }
  474. Vector<Variant> ConnectDialog::get_binds() const {
  475. return cdbinds->params;
  476. }
  477. String ConnectDialog::get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names) {
  478. PackedStringArray signature;
  479. signature.append(p_method.name);
  480. signature.append("(");
  481. for (int64_t i = 0; i < p_method.arguments.size(); ++i) {
  482. if (i > 0) {
  483. signature.append(", ");
  484. }
  485. const PropertyInfo &pi = p_method.arguments[i];
  486. String type_name;
  487. switch (pi.type) {
  488. case Variant::NIL:
  489. type_name = "Variant";
  490. break;
  491. case Variant::INT:
  492. if ((pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && pi.class_name != StringName() && !String(pi.class_name).begins_with("res://")) {
  493. type_name = pi.class_name;
  494. } else {
  495. type_name = "int";
  496. }
  497. break;
  498. case Variant::ARRAY:
  499. if (pi.hint == PROPERTY_HINT_ARRAY_TYPE && !pi.hint_string.is_empty() && !pi.hint_string.begins_with("res://")) {
  500. type_name = "Array[" + pi.hint_string + "]";
  501. } else {
  502. type_name = "Array";
  503. }
  504. break;
  505. case Variant::DICTIONARY:
  506. type_name = "Dictionary";
  507. if (pi.hint == PROPERTY_HINT_DICTIONARY_TYPE && !pi.hint_string.is_empty()) {
  508. String key_hint = pi.hint_string.get_slicec(';', 0);
  509. String value_hint = pi.hint_string.get_slicec(';', 1);
  510. if (key_hint.is_empty() || key_hint.begins_with("res://")) {
  511. key_hint = "Variant";
  512. }
  513. if (value_hint.is_empty() || value_hint.begins_with("res://")) {
  514. value_hint = "Variant";
  515. }
  516. if (key_hint != "Variant" || value_hint != "Variant") {
  517. type_name += "[" + key_hint + ", " + value_hint + "]";
  518. }
  519. }
  520. break;
  521. case Variant::OBJECT:
  522. if (pi.class_name != StringName()) {
  523. type_name = pi.class_name;
  524. } else {
  525. type_name = "Object";
  526. }
  527. break;
  528. default:
  529. type_name = Variant::get_type_name(pi.type);
  530. break;
  531. }
  532. String arg_name = pi.name.is_empty() ? "arg" + itos(i) : pi.name;
  533. signature.append(arg_name + ": " + type_name);
  534. if (r_arg_names) {
  535. r_arg_names->push_back(arg_name + ": " + type_name);
  536. }
  537. }
  538. if (p_method.flags & METHOD_FLAG_VARARG) {
  539. signature.append(p_method.arguments.is_empty() ? "..." : ", ...");
  540. }
  541. signature.append(")");
  542. return String().join(signature);
  543. }
  544. bool ConnectDialog::get_deferred() const {
  545. return deferred->is_pressed();
  546. }
  547. bool ConnectDialog::get_one_shot() const {
  548. return one_shot->is_pressed();
  549. }
  550. bool ConnectDialog::get_append_source() const {
  551. return !append_source->is_disabled() && append_source->is_pressed();
  552. }
  553. /*
  554. * Returns true if ConnectDialog is being used to edit an existing connection.
  555. */
  556. bool ConnectDialog::is_editing() const {
  557. return edit_mode;
  558. }
  559. void ConnectDialog::shortcut_input(const Ref<InputEvent> &p_event) {
  560. const Ref<InputEventKey> &key = p_event;
  561. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  562. if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
  563. filter_nodes->grab_focus();
  564. filter_nodes->select_all();
  565. filter_nodes->accept_event();
  566. }
  567. }
  568. }
  569. /*
  570. * Initialize ConnectDialog and populate fields with expected data.
  571. * If creating a connection from scratch, sensible defaults are used.
  572. * If editing an existing connection, previous data is retained.
  573. */
  574. void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit) {
  575. set_hide_on_ok(false);
  576. source = p_cd.source;
  577. signal = p_cd.signal;
  578. signal_args = p_signal_args;
  579. tree->set_selected(nullptr);
  580. tree->set_marked(Object::cast_to<Node>(source));
  581. if (p_cd.target) {
  582. set_dst_node(Object::cast_to<Node>(p_cd.target));
  583. set_dst_method(p_cd.method);
  584. }
  585. _update_ok_enabled();
  586. bool b_deferred = (p_cd.flags & CONNECT_DEFERRED);
  587. bool b_oneshot = (p_cd.flags & CONNECT_ONE_SHOT);
  588. bool b_append_source = (p_cd.flags & CONNECT_APPEND_SOURCE_OBJECT);
  589. deferred->set_pressed(b_deferred);
  590. one_shot->set_pressed(b_oneshot);
  591. append_source->set_pressed(b_append_source);
  592. unbind_count->set_max(p_signal_args.size());
  593. unbind_count->set_value(p_cd.unbinds);
  594. cdbinds->params.clear();
  595. cdbinds->params = p_cd.binds;
  596. cdbinds->notify_changed();
  597. edit_mode = p_edit;
  598. source_connection_data = p_cd;
  599. }
  600. void ConnectDialog::popup_dialog(const String &p_for_signal) {
  601. from_signal->set_text(p_for_signal);
  602. warning_label->add_theme_color_override(SceneStringName(font_color), warning_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  603. error_label->add_theme_color_override(SceneStringName(font_color), error_label->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  604. filter_nodes->clear();
  605. if (!advanced->is_pressed()) {
  606. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  607. }
  608. if (first_popup) {
  609. first_popup = false;
  610. _advanced_pressed();
  611. }
  612. popup_centered();
  613. }
  614. void ConnectDialog::_advanced_pressed() {
  615. if (advanced->is_pressed()) {
  616. connect_to_label->set_text(TTR("Connect to Node:"));
  617. tree->set_connect_to_script_mode(false);
  618. vbc_right->show();
  619. error_label->hide();
  620. } else {
  621. reset_size();
  622. connect_to_label->set_text(TTR("Connect to Script:"));
  623. tree->set_connect_to_script_mode(true);
  624. vbc_right->hide();
  625. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  626. }
  627. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "use_advanced_connections", advanced->is_pressed());
  628. popup_centered();
  629. }
  630. ConnectDialog::ConnectDialog() {
  631. set_min_size(Size2(0, 500) * EDSCALE);
  632. HBoxContainer *main_hb = memnew(HBoxContainer);
  633. add_child(main_hb);
  634. VBoxContainer *vbc_left = memnew(VBoxContainer);
  635. main_hb->add_child(vbc_left);
  636. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  637. vbc_left->set_custom_minimum_size(Vector2(400 * EDSCALE, 0));
  638. from_signal = memnew(LineEdit);
  639. from_signal->set_accessibility_name(TTRC("From Signal:"));
  640. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  641. from_signal->set_editable(false);
  642. tree = memnew(SceneTreeEditor(false));
  643. tree->set_update_when_invisible(false);
  644. tree->set_connecting_signal(true);
  645. tree->set_show_enabled_subscene(true);
  646. tree->set_v_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
  647. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  648. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  649. tree->set_connect_to_script_mode(true);
  650. tree->get_scene_tree()->set_theme_type_variation("TreeSecondary");
  651. HBoxContainer *hbc_filter = memnew(HBoxContainer);
  652. filter_nodes = memnew(LineEdit);
  653. hbc_filter->add_child(filter_nodes);
  654. filter_nodes->set_h_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
  655. filter_nodes->set_placeholder(TTR("Filter Nodes"));
  656. filter_nodes->set_accessibility_name(TTRC("Filter Nodes"));
  657. filter_nodes->set_clear_button_enabled(true);
  658. filter_nodes->connect(SceneStringName(text_changed), callable_mp(tree, &SceneTreeEditor::set_filter));
  659. Button *focus_current = memnew(Button);
  660. hbc_filter->add_child(focus_current);
  661. focus_current->set_text(TTR("Go to Source"));
  662. focus_current->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_focus_currently_connected));
  663. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), hbc_filter, false);
  664. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  665. vbc_left->add_child(tree);
  666. warning_label = memnew(Label);
  667. warning_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  668. vbc_left->add_child(warning_label);
  669. warning_label->hide();
  670. error_label = memnew(Label);
  671. error_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  672. error_label->set_text(TTR("Scene does not contain any script."));
  673. vbc_left->add_child(error_label);
  674. error_label->hide();
  675. method_popup = memnew(AcceptDialog);
  676. method_popup->set_title(TTR("Select Method"));
  677. method_popup->set_min_size(Vector2(400, 600) * EDSCALE);
  678. add_child(method_popup);
  679. VBoxContainer *method_vbc = memnew(VBoxContainer);
  680. method_popup->add_child(method_vbc);
  681. method_search = memnew(LineEdit);
  682. method_vbc->add_child(method_search);
  683. method_search->set_placeholder(TTR("Filter Methods"));
  684. method_search->set_accessibility_name(TTRC("Filter Methods"));
  685. method_search->set_clear_button_enabled(true);
  686. method_search->connect(SceneStringName(text_changed), callable_mp(this, &ConnectDialog::_update_method_tree).unbind(1));
  687. method_tree = memnew(Tree);
  688. method_vbc->add_child(method_tree);
  689. method_tree->set_accessibility_name(TTRC("Methods"));
  690. method_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  691. method_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  692. method_tree->set_hide_root(true);
  693. method_tree->connect(SceneStringName(item_selected), callable_mp(this, &ConnectDialog::_method_selected));
  694. method_tree->connect("item_activated", callable_mp((Window *)method_popup, &Window::hide));
  695. empty_tree_label = memnew(Label(TTR("No method found matching given filters.")));
  696. method_popup->add_child(empty_tree_label);
  697. empty_tree_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  698. empty_tree_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  699. empty_tree_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  700. script_methods_only = memnew(CheckButton(TTR("Script Methods Only")));
  701. method_vbc->add_child(script_methods_only);
  702. script_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
  703. script_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_script_methods_only", true));
  704. script_methods_only->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(script_methods_only));
  705. compatible_methods_only = memnew(CheckButton(TTR("Compatible Methods Only")));
  706. method_vbc->add_child(compatible_methods_only);
  707. compatible_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
  708. compatible_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_compatible_methods_only", true));
  709. compatible_methods_only->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(compatible_methods_only));
  710. vbc_right = memnew(VBoxContainer);
  711. main_hb->add_child(vbc_right);
  712. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  713. vbc_right->set_custom_minimum_size(Vector2(150 * EDSCALE, 0));
  714. vbc_right->hide();
  715. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  716. type_list = memnew(EditorVariantTypeOptionButton);
  717. type_list->set_accessibility_name(TTRC("Type"));
  718. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  719. type_list->populate({ Variant::NIL, Variant::OBJECT });
  720. add_bind_hb->add_child(type_list);
  721. bind_controls.push_back(type_list);
  722. Button *add_bind = memnew(Button);
  723. add_bind->set_text(TTR("Add"));
  724. add_bind_hb->add_child(add_bind);
  725. add_bind->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_add_bind));
  726. bind_controls.push_back(add_bind);
  727. Button *del_bind = memnew(Button);
  728. del_bind->set_text(TTR("Remove"));
  729. add_bind_hb->add_child(del_bind);
  730. del_bind->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_remove_bind));
  731. bind_controls.push_back(del_bind);
  732. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  733. bind_editor = memnew(EditorInspector);
  734. bind_editor->set_accessibility_name(TTRC("Extra Call Arguments:"));
  735. bind_controls.push_back(bind_editor);
  736. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  737. unbind_count = memnew(SpinBox);
  738. unbind_count->set_tooltip_text(TTR("Allows to drop arguments sent by signal emitter."));
  739. unbind_count->set_accessibility_name(TTRC("Unbind Signal Arguments:"));
  740. vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);
  741. HBoxContainer *hbc_method = memnew(HBoxContainer);
  742. vbc_left->add_margin_child(TTR("Receiver Method:"), hbc_method);
  743. dst_method = memnew(LineEdit);
  744. dst_method->set_accessibility_name(TTRC("Receiver Method"));
  745. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  746. dst_method->connect(SceneStringName(text_changed), callable_mp(method_tree, &Tree::deselect_all).unbind(1));
  747. hbc_method->add_child(dst_method);
  748. register_text_enter(dst_method);
  749. open_method_tree = memnew(Button(TTRC("Pick")));
  750. hbc_method->add_child(open_method_tree);
  751. open_method_tree->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_open_method_popup));
  752. advanced = memnew(CheckButton(TTR("Advanced")));
  753. vbc_left->add_child(advanced);
  754. advanced->set_h_size_flags(Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND);
  755. advanced->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "use_advanced_connections", false));
  756. advanced->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_advanced_pressed));
  757. FlowContainer *fc_flags = memnew(FlowContainer);
  758. vbc_right->add_child(fc_flags);
  759. deferred = memnew(CheckBox);
  760. deferred->set_text(TTR("Deferred"));
  761. deferred->set_tooltip_text(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  762. fc_flags->add_child(deferred);
  763. one_shot = memnew(CheckBox);
  764. one_shot->set_text(TTR("One Shot"));
  765. one_shot->set_tooltip_text(TTR("Disconnects the signal after its first emission."));
  766. fc_flags->add_child(one_shot);
  767. append_source = memnew(CheckBox);
  768. append_source->set_text(TTRC("Append Source"));
  769. append_source->set_tooltip_text(TTRC("The source object is automatically sent when the signal is emitted."));
  770. fc_flags->add_child(append_source);
  771. cdbinds = memnew(ConnectDialogBinds);
  772. error = memnew(AcceptDialog);
  773. add_child(error);
  774. error->set_title(TTR("Cannot connect signal"));
  775. error->set_ok_button_text(TTR("Close"));
  776. set_ok_button_text(TTR("Connect"));
  777. }
  778. ConnectDialog::~ConnectDialog() {
  779. memdelete(cdbinds);
  780. }
  781. //////////////////////////////////////////
  782. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  783. // If it's not a doc tooltip, fallback to the default one.
  784. if (p_text.is_empty() || p_text.contains(" :: ")) {
  785. return nullptr;
  786. }
  787. return EditorHelpBitTooltip::show_tooltip(const_cast<ConnectionsDockTree *>(this), p_text);
  788. }
  789. struct _ConnectionsDockMethodInfoSort {
  790. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  791. return a.name < b.name;
  792. }
  793. };
  794. void ConnectionsDock::_filter_changed(const String &p_text) {
  795. update_tree();
  796. }
  797. /*
  798. * Post-ConnectDialog callback for creating/editing connections.
  799. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  800. */
  801. void ConnectionsDock::_make_or_edit_connection() {
  802. NodePath dst_path = connect_dialog->get_dst_path();
  803. Node *target = Object::cast_to<Node>(selected_object)->get_node(dst_path);
  804. ERR_FAIL_NULL(target);
  805. ConnectDialog::ConnectionData cd;
  806. cd.source = connect_dialog->get_source();
  807. cd.target = target;
  808. cd.signal = connect_dialog->get_signal_name();
  809. cd.method = connect_dialog->get_dst_method_name();
  810. cd.unbinds = connect_dialog->get_unbinds();
  811. cd.binds = connect_dialog->get_binds();
  812. bool b_deferred = connect_dialog->get_deferred();
  813. bool b_oneshot = connect_dialog->get_one_shot();
  814. bool b_append_source = connect_dialog->get_append_source();
  815. cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONE_SHOT : 0) | (b_append_source ? CONNECT_APPEND_SOURCE_OBJECT : 0);
  816. // If the function is found in target's own script, check the editor setting
  817. // to determine if the script should be opened.
  818. // If the function is found in an inherited class or script no need to do anything
  819. // except making a connection.
  820. bool add_script_function_request = false;
  821. Ref<Script> scr = target->get_script();
  822. if (scr.is_valid() && !ClassDB::has_method(target->get_class(), cd.method)) {
  823. // Check in target's own script.
  824. int line = scr->get_language()->find_function(cd.method, scr->get_source_code());
  825. if (line != -1) {
  826. add_script_function_request = EDITOR_GET("text_editor/behavior/navigation/open_script_when_connecting_signal_to_existing_method");
  827. } else {
  828. // There is a chance that the method is inherited from another script.
  829. bool found_inherited_function = false;
  830. Ref<Script> inherited_scr = scr->get_base_script();
  831. while (inherited_scr.is_valid()) {
  832. int inherited_line = inherited_scr->get_language()->find_function(cd.method, inherited_scr->get_source_code());
  833. if (inherited_line != -1) {
  834. found_inherited_function = true;
  835. break;
  836. }
  837. inherited_scr = inherited_scr->get_base_script();
  838. }
  839. add_script_function_request = !found_inherited_function;
  840. }
  841. }
  842. if (add_script_function_request) {
  843. PackedStringArray script_function_args = connect_dialog->get_signal_args();
  844. script_function_args.resize(script_function_args.size() - cd.unbinds);
  845. // Append the source.
  846. if (b_append_source) {
  847. String class_name = cd.source->get_class();
  848. bool found = false;
  849. Ref<Script> source_script = cd.source->get_script();
  850. if (source_script.is_valid()) {
  851. found = source_script->has_script_signal(cd.signal);
  852. if (found) {
  853. // Check global name in script inheritance chain.
  854. bool need_check = found;
  855. Ref<Script> base_script = source_script->get_base_script();
  856. while (base_script.is_valid()) {
  857. need_check = base_script->has_script_signal(cd.signal);
  858. if (!need_check) {
  859. break;
  860. }
  861. source_script = base_script;
  862. base_script = source_script->get_base_script();
  863. }
  864. class_name = source_script->get_global_name();
  865. }
  866. }
  867. if (!found) {
  868. while (!class_name.is_empty()) {
  869. // Search in ClassDB according to the inheritance chain.
  870. found = ClassDB::has_signal(class_name, cd.signal, true);
  871. if (found) {
  872. break;
  873. }
  874. class_name = ClassDB::get_parent_class(class_name);
  875. }
  876. }
  877. script_function_args.push_back("source:" + class_name);
  878. }
  879. for (int i = 0; i < cd.binds.size(); i++) {
  880. script_function_args.push_back("extra_arg_" + itos(i) + ": " + Variant::get_type_name(cd.binds[i].get_type()));
  881. }
  882. EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), cd.target, cd.method, script_function_args);
  883. }
  884. if (connect_dialog->is_editing()) {
  885. _disconnect(connect_dialog->get_source_connection_data());
  886. _connect(cd);
  887. } else {
  888. _connect(cd);
  889. }
  890. update_tree();
  891. }
  892. /*
  893. * Creates single connection w/ undo-redo functionality.
  894. */
  895. void ConnectionsDock::_connect(const ConnectDialog::ConnectionData &p_cd) {
  896. Object *source = p_cd.source;
  897. Node *target = Object::cast_to<Node>(p_cd.target);
  898. if (!source || !target) {
  899. return;
  900. }
  901. Callable callable = p_cd.get_callable();
  902. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  903. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));
  904. undo_redo->add_do_method(source, "connect", p_cd.signal, callable, p_cd.flags);
  905. undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);
  906. undo_redo->add_do_method(this, "update_tree");
  907. undo_redo->add_undo_method(this, "update_tree");
  908. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  909. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  910. undo_redo->commit_action();
  911. }
  912. /*
  913. * Break single connection w/ undo-redo functionality.
  914. */
  915. void ConnectionsDock::_disconnect(const ConnectDialog::ConnectionData &p_cd) {
  916. ERR_FAIL_COND(p_cd.source != selected_object); // Shouldn't happen but... Bugcheck.
  917. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  918. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), p_cd.signal, p_cd.method));
  919. Callable callable = p_cd.get_callable();
  920. undo_redo->add_do_method(selected_object, "disconnect", p_cd.signal, callable);
  921. undo_redo->add_undo_method(selected_object, "connect", p_cd.signal, callable, p_cd.flags);
  922. undo_redo->add_do_method(this, "update_tree");
  923. undo_redo->add_undo_method(this, "update_tree");
  924. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  925. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  926. undo_redo->commit_action();
  927. }
  928. /*
  929. * Break all connections of currently selected signal.
  930. * Can undo-redo as a single action.
  931. */
  932. void ConnectionsDock::_disconnect_all() {
  933. TreeItem *item = tree->get_selected();
  934. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
  935. return;
  936. }
  937. TreeItem *child = item->get_first_child();
  938. String signal_name = item->get_metadata(0).operator Dictionary()["name"];
  939. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  940. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));
  941. while (child) {
  942. Connection connection = child->get_metadata(0);
  943. if (!_is_connection_inherited(connection)) {
  944. ConnectDialog::ConnectionData cd = connection;
  945. undo_redo->add_do_method(selected_object, "disconnect", cd.signal, cd.get_callable());
  946. undo_redo->add_undo_method(selected_object, "connect", cd.signal, cd.get_callable(), cd.flags);
  947. }
  948. child = child->get_next();
  949. }
  950. undo_redo->add_do_method(this, "update_tree");
  951. undo_redo->add_undo_method(this, "update_tree");
  952. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  953. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  954. undo_redo->commit_action();
  955. }
  956. void ConnectionsDock::_tree_item_selected() {
  957. TreeItem *item = tree->get_selected();
  958. if (item && _get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
  959. connect_button->set_text(TTR("Connect..."));
  960. connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));
  961. connect_button->set_disabled(is_editing_resource);
  962. } else if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
  963. connect_button->set_text(TTR("Disconnect"));
  964. connect_button->set_button_icon(get_editor_theme_icon(SNAME("Unlinked")));
  965. Object::Connection connection = item->get_metadata(0);
  966. connect_button->set_disabled(_is_connection_inherited(connection));
  967. } else {
  968. connect_button->set_text(TTR("Connect..."));
  969. connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));
  970. connect_button->set_disabled(true);
  971. }
  972. }
  973. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  974. TreeItem *item = tree->get_selected();
  975. if (!item) {
  976. return;
  977. }
  978. if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
  979. _open_connection_dialog(*item);
  980. } else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
  981. _go_to_method(*item);
  982. }
  983. }
  984. ConnectionsDock::TreeItemType ConnectionsDock::_get_item_type(const TreeItem &p_item) const {
  985. if (&p_item == tree->get_root()) {
  986. return TREE_ITEM_TYPE_ROOT;
  987. } else if (p_item.get_parent() == tree->get_root()) {
  988. return TREE_ITEM_TYPE_CLASS;
  989. } else if (p_item.get_parent()->get_parent() == tree->get_root()) {
  990. return TREE_ITEM_TYPE_SIGNAL;
  991. } else {
  992. return TREE_ITEM_TYPE_CONNECTION;
  993. }
  994. }
  995. bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {
  996. return bool(p_connection.flags & CONNECT_INHERITED);
  997. }
  998. /*
  999. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  1000. */
  1001. void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
  1002. if (is_editing_resource) {
  1003. return;
  1004. }
  1005. const Dictionary sinfo = p_item.get_metadata(0);
  1006. const StringName signal_name = sinfo["name"];
  1007. const PackedStringArray signal_args = sinfo["args"];
  1008. ConnectDialog::ConnectionData cd;
  1009. Node *selected_node = Object::cast_to<Node>(selected_object);
  1010. Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
  1011. if (!dst_node || dst_node->get_script().is_null()) {
  1012. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  1013. }
  1014. cd.source = selected_object;
  1015. cd.target = dst_node;
  1016. cd.signal = signal_name;
  1017. cd.method = ConnectDialog::generate_method_callback_name(cd.source, signal_name, cd.target);
  1018. connect_dialog->init(cd, signal_args);
  1019. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  1020. connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");
  1021. }
  1022. /*
  1023. * Open connection dialog with Connection data to EDIT an existing connection.
  1024. */
  1025. void ConnectionsDock::_open_edit_connection_dialog(TreeItem &p_item) {
  1026. TreeItem *signal_item = p_item.get_parent();
  1027. ERR_FAIL_NULL(signal_item);
  1028. Connection connection = p_item.get_metadata(0);
  1029. ConnectDialog::ConnectionData cd = connection;
  1030. Object *src = cd.source;
  1031. Object *dst = cd.target;
  1032. if (src && dst) {
  1033. const StringName &signal_name = cd.signal;
  1034. const PackedStringArray signal_args = signal_item->get_metadata(0).operator Dictionary()["args"];
  1035. connect_dialog->init(cd, signal_args, true);
  1036. connect_dialog->set_title(vformat(TTR("Edit Connection: '%s'"), cd.signal));
  1037. connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");
  1038. }
  1039. }
  1040. /*
  1041. * Open slot method location in script editor.
  1042. */
  1043. void ConnectionsDock::_go_to_method(TreeItem &p_item) {
  1044. if (_get_item_type(p_item) != TREE_ITEM_TYPE_CONNECTION) {
  1045. return;
  1046. }
  1047. Connection connection = p_item.get_metadata(0);
  1048. ConnectDialog::ConnectionData cd = connection;
  1049. ERR_FAIL_COND(cd.source != selected_object); // Shouldn't happen but... bugcheck.
  1050. if (!cd.target) {
  1051. return;
  1052. }
  1053. Ref<Script> scr = cd.target->get_script();
  1054. if (scr.is_null()) {
  1055. return;
  1056. }
  1057. if (scr.is_valid() && ScriptEditor::get_singleton()->script_goto_method(scr, cd.method)) {
  1058. EditorNode::get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1059. }
  1060. }
  1061. void ConnectionsDock::_handle_class_menu_option(int p_option) {
  1062. switch (p_option) {
  1063. case CLASS_MENU_OPEN_DOCS:
  1064. ScriptEditor::get_singleton()->goto_help("class:" + class_menu_doc_class_name);
  1065. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1066. break;
  1067. }
  1068. }
  1069. void ConnectionsDock::_class_menu_about_to_popup() {
  1070. class_menu->set_item_disabled(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), class_menu_doc_class_name.is_empty());
  1071. }
  1072. void ConnectionsDock::_handle_signal_menu_option(int p_option) {
  1073. TreeItem *item = tree->get_selected();
  1074. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
  1075. return;
  1076. }
  1077. Dictionary meta = item->get_metadata(0);
  1078. switch (p_option) {
  1079. case SIGNAL_MENU_CONNECT: {
  1080. _open_connection_dialog(*item);
  1081. } break;
  1082. case SIGNAL_MENU_DISCONNECT_ALL: {
  1083. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), meta["name"]));
  1084. disconnect_all_dialog->popup_centered();
  1085. } break;
  1086. case SIGNAL_MENU_COPY_NAME: {
  1087. DisplayServer::get_singleton()->clipboard_set(meta["name"]);
  1088. } break;
  1089. case SIGNAL_MENU_OPEN_DOCS: {
  1090. ScriptEditor::get_singleton()->goto_help("class_signal:" + String(meta["class"]) + ":" + String(meta["name"]));
  1091. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1092. } break;
  1093. }
  1094. }
  1095. void ConnectionsDock::_signal_menu_about_to_popup() {
  1096. TreeItem *item = tree->get_selected();
  1097. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
  1098. return;
  1099. }
  1100. Dictionary meta = item->get_metadata(0);
  1101. bool disable_disconnect_all = true;
  1102. for (int i = 0; i < item->get_child_count(); i++) {
  1103. if (!item->get_child(i)->has_meta("_inherited_connection")) {
  1104. disable_disconnect_all = false;
  1105. }
  1106. }
  1107. signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_CONNECT), is_editing_resource);
  1108. signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), disable_disconnect_all);
  1109. signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), String(meta["class"]).is_empty());
  1110. }
  1111. void ConnectionsDock::_handle_slot_menu_option(int p_option) {
  1112. TreeItem *item = tree->get_selected();
  1113. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
  1114. return;
  1115. }
  1116. switch (p_option) {
  1117. case SLOT_MENU_EDIT: {
  1118. _open_edit_connection_dialog(*item);
  1119. } break;
  1120. case SLOT_MENU_GO_TO_METHOD: {
  1121. _go_to_method(*item);
  1122. } break;
  1123. case SLOT_MENU_DISCONNECT: {
  1124. Connection connection = item->get_metadata(0);
  1125. _disconnect(connection);
  1126. update_tree();
  1127. } break;
  1128. }
  1129. }
  1130. void ConnectionsDock::_slot_menu_about_to_popup() {
  1131. TreeItem *item = tree->get_selected();
  1132. if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
  1133. return;
  1134. }
  1135. bool connection_is_inherited = item->has_meta("_inherited_connection");
  1136. slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_EDIT), connection_is_inherited);
  1137. slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), connection_is_inherited);
  1138. }
  1139. void ConnectionsDock::_tree_gui_input(const Ref<InputEvent> &p_event) {
  1140. TreeItem *item = nullptr;
  1141. Point2 item_pos;
  1142. const Ref<InputEventKey> &key = p_event;
  1143. if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
  1144. if (ED_IS_SHORTCUT("connections_editor/disconnect", p_event)) {
  1145. item = tree->get_selected();
  1146. if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
  1147. Connection connection = item->get_metadata(0);
  1148. _disconnect(connection);
  1149. update_tree();
  1150. // Stop the Delete input from propagating elsewhere.
  1151. accept_event();
  1152. return;
  1153. }
  1154. } else if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
  1155. search_box->grab_focus();
  1156. search_box->select_all();
  1157. accept_event();
  1158. return;
  1159. }
  1160. }
  1161. if (key.is_valid() && key->is_pressed() && key->is_action("ui_menu", true)) {
  1162. item = tree->get_selected();
  1163. if (!item) {
  1164. return;
  1165. }
  1166. item_pos = tree->get_item_rect(item).position;
  1167. }
  1168. // Handle RMB press.
  1169. const Ref<InputEventMouseButton> &mb_event = p_event;
  1170. if (mb_event.is_valid() && mb_event->is_pressed() && mb_event->get_button_index() == MouseButton::RIGHT) {
  1171. item = tree->get_item_at_position(mb_event->get_position());
  1172. if (!item) {
  1173. return;
  1174. }
  1175. item_pos = mb_event->get_position();
  1176. }
  1177. if (item) {
  1178. if (item->is_selectable(0)) {
  1179. // Update selection now, before `about_to_popup` signal. Needed for SIGNAL and CONNECTION context menus.
  1180. tree->set_selected(item);
  1181. }
  1182. Vector2 screen_position = tree->get_screen_position() + item_pos;
  1183. switch (_get_item_type(*item)) {
  1184. case TREE_ITEM_TYPE_ROOT:
  1185. break;
  1186. case TREE_ITEM_TYPE_CLASS:
  1187. class_menu_doc_class_name = item->get_metadata(0);
  1188. class_menu->set_position(screen_position);
  1189. class_menu->reset_size();
  1190. class_menu->popup();
  1191. accept_event(); // Don't collapse item.
  1192. break;
  1193. case TREE_ITEM_TYPE_SIGNAL:
  1194. signal_menu->set_position(screen_position);
  1195. signal_menu->reset_size();
  1196. signal_menu->popup();
  1197. break;
  1198. case TREE_ITEM_TYPE_CONNECTION:
  1199. slot_menu->set_position(screen_position);
  1200. slot_menu->reset_size();
  1201. slot_menu->popup();
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. void ConnectionsDock::_close() {
  1207. hide();
  1208. }
  1209. void ConnectionsDock::_connect_pressed() {
  1210. TreeItem *item = tree->get_selected();
  1211. if (!item) {
  1212. connect_button->set_disabled(true);
  1213. return;
  1214. }
  1215. if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
  1216. _open_connection_dialog(*item);
  1217. } else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
  1218. Connection connection = item->get_metadata(0);
  1219. _disconnect(connection);
  1220. update_tree();
  1221. }
  1222. }
  1223. void ConnectionsDock::_notification(int p_what) {
  1224. switch (p_what) {
  1225. case NOTIFICATION_THEME_CHANGED: {
  1226. search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  1227. class_menu->set_item_icon(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
  1228. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_CONNECT), get_editor_theme_icon(SNAME("Instance")));
  1229. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), get_editor_theme_icon(SNAME("Unlinked")));
  1230. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_COPY_NAME), get_editor_theme_icon(SNAME("ActionCopy")));
  1231. signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
  1232. slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_EDIT), get_editor_theme_icon(SNAME("Edit")));
  1233. slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_GO_TO_METHOD), get_editor_theme_icon(SNAME("ArrowRight")));
  1234. slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), get_editor_theme_icon(SNAME("Unlinked")));
  1235. tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  1236. update_tree();
  1237. } break;
  1238. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1239. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editors")) {
  1240. update_tree();
  1241. }
  1242. } break;
  1243. }
  1244. }
  1245. void ConnectionsDock::_bind_methods() {
  1246. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  1247. }
  1248. void ConnectionsDock::set_selection(const Vector<Object *> &p_objects) {
  1249. if (p_objects.size() != 1) {
  1250. select_a_node->show();
  1251. holder->hide();
  1252. selected_object = nullptr;
  1253. } else {
  1254. select_a_node->hide();
  1255. holder->show();
  1256. selected_object = p_objects[0];
  1257. }
  1258. is_editing_resource = (Object::cast_to<Resource>(selected_object) != nullptr);
  1259. update_tree();
  1260. }
  1261. void ConnectionsDock::update_tree() {
  1262. String prev_selected;
  1263. if (tree->is_anything_selected()) {
  1264. prev_selected = tree->get_selected()->get_text(0);
  1265. }
  1266. tree->clear();
  1267. if (!selected_object) {
  1268. return;
  1269. }
  1270. TreeItem *root = tree->create_item();
  1271. DocTools *doc_data = EditorHelp::get_doc_data();
  1272. EditorData &editor_data = EditorNode::get_editor_data();
  1273. StringName native_base = selected_object->get_class();
  1274. Ref<Script> script_base = selected_object->get_script();
  1275. while (native_base != StringName()) {
  1276. String class_name;
  1277. String doc_class_name;
  1278. Ref<Texture2D> class_icon;
  1279. List<MethodInfo> class_signals;
  1280. if (script_base.is_valid()) {
  1281. class_name = script_base->get_global_name();
  1282. if (class_name.is_empty()) {
  1283. class_name = script_base->get_path().get_file();
  1284. }
  1285. doc_class_name = script_base->get_global_name();
  1286. if (doc_class_name.is_empty()) {
  1287. doc_class_name = script_base->get_path().trim_prefix("res://").quote();
  1288. }
  1289. if (!doc_class_name.is_empty() && !doc_data->class_list.find(doc_class_name)) {
  1290. doc_class_name = String();
  1291. }
  1292. class_icon = editor_data.get_script_icon(script_base->get_path());
  1293. if (class_icon.is_null() && has_theme_icon(native_base, EditorStringName(EditorIcons))) {
  1294. class_icon = get_editor_theme_icon(native_base);
  1295. }
  1296. script_base->get_script_signal_list(&class_signals);
  1297. // TODO: Core: Add optional parameter to ignore base classes (no_inheritance like in ClassDB).
  1298. Ref<Script> base = script_base->get_base_script();
  1299. if (base.is_valid()) {
  1300. List<MethodInfo> base_signals;
  1301. base->get_script_signal_list(&base_signals);
  1302. HashSet<String> base_signal_names;
  1303. for (const MethodInfo &signal : base_signals) {
  1304. base_signal_names.insert(signal.name);
  1305. }
  1306. for (List<MethodInfo>::Element *F = class_signals.front(); F;) {
  1307. List<MethodInfo>::Element *N = F->next();
  1308. if (base_signal_names.has(F->get().name)) {
  1309. class_signals.erase(F);
  1310. }
  1311. F = N;
  1312. }
  1313. }
  1314. script_base = base;
  1315. } else {
  1316. class_name = native_base;
  1317. doc_class_name = native_base;
  1318. if (!doc_data->class_list.find(doc_class_name)) {
  1319. doc_class_name = String();
  1320. }
  1321. if (has_theme_icon(native_base, EditorStringName(EditorIcons))) {
  1322. class_icon = get_editor_theme_icon(native_base);
  1323. }
  1324. ClassDB::get_signal_list(native_base, &class_signals, true);
  1325. native_base = ClassDB::get_parent_class(native_base);
  1326. }
  1327. if (class_icon.is_null()) {
  1328. class_icon = get_editor_theme_icon(SNAME("Object"));
  1329. }
  1330. TreeItem *section_item = nullptr;
  1331. // Create subsections.
  1332. if (!class_signals.is_empty()) {
  1333. class_signals.sort();
  1334. section_item = tree->create_item(root);
  1335. section_item->set_text(0, class_name);
  1336. // `|` separators used in `EditorHelpBit`.
  1337. section_item->set_tooltip_text(0, "class|" + doc_class_name + "|");
  1338. section_item->set_icon(0, class_icon);
  1339. section_item->set_selectable(0, false);
  1340. section_item->set_editable(0, false);
  1341. section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  1342. section_item->set_custom_stylebox(0, get_theme_stylebox(SNAME("prop_subsection_stylebox"), EditorStringName(Editor)));
  1343. section_item->set_metadata(0, doc_class_name);
  1344. }
  1345. for (MethodInfo &mi : class_signals) {
  1346. const StringName &signal_name = mi.name;
  1347. if (!search_box->get_text().is_subsequence_ofn(signal_name)) {
  1348. continue;
  1349. }
  1350. PackedStringArray argnames;
  1351. // Create the children of the subsection - the actual list of signals.
  1352. TreeItem *signal_item = tree->create_item(section_item);
  1353. String signame = connect_dialog->get_signature(mi, &argnames);
  1354. signal_item->set_text(0, signame);
  1355. if (signame == prev_selected) {
  1356. signal_item->select(0);
  1357. prev_selected = "";
  1358. }
  1359. Dictionary sinfo;
  1360. sinfo["class"] = doc_class_name;
  1361. sinfo["name"] = signal_name;
  1362. sinfo["args"] = argnames;
  1363. signal_item->set_metadata(0, sinfo);
  1364. signal_item->set_icon(0, get_editor_theme_icon(SNAME("Signal")));
  1365. // `|` separators used in `EditorHelpBit`.
  1366. signal_item->set_tooltip_text(0, "signal|" + doc_class_name + "|" + String(signal_name));
  1367. // List existing connections.
  1368. List<Object::Connection> existing_connections;
  1369. selected_object->get_signal_connection_list(signal_name, &existing_connections);
  1370. for (const Object::Connection &F : existing_connections) {
  1371. Connection connection = F;
  1372. if (!(connection.flags & CONNECT_PERSIST)) {
  1373. continue;
  1374. }
  1375. ConnectDialog::ConnectionData cd = connection;
  1376. Node *target = Object::cast_to<Node>(cd.target);
  1377. if (!target) {
  1378. continue;
  1379. }
  1380. String path = String(Object::cast_to<Node>(selected_object)->get_path_to(target)) + " :: " + cd.method + "()";
  1381. if (cd.flags & CONNECT_DEFERRED) {
  1382. path += " (deferred)";
  1383. }
  1384. if (cd.flags & CONNECT_ONE_SHOT) {
  1385. path += " (one-shot)";
  1386. }
  1387. if (cd.flags & CONNECT_APPEND_SOURCE_OBJECT) {
  1388. path += " (source)";
  1389. }
  1390. if (cd.unbinds > 0) {
  1391. path += " unbinds(" + itos(cd.unbinds) + ")";
  1392. }
  1393. if (!cd.binds.is_empty()) {
  1394. path += " binds(";
  1395. for (int i = 0; i < cd.binds.size(); i++) {
  1396. if (i > 0) {
  1397. path += ", ";
  1398. }
  1399. path += cd.binds[i].operator String();
  1400. }
  1401. path += ")";
  1402. }
  1403. TreeItem *connection_item = tree->create_item(signal_item);
  1404. connection_item->set_text(0, path);
  1405. connection_item->set_metadata(0, connection);
  1406. connection_item->set_icon(0, get_editor_theme_icon(SNAME("Slot")));
  1407. if (_is_connection_inherited(connection)) {
  1408. // The scene inherits this connection.
  1409. connection_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  1410. connection_item->set_meta("_inherited_connection", true);
  1411. }
  1412. }
  1413. }
  1414. }
  1415. connect_button->set_text(TTRC("Connect..."));
  1416. connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));
  1417. connect_button->set_disabled(true);
  1418. }
  1419. ConnectionsDock::ConnectionsDock() {
  1420. set_name(TTR("Signals"));
  1421. holder = memnew(VBoxContainer);
  1422. holder->set_v_size_flags(SIZE_EXPAND_FILL);
  1423. holder->hide();
  1424. add_child(holder);
  1425. search_box = memnew(LineEdit);
  1426. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1427. search_box->set_placeholder(TTR("Filter Signals"));
  1428. search_box->set_accessibility_name(TTRC("Filter Signals"));
  1429. search_box->set_clear_button_enabled(true);
  1430. search_box->connect(SceneStringName(text_changed), callable_mp(this, &ConnectionsDock::_filter_changed));
  1431. holder->add_child(search_box);
  1432. tree = memnew(ConnectionsDockTree);
  1433. tree->set_accessibility_name(TTRC("Connections"));
  1434. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1435. tree->set_columns(1);
  1436. tree->set_select_mode(Tree::SELECT_ROW);
  1437. tree->set_hide_root(true);
  1438. tree->set_column_clip_content(0, true);
  1439. holder->add_child(tree);
  1440. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1441. tree->set_allow_rmb_select(true);
  1442. connect_button = memnew(Button);
  1443. connect_button->set_accessibility_name(TTRC("Connect"));
  1444. HBoxContainer *hb = memnew(HBoxContainer);
  1445. holder->add_child(hb);
  1446. hb->add_spacer();
  1447. hb->add_child(connect_button);
  1448. connect_button->connect(SceneStringName(pressed), callable_mp(this, &ConnectionsDock::_connect_pressed));
  1449. connect_dialog = memnew(ConnectDialog);
  1450. connect_dialog->set_process_shortcut_input(true);
  1451. holder->add_child(connect_dialog);
  1452. disconnect_all_dialog = memnew(ConfirmationDialog);
  1453. holder->add_child(disconnect_all_dialog);
  1454. disconnect_all_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ConnectionsDock::_disconnect_all));
  1455. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  1456. class_menu = memnew(PopupMenu);
  1457. class_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_class_menu_option));
  1458. class_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_class_menu_about_to_popup));
  1459. class_menu->add_item(TTR("Open Documentation"), CLASS_MENU_OPEN_DOCS);
  1460. holder->add_child(class_menu);
  1461. signal_menu = memnew(PopupMenu);
  1462. signal_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  1463. signal_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_signal_menu_about_to_popup));
  1464. signal_menu->add_item(TTR("Connect..."), SIGNAL_MENU_CONNECT);
  1465. signal_menu->add_item(TTR("Disconnect All"), SIGNAL_MENU_DISCONNECT_ALL);
  1466. signal_menu->add_item(TTR("Copy Name"), SIGNAL_MENU_COPY_NAME);
  1467. signal_menu->add_separator();
  1468. signal_menu->add_item(TTR("Open Documentation"), SIGNAL_MENU_OPEN_DOCS);
  1469. holder->add_child(signal_menu);
  1470. slot_menu = memnew(PopupMenu);
  1471. slot_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  1472. slot_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_slot_menu_about_to_popup));
  1473. slot_menu->add_item(TTR("Edit..."), SLOT_MENU_EDIT);
  1474. slot_menu->add_item(TTR("Go to Method"), SLOT_MENU_GO_TO_METHOD);
  1475. slot_menu->add_shortcut(ED_SHORTCUT("connections_editor/disconnect", TTRC("Disconnect"), Key::KEY_DELETE), SLOT_MENU_DISCONNECT);
  1476. holder->add_child(slot_menu);
  1477. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  1478. tree->connect(SceneStringName(item_selected), callable_mp(this, &ConnectionsDock::_tree_item_selected));
  1479. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  1480. tree->connect(SceneStringName(gui_input), callable_mp(this, &ConnectionsDock::_tree_gui_input));
  1481. add_theme_constant_override("separation", 3 * EDSCALE);
  1482. select_a_node = memnew(Label);
  1483. select_a_node->set_focus_mode(FOCUS_ACCESSIBILITY);
  1484. select_a_node->set_text(TTRC("Select a single node or resource to edit its signals."));
  1485. select_a_node->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1486. select_a_node->set_v_size_flags(SIZE_EXPAND_FILL);
  1487. select_a_node->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1488. select_a_node->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1489. select_a_node->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  1490. add_child(select_a_node);
  1491. }