connections_dialog.cpp 33 KB

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