connections_dialog.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. /*************************************************************************/
  2. /* connections_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/string/print_string.h"
  32. #include "editor/doc_tools.h"
  33. #include "editor_node.h"
  34. #include "editor_scale.h"
  35. #include "editor_settings.h"
  36. #include "plugins/script_editor_plugin.h"
  37. #include "scene/gui/label.h"
  38. #include "scene/gui/popup_menu.h"
  39. static Node *_find_first_script(Node *p_root, Node *p_node) {
  40. if (p_node != p_root && p_node->get_owner() != p_root) {
  41. return nullptr;
  42. }
  43. if (!p_node->get_script().is_null()) {
  44. return p_node;
  45. }
  46. for (int i = 0; i < p_node->get_child_count(); i++) {
  47. Node *ret = _find_first_script(p_root, p_node->get_child(i));
  48. if (ret) {
  49. return ret;
  50. }
  51. }
  52. return nullptr;
  53. }
  54. class ConnectDialogBinds : public Object {
  55. GDCLASS(ConnectDialogBinds, Object);
  56. public:
  57. Vector<Variant> params;
  58. bool _set(const StringName &p_name, const Variant &p_value) {
  59. String name = p_name;
  60. if (name.begins_with("bind/")) {
  61. int which = name.get_slice("/", 1).to_int() - 1;
  62. ERR_FAIL_INDEX_V(which, params.size(), false);
  63. params.write[which] = p_value;
  64. } else {
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool _get(const StringName &p_name, Variant &r_ret) const {
  70. String name = p_name;
  71. if (name.begins_with("bind/")) {
  72. int which = name.get_slice("/", 1).to_int() - 1;
  73. ERR_FAIL_INDEX_V(which, params.size(), false);
  74. r_ret = params[which];
  75. } else {
  76. return false;
  77. }
  78. return true;
  79. }
  80. void _get_property_list(List<PropertyInfo> *p_list) const {
  81. for (int i = 0; i < params.size(); i++) {
  82. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/" + itos(i + 1)));
  83. }
  84. }
  85. void notify_changed() {
  86. notify_property_list_changed();
  87. }
  88. ConnectDialogBinds() {
  89. }
  90. };
  91. /*
  92. * Signal automatically called by parent dialog.
  93. */
  94. void ConnectDialog::ok_pressed() {
  95. String method_name = dst_method->get_text();
  96. if (method_name == "") {
  97. error->set_text(TTR("Method in target node must be specified."));
  98. error->popup_centered();
  99. return;
  100. }
  101. if (!method_name.strip_edges().is_valid_identifier()) {
  102. error->set_text(TTR("Method name must be a valid identifier."));
  103. error->popup_centered();
  104. return;
  105. }
  106. Node *target = tree->get_selected();
  107. if (!target) {
  108. return; // Nothing selected in the tree, not an error.
  109. }
  110. if (target->get_script().is_null()) {
  111. if (!target->has_method(method_name)) {
  112. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  113. error->popup_centered();
  114. return;
  115. }
  116. }
  117. emit_signal("connected");
  118. hide();
  119. }
  120. void ConnectDialog::_cancel_pressed() {
  121. hide();
  122. }
  123. void ConnectDialog::_item_activated() {
  124. _ok_pressed(); // From AcceptDialog.
  125. }
  126. void ConnectDialog::_text_entered(const String &p_text) {
  127. _ok_pressed(); // From AcceptDialog.
  128. }
  129. /*
  130. * Called each time a target node is selected within the target node tree.
  131. */
  132. void ConnectDialog::_tree_node_selected() {
  133. Node *current = tree->get_selected();
  134. if (!current) {
  135. return;
  136. }
  137. dst_path = source->get_path_to(current);
  138. _update_ok_enabled();
  139. }
  140. /*
  141. * Adds a new parameter bind to connection.
  142. */
  143. void ConnectDialog::_add_bind() {
  144. if (cdbinds->params.size() >= VARIANT_ARG_MAX) {
  145. return;
  146. }
  147. Variant::Type vt = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  148. Variant value;
  149. switch (vt) {
  150. case Variant::BOOL:
  151. value = false;
  152. break;
  153. case Variant::INT:
  154. value = 0;
  155. break;
  156. case Variant::FLOAT:
  157. value = 0.0;
  158. break;
  159. case Variant::STRING:
  160. value = "";
  161. break;
  162. case Variant::STRING_NAME:
  163. value = "";
  164. break;
  165. case Variant::VECTOR2:
  166. value = Vector2();
  167. break;
  168. case Variant::RECT2:
  169. value = Rect2();
  170. break;
  171. case Variant::VECTOR3:
  172. value = Vector3();
  173. break;
  174. case Variant::PLANE:
  175. value = Plane();
  176. break;
  177. case Variant::QUAT:
  178. value = Quat();
  179. break;
  180. case Variant::AABB:
  181. value = AABB();
  182. break;
  183. case Variant::BASIS:
  184. value = Basis();
  185. break;
  186. case Variant::TRANSFORM:
  187. value = Transform();
  188. break;
  189. case Variant::COLOR:
  190. value = Color();
  191. break;
  192. default: {
  193. ERR_FAIL();
  194. } break;
  195. }
  196. ERR_FAIL_COND(value.get_type() == Variant::NIL);
  197. cdbinds->params.push_back(value);
  198. cdbinds->notify_changed();
  199. }
  200. /*
  201. * Remove parameter bind from connection.
  202. */
  203. void ConnectDialog::_remove_bind() {
  204. String st = bind_editor->get_selected_path();
  205. if (st == "") {
  206. return;
  207. }
  208. int idx = st.get_slice("/", 1).to_int() - 1;
  209. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  210. cdbinds->params.remove(idx);
  211. cdbinds->notify_changed();
  212. }
  213. /*
  214. * Enables or disables the connect button. The connect button is enabled if a
  215. * node is selected and valid in the selected mode.
  216. */
  217. void ConnectDialog::_update_ok_enabled() {
  218. Node *target = tree->get_selected();
  219. if (target == nullptr) {
  220. get_ok_button()->set_disabled(true);
  221. return;
  222. }
  223. if (!advanced->is_pressed() && target->get_script().is_null()) {
  224. get_ok_button()->set_disabled(true);
  225. return;
  226. }
  227. get_ok_button()->set_disabled(false);
  228. }
  229. void ConnectDialog::_notification(int p_what) {
  230. if (p_what == NOTIFICATION_ENTER_TREE) {
  231. bind_editor->edit(cdbinds);
  232. }
  233. }
  234. void ConnectDialog::_bind_methods() {
  235. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  236. ClassDB::bind_method("_update_ok_enabled", &ConnectDialog::_update_ok_enabled);
  237. ADD_SIGNAL(MethodInfo("connected"));
  238. }
  239. Node *ConnectDialog::get_source() const {
  240. return source;
  241. }
  242. StringName ConnectDialog::get_signal_name() const {
  243. return signal;
  244. }
  245. NodePath ConnectDialog::get_dst_path() const {
  246. return dst_path;
  247. }
  248. void ConnectDialog::set_dst_node(Node *p_node) {
  249. tree->set_selected(p_node);
  250. }
  251. StringName ConnectDialog::get_dst_method_name() const {
  252. String txt = dst_method->get_text();
  253. if (txt.find("(") != -1) {
  254. txt = txt.left(txt.find("(")).strip_edges();
  255. }
  256. return txt;
  257. }
  258. void ConnectDialog::set_dst_method(const StringName &p_method) {
  259. dst_method->set_text(p_method);
  260. }
  261. Vector<Variant> ConnectDialog::get_binds() const {
  262. return cdbinds->params;
  263. }
  264. bool ConnectDialog::get_deferred() const {
  265. return deferred->is_pressed();
  266. }
  267. bool ConnectDialog::get_oneshot() const {
  268. return oneshot->is_pressed();
  269. }
  270. /*
  271. * Returns true if ConnectDialog is being used to edit an existing connection.
  272. */
  273. bool ConnectDialog::is_editing() const {
  274. return bEditMode;
  275. }
  276. /*
  277. * Initialize ConnectDialog and populate fields with expected data.
  278. * If creating a connection from scratch, sensible defaults are used.
  279. * If editing an existing connection, previous data is retained.
  280. */
  281. void ConnectDialog::init(ConnectionData c, bool bEdit) {
  282. set_hide_on_ok(false);
  283. source = static_cast<Node *>(c.source);
  284. signal = c.signal;
  285. tree->set_selected(nullptr);
  286. tree->set_marked(source, true);
  287. if (c.target) {
  288. set_dst_node(static_cast<Node *>(c.target));
  289. set_dst_method(c.method);
  290. }
  291. _update_ok_enabled();
  292. bool bDeferred = (c.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  293. bool bOneshot = (c.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT;
  294. deferred->set_pressed(bDeferred);
  295. oneshot->set_pressed(bOneshot);
  296. cdbinds->params.clear();
  297. cdbinds->params = c.binds;
  298. cdbinds->notify_changed();
  299. bEditMode = bEdit;
  300. }
  301. void ConnectDialog::popup_dialog(const String &p_for_signal) {
  302. from_signal->set_text(p_for_signal);
  303. error_label->add_theme_color_override("font_color", error_label->get_theme_color("error_color", "Editor"));
  304. if (!advanced->is_pressed()) {
  305. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  306. }
  307. popup_centered();
  308. }
  309. void ConnectDialog::_advanced_pressed() {
  310. if (advanced->is_pressed()) {
  311. set_min_size(Size2(900, 500) * EDSCALE);
  312. connect_to_label->set_text(TTR("Connect to Node:"));
  313. tree->set_connect_to_script_mode(false);
  314. vbc_right->show();
  315. error_label->hide();
  316. } else {
  317. set_min_size(Size2(600, 500) * EDSCALE);
  318. set_size(Size2());
  319. connect_to_label->set_text(TTR("Connect to Script:"));
  320. tree->set_connect_to_script_mode(true);
  321. vbc_right->hide();
  322. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  323. }
  324. _update_ok_enabled();
  325. popup_centered();
  326. }
  327. ConnectDialog::ConnectDialog() {
  328. set_min_size(Size2(600, 500) * EDSCALE);
  329. VBoxContainer *vbc = memnew(VBoxContainer);
  330. add_child(vbc);
  331. HBoxContainer *main_hb = memnew(HBoxContainer);
  332. vbc->add_child(main_hb);
  333. main_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  334. VBoxContainer *vbc_left = memnew(VBoxContainer);
  335. main_hb->add_child(vbc_left);
  336. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  337. from_signal = memnew(LineEdit);
  338. from_signal->set_editable(false);
  339. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  340. tree = memnew(SceneTreeEditor(false));
  341. tree->set_connecting_signal(true);
  342. tree->set_show_enabled_subscene(true);
  343. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  344. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  345. tree->set_connect_to_script_mode(true);
  346. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), tree, true);
  347. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  348. error_label = memnew(Label);
  349. error_label->set_text(TTR("Scene does not contain any script."));
  350. vbc_left->add_child(error_label);
  351. error_label->hide();
  352. vbc_right = memnew(VBoxContainer);
  353. main_hb->add_child(vbc_right);
  354. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  355. vbc_right->hide();
  356. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  357. type_list = memnew(OptionButton);
  358. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  359. add_bind_hb->add_child(type_list);
  360. type_list->add_item("bool", Variant::BOOL);
  361. type_list->add_item("int", Variant::INT);
  362. type_list->add_item("real", Variant::FLOAT);
  363. type_list->add_item("String", Variant::STRING);
  364. type_list->add_item("StringName", Variant::STRING_NAME);
  365. type_list->add_item("Vector2", Variant::VECTOR2);
  366. type_list->add_item("Rect2", Variant::RECT2);
  367. type_list->add_item("Vector3", Variant::VECTOR3);
  368. type_list->add_item("Plane", Variant::PLANE);
  369. type_list->add_item("Quat", Variant::QUAT);
  370. type_list->add_item("AABB", Variant::AABB);
  371. type_list->add_item("Basis", Variant::BASIS);
  372. type_list->add_item("Transform", Variant::TRANSFORM);
  373. type_list->add_item("Color", Variant::COLOR);
  374. type_list->select(0);
  375. Button *add_bind = memnew(Button);
  376. add_bind->set_text(TTR("Add"));
  377. add_bind_hb->add_child(add_bind);
  378. add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind));
  379. Button *del_bind = memnew(Button);
  380. del_bind->set_text(TTR("Remove"));
  381. add_bind_hb->add_child(del_bind);
  382. del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind));
  383. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  384. bind_editor = memnew(EditorInspector);
  385. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  386. HBoxContainer *dstm_hb = memnew(HBoxContainer);
  387. vbc_left->add_margin_child(TTR("Receiver Method:"), dstm_hb);
  388. dst_method = memnew(LineEdit);
  389. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  390. dst_method->connect("text_entered", callable_mp(this, &ConnectDialog::_text_entered));
  391. dstm_hb->add_child(dst_method);
  392. advanced = memnew(CheckButton);
  393. dstm_hb->add_child(advanced);
  394. advanced->set_text(TTR("Advanced"));
  395. advanced->connect("pressed", callable_mp(this, &ConnectDialog::_advanced_pressed));
  396. deferred = memnew(CheckBox);
  397. deferred->set_h_size_flags(0);
  398. deferred->set_text(TTR("Deferred"));
  399. deferred->set_tooltip(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  400. vbc_right->add_child(deferred);
  401. oneshot = memnew(CheckBox);
  402. oneshot->set_h_size_flags(0);
  403. oneshot->set_text(TTR("Oneshot"));
  404. oneshot->set_tooltip(TTR("Disconnects the signal after its first emission."));
  405. vbc_right->add_child(oneshot);
  406. cdbinds = memnew(ConnectDialogBinds);
  407. error = memnew(AcceptDialog);
  408. add_child(error);
  409. error->set_title(TTR("Cannot connect signal"));
  410. error->get_ok_button()->set_text(TTR("Close"));
  411. get_ok_button()->set_text(TTR("Connect"));
  412. }
  413. ConnectDialog::~ConnectDialog() {
  414. memdelete(cdbinds);
  415. }
  416. //////////////////////////////////////////
  417. // Originally copied and adapted from EditorProperty, try to keep style in sync.
  418. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  419. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  420. help_bit->add_theme_style_override("panel", get_theme_stylebox("panel", "TooltipPanel"));
  421. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  422. String text = TTR("Signal:") + " [u][b]" + p_text.get_slice("::", 0) + "[/b][/u]";
  423. text += p_text.get_slice("::", 1).strip_edges() + "\n";
  424. text += p_text.get_slice("::", 2).strip_edges();
  425. help_bit->call_deferred("set_text", text); //hack so it uses proper theme once inside scene
  426. return help_bit;
  427. }
  428. struct _ConnectionsDockMethodInfoSort {
  429. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  430. return a.name < b.name;
  431. }
  432. };
  433. void ConnectionsDock::_filter_changed(const String &p_text) {
  434. update_tree();
  435. }
  436. /*
  437. * Post-ConnectDialog callback for creating/editing connections.
  438. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  439. */
  440. void ConnectionsDock::_make_or_edit_connection() {
  441. TreeItem *it = tree->get_selected();
  442. ERR_FAIL_COND(!it);
  443. NodePath dst_path = connect_dialog->get_dst_path();
  444. Node *target = selectedNode->get_node(dst_path);
  445. ERR_FAIL_COND(!target);
  446. ConnectDialog::ConnectionData cToMake;
  447. cToMake.source = connect_dialog->get_source();
  448. cToMake.target = target;
  449. cToMake.signal = connect_dialog->get_signal_name();
  450. cToMake.method = connect_dialog->get_dst_method_name();
  451. cToMake.binds = connect_dialog->get_binds();
  452. bool defer = connect_dialog->get_deferred();
  453. bool oshot = connect_dialog->get_oneshot();
  454. cToMake.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0);
  455. // Conditions to add function: must have a script and must not have the method already
  456. // (in the class, the script itself, or inherited).
  457. bool add_script_function = false;
  458. Ref<Script> script = target->get_script();
  459. if (!target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cToMake.method)) {
  460. // There is a chance that the method is inherited from another script.
  461. bool found_inherited_function = false;
  462. Ref<Script> inherited_script = script->get_base_script();
  463. while (!inherited_script.is_null()) {
  464. int line = inherited_script->get_language()->find_function(cToMake.method, inherited_script->get_source_code());
  465. if (line != -1) {
  466. found_inherited_function = true;
  467. break;
  468. }
  469. inherited_script = inherited_script->get_base_script();
  470. }
  471. add_script_function = !found_inherited_function;
  472. }
  473. PackedStringArray script_function_args;
  474. if (add_script_function) {
  475. // Pick up args here before "it" is deleted by update_tree.
  476. script_function_args = it->get_metadata(0).operator Dictionary()["args"];
  477. for (int i = 0; i < cToMake.binds.size(); i++) {
  478. script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cToMake.binds[i].get_type()));
  479. }
  480. }
  481. if (connect_dialog->is_editing()) {
  482. _disconnect(*it);
  483. _connect(cToMake);
  484. } else {
  485. _connect(cToMake);
  486. }
  487. // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, which will delete the object "it" is pointing to.
  488. it = nullptr;
  489. if (add_script_function) {
  490. editor->emit_signal("script_add_function_request", target, cToMake.method, script_function_args);
  491. hide();
  492. }
  493. update_tree();
  494. }
  495. /*
  496. * Creates single connection w/ undo-redo functionality.
  497. */
  498. void ConnectionsDock::_connect(ConnectDialog::ConnectionData cToMake) {
  499. Node *source = static_cast<Node *>(cToMake.source);
  500. Node *target = static_cast<Node *>(cToMake.target);
  501. if (!source || !target) {
  502. return;
  503. }
  504. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(cToMake.signal), String(cToMake.method)));
  505. Callable c(target, cToMake.method);
  506. undo_redo->add_do_method(source, "connect", cToMake.signal, c, cToMake.binds, cToMake.flags);
  507. undo_redo->add_undo_method(source, "disconnect", cToMake.signal, c);
  508. undo_redo->add_do_method(this, "update_tree");
  509. undo_redo->add_undo_method(this, "update_tree");
  510. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
  511. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  512. undo_redo->commit_action();
  513. }
  514. /*
  515. * Break single connection w/ undo-redo functionality.
  516. */
  517. void ConnectionsDock::_disconnect(TreeItem &item) {
  518. Connection cd = item.get_metadata(0);
  519. ConnectDialog::ConnectionData c = cd;
  520. ERR_FAIL_COND(c.source != selectedNode); // Shouldn't happen but... Bugcheck.
  521. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method));
  522. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, Callable(c.target, c.method));
  523. undo_redo->add_undo_method(selectedNode, "connect", c.signal, Callable(c.target, c.method), c.binds, c.flags);
  524. undo_redo->add_do_method(this, "update_tree");
  525. undo_redo->add_undo_method(this, "update_tree");
  526. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  527. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  528. undo_redo->commit_action();
  529. }
  530. /*
  531. * Break all connections of currently selected signal.
  532. * Can undo-redo as a single action.
  533. */
  534. void ConnectionsDock::_disconnect_all() {
  535. TreeItem *item = tree->get_selected();
  536. if (!_is_item_signal(*item)) {
  537. return;
  538. }
  539. TreeItem *child = item->get_children();
  540. String signalName = item->get_metadata(0).operator Dictionary()["name"];
  541. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signalName));
  542. while (child) {
  543. Connection cd = child->get_metadata(0);
  544. ConnectDialog::ConnectionData c = cd;
  545. undo_redo->add_do_method(selectedNode, "disconnect", c.signal, Callable(c.target, c.method));
  546. undo_redo->add_undo_method(selectedNode, "connect", c.signal, Callable(c.target, c.method), c.binds, c.flags);
  547. child = child->get_next();
  548. }
  549. undo_redo->add_do_method(this, "update_tree");
  550. undo_redo->add_undo_method(this, "update_tree");
  551. undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  552. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
  553. undo_redo->commit_action();
  554. }
  555. void ConnectionsDock::_tree_item_selected() {
  556. TreeItem *item = tree->get_selected();
  557. if (!item) { // Unlikely. Disable button just in case.
  558. connect_button->set_text(TTR("Connect..."));
  559. connect_button->set_disabled(true);
  560. } else if (_is_item_signal(*item)) {
  561. connect_button->set_text(TTR("Connect..."));
  562. connect_button->set_disabled(false);
  563. } else {
  564. connect_button->set_text(TTR("Disconnect"));
  565. connect_button->set_disabled(false);
  566. }
  567. }
  568. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  569. TreeItem *item = tree->get_selected();
  570. if (!item) {
  571. return;
  572. }
  573. if (_is_item_signal(*item)) {
  574. _open_connection_dialog(*item);
  575. } else {
  576. _go_to_script(*item);
  577. }
  578. }
  579. bool ConnectionsDock::_is_item_signal(TreeItem &item) {
  580. return (item.get_parent() == tree->get_root() || item.get_parent()->get_parent() == tree->get_root());
  581. }
  582. /*
  583. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  584. */
  585. void ConnectionsDock::_open_connection_dialog(TreeItem &item) {
  586. String signal = item.get_metadata(0).operator Dictionary()["name"];
  587. const String &signalname = signal;
  588. String midname = selectedNode->get_name();
  589. for (int i = 0; i < midname.length(); i++) { //TODO: Regex filter may be cleaner.
  590. char32_t c = midname[i];
  591. if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) {
  592. if (c == ' ') {
  593. // Replace spaces with underlines.
  594. c = '_';
  595. } else {
  596. // Remove any other characters.
  597. midname.remove(i);
  598. i--;
  599. continue;
  600. }
  601. }
  602. midname[i] = c;
  603. }
  604. Node *dst_node = selectedNode->get_owner() ? selectedNode->get_owner() : selectedNode;
  605. if (!dst_node || dst_node->get_script().is_null()) {
  606. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  607. }
  608. StringName dst_method = "_on_" + midname + "_" + signal;
  609. ConnectDialog::ConnectionData c;
  610. c.source = selectedNode;
  611. c.signal = StringName(signalname);
  612. c.target = dst_node;
  613. c.method = dst_method;
  614. connect_dialog->popup_dialog(signalname);
  615. connect_dialog->init(c);
  616. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  617. }
  618. /*
  619. * Open connection dialog with Connection data to EDIT an existing connection.
  620. */
  621. void ConnectionsDock::_open_connection_dialog(ConnectDialog::ConnectionData cToEdit) {
  622. Node *src = static_cast<Node *>(cToEdit.source);
  623. Node *dst = static_cast<Node *>(cToEdit.target);
  624. if (src && dst) {
  625. const String &signalname = cToEdit.signal;
  626. connect_dialog->set_title(TTR("Edit Connection:") + cToEdit.signal);
  627. connect_dialog->popup_dialog(signalname);
  628. connect_dialog->init(cToEdit, true);
  629. }
  630. }
  631. /*
  632. * Open slot method location in script editor.
  633. */
  634. void ConnectionsDock::_go_to_script(TreeItem &item) {
  635. if (_is_item_signal(item)) {
  636. return;
  637. }
  638. Connection cd = item.get_metadata(0);
  639. ConnectDialog::ConnectionData c = cd;
  640. ERR_FAIL_COND(c.source != selectedNode); //shouldn't happen but...bugcheck
  641. if (!c.target) {
  642. return;
  643. }
  644. Ref<Script> script = c.target->get_script();
  645. if (script.is_null()) {
  646. return;
  647. }
  648. if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, c.method)) {
  649. editor->call("_editor_select", EditorNode::EDITOR_SCRIPT);
  650. }
  651. }
  652. void ConnectionsDock::_handle_signal_menu_option(int option) {
  653. TreeItem *item = tree->get_selected();
  654. if (!item) {
  655. return;
  656. }
  657. switch (option) {
  658. case CONNECT: {
  659. _open_connection_dialog(*item);
  660. } break;
  661. case DISCONNECT_ALL: {
  662. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  663. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  664. disconnect_all_dialog->popup_centered();
  665. } break;
  666. }
  667. }
  668. void ConnectionsDock::_handle_slot_menu_option(int option) {
  669. TreeItem *item = tree->get_selected();
  670. if (!item) {
  671. return;
  672. }
  673. switch (option) {
  674. case EDIT: {
  675. Connection c = item->get_metadata(0);
  676. _open_connection_dialog(c);
  677. } break;
  678. case GO_TO_SCRIPT: {
  679. _go_to_script(*item);
  680. } break;
  681. case DISCONNECT: {
  682. _disconnect(*item);
  683. update_tree();
  684. } break;
  685. }
  686. }
  687. void ConnectionsDock::_rmb_pressed(Vector2 position) {
  688. TreeItem *item = tree->get_selected();
  689. if (!item) {
  690. return;
  691. }
  692. Vector2 global_position = tree->get_global_position() + position;
  693. if (_is_item_signal(*item)) {
  694. signal_menu->set_position(global_position);
  695. signal_menu->popup();
  696. } else {
  697. slot_menu->set_position(global_position);
  698. slot_menu->popup();
  699. }
  700. }
  701. void ConnectionsDock::_close() {
  702. hide();
  703. }
  704. void ConnectionsDock::_connect_pressed() {
  705. TreeItem *item = tree->get_selected();
  706. if (!item) {
  707. connect_button->set_disabled(true);
  708. return;
  709. }
  710. if (_is_item_signal(*item)) {
  711. _open_connection_dialog(*item);
  712. } else {
  713. _disconnect(*item);
  714. update_tree();
  715. }
  716. }
  717. void ConnectionsDock::_notification(int p_what) {
  718. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  719. update_tree();
  720. }
  721. }
  722. void ConnectionsDock::_bind_methods() {
  723. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  724. }
  725. void ConnectionsDock::set_node(Node *p_node) {
  726. selectedNode = p_node;
  727. update_tree();
  728. }
  729. void ConnectionsDock::update_tree() {
  730. tree->clear();
  731. if (!selectedNode) {
  732. return;
  733. }
  734. TreeItem *root = tree->create_item();
  735. List<MethodInfo> node_signals;
  736. selectedNode->get_signal_list(&node_signals);
  737. bool did_script = false;
  738. StringName base = selectedNode->get_class();
  739. while (base) {
  740. List<MethodInfo> node_signals2;
  741. Ref<Texture2D> icon;
  742. String name;
  743. if (!did_script) {
  744. // Get script signals (including signals from any base scripts).
  745. Ref<Script> scr = selectedNode->get_script();
  746. if (scr.is_valid()) {
  747. scr->get_script_signal_list(&node_signals2);
  748. if (scr->get_path().is_resource_file()) {
  749. name = scr->get_path().get_file();
  750. } else {
  751. name = scr->get_class();
  752. }
  753. if (has_theme_icon(scr->get_class(), "EditorIcons")) {
  754. icon = get_theme_icon(scr->get_class(), "EditorIcons");
  755. }
  756. }
  757. } else {
  758. ClassDB::get_signal_list(base, &node_signals2, true);
  759. if (has_theme_icon(base, "EditorIcons")) {
  760. icon = get_theme_icon(base, "EditorIcons");
  761. }
  762. name = base;
  763. }
  764. if (!icon.is_valid()) {
  765. icon = get_theme_icon("Object", "EditorIcons");
  766. }
  767. TreeItem *section_item = nullptr;
  768. // Create subsections.
  769. if (node_signals2.size()) {
  770. section_item = tree->create_item(root);
  771. section_item->set_text(0, name);
  772. section_item->set_icon(0, icon);
  773. section_item->set_selectable(0, false);
  774. section_item->set_editable(0, false);
  775. section_item->set_custom_bg_color(0, get_theme_color("prop_subsection", "Editor"));
  776. node_signals2.sort();
  777. }
  778. for (List<MethodInfo>::Element *E = node_signals2.front(); E; E = E->next()) {
  779. MethodInfo &mi = E->get();
  780. StringName signal_name = mi.name;
  781. String signaldesc = "(";
  782. PackedStringArray argnames;
  783. String filter_text = search_box->get_text();
  784. if (!filter_text.is_subsequence_ofi(signal_name)) {
  785. continue;
  786. }
  787. if (mi.arguments.size()) {
  788. for (int i = 0; i < mi.arguments.size(); i++) {
  789. PropertyInfo &pi = mi.arguments[i];
  790. if (i > 0) {
  791. signaldesc += ", ";
  792. }
  793. String tname = "var";
  794. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  795. tname = pi.class_name.operator String();
  796. } else if (pi.type != Variant::NIL) {
  797. tname = Variant::get_type_name(pi.type);
  798. }
  799. signaldesc += (pi.name == "" ? String("arg " + itos(i)) : pi.name) + ": " + tname;
  800. argnames.push_back(pi.name + ":" + tname);
  801. }
  802. }
  803. signaldesc += ")";
  804. // Create the children of the subsection - the actual list of signals.
  805. TreeItem *signal_item = tree->create_item(section_item);
  806. signal_item->set_text(0, String(signal_name) + signaldesc);
  807. Dictionary sinfo;
  808. sinfo["name"] = signal_name;
  809. sinfo["args"] = argnames;
  810. signal_item->set_metadata(0, sinfo);
  811. signal_item->set_icon(0, get_theme_icon("Signal", "EditorIcons"));
  812. // Set tooltip with the signal's documentation.
  813. {
  814. String descr;
  815. bool found = false;
  816. Map<StringName, Map<StringName, String>>::Element *G = descr_cache.find(base);
  817. if (G) {
  818. Map<StringName, String>::Element *F = G->get().find(signal_name);
  819. if (F) {
  820. found = true;
  821. descr = F->get();
  822. }
  823. }
  824. if (!found) {
  825. DocTools *dd = EditorHelp::get_doc_data();
  826. Map<String, DocData::ClassDoc>::Element *F = dd->class_list.find(base);
  827. while (F && descr == String()) {
  828. for (int i = 0; i < F->get().signals.size(); i++) {
  829. if (F->get().signals[i].name == signal_name.operator String()) {
  830. descr = DTR(F->get().signals[i].description);
  831. break;
  832. }
  833. }
  834. if (!F->get().inherits.is_empty()) {
  835. F = dd->class_list.find(F->get().inherits);
  836. } else {
  837. break;
  838. }
  839. }
  840. descr_cache[base][signal_name] = descr;
  841. }
  842. // "::" separators used in make_custom_tooltip for formatting.
  843. signal_item->set_tooltip(0, String(signal_name) + "::" + signaldesc + "::" + descr);
  844. }
  845. // List existing connections
  846. List<Object::Connection> connections;
  847. selectedNode->get_signal_connection_list(signal_name, &connections);
  848. for (List<Object::Connection>::Element *F = connections.front(); F; F = F->next()) {
  849. Connection cn = F->get();
  850. if (!(cn.flags & CONNECT_PERSIST)) {
  851. continue;
  852. }
  853. ConnectDialog::ConnectionData c = cn;
  854. Node *target = Object::cast_to<Node>(c.target);
  855. if (!target) {
  856. continue;
  857. }
  858. String path = String(selectedNode->get_path_to(target)) + " :: " + c.method + "()";
  859. if (c.flags & CONNECT_DEFERRED) {
  860. path += " (deferred)";
  861. }
  862. if (c.flags & CONNECT_ONESHOT) {
  863. path += " (oneshot)";
  864. }
  865. if (c.binds.size()) {
  866. path += " binds(";
  867. for (int i = 0; i < c.binds.size(); i++) {
  868. if (i > 0) {
  869. path += ", ";
  870. }
  871. path += c.binds[i].operator String();
  872. }
  873. path += ")";
  874. }
  875. TreeItem *connection_item = tree->create_item(signal_item);
  876. connection_item->set_text(0, path);
  877. Connection cd = c;
  878. connection_item->set_metadata(0, cd);
  879. connection_item->set_icon(0, get_theme_icon("Slot", "EditorIcons"));
  880. }
  881. }
  882. if (!did_script) {
  883. did_script = true;
  884. } else {
  885. base = ClassDB::get_parent_class(base);
  886. }
  887. }
  888. connect_button->set_text(TTR("Connect..."));
  889. connect_button->set_disabled(true);
  890. }
  891. ConnectionsDock::ConnectionsDock(EditorNode *p_editor) {
  892. editor = p_editor;
  893. set_name(TTR("Signals"));
  894. VBoxContainer *vbc = this;
  895. search_box = memnew(LineEdit);
  896. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  897. search_box->set_placeholder(TTR("Filter signals"));
  898. search_box->set_right_icon(get_theme_icon("Search", "EditorIcons"));
  899. search_box->set_clear_button_enabled(true);
  900. search_box->connect("text_changed", callable_mp(this, &ConnectionsDock::_filter_changed));
  901. vbc->add_child(search_box);
  902. tree = memnew(ConnectionsDockTree);
  903. tree->set_columns(1);
  904. tree->set_select_mode(Tree::SELECT_ROW);
  905. tree->set_hide_root(true);
  906. vbc->add_child(tree);
  907. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  908. tree->set_allow_rmb_select(true);
  909. connect_button = memnew(Button);
  910. HBoxContainer *hb = memnew(HBoxContainer);
  911. vbc->add_child(hb);
  912. hb->add_spacer();
  913. hb->add_child(connect_button);
  914. connect_button->connect("pressed", callable_mp(this, &ConnectionsDock::_connect_pressed));
  915. connect_dialog = memnew(ConnectDialog);
  916. add_child(connect_dialog);
  917. disconnect_all_dialog = memnew(ConfirmationDialog);
  918. add_child(disconnect_all_dialog);
  919. disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
  920. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  921. signal_menu = memnew(PopupMenu);
  922. add_child(signal_menu);
  923. signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  924. signal_menu->add_item(TTR("Connect..."), CONNECT);
  925. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  926. slot_menu = memnew(PopupMenu);
  927. add_child(slot_menu);
  928. slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  929. slot_menu->add_item(TTR("Edit..."), EDIT);
  930. slot_menu->add_item(TTR("Go To Method"), GO_TO_SCRIPT);
  931. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  932. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  933. tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
  934. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  935. tree->connect("item_rmb_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed));
  936. add_theme_constant_override("separation", 3 * EDSCALE);
  937. }
  938. ConnectionsDock::~ConnectionsDock() {
  939. }