connections_dialog.cpp 35 KB

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