connections_dialog.cpp 29 KB

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