connections_dialog.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /*************************************************************************/
  2. /* connections_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "connections_dialog.h"
  31. #include "editor/doc_tools.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/scene_tree_dock.h"
  36. #include "plugins/script_editor_plugin.h"
  37. static Node *_find_first_script(Node *p_root, Node *p_node) {
  38. if (p_node != p_root && p_node->get_owner() != p_root) {
  39. return nullptr;
  40. }
  41. if (!p_node->get_script().is_null()) {
  42. return p_node;
  43. }
  44. for (int i = 0; i < p_node->get_child_count(); i++) {
  45. Node *ret = _find_first_script(p_root, p_node->get_child(i));
  46. if (ret) {
  47. return ret;
  48. }
  49. }
  50. return nullptr;
  51. }
  52. class ConnectDialogBinds : public Object {
  53. GDCLASS(ConnectDialogBinds, Object);
  54. public:
  55. Vector<Variant> params;
  56. bool _set(const StringName &p_name, const Variant &p_value) {
  57. String name = p_name;
  58. if (name.begins_with("bind/argument_")) {
  59. int which = name.get_slice("_", 1).to_int() - 1;
  60. ERR_FAIL_INDEX_V(which, params.size(), false);
  61. params.write[which] = p_value;
  62. } else {
  63. return false;
  64. }
  65. return true;
  66. }
  67. bool _get(const StringName &p_name, Variant &r_ret) const {
  68. String name = p_name;
  69. if (name.begins_with("bind/argument_")) {
  70. int which = name.get_slice("_", 1).to_int() - 1;
  71. ERR_FAIL_INDEX_V(which, params.size(), false);
  72. r_ret = params[which];
  73. } else {
  74. return false;
  75. }
  76. return true;
  77. }
  78. void _get_property_list(List<PropertyInfo> *p_list) const {
  79. for (int i = 0; i < params.size(); i++) {
  80. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));
  81. }
  82. }
  83. void notify_changed() {
  84. notify_property_list_changed();
  85. }
  86. ConnectDialogBinds() {
  87. }
  88. };
  89. /*
  90. * Signal automatically called by parent dialog.
  91. */
  92. void ConnectDialog::ok_pressed() {
  93. String method_name = dst_method->get_text();
  94. if (method_name.is_empty()) {
  95. error->set_text(TTR("Method in target node must be specified."));
  96. error->popup_centered();
  97. return;
  98. }
  99. if (!method_name.strip_edges().is_valid_identifier()) {
  100. error->set_text(TTR("Method name must be a valid identifier."));
  101. error->popup_centered();
  102. return;
  103. }
  104. Node *target = tree->get_selected();
  105. if (!target) {
  106. return; // Nothing selected in the tree, not an error.
  107. }
  108. if (target->get_script().is_null()) {
  109. if (!target->has_method(method_name)) {
  110. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  111. error->popup_centered();
  112. return;
  113. }
  114. }
  115. emit_signal(SNAME("connected"));
  116. hide();
  117. }
  118. void ConnectDialog::_cancel_pressed() {
  119. hide();
  120. }
  121. void ConnectDialog::_item_activated() {
  122. _ok_pressed(); // From AcceptDialog.
  123. }
  124. void ConnectDialog::_text_submitted(const String &p_text) {
  125. _ok_pressed(); // From AcceptDialog.
  126. }
  127. /*
  128. * Called each time a target node is selected within the target node tree.
  129. */
  130. void ConnectDialog::_tree_node_selected() {
  131. Node *current = tree->get_selected();
  132. if (!current) {
  133. return;
  134. }
  135. dst_path = source->get_path_to(current);
  136. _update_ok_enabled();
  137. }
  138. void ConnectDialog::_unbind_count_changed(double p_count) {
  139. for (Control *control : bind_controls) {
  140. BaseButton *b = Object::cast_to<BaseButton>(control);
  141. if (b) {
  142. b->set_disabled(p_count > 0);
  143. }
  144. EditorInspector *e = Object::cast_to<EditorInspector>(control);
  145. if (e) {
  146. e->set_read_only(p_count > 0);
  147. }
  148. }
  149. }
  150. /*
  151. * Adds a new parameter bind to connection.
  152. */
  153. void ConnectDialog::_add_bind() {
  154. Variant::Type type = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  155. Variant value;
  156. Callable::CallError error;
  157. Variant::construct(type, value, nullptr, 0, error);
  158. cdbinds->params.push_back(value);
  159. cdbinds->notify_changed();
  160. }
  161. /*
  162. * Remove parameter bind from connection.
  163. */
  164. void ConnectDialog::_remove_bind() {
  165. String st = bind_editor->get_selected_path();
  166. if (st.is_empty()) {
  167. return;
  168. }
  169. int idx = st.get_slice("/", 1).to_int() - 1;
  170. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  171. cdbinds->params.remove_at(idx);
  172. cdbinds->notify_changed();
  173. }
  174. /*
  175. * Enables or disables the connect button. The connect button is enabled if a
  176. * node is selected and valid in the selected mode.
  177. */
  178. void ConnectDialog::_update_ok_enabled() {
  179. Node *target = tree->get_selected();
  180. if (target == nullptr) {
  181. get_ok_button()->set_disabled(true);
  182. return;
  183. }
  184. if (!advanced->is_pressed() && target->get_script().is_null()) {
  185. get_ok_button()->set_disabled(true);
  186. return;
  187. }
  188. get_ok_button()->set_disabled(false);
  189. }
  190. void ConnectDialog::_notification(int p_what) {
  191. switch (p_what) {
  192. case NOTIFICATION_ENTER_TREE: {
  193. bind_editor->edit(cdbinds);
  194. [[fallthrough]];
  195. }
  196. case NOTIFICATION_THEME_CHANGED: {
  197. for (int i = 0; i < type_list->get_item_count(); i++) {
  198. String type_name = Variant::get_type_name((Variant::Type)type_list->get_item_id(i));
  199. type_list->set_item_icon(i, get_theme_icon(type_name, SNAME("EditorIcons")));
  200. }
  201. } break;
  202. }
  203. }
  204. void ConnectDialog::_bind_methods() {
  205. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  206. ClassDB::bind_method("_update_ok_enabled", &ConnectDialog::_update_ok_enabled);
  207. ADD_SIGNAL(MethodInfo("connected"));
  208. }
  209. Node *ConnectDialog::get_source() const {
  210. return source;
  211. }
  212. StringName ConnectDialog::get_signal_name() const {
  213. return signal;
  214. }
  215. NodePath ConnectDialog::get_dst_path() const {
  216. return dst_path;
  217. }
  218. void ConnectDialog::set_dst_node(Node *p_node) {
  219. tree->set_selected(p_node);
  220. }
  221. StringName ConnectDialog::get_dst_method_name() const {
  222. String txt = dst_method->get_text();
  223. if (txt.contains("(")) {
  224. txt = txt.left(txt.find("(")).strip_edges();
  225. }
  226. return txt;
  227. }
  228. void ConnectDialog::set_dst_method(const StringName &p_method) {
  229. dst_method->set_text(p_method);
  230. }
  231. int ConnectDialog::get_unbinds() const {
  232. return int(unbind_count->get_value());
  233. }
  234. Vector<Variant> ConnectDialog::get_binds() const {
  235. return cdbinds->params;
  236. }
  237. bool ConnectDialog::get_deferred() const {
  238. return deferred->is_pressed();
  239. }
  240. bool ConnectDialog::get_oneshot() const {
  241. return oneshot->is_pressed();
  242. }
  243. /*
  244. * Returns true if ConnectDialog is being used to edit an existing connection.
  245. */
  246. bool ConnectDialog::is_editing() const {
  247. return edit_mode;
  248. }
  249. /*
  250. * Initialize ConnectDialog and populate fields with expected data.
  251. * If creating a connection from scratch, sensible defaults are used.
  252. * If editing an existing connection, previous data is retained.
  253. */
  254. void ConnectDialog::init(ConnectionData p_cd, bool p_edit) {
  255. set_hide_on_ok(false);
  256. source = static_cast<Node *>(p_cd.source);
  257. signal = p_cd.signal;
  258. tree->set_selected(nullptr);
  259. tree->set_marked(source, true);
  260. if (p_cd.target) {
  261. set_dst_node(static_cast<Node *>(p_cd.target));
  262. set_dst_method(p_cd.method);
  263. }
  264. _update_ok_enabled();
  265. bool b_deferred = (p_cd.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  266. bool b_oneshot = (p_cd.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT;
  267. deferred->set_pressed(b_deferred);
  268. oneshot->set_pressed(b_oneshot);
  269. MethodInfo r_signal;
  270. Ref<Script> source_script = source->get_script();
  271. if (source_script.is_valid() && source_script->has_script_signal(signal)) {
  272. List<MethodInfo> signals;
  273. source_script->get_script_signal_list(&signals);
  274. for (MethodInfo &mi : signals) {
  275. if (mi.name == signal) {
  276. r_signal = mi;
  277. break;
  278. }
  279. }
  280. } else {
  281. ClassDB::get_signal(source->get_class(), signal, &r_signal);
  282. }
  283. unbind_count->set_max(r_signal.arguments.size());
  284. unbind_count->set_value(p_cd.unbinds);
  285. _unbind_count_changed(p_cd.unbinds);
  286. cdbinds->params.clear();
  287. cdbinds->params = p_cd.binds;
  288. cdbinds->notify_changed();
  289. edit_mode = p_edit;
  290. }
  291. void ConnectDialog::popup_dialog(const String &p_for_signal) {
  292. from_signal->set_text(p_for_signal);
  293. error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  294. if (!advanced->is_pressed()) {
  295. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  296. }
  297. popup_centered();
  298. }
  299. void ConnectDialog::_advanced_pressed() {
  300. if (advanced->is_pressed()) {
  301. set_min_size(Size2(900, 500) * EDSCALE);
  302. connect_to_label->set_text(TTR("Connect to Node:"));
  303. tree->set_connect_to_script_mode(false);
  304. vbc_right->show();
  305. error_label->hide();
  306. } else {
  307. set_min_size(Size2(600, 500) * EDSCALE);
  308. reset_size();
  309. connect_to_label->set_text(TTR("Connect to Script:"));
  310. tree->set_connect_to_script_mode(true);
  311. vbc_right->hide();
  312. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  313. }
  314. _update_ok_enabled();
  315. popup_centered();
  316. }
  317. ConnectDialog::ConnectDialog() {
  318. set_min_size(Size2(600, 500) * EDSCALE);
  319. VBoxContainer *vbc = memnew(VBoxContainer);
  320. add_child(vbc);
  321. HBoxContainer *main_hb = memnew(HBoxContainer);
  322. vbc->add_child(main_hb);
  323. main_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  324. VBoxContainer *vbc_left = memnew(VBoxContainer);
  325. main_hb->add_child(vbc_left);
  326. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  327. from_signal = memnew(LineEdit);
  328. from_signal->set_editable(false);
  329. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  330. tree = memnew(SceneTreeEditor(false));
  331. tree->set_connecting_signal(true);
  332. tree->set_show_enabled_subscene(true);
  333. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  334. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  335. tree->set_connect_to_script_mode(true);
  336. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), tree, true);
  337. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  338. error_label = memnew(Label);
  339. error_label->set_text(TTR("Scene does not contain any script."));
  340. vbc_left->add_child(error_label);
  341. error_label->hide();
  342. vbc_right = memnew(VBoxContainer);
  343. main_hb->add_child(vbc_right);
  344. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  345. vbc_right->hide();
  346. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  347. type_list = memnew(OptionButton);
  348. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  349. add_bind_hb->add_child(type_list);
  350. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  351. if (i == Variant::NIL || i == Variant::OBJECT || i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  352. // These types can't be constructed or serialized properly, so skip them.
  353. continue;
  354. }
  355. type_list->add_item(Variant::get_type_name(Variant::Type(i)), i);
  356. }
  357. bind_controls.push_back(type_list);
  358. Button *add_bind = memnew(Button);
  359. add_bind->set_text(TTR("Add"));
  360. add_bind_hb->add_child(add_bind);
  361. add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind));
  362. bind_controls.push_back(add_bind);
  363. Button *del_bind = memnew(Button);
  364. del_bind->set_text(TTR("Remove"));
  365. add_bind_hb->add_child(del_bind);
  366. del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind));
  367. bind_controls.push_back(del_bind);
  368. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  369. bind_editor = memnew(EditorInspector);
  370. bind_controls.push_back(bind_editor);
  371. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  372. unbind_count = memnew(SpinBox);
  373. unbind_count->set_tooltip(TTR("Allows to drop arguments sent by signal emitter."));
  374. unbind_count->connect("value_changed", callable_mp(this, &ConnectDialog::_unbind_count_changed));
  375. vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);
  376. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  377. vbc_left->add_margin_child(TTR("Receiver Method:"), dstm_hb);
  378. dst_method = memnew(LineEdit);
  379. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  380. dst_method->connect("text_submitted", callable_mp(this, &ConnectDialog::_text_submitted));
  381. dstm_hb->add_child(dst_method);
  382. advanced = memnew(CheckButton);
  383. dstm_hb->add_child(advanced);
  384. advanced->set_text(TTR("Advanced"));
  385. advanced->connect("pressed", callable_mp(this, &ConnectDialog::_advanced_pressed));
  386. deferred = memnew(CheckBox);
  387. deferred->set_h_size_flags(0);
  388. deferred->set_text(TTR("Deferred"));
  389. deferred->set_tooltip(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  390. vbc_right->add_child(deferred);
  391. oneshot = memnew(CheckBox);
  392. oneshot->set_h_size_flags(0);
  393. oneshot->set_text(TTR("Oneshot"));
  394. oneshot->set_tooltip(TTR("Disconnects the signal after its first emission."));
  395. vbc_right->add_child(oneshot);
  396. cdbinds = memnew(ConnectDialogBinds);
  397. error = memnew(AcceptDialog);
  398. add_child(error);
  399. error->set_title(TTR("Cannot connect signal"));
  400. error->get_ok_button()->set_text(TTR("Close"));
  401. get_ok_button()->set_text(TTR("Connect"));
  402. }
  403. ConnectDialog::~ConnectDialog() {
  404. memdelete(cdbinds);
  405. }
  406. //////////////////////////////////////////
  407. // Originally copied and adapted from EditorProperty, try to keep style in sync.
  408. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  409. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  410. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  411. // p_text is expected to be something like this:
  412. // "gui_input::(event: InputEvent)::<Signal description>"
  413. // with the latter being possibly empty.
  414. PackedStringArray slices = p_text.split("::", false);
  415. if (slices.size() < 2) {
  416. // Shouldn't happen here, but just in case pass the text along.
  417. help_bit->set_text(p_text);
  418. return help_bit;
  419. }
  420. String text = TTR("Signal:") + " [u][b]" + slices[0] + "[/b][/u]";
  421. text += slices[1].strip_edges() + "\n";
  422. if (slices.size() > 2) {
  423. text += slices[2].strip_edges();
  424. } else {
  425. text += "[i]" + TTR("No description.") + "[/i]";
  426. }
  427. help_bit->set_text(text);
  428. return help_bit;
  429. }
  430. struct _ConnectionsDockMethodInfoSort {
  431. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  432. return a.name < b.name;
  433. }
  434. };
  435. void ConnectionsDock::_filter_changed(const String &p_text) {
  436. update_tree();
  437. }
  438. /*
  439. * Post-ConnectDialog callback for creating/editing connections.
  440. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  441. */
  442. void ConnectionsDock::_make_or_edit_connection() {
  443. TreeItem *it = tree->get_selected();
  444. ERR_FAIL_COND(!it);
  445. NodePath dst_path = connect_dialog->get_dst_path();
  446. Node *target = selected_node->get_node(dst_path);
  447. ERR_FAIL_COND(!target);
  448. ConnectDialog::ConnectionData cd;
  449. cd.source = connect_dialog->get_source();
  450. cd.target = target;
  451. cd.signal = connect_dialog->get_signal_name();
  452. cd.method = connect_dialog->get_dst_method_name();
  453. cd.unbinds = connect_dialog->get_unbinds();
  454. if (cd.unbinds == 0) {
  455. cd.binds = connect_dialog->get_binds();
  456. }
  457. bool b_deferred = connect_dialog->get_deferred();
  458. bool b_oneshot = connect_dialog->get_oneshot();
  459. cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONESHOT : 0);
  460. // Conditions to add function: must have a script and must not have the method already
  461. // (in the class, the script itself, or inherited).
  462. bool add_script_function = false;
  463. Ref<Script> script = target->get_script();
  464. if (!target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cd.method)) {
  465. // There is a chance that the method is inherited from another script.
  466. bool found_inherited_function = false;
  467. Ref<Script> inherited_script = script->get_base_script();
  468. while (!inherited_script.is_null()) {
  469. int line = inherited_script->get_language()->find_function(cd.method, inherited_script->get_source_code());
  470. if (line != -1) {
  471. found_inherited_function = true;
  472. break;
  473. }
  474. inherited_script = inherited_script->get_base_script();
  475. }
  476. add_script_function = !found_inherited_function;
  477. }
  478. PackedStringArray script_function_args;
  479. if (add_script_function) {
  480. // Pick up args here before "it" is deleted by update_tree.
  481. script_function_args = it->get_metadata(0).operator Dictionary()["args"];
  482. script_function_args.resize(script_function_args.size() - cd.unbinds);
  483. for (int i = 0; i < cd.binds.size(); i++) {
  484. script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cd.binds[i].get_type()));
  485. }
  486. }
  487. if (connect_dialog->is_editing()) {
  488. _disconnect(*it);
  489. _connect(cd);
  490. } else {
  491. _connect(cd);
  492. }
  493. // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, which will delete the object "it" is pointing to.
  494. it = nullptr;
  495. if (add_script_function) {
  496. EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args);
  497. hide();
  498. }
  499. update_tree();
  500. }
  501. /*
  502. * Creates single connection w/ undo-redo functionality.
  503. */
  504. void ConnectionsDock::_connect(ConnectDialog::ConnectionData p_cd) {
  505. Node *source = Object::cast_to<Node>(p_cd.source);
  506. Node *target = Object::cast_to<Node>(p_cd.target);
  507. if (!source || !target) {
  508. return;
  509. }
  510. Callable callable = p_cd.get_callable();
  511. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));
  512. undo_redo->add_do_method(source, "connect", p_cd.signal, callable, varray(), p_cd.flags);
  513. undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);
  514. undo_redo->add_do_method(this, "update_tree");
  515. undo_redo->add_undo_method(this, "update_tree");
  516. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  517. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  518. undo_redo->commit_action();
  519. }
  520. /*
  521. * Break single connection w/ undo-redo functionality.
  522. */
  523. void ConnectionsDock::_disconnect(TreeItem &p_item) {
  524. Connection connection = p_item.get_metadata(0);
  525. ConnectDialog::ConnectionData cd = connection;
  526. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... Bugcheck.
  527. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), cd.signal, cd.method));
  528. Callable callable = cd.get_callable();
  529. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, callable);
  530. undo_redo->add_undo_method(selected_node, "connect", cd.signal, callable, cd.binds, cd.flags);
  531. undo_redo->add_do_method(this, "update_tree");
  532. undo_redo->add_undo_method(this, "update_tree");
  533. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  534. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  535. undo_redo->commit_action();
  536. }
  537. /*
  538. * Break all connections of currently selected signal.
  539. * Can undo-redo as a single action.
  540. */
  541. void ConnectionsDock::_disconnect_all() {
  542. TreeItem *item = tree->get_selected();
  543. if (!_is_item_signal(*item)) {
  544. return;
  545. }
  546. TreeItem *child = item->get_first_child();
  547. String signal_name = item->get_metadata(0).operator Dictionary()["name"];
  548. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));
  549. while (child) {
  550. Connection connection = child->get_metadata(0);
  551. ConnectDialog::ConnectionData cd = connection;
  552. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable());
  553. undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.binds, cd.flags);
  554. child = child->get_next();
  555. }
  556. undo_redo->add_do_method(this, "update_tree");
  557. undo_redo->add_undo_method(this, "update_tree");
  558. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  559. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  560. undo_redo->commit_action();
  561. }
  562. void ConnectionsDock::_tree_item_selected() {
  563. TreeItem *item = tree->get_selected();
  564. if (!item) { // Unlikely. Disable button just in case.
  565. connect_button->set_text(TTR("Connect..."));
  566. connect_button->set_disabled(true);
  567. } else if (_is_item_signal(*item)) {
  568. connect_button->set_text(TTR("Connect..."));
  569. connect_button->set_disabled(false);
  570. } else {
  571. connect_button->set_text(TTR("Disconnect"));
  572. connect_button->set_disabled(false);
  573. }
  574. }
  575. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  576. TreeItem *item = tree->get_selected();
  577. if (!item) {
  578. return;
  579. }
  580. if (_is_item_signal(*item)) {
  581. _open_connection_dialog(*item);
  582. } else {
  583. _go_to_script(*item);
  584. }
  585. }
  586. bool ConnectionsDock::_is_item_signal(TreeItem &p_item) {
  587. return (p_item.get_parent() == tree->get_root() || p_item.get_parent()->get_parent() == tree->get_root());
  588. }
  589. /*
  590. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  591. */
  592. void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
  593. String signal_name = p_item.get_metadata(0).operator Dictionary()["name"];
  594. const String &signal_name_ref = signal_name;
  595. String node_name = selected_node->get_name();
  596. for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.
  597. char32_t c = node_name[i];
  598. if (!is_ascii_identifier_char(c)) {
  599. if (c == ' ') {
  600. // Replace spaces with underlines.
  601. c = '_';
  602. } else {
  603. // Remove any other characters.
  604. node_name.remove_at(i);
  605. i--;
  606. continue;
  607. }
  608. }
  609. node_name[i] = c;
  610. }
  611. Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
  612. if (!dst_node || dst_node->get_script().is_null()) {
  613. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  614. }
  615. Dictionary subst;
  616. String s = node_name.capitalize().replace(" ", "");
  617. subst["NodeName"] = s;
  618. if (!s.is_empty()) {
  619. s[0] = s.to_lower()[0];
  620. }
  621. subst["nodeName"] = s;
  622. subst["node_name"] = node_name.capitalize().replace(" ", "_").to_lower();
  623. s = signal_name.capitalize().replace(" ", "");
  624. subst["SignalName"] = s;
  625. if (!s.is_empty()) {
  626. s[0] = s.to_lower()[0];
  627. }
  628. subst["signalName"] = s;
  629. subst["signal_name"] = signal_name.capitalize().replace(" ", "_").to_lower();
  630. String dst_method = String(EDITOR_GET("interface/editors/default_signal_callback_name")).format(subst);
  631. ConnectDialog::ConnectionData cd;
  632. cd.source = selected_node;
  633. cd.signal = StringName(signal_name_ref);
  634. cd.target = dst_node;
  635. cd.method = StringName(dst_method);
  636. connect_dialog->popup_dialog(signal_name_ref);
  637. connect_dialog->init(cd);
  638. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  639. }
  640. /*
  641. * Open connection dialog with Connection data to EDIT an existing connection.
  642. */
  643. void ConnectionsDock::_open_connection_dialog(ConnectDialog::ConnectionData p_cd) {
  644. Node *src = Object::cast_to<Node>(p_cd.source);
  645. Node *dst = Object::cast_to<Node>(p_cd.target);
  646. if (src && dst) {
  647. const String &signal_name_ref = p_cd.signal;
  648. connect_dialog->set_title(TTR("Edit Connection:") + p_cd.signal);
  649. connect_dialog->popup_dialog(signal_name_ref);
  650. connect_dialog->init(p_cd, true);
  651. }
  652. }
  653. /*
  654. * Open slot method location in script editor.
  655. */
  656. void ConnectionsDock::_go_to_script(TreeItem &p_item) {
  657. if (_is_item_signal(p_item)) {
  658. return;
  659. }
  660. Connection connection = p_item.get_metadata(0);
  661. ConnectDialog::ConnectionData cd = connection;
  662. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck.
  663. if (!cd.target) {
  664. return;
  665. }
  666. Ref<Script> script = cd.target->get_script();
  667. if (script.is_null()) {
  668. return;
  669. }
  670. if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, cd.method)) {
  671. EditorNode::get_singleton()->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  672. }
  673. }
  674. void ConnectionsDock::_handle_signal_menu_option(int p_option) {
  675. TreeItem *item = tree->get_selected();
  676. if (!item) {
  677. return;
  678. }
  679. switch (p_option) {
  680. case CONNECT: {
  681. _open_connection_dialog(*item);
  682. } break;
  683. case DISCONNECT_ALL: {
  684. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  685. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  686. disconnect_all_dialog->popup_centered();
  687. } break;
  688. }
  689. }
  690. void ConnectionsDock::_handle_slot_menu_option(int p_option) {
  691. TreeItem *item = tree->get_selected();
  692. if (!item) {
  693. return;
  694. }
  695. switch (p_option) {
  696. case EDIT: {
  697. Connection connection = item->get_metadata(0);
  698. _open_connection_dialog(connection);
  699. } break;
  700. case GO_TO_SCRIPT: {
  701. _go_to_script(*item);
  702. } break;
  703. case DISCONNECT: {
  704. _disconnect(*item);
  705. update_tree();
  706. } break;
  707. }
  708. }
  709. void ConnectionsDock::_rmb_pressed(Vector2 p_position) {
  710. TreeItem *item = tree->get_selected();
  711. if (!item) {
  712. return;
  713. }
  714. Vector2 screen_position = tree->get_screen_position() + p_position;
  715. if (_is_item_signal(*item)) {
  716. signal_menu->set_position(screen_position);
  717. signal_menu->reset_size();
  718. signal_menu->popup();
  719. } else {
  720. slot_menu->set_position(screen_position);
  721. slot_menu->reset_size();
  722. slot_menu->popup();
  723. }
  724. }
  725. void ConnectionsDock::_close() {
  726. hide();
  727. }
  728. void ConnectionsDock::_connect_pressed() {
  729. TreeItem *item = tree->get_selected();
  730. if (!item) {
  731. connect_button->set_disabled(true);
  732. return;
  733. }
  734. if (_is_item_signal(*item)) {
  735. _open_connection_dialog(*item);
  736. } else {
  737. _disconnect(*item);
  738. update_tree();
  739. }
  740. }
  741. void ConnectionsDock::_notification(int p_what) {
  742. switch (p_what) {
  743. case NOTIFICATION_ENTER_TREE:
  744. case NOTIFICATION_THEME_CHANGED: {
  745. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  746. } break;
  747. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  748. update_tree();
  749. } break;
  750. }
  751. }
  752. void ConnectionsDock::_bind_methods() {
  753. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  754. }
  755. void ConnectionsDock::set_node(Node *p_node) {
  756. selected_node = p_node;
  757. update_tree();
  758. }
  759. void ConnectionsDock::update_tree() {
  760. tree->clear();
  761. if (!selected_node) {
  762. return;
  763. }
  764. TreeItem *root = tree->create_item();
  765. List<MethodInfo> node_signals;
  766. selected_node->get_signal_list(&node_signals);
  767. bool did_script = false;
  768. StringName base = selected_node->get_class();
  769. while (base) {
  770. List<MethodInfo> node_signals2;
  771. Ref<Texture2D> icon;
  772. String name;
  773. if (!did_script) {
  774. // Get script signals (including signals from any base scripts).
  775. Ref<Script> scr = selected_node->get_script();
  776. if (scr.is_valid()) {
  777. scr->get_script_signal_list(&node_signals2);
  778. if (scr->get_path().is_resource_file()) {
  779. name = scr->get_path().get_file();
  780. } else {
  781. name = scr->get_class();
  782. }
  783. if (has_theme_icon(scr->get_class(), SNAME("EditorIcons"))) {
  784. icon = get_theme_icon(scr->get_class(), SNAME("EditorIcons"));
  785. }
  786. }
  787. } else {
  788. ClassDB::get_signal_list(base, &node_signals2, true);
  789. if (has_theme_icon(base, SNAME("EditorIcons"))) {
  790. icon = get_theme_icon(base, SNAME("EditorIcons"));
  791. }
  792. name = base;
  793. }
  794. if (!icon.is_valid()) {
  795. icon = get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  796. }
  797. TreeItem *section_item = nullptr;
  798. // Create subsections.
  799. if (node_signals2.size()) {
  800. section_item = tree->create_item(root);
  801. section_item->set_text(0, name);
  802. section_item->set_icon(0, icon);
  803. section_item->set_selectable(0, false);
  804. section_item->set_editable(0, false);
  805. section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  806. node_signals2.sort();
  807. }
  808. for (MethodInfo &mi : node_signals2) {
  809. StringName signal_name = mi.name;
  810. String signaldesc = "(";
  811. PackedStringArray argnames;
  812. String filter_text = search_box->get_text();
  813. if (!filter_text.is_subsequence_ofn(signal_name)) {
  814. continue;
  815. }
  816. if (mi.arguments.size()) {
  817. for (int i = 0; i < mi.arguments.size(); i++) {
  818. PropertyInfo &pi = mi.arguments[i];
  819. if (i > 0) {
  820. signaldesc += ", ";
  821. }
  822. String tname = "var";
  823. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  824. tname = pi.class_name.operator String();
  825. } else if (pi.type != Variant::NIL) {
  826. tname = Variant::get_type_name(pi.type);
  827. }
  828. signaldesc += (pi.name.is_empty() ? String("arg " + itos(i)) : pi.name) + ": " + tname;
  829. argnames.push_back(pi.name + ":" + tname);
  830. }
  831. }
  832. signaldesc += ")";
  833. // Create the children of the subsection - the actual list of signals.
  834. TreeItem *signal_item = tree->create_item(section_item);
  835. signal_item->set_text(0, String(signal_name) + signaldesc);
  836. Dictionary sinfo;
  837. sinfo["name"] = signal_name;
  838. sinfo["args"] = argnames;
  839. signal_item->set_metadata(0, sinfo);
  840. signal_item->set_icon(0, get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")));
  841. // Set tooltip with the signal's documentation.
  842. {
  843. String descr;
  844. bool found = false;
  845. HashMap<StringName, HashMap<StringName, String>>::Iterator G = descr_cache.find(base);
  846. if (G) {
  847. HashMap<StringName, String>::Iterator F = G->value.find(signal_name);
  848. if (F) {
  849. found = true;
  850. descr = F->value;
  851. }
  852. }
  853. if (!found) {
  854. DocTools *dd = EditorHelp::get_doc_data();
  855. HashMap<String, DocData::ClassDoc>::Iterator F = dd->class_list.find(base);
  856. while (F && descr.is_empty()) {
  857. for (int i = 0; i < F->value.signals.size(); i++) {
  858. if (F->value.signals[i].name == signal_name.operator String()) {
  859. descr = DTR(F->value.signals[i].description);
  860. break;
  861. }
  862. }
  863. if (!F->value.inherits.is_empty()) {
  864. F = dd->class_list.find(F->value.inherits);
  865. } else {
  866. break;
  867. }
  868. }
  869. descr_cache[base][signal_name] = descr;
  870. }
  871. // "::" separators used in make_custom_tooltip for formatting.
  872. signal_item->set_tooltip(0, String(signal_name) + "::" + signaldesc + "::" + descr);
  873. }
  874. // List existing connections.
  875. List<Object::Connection> connections;
  876. selected_node->get_signal_connection_list(signal_name, &connections);
  877. for (const Object::Connection &F : connections) {
  878. Connection connection = F;
  879. if (!(connection.flags & CONNECT_PERSIST)) {
  880. continue;
  881. }
  882. ConnectDialog::ConnectionData cd = connection;
  883. Node *target = Object::cast_to<Node>(cd.target);
  884. if (!target) {
  885. continue;
  886. }
  887. String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()";
  888. if (cd.flags & CONNECT_DEFERRED) {
  889. path += " (deferred)";
  890. }
  891. if (cd.flags & CONNECT_ONESHOT) {
  892. path += " (oneshot)";
  893. }
  894. if (cd.unbinds > 0) {
  895. path += " unbinds(" + itos(cd.unbinds) + ")";
  896. } else if (!cd.binds.is_empty()) {
  897. path += " binds(";
  898. for (int i = 0; i < cd.binds.size(); i++) {
  899. if (i > 0) {
  900. path += ", ";
  901. }
  902. path += cd.binds[i].operator String();
  903. }
  904. path += ")";
  905. }
  906. TreeItem *connection_item = tree->create_item(signal_item);
  907. connection_item->set_text(0, path);
  908. connection_item->set_metadata(0, connection);
  909. connection_item->set_icon(0, get_theme_icon(SNAME("Slot"), SNAME("EditorIcons")));
  910. }
  911. }
  912. if (!did_script) {
  913. did_script = true;
  914. } else {
  915. base = ClassDB::get_parent_class(base);
  916. }
  917. }
  918. connect_button->set_text(TTR("Connect..."));
  919. connect_button->set_disabled(true);
  920. }
  921. ConnectionsDock::ConnectionsDock() {
  922. set_name(TTR("Signals"));
  923. VBoxContainer *vbc = this;
  924. search_box = memnew(LineEdit);
  925. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  926. search_box->set_placeholder(TTR("Filter signals"));
  927. search_box->set_clear_button_enabled(true);
  928. search_box->connect("text_changed", callable_mp(this, &ConnectionsDock::_filter_changed));
  929. vbc->add_child(search_box);
  930. tree = memnew(ConnectionsDockTree);
  931. tree->set_columns(1);
  932. tree->set_select_mode(Tree::SELECT_ROW);
  933. tree->set_hide_root(true);
  934. vbc->add_child(tree);
  935. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  936. tree->set_allow_rmb_select(true);
  937. connect_button = memnew(Button);
  938. HBoxContainer *hb = memnew(HBoxContainer);
  939. vbc->add_child(hb);
  940. hb->add_spacer();
  941. hb->add_child(connect_button);
  942. connect_button->connect("pressed", callable_mp(this, &ConnectionsDock::_connect_pressed));
  943. connect_dialog = memnew(ConnectDialog);
  944. add_child(connect_dialog);
  945. disconnect_all_dialog = memnew(ConfirmationDialog);
  946. add_child(disconnect_all_dialog);
  947. disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
  948. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  949. signal_menu = memnew(PopupMenu);
  950. add_child(signal_menu);
  951. signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  952. signal_menu->add_item(TTR("Connect..."), CONNECT);
  953. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  954. slot_menu = memnew(PopupMenu);
  955. add_child(slot_menu);
  956. slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  957. slot_menu->add_item(TTR("Edit..."), EDIT);
  958. slot_menu->add_item(TTR("Go to Method"), GO_TO_SCRIPT);
  959. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  960. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  961. tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
  962. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  963. tree->connect("item_rmb_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed));
  964. add_theme_constant_override("separation", 3 * EDSCALE);
  965. EDITOR_DEF("interface/editors/default_signal_callback_name", "_on_{node_name}_{signal_name}");
  966. }
  967. ConnectionsDock::~ConnectionsDock() {
  968. }