connections_dialog.cpp 54 KB

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