connections_dialog.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*************************************************************************/
  2. /* connections_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "core/print_string.h"
  32. #include "editor_node.h"
  33. #include "editor_scale.h"
  34. #include "editor_settings.h"
  35. #include "plugins/script_editor_plugin.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/popup_menu.h"
  38. static Node *_find_first_script(Node *p_root, Node *p_node) {
  39. if (p_node != p_root && p_node->get_owner() != p_root) {
  40. return nullptr;
  41. }
  42. if (!p_node->get_script().is_null()) {
  43. return p_node;
  44. }
  45. for (int i = 0; i < p_node->get_child_count(); i++) {
  46. Node *ret = _find_first_script(p_root, p_node->get_child(i));
  47. if (ret) {
  48. return ret;
  49. }
  50. }
  51. return nullptr;
  52. }
  53. class ConnectDialogBinds : public Object {
  54. GDCLASS(ConnectDialogBinds, Object);
  55. public:
  56. Vector<Variant> params;
  57. bool _set(const StringName &p_name, const Variant &p_value) {
  58. String name = p_name;
  59. if (name.begins_with("bind/")) {
  60. int which = name.get_slice("/", 1).to_int() - 1;
  61. ERR_FAIL_INDEX_V(which, params.size(), false);
  62. params.write[which] = p_value;
  63. } else
  64. return false;
  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/")) {
  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. return true;
  76. }
  77. void _get_property_list(List<PropertyInfo> *p_list) const {
  78. for (int i = 0; i < params.size(); i++) {
  79. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/" + itos(i + 1)));
  80. }
  81. }
  82. void notify_changed() {
  83. _change_notify();
  84. }
  85. ConnectDialogBinds() {
  86. }
  87. };
  88. /*
  89. * Signal automatically called by parent dialog.
  90. */
  91. void ConnectDialog::ok_pressed() {
  92. if (dst_method->get_text() == "") {
  93. error->set_text(TTR("Method in target node must be specified."));
  94. error->popup_centered();
  95. return;
  96. }
  97. Node *target = tree->get_selected();
  98. if (!target) {
  99. return; // Nothing selected in the tree, not an error.
  100. }
  101. if (target->get_script().is_null()) {
  102. if (!target->has_method(dst_method->get_text())) {
  103. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  104. error->popup_centered();
  105. return;
  106. }
  107. }
  108. emit_signal("connected");
  109. hide();
  110. }
  111. void ConnectDialog::_cancel_pressed() {
  112. hide();
  113. }
  114. void ConnectDialog::_item_activated() {
  115. _ok_pressed(); // From AcceptDialog.
  116. }
  117. void ConnectDialog::_text_entered(const String &p_text) {
  118. _ok_pressed(); // From AcceptDialog.
  119. }
  120. /*
  121. * Called each time a target node is selected within the target node tree.
  122. */
  123. void ConnectDialog::_tree_node_selected() {
  124. Node *current = tree->get_selected();
  125. if (!current)
  126. return;
  127. dst_path = source->get_path_to(current);
  128. _update_ok_enabled();
  129. }
  130. /*
  131. * Adds a new parameter bind to connection.
  132. */
  133. void ConnectDialog::_add_bind() {
  134. if (cdbinds->params.size() >= VARIANT_ARG_MAX)
  135. return;
  136. Variant::Type vt = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  137. Variant value;
  138. switch (vt) {
  139. case Variant::BOOL: value = false; break;
  140. case Variant::INT: value = 0; break;
  141. case Variant::FLOAT: value = 0.0; break;
  142. case Variant::STRING: value = ""; break;
  143. case Variant::STRING_NAME: value = ""; break;
  144. case Variant::VECTOR2: value = Vector2(); break;
  145. case Variant::RECT2: value = Rect2(); break;
  146. case Variant::VECTOR3: value = Vector3(); break;
  147. case Variant::PLANE: value = Plane(); break;
  148. case Variant::QUAT: value = Quat(); break;
  149. case Variant::AABB: value = AABB(); break;
  150. case Variant::BASIS: value = Basis(); break;
  151. case Variant::TRANSFORM: value = Transform(); break;
  152. case Variant::COLOR: value = Color(); break;
  153. default: {
  154. ERR_FAIL();
  155. } break;
  156. }
  157. ERR_FAIL_COND(value.get_type() == Variant::NIL);
  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 == "")
  167. return;
  168. int idx = st.get_slice("/", 1).to_int() - 1;
  169. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  170. cdbinds->params.remove(idx);
  171. cdbinds->notify_changed();
  172. }
  173. /*
  174. * Enables or disables the connect button. The connect button is enabled if a
  175. * node is selected and valid in the selected mode.
  176. */
  177. void ConnectDialog::_update_ok_enabled() {
  178. Node *target = tree->get_selected();
  179. if (target == nullptr) {
  180. get_ok()->set_disabled(true);
  181. return;
  182. }
  183. if (!advanced->is_pressed() && target->get_script().is_null()) {
  184. get_ok()->set_disabled(true);
  185. return;
  186. }
  187. get_ok()->set_disabled(false);
  188. }
  189. void ConnectDialog::_notification(int p_what) {
  190. if (p_what == NOTIFICATION_ENTER_TREE) {
  191. bind_editor->edit(cdbinds);
  192. }
  193. }
  194. void ConnectDialog::_bind_methods() {
  195. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  196. ClassDB::bind_method("_update_ok_enabled", &ConnectDialog::_update_ok_enabled);
  197. ADD_SIGNAL(MethodInfo("connected"));
  198. }
  199. Node *ConnectDialog::get_source() const {
  200. return source;
  201. }
  202. StringName ConnectDialog::get_signal_name() const {
  203. return signal;
  204. }
  205. NodePath ConnectDialog::get_dst_path() const {
  206. return dst_path;
  207. }
  208. void ConnectDialog::set_dst_node(Node *p_node) {
  209. tree->set_selected(p_node);
  210. }
  211. StringName ConnectDialog::get_dst_method_name() const {
  212. String txt = dst_method->get_text();
  213. if (txt.find("(") != -1)
  214. txt = txt.left(txt.find("(")).strip_edges();
  215. return txt;
  216. }
  217. void ConnectDialog::set_dst_method(const StringName &p_method) {
  218. dst_method->set_text(p_method);
  219. }
  220. Vector<Variant> ConnectDialog::get_binds() const {
  221. return cdbinds->params;
  222. }
  223. bool ConnectDialog::get_deferred() const {
  224. return deferred->is_pressed();
  225. }
  226. bool ConnectDialog::get_oneshot() const {
  227. return oneshot->is_pressed();
  228. }
  229. /*
  230. * Returns true if ConnectDialog is being used to edit an existing connection.
  231. */
  232. bool ConnectDialog::is_editing() const {
  233. return bEditMode;
  234. }
  235. /*
  236. * Initialize ConnectDialog and populate fields with expected data.
  237. * If creating a connection from scratch, sensible defaults are used.
  238. * If editing an existing connection, previous data is retained.
  239. */
  240. void ConnectDialog::init(ConnectionData c, bool bEdit) {
  241. set_hide_on_ok(false);
  242. source = static_cast<Node *>(c.source);
  243. signal = c.signal;
  244. tree->set_selected(nullptr);
  245. tree->set_marked(source, true);
  246. if (c.target) {
  247. set_dst_node(static_cast<Node *>(c.target));
  248. set_dst_method(c.method);
  249. }
  250. _update_ok_enabled();
  251. bool bDeferred = (c.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  252. bool bOneshot = (c.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT;
  253. deferred->set_pressed(bDeferred);
  254. oneshot->set_pressed(bOneshot);
  255. cdbinds->params.clear();
  256. cdbinds->params = c.binds;
  257. cdbinds->notify_changed();
  258. bEditMode = bEdit;
  259. }
  260. void ConnectDialog::popup_dialog(const String &p_for_signal) {
  261. from_signal->set_text(p_for_signal);
  262. error_label->add_theme_color_override("font_color", error_label->get_theme_color("error_color", "Editor"));
  263. if (!advanced->is_pressed())
  264. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  265. popup_centered();
  266. }
  267. void ConnectDialog::_advanced_pressed() {
  268. if (advanced->is_pressed()) {
  269. set_min_size(Size2(900, 500) * EDSCALE);
  270. connect_to_label->set_text(TTR("Connect to Node:"));
  271. tree->set_connect_to_script_mode(false);
  272. vbc_right->show();
  273. error_label->hide();
  274. } else {
  275. set_min_size(Size2(600, 500) * EDSCALE);
  276. set_size(Size2());
  277. connect_to_label->set_text(TTR("Connect to Script:"));
  278. tree->set_connect_to_script_mode(true);
  279. vbc_right->hide();
  280. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  281. }
  282. _update_ok_enabled();
  283. popup_centered();
  284. }
  285. ConnectDialog::ConnectDialog() {
  286. set_min_size(Size2(600, 500) * EDSCALE);
  287. VBoxContainer *vbc = memnew(VBoxContainer);
  288. add_child(vbc);
  289. HBoxContainer *main_hb = memnew(HBoxContainer);
  290. vbc->add_child(main_hb);
  291. main_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  292. VBoxContainer *vbc_left = memnew(VBoxContainer);
  293. main_hb->add_child(vbc_left);
  294. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  295. from_signal = memnew(LineEdit);
  296. from_signal->set_editable(false);
  297. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  298. tree = memnew(SceneTreeEditor(false));
  299. tree->set_connecting_signal(true);
  300. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  301. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  302. tree->set_connect_to_script_mode(true);
  303. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), tree, true);
  304. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  305. error_label = memnew(Label);
  306. error_label->set_text(TTR("Scene does not contain any script."));
  307. vbc_left->add_child(error_label);
  308. error_label->hide();
  309. vbc_right = memnew(VBoxContainer);
  310. main_hb->add_child(vbc_right);
  311. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  312. vbc_right->hide();
  313. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  314. type_list = memnew(OptionButton);
  315. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  316. add_bind_hb->add_child(type_list);
  317. type_list->add_item("bool", Variant::BOOL);
  318. type_list->add_item("int", Variant::INT);
  319. type_list->add_item("real", Variant::FLOAT);
  320. type_list->add_item("String", Variant::STRING);
  321. type_list->add_item("StringName", Variant::STRING_NAME);
  322. type_list->add_item("Vector2", Variant::VECTOR2);
  323. type_list->add_item("Rect2", Variant::RECT2);
  324. type_list->add_item("Vector3", Variant::VECTOR3);
  325. type_list->add_item("Plane", Variant::PLANE);
  326. type_list->add_item("Quat", Variant::QUAT);
  327. type_list->add_item("AABB", Variant::AABB);
  328. type_list->add_item("Basis", Variant::BASIS);
  329. type_list->add_item("Transform", Variant::TRANSFORM);
  330. type_list->add_item("Color", Variant::COLOR);
  331. type_list->select(0);
  332. Button *add_bind = memnew(Button);
  333. add_bind->set_text(TTR("Add"));
  334. add_bind_hb->add_child(add_bind);
  335. add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind));
  336. Button *del_bind = memnew(Button);
  337. del_bind->set_text(TTR("Remove"));
  338. add_bind_hb->add_child(del_bind);
  339. del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind));
  340. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  341. bind_editor = memnew(EditorInspector);
  342. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  343. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  344. vbc_left->add_margin_child(TTR("Receiver Method:"), dstm_hb);
  345. dst_method = memnew(LineEdit);
  346. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  347. dst_method->connect("text_entered", callable_mp(this, &ConnectDialog::_text_entered));
  348. dstm_hb->add_child(dst_method);
  349. advanced = memnew(CheckButton);
  350. dstm_hb->add_child(advanced);
  351. advanced->set_text(TTR("Advanced"));
  352. advanced->connect("pressed", callable_mp(this, &ConnectDialog::_advanced_pressed));
  353. // Add spacing so the tree and inspector are the same size.
  354. Control *spacing = memnew(Control);
  355. spacing->set_custom_minimum_size(Size2(0, 4) * EDSCALE);
  356. vbc_right->add_child(spacing);
  357. deferred = memnew(CheckBox);
  358. deferred->set_h_size_flags(0);
  359. deferred->set_text(TTR("Deferred"));
  360. deferred->set_tooltip(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  361. vbc_right->add_child(deferred);
  362. oneshot = memnew(CheckBox);
  363. oneshot->set_h_size_flags(0);
  364. oneshot->set_text(TTR("Oneshot"));
  365. oneshot->set_tooltip(TTR("Disconnects the signal after its first emission."));
  366. vbc_right->add_child(oneshot);
  367. cdbinds = memnew(ConnectDialogBinds);
  368. error = memnew(AcceptDialog);
  369. add_child(error);
  370. error->set_title(TTR("Cannot connect signal"));
  371. error->get_ok()->set_text(TTR("Close"));
  372. get_ok()->set_text(TTR("Connect"));
  373. }
  374. ConnectDialog::~ConnectDialog() {
  375. memdelete(cdbinds);
  376. }
  377. //////////////////////////////////////////
  378. // Originally copied and adapted from EditorProperty, try to keep style in sync.
  379. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  380. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  381. help_bit->add_theme_style_override("panel", get_theme_stylebox("panel", "TooltipPanel"));
  382. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  383. String text = TTR("Signal:") + " [u][b]" + p_text.get_slice("::", 0) + "[/b][/u]";
  384. text += p_text.get_slice("::", 1).strip_edges() + "\n";
  385. text += p_text.get_slice("::", 2).strip_edges();
  386. help_bit->call_deferred("set_text", text); //hack so it uses proper theme once inside scene
  387. return help_bit;
  388. }
  389. struct _ConnectionsDockMethodInfoSort {
  390. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  391. return a.name < b.name;
  392. }
  393. };
  394. /*
  395. * Post-ConnectDialog callback for creating/editing connections.
  396. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  397. */
  398. void ConnectionsDock::_make_or_edit_connection() {
  399. TreeItem *it = tree->get_selected();
  400. ERR_FAIL_COND(!it);
  401. NodePath dst_path = connect_dialog->get_dst_path();
  402. Node *target = selectedNode->get_node(dst_path);
  403. ERR_FAIL_COND(!target);
  404. ConnectDialog::ConnectionData cToMake;
  405. cToMake.source = connect_dialog->get_source();
  406. cToMake.target = target;
  407. cToMake.signal = connect_dialog->get_signal_name();
  408. cToMake.method = connect_dialog->get_dst_method_name();
  409. cToMake.binds = connect_dialog->get_binds();
  410. bool defer = connect_dialog->get_deferred();
  411. bool oshot = connect_dialog->get_oneshot();
  412. cToMake.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0);
  413. // Conditions to add function: must have a script and must not have the method already
  414. // (in the class, the script itself, or inherited).
  415. bool add_script_function = false;
  416. Ref<Script> script = target->get_script();
  417. if (!target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cToMake.method)) {
  418. // There is a chance that the method is inherited from another script.
  419. bool found_inherited_function = false;
  420. Ref<Script> inherited_script = script->get_base_script();
  421. while (!inherited_script.is_null()) {
  422. int line = inherited_script->get_language()->find_function(cToMake.method, inherited_script->get_source_code());
  423. if (line != -1) {
  424. found_inherited_function = true;
  425. break;
  426. }
  427. inherited_script = inherited_script->get_base_script();
  428. }
  429. add_script_function = !found_inherited_function;
  430. }
  431. PackedStringArray script_function_args;
  432. if (add_script_function) {
  433. // Pick up args here before "it" is deleted by update_tree.
  434. script_function_args = it->get_metadata(0).operator Dictionary()["args"];
  435. for (int i = 0; i < cToMake.binds.size(); i++) {
  436. script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cToMake.binds[i].get_type()));
  437. }
  438. }
  439. if (connect_dialog->is_editing()) {
  440. _disconnect(*it);
  441. _connect(cToMake);
  442. } else {
  443. _connect(cToMake);
  444. }
  445. // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, which will delete the object "it" is pointing to.
  446. it = nullptr;
  447. if (add_script_function) {
  448. editor->emit_signal("script_add_function_request", target, cToMake.method, script_function_args);
  449. hide();
  450. }
  451. update_tree();
  452. }
  453. /*
  454. * Creates single connection w/ undo-redo functionality.
  455. */
  456. void ConnectionsDock::_connect(ConnectDialog::ConnectionData cToMake) {
  457. Node *source = static_cast<Node *>(cToMake.source);
  458. Node *target = static_cast<Node *>(cToMake.target);
  459. if (!source || !target)
  460. return;
  461. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(cToMake.signal), String(cToMake.method)));
  462. Callable c(target, cToMake.method);
  463. undo_redo->add_do_method(source, "connect", cToMake.signal, c, cToMake.binds, cToMake.flags);
  464. undo_redo->add_undo_method(source, "disconnect", cToMake.signal, c);
  465. undo_redo->add_do_method(this, "update_tree");
  466. undo_redo->add_undo_method(this, "update_tree");
  467. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  468. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  469. undo_redo->commit_action();
  470. }
  471. /*
  472. * Break single connection w/ undo-redo functionality.
  473. */
  474. void ConnectionsDock::_disconnect(TreeItem &item) {
  475. Connection cd = item.get_metadata(0);
  476. ConnectDialog::ConnectionData c = cd;
  477. ERR_FAIL_COND(c.source != selectedNode); // Shouldn't happen but... Bugcheck.
  478. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method));
  479. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, Callable(c.target, c.method));
  480. undo_redo->add_undo_method(selectedNode, "connect", c.signal, Callable(c.target, c.method), c.binds, c.flags);
  481. undo_redo->add_do_method(this, "update_tree");
  482. undo_redo->add_undo_method(this, "update_tree");
  483. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  484. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  485. undo_redo->commit_action();
  486. }
  487. /*
  488. * Break all connections of currently selected signal.
  489. * Can undo-redo as a single action.
  490. */
  491. void ConnectionsDock::_disconnect_all() {
  492. TreeItem *item = tree->get_selected();
  493. if (!_is_item_signal(*item))
  494. return;
  495. TreeItem *child = item->get_children();
  496. String signalName = item->get_metadata(0).operator Dictionary()["name"];
  497. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signalName));
  498. while (child) {
  499. Connection cd = child->get_metadata(0);
  500. ConnectDialog::ConnectionData c = cd;
  501. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, Callable(c.target, c.method));
  502. undo_redo->add_undo_method(selectedNode, "connect", c.signal, Callable(c.target, c.method), c.binds, c.flags);
  503. child = child->get_next();
  504. }
  505. undo_redo->add_do_method(this, "update_tree");
  506. undo_redo->add_undo_method(this, "update_tree");
  507. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  508. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  509. undo_redo->commit_action();
  510. }
  511. void ConnectionsDock::_tree_item_selected() {
  512. TreeItem *item = tree->get_selected();
  513. if (!item) { // Unlikely. Disable button just in case.
  514. connect_button->set_text(TTR("Connect..."));
  515. connect_button->set_disabled(true);
  516. } else if (_is_item_signal(*item)) {
  517. connect_button->set_text(TTR("Connect..."));
  518. connect_button->set_disabled(false);
  519. } else {
  520. connect_button->set_text(TTR("Disconnect"));
  521. connect_button->set_disabled(false);
  522. }
  523. }
  524. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  525. TreeItem *item = tree->get_selected();
  526. if (!item)
  527. return;
  528. if (_is_item_signal(*item)) {
  529. _open_connection_dialog(*item);
  530. } else {
  531. _go_to_script(*item);
  532. }
  533. }
  534. bool ConnectionsDock::_is_item_signal(TreeItem &item) {
  535. return (item.get_parent() == tree->get_root() || item.get_parent()->get_parent() == tree->get_root());
  536. }
  537. /*
  538. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  539. */
  540. void ConnectionsDock::_open_connection_dialog(TreeItem &item) {
  541. String signal = item.get_metadata(0).operator Dictionary()["name"];
  542. const String &signalname = signal;
  543. String midname = selectedNode->get_name();
  544. for (int i = 0; i < midname.length(); i++) { //TODO: Regex filter may be cleaner.
  545. CharType c = midname[i];
  546. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) {
  547. if (c == ' ') {
  548. // Replace spaces with underlines.
  549. c = '_';
  550. } else {
  551. // Remove any other characters.
  552. midname.remove(i);
  553. i--;
  554. continue;
  555. }
  556. }
  557. midname[i] = c;
  558. }
  559. Node *dst_node = selectedNode->get_owner() ? selectedNode->get_owner() : selectedNode;
  560. if (!dst_node || dst_node->get_script().is_null()) {
  561. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  562. }
  563. StringName dst_method = "_on_" + midname + "_" + signal;
  564. ConnectDialog::ConnectionData c;
  565. c.source = selectedNode;
  566. c.signal = StringName(signalname);
  567. c.target = dst_node;
  568. c.method = dst_method;
  569. connect_dialog->popup_dialog(signalname);
  570. connect_dialog->init(c);
  571. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  572. }
  573. /*
  574. * Open connection dialog with Connection data to EDIT an existing connection.
  575. */
  576. void ConnectionsDock::_open_connection_dialog(ConnectDialog::ConnectionData cToEdit) {
  577. Node *src = static_cast<Node *>(cToEdit.source);
  578. Node *dst = static_cast<Node *>(cToEdit.target);
  579. if (src && dst) {
  580. connect_dialog->set_title(TTR("Edit Connection:") + cToEdit.signal);
  581. connect_dialog->popup_centered();
  582. connect_dialog->init(cToEdit, true);
  583. }
  584. }
  585. /*
  586. * Open slot method location in script editor.
  587. */
  588. void ConnectionsDock::_go_to_script(TreeItem &item) {
  589. if (_is_item_signal(item))
  590. return;
  591. Connection cd = item.get_metadata(0);
  592. ConnectDialog::ConnectionData c = cd;
  593. ERR_FAIL_COND(c.source != selectedNode); //shouldn't happen but...bugcheck
  594. if (!c.target)
  595. return;
  596. Ref<Script> script = c.target->get_script();
  597. if (script.is_null())
  598. return;
  599. if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, c.method)) {
  600. editor->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  601. }
  602. }
  603. void ConnectionsDock::_handle_signal_menu_option(int option) {
  604. TreeItem *item = tree->get_selected();
  605. if (!item)
  606. return;
  607. switch (option) {
  608. case CONNECT: {
  609. _open_connection_dialog(*item);
  610. } break;
  611. case DISCONNECT_ALL: {
  612. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  613. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  614. disconnect_all_dialog->popup_centered();
  615. } break;
  616. }
  617. }
  618. void ConnectionsDock::_handle_slot_menu_option(int option) {
  619. TreeItem *item = tree->get_selected();
  620. if (!item)
  621. return;
  622. switch (option) {
  623. case EDIT: {
  624. Connection c = item->get_metadata(0);
  625. _open_connection_dialog(c);
  626. } break;
  627. case GO_TO_SCRIPT: {
  628. _go_to_script(*item);
  629. } break;
  630. case DISCONNECT: {
  631. _disconnect(*item);
  632. update_tree();
  633. } break;
  634. }
  635. }
  636. void ConnectionsDock::_rmb_pressed(Vector2 position) {
  637. TreeItem *item = tree->get_selected();
  638. if (!item)
  639. return;
  640. Vector2 global_position = tree->get_global_position() + position;
  641. if (_is_item_signal(*item)) {
  642. signal_menu->set_position(global_position);
  643. signal_menu->popup();
  644. } else {
  645. slot_menu->set_position(global_position);
  646. slot_menu->popup();
  647. }
  648. }
  649. void ConnectionsDock::_close() {
  650. hide();
  651. }
  652. void ConnectionsDock::_connect_pressed() {
  653. TreeItem *item = tree->get_selected();
  654. if (!item) {
  655. connect_button->set_disabled(true);
  656. return;
  657. }
  658. if (_is_item_signal(*item)) {
  659. _open_connection_dialog(*item);
  660. } else {
  661. _disconnect(*item);
  662. update_tree();
  663. }
  664. }
  665. void ConnectionsDock::_notification(int p_what) {
  666. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  667. update_tree();
  668. }
  669. }
  670. void ConnectionsDock::_bind_methods() {
  671. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  672. }
  673. void ConnectionsDock::set_node(Node *p_node) {
  674. selectedNode = p_node;
  675. update_tree();
  676. }
  677. void ConnectionsDock::update_tree() {
  678. tree->clear();
  679. if (!selectedNode)
  680. return;
  681. TreeItem *root = tree->create_item();
  682. List<MethodInfo> node_signals;
  683. selectedNode->get_signal_list(&node_signals);
  684. bool did_script = false;
  685. StringName base = selectedNode->get_class();
  686. while (base) {
  687. List<MethodInfo> node_signals2;
  688. Ref<Texture2D> icon;
  689. String name;
  690. if (!did_script) {
  691. Ref<Script> scr = selectedNode->get_script();
  692. if (scr.is_valid()) {
  693. scr->get_script_signal_list(&node_signals2);
  694. if (scr->get_path().is_resource_file())
  695. name = scr->get_path().get_file();
  696. else
  697. name = scr->get_class();
  698. if (has_theme_icon(scr->get_class(), "EditorIcons")) {
  699. icon = get_theme_icon(scr->get_class(), "EditorIcons");
  700. }
  701. }
  702. } else {
  703. ClassDB::get_signal_list(base, &node_signals2, true);
  704. if (has_theme_icon(base, "EditorIcons")) {
  705. icon = get_theme_icon(base, "EditorIcons");
  706. }
  707. name = base;
  708. }
  709. if (!icon.is_valid()) {
  710. icon = get_theme_icon("Object", "EditorIcons");
  711. }
  712. TreeItem *pitem = nullptr;
  713. if (node_signals2.size()) {
  714. pitem = tree->create_item(root);
  715. pitem->set_text(0, name);
  716. pitem->set_icon(0, icon);
  717. pitem->set_selectable(0, false);
  718. pitem->set_editable(0, false);
  719. pitem->set_custom_bg_color(0, get_theme_color("prop_subsection", "Editor"));
  720. node_signals2.sort();
  721. }
  722. for (List<MethodInfo>::Element *E = node_signals2.front(); E; E = E->next()) {
  723. MethodInfo &mi = E->get();
  724. StringName signal_name = mi.name;
  725. String signaldesc = "(";
  726. PackedStringArray argnames;
  727. if (mi.arguments.size()) {
  728. for (int i = 0; i < mi.arguments.size(); i++) {
  729. PropertyInfo &pi = mi.arguments[i];
  730. if (i > 0)
  731. signaldesc += ", ";
  732. String tname = "var";
  733. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  734. tname = pi.class_name.operator String();
  735. } else if (pi.type != Variant::NIL) {
  736. tname = Variant::get_type_name(pi.type);
  737. }
  738. signaldesc += (pi.name == "" ? String("arg " + itos(i)) : pi.name) + ": " + tname;
  739. argnames.push_back(pi.name + ":" + tname);
  740. }
  741. }
  742. signaldesc += ")";
  743. TreeItem *item = tree->create_item(pitem);
  744. item->set_text(0, String(signal_name) + signaldesc);
  745. Dictionary sinfo;
  746. sinfo["name"] = signal_name;
  747. sinfo["args"] = argnames;
  748. item->set_metadata(0, sinfo);
  749. item->set_icon(0, get_theme_icon("Signal", "EditorIcons"));
  750. // Set tooltip with the signal's documentation.
  751. {
  752. String descr;
  753. bool found = false;
  754. Map<StringName, Map<StringName, String>>::Element *G = descr_cache.find(base);
  755. if (G) {
  756. Map<StringName, String>::Element *F = G->get().find(signal_name);
  757. if (F) {
  758. found = true;
  759. descr = F->get();
  760. }
  761. }
  762. if (!found) {
  763. DocData *dd = EditorHelp::get_doc_data();
  764. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(base);
  765. while (F && descr == String()) {
  766. for (int i = 0; i < F->get().signals.size(); i++) {
  767. if (F->get().signals[i].name == signal_name.operator String()) {
  768. descr = DTR(F->get().signals[i].description.strip_edges());
  769. break;
  770. }
  771. }
  772. if (!F->get().inherits.empty()) {
  773. F = dd->class_list.find(F->get().inherits);
  774. } else {
  775. break;
  776. }
  777. }
  778. descr_cache[base][signal_name] = descr;
  779. }
  780. // "::" separators used in make_custom_tooltip for formatting.
  781. item->set_tooltip(0, String(signal_name) + "::" + signaldesc + "::" + descr);
  782. }
  783. // List existing connections
  784. List<Object::Connection> connections;
  785. selectedNode->get_signal_connection_list(signal_name, &connections);
  786. for (List<Object::Connection>::Element *F = connections.front(); F; F = F->next()) {
  787. Connection cn = F->get();
  788. if (!(cn.flags & CONNECT_PERSIST))
  789. continue;
  790. ConnectDialog::ConnectionData c = cn;
  791. Node *target = Object::cast_to<Node>(c.target);
  792. if (!target)
  793. continue;
  794. String path = String(selectedNode->get_path_to(target)) + " :: " + c.method + "()";
  795. if (c.flags & CONNECT_DEFERRED)
  796. path += " (deferred)";
  797. if (c.flags & CONNECT_ONESHOT)
  798. path += " (oneshot)";
  799. if (c.binds.size()) {
  800. path += " binds(";
  801. for (int i = 0; i < c.binds.size(); i++) {
  802. if (i > 0)
  803. path += ", ";
  804. path += c.binds[i].operator String();
  805. }
  806. path += ")";
  807. }
  808. TreeItem *item2 = tree->create_item(item);
  809. item2->set_text(0, path);
  810. Connection cd = c;
  811. item2->set_metadata(0, cd);
  812. item2->set_icon(0, get_theme_icon("Slot", "EditorIcons"));
  813. }
  814. }
  815. if (!did_script) {
  816. did_script = true;
  817. } else {
  818. base = ClassDB::get_parent_class(base);
  819. }
  820. }
  821. connect_button->set_text(TTR("Connect..."));
  822. connect_button->set_disabled(true);
  823. }
  824. ConnectionsDock::ConnectionsDock(EditorNode *p_editor) {
  825. editor = p_editor;
  826. set_name(TTR("Signals"));
  827. VBoxContainer *vbc = this;
  828. tree = memnew(ConnectionsDockTree);
  829. tree->set_columns(1);
  830. tree->set_select_mode(Tree::SELECT_ROW);
  831. tree->set_hide_root(true);
  832. vbc->add_child(tree);
  833. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  834. tree->set_allow_rmb_select(true);
  835. connect_button = memnew(Button);
  836. HBoxContainer *hb = memnew(HBoxContainer);
  837. vbc->add_child(hb);
  838. hb->add_spacer();
  839. hb->add_child(connect_button);
  840. connect_button->connect("pressed", callable_mp(this, &ConnectionsDock::_connect_pressed));
  841. connect_dialog = memnew(ConnectDialog);
  842. add_child(connect_dialog);
  843. disconnect_all_dialog = memnew(ConfirmationDialog);
  844. add_child(disconnect_all_dialog);
  845. disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
  846. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  847. signal_menu = memnew(PopupMenu);
  848. add_child(signal_menu);
  849. signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  850. signal_menu->add_item(TTR("Connect..."), CONNECT);
  851. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  852. slot_menu = memnew(PopupMenu);
  853. add_child(slot_menu);
  854. slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  855. slot_menu->add_item(TTR("Edit..."), EDIT);
  856. slot_menu->add_item(TTR("Go To Method"), GO_TO_SCRIPT);
  857. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  858. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  859. tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
  860. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  861. tree->connect("item_rmb_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed));
  862. add_theme_constant_override("separation", 3 * EDSCALE);
  863. }
  864. ConnectionsDock::~ConnectionsDock() {
  865. }