connections_dialog.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*************************************************************************/
  2. /* connections_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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_node.h"
  32. #include "editor_settings.h"
  33. #include "plugins/script_editor_plugin.h"
  34. #include "print_string.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[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. void ConnectDialog::_notification(int p_what) {
  73. if (p_what == NOTIFICATION_ENTER_TREE) {
  74. bind_editor->edit(cdbinds);
  75. }
  76. }
  77. void ConnectDialog::_tree_node_selected() {
  78. //dst_method_list->get_popup()->clear();
  79. Node *current = tree->get_selected();
  80. if (!current) {
  81. make_callback->hide();
  82. return;
  83. }
  84. if (current->get_script().is_null())
  85. make_callback->hide();
  86. else
  87. make_callback->show();
  88. dst_path->set_text(node->get_path_to(current));
  89. }
  90. void ConnectDialog::edit(Node *p_node) {
  91. node = p_node;
  92. //dst_method_list->get_popup()->clear();
  93. tree->set_selected(NULL);
  94. tree->set_marked(node, true);
  95. dst_path->set_text("");
  96. dst_method->set_text("");
  97. deferred->set_pressed(false);
  98. oneshot->set_pressed(false);
  99. cdbinds->params.clear();
  100. cdbinds->notify_changed();
  101. }
  102. void ConnectDialog::ok_pressed() {
  103. if (dst_method->get_text() == "") {
  104. error->set_text(TTR("Method in target Node must be specified!"));
  105. error->popup_centered_minsize();
  106. return;
  107. }
  108. Node *target = tree->get_selected();
  109. if (target->get_script().is_null()) {
  110. if (!target->has_method(dst_method->get_text())) {
  111. error->set_text(TTR("Target method not found! Specify a valid method or attach a script to target Node."));
  112. error->popup_centered_minsize();
  113. return;
  114. }
  115. }
  116. emit_signal("connected");
  117. hide();
  118. }
  119. void ConnectDialog::_cancel_pressed() {
  120. hide();
  121. }
  122. NodePath ConnectDialog::get_dst_path() const {
  123. return dst_path->get_text();
  124. }
  125. bool ConnectDialog::get_deferred() const {
  126. return deferred->is_pressed();
  127. }
  128. bool ConnectDialog::get_oneshot() const {
  129. return oneshot->is_pressed();
  130. }
  131. StringName ConnectDialog::get_dst_method() const {
  132. String txt = dst_method->get_text();
  133. if (txt.find("(") != -1)
  134. txt = txt.left(txt.find("(")).strip_edges();
  135. return txt;
  136. }
  137. Vector<Variant> ConnectDialog::get_binds() const {
  138. return cdbinds->params;
  139. }
  140. void ConnectDialog::_add_bind() {
  141. if (cdbinds->params.size() >= VARIANT_ARG_MAX)
  142. return;
  143. Variant::Type vt = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  144. Variant value;
  145. switch (vt) {
  146. case Variant::BOOL: value = false; break;
  147. case Variant::INT: value = 0; break;
  148. case Variant::REAL: value = 0.0; break;
  149. case Variant::STRING: value = ""; break;
  150. case Variant::VECTOR2: value = Vector2(); break;
  151. case Variant::RECT2: value = Rect2(); break;
  152. case Variant::VECTOR3: value = Vector3(); break;
  153. case Variant::PLANE: value = Plane(); break;
  154. case Variant::QUAT: value = Quat(); break;
  155. case Variant::AABB: value = AABB(); break;
  156. case Variant::BASIS: value = Basis(); break;
  157. case Variant::TRANSFORM: value = Transform(); break;
  158. case Variant::COLOR: value = Color(); break;
  159. default: { ERR_FAIL(); } break;
  160. }
  161. ERR_FAIL_COND(value.get_type() == Variant::NIL);
  162. cdbinds->params.push_back(value);
  163. cdbinds->notify_changed();
  164. }
  165. void ConnectDialog::_remove_bind() {
  166. String st = bind_editor->get_selected_path();
  167. if (st == "")
  168. return;
  169. int idx = st.get_slice("/", 1).to_int() - 1;
  170. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  171. cdbinds->params.remove(idx);
  172. cdbinds->notify_changed();
  173. }
  174. void ConnectDialog::set_dst_node(Node *p_node) {
  175. tree->set_selected(p_node);
  176. }
  177. void ConnectDialog::set_dst_method(const StringName &p_method) {
  178. dst_method->set_text(p_method);
  179. }
  180. void ConnectDialog::_bind_methods() {
  181. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  182. ClassDB::bind_method("_tree_node_selected", &ConnectDialog::_tree_node_selected);
  183. ClassDB::bind_method("_add_bind", &ConnectDialog::_add_bind);
  184. ClassDB::bind_method("_remove_bind", &ConnectDialog::_remove_bind);
  185. ADD_SIGNAL(MethodInfo("connected"));
  186. }
  187. ConnectDialog::ConnectDialog() {
  188. VBoxContainer *vbc = memnew(VBoxContainer);
  189. add_child(vbc);
  190. HBoxContainer *main_hb = memnew(HBoxContainer);
  191. vbc->add_child(main_hb);
  192. main_hb->set_v_size_flags(SIZE_EXPAND_FILL);
  193. VBoxContainer *vbc_left = memnew(VBoxContainer);
  194. main_hb->add_child(vbc_left);
  195. vbc_left->set_h_size_flags(SIZE_EXPAND_FILL);
  196. tree = memnew(SceneTreeEditor(false));
  197. tree->get_scene_tree()->connect("item_activated", this, "_ok");
  198. vbc_left->add_margin_child(TTR("Connect To Node:"), tree, true);
  199. VBoxContainer *vbc_right = memnew(VBoxContainer);
  200. main_hb->add_child(vbc_right);
  201. vbc_right->set_h_size_flags(SIZE_EXPAND_FILL);
  202. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  203. type_list = memnew(OptionButton);
  204. type_list->set_h_size_flags(SIZE_EXPAND_FILL);
  205. add_bind_hb->add_child(type_list);
  206. type_list->add_item("bool", Variant::BOOL);
  207. type_list->add_item("int", Variant::INT);
  208. type_list->add_item("real", Variant::REAL);
  209. type_list->add_item("string", Variant::STRING);
  210. //type_list->add_separator();
  211. type_list->add_item("Vector2", Variant::VECTOR2);
  212. type_list->add_item("Rect2", Variant::RECT2);
  213. type_list->add_item("Vector3", Variant::VECTOR3);
  214. type_list->add_item("Plane", Variant::PLANE);
  215. type_list->add_item("Quat", Variant::QUAT);
  216. type_list->add_item("AABB", Variant::AABB);
  217. type_list->add_item("Basis", Variant::BASIS);
  218. type_list->add_item("Transform", Variant::TRANSFORM);
  219. //type_list->add_separator();
  220. type_list->add_item("Color", Variant::COLOR);
  221. type_list->select(0);
  222. Button *add_bind = memnew(Button);
  223. add_bind->set_text(TTR("Add"));
  224. add_bind_hb->add_child(add_bind);
  225. add_bind->connect("pressed", this, "_add_bind");
  226. Button *del_bind = memnew(Button);
  227. del_bind->set_text(TTR("Remove"));
  228. add_bind_hb->add_child(del_bind);
  229. del_bind->connect("pressed", this, "_remove_bind");
  230. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  231. bind_editor = memnew(PropertyEditor);
  232. bind_editor->hide_top_label();
  233. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  234. dst_path = memnew(LineEdit);
  235. vbc->add_margin_child(TTR("Path to Node:"), dst_path);
  236. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  237. vbc->add_margin_child("Method In Node:", dstm_hb);
  238. dst_method = memnew(LineEdit);
  239. dst_method->set_h_size_flags(SIZE_EXPAND_FILL);
  240. dstm_hb->add_child(dst_method);
  241. /*dst_method_list = memnew( MenuButton );
  242. dst_method_list->set_text("List...");
  243. dst_method_list->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  244. dst_method_list->set_anchor( MARGIN_LEFT, ANCHOR_END );
  245. dst_method_list->set_anchor( MARGIN_TOP, ANCHOR_END );
  246. dst_method_list->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  247. dst_method_list->set_begin( Point2( 70,59) );
  248. dst_method_list->set_end( Point2( 15,39 ) );
  249. */
  250. //add_child(dst_method_list);
  251. make_callback = memnew(CheckButton);
  252. make_callback->set_toggle_mode(true);
  253. make_callback->set_pressed(EDITOR_DEF("text_editor/tools/create_signal_callbacks", true));
  254. make_callback->set_text(TTR("Make Function"));
  255. dstm_hb->add_child(make_callback);
  256. deferred = memnew(CheckButton);
  257. deferred->set_text(TTR("Deferred"));
  258. dstm_hb->add_child(deferred);
  259. oneshot = memnew(CheckButton);
  260. oneshot->set_text(TTR("Oneshot"));
  261. dstm_hb->add_child(oneshot);
  262. tree->connect("node_selected", this, "_tree_node_selected");
  263. set_as_toplevel(true);
  264. cdbinds = memnew(ConnectDialogBinds);
  265. error = memnew(ConfirmationDialog);
  266. add_child(error);
  267. error->get_ok()->set_text(TTR("Close"));
  268. get_ok()->set_text(TTR("Connect"));
  269. }
  270. ConnectDialog::~ConnectDialog() {
  271. memdelete(cdbinds);
  272. }
  273. void ConnectionsDock::_notification(int p_what) {
  274. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  275. update_tree();
  276. }
  277. }
  278. void ConnectionsDock::_close() {
  279. hide();
  280. }
  281. void ConnectionsDock::_connect() {
  282. TreeItem *it = tree->get_selected();
  283. ERR_FAIL_COND(!it);
  284. String signal = it->get_metadata(0).operator Dictionary()["name"];
  285. NodePath dst_path = connect_dialog->get_dst_path();
  286. Node *target = node->get_node(dst_path);
  287. ERR_FAIL_COND(!target);
  288. StringName dst_method = connect_dialog->get_dst_method();
  289. bool defer = connect_dialog->get_deferred();
  290. bool oshot = connect_dialog->get_oneshot();
  291. Vector<Variant> binds = connect_dialog->get_binds();
  292. PoolStringArray args = it->get_metadata(0).operator Dictionary()["args"];
  293. int flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0);
  294. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), signal, String(dst_method)));
  295. undo_redo->add_do_method(node, "connect", signal, target, dst_method, binds, flags);
  296. undo_redo->add_undo_method(node, "disconnect", signal, target, dst_method);
  297. undo_redo->add_do_method(this, "update_tree");
  298. undo_redo->add_undo_method(this, "update_tree");
  299. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  300. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  301. undo_redo->commit_action();
  302. if (connect_dialog->get_make_callback()) {
  303. print_line("request connect");
  304. editor->emit_signal("script_add_function_request", target, dst_method, args);
  305. hide();
  306. }
  307. update_tree();
  308. }
  309. void ConnectionsDock::_disconnect( TreeItem *item )
  310. {
  311. Connection c = item->get_metadata(0);
  312. ERR_FAIL_COND(c.source != node); //shouldn't happen but...bugcheck
  313. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method));
  314. undo_redo->add_do_method(node, "disconnect", c.signal, c.target, c.method);
  315. undo_redo->add_undo_method(node, "connect", c.signal, c.target, c.method, Vector<Variant>(), c.flags);
  316. undo_redo->add_do_method(this, "update_tree");
  317. undo_redo->add_undo_method(this, "update_tree");
  318. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  319. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  320. undo_redo->commit_action();
  321. c.source->disconnect(c.signal, c.target, c.method);
  322. update_tree();
  323. }
  324. void ConnectionsDock::_open_connection_dialog( TreeItem *item )
  325. {
  326. String signal = item->get_metadata(0).operator Dictionary()["name"];
  327. String signalname = signal;
  328. String midname = node->get_name();
  329. for (int i = 0; i < midname.length(); i++) {
  330. CharType c = midname[i];
  331. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
  332. //all good
  333. } else if (c == ' ') {
  334. c = '_';
  335. } else {
  336. midname.remove(i);
  337. i--;
  338. continue;
  339. }
  340. midname[i] = c;
  341. }
  342. connect_dialog->edit(node);
  343. connect_dialog->popup_centered_ratio();
  344. connect_dialog->set_title(TTR("Connecting Signal:") + " " + signalname);
  345. connect_dialog->set_dst_method("_on_" + midname + "_" + signal);
  346. connect_dialog->set_dst_node(node->get_owner() ? node->get_owner() : node);
  347. }
  348. void ConnectionsDock::_connect_pressed() {
  349. TreeItem *item = tree->get_selected();
  350. if (!item) {
  351. connect_button->set_disabled(true);
  352. return;
  353. }
  354. if (_is_item_signal( item )) {
  355. _open_connection_dialog( item );
  356. } else {
  357. _disconnect( item );
  358. }
  359. }
  360. struct _ConnectionsDockMethodInfoSort {
  361. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  362. return a.name < b.name;
  363. }
  364. };
  365. void ConnectionsDock::update_tree() {
  366. tree->clear();
  367. if (!node)
  368. return;
  369. TreeItem *root = tree->create_item();
  370. List<MethodInfo> node_signals;
  371. node->get_signal_list(&node_signals);
  372. //node_signals.sort_custom<_ConnectionsDockMethodInfoSort>();
  373. bool did_script = false;
  374. StringName base = node->get_class();
  375. while (base) {
  376. List<MethodInfo> node_signals;
  377. Ref<Texture> icon;
  378. String name;
  379. if (!did_script) {
  380. Ref<Script> scr = node->get_script();
  381. if (scr.is_valid()) {
  382. scr->get_script_signal_list(&node_signals);
  383. if (scr->get_path().is_resource_file())
  384. name = scr->get_path().get_file();
  385. else
  386. name = scr->get_class();
  387. if (has_icon(scr->get_class(), "EditorIcons")) {
  388. icon = get_icon(scr->get_class(), "EditorIcons");
  389. }
  390. }
  391. } else {
  392. ClassDB::get_signal_list(base, &node_signals, true);
  393. if (has_icon(base, "EditorIcons")) {
  394. icon = get_icon(base, "EditorIcons");
  395. }
  396. name = base;
  397. }
  398. TreeItem *pitem = NULL;
  399. if (node_signals.size()) {
  400. pitem = tree->create_item(root);
  401. pitem->set_text(0, name);
  402. pitem->set_icon(0, icon);
  403. pitem->set_selectable(0, false);
  404. pitem->set_editable(0, false);
  405. pitem->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  406. node_signals.sort();
  407. }
  408. for (List<MethodInfo>::Element *E = node_signals.front(); E; E = E->next()) {
  409. MethodInfo &mi = E->get();
  410. String signaldesc;
  411. signaldesc = mi.name + "(";
  412. PoolStringArray argnames;
  413. if (mi.arguments.size()) {
  414. signaldesc += " ";
  415. for (int i = 0; i < mi.arguments.size(); i++) {
  416. PropertyInfo &pi = mi.arguments[i];
  417. if (i > 0)
  418. signaldesc += ", ";
  419. String tname = "var";
  420. if (pi.type != Variant::NIL) {
  421. tname = Variant::get_type_name(pi.type);
  422. }
  423. signaldesc += tname + " " + (pi.name == "" ? String("arg " + itos(i)) : pi.name);
  424. argnames.push_back(pi.name + ":" + tname);
  425. }
  426. signaldesc += " ";
  427. }
  428. signaldesc += ")";
  429. TreeItem *item = tree->create_item(pitem);
  430. item->set_text(0, signaldesc);
  431. Dictionary sinfo;
  432. sinfo["name"] = mi.name;
  433. sinfo["args"] = argnames;
  434. item->set_metadata(0, sinfo);
  435. item->set_icon(0, get_icon("Signal", "EditorIcons"));
  436. List<Object::Connection> connections;
  437. node->get_signal_connection_list(mi.name, &connections);
  438. for (List<Object::Connection>::Element *F = connections.front(); F; F = F->next()) {
  439. Object::Connection &c = F->get();
  440. if (!(c.flags & CONNECT_PERSIST))
  441. continue;
  442. Node *target = Object::cast_to<Node>(c.target);
  443. if (!target)
  444. continue;
  445. String path = String(node->get_path_to(target)) + " :: " + c.method + "()";
  446. if (c.flags & CONNECT_DEFERRED)
  447. path += " (deferred)";
  448. if (c.flags & CONNECT_ONESHOT)
  449. path += " (oneshot)";
  450. if (c.binds.size()) {
  451. path += " binds( ";
  452. for (int i = 0; i < c.binds.size(); i++) {
  453. if (i > 0)
  454. path += ", ";
  455. path += c.binds[i].operator String();
  456. }
  457. path += " )";
  458. }
  459. TreeItem *item2 = tree->create_item(item);
  460. item2->set_text(0, path);
  461. item2->set_metadata(0, c);
  462. item2->set_icon(0, get_icon("Slot", "EditorIcons"));
  463. }
  464. }
  465. if (!did_script) {
  466. did_script = true;
  467. } else {
  468. base = ClassDB::get_parent_class(base);
  469. }
  470. }
  471. connect_button->set_text(TTR("Connect"));
  472. connect_button->set_disabled(true);
  473. }
  474. void ConnectionsDock::set_node(Node *p_node) {
  475. node = p_node;
  476. update_tree();
  477. }
  478. void ConnectionsDock::_something_selected() {
  479. TreeItem *item = tree->get_selected();
  480. if (!item) {
  481. //no idea how this happened, but disable
  482. connect_button->set_text(TTR("Connect..."));
  483. connect_button->set_disabled(true);
  484. } else if (_is_item_signal( item )) {
  485. connect_button->set_text(TTR("Connect..."));
  486. connect_button->set_disabled(false);
  487. } else {
  488. connect_button->set_text(TTR("Disconnect"));
  489. connect_button->set_disabled(false);
  490. }
  491. }
  492. void ConnectionsDock::_something_activated() {
  493. TreeItem *item = tree->get_selected();
  494. if (!item)
  495. return;
  496. if (_is_item_signal( item )) {
  497. _open_connection_dialog( item );
  498. } else {
  499. //Go to method within script editor, if avaiable.
  500. Connection c = item->get_metadata(0);
  501. ERR_FAIL_COND(c.source != node); //shouldn't happen but...bugcheck
  502. if (!c.target)
  503. return;
  504. Ref<Script> script = c.target->get_script();
  505. if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, c.method)) {
  506. editor->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  507. }
  508. }
  509. }
  510. bool ConnectionsDock::_is_item_signal( TreeItem *item ) {
  511. return (item->get_parent() == tree->get_root() || item->get_parent()->get_parent() == tree->get_root());
  512. }
  513. void ConnectionsDock::_handle_signal_option( int option ) {
  514. TreeItem *item = tree->get_selected();
  515. switch( option )
  516. {
  517. case SignalMenuOption::CONNECT:
  518. {
  519. _open_connection_dialog(item);
  520. } break;
  521. }
  522. }
  523. void ConnectionsDock::_handle_slot_option( int option ) {
  524. TreeItem *item = tree->get_selected();
  525. switch( option )
  526. {
  527. case SlotMenuOption::EDIT:
  528. {
  529. //TODO: add edit functionality
  530. } break;
  531. case SlotMenuOption::DISCONNECT:
  532. {
  533. _disconnect(item);
  534. } break;
  535. }
  536. }
  537. void ConnectionsDock::_rmb_pressed( Vector2 position ) {
  538. TreeItem *item = tree->get_selected();
  539. if( !item )
  540. return;
  541. Vector2 global_position = tree->get_global_position() + position;
  542. if( _is_item_signal(item) )
  543. {
  544. signal_menu->set_position( global_position );
  545. signal_menu->popup();
  546. }
  547. else
  548. {
  549. slot_menu->set_position( global_position );
  550. slot_menu->popup();
  551. }
  552. }
  553. void ConnectionsDock::_bind_methods() {
  554. ClassDB::bind_method("_connect", &ConnectionsDock::_connect);
  555. ClassDB::bind_method("_something_selected", &ConnectionsDock::_something_selected);
  556. ClassDB::bind_method("_something_activated", &ConnectionsDock::_something_activated);
  557. ClassDB::bind_method("_handle_signal_option", &ConnectionsDock::_handle_signal_option);
  558. ClassDB::bind_method("_handle_slot_option", &ConnectionsDock::_handle_slot_option);
  559. ClassDB::bind_method("_rmb_pressed", &ConnectionsDock::_rmb_pressed);
  560. ClassDB::bind_method("_close", &ConnectionsDock::_close);
  561. ClassDB::bind_method("_connect_pressed", &ConnectionsDock::_connect_pressed);
  562. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  563. }
  564. ConnectionsDock::ConnectionsDock(EditorNode *p_editor) {
  565. editor = p_editor;
  566. set_name(TTR("Signals"));
  567. VBoxContainer *vbc = this;
  568. tree = memnew(Tree);
  569. tree->set_columns(1);
  570. tree->set_select_mode(Tree::SELECT_ROW);
  571. tree->set_hide_root(true);
  572. vbc->add_child(tree);
  573. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  574. tree->set_allow_rmb_select(true);
  575. connect_button = memnew(Button);
  576. connect_button->set_text(TTR("Connect"));
  577. HBoxContainer *hb = memnew(HBoxContainer);
  578. vbc->add_child(hb);
  579. hb->add_spacer();
  580. hb->add_child(connect_button);
  581. connect_button->connect("pressed", this, "_connect_pressed");
  582. //add_child(tree);
  583. connect_dialog = memnew(ConnectDialog);
  584. connect_dialog->set_as_toplevel(true);
  585. add_child(connect_dialog);
  586. remove_confirm = memnew(ConfirmationDialog);
  587. remove_confirm->set_as_toplevel(true);
  588. add_child(remove_confirm);
  589. signal_menu = memnew(PopupMenu);
  590. add_child(signal_menu);
  591. signal_menu->connect( "id_pressed", this, "_handle_signal_option" );
  592. signal_menu->add_item(TTR("Connect"), SignalMenuOption::CONNECT );
  593. slot_menu = memnew(PopupMenu);
  594. add_child(slot_menu);
  595. slot_menu->connect( "id_pressed", this, "_handle_slot_option" );
  596. slot_menu->add_item(TTR("Edit"), SlotMenuOption::EDIT);
  597. slot_menu->add_item(TTR("Disconnect"), SlotMenuOption::DISCONNECT);
  598. /*
  599. node_only->set_anchor( MARGIN_TOP, ANCHOR_END );
  600. node_only->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  601. node_only->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  602. node_only->set_begin( Point2( 20,51) );
  603. node_only->set_end( Point2( 10,44) );
  604. */
  605. remove_confirm->connect("confirmed", this, "_remove_confirm");
  606. connect_dialog->connect("connected", this, "_connect");
  607. tree->connect("item_selected", this, "_something_selected");
  608. tree->connect("item_activated", this, "_something_activated");
  609. tree->connect("item_rmb_selected", this, "_rmb_pressed" );
  610. add_constant_override("separation", 3 * EDSCALE);
  611. }
  612. ConnectionsDock::~ConnectionsDock() {
  613. }