connections_dialog.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*************************************************************************/
  2. /* connections_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_settings.h"
  34. #include "plugins/script_editor_plugin.h"
  35. #include "scene/gui/label.h"
  36. #include "scene/gui/popup_menu.h"
  37. class ConnectDialogBinds : public Object {
  38. GDCLASS(ConnectDialogBinds, Object);
  39. public:
  40. Vector<Variant> params;
  41. bool _set(const StringName &p_name, const Variant &p_value) {
  42. String name = p_name;
  43. if (name.begins_with("bind/")) {
  44. int which = name.get_slice("/", 1).to_int() - 1;
  45. ERR_FAIL_INDEX_V(which, params.size(), false);
  46. params.write[which] = p_value;
  47. } else
  48. return false;
  49. return true;
  50. }
  51. bool _get(const StringName &p_name, Variant &r_ret) const {
  52. String name = p_name;
  53. if (name.begins_with("bind/")) {
  54. int which = name.get_slice("/", 1).to_int() - 1;
  55. ERR_FAIL_INDEX_V(which, params.size(), false);
  56. r_ret = params[which];
  57. } else
  58. return false;
  59. return true;
  60. }
  61. void _get_property_list(List<PropertyInfo> *p_list) const {
  62. for (int i = 0; i < params.size(); i++) {
  63. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/" + itos(i + 1)));
  64. }
  65. }
  66. void notify_changed() {
  67. _change_notify();
  68. }
  69. ConnectDialogBinds() {
  70. }
  71. };
  72. /*
  73. Signal automatically called by parent dialog.
  74. */
  75. void ConnectDialog::ok_pressed() {
  76. if (dst_method->get_text() == "") {
  77. error->set_text(TTR("Method in target Node must be specified!"));
  78. error->popup_centered_minsize();
  79. return;
  80. }
  81. Node *target = tree->get_selected();
  82. if (target->get_script().is_null()) {
  83. if (!target->has_method(dst_method->get_text())) {
  84. error->set_text(TTR("Target method not found! Specify a valid method or attach a script to target Node."));
  85. error->popup_centered_minsize();
  86. return;
  87. }
  88. }
  89. emit_signal("connected");
  90. hide();
  91. }
  92. void ConnectDialog::_cancel_pressed() {
  93. hide();
  94. }
  95. /*
  96. Called each time a target node is selected within the target node tree.
  97. */
  98. void ConnectDialog::_tree_node_selected() {
  99. Node *current = tree->get_selected();
  100. if (!current) {
  101. make_callback->hide();
  102. return;
  103. }
  104. if (current->get_script().is_null())
  105. make_callback->hide();
  106. else
  107. make_callback->show();
  108. dst_path->set_text(source->get_path_to(current));
  109. }
  110. /*
  111. Adds a new parameter bind to connection.
  112. */
  113. void ConnectDialog::_add_bind() {
  114. if (cdbinds->params.size() >= VARIANT_ARG_MAX)
  115. return;
  116. Variant::Type vt = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  117. Variant value;
  118. switch (vt) {
  119. case Variant::BOOL: value = false; break;
  120. case Variant::INT: value = 0; break;
  121. case Variant::REAL: value = 0.0; break;
  122. case Variant::STRING: value = ""; break;
  123. case Variant::VECTOR2: value = Vector2(); break;
  124. case Variant::RECT2: value = Rect2(); break;
  125. case Variant::VECTOR3: value = Vector3(); break;
  126. case Variant::PLANE: value = Plane(); break;
  127. case Variant::QUAT: value = Quat(); break;
  128. case Variant::AABB: value = AABB(); break;
  129. case Variant::BASIS: value = Basis(); break;
  130. case Variant::TRANSFORM: value = Transform(); break;
  131. case Variant::COLOR: value = Color(); break;
  132. default: { ERR_FAIL(); } break;
  133. }
  134. ERR_FAIL_COND(value.get_type() == Variant::NIL);
  135. cdbinds->params.push_back(value);
  136. cdbinds->notify_changed();
  137. }
  138. /*
  139. Remove parameter bind from connection.
  140. */
  141. void ConnectDialog::_remove_bind() {
  142. String st = bind_editor->get_selected_path();
  143. if (st == "")
  144. return;
  145. int idx = st.get_slice("/", 1).to_int() - 1;
  146. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  147. cdbinds->params.remove(idx);
  148. cdbinds->notify_changed();
  149. }
  150. void ConnectDialog::_notification(int p_what) {
  151. if (p_what == NOTIFICATION_ENTER_TREE) {
  152. bind_editor->edit(cdbinds);
  153. }
  154. }
  155. void ConnectDialog::_bind_methods() {
  156. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  157. ClassDB::bind_method("_tree_node_selected", &ConnectDialog::_tree_node_selected);
  158. ClassDB::bind_method("_add_bind", &ConnectDialog::_add_bind);
  159. ClassDB::bind_method("_remove_bind", &ConnectDialog::_remove_bind);
  160. ADD_SIGNAL(MethodInfo("connected"));
  161. }
  162. Node *ConnectDialog::get_source() const {
  163. return source;
  164. }
  165. StringName ConnectDialog::get_signal_name() const {
  166. return signal;
  167. }
  168. NodePath ConnectDialog::get_dst_path() const {
  169. return dst_path->get_text();
  170. }
  171. void ConnectDialog::set_dst_node(Node *p_node) {
  172. tree->set_selected(p_node);
  173. }
  174. StringName ConnectDialog::get_dst_method_name() const {
  175. String txt = dst_method->get_text();
  176. if (txt.find("(") != -1)
  177. txt = txt.left(txt.find("(")).strip_edges();
  178. return txt;
  179. }
  180. void ConnectDialog::set_dst_method(const StringName &p_method) {
  181. dst_method->set_text(p_method);
  182. }
  183. Vector<Variant> ConnectDialog::get_binds() const {
  184. return cdbinds->params;
  185. }
  186. bool ConnectDialog::get_deferred() const {
  187. return deferred->is_pressed();
  188. }
  189. bool ConnectDialog::get_oneshot() const {
  190. return oneshot->is_pressed();
  191. }
  192. /*
  193. Returns true if ConnectDialog is being used to edit an existing connection.
  194. */
  195. bool ConnectDialog::is_editing() const {
  196. return bEditMode;
  197. }
  198. /*
  199. Initialize ConnectDialog and populate fields with expected data.
  200. If creating a connection from scratch, sensible defaults are used.
  201. If editing an existing connection, previous data is retained.
  202. */
  203. void ConnectDialog::init(Connection c, bool bEdit) {
  204. source = static_cast<Node *>(c.source);
  205. signal = c.signal;
  206. tree->set_selected(NULL);
  207. tree->set_marked(source, true);
  208. set_dst_node(static_cast<Node *>(c.target));
  209. set_dst_method(c.method);
  210. bool bDeferred = (c.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  211. bool bOneshot = (c.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT;
  212. deferred->set_pressed(bDeferred);
  213. oneshot->set_pressed(bOneshot);
  214. cdbinds->params.clear();
  215. cdbinds->params = c.binds;
  216. cdbinds->notify_changed();
  217. bEditMode = bEdit;
  218. }
  219. ConnectDialog::ConnectDialog() {
  220. VBoxContainer *vbc = memnew(VBoxContainer);
  221. add_child(vbc);
  222. HBoxContainer *main_hb = memnew(HBoxContainer);
  223. vbc->add_child(main_hb);
  224. main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  225. VBoxContainer *vbc_left = memnew(VBoxContainer);
  226. main_hb->add_child(vbc_left);
  227. vbc_left->set_h_size_flags(SIZE_EXPAND_FILL);
  228. tree = memnew(SceneTreeEditor(false));
  229. tree->get_scene_tree()->connect("item_activated", this, "_ok");
  230. tree->connect("node_selected", this, "_tree_node_selected");
  231. vbc_left->add_margin_child(TTR("Connect To Node:"), tree, true);
  232. VBoxContainer *vbc_right = memnew(VBoxContainer);
  233. main_hb->add_child(vbc_right);
  234. vbc_right->set_h_size_flags(SIZE_EXPAND_FILL);
  235. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  236. type_list = memnew(OptionButton);
  237. type_list->set_h_size_flags(SIZE_EXPAND_FILL);
  238. add_bind_hb->add_child(type_list);
  239. type_list->add_item("bool", Variant::BOOL);
  240. type_list->add_item("int", Variant::INT);
  241. type_list->add_item("real", Variant::REAL);
  242. type_list->add_item("string", Variant::STRING);
  243. type_list->add_item("Vector2", Variant::VECTOR2);
  244. type_list->add_item("Rect2", Variant::RECT2);
  245. type_list->add_item("Vector3", Variant::VECTOR3);
  246. type_list->add_item("Plane", Variant::PLANE);
  247. type_list->add_item("Quat", Variant::QUAT);
  248. type_list->add_item("AABB", Variant::AABB);
  249. type_list->add_item("Basis", Variant::BASIS);
  250. type_list->add_item("Transform", Variant::TRANSFORM);
  251. type_list->add_item("Color", Variant::COLOR);
  252. type_list->select(0);
  253. Button *add_bind = memnew(Button);
  254. add_bind->set_text(TTR("Add"));
  255. add_bind_hb->add_child(add_bind);
  256. add_bind->connect("pressed", this, "_add_bind");
  257. Button *del_bind = memnew(Button);
  258. del_bind->set_text(TTR("Remove"));
  259. add_bind_hb->add_child(del_bind);
  260. del_bind->connect("pressed", this, "_remove_bind");
  261. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  262. bind_editor = memnew(EditorInspector);
  263. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  264. dst_path = memnew(LineEdit);
  265. vbc->add_margin_child(TTR("Path to Node:"), dst_path);
  266. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  267. vbc->add_margin_child("Method In Node:", dstm_hb);
  268. dst_method = memnew(LineEdit);
  269. dst_method->set_h_size_flags(SIZE_EXPAND_FILL);
  270. dstm_hb->add_child(dst_method);
  271. /*
  272. dst_method_list = memnew( MenuButton );
  273. dst_method_list->set_text("List...");
  274. dst_method_list->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  275. dst_method_list->set_anchor( MARGIN_LEFT, ANCHOR_END );
  276. dst_method_list->set_anchor( MARGIN_TOP, ANCHOR_END );
  277. dst_method_list->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  278. dst_method_list->set_begin( Point2( 70,59) );
  279. dst_method_list->set_end( Point2( 15,39 ) );
  280. */
  281. make_callback = memnew(CheckButton);
  282. make_callback->set_toggle_mode(true);
  283. make_callback->set_pressed(EDITOR_DEF("text_editor/tools/create_signal_callbacks", true));
  284. make_callback->set_text(TTR("Make Function"));
  285. dstm_hb->add_child(make_callback);
  286. deferred = memnew(CheckButton);
  287. deferred->set_text(TTR("Deferred"));
  288. dstm_hb->add_child(deferred);
  289. oneshot = memnew(CheckButton);
  290. oneshot->set_text(TTR("Oneshot"));
  291. dstm_hb->add_child(oneshot);
  292. set_as_toplevel(true);
  293. cdbinds = memnew(ConnectDialogBinds);
  294. error = memnew(ConfirmationDialog);
  295. add_child(error);
  296. error->get_ok()->set_text(TTR("Close"));
  297. get_ok()->set_text(TTR("Connect"));
  298. }
  299. ConnectDialog::~ConnectDialog() {
  300. memdelete(cdbinds);
  301. }
  302. //ConnectionsDock ==========================
  303. struct _ConnectionsDockMethodInfoSort {
  304. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  305. return a.name < b.name;
  306. }
  307. };
  308. /*
  309. Post-ConnectDialog callback for creating/editing connections.
  310. Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  311. */
  312. void ConnectionsDock::_make_or_edit_connection() {
  313. TreeItem *it = tree->get_selected();
  314. ERR_FAIL_COND(!it);
  315. NodePath dst_path = connect_dialog->get_dst_path();
  316. Node *target = selectedNode->get_node(dst_path);
  317. ERR_FAIL_COND(!target);
  318. Connection cToMake;
  319. cToMake.source = connect_dialog->get_source();
  320. cToMake.target = target;
  321. cToMake.signal = connect_dialog->get_signal_name();
  322. cToMake.method = connect_dialog->get_dst_method_name();
  323. cToMake.binds = connect_dialog->get_binds();
  324. bool defer = connect_dialog->get_deferred();
  325. bool oshot = connect_dialog->get_oneshot();
  326. cToMake.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0);
  327. bool add_script_function = connect_dialog->get_make_callback();
  328. PoolStringArray script_function_args;
  329. if (add_script_function) {
  330. // pick up args here before "it" is deleted by update_tree
  331. script_function_args = it->get_metadata(0).operator Dictionary()["args"];
  332. for (int i = 0; i < cToMake.binds.size(); i++) {
  333. script_function_args.append("extra_arg_" + itos(i));
  334. }
  335. }
  336. if (connect_dialog->is_editing()) {
  337. _disconnect(*it);
  338. _connect(cToMake);
  339. } else {
  340. _connect(cToMake);
  341. }
  342. // IMPORTANT NOTE: _disconnect and _connect cause an update_tree,
  343. // which will delete the object "it" is pointing to
  344. it = NULL;
  345. if (add_script_function) {
  346. editor->emit_signal("script_add_function_request", target, cToMake.method, script_function_args);
  347. hide();
  348. }
  349. update_tree();
  350. }
  351. /*
  352. Creates single connection w/ undo-redo functionality.
  353. */
  354. void ConnectionsDock::_connect(Connection cToMake) {
  355. Node *source = static_cast<Node *>(cToMake.source);
  356. Node *target = static_cast<Node *>(cToMake.target);
  357. if (!source || !target)
  358. return;
  359. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(cToMake.signal), String(cToMake.method)));
  360. undo_redo->add_do_method(source, "connect", cToMake.signal, target, cToMake.method, cToMake.binds, cToMake.flags);
  361. undo_redo->add_undo_method(source, "disconnect", cToMake.signal, target, cToMake.method);
  362. undo_redo->add_do_method(this, "update_tree");
  363. undo_redo->add_undo_method(this, "update_tree");
  364. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  365. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  366. undo_redo->commit_action();
  367. }
  368. /*
  369. Break single connection w/ undo-redo functionality.
  370. */
  371. void ConnectionsDock::_disconnect(TreeItem &item) {
  372. Connection c = item.get_metadata(0);
  373. ERR_FAIL_COND(c.source != selectedNode); //shouldn't happen but...bugcheck
  374. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method));
  375. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, c.target, c.method);
  376. undo_redo->add_undo_method(selectedNode, "connect", c.signal, c.target, c.method, c.binds, c.flags);
  377. undo_redo->add_do_method(this, "update_tree");
  378. undo_redo->add_undo_method(this, "update_tree");
  379. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  380. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  381. undo_redo->commit_action();
  382. }
  383. /*
  384. Break all connections of currently selected signal.
  385. Can undo-redo as a single action.
  386. */
  387. void ConnectionsDock::_disconnect_all() {
  388. TreeItem *item = tree->get_selected();
  389. if (!_is_item_signal(*item))
  390. return;
  391. TreeItem *child = item->get_children();
  392. String signalName = item->get_metadata(0).operator Dictionary()["name"];
  393. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signalName));
  394. while (child) {
  395. Connection c = child->get_metadata(0);
  396. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, c.target, c.method);
  397. undo_redo->add_undo_method(selectedNode, "connect", c.signal, c.target, c.method, c.binds, c.flags);
  398. child = child->get_next();
  399. }
  400. undo_redo->add_do_method(this, "update_tree");
  401. undo_redo->add_undo_method(this, "update_tree");
  402. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  403. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  404. undo_redo->commit_action();
  405. }
  406. void ConnectionsDock::_tree_item_selected() {
  407. TreeItem *item = tree->get_selected();
  408. if (!item) { //Unlikely. Disable button just in case.
  409. connect_button->set_text(TTR("Connect..."));
  410. connect_button->set_disabled(true);
  411. } else if (_is_item_signal(*item)) {
  412. connect_button->set_text(TTR("Connect..."));
  413. connect_button->set_disabled(false);
  414. } else {
  415. connect_button->set_text(TTR("Disconnect"));
  416. connect_button->set_disabled(false);
  417. }
  418. }
  419. void ConnectionsDock::_tree_item_activated() { //"Activation" on double-click.
  420. TreeItem *item = tree->get_selected();
  421. if (!item)
  422. return;
  423. if (_is_item_signal(*item)) {
  424. _open_connection_dialog(*item);
  425. } else {
  426. _go_to_script(*item);
  427. }
  428. }
  429. bool ConnectionsDock::_is_item_signal(TreeItem &item) {
  430. return (item.get_parent() == tree->get_root() || item.get_parent()->get_parent() == tree->get_root());
  431. }
  432. /*
  433. Open connection dialog with TreeItem data to CREATE a brand-new connection.
  434. */
  435. void ConnectionsDock::_open_connection_dialog(TreeItem &item) {
  436. String signal = item.get_metadata(0).operator Dictionary()["name"];
  437. String signalname = signal;
  438. String midname = selectedNode->get_name();
  439. for (int i = 0; i < midname.length(); i++) { //TODO: Regex filter may be cleaner.
  440. CharType c = midname[i];
  441. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) {
  442. if (c == ' ') {
  443. //Replace spaces with underlines.
  444. c = '_';
  445. } else {
  446. //Remove any other characters.
  447. midname.remove(i);
  448. i--;
  449. continue;
  450. }
  451. }
  452. midname[i] = c;
  453. }
  454. Node *dst_node = selectedNode->get_owner() ? selectedNode->get_owner() : selectedNode;
  455. StringName dst_method = "_on_" + midname + "_" + signal;
  456. Connection c;
  457. c.source = selectedNode;
  458. c.signal = StringName(signalname);
  459. c.target = dst_node;
  460. c.method = dst_method;
  461. connect_dialog->init(c);
  462. connect_dialog->set_title(TTR("Connect Signal: ") + signalname);
  463. connect_dialog->popup_centered_ratio();
  464. }
  465. /*
  466. Open connection dialog with Connection data to EDIT an existing connection.
  467. */
  468. void ConnectionsDock::_open_connection_dialog(Connection cToEdit) {
  469. Node *src = static_cast<Node *>(cToEdit.source);
  470. Node *dst = static_cast<Node *>(cToEdit.target);
  471. if (src && dst) {
  472. connect_dialog->init(cToEdit, true);
  473. connect_dialog->set_title(TTR("Edit Connection: ") + cToEdit.signal);
  474. connect_dialog->popup_centered_ratio();
  475. }
  476. }
  477. /*
  478. Open slot method location in script editor.
  479. */
  480. void ConnectionsDock::_go_to_script(TreeItem &item) {
  481. if (_is_item_signal(item))
  482. return;
  483. Connection c = item.get_metadata(0);
  484. ERR_FAIL_COND(c.source != selectedNode); //shouldn't happen but...bugcheck
  485. if (!c.target)
  486. return;
  487. Ref<Script> script = c.target->get_script();
  488. if (script.is_null())
  489. return;
  490. if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, c.method)) {
  491. editor->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  492. }
  493. }
  494. void ConnectionsDock::_handle_signal_menu_option(int option) {
  495. TreeItem *item = tree->get_selected();
  496. if (!item)
  497. return;
  498. switch (option) {
  499. case CONNECT: {
  500. _open_connection_dialog(*item);
  501. } break;
  502. case DISCONNECT_ALL: {
  503. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  504. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  505. disconnect_all_dialog->popup_centered();
  506. } break;
  507. }
  508. }
  509. void ConnectionsDock::_handle_slot_menu_option(int option) {
  510. TreeItem *item = tree->get_selected();
  511. if (!item)
  512. return;
  513. switch (option) {
  514. case EDIT: {
  515. Connection c = item->get_metadata(0);
  516. _open_connection_dialog(c);
  517. } break;
  518. case GO_TO_SCRIPT: {
  519. _go_to_script(*item);
  520. } break;
  521. case DISCONNECT: {
  522. _disconnect(*item);
  523. update_tree();
  524. } break;
  525. }
  526. }
  527. void ConnectionsDock::_rmb_pressed(Vector2 position) {
  528. TreeItem *item = tree->get_selected();
  529. if (!item)
  530. return;
  531. Vector2 global_position = tree->get_global_position() + position;
  532. if (_is_item_signal(*item)) {
  533. signal_menu->set_position(global_position);
  534. signal_menu->popup();
  535. } else {
  536. slot_menu->set_position(global_position);
  537. slot_menu->popup();
  538. }
  539. }
  540. void ConnectionsDock::_close() {
  541. hide();
  542. }
  543. void ConnectionsDock::_connect_pressed() {
  544. TreeItem *item = tree->get_selected();
  545. if (!item) {
  546. connect_button->set_disabled(true);
  547. return;
  548. }
  549. if (_is_item_signal(*item)) {
  550. _open_connection_dialog(*item);
  551. } else {
  552. _disconnect(*item);
  553. update_tree();
  554. }
  555. }
  556. void ConnectionsDock::_notification(int p_what) {
  557. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  558. update_tree();
  559. }
  560. }
  561. void ConnectionsDock::_bind_methods() {
  562. ClassDB::bind_method("_make_or_edit_connection", &ConnectionsDock::_make_or_edit_connection);
  563. ClassDB::bind_method("_disconnect_all", &ConnectionsDock::_disconnect_all);
  564. ClassDB::bind_method("_tree_item_selected", &ConnectionsDock::_tree_item_selected);
  565. ClassDB::bind_method("_tree_item_activated", &ConnectionsDock::_tree_item_activated);
  566. ClassDB::bind_method("_handle_signal_menu_option", &ConnectionsDock::_handle_signal_menu_option);
  567. ClassDB::bind_method("_handle_slot_menu_option", &ConnectionsDock::_handle_slot_menu_option);
  568. ClassDB::bind_method("_rmb_pressed", &ConnectionsDock::_rmb_pressed);
  569. ClassDB::bind_method("_close", &ConnectionsDock::_close);
  570. ClassDB::bind_method("_connect_pressed", &ConnectionsDock::_connect_pressed);
  571. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  572. }
  573. void ConnectionsDock::set_node(Node *p_node) {
  574. selectedNode = p_node;
  575. update_tree();
  576. }
  577. void ConnectionsDock::update_tree() {
  578. tree->clear();
  579. if (!selectedNode)
  580. return;
  581. TreeItem *root = tree->create_item();
  582. List<MethodInfo> node_signals;
  583. selectedNode->get_signal_list(&node_signals);
  584. //node_signals.sort_custom<_ConnectionsDockMethodInfoSort>();
  585. bool did_script = false;
  586. StringName base = selectedNode->get_class();
  587. while (base) {
  588. List<MethodInfo> node_signals;
  589. Ref<Texture> icon;
  590. String name;
  591. if (!did_script) {
  592. Ref<Script> scr = selectedNode->get_script();
  593. if (scr.is_valid()) {
  594. scr->get_script_signal_list(&node_signals);
  595. if (scr->get_path().is_resource_file())
  596. name = scr->get_path().get_file();
  597. else
  598. name = scr->get_class();
  599. if (has_icon(scr->get_class(), "EditorIcons")) {
  600. icon = get_icon(scr->get_class(), "EditorIcons");
  601. }
  602. }
  603. } else {
  604. ClassDB::get_signal_list(base, &node_signals, true);
  605. if (has_icon(base, "EditorIcons")) {
  606. icon = get_icon(base, "EditorIcons");
  607. }
  608. name = base;
  609. }
  610. TreeItem *pitem = NULL;
  611. if (node_signals.size()) {
  612. pitem = tree->create_item(root);
  613. pitem->set_text(0, name);
  614. pitem->set_icon(0, icon);
  615. pitem->set_selectable(0, false);
  616. pitem->set_editable(0, false);
  617. pitem->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  618. node_signals.sort();
  619. }
  620. for (List<MethodInfo>::Element *E = node_signals.front(); E; E = E->next()) {
  621. MethodInfo &mi = E->get();
  622. String signaldesc;
  623. signaldesc = mi.name + "(";
  624. PoolStringArray argnames;
  625. if (mi.arguments.size()) {
  626. signaldesc += " ";
  627. for (int i = 0; i < mi.arguments.size(); i++) {
  628. PropertyInfo &pi = mi.arguments[i];
  629. if (i > 0)
  630. signaldesc += ", ";
  631. String tname = "var";
  632. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  633. tname = pi.class_name.operator String();
  634. } else if (pi.type != Variant::NIL) {
  635. tname = Variant::get_type_name(pi.type);
  636. }
  637. signaldesc += tname + " " + (pi.name == "" ? String("arg " + itos(i)) : pi.name);
  638. argnames.push_back(pi.name + ":" + tname);
  639. }
  640. signaldesc += " ";
  641. }
  642. signaldesc += ")";
  643. TreeItem *item = tree->create_item(pitem);
  644. item->set_text(0, signaldesc);
  645. Dictionary sinfo;
  646. sinfo["name"] = mi.name;
  647. sinfo["args"] = argnames;
  648. item->set_metadata(0, sinfo);
  649. item->set_icon(0, get_icon("Signal", "EditorIcons"));
  650. List<Object::Connection> connections;
  651. selectedNode->get_signal_connection_list(mi.name, &connections);
  652. for (List<Object::Connection>::Element *F = connections.front(); F; F = F->next()) {
  653. Object::Connection &c = F->get();
  654. if (!(c.flags & CONNECT_PERSIST))
  655. continue;
  656. Node *target = Object::cast_to<Node>(c.target);
  657. if (!target)
  658. continue;
  659. String path = String(selectedNode->get_path_to(target)) + " :: " + c.method + "()";
  660. if (c.flags & CONNECT_DEFERRED)
  661. path += " (deferred)";
  662. if (c.flags & CONNECT_ONESHOT)
  663. path += " (oneshot)";
  664. if (c.binds.size()) {
  665. path += " binds( ";
  666. for (int i = 0; i < c.binds.size(); i++) {
  667. if (i > 0)
  668. path += ", ";
  669. path += c.binds[i].operator String();
  670. }
  671. path += " )";
  672. }
  673. TreeItem *item2 = tree->create_item(item);
  674. item2->set_text(0, path);
  675. item2->set_metadata(0, c);
  676. item2->set_icon(0, get_icon("Slot", "EditorIcons"));
  677. }
  678. }
  679. if (!did_script) {
  680. did_script = true;
  681. } else {
  682. base = ClassDB::get_parent_class(base);
  683. }
  684. }
  685. connect_button->set_text(TTR("Connect"));
  686. connect_button->set_disabled(true);
  687. }
  688. ConnectionsDock::ConnectionsDock(EditorNode *p_editor) {
  689. editor = p_editor;
  690. set_name(TTR("Signals"));
  691. VBoxContainer *vbc = this;
  692. tree = memnew(Tree);
  693. tree->set_columns(1);
  694. tree->set_select_mode(Tree::SELECT_ROW);
  695. tree->set_hide_root(true);
  696. vbc->add_child(tree);
  697. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  698. tree->set_allow_rmb_select(true);
  699. connect_button = memnew(Button);
  700. connect_button->set_text(TTR("Connect"));
  701. HBoxContainer *hb = memnew(HBoxContainer);
  702. vbc->add_child(hb);
  703. hb->add_spacer();
  704. hb->add_child(connect_button);
  705. connect_button->connect("pressed", this, "_connect_pressed");
  706. connect_dialog = memnew(ConnectDialog);
  707. connect_dialog->set_as_toplevel(true);
  708. add_child(connect_dialog);
  709. disconnect_all_dialog = memnew(ConfirmationDialog);
  710. disconnect_all_dialog->set_as_toplevel(true);
  711. add_child(disconnect_all_dialog);
  712. disconnect_all_dialog->connect("confirmed", this, "_disconnect_all");
  713. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  714. signal_menu = memnew(PopupMenu);
  715. add_child(signal_menu);
  716. signal_menu->connect("id_pressed", this, "_handle_signal_menu_option");
  717. signal_menu->add_item(TTR("Connect..."), CONNECT);
  718. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  719. slot_menu = memnew(PopupMenu);
  720. add_child(slot_menu);
  721. slot_menu->connect("id_pressed", this, "_handle_slot_menu_option");
  722. slot_menu->add_item(TTR("Edit..."), EDIT);
  723. slot_menu->add_item(TTR("Go To Method"), GO_TO_SCRIPT);
  724. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  725. /*
  726. node_only->set_anchor( MARGIN_TOP, ANCHOR_END );
  727. node_only->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  728. node_only->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  729. node_only->set_begin( Point2( 20,51) );
  730. node_only->set_end( Point2( 10,44) );
  731. */
  732. connect_dialog->connect("connected", this, "_make_or_edit_connection");
  733. tree->connect("item_selected", this, "_tree_item_selected");
  734. tree->connect("item_activated", this, "_tree_item_activated");
  735. tree->connect("item_rmb_selected", this, "_rmb_pressed");
  736. add_constant_override("separation", 3 * EDSCALE);
  737. }
  738. ConnectionsDock::~ConnectionsDock() {
  739. }