connections_dialog.cpp 33 KB

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