connections_dialog.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  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 vt = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  155. Variant value;
  156. switch (vt) {
  157. case Variant::BOOL:
  158. value = false;
  159. break;
  160. case Variant::INT:
  161. value = 0;
  162. break;
  163. case Variant::FLOAT:
  164. value = 0.0;
  165. break;
  166. case Variant::STRING:
  167. value = "";
  168. break;
  169. case Variant::STRING_NAME:
  170. value = "";
  171. break;
  172. case Variant::VECTOR2:
  173. value = Vector2();
  174. break;
  175. case Variant::RECT2:
  176. value = Rect2();
  177. break;
  178. case Variant::VECTOR3:
  179. value = Vector3();
  180. break;
  181. case Variant::PLANE:
  182. value = Plane();
  183. break;
  184. case Variant::QUATERNION:
  185. value = Quaternion();
  186. break;
  187. case Variant::AABB:
  188. value = AABB();
  189. break;
  190. case Variant::BASIS:
  191. value = Basis();
  192. break;
  193. case Variant::TRANSFORM3D:
  194. value = Transform3D();
  195. break;
  196. case Variant::COLOR:
  197. value = Color();
  198. break;
  199. default: {
  200. ERR_FAIL();
  201. } break;
  202. }
  203. ERR_FAIL_COND(value.get_type() == Variant::NIL);
  204. cdbinds->params.push_back(value);
  205. cdbinds->notify_changed();
  206. }
  207. /*
  208. * Remove parameter bind from connection.
  209. */
  210. void ConnectDialog::_remove_bind() {
  211. String st = bind_editor->get_selected_path();
  212. if (st.is_empty()) {
  213. return;
  214. }
  215. int idx = st.get_slice("/", 1).to_int() - 1;
  216. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  217. cdbinds->params.remove_at(idx);
  218. cdbinds->notify_changed();
  219. }
  220. /*
  221. * Enables or disables the connect button. The connect button is enabled if a
  222. * node is selected and valid in the selected mode.
  223. */
  224. void ConnectDialog::_update_ok_enabled() {
  225. Node *target = tree->get_selected();
  226. if (target == nullptr) {
  227. get_ok_button()->set_disabled(true);
  228. return;
  229. }
  230. if (!advanced->is_pressed() && target->get_script().is_null()) {
  231. get_ok_button()->set_disabled(true);
  232. return;
  233. }
  234. get_ok_button()->set_disabled(false);
  235. }
  236. void ConnectDialog::_notification(int p_what) {
  237. switch (p_what) {
  238. case NOTIFICATION_ENTER_TREE: {
  239. bind_editor->edit(cdbinds);
  240. } break;
  241. }
  242. }
  243. void ConnectDialog::_bind_methods() {
  244. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  245. ClassDB::bind_method("_update_ok_enabled", &ConnectDialog::_update_ok_enabled);
  246. ADD_SIGNAL(MethodInfo("connected"));
  247. }
  248. Node *ConnectDialog::get_source() const {
  249. return source;
  250. }
  251. StringName ConnectDialog::get_signal_name() const {
  252. return signal;
  253. }
  254. NodePath ConnectDialog::get_dst_path() const {
  255. return dst_path;
  256. }
  257. void ConnectDialog::set_dst_node(Node *p_node) {
  258. tree->set_selected(p_node);
  259. }
  260. StringName ConnectDialog::get_dst_method_name() const {
  261. String txt = dst_method->get_text();
  262. if (txt.contains("(")) {
  263. txt = txt.left(txt.find("(")).strip_edges();
  264. }
  265. return txt;
  266. }
  267. void ConnectDialog::set_dst_method(const StringName &p_method) {
  268. dst_method->set_text(p_method);
  269. }
  270. int ConnectDialog::get_unbinds() const {
  271. return int(unbind_count->get_value());
  272. }
  273. Vector<Variant> ConnectDialog::get_binds() const {
  274. return cdbinds->params;
  275. }
  276. bool ConnectDialog::get_deferred() const {
  277. return deferred->is_pressed();
  278. }
  279. bool ConnectDialog::get_oneshot() const {
  280. return oneshot->is_pressed();
  281. }
  282. /*
  283. * Returns true if ConnectDialog is being used to edit an existing connection.
  284. */
  285. bool ConnectDialog::is_editing() const {
  286. return edit_mode;
  287. }
  288. /*
  289. * Initialize ConnectDialog and populate fields with expected data.
  290. * If creating a connection from scratch, sensible defaults are used.
  291. * If editing an existing connection, previous data is retained.
  292. */
  293. void ConnectDialog::init(ConnectionData p_cd, bool p_edit) {
  294. set_hide_on_ok(false);
  295. source = static_cast<Node *>(p_cd.source);
  296. signal = p_cd.signal;
  297. tree->set_selected(nullptr);
  298. tree->set_marked(source, true);
  299. if (p_cd.target) {
  300. set_dst_node(static_cast<Node *>(p_cd.target));
  301. set_dst_method(p_cd.method);
  302. }
  303. _update_ok_enabled();
  304. bool b_deferred = (p_cd.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  305. bool b_oneshot = (p_cd.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT;
  306. deferred->set_pressed(b_deferred);
  307. oneshot->set_pressed(b_oneshot);
  308. MethodInfo r_signal;
  309. Ref<Script> source_script = source->get_script();
  310. if (source_script.is_valid() && source_script->has_script_signal(signal)) {
  311. List<MethodInfo> signals;
  312. source_script->get_script_signal_list(&signals);
  313. for (MethodInfo &mi : signals) {
  314. if (mi.name == signal) {
  315. r_signal = mi;
  316. break;
  317. }
  318. }
  319. } else {
  320. ClassDB::get_signal(source->get_class(), signal, &r_signal);
  321. }
  322. unbind_count->set_max(r_signal.arguments.size());
  323. unbind_count->set_value(p_cd.unbinds);
  324. _unbind_count_changed(p_cd.unbinds);
  325. cdbinds->params.clear();
  326. cdbinds->params = p_cd.binds;
  327. cdbinds->notify_changed();
  328. edit_mode = p_edit;
  329. }
  330. void ConnectDialog::popup_dialog(const String &p_for_signal) {
  331. from_signal->set_text(p_for_signal);
  332. error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  333. if (!advanced->is_pressed()) {
  334. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  335. }
  336. popup_centered();
  337. }
  338. void ConnectDialog::_advanced_pressed() {
  339. if (advanced->is_pressed()) {
  340. set_min_size(Size2(900, 500) * EDSCALE);
  341. connect_to_label->set_text(TTR("Connect to Node:"));
  342. tree->set_connect_to_script_mode(false);
  343. vbc_right->show();
  344. error_label->hide();
  345. } else {
  346. set_min_size(Size2(600, 500) * EDSCALE);
  347. reset_size();
  348. connect_to_label->set_text(TTR("Connect to Script:"));
  349. tree->set_connect_to_script_mode(true);
  350. vbc_right->hide();
  351. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  352. }
  353. _update_ok_enabled();
  354. popup_centered();
  355. }
  356. ConnectDialog::ConnectDialog() {
  357. set_min_size(Size2(600, 500) * EDSCALE);
  358. VBoxContainer *vbc = memnew(VBoxContainer);
  359. add_child(vbc);
  360. HBoxContainer *main_hb = memnew(HBoxContainer);
  361. vbc->add_child(main_hb);
  362. main_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  363. VBoxContainer *vbc_left = memnew(VBoxContainer);
  364. main_hb->add_child(vbc_left);
  365. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  366. from_signal = memnew(LineEdit);
  367. from_signal->set_editable(false);
  368. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  369. tree = memnew(SceneTreeEditor(false));
  370. tree->set_connecting_signal(true);
  371. tree->set_show_enabled_subscene(true);
  372. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  373. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  374. tree->set_connect_to_script_mode(true);
  375. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), tree, true);
  376. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  377. error_label = memnew(Label);
  378. error_label->set_text(TTR("Scene does not contain any script."));
  379. vbc_left->add_child(error_label);
  380. error_label->hide();
  381. vbc_right = memnew(VBoxContainer);
  382. main_hb->add_child(vbc_right);
  383. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  384. vbc_right->hide();
  385. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  386. type_list = memnew(OptionButton);
  387. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  388. add_bind_hb->add_child(type_list);
  389. type_list->add_item("bool", Variant::BOOL);
  390. type_list->add_item("int", Variant::INT);
  391. type_list->add_item("real", Variant::FLOAT);
  392. type_list->add_item("String", Variant::STRING);
  393. type_list->add_item("StringName", Variant::STRING_NAME);
  394. type_list->add_item("Vector2", Variant::VECTOR2);
  395. type_list->add_item("Rect2", Variant::RECT2);
  396. type_list->add_item("Vector3", Variant::VECTOR3);
  397. type_list->add_item("Plane", Variant::PLANE);
  398. type_list->add_item("Quaternion", Variant::QUATERNION);
  399. type_list->add_item("AABB", Variant::AABB);
  400. type_list->add_item("Basis", Variant::BASIS);
  401. type_list->add_item("Transform3D", Variant::TRANSFORM3D);
  402. type_list->add_item("Color", Variant::COLOR);
  403. type_list->select(0);
  404. bind_controls.push_back(type_list);
  405. Button *add_bind = memnew(Button);
  406. add_bind->set_text(TTR("Add"));
  407. add_bind_hb->add_child(add_bind);
  408. add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind));
  409. bind_controls.push_back(add_bind);
  410. Button *del_bind = memnew(Button);
  411. del_bind->set_text(TTR("Remove"));
  412. add_bind_hb->add_child(del_bind);
  413. del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind));
  414. bind_controls.push_back(del_bind);
  415. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  416. bind_editor = memnew(EditorInspector);
  417. bind_controls.push_back(bind_editor);
  418. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  419. unbind_count = memnew(SpinBox);
  420. unbind_count->set_tooltip(TTR("Allows to drop arguments sent by signal emitter."));
  421. unbind_count->connect("value_changed", callable_mp(this, &ConnectDialog::_unbind_count_changed));
  422. vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);
  423. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  424. vbc_left->add_margin_child(TTR("Receiver Method:"), dstm_hb);
  425. dst_method = memnew(LineEdit);
  426. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  427. dst_method->connect("text_submitted", callable_mp(this, &ConnectDialog::_text_submitted));
  428. dstm_hb->add_child(dst_method);
  429. advanced = memnew(CheckButton);
  430. dstm_hb->add_child(advanced);
  431. advanced->set_text(TTR("Advanced"));
  432. advanced->connect("pressed", callable_mp(this, &ConnectDialog::_advanced_pressed));
  433. deferred = memnew(CheckBox);
  434. deferred->set_h_size_flags(0);
  435. deferred->set_text(TTR("Deferred"));
  436. deferred->set_tooltip(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  437. vbc_right->add_child(deferred);
  438. oneshot = memnew(CheckBox);
  439. oneshot->set_h_size_flags(0);
  440. oneshot->set_text(TTR("Oneshot"));
  441. oneshot->set_tooltip(TTR("Disconnects the signal after its first emission."));
  442. vbc_right->add_child(oneshot);
  443. cdbinds = memnew(ConnectDialogBinds);
  444. error = memnew(AcceptDialog);
  445. add_child(error);
  446. error->set_title(TTR("Cannot connect signal"));
  447. error->get_ok_button()->set_text(TTR("Close"));
  448. get_ok_button()->set_text(TTR("Connect"));
  449. }
  450. ConnectDialog::~ConnectDialog() {
  451. memdelete(cdbinds);
  452. }
  453. //////////////////////////////////////////
  454. // Originally copied and adapted from EditorProperty, try to keep style in sync.
  455. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  456. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  457. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  458. // p_text is expected to be something like this:
  459. // "gui_input::(event: InputEvent)::<Signal description>"
  460. // with the latter being possibly empty.
  461. PackedStringArray slices = p_text.split("::", false);
  462. if (slices.size() < 2) {
  463. // Shouldn't happen here, but just in case pass the text along.
  464. help_bit->set_text(p_text);
  465. return help_bit;
  466. }
  467. String text = TTR("Signal:") + " [u][b]" + slices[0] + "[/b][/u]";
  468. text += slices[1].strip_edges() + "\n";
  469. if (slices.size() > 2) {
  470. text += slices[2].strip_edges();
  471. } else {
  472. text += "[i]" + TTR("No description.") + "[/i]";
  473. }
  474. help_bit->set_text(text);
  475. return help_bit;
  476. }
  477. struct _ConnectionsDockMethodInfoSort {
  478. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  479. return a.name < b.name;
  480. }
  481. };
  482. void ConnectionsDock::_filter_changed(const String &p_text) {
  483. update_tree();
  484. }
  485. /*
  486. * Post-ConnectDialog callback for creating/editing connections.
  487. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  488. */
  489. void ConnectionsDock::_make_or_edit_connection() {
  490. TreeItem *it = tree->get_selected();
  491. ERR_FAIL_COND(!it);
  492. NodePath dst_path = connect_dialog->get_dst_path();
  493. Node *target = selected_node->get_node(dst_path);
  494. ERR_FAIL_COND(!target);
  495. ConnectDialog::ConnectionData cd;
  496. cd.source = connect_dialog->get_source();
  497. cd.target = target;
  498. cd.signal = connect_dialog->get_signal_name();
  499. cd.method = connect_dialog->get_dst_method_name();
  500. cd.unbinds = connect_dialog->get_unbinds();
  501. if (cd.unbinds == 0) {
  502. cd.binds = connect_dialog->get_binds();
  503. }
  504. bool b_deferred = connect_dialog->get_deferred();
  505. bool b_oneshot = connect_dialog->get_oneshot();
  506. cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONESHOT : 0);
  507. // Conditions to add function: must have a script and must not have the method already
  508. // (in the class, the script itself, or inherited).
  509. bool add_script_function = false;
  510. Ref<Script> script = target->get_script();
  511. if (!target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cd.method)) {
  512. // There is a chance that the method is inherited from another script.
  513. bool found_inherited_function = false;
  514. Ref<Script> inherited_script = script->get_base_script();
  515. while (!inherited_script.is_null()) {
  516. int line = inherited_script->get_language()->find_function(cd.method, inherited_script->get_source_code());
  517. if (line != -1) {
  518. found_inherited_function = true;
  519. break;
  520. }
  521. inherited_script = inherited_script->get_base_script();
  522. }
  523. add_script_function = !found_inherited_function;
  524. }
  525. PackedStringArray script_function_args;
  526. if (add_script_function) {
  527. // Pick up args here before "it" is deleted by update_tree.
  528. script_function_args = it->get_metadata(0).operator Dictionary()["args"];
  529. script_function_args.resize(script_function_args.size() - cd.unbinds);
  530. for (int i = 0; i < cd.binds.size(); i++) {
  531. script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cd.binds[i].get_type()));
  532. }
  533. }
  534. if (connect_dialog->is_editing()) {
  535. _disconnect(*it);
  536. _connect(cd);
  537. } else {
  538. _connect(cd);
  539. }
  540. // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, which will delete the object "it" is pointing to.
  541. it = nullptr;
  542. if (add_script_function) {
  543. EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args);
  544. hide();
  545. }
  546. update_tree();
  547. }
  548. /*
  549. * Creates single connection w/ undo-redo functionality.
  550. */
  551. void ConnectionsDock::_connect(ConnectDialog::ConnectionData p_cd) {
  552. Node *source = Object::cast_to<Node>(p_cd.source);
  553. Node *target = Object::cast_to<Node>(p_cd.target);
  554. if (!source || !target) {
  555. return;
  556. }
  557. Callable callable = p_cd.get_callable();
  558. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));
  559. undo_redo->add_do_method(source, "connect", p_cd.signal, callable, varray(), p_cd.flags);
  560. undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);
  561. undo_redo->add_do_method(this, "update_tree");
  562. undo_redo->add_undo_method(this, "update_tree");
  563. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  564. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  565. undo_redo->commit_action();
  566. }
  567. /*
  568. * Break single connection w/ undo-redo functionality.
  569. */
  570. void ConnectionsDock::_disconnect(TreeItem &p_item) {
  571. Connection connection = p_item.get_metadata(0);
  572. ConnectDialog::ConnectionData cd = connection;
  573. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... Bugcheck.
  574. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), cd.signal, cd.method));
  575. Callable callable = cd.get_callable();
  576. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, callable);
  577. undo_redo->add_undo_method(selected_node, "connect", cd.signal, callable, cd.binds, cd.flags);
  578. undo_redo->add_do_method(this, "update_tree");
  579. undo_redo->add_undo_method(this, "update_tree");
  580. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  581. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  582. undo_redo->commit_action();
  583. }
  584. /*
  585. * Break all connections of currently selected signal.
  586. * Can undo-redo as a single action.
  587. */
  588. void ConnectionsDock::_disconnect_all() {
  589. TreeItem *item = tree->get_selected();
  590. if (!_is_item_signal(*item)) {
  591. return;
  592. }
  593. TreeItem *child = item->get_first_child();
  594. String signal_name = item->get_metadata(0).operator Dictionary()["name"];
  595. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));
  596. while (child) {
  597. Connection connection = child->get_metadata(0);
  598. ConnectDialog::ConnectionData cd = connection;
  599. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable());
  600. undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.binds, cd.flags);
  601. child = child->get_next();
  602. }
  603. undo_redo->add_do_method(this, "update_tree");
  604. undo_redo->add_undo_method(this, "update_tree");
  605. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  606. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  607. undo_redo->commit_action();
  608. }
  609. void ConnectionsDock::_tree_item_selected() {
  610. TreeItem *item = tree->get_selected();
  611. if (!item) { // Unlikely. Disable button just in case.
  612. connect_button->set_text(TTR("Connect..."));
  613. connect_button->set_disabled(true);
  614. } else if (_is_item_signal(*item)) {
  615. connect_button->set_text(TTR("Connect..."));
  616. connect_button->set_disabled(false);
  617. } else {
  618. connect_button->set_text(TTR("Disconnect"));
  619. connect_button->set_disabled(false);
  620. }
  621. }
  622. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  623. TreeItem *item = tree->get_selected();
  624. if (!item) {
  625. return;
  626. }
  627. if (_is_item_signal(*item)) {
  628. _open_connection_dialog(*item);
  629. } else {
  630. _go_to_script(*item);
  631. }
  632. }
  633. bool ConnectionsDock::_is_item_signal(TreeItem &p_item) {
  634. return (p_item.get_parent() == tree->get_root() || p_item.get_parent()->get_parent() == tree->get_root());
  635. }
  636. /*
  637. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  638. */
  639. void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
  640. String signal_name = p_item.get_metadata(0).operator Dictionary()["name"];
  641. const String &signal_name_ref = signal_name;
  642. String node_name = selected_node->get_name();
  643. for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.
  644. char32_t c = node_name[i];
  645. if (!is_ascii_identifier_char(c)) {
  646. if (c == ' ') {
  647. // Replace spaces with underlines.
  648. c = '_';
  649. } else {
  650. // Remove any other characters.
  651. node_name.remove_at(i);
  652. i--;
  653. continue;
  654. }
  655. }
  656. node_name[i] = c;
  657. }
  658. Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
  659. if (!dst_node || dst_node->get_script().is_null()) {
  660. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  661. }
  662. Dictionary subst;
  663. String s = node_name.capitalize().replace(" ", "");
  664. subst["NodeName"] = s;
  665. if (!s.is_empty()) {
  666. s[0] = s.to_lower()[0];
  667. }
  668. subst["nodeName"] = s;
  669. subst["node_name"] = node_name.capitalize().replace(" ", "_").to_lower();
  670. s = signal_name.capitalize().replace(" ", "");
  671. subst["SignalName"] = s;
  672. if (!s.is_empty()) {
  673. s[0] = s.to_lower()[0];
  674. }
  675. subst["signalName"] = s;
  676. subst["signal_name"] = signal_name.capitalize().replace(" ", "_").to_lower();
  677. String dst_method = String(EDITOR_GET("interface/editors/default_signal_callback_name")).format(subst);
  678. ConnectDialog::ConnectionData cd;
  679. cd.source = selected_node;
  680. cd.signal = StringName(signal_name_ref);
  681. cd.target = dst_node;
  682. cd.method = StringName(dst_method);
  683. connect_dialog->popup_dialog(signal_name_ref);
  684. connect_dialog->init(cd);
  685. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  686. }
  687. /*
  688. * Open connection dialog with Connection data to EDIT an existing connection.
  689. */
  690. void ConnectionsDock::_open_connection_dialog(ConnectDialog::ConnectionData p_cd) {
  691. Node *src = Object::cast_to<Node>(p_cd.source);
  692. Node *dst = Object::cast_to<Node>(p_cd.target);
  693. if (src && dst) {
  694. const String &signal_name_ref = p_cd.signal;
  695. connect_dialog->set_title(TTR("Edit Connection:") + p_cd.signal);
  696. connect_dialog->popup_dialog(signal_name_ref);
  697. connect_dialog->init(p_cd, true);
  698. }
  699. }
  700. /*
  701. * Open slot method location in script editor.
  702. */
  703. void ConnectionsDock::_go_to_script(TreeItem &p_item) {
  704. if (_is_item_signal(p_item)) {
  705. return;
  706. }
  707. Connection connection = p_item.get_metadata(0);
  708. ConnectDialog::ConnectionData cd = connection;
  709. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck.
  710. if (!cd.target) {
  711. return;
  712. }
  713. Ref<Script> script = cd.target->get_script();
  714. if (script.is_null()) {
  715. return;
  716. }
  717. if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, cd.method)) {
  718. EditorNode::get_singleton()->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  719. }
  720. }
  721. void ConnectionsDock::_handle_signal_menu_option(int p_option) {
  722. TreeItem *item = tree->get_selected();
  723. if (!item) {
  724. return;
  725. }
  726. switch (p_option) {
  727. case CONNECT: {
  728. _open_connection_dialog(*item);
  729. } break;
  730. case DISCONNECT_ALL: {
  731. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  732. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  733. disconnect_all_dialog->popup_centered();
  734. } break;
  735. }
  736. }
  737. void ConnectionsDock::_handle_slot_menu_option(int p_option) {
  738. TreeItem *item = tree->get_selected();
  739. if (!item) {
  740. return;
  741. }
  742. switch (p_option) {
  743. case EDIT: {
  744. Connection connection = item->get_metadata(0);
  745. _open_connection_dialog(connection);
  746. } break;
  747. case GO_TO_SCRIPT: {
  748. _go_to_script(*item);
  749. } break;
  750. case DISCONNECT: {
  751. _disconnect(*item);
  752. update_tree();
  753. } break;
  754. }
  755. }
  756. void ConnectionsDock::_rmb_pressed(Vector2 p_position) {
  757. TreeItem *item = tree->get_selected();
  758. if (!item) {
  759. return;
  760. }
  761. Vector2 screen_position = tree->get_screen_position() + p_position;
  762. if (_is_item_signal(*item)) {
  763. signal_menu->set_position(screen_position);
  764. signal_menu->reset_size();
  765. signal_menu->popup();
  766. } else {
  767. slot_menu->set_position(screen_position);
  768. slot_menu->reset_size();
  769. slot_menu->popup();
  770. }
  771. }
  772. void ConnectionsDock::_close() {
  773. hide();
  774. }
  775. void ConnectionsDock::_connect_pressed() {
  776. TreeItem *item = tree->get_selected();
  777. if (!item) {
  778. connect_button->set_disabled(true);
  779. return;
  780. }
  781. if (_is_item_signal(*item)) {
  782. _open_connection_dialog(*item);
  783. } else {
  784. _disconnect(*item);
  785. update_tree();
  786. }
  787. }
  788. void ConnectionsDock::_notification(int p_what) {
  789. switch (p_what) {
  790. case NOTIFICATION_ENTER_TREE:
  791. case NOTIFICATION_THEME_CHANGED: {
  792. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  793. } break;
  794. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  795. update_tree();
  796. } break;
  797. }
  798. }
  799. void ConnectionsDock::_bind_methods() {
  800. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  801. }
  802. void ConnectionsDock::set_node(Node *p_node) {
  803. selected_node = p_node;
  804. update_tree();
  805. }
  806. void ConnectionsDock::update_tree() {
  807. tree->clear();
  808. if (!selected_node) {
  809. return;
  810. }
  811. TreeItem *root = tree->create_item();
  812. List<MethodInfo> node_signals;
  813. selected_node->get_signal_list(&node_signals);
  814. bool did_script = false;
  815. StringName base = selected_node->get_class();
  816. while (base) {
  817. List<MethodInfo> node_signals2;
  818. Ref<Texture2D> icon;
  819. String name;
  820. if (!did_script) {
  821. // Get script signals (including signals from any base scripts).
  822. Ref<Script> scr = selected_node->get_script();
  823. if (scr.is_valid()) {
  824. scr->get_script_signal_list(&node_signals2);
  825. if (scr->get_path().is_resource_file()) {
  826. name = scr->get_path().get_file();
  827. } else {
  828. name = scr->get_class();
  829. }
  830. if (has_theme_icon(scr->get_class(), SNAME("EditorIcons"))) {
  831. icon = get_theme_icon(scr->get_class(), SNAME("EditorIcons"));
  832. }
  833. }
  834. } else {
  835. ClassDB::get_signal_list(base, &node_signals2, true);
  836. if (has_theme_icon(base, SNAME("EditorIcons"))) {
  837. icon = get_theme_icon(base, SNAME("EditorIcons"));
  838. }
  839. name = base;
  840. }
  841. if (!icon.is_valid()) {
  842. icon = get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  843. }
  844. TreeItem *section_item = nullptr;
  845. // Create subsections.
  846. if (node_signals2.size()) {
  847. section_item = tree->create_item(root);
  848. section_item->set_text(0, name);
  849. section_item->set_icon(0, icon);
  850. section_item->set_selectable(0, false);
  851. section_item->set_editable(0, false);
  852. section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  853. node_signals2.sort();
  854. }
  855. for (MethodInfo &mi : node_signals2) {
  856. StringName signal_name = mi.name;
  857. String signaldesc = "(";
  858. PackedStringArray argnames;
  859. String filter_text = search_box->get_text();
  860. if (!filter_text.is_subsequence_ofn(signal_name)) {
  861. continue;
  862. }
  863. if (mi.arguments.size()) {
  864. for (int i = 0; i < mi.arguments.size(); i++) {
  865. PropertyInfo &pi = mi.arguments[i];
  866. if (i > 0) {
  867. signaldesc += ", ";
  868. }
  869. String tname = "var";
  870. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  871. tname = pi.class_name.operator String();
  872. } else if (pi.type != Variant::NIL) {
  873. tname = Variant::get_type_name(pi.type);
  874. }
  875. signaldesc += (pi.name.is_empty() ? String("arg " + itos(i)) : pi.name) + ": " + tname;
  876. argnames.push_back(pi.name + ":" + tname);
  877. }
  878. }
  879. signaldesc += ")";
  880. // Create the children of the subsection - the actual list of signals.
  881. TreeItem *signal_item = tree->create_item(section_item);
  882. signal_item->set_text(0, String(signal_name) + signaldesc);
  883. Dictionary sinfo;
  884. sinfo["name"] = signal_name;
  885. sinfo["args"] = argnames;
  886. signal_item->set_metadata(0, sinfo);
  887. signal_item->set_icon(0, get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")));
  888. // Set tooltip with the signal's documentation.
  889. {
  890. String descr;
  891. bool found = false;
  892. Map<StringName, Map<StringName, String>>::Element *G = descr_cache.find(base);
  893. if (G) {
  894. Map<StringName, String>::Element *F = G->get().find(signal_name);
  895. if (F) {
  896. found = true;
  897. descr = F->get();
  898. }
  899. }
  900. if (!found) {
  901. DocTools *dd = EditorHelp::get_doc_data();
  902. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(base);
  903. while (F && descr.is_empty()) {
  904. for (int i = 0; i < F->get().signals.size(); i++) {
  905. if (F->get().signals[i].name == signal_name.operator String()) {
  906. descr = DTR(F->get().signals[i].description);
  907. break;
  908. }
  909. }
  910. if (!F->get().inherits.is_empty()) {
  911. F = dd->class_list.find(F->get().inherits);
  912. } else {
  913. break;
  914. }
  915. }
  916. descr_cache[base][signal_name] = descr;
  917. }
  918. // "::" separators used in make_custom_tooltip for formatting.
  919. signal_item->set_tooltip(0, String(signal_name) + "::" + signaldesc + "::" + descr);
  920. }
  921. // List existing connections.
  922. List<Object::Connection> connections;
  923. selected_node->get_signal_connection_list(signal_name, &connections);
  924. for (const Object::Connection &F : connections) {
  925. Connection connection = F;
  926. if (!(connection.flags & CONNECT_PERSIST)) {
  927. continue;
  928. }
  929. ConnectDialog::ConnectionData cd = connection;
  930. Node *target = Object::cast_to<Node>(cd.target);
  931. if (!target) {
  932. continue;
  933. }
  934. String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()";
  935. if (cd.flags & CONNECT_DEFERRED) {
  936. path += " (deferred)";
  937. }
  938. if (cd.flags & CONNECT_ONESHOT) {
  939. path += " (oneshot)";
  940. }
  941. if (cd.unbinds > 0) {
  942. path += " unbinds(" + itos(cd.unbinds) + ")";
  943. } else if (!cd.binds.is_empty()) {
  944. path += " binds(";
  945. for (int i = 0; i < cd.binds.size(); i++) {
  946. if (i > 0) {
  947. path += ", ";
  948. }
  949. path += cd.binds[i].operator String();
  950. }
  951. path += ")";
  952. }
  953. TreeItem *connection_item = tree->create_item(signal_item);
  954. connection_item->set_text(0, path);
  955. connection_item->set_metadata(0, connection);
  956. connection_item->set_icon(0, get_theme_icon(SNAME("Slot"), SNAME("EditorIcons")));
  957. }
  958. }
  959. if (!did_script) {
  960. did_script = true;
  961. } else {
  962. base = ClassDB::get_parent_class(base);
  963. }
  964. }
  965. connect_button->set_text(TTR("Connect..."));
  966. connect_button->set_disabled(true);
  967. }
  968. ConnectionsDock::ConnectionsDock() {
  969. set_name(TTR("Signals"));
  970. VBoxContainer *vbc = this;
  971. search_box = memnew(LineEdit);
  972. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  973. search_box->set_placeholder(TTR("Filter signals"));
  974. search_box->set_clear_button_enabled(true);
  975. search_box->connect("text_changed", callable_mp(this, &ConnectionsDock::_filter_changed));
  976. vbc->add_child(search_box);
  977. tree = memnew(ConnectionsDockTree);
  978. tree->set_columns(1);
  979. tree->set_select_mode(Tree::SELECT_ROW);
  980. tree->set_hide_root(true);
  981. vbc->add_child(tree);
  982. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  983. tree->set_allow_rmb_select(true);
  984. connect_button = memnew(Button);
  985. HBoxContainer *hb = memnew(HBoxContainer);
  986. vbc->add_child(hb);
  987. hb->add_spacer();
  988. hb->add_child(connect_button);
  989. connect_button->connect("pressed", callable_mp(this, &ConnectionsDock::_connect_pressed));
  990. connect_dialog = memnew(ConnectDialog);
  991. add_child(connect_dialog);
  992. disconnect_all_dialog = memnew(ConfirmationDialog);
  993. add_child(disconnect_all_dialog);
  994. disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
  995. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  996. signal_menu = memnew(PopupMenu);
  997. add_child(signal_menu);
  998. signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  999. signal_menu->add_item(TTR("Connect..."), CONNECT);
  1000. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  1001. slot_menu = memnew(PopupMenu);
  1002. add_child(slot_menu);
  1003. slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  1004. slot_menu->add_item(TTR("Edit..."), EDIT);
  1005. slot_menu->add_item(TTR("Go to Method"), GO_TO_SCRIPT);
  1006. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  1007. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  1008. tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
  1009. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  1010. tree->connect("item_rmb_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed));
  1011. add_theme_constant_override("separation", 3 * EDSCALE);
  1012. EDITOR_DEF("interface/editors/default_signal_callback_name", "_on_{node_name}_{signal_name}");
  1013. }
  1014. ConnectionsDock::~ConnectionsDock() {
  1015. }