connections_dialog.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*************************************************************************/
  2. /* connections_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "connections_dialog.h"
  31. #include "editor/doc_tools.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_undo_redo_manager.h"
  36. #include "editor/scene_tree_dock.h"
  37. #include "plugins/script_editor_plugin.h"
  38. static Node *_find_first_script(Node *p_root, Node *p_node) {
  39. if (p_node != p_root && p_node->get_owner() != p_root) {
  40. return nullptr;
  41. }
  42. if (!p_node->get_script().is_null()) {
  43. return p_node;
  44. }
  45. for (int i = 0; i < p_node->get_child_count(); i++) {
  46. Node *ret = _find_first_script(p_root, p_node->get_child(i));
  47. if (ret) {
  48. return ret;
  49. }
  50. }
  51. return nullptr;
  52. }
  53. class ConnectDialogBinds : public Object {
  54. GDCLASS(ConnectDialogBinds, Object);
  55. public:
  56. Vector<Variant> params;
  57. bool _set(const StringName &p_name, const Variant &p_value) {
  58. String name = p_name;
  59. if (name.begins_with("bind/argument_")) {
  60. int which = name.get_slice("_", 1).to_int() - 1;
  61. ERR_FAIL_INDEX_V(which, params.size(), false);
  62. params.write[which] = p_value;
  63. } else {
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool _get(const StringName &p_name, Variant &r_ret) const {
  69. String name = p_name;
  70. if (name.begins_with("bind/argument_")) {
  71. int which = name.get_slice("_", 1).to_int() - 1;
  72. ERR_FAIL_INDEX_V(which, params.size(), false);
  73. r_ret = params[which];
  74. } else {
  75. return false;
  76. }
  77. return true;
  78. }
  79. void _get_property_list(List<PropertyInfo> *p_list) const {
  80. for (int i = 0; i < params.size(); i++) {
  81. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));
  82. }
  83. }
  84. void notify_changed() {
  85. notify_property_list_changed();
  86. }
  87. ConnectDialogBinds() {
  88. }
  89. };
  90. /*
  91. * Signal automatically called by parent dialog.
  92. */
  93. void ConnectDialog::ok_pressed() {
  94. String method_name = dst_method->get_text();
  95. if (method_name.is_empty()) {
  96. error->set_text(TTR("Method in target node must be specified."));
  97. error->popup_centered();
  98. return;
  99. }
  100. if (!method_name.strip_edges().is_valid_identifier()) {
  101. error->set_text(TTR("Method name must be a valid identifier."));
  102. error->popup_centered();
  103. return;
  104. }
  105. Node *target = tree->get_selected();
  106. if (!target) {
  107. return; // Nothing selected in the tree, not an error.
  108. }
  109. if (target->get_script().is_null()) {
  110. if (!target->has_method(method_name)) {
  111. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  112. error->popup_centered();
  113. return;
  114. }
  115. }
  116. emit_signal(SNAME("connected"));
  117. hide();
  118. }
  119. void ConnectDialog::_cancel_pressed() {
  120. hide();
  121. }
  122. void ConnectDialog::_item_activated() {
  123. _ok_pressed(); // From AcceptDialog.
  124. }
  125. void ConnectDialog::_text_submitted(const String &p_text) {
  126. _ok_pressed(); // From AcceptDialog.
  127. }
  128. /*
  129. * Called each time a target node is selected within the target node tree.
  130. */
  131. void ConnectDialog::_tree_node_selected() {
  132. Node *current = tree->get_selected();
  133. if (!current) {
  134. return;
  135. }
  136. dst_path = source->get_path_to(current);
  137. _update_ok_enabled();
  138. }
  139. void ConnectDialog::_unbind_count_changed(double p_count) {
  140. for (Control *control : bind_controls) {
  141. BaseButton *b = Object::cast_to<BaseButton>(control);
  142. if (b) {
  143. b->set_disabled(p_count > 0);
  144. }
  145. EditorInspector *e = Object::cast_to<EditorInspector>(control);
  146. if (e) {
  147. e->set_read_only(p_count > 0);
  148. }
  149. }
  150. }
  151. /*
  152. * Adds a new parameter bind to connection.
  153. */
  154. void ConnectDialog::_add_bind() {
  155. Variant::Type type = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  156. Variant value;
  157. Callable::CallError err;
  158. Variant::construct(type, value, nullptr, 0, err);
  159. cdbinds->params.push_back(value);
  160. cdbinds->notify_changed();
  161. }
  162. /*
  163. * Remove parameter bind from connection.
  164. */
  165. void ConnectDialog::_remove_bind() {
  166. String st = bind_editor->get_selected_path();
  167. if (st.is_empty()) {
  168. return;
  169. }
  170. int idx = st.get_slice("/", 1).to_int() - 1;
  171. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  172. cdbinds->params.remove_at(idx);
  173. cdbinds->notify_changed();
  174. }
  175. /*
  176. * Enables or disables the connect button. The connect button is enabled if a
  177. * node is selected and valid in the selected mode.
  178. */
  179. void ConnectDialog::_update_ok_enabled() {
  180. Node *target = tree->get_selected();
  181. if (target == nullptr) {
  182. get_ok_button()->set_disabled(true);
  183. return;
  184. }
  185. if (!advanced->is_pressed() && target->get_script().is_null()) {
  186. get_ok_button()->set_disabled(true);
  187. return;
  188. }
  189. get_ok_button()->set_disabled(false);
  190. }
  191. void ConnectDialog::_notification(int p_what) {
  192. switch (p_what) {
  193. case NOTIFICATION_ENTER_TREE: {
  194. bind_editor->edit(cdbinds);
  195. [[fallthrough]];
  196. }
  197. case NOTIFICATION_THEME_CHANGED: {
  198. for (int i = 0; i < type_list->get_item_count(); i++) {
  199. String type_name = Variant::get_type_name((Variant::Type)type_list->get_item_id(i));
  200. type_list->set_item_icon(i, get_theme_icon(type_name, SNAME("EditorIcons")));
  201. }
  202. Ref<StyleBox> style = get_theme_stylebox("normal", "LineEdit")->duplicate();
  203. if (style.is_valid()) {
  204. style->set_default_margin(SIDE_TOP, style->get_default_margin(SIDE_TOP) + 1.0);
  205. from_signal->add_theme_style_override("normal", style);
  206. }
  207. } break;
  208. }
  209. }
  210. void ConnectDialog::_bind_methods() {
  211. ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed);
  212. ClassDB::bind_method("_update_ok_enabled", &ConnectDialog::_update_ok_enabled);
  213. ADD_SIGNAL(MethodInfo("connected"));
  214. }
  215. Node *ConnectDialog::get_source() const {
  216. return source;
  217. }
  218. StringName ConnectDialog::get_signal_name() const {
  219. return signal;
  220. }
  221. NodePath ConnectDialog::get_dst_path() const {
  222. return dst_path;
  223. }
  224. void ConnectDialog::set_dst_node(Node *p_node) {
  225. tree->set_selected(p_node);
  226. }
  227. StringName ConnectDialog::get_dst_method_name() const {
  228. String txt = dst_method->get_text();
  229. if (txt.contains("(")) {
  230. txt = txt.left(txt.find("(")).strip_edges();
  231. }
  232. return txt;
  233. }
  234. void ConnectDialog::set_dst_method(const StringName &p_method) {
  235. dst_method->set_text(p_method);
  236. }
  237. int ConnectDialog::get_unbinds() const {
  238. return int(unbind_count->get_value());
  239. }
  240. Vector<Variant> ConnectDialog::get_binds() const {
  241. return cdbinds->params;
  242. }
  243. bool ConnectDialog::get_deferred() const {
  244. return deferred->is_pressed();
  245. }
  246. bool ConnectDialog::get_one_shot() const {
  247. return one_shot->is_pressed();
  248. }
  249. /*
  250. * Returns true if ConnectDialog is being used to edit an existing connection.
  251. */
  252. bool ConnectDialog::is_editing() const {
  253. return edit_mode;
  254. }
  255. /*
  256. * Initialize ConnectDialog and populate fields with expected data.
  257. * If creating a connection from scratch, sensible defaults are used.
  258. * If editing an existing connection, previous data is retained.
  259. */
  260. void ConnectDialog::init(ConnectionData p_cd, bool p_edit) {
  261. set_hide_on_ok(false);
  262. source = static_cast<Node *>(p_cd.source);
  263. signal = p_cd.signal;
  264. tree->set_selected(nullptr);
  265. tree->set_marked(source, true);
  266. if (p_cd.target) {
  267. set_dst_node(static_cast<Node *>(p_cd.target));
  268. set_dst_method(p_cd.method);
  269. }
  270. _update_ok_enabled();
  271. bool b_deferred = (p_cd.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  272. bool b_oneshot = (p_cd.flags & CONNECT_ONE_SHOT) == CONNECT_ONE_SHOT;
  273. deferred->set_pressed(b_deferred);
  274. one_shot->set_pressed(b_oneshot);
  275. MethodInfo r_signal;
  276. Ref<Script> source_script = source->get_script();
  277. if (source_script.is_valid() && source_script->has_script_signal(signal)) {
  278. List<MethodInfo> signals;
  279. source_script->get_script_signal_list(&signals);
  280. for (MethodInfo &mi : signals) {
  281. if (mi.name == signal) {
  282. r_signal = mi;
  283. break;
  284. }
  285. }
  286. } else {
  287. ClassDB::get_signal(source->get_class(), signal, &r_signal);
  288. }
  289. unbind_count->set_max(r_signal.arguments.size());
  290. unbind_count->set_value(p_cd.unbinds);
  291. _unbind_count_changed(p_cd.unbinds);
  292. cdbinds->params.clear();
  293. cdbinds->params = p_cd.binds;
  294. cdbinds->notify_changed();
  295. edit_mode = p_edit;
  296. }
  297. void ConnectDialog::popup_dialog(const String &p_for_signal) {
  298. from_signal->set_text(p_for_signal);
  299. error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  300. if (!advanced->is_pressed()) {
  301. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  302. }
  303. if (first_popup) {
  304. first_popup = false;
  305. _advanced_pressed();
  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. reset_size();
  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. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "use_advanced_connections", advanced->is_pressed());
  326. popup_centered();
  327. }
  328. ConnectDialog::ConnectDialog() {
  329. set_min_size(Size2(600, 500) * EDSCALE);
  330. VBoxContainer *vbc = memnew(VBoxContainer);
  331. add_child(vbc);
  332. HBoxContainer *main_hb = memnew(HBoxContainer);
  333. vbc->add_child(main_hb);
  334. main_hb->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  335. VBoxContainer *vbc_left = memnew(VBoxContainer);
  336. main_hb->add_child(vbc_left);
  337. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  338. from_signal = memnew(LineEdit);
  339. from_signal->set_editable(false);
  340. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  341. tree = memnew(SceneTreeEditor(false));
  342. tree->set_connecting_signal(true);
  343. tree->set_show_enabled_subscene(true);
  344. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  345. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  346. tree->set_connect_to_script_mode(true);
  347. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), tree, true);
  348. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  349. error_label = memnew(Label);
  350. error_label->set_text(TTR("Scene does not contain any script."));
  351. vbc_left->add_child(error_label);
  352. error_label->hide();
  353. vbc_right = memnew(VBoxContainer);
  354. main_hb->add_child(vbc_right);
  355. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  356. vbc_right->hide();
  357. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  358. type_list = memnew(OptionButton);
  359. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  360. add_bind_hb->add_child(type_list);
  361. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  362. if (i == Variant::NIL || i == Variant::OBJECT || i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  363. // These types can't be constructed or serialized properly, so skip them.
  364. continue;
  365. }
  366. type_list->add_item(Variant::get_type_name(Variant::Type(i)), i);
  367. }
  368. bind_controls.push_back(type_list);
  369. Button *add_bind = memnew(Button);
  370. add_bind->set_text(TTR("Add"));
  371. add_bind_hb->add_child(add_bind);
  372. add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind));
  373. bind_controls.push_back(add_bind);
  374. Button *del_bind = memnew(Button);
  375. del_bind->set_text(TTR("Remove"));
  376. add_bind_hb->add_child(del_bind);
  377. del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind));
  378. bind_controls.push_back(del_bind);
  379. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  380. bind_editor = memnew(EditorInspector);
  381. bind_controls.push_back(bind_editor);
  382. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  383. unbind_count = memnew(SpinBox);
  384. unbind_count->set_tooltip_text(TTR("Allows to drop arguments sent by signal emitter."));
  385. unbind_count->connect("value_changed", callable_mp(this, &ConnectDialog::_unbind_count_changed));
  386. vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);
  387. dst_method = memnew(LineEdit);
  388. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  389. dst_method->connect("text_submitted", callable_mp(this, &ConnectDialog::_text_submitted));
  390. vbc_left->add_margin_child(TTR("Receiver Method:"), dst_method);
  391. advanced = memnew(CheckButton);
  392. vbc_left->add_child(advanced);
  393. advanced->set_text(TTR("Advanced"));
  394. advanced->set_h_size_flags(Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND);
  395. advanced->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "use_advanced_connections", false));
  396. advanced->connect("pressed", callable_mp(this, &ConnectDialog::_advanced_pressed));
  397. HBoxContainer *hbox = memnew(HBoxContainer);
  398. vbc_right->add_child(hbox);
  399. deferred = memnew(CheckBox);
  400. deferred->set_h_size_flags(0);
  401. deferred->set_text(TTR("Deferred"));
  402. deferred->set_tooltip_text(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  403. hbox->add_child(deferred);
  404. one_shot = memnew(CheckBox);
  405. one_shot->set_h_size_flags(0);
  406. one_shot->set_text(TTR("One Shot"));
  407. one_shot->set_tooltip_text(TTR("Disconnects the signal after its first emission."));
  408. hbox->add_child(one_shot);
  409. cdbinds = memnew(ConnectDialogBinds);
  410. error = memnew(AcceptDialog);
  411. add_child(error);
  412. error->set_title(TTR("Cannot connect signal"));
  413. error->set_ok_button_text(TTR("Close"));
  414. set_ok_button_text(TTR("Connect"));
  415. }
  416. ConnectDialog::~ConnectDialog() {
  417. memdelete(cdbinds);
  418. }
  419. //////////////////////////////////////////
  420. // Originally copied and adapted from EditorProperty, try to keep style in sync.
  421. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  422. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  423. help_bit->get_rich_text()->set_fixed_size_to_width(360 * EDSCALE);
  424. // p_text is expected to be something like this:
  425. // "gui_input::(event: InputEvent)::<Signal description>"
  426. // with the latter being possibly empty.
  427. PackedStringArray slices = p_text.split("::", false);
  428. if (slices.size() < 2) {
  429. // Shouldn't happen here, but just in case pass the text along.
  430. help_bit->set_text(p_text);
  431. return help_bit;
  432. }
  433. String text = TTR("Signal:") + " [u][b]" + slices[0] + "[/b][/u]";
  434. text += slices[1].strip_edges() + "\n";
  435. if (slices.size() > 2) {
  436. text += slices[2].strip_edges();
  437. } else {
  438. text += "[i]" + TTR("No description.") + "[/i]";
  439. }
  440. help_bit->set_text(text);
  441. return help_bit;
  442. }
  443. struct _ConnectionsDockMethodInfoSort {
  444. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  445. return a.name < b.name;
  446. }
  447. };
  448. void ConnectionsDock::_filter_changed(const String &p_text) {
  449. update_tree();
  450. }
  451. /*
  452. * Post-ConnectDialog callback for creating/editing connections.
  453. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  454. */
  455. void ConnectionsDock::_make_or_edit_connection() {
  456. TreeItem *it = tree->get_selected();
  457. ERR_FAIL_COND(!it);
  458. NodePath dst_path = connect_dialog->get_dst_path();
  459. Node *target = selected_node->get_node(dst_path);
  460. ERR_FAIL_COND(!target);
  461. ConnectDialog::ConnectionData cd;
  462. cd.source = connect_dialog->get_source();
  463. cd.target = target;
  464. cd.signal = connect_dialog->get_signal_name();
  465. cd.method = connect_dialog->get_dst_method_name();
  466. cd.unbinds = connect_dialog->get_unbinds();
  467. if (cd.unbinds == 0) {
  468. cd.binds = connect_dialog->get_binds();
  469. }
  470. bool b_deferred = connect_dialog->get_deferred();
  471. bool b_oneshot = connect_dialog->get_one_shot();
  472. cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONE_SHOT : 0);
  473. // Conditions to add function: must have a script and must not have the method already
  474. // (in the class, the script itself, or inherited).
  475. bool add_script_function = false;
  476. Ref<Script> scr = target->get_script();
  477. if (!scr.is_null() && !ClassDB::has_method(target->get_class(), cd.method)) {
  478. // There is a chance that the method is inherited from another script.
  479. bool found_inherited_function = false;
  480. Ref<Script> inherited_scr = scr->get_base_script();
  481. while (!inherited_scr.is_null()) {
  482. int line = inherited_scr->get_language()->find_function(cd.method, inherited_scr->get_source_code());
  483. if (line != -1) {
  484. found_inherited_function = true;
  485. break;
  486. }
  487. inherited_scr = inherited_scr->get_base_script();
  488. }
  489. add_script_function = !found_inherited_function;
  490. }
  491. PackedStringArray script_function_args;
  492. if (add_script_function) {
  493. // Pick up args here before "it" is deleted by update_tree.
  494. script_function_args = it->get_metadata(0).operator Dictionary()["args"];
  495. script_function_args.resize(script_function_args.size() - cd.unbinds);
  496. for (int i = 0; i < cd.binds.size(); i++) {
  497. script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cd.binds[i].get_type()));
  498. }
  499. }
  500. if (connect_dialog->is_editing()) {
  501. _disconnect(*it);
  502. _connect(cd);
  503. } else {
  504. _connect(cd);
  505. }
  506. // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, which will delete the object "it" is pointing to.
  507. it = nullptr;
  508. if (add_script_function) {
  509. EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args);
  510. hide();
  511. }
  512. update_tree();
  513. }
  514. /*
  515. * Creates single connection w/ undo-redo functionality.
  516. */
  517. void ConnectionsDock::_connect(ConnectDialog::ConnectionData p_cd) {
  518. Node *source = Object::cast_to<Node>(p_cd.source);
  519. Node *target = Object::cast_to<Node>(p_cd.target);
  520. if (!source || !target) {
  521. return;
  522. }
  523. Callable callable = p_cd.get_callable();
  524. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));
  525. undo_redo->add_do_method(source, "connect", p_cd.signal, callable, p_cd.flags);
  526. undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);
  527. undo_redo->add_do_method(this, "update_tree");
  528. undo_redo->add_undo_method(this, "update_tree");
  529. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  530. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  531. undo_redo->commit_action();
  532. }
  533. /*
  534. * Break single connection w/ undo-redo functionality.
  535. */
  536. void ConnectionsDock::_disconnect(TreeItem &p_item) {
  537. Connection connection = p_item.get_metadata(0);
  538. ConnectDialog::ConnectionData cd = connection;
  539. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... Bugcheck.
  540. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), cd.signal, cd.method));
  541. Callable callable = cd.get_callable();
  542. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, callable);
  543. undo_redo->add_undo_method(selected_node, "connect", cd.signal, callable, cd.binds, cd.flags);
  544. undo_redo->add_do_method(this, "update_tree");
  545. undo_redo->add_undo_method(this, "update_tree");
  546. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  547. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  548. undo_redo->commit_action();
  549. }
  550. /*
  551. * Break all connections of currently selected signal.
  552. * Can undo-redo as a single action.
  553. */
  554. void ConnectionsDock::_disconnect_all() {
  555. TreeItem *item = tree->get_selected();
  556. if (!_is_item_signal(*item)) {
  557. return;
  558. }
  559. TreeItem *child = item->get_first_child();
  560. String signal_name = item->get_metadata(0).operator Dictionary()["name"];
  561. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));
  562. while (child) {
  563. Connection connection = child->get_metadata(0);
  564. ConnectDialog::ConnectionData cd = connection;
  565. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable());
  566. undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.binds, cd.flags);
  567. child = child->get_next();
  568. }
  569. undo_redo->add_do_method(this, "update_tree");
  570. undo_redo->add_undo_method(this, "update_tree");
  571. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  572. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  573. undo_redo->commit_action();
  574. }
  575. void ConnectionsDock::_tree_item_selected() {
  576. TreeItem *item = tree->get_selected();
  577. if (!item) { // Unlikely. Disable button just in case.
  578. connect_button->set_text(TTR("Connect..."));
  579. connect_button->set_disabled(true);
  580. } else if (_is_item_signal(*item)) {
  581. connect_button->set_text(TTR("Connect..."));
  582. connect_button->set_disabled(false);
  583. } else {
  584. connect_button->set_text(TTR("Disconnect"));
  585. connect_button->set_disabled(false);
  586. }
  587. }
  588. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  589. TreeItem *item = tree->get_selected();
  590. if (!item) {
  591. return;
  592. }
  593. if (_is_item_signal(*item)) {
  594. _open_connection_dialog(*item);
  595. } else {
  596. _go_to_script(*item);
  597. }
  598. }
  599. bool ConnectionsDock::_is_item_signal(TreeItem &p_item) {
  600. return (p_item.get_parent() == tree->get_root() || p_item.get_parent()->get_parent() == tree->get_root());
  601. }
  602. /*
  603. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  604. */
  605. void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
  606. String signal_name = p_item.get_metadata(0).operator Dictionary()["name"];
  607. const String &signal_name_ref = signal_name;
  608. String node_name = selected_node->get_name();
  609. for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.
  610. char32_t c = node_name[i];
  611. if (!is_ascii_identifier_char(c)) {
  612. if (c == ' ') {
  613. // Replace spaces with underlines.
  614. c = '_';
  615. } else {
  616. // Remove any other characters.
  617. node_name.remove_at(i);
  618. i--;
  619. continue;
  620. }
  621. }
  622. node_name[i] = c;
  623. }
  624. Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
  625. if (!dst_node || dst_node->get_script().is_null()) {
  626. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  627. }
  628. Dictionary subst;
  629. subst["NodeName"] = node_name.to_pascal_case();
  630. subst["nodeName"] = node_name.to_camel_case();
  631. subst["node_name"] = node_name.to_snake_case();
  632. subst["SignalName"] = signal_name.to_pascal_case();
  633. subst["signalName"] = signal_name.to_camel_case();
  634. subst["signal_name"] = signal_name.to_snake_case();
  635. String dst_method = String(EDITOR_GET("interface/editors/default_signal_callback_name")).format(subst);
  636. ConnectDialog::ConnectionData cd;
  637. cd.source = selected_node;
  638. cd.signal = StringName(signal_name_ref);
  639. cd.target = dst_node;
  640. cd.method = StringName(dst_method);
  641. connect_dialog->popup_dialog(signal_name_ref);
  642. connect_dialog->init(cd);
  643. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  644. }
  645. /*
  646. * Open connection dialog with Connection data to EDIT an existing connection.
  647. */
  648. void ConnectionsDock::_open_connection_dialog(ConnectDialog::ConnectionData p_cd) {
  649. Node *src = Object::cast_to<Node>(p_cd.source);
  650. Node *dst = Object::cast_to<Node>(p_cd.target);
  651. if (src && dst) {
  652. const String &signal_name_ref = p_cd.signal;
  653. connect_dialog->set_title(TTR("Edit Connection:") + p_cd.signal);
  654. connect_dialog->popup_dialog(signal_name_ref);
  655. connect_dialog->init(p_cd, true);
  656. }
  657. }
  658. /*
  659. * Open slot method location in script editor.
  660. */
  661. void ConnectionsDock::_go_to_script(TreeItem &p_item) {
  662. if (_is_item_signal(p_item)) {
  663. return;
  664. }
  665. Connection connection = p_item.get_metadata(0);
  666. ConnectDialog::ConnectionData cd = connection;
  667. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck.
  668. if (!cd.target) {
  669. return;
  670. }
  671. Ref<Script> scr = cd.target->get_script();
  672. if (scr.is_null()) {
  673. return;
  674. }
  675. if (scr.is_valid() && ScriptEditor::get_singleton()->script_goto_method(scr, cd.method)) {
  676. EditorNode::get_singleton()->editor_select(EditorNode::EDITOR_SCRIPT);
  677. }
  678. }
  679. void ConnectionsDock::_handle_signal_menu_option(int p_option) {
  680. TreeItem *item = tree->get_selected();
  681. if (!item) {
  682. return;
  683. }
  684. switch (p_option) {
  685. case CONNECT: {
  686. _open_connection_dialog(*item);
  687. } break;
  688. case DISCONNECT_ALL: {
  689. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  690. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  691. disconnect_all_dialog->popup_centered();
  692. } break;
  693. case COPY_NAME: {
  694. DisplayServer::get_singleton()->clipboard_set(item->get_metadata(0).operator Dictionary()["name"]);
  695. } break;
  696. }
  697. }
  698. void ConnectionsDock::_handle_slot_menu_option(int p_option) {
  699. TreeItem *item = tree->get_selected();
  700. if (!item) {
  701. return;
  702. }
  703. switch (p_option) {
  704. case EDIT: {
  705. Connection connection = item->get_metadata(0);
  706. _open_connection_dialog(connection);
  707. } break;
  708. case GO_TO_SCRIPT: {
  709. _go_to_script(*item);
  710. } break;
  711. case DISCONNECT: {
  712. _disconnect(*item);
  713. update_tree();
  714. } break;
  715. }
  716. }
  717. void ConnectionsDock::_rmb_pressed(Vector2 p_position, MouseButton p_button) {
  718. if (p_button != MouseButton::RIGHT) {
  719. return;
  720. }
  721. TreeItem *item = tree->get_selected();
  722. if (!item) {
  723. return;
  724. }
  725. Vector2 screen_position = tree->get_screen_position() + p_position;
  726. if (_is_item_signal(*item)) {
  727. signal_menu->set_position(screen_position);
  728. signal_menu->reset_size();
  729. signal_menu->popup();
  730. } else {
  731. slot_menu->set_position(screen_position);
  732. slot_menu->reset_size();
  733. slot_menu->popup();
  734. }
  735. }
  736. void ConnectionsDock::_close() {
  737. hide();
  738. }
  739. void ConnectionsDock::_connect_pressed() {
  740. TreeItem *item = tree->get_selected();
  741. if (!item) {
  742. connect_button->set_disabled(true);
  743. return;
  744. }
  745. if (_is_item_signal(*item)) {
  746. _open_connection_dialog(*item);
  747. } else {
  748. _disconnect(*item);
  749. update_tree();
  750. }
  751. }
  752. void ConnectionsDock::_notification(int p_what) {
  753. switch (p_what) {
  754. case NOTIFICATION_ENTER_TREE:
  755. case NOTIFICATION_THEME_CHANGED: {
  756. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  757. } break;
  758. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  759. update_tree();
  760. } break;
  761. }
  762. }
  763. void ConnectionsDock::_bind_methods() {
  764. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  765. }
  766. void ConnectionsDock::set_undo_redo(Ref<EditorUndoRedoManager> p_undo_redo) {
  767. undo_redo = p_undo_redo;
  768. }
  769. void ConnectionsDock::set_node(Node *p_node) {
  770. selected_node = p_node;
  771. update_tree();
  772. }
  773. void ConnectionsDock::update_tree() {
  774. tree->clear();
  775. if (!selected_node) {
  776. return;
  777. }
  778. TreeItem *root = tree->create_item();
  779. List<MethodInfo> node_signals;
  780. selected_node->get_signal_list(&node_signals);
  781. bool did_script = false;
  782. StringName base = selected_node->get_class();
  783. while (base) {
  784. List<MethodInfo> node_signals2;
  785. Ref<Texture2D> icon;
  786. String name;
  787. if (!did_script) {
  788. // Get script signals (including signals from any base scripts).
  789. Ref<Script> scr = selected_node->get_script();
  790. if (scr.is_valid()) {
  791. scr->get_script_signal_list(&node_signals2);
  792. if (scr->get_path().is_resource_file()) {
  793. name = scr->get_path().get_file();
  794. } else {
  795. name = scr->get_class();
  796. }
  797. if (has_theme_icon(scr->get_class(), SNAME("EditorIcons"))) {
  798. icon = get_theme_icon(scr->get_class(), SNAME("EditorIcons"));
  799. }
  800. }
  801. } else {
  802. ClassDB::get_signal_list(base, &node_signals2, true);
  803. if (has_theme_icon(base, SNAME("EditorIcons"))) {
  804. icon = get_theme_icon(base, SNAME("EditorIcons"));
  805. }
  806. name = base;
  807. }
  808. if (!icon.is_valid()) {
  809. icon = get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  810. }
  811. TreeItem *section_item = nullptr;
  812. // Create subsections.
  813. if (node_signals2.size()) {
  814. section_item = tree->create_item(root);
  815. section_item->set_text(0, name);
  816. section_item->set_icon(0, icon);
  817. section_item->set_selectable(0, false);
  818. section_item->set_editable(0, false);
  819. section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  820. node_signals2.sort();
  821. }
  822. for (MethodInfo &mi : node_signals2) {
  823. StringName signal_name = mi.name;
  824. String signaldesc = "(";
  825. PackedStringArray argnames;
  826. String filter_text = search_box->get_text();
  827. if (!filter_text.is_subsequence_ofn(signal_name)) {
  828. continue;
  829. }
  830. if (mi.arguments.size()) {
  831. for (int i = 0; i < mi.arguments.size(); i++) {
  832. PropertyInfo &pi = mi.arguments[i];
  833. if (i > 0) {
  834. signaldesc += ", ";
  835. }
  836. String tname = "var";
  837. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  838. tname = pi.class_name.operator String();
  839. } else if (pi.type != Variant::NIL) {
  840. tname = Variant::get_type_name(pi.type);
  841. }
  842. signaldesc += (pi.name.is_empty() ? String("arg " + itos(i)) : pi.name) + ": " + tname;
  843. argnames.push_back(pi.name + ":" + tname);
  844. }
  845. }
  846. signaldesc += ")";
  847. // Create the children of the subsection - the actual list of signals.
  848. TreeItem *signal_item = tree->create_item(section_item);
  849. signal_item->set_text(0, String(signal_name) + signaldesc);
  850. Dictionary sinfo;
  851. sinfo["name"] = signal_name;
  852. sinfo["args"] = argnames;
  853. signal_item->set_metadata(0, sinfo);
  854. signal_item->set_icon(0, get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")));
  855. // Set tooltip with the signal's documentation.
  856. {
  857. String descr;
  858. bool found = false;
  859. HashMap<StringName, HashMap<StringName, String>>::Iterator G = descr_cache.find(base);
  860. if (G) {
  861. HashMap<StringName, String>::Iterator F = G->value.find(signal_name);
  862. if (F) {
  863. found = true;
  864. descr = F->value;
  865. }
  866. }
  867. if (!found) {
  868. DocTools *dd = EditorHelp::get_doc_data();
  869. HashMap<String, DocData::ClassDoc>::Iterator F = dd->class_list.find(base);
  870. while (F && descr.is_empty()) {
  871. for (int i = 0; i < F->value.signals.size(); i++) {
  872. if (F->value.signals[i].name == signal_name.operator String()) {
  873. descr = DTR(F->value.signals[i].description);
  874. break;
  875. }
  876. }
  877. if (!F->value.inherits.is_empty()) {
  878. F = dd->class_list.find(F->value.inherits);
  879. } else {
  880. break;
  881. }
  882. }
  883. descr_cache[base][signal_name] = descr;
  884. }
  885. // "::" separators used in make_custom_tooltip for formatting.
  886. signal_item->set_tooltip_text(0, String(signal_name) + "::" + signaldesc + "::" + descr);
  887. }
  888. // List existing connections.
  889. List<Object::Connection> existing_connections;
  890. selected_node->get_signal_connection_list(signal_name, &existing_connections);
  891. for (const Object::Connection &F : existing_connections) {
  892. Connection connection = F;
  893. if (!(connection.flags & CONNECT_PERSIST)) {
  894. continue;
  895. }
  896. ConnectDialog::ConnectionData cd = connection;
  897. Node *target = Object::cast_to<Node>(cd.target);
  898. if (!target) {
  899. continue;
  900. }
  901. String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()";
  902. if (cd.flags & CONNECT_DEFERRED) {
  903. path += " (deferred)";
  904. }
  905. if (cd.flags & CONNECT_ONE_SHOT) {
  906. path += " (one-shot)";
  907. }
  908. if (cd.unbinds > 0) {
  909. path += " unbinds(" + itos(cd.unbinds) + ")";
  910. } else if (!cd.binds.is_empty()) {
  911. path += " binds(";
  912. for (int i = 0; i < cd.binds.size(); i++) {
  913. if (i > 0) {
  914. path += ", ";
  915. }
  916. path += cd.binds[i].operator String();
  917. }
  918. path += ")";
  919. }
  920. TreeItem *connection_item = tree->create_item(signal_item);
  921. connection_item->set_text(0, path);
  922. connection_item->set_metadata(0, connection);
  923. connection_item->set_icon(0, get_theme_icon(SNAME("Slot"), SNAME("EditorIcons")));
  924. }
  925. }
  926. if (!did_script) {
  927. did_script = true;
  928. } else {
  929. base = ClassDB::get_parent_class(base);
  930. }
  931. }
  932. connect_button->set_text(TTR("Connect..."));
  933. connect_button->set_disabled(true);
  934. }
  935. ConnectionsDock::ConnectionsDock() {
  936. set_name(TTR("Signals"));
  937. VBoxContainer *vbc = this;
  938. search_box = memnew(LineEdit);
  939. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  940. search_box->set_placeholder(TTR("Filter Signals"));
  941. search_box->set_clear_button_enabled(true);
  942. search_box->connect("text_changed", callable_mp(this, &ConnectionsDock::_filter_changed));
  943. vbc->add_child(search_box);
  944. tree = memnew(ConnectionsDockTree);
  945. tree->set_columns(1);
  946. tree->set_select_mode(Tree::SELECT_ROW);
  947. tree->set_hide_root(true);
  948. vbc->add_child(tree);
  949. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  950. tree->set_allow_rmb_select(true);
  951. connect_button = memnew(Button);
  952. HBoxContainer *hb = memnew(HBoxContainer);
  953. vbc->add_child(hb);
  954. hb->add_spacer();
  955. hb->add_child(connect_button);
  956. connect_button->connect("pressed", callable_mp(this, &ConnectionsDock::_connect_pressed));
  957. connect_dialog = memnew(ConnectDialog);
  958. add_child(connect_dialog);
  959. disconnect_all_dialog = memnew(ConfirmationDialog);
  960. add_child(disconnect_all_dialog);
  961. disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
  962. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  963. signal_menu = memnew(PopupMenu);
  964. add_child(signal_menu);
  965. signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  966. signal_menu->add_item(TTR("Connect..."), CONNECT);
  967. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  968. signal_menu->add_item(TTR("Copy Name"), COPY_NAME);
  969. slot_menu = memnew(PopupMenu);
  970. add_child(slot_menu);
  971. slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  972. slot_menu->add_item(TTR("Edit..."), EDIT);
  973. slot_menu->add_item(TTR("Go to Method"), GO_TO_SCRIPT);
  974. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  975. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  976. tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
  977. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  978. tree->connect("item_mouse_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed));
  979. add_theme_constant_override("separation", 3 * EDSCALE);
  980. EDITOR_DEF("interface/editors/default_signal_callback_name", "_on_{node_name}_{signal_name}");
  981. }
  982. ConnectionsDock::~ConnectionsDock() {
  983. }