connections_dialog.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 "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: { ERR_FAIL(); } break;
  189. }
  190. ERR_FAIL_COND(value.get_type() == Variant::NIL);
  191. cdbinds->params.push_back(value);
  192. cdbinds->notify_changed();
  193. }
  194. void ConnectDialog::_remove_bind() {
  195. String st = bind_editor->get_selected_path();
  196. if (st == "")
  197. return;
  198. int idx = st.get_slice("/", 1).to_int() - 1;
  199. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  200. cdbinds->params.remove(idx);
  201. cdbinds->notify_changed();
  202. }
  203. void ConnectDialog::set_dst_node(Node *p_node) {
  204. tree->set_selected(p_node);
  205. }
  206. void ConnectDialog::set_dst_method(const StringName &p_method) {
  207. dst_method->set_text(p_method);
  208. }
  209. void ConnectDialog::_bind_methods() {
  210. //ObjectTypeDB::bind_method("_ok",&ConnectDialog::_ok_pressed);
  211. ObjectTypeDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  212. //ObjectTypeDB::bind_method("_dst_method_list_selected",&ConnectDialog::_dst_method_list_selected);
  213. ObjectTypeDB::bind_method("_tree_node_selected", &ConnectDialog::_tree_node_selected);
  214. ObjectTypeDB::bind_method("_add_bind", &ConnectDialog::_add_bind);
  215. ObjectTypeDB::bind_method("_remove_bind", &ConnectDialog::_remove_bind);
  216. ADD_SIGNAL(MethodInfo("connected"));
  217. }
  218. ConnectDialog::ConnectDialog() {
  219. VBoxContainer *vbc = memnew(VBoxContainer);
  220. add_child(vbc);
  221. set_child_rect(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. vbc_left->add_margin_child(TTR("Connect To Node:"), tree, true);
  231. VBoxContainer *vbc_right = memnew(VBoxContainer);
  232. main_hb->add_child(vbc_right);
  233. vbc_right->set_h_size_flags(SIZE_EXPAND_FILL);
  234. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  235. type_list = memnew(OptionButton);
  236. type_list->set_h_size_flags(SIZE_EXPAND_FILL);
  237. add_bind_hb->add_child(type_list);
  238. type_list->add_item("bool", Variant::BOOL);
  239. type_list->add_item("int", Variant::INT);
  240. type_list->add_item("real", Variant::REAL);
  241. type_list->add_item("string", Variant::STRING);
  242. //type_list->add_separator();
  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("Matrix3", Variant::MATRIX3);
  250. type_list->add_item("Transform", Variant::TRANSFORM);
  251. //type_list->add_separator();
  252. type_list->add_item("Color", Variant::COLOR);
  253. type_list->add_item("Image", Variant::IMAGE);
  254. type_list->select(0);
  255. Button *add_bind = memnew(Button);
  256. add_bind->set_text(TTR("Add"));
  257. add_bind_hb->add_child(add_bind);
  258. add_bind->connect("pressed", this, "_add_bind");
  259. Button *del_bind = memnew(Button);
  260. del_bind->set_text(TTR("Remove"));
  261. add_bind_hb->add_child(del_bind);
  262. del_bind->connect("pressed", this, "_remove_bind");
  263. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  264. bind_editor = memnew(PropertyEditor);
  265. bind_editor->hide_top_label();
  266. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  267. dst_path = memnew(LineEdit);
  268. vbc->add_margin_child(TTR("Path to Node:"), dst_path);
  269. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  270. vbc->add_margin_child("Method In Node:", dstm_hb);
  271. dst_method = memnew(LineEdit);
  272. dst_method->set_h_size_flags(SIZE_EXPAND_FILL);
  273. dstm_hb->add_child(dst_method);
  274. /*dst_method_list = memnew( MenuButton );
  275. dst_method_list->set_text("List..");
  276. dst_method_list->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  277. dst_method_list->set_anchor( MARGIN_LEFT, ANCHOR_END );
  278. dst_method_list->set_anchor( MARGIN_TOP, ANCHOR_END );
  279. dst_method_list->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  280. dst_method_list->set_begin( Point2( 70,59) );
  281. dst_method_list->set_end( Point2( 15,39 ) );
  282. */
  283. //add_child(dst_method_list);
  284. make_callback = memnew(CheckButton);
  285. make_callback->set_toggle_mode(true);
  286. make_callback->set_pressed(EDITOR_DEF("text_editor/create_signal_callbacks", true));
  287. make_callback->set_text(TTR("Make Function"));
  288. dstm_hb->add_child(make_callback);
  289. deferred = memnew(CheckButton);
  290. deferred->set_text(TTR("Deferred"));
  291. dstm_hb->add_child(deferred);
  292. oneshot = memnew(CheckButton);
  293. oneshot->set_text(TTR("Oneshot"));
  294. dstm_hb->add_child(oneshot);
  295. /*
  296. realtime = memnew( CheckButton );
  297. realtime->set_anchor( MARGIN_TOP, ANCHOR_END );
  298. realtime->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  299. realtime->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  300. realtime->set_begin( Point2( 120, button_margin-10 ) );
  301. realtime->set_end( Point2( 80, margin ) );
  302. realtime->set_text("Realtime");
  303. add_child(realtime);
  304. */
  305. // dst_method_list->get_popup()->connect("item_pressed", this,"_dst_method_list_selected");
  306. tree->connect("node_selected", this, "_tree_node_selected");
  307. set_as_toplevel(true);
  308. cdbinds = memnew(ConnectDialogBinds);
  309. error = memnew(ConfirmationDialog);
  310. add_child(error);
  311. error->get_ok()->set_text(TTR("Close"));
  312. get_ok()->set_text(TTR("Connect"));
  313. // error->get_cancel()->set_text("Close");
  314. }
  315. ConnectDialog::~ConnectDialog() {
  316. memdelete(cdbinds);
  317. }
  318. void ConnectionsDock::_notification(int p_what) {
  319. if (p_what == NOTIFICATION_DRAW) {
  320. //RID ci = get_canvas_item();
  321. //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
  322. }
  323. }
  324. void ConnectionsDock::_close() {
  325. hide();
  326. }
  327. void ConnectionsDock::_connect() {
  328. TreeItem *it = tree->get_selected();
  329. ERR_FAIL_COND(!it);
  330. String signal = it->get_metadata(0).operator Dictionary()["name"];
  331. NodePath dst_path = connect_dialog->get_dst_path();
  332. Node *target = node->get_node(dst_path);
  333. ERR_FAIL_COND(!target);
  334. StringName dst_method = connect_dialog->get_dst_method();
  335. bool defer = connect_dialog->get_deferred();
  336. bool oshot = connect_dialog->get_oneshot();
  337. Vector<Variant> binds = connect_dialog->get_binds();
  338. StringArray args = it->get_metadata(0).operator Dictionary()["args"];
  339. int flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0);
  340. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), signal, String(dst_method)));
  341. undo_redo->add_do_method(node, "connect", signal, target, dst_method, binds, flags);
  342. undo_redo->add_undo_method(node, "disconnect", signal, target, dst_method);
  343. undo_redo->add_do_method(this, "update_tree");
  344. undo_redo->add_undo_method(this, "update_tree");
  345. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  346. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  347. undo_redo->commit_action();
  348. if (connect_dialog->get_make_callback()) {
  349. print_line("request connect");
  350. editor->emit_signal("script_add_function_request", target, dst_method, args);
  351. hide();
  352. }
  353. update_tree();
  354. }
  355. void ConnectionsDock::_connect_pressed() {
  356. TreeItem *item = tree->get_selected();
  357. if (!item) {
  358. //no idea how this happened, but disable
  359. connect_button->set_disabled(true);
  360. return;
  361. }
  362. if (item->get_parent() == tree->get_root() || item->get_parent()->get_parent() == tree->get_root()) {
  363. //a signal - connect
  364. String signal = item->get_metadata(0).operator Dictionary()["name"];
  365. String signalname = signal;
  366. String midname = node->get_name();
  367. for (int i = 0; i < midname.length(); i++) {
  368. CharType c = midname[i];
  369. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
  370. //all good
  371. } else if (c == ' ') {
  372. c = '_';
  373. } else {
  374. midname.remove(i);
  375. i--;
  376. continue;
  377. }
  378. midname[i] = c;
  379. }
  380. connect_dialog->edit(node);
  381. connect_dialog->popup_centered_ratio();
  382. connect_dialog->set_title(TTR("Connecting Signal:") + " " + signalname);
  383. connect_dialog->set_dst_method("_on_" + midname + "_" + signal);
  384. connect_dialog->set_dst_node(node->get_owner() ? node->get_owner() : node);
  385. } else {
  386. //a slot- disconnect
  387. Connection c = item->get_metadata(0);
  388. ERR_FAIL_COND(c.source != node); //shouldn't happen but...bugcheck
  389. undo_redo->create_action(TTR("Create Subscription"));
  390. undo_redo->add_do_method(node, "disconnect", c.signal, c.target, c.method);
  391. undo_redo->add_undo_method(node, "connect", c.signal, c.target, c.method, Vector<Variant>(), c.flags);
  392. undo_redo->add_do_method(this, "update_tree");
  393. undo_redo->add_undo_method(this, "update_tree");
  394. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  395. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  396. undo_redo->commit_action();
  397. c.source->disconnect(c.signal, c.target, c.method);
  398. update_tree();
  399. }
  400. }
  401. /*
  402. void ConnectionsDock::_remove() {
  403. if (!tree->get_selected())
  404. return;
  405. TreeItem *selected=tree->get_selected();
  406. if (!selected)
  407. return;
  408. Dictionary meta=selected->get_metadata(0);
  409. remove_confirm->set_text(String()+"Remove Connection \""+meta["from_event"].operator String()+"\" ?");
  410. remove_confirm->popup_centered(Size2(340,80));
  411. }
  412. */
  413. /*
  414. void ConnectionsDock::_remove_confirm() {
  415. if (!tree->get_selected())
  416. return;
  417. TreeItem *selected=tree->get_selected();
  418. if (!selected)
  419. return;
  420. Dictionary meta=selected->get_metadata(0);
  421. undo_redo->create_action("Remove Subscription");
  422. undo_redo->add_do_method(node,"unsubscribe_path_event",meta["from_event"].operator String(),meta["from_path"].operator NodePath(),meta["to_method"].operator String());
  423. 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);
  424. undo_redo->add_do_method(this,"update_tree");
  425. undo_redo->add_undo_method(this,"update_tree");
  426. undo_redo->commit_action();
  427. }
  428. */
  429. struct _ConnectionsDockMethodInfoSort {
  430. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  431. return a.name < b.name;
  432. }
  433. };
  434. void ConnectionsDock::update_tree() {
  435. tree->clear();
  436. if (!node)
  437. return;
  438. TreeItem *root = tree->create_item();
  439. List<MethodInfo> node_signals;
  440. node->get_signal_list(&node_signals);
  441. //node_signals.sort_custom<_ConnectionsDockMethodInfoSort>();
  442. bool did_script = false;
  443. StringName base = node->get_type();
  444. while (base) {
  445. List<MethodInfo> node_signals;
  446. Ref<Texture> icon;
  447. String name;
  448. if (!did_script) {
  449. Ref<Script> scr = node->get_script();
  450. if (scr.is_valid()) {
  451. scr->get_script_signal_list(&node_signals);
  452. if (scr->get_path().is_resource_file())
  453. name = scr->get_path().get_file();
  454. else
  455. name = scr->get_type();
  456. if (has_icon(scr->get_type(), "EditorIcons")) {
  457. icon = get_icon(scr->get_type(), "EditorIcons");
  458. }
  459. }
  460. } else {
  461. ObjectTypeDB::get_signal_list(base, &node_signals, true);
  462. if (has_icon(base, "EditorIcons")) {
  463. icon = get_icon(base, "EditorIcons");
  464. }
  465. name = base;
  466. }
  467. TreeItem *pitem = NULL;
  468. if (node_signals.size()) {
  469. pitem = tree->create_item(root);
  470. pitem->set_text(0, name);
  471. pitem->set_icon(0, icon);
  472. pitem->set_selectable(0, false);
  473. pitem->set_editable(0, false);
  474. pitem->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  475. node_signals.sort();
  476. }
  477. for (List<MethodInfo>::Element *E = node_signals.front(); E; E = E->next()) {
  478. MethodInfo &mi = E->get();
  479. String signaldesc;
  480. signaldesc = mi.name + "(";
  481. StringArray argnames;
  482. if (mi.arguments.size()) {
  483. signaldesc += " ";
  484. for (int i = 0; i < mi.arguments.size(); i++) {
  485. PropertyInfo &pi = mi.arguments[i];
  486. if (i > 0)
  487. signaldesc += ", ";
  488. String tname = "var";
  489. if (pi.type != Variant::NIL) {
  490. tname = Variant::get_type_name(pi.type);
  491. }
  492. signaldesc += tname + " " + (pi.name == "" ? String("arg " + itos(i)) : pi.name);
  493. argnames.push_back(pi.name);
  494. }
  495. signaldesc += " ";
  496. }
  497. signaldesc += ")";
  498. TreeItem *item = tree->create_item(pitem);
  499. item->set_text(0, signaldesc);
  500. Dictionary sinfo;
  501. sinfo["name"] = mi.name;
  502. sinfo["args"] = argnames;
  503. item->set_metadata(0, sinfo);
  504. item->set_icon(0, get_icon("Signal", "EditorIcons"));
  505. List<Object::Connection> connections;
  506. node->get_signal_connection_list(mi.name, &connections);
  507. for (List<Object::Connection>::Element *F = connections.front(); F; F = F->next()) {
  508. Object::Connection &c = F->get();
  509. if (!(c.flags & CONNECT_PERSIST))
  510. continue;
  511. Node *target = c.target->cast_to<Node>();
  512. if (!target)
  513. continue;
  514. String path = String(node->get_path_to(target)) + " :: " + c.method + "()";
  515. if (c.flags & CONNECT_DEFERRED)
  516. path += " (deferred)";
  517. if (c.flags & CONNECT_ONESHOT)
  518. path += " (oneshot)";
  519. if (c.binds.size()) {
  520. path += " binds( ";
  521. for (int i = 0; i < c.binds.size(); i++) {
  522. if (i > 0)
  523. path += ", ";
  524. path += c.binds[i].operator String();
  525. }
  526. path += " )";
  527. }
  528. TreeItem *item2 = tree->create_item(item);
  529. item2->set_text(0, path);
  530. item2->set_metadata(0, c);
  531. item2->set_icon(0, get_icon("Slot", "EditorIcons"));
  532. }
  533. }
  534. if (!did_script) {
  535. did_script = true;
  536. } else {
  537. base = ObjectTypeDB::type_inherits_from(base);
  538. }
  539. }
  540. connect_button->set_text(TTR("Connect"));
  541. connect_button->set_disabled(true);
  542. }
  543. void ConnectionsDock::set_node(Node *p_node) {
  544. node = p_node;
  545. update_tree();
  546. }
  547. void ConnectionsDock::_something_selected() {
  548. TreeItem *item = tree->get_selected();
  549. if (!item) {
  550. //no idea how this happened, but disable
  551. connect_button->set_text(TTR("Connect.."));
  552. connect_button->set_disabled(true);
  553. } else if (item->get_parent() == tree->get_root() || item->get_parent()->get_parent() == tree->get_root()) {
  554. //a signal - connect
  555. connect_button->set_text(TTR("Connect.."));
  556. connect_button->set_disabled(false);
  557. } else {
  558. //a slot- disconnect
  559. connect_button->set_text(TTR("Disconnect"));
  560. connect_button->set_disabled(false);
  561. }
  562. }
  563. void ConnectionsDock::_something_activated() {
  564. TreeItem *item = tree->get_selected();
  565. if (!item)
  566. return;
  567. if (item->get_parent() == tree->get_root() || item->get_parent()->get_parent() == tree->get_root()) {
  568. // a signal - connect
  569. String signal = item->get_metadata(0).operator Dictionary()["name"];
  570. String midname = node->get_name();
  571. for (int i = 0; i < midname.length(); i++) {
  572. CharType c = midname[i];
  573. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
  574. //all good
  575. } else if (c == ' ') {
  576. c = '_';
  577. } else {
  578. midname.remove(i);
  579. i--;
  580. continue;
  581. }
  582. midname[i] = c;
  583. }
  584. connect_dialog->edit(node);
  585. connect_dialog->popup_centered_ratio();
  586. connect_dialog->set_dst_method("_on_" + midname + "_" + signal);
  587. connect_dialog->set_dst_node(node->get_owner() ? node->get_owner() : node);
  588. } else {
  589. // a slot - go to target method
  590. Connection c = item->get_metadata(0);
  591. ERR_FAIL_COND(c.source != node); //shouldn't happen but...bugcheck
  592. if (!c.target)
  593. return;
  594. Ref<Script> script = c.target->get_script();
  595. if (script.is_valid() && ScriptEditor::get_singleton()->script_go_to_method(script, c.method)) {
  596. editor->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  597. }
  598. }
  599. }
  600. void ConnectionsDock::_bind_methods() {
  601. ObjectTypeDB::bind_method("_connect", &ConnectionsDock::_connect);
  602. ObjectTypeDB::bind_method("_something_selected", &ConnectionsDock::_something_selected);
  603. ObjectTypeDB::bind_method("_something_activated", &ConnectionsDock::_something_activated);
  604. ObjectTypeDB::bind_method("_close", &ConnectionsDock::_close);
  605. ObjectTypeDB::bind_method("_connect_pressed", &ConnectionsDock::_connect_pressed);
  606. ObjectTypeDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  607. }
  608. ConnectionsDock::ConnectionsDock(EditorNode *p_editor) {
  609. editor = p_editor;
  610. set_name(TTR("Signals"));
  611. VBoxContainer *vbc = this;
  612. tree = memnew(Tree);
  613. tree->set_columns(1);
  614. tree->set_select_mode(Tree::SELECT_ROW);
  615. tree->set_hide_root(true);
  616. vbc->add_child(tree);
  617. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  618. connect_button = memnew(Button);
  619. connect_button->set_text("Connect");
  620. HBoxContainer *hb = memnew(HBoxContainer);
  621. vbc->add_child(hb);
  622. hb->add_spacer();
  623. hb->add_child(connect_button);
  624. connect_button->connect("pressed", this, "_connect_pressed");
  625. // add_child(tree);
  626. connect_dialog = memnew(ConnectDialog);
  627. connect_dialog->set_as_toplevel(true);
  628. add_child(connect_dialog);
  629. remove_confirm = memnew(ConfirmationDialog);
  630. remove_confirm->set_as_toplevel(true);
  631. add_child(remove_confirm);
  632. /*
  633. node_only->set_anchor( MARGIN_TOP, ANCHOR_END );
  634. node_only->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  635. node_only->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  636. node_only->set_begin( Point2( 20,51) );
  637. node_only->set_end( Point2( 10,44) );
  638. */
  639. remove_confirm->connect("confirmed", this, "_remove_confirm");
  640. connect_dialog->connect("connected", this, "_connect");
  641. tree->connect("item_selected", this, "_something_selected");
  642. tree->connect("item_activated", this, "_something_activated");
  643. add_constant_override("separation", 3 * EDSCALE);
  644. }
  645. ConnectionsDock::~ConnectionsDock() {
  646. }