connections_dialog.cpp 24 KB

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