connections_dialog.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /**************************************************************************/
  2. /* connections_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "connections_dialog.h"
  31. #include "core/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/argument_")) {
  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. }
  66. return true;
  67. }
  68. bool _get(const StringName &p_name, Variant &r_ret) const {
  69. String name = p_name;
  70. if (name.begins_with("bind/argument_")) {
  71. int which = name.get_slice("_", 1).to_int() - 1;
  72. ERR_FAIL_INDEX_V(which, params.size(), false);
  73. r_ret = params[which];
  74. } else {
  75. return false;
  76. }
  77. return true;
  78. }
  79. void _get_property_list(List<PropertyInfo> *p_list) const {
  80. for (int i = 0; i < params.size(); i++) {
  81. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));
  82. }
  83. }
  84. void notify_changed() {
  85. _change_notify();
  86. }
  87. ConnectDialogBinds() {
  88. }
  89. };
  90. /*
  91. * Signal automatically called by parent dialog.
  92. */
  93. void ConnectDialog::ok_pressed() {
  94. String method_name = dst_method->get_text();
  95. if (method_name == "") {
  96. error->set_text(TTR("Method in target node must be specified."));
  97. error->popup_centered_minsize();
  98. return;
  99. }
  100. if (!method_name.strip_edges().is_valid_identifier()) {
  101. error->set_text(TTR("Method name must be a valid identifier."));
  102. error->popup_centered();
  103. return;
  104. }
  105. Node *target = tree->get_selected();
  106. if (!target) {
  107. return; // Nothing selected in the tree, not an error.
  108. }
  109. if (target->get_script().is_null()) {
  110. if (!target->has_method(method_name)) {
  111. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  112. error->popup_centered_minsize();
  113. return;
  114. }
  115. }
  116. emit_signal("connected");
  117. hide();
  118. }
  119. void ConnectDialog::_cancel_pressed() {
  120. hide();
  121. }
  122. /*
  123. * Called each time a target node is selected within the target node tree.
  124. */
  125. void ConnectDialog::_tree_node_selected() {
  126. Node *current = tree->get_selected();
  127. if (!current) {
  128. return;
  129. }
  130. dst_path = source->get_path_to(current);
  131. _update_ok_enabled();
  132. }
  133. /*
  134. * Adds a new parameter bind to connection.
  135. */
  136. void ConnectDialog::_add_bind() {
  137. if (cdbinds->params.size() >= VARIANT_ARG_MAX) {
  138. return;
  139. }
  140. Variant::Type vt = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  141. Variant value;
  142. switch (vt) {
  143. case Variant::BOOL:
  144. value = false;
  145. break;
  146. case Variant::INT:
  147. value = 0;
  148. break;
  149. case Variant::REAL:
  150. value = 0.0;
  151. break;
  152. case Variant::STRING:
  153. value = "";
  154. break;
  155. case Variant::VECTOR2:
  156. value = Vector2();
  157. break;
  158. case Variant::RECT2:
  159. value = Rect2();
  160. break;
  161. case Variant::VECTOR3:
  162. value = Vector3();
  163. break;
  164. case Variant::PLANE:
  165. value = Plane();
  166. break;
  167. case Variant::QUAT:
  168. value = Quat();
  169. break;
  170. case Variant::AABB:
  171. value = AABB();
  172. break;
  173. case Variant::BASIS:
  174. value = Basis();
  175. break;
  176. case Variant::TRANSFORM:
  177. value = Transform();
  178. break;
  179. case Variant::COLOR:
  180. value = Color();
  181. break;
  182. default: {
  183. ERR_FAIL();
  184. } break;
  185. }
  186. ERR_FAIL_COND(value.get_type() == Variant::NIL);
  187. cdbinds->params.push_back(value);
  188. cdbinds->notify_changed();
  189. }
  190. /*
  191. * Remove parameter bind from connection.
  192. */
  193. void ConnectDialog::_remove_bind() {
  194. String st = bind_editor->get_selected_path();
  195. if (st == "") {
  196. return;
  197. }
  198. int idx = st.get_slice("/", 1).to_int() - 1;
  199. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  200. cdbinds->params.remove(idx);
  201. cdbinds->notify_changed();
  202. }
  203. /*
  204. * Enables or disables the connect button. The connect button is enabled if a
  205. * node is selected and valid in the selected mode.
  206. */
  207. void ConnectDialog::_update_ok_enabled() {
  208. Node *target = tree->get_selected();
  209. if (target == nullptr) {
  210. get_ok()->set_disabled(true);
  211. return;
  212. }
  213. if (!advanced->is_pressed() && target->get_script().is_null()) {
  214. get_ok()->set_disabled(true);
  215. return;
  216. }
  217. get_ok()->set_disabled(false);
  218. }
  219. void ConnectDialog::_notification(int p_what) {
  220. if (p_what == NOTIFICATION_ENTER_TREE) {
  221. bind_editor->edit(cdbinds);
  222. }
  223. }
  224. void ConnectDialog::_bind_methods() {
  225. ClassDB::bind_method("_advanced_pressed", &ConnectDialog::_advanced_pressed);
  226. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  227. ClassDB::bind_method("_tree_node_selected", &ConnectDialog::_tree_node_selected);
  228. ClassDB::bind_method("_add_bind", &ConnectDialog::_add_bind);
  229. ClassDB::bind_method("_remove_bind", &ConnectDialog::_remove_bind);
  230. ClassDB::bind_method("_update_ok_enabled", &ConnectDialog::_update_ok_enabled);
  231. ADD_SIGNAL(MethodInfo("connected"));
  232. }
  233. Node *ConnectDialog::get_source() const {
  234. return source;
  235. }
  236. StringName ConnectDialog::get_signal_name() const {
  237. return signal;
  238. }
  239. NodePath ConnectDialog::get_dst_path() const {
  240. return dst_path;
  241. }
  242. void ConnectDialog::set_dst_node(Node *p_node) {
  243. tree->set_selected(p_node);
  244. }
  245. StringName ConnectDialog::get_dst_method_name() const {
  246. String txt = dst_method->get_text();
  247. if (txt.find("(") != -1) {
  248. txt = txt.left(txt.find("(")).strip_edges();
  249. }
  250. return txt;
  251. }
  252. void ConnectDialog::set_dst_method(const StringName &p_method) {
  253. dst_method->set_text(p_method);
  254. }
  255. Vector<Variant> ConnectDialog::get_binds() const {
  256. return cdbinds->params;
  257. }
  258. bool ConnectDialog::get_deferred() const {
  259. return deferred->is_pressed();
  260. }
  261. bool ConnectDialog::get_oneshot() const {
  262. return oneshot->is_pressed();
  263. }
  264. /*
  265. * Returns true if ConnectDialog is being used to edit an existing connection.
  266. */
  267. bool ConnectDialog::is_editing() const {
  268. return bEditMode;
  269. }
  270. /*
  271. * Initialize ConnectDialog and populate fields with expected data.
  272. * If creating a connection from scratch, sensible defaults are used.
  273. * If editing an existing connection, previous data is retained.
  274. */
  275. void ConnectDialog::init(Connection c, bool bEdit) {
  276. set_hide_on_ok(false);
  277. source = static_cast<Node *>(c.source);
  278. signal = c.signal;
  279. tree->set_selected(nullptr);
  280. tree->set_marked(source, true);
  281. if (c.target) {
  282. set_dst_node(static_cast<Node *>(c.target));
  283. set_dst_method(c.method);
  284. }
  285. _update_ok_enabled();
  286. bool bDeferred = (c.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  287. bool bOneshot = (c.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT;
  288. deferred->set_pressed(bDeferred);
  289. oneshot->set_pressed(bOneshot);
  290. cdbinds->params.clear();
  291. cdbinds->params = c.binds;
  292. cdbinds->notify_changed();
  293. bEditMode = bEdit;
  294. }
  295. void ConnectDialog::popup_dialog(const String &p_for_signal) {
  296. from_signal->set_text(p_for_signal);
  297. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  298. if (!advanced->is_pressed()) {
  299. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  300. }
  301. popup_centered();
  302. }
  303. void ConnectDialog::_advanced_pressed() {
  304. if (advanced->is_pressed()) {
  305. set_custom_minimum_size(Size2(900, 500) * EDSCALE);
  306. connect_to_label->set_text(TTR("Connect to Node:"));
  307. tree->set_connect_to_script_mode(false);
  308. vbc_right->show();
  309. error_label->hide();
  310. } else {
  311. set_custom_minimum_size(Size2(600, 500) * EDSCALE);
  312. set_size(Size2());
  313. connect_to_label->set_text(TTR("Connect to Script:"));
  314. tree->set_connect_to_script_mode(true);
  315. vbc_right->hide();
  316. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  317. }
  318. _update_ok_enabled();
  319. set_position((get_viewport_rect().size - get_custom_minimum_size()) / 2);
  320. }
  321. ConnectDialog::ConnectDialog() {
  322. set_custom_minimum_size(Size2(600, 500) * EDSCALE);
  323. VBoxContainer *vbc = memnew(VBoxContainer);
  324. add_child(vbc);
  325. HBoxContainer *main_hb = memnew(HBoxContainer);
  326. vbc->add_child(main_hb);
  327. main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  328. VBoxContainer *vbc_left = memnew(VBoxContainer);
  329. main_hb->add_child(vbc_left);
  330. vbc_left->set_h_size_flags(SIZE_EXPAND_FILL);
  331. from_signal = memnew(LineEdit);
  332. from_signal->set_editable(false);
  333. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  334. tree = memnew(SceneTreeEditor(false));
  335. tree->set_connecting_signal(true);
  336. tree->set_show_enabled_subscene(true);
  337. tree->get_scene_tree()->connect("item_activated", this, "_ok");
  338. tree->connect("node_selected", this, "_tree_node_selected");
  339. tree->set_connect_to_script_mode(true);
  340. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), tree, true);
  341. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  342. error_label = memnew(Label);
  343. error_label->set_text(TTR("Scene does not contain any script."));
  344. vbc_left->add_child(error_label);
  345. error_label->hide();
  346. vbc_right = memnew(VBoxContainer);
  347. main_hb->add_child(vbc_right);
  348. vbc_right->set_h_size_flags(SIZE_EXPAND_FILL);
  349. vbc_right->hide();
  350. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  351. type_list = memnew(OptionButton);
  352. type_list->set_h_size_flags(SIZE_EXPAND_FILL);
  353. add_bind_hb->add_child(type_list);
  354. type_list->add_item("bool", Variant::BOOL);
  355. type_list->add_item("int", Variant::INT);
  356. type_list->add_item("real", Variant::REAL);
  357. type_list->add_item("String", Variant::STRING);
  358. type_list->add_item("Vector2", Variant::VECTOR2);
  359. type_list->add_item("Rect2", Variant::RECT2);
  360. type_list->add_item("Vector3", Variant::VECTOR3);
  361. type_list->add_item("Plane", Variant::PLANE);
  362. type_list->add_item("Quat", Variant::QUAT);
  363. type_list->add_item("AABB", Variant::AABB);
  364. type_list->add_item("Basis", Variant::BASIS);
  365. type_list->add_item("Transform", Variant::TRANSFORM);
  366. type_list->add_item("Color", Variant::COLOR);
  367. type_list->select(0);
  368. Button *add_bind = memnew(Button);
  369. add_bind->set_text(TTR("Add"));
  370. add_bind_hb->add_child(add_bind);
  371. add_bind->connect("pressed", this, "_add_bind");
  372. Button *del_bind = memnew(Button);
  373. del_bind->set_text(TTR("Remove"));
  374. add_bind_hb->add_child(del_bind);
  375. del_bind->connect("pressed", this, "_remove_bind");
  376. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  377. bind_editor = memnew(EditorInspector);
  378. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  379. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  380. vbc_left->add_margin_child(TTR("Receiver Method:"), dstm_hb);
  381. dst_method = memnew(LineEdit);
  382. dst_method->set_h_size_flags(SIZE_EXPAND_FILL);
  383. dst_method->connect("text_entered", this, "_builtin_text_entered");
  384. dstm_hb->add_child(dst_method);
  385. advanced = memnew(CheckButton);
  386. dstm_hb->add_child(advanced);
  387. advanced->set_text(TTR("Advanced"));
  388. advanced->connect("pressed", this, "_advanced_pressed");
  389. deferred = memnew(CheckBox);
  390. deferred->set_h_size_flags(0);
  391. deferred->set_text(TTR("Deferred"));
  392. deferred->set_tooltip(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  393. vbc_right->add_child(deferred);
  394. oneshot = memnew(CheckBox);
  395. oneshot->set_h_size_flags(0);
  396. oneshot->set_text(TTR("Oneshot"));
  397. oneshot->set_tooltip(TTR("Disconnects the signal after its first emission."));
  398. vbc_right->add_child(oneshot);
  399. set_as_toplevel(true);
  400. cdbinds = memnew(ConnectDialogBinds);
  401. error = memnew(AcceptDialog);
  402. add_child(error);
  403. error->set_title(TTR("Cannot connect signal"));
  404. error->get_ok()->set_text(TTR("Close"));
  405. get_ok()->set_text(TTR("Connect"));
  406. }
  407. ConnectDialog::~ConnectDialog() {
  408. memdelete(cdbinds);
  409. }
  410. //////////////////////////////////////////
  411. // Originally copied and adapted from EditorProperty, try to keep style in sync.
  412. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  413. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  414. help_bit->add_style_override("panel", get_stylebox("panel", "TooltipPanel"));
  415. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  416. String text = TTR("Signal:") + " [u][b]" + p_text.get_slice("::", 0) + "[/b][/u]";
  417. text += p_text.get_slice("::", 1).strip_edges() + "\n";
  418. text += p_text.get_slice("::", 2).strip_edges();
  419. help_bit->call_deferred("set_text", text); //hack so it uses proper theme once inside scene
  420. return help_bit;
  421. }
  422. struct _ConnectionsDockMethodInfoSort {
  423. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  424. return a.name < b.name;
  425. }
  426. };
  427. void ConnectionsDock::_filter_changed(const String &p_text) {
  428. update_tree();
  429. }
  430. /*
  431. * Post-ConnectDialog callback for creating/editing connections.
  432. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  433. */
  434. void ConnectionsDock::_make_or_edit_connection() {
  435. TreeItem *it = tree->get_selected();
  436. ERR_FAIL_COND(!it);
  437. NodePath dst_path = connect_dialog->get_dst_path();
  438. Node *target = selectedNode->get_node(dst_path);
  439. ERR_FAIL_COND(!target);
  440. Connection cToMake;
  441. cToMake.source = connect_dialog->get_source();
  442. cToMake.target = target;
  443. cToMake.signal = connect_dialog->get_signal_name();
  444. cToMake.method = connect_dialog->get_dst_method_name();
  445. cToMake.binds = connect_dialog->get_binds();
  446. bool defer = connect_dialog->get_deferred();
  447. bool oshot = connect_dialog->get_oneshot();
  448. cToMake.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0);
  449. // Conditions to add function: must have a script and must not have the method already
  450. // (in the class, the script itself, or inherited).
  451. bool add_script_function = false;
  452. Ref<Script> script = target->get_script();
  453. if (!target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cToMake.method)) {
  454. // There is a chance that the method is inherited from another script.
  455. bool found_inherited_function = false;
  456. Ref<Script> inherited_script = script->get_base_script();
  457. while (!inherited_script.is_null()) {
  458. int line = inherited_script->get_language()->find_function(cToMake.method, inherited_script->get_source_code());
  459. if (line != -1) {
  460. found_inherited_function = true;
  461. break;
  462. }
  463. inherited_script = inherited_script->get_base_script();
  464. }
  465. add_script_function = !found_inherited_function;
  466. }
  467. PoolStringArray script_function_args;
  468. if (add_script_function) {
  469. // Pick up args here before "it" is deleted by update_tree.
  470. script_function_args = it->get_metadata(0).operator Dictionary()["args"];
  471. for (int i = 0; i < cToMake.binds.size(); i++) {
  472. script_function_args.append("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cToMake.binds[i].get_type()));
  473. }
  474. }
  475. if (connect_dialog->is_editing()) {
  476. _disconnect(*it);
  477. _connect(cToMake);
  478. } else {
  479. _connect(cToMake);
  480. }
  481. // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, which will delete the object "it" is pointing to.
  482. it = nullptr;
  483. if (add_script_function) {
  484. editor->emit_signal("script_add_function_request", target, cToMake.method, script_function_args);
  485. hide();
  486. }
  487. update_tree();
  488. }
  489. /*
  490. * Creates single connection w/ undo-redo functionality.
  491. */
  492. void ConnectionsDock::_connect(Connection cToMake) {
  493. Node *source = static_cast<Node *>(cToMake.source);
  494. Node *target = static_cast<Node *>(cToMake.target);
  495. if (!source || !target) {
  496. return;
  497. }
  498. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(cToMake.signal), String(cToMake.method)));
  499. undo_redo->add_do_method(source, "connect", cToMake.signal, target, cToMake.method, cToMake.binds, cToMake.flags);
  500. undo_redo->add_undo_method(source, "disconnect", cToMake.signal, target, cToMake.method);
  501. undo_redo->add_do_method(this, "update_tree");
  502. undo_redo->add_undo_method(this, "update_tree");
  503. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  504. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  505. undo_redo->commit_action();
  506. }
  507. /*
  508. * Break single connection w/ undo-redo functionality.
  509. */
  510. void ConnectionsDock::_disconnect(TreeItem &item) {
  511. Connection c = item.get_metadata(0);
  512. ERR_FAIL_COND(c.source != selectedNode); // Shouldn't happen but... Bugcheck.
  513. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method));
  514. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, c.target, c.method);
  515. undo_redo->add_undo_method(selectedNode, "connect", c.signal, c.target, c.method, c.binds, c.flags);
  516. undo_redo->add_do_method(this, "update_tree");
  517. undo_redo->add_undo_method(this, "update_tree");
  518. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  519. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  520. undo_redo->commit_action();
  521. }
  522. /*
  523. * Break all connections of currently selected signal.
  524. * Can undo-redo as a single action.
  525. */
  526. void ConnectionsDock::_disconnect_all() {
  527. TreeItem *item = tree->get_selected();
  528. if (!_is_item_signal(*item)) {
  529. return;
  530. }
  531. TreeItem *child = item->get_children();
  532. String signalName = item->get_metadata(0).operator Dictionary()["name"];
  533. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signalName));
  534. while (child) {
  535. Connection c = child->get_metadata(0);
  536. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, c.target, c.method);
  537. undo_redo->add_undo_method(selectedNode, "connect", c.signal, c.target, c.method, c.binds, c.flags);
  538. child = child->get_next();
  539. }
  540. undo_redo->add_do_method(this, "update_tree");
  541. undo_redo->add_undo_method(this, "update_tree");
  542. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  543. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  544. undo_redo->commit_action();
  545. }
  546. void ConnectionsDock::_tree_item_selected() {
  547. TreeItem *item = tree->get_selected();
  548. if (!item) { // Unlikely. Disable button just in case.
  549. connect_button->set_text(TTR("Connect..."));
  550. connect_button->set_disabled(true);
  551. } else if (_is_item_signal(*item)) {
  552. connect_button->set_text(TTR("Connect..."));
  553. connect_button->set_disabled(false);
  554. } else {
  555. connect_button->set_text(TTR("Disconnect"));
  556. connect_button->set_disabled(false);
  557. }
  558. }
  559. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  560. TreeItem *item = tree->get_selected();
  561. if (!item) {
  562. return;
  563. }
  564. if (_is_item_signal(*item)) {
  565. _open_connection_dialog(*item);
  566. } else {
  567. _go_to_script(*item);
  568. }
  569. }
  570. bool ConnectionsDock::_is_item_signal(TreeItem &item) {
  571. return (item.get_parent() == tree->get_root() || item.get_parent()->get_parent() == tree->get_root());
  572. }
  573. /*
  574. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  575. */
  576. void ConnectionsDock::_open_connection_dialog(TreeItem &item) {
  577. String signal = item.get_metadata(0).operator Dictionary()["name"];
  578. const String &signalname = signal;
  579. String midname = selectedNode->get_name();
  580. for (int i = 0; i < midname.length(); i++) { //TODO: Regex filter may be cleaner.
  581. CharType c = midname[i];
  582. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) {
  583. if (c == ' ') {
  584. // Replace spaces with underlines.
  585. c = '_';
  586. } else {
  587. // Remove any other characters.
  588. midname.remove(i);
  589. i--;
  590. continue;
  591. }
  592. }
  593. midname[i] = c;
  594. }
  595. Node *dst_node = selectedNode->get_owner() ? selectedNode->get_owner() : selectedNode;
  596. if (!dst_node || dst_node->get_script().is_null()) {
  597. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  598. }
  599. StringName dst_method = "_on_" + midname + "_" + signal;
  600. Connection c;
  601. c.source = selectedNode;
  602. c.signal = StringName(signalname);
  603. c.target = dst_node;
  604. c.method = dst_method;
  605. connect_dialog->popup_dialog(signalname);
  606. connect_dialog->init(c);
  607. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  608. }
  609. /*
  610. * Open connection dialog with Connection data to EDIT an existing connection.
  611. */
  612. void ConnectionsDock::_open_connection_dialog(Connection cToEdit) {
  613. Node *src = static_cast<Node *>(cToEdit.source);
  614. Node *dst = static_cast<Node *>(cToEdit.target);
  615. if (src && dst) {
  616. const String &signalname = cToEdit.signal;
  617. connect_dialog->set_title(TTR("Edit Connection:") + cToEdit.signal);
  618. connect_dialog->popup_dialog(signalname);
  619. connect_dialog->init(cToEdit, true);
  620. }
  621. }
  622. /*
  623. * Open slot method location in script editor.
  624. */
  625. void ConnectionsDock::_go_to_script(TreeItem &item) {
  626. if (_is_item_signal(item)) {
  627. return;
  628. }
  629. Connection c = item.get_metadata(0);
  630. ERR_FAIL_COND(c.source != selectedNode); //shouldn't happen but...bugcheck
  631. if (!c.target) {
  632. return;
  633. }
  634. Ref<Script> script = c.target->get_script();
  635. if (script.is_null()) {
  636. return;
  637. }
  638. if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, c.method)) {
  639. editor->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  640. }
  641. }
  642. void ConnectionsDock::_handle_signal_menu_option(int option) {
  643. TreeItem *item = tree->get_selected();
  644. if (!item) {
  645. return;
  646. }
  647. switch (option) {
  648. case CONNECT: {
  649. _open_connection_dialog(*item);
  650. } break;
  651. case DISCONNECT_ALL: {
  652. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  653. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  654. disconnect_all_dialog->popup_centered();
  655. } break;
  656. }
  657. }
  658. void ConnectionsDock::_handle_slot_menu_option(int option) {
  659. TreeItem *item = tree->get_selected();
  660. if (!item) {
  661. return;
  662. }
  663. switch (option) {
  664. case EDIT: {
  665. Connection c = item->get_metadata(0);
  666. _open_connection_dialog(c);
  667. } break;
  668. case GO_TO_SCRIPT: {
  669. _go_to_script(*item);
  670. } break;
  671. case DISCONNECT: {
  672. _disconnect(*item);
  673. update_tree();
  674. } break;
  675. }
  676. }
  677. void ConnectionsDock::_rmb_pressed(Vector2 position) {
  678. TreeItem *item = tree->get_selected();
  679. if (!item) {
  680. return;
  681. }
  682. Vector2 global_position = tree->get_global_position() + position;
  683. if (_is_item_signal(*item)) {
  684. signal_menu->set_position(global_position);
  685. signal_menu->popup();
  686. } else {
  687. slot_menu->set_position(global_position);
  688. slot_menu->popup();
  689. }
  690. }
  691. void ConnectionsDock::_close() {
  692. hide();
  693. }
  694. void ConnectionsDock::_connect_pressed() {
  695. TreeItem *item = tree->get_selected();
  696. if (!item) {
  697. connect_button->set_disabled(true);
  698. return;
  699. }
  700. if (_is_item_signal(*item)) {
  701. _open_connection_dialog(*item);
  702. } else {
  703. _disconnect(*item);
  704. update_tree();
  705. }
  706. }
  707. void ConnectionsDock::_notification(int p_what) {
  708. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  709. update_tree();
  710. }
  711. }
  712. void ConnectionsDock::_bind_methods() {
  713. ClassDB::bind_method("_make_or_edit_connection", &ConnectionsDock::_make_or_edit_connection);
  714. ClassDB::bind_method("_disconnect_all", &ConnectionsDock::_disconnect_all);
  715. ClassDB::bind_method("_tree_item_selected", &ConnectionsDock::_tree_item_selected);
  716. ClassDB::bind_method("_tree_item_activated", &ConnectionsDock::_tree_item_activated);
  717. ClassDB::bind_method("_handle_signal_menu_option", &ConnectionsDock::_handle_signal_menu_option);
  718. ClassDB::bind_method("_handle_slot_menu_option", &ConnectionsDock::_handle_slot_menu_option);
  719. ClassDB::bind_method("_rmb_pressed", &ConnectionsDock::_rmb_pressed);
  720. ClassDB::bind_method("_close", &ConnectionsDock::_close);
  721. ClassDB::bind_method("_connect_pressed", &ConnectionsDock::_connect_pressed);
  722. ClassDB::bind_method("_filter_changed", &ConnectionsDock::_filter_changed);
  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<Texture> 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_icon(scr->get_class(), "EditorIcons")) {
  754. icon = get_icon(scr->get_class(), "EditorIcons");
  755. }
  756. }
  757. } else {
  758. ClassDB::get_signal_list(base, &node_signals2, true);
  759. if (has_icon(base, "EditorIcons")) {
  760. icon = get_icon(base, "EditorIcons");
  761. }
  762. name = base;
  763. }
  764. if (!icon.is_valid()) {
  765. icon = get_icon("Object", "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_color("prop_subsection", "Editor"));
  776. node_signals2.sort();
  777. }
  778. for (List<MethodInfo>::Element *E = node_signals2.front(); E; E = E->next()) {
  779. MethodInfo &mi = E->get();
  780. StringName signal_name = mi.name;
  781. String signaldesc = "(";
  782. PoolStringArray argnames;
  783. String filter_text = search_box->get_text();
  784. if (!filter_text.is_subsequence_ofi(signal_name)) {
  785. continue;
  786. }
  787. if (mi.arguments.size()) {
  788. for (int i = 0; i < mi.arguments.size(); i++) {
  789. PropertyInfo &pi = mi.arguments[i];
  790. if (i > 0) {
  791. signaldesc += ", ";
  792. }
  793. String tname = "var";
  794. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  795. tname = pi.class_name.operator String();
  796. } else if (pi.type != Variant::NIL) {
  797. tname = Variant::get_type_name(pi.type);
  798. }
  799. signaldesc += (pi.name == "" ? String("arg " + itos(i)) : pi.name) + ": " + tname;
  800. argnames.push_back(pi.name + ":" + tname);
  801. }
  802. }
  803. signaldesc += ")";
  804. // Create the children of the subsection - the actual list of signals.
  805. TreeItem *signal_item = tree->create_item(section_item);
  806. signal_item->set_text(0, String(signal_name) + signaldesc);
  807. Dictionary sinfo;
  808. sinfo["name"] = signal_name;
  809. sinfo["args"] = argnames;
  810. signal_item->set_metadata(0, sinfo);
  811. signal_item->set_icon(0, get_icon("Signal", "EditorIcons"));
  812. // Set tooltip with the signal's documentation.
  813. {
  814. String descr;
  815. bool found = false;
  816. Map<StringName, Map<StringName, String>>::Element *G = descr_cache.find(base);
  817. if (G) {
  818. Map<StringName, String>::Element *F = G->get().find(signal_name);
  819. if (F) {
  820. found = true;
  821. descr = F->get();
  822. }
  823. }
  824. if (!found) {
  825. DocData *dd = EditorHelp::get_doc_data();
  826. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(base);
  827. while (F && descr == String()) {
  828. for (int i = 0; i < F->get().signals.size(); i++) {
  829. if (F->get().signals[i].name == signal_name.operator String()) {
  830. descr = DTR(F->get().signals[i].description);
  831. break;
  832. }
  833. }
  834. if (!F->get().inherits.empty()) {
  835. F = dd->class_list.find(F->get().inherits);
  836. } else {
  837. break;
  838. }
  839. }
  840. descr_cache[base][signal_name] = descr;
  841. }
  842. // "::" separators used in make_custom_tooltip for formatting.
  843. signal_item->set_tooltip(0, String(signal_name) + "::" + signaldesc + "::" + descr);
  844. }
  845. // List existing connections
  846. List<Object::Connection> connections;
  847. selectedNode->get_signal_connection_list(signal_name, &connections);
  848. for (List<Object::Connection>::Element *F = connections.front(); F; F = F->next()) {
  849. Object::Connection &c = F->get();
  850. if (!(c.flags & CONNECT_PERSIST)) {
  851. continue;
  852. }
  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_icon("Slot", "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_icon("Search", "EditorIcons"));
  898. search_box->set_clear_button_enabled(true);
  899. search_box->connect("text_changed", this, "_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(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", this, "_connect_pressed");
  914. connect_dialog = memnew(ConnectDialog);
  915. connect_dialog->set_as_toplevel(true);
  916. add_child(connect_dialog);
  917. disconnect_all_dialog = memnew(ConfirmationDialog);
  918. disconnect_all_dialog->set_as_toplevel(true);
  919. add_child(disconnect_all_dialog);
  920. disconnect_all_dialog->connect("confirmed", this, "_disconnect_all");
  921. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  922. signal_menu = memnew(PopupMenu);
  923. add_child(signal_menu);
  924. signal_menu->connect("id_pressed", this, "_handle_signal_menu_option");
  925. signal_menu->add_item(TTR("Connect..."), CONNECT);
  926. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  927. slot_menu = memnew(PopupMenu);
  928. add_child(slot_menu);
  929. slot_menu->connect("id_pressed", this, "_handle_slot_menu_option");
  930. slot_menu->add_item(TTR("Edit..."), EDIT);
  931. slot_menu->add_item(TTR("Go to Method"), GO_TO_SCRIPT);
  932. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  933. connect_dialog->connect("connected", this, "_make_or_edit_connection");
  934. tree->connect("item_selected", this, "_tree_item_selected");
  935. tree->connect("item_activated", this, "_tree_item_activated");
  936. tree->connect("item_rmb_selected", this, "_rmb_pressed");
  937. add_constant_override("separation", 3 * EDSCALE);
  938. }
  939. ConnectionsDock::~ConnectionsDock() {
  940. }