connections_dialog.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /**************************************************************************/
  2. /* connections_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/project_settings.h"
  32. #include "editor/doc_tools.h"
  33. #include "editor/editor_help.h"
  34. #include "editor/editor_inspector.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/gui/scene_tree_editor.h"
  40. #include "editor/node_dock.h"
  41. #include "editor/scene_tree_dock.h"
  42. #include "plugins/script_editor_plugin.h"
  43. #include "scene/gui/button.h"
  44. #include "scene/gui/check_box.h"
  45. #include "scene/gui/label.h"
  46. #include "scene/gui/line_edit.h"
  47. #include "scene/gui/option_button.h"
  48. #include "scene/gui/popup_menu.h"
  49. #include "scene/gui/spin_box.h"
  50. #include "scene/resources/packed_scene.h"
  51. static Node *_find_first_script(Node *p_root, Node *p_node) {
  52. if (p_node != p_root && p_node->get_owner() != p_root) {
  53. return nullptr;
  54. }
  55. if (!p_node->get_script().is_null()) {
  56. return p_node;
  57. }
  58. for (int i = 0; i < p_node->get_child_count(); i++) {
  59. Node *ret = _find_first_script(p_root, p_node->get_child(i));
  60. if (ret) {
  61. return ret;
  62. }
  63. }
  64. return nullptr;
  65. }
  66. class ConnectDialogBinds : public Object {
  67. GDCLASS(ConnectDialogBinds, Object);
  68. public:
  69. Vector<Variant> params;
  70. bool _set(const StringName &p_name, const Variant &p_value) {
  71. String name = p_name;
  72. if (name.begins_with("bind/argument_")) {
  73. int which = name.get_slice("_", 1).to_int() - 1;
  74. ERR_FAIL_INDEX_V(which, params.size(), false);
  75. params.write[which] = p_value;
  76. } else {
  77. return false;
  78. }
  79. return true;
  80. }
  81. bool _get(const StringName &p_name, Variant &r_ret) const {
  82. String name = p_name;
  83. if (name.begins_with("bind/argument_")) {
  84. int which = name.get_slice("_", 1).to_int() - 1;
  85. ERR_FAIL_INDEX_V(which, params.size(), false);
  86. r_ret = params[which];
  87. } else {
  88. return false;
  89. }
  90. return true;
  91. }
  92. void _get_property_list(List<PropertyInfo> *p_list) const {
  93. for (int i = 0; i < params.size(); i++) {
  94. p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));
  95. }
  96. }
  97. void notify_changed() {
  98. notify_property_list_changed();
  99. }
  100. ConnectDialogBinds() {
  101. }
  102. };
  103. /*
  104. * Signal automatically called by parent dialog.
  105. */
  106. void ConnectDialog::ok_pressed() {
  107. String method_name = dst_method->get_text();
  108. if (method_name.is_empty()) {
  109. error->set_text(TTR("Method in target node must be specified."));
  110. error->popup_centered();
  111. return;
  112. }
  113. if (!TS->is_valid_identifier(method_name.strip_edges())) {
  114. error->set_text(TTR("Method name must be a valid identifier."));
  115. error->popup_centered();
  116. return;
  117. }
  118. Node *target = tree->get_selected();
  119. if (!target) {
  120. return; // Nothing selected in the tree, not an error.
  121. }
  122. if (target->get_script().is_null()) {
  123. if (!target->has_method(method_name)) {
  124. error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
  125. error->popup_centered();
  126. return;
  127. }
  128. }
  129. emit_signal(SNAME("connected"));
  130. hide();
  131. }
  132. void ConnectDialog::_cancel_pressed() {
  133. hide();
  134. }
  135. void ConnectDialog::_item_activated() {
  136. _ok_pressed(); // From AcceptDialog.
  137. }
  138. void ConnectDialog::_text_submitted(const String &p_text) {
  139. _ok_pressed(); // From AcceptDialog.
  140. }
  141. /*
  142. * Called each time a target node is selected within the target node tree.
  143. */
  144. void ConnectDialog::_tree_node_selected() {
  145. Node *current = tree->get_selected();
  146. if (!current) {
  147. return;
  148. }
  149. dst_path = source->get_path_to(current);
  150. if (!edit_mode) {
  151. set_dst_method(generate_method_callback_name(source, signal, current));
  152. }
  153. _update_method_tree();
  154. _update_ok_enabled();
  155. }
  156. void ConnectDialog::_focus_currently_connected() {
  157. tree->set_selected(source);
  158. }
  159. void ConnectDialog::_unbind_count_changed(double p_count) {
  160. for (Control *control : bind_controls) {
  161. BaseButton *b = Object::cast_to<BaseButton>(control);
  162. if (b) {
  163. b->set_disabled(p_count > 0);
  164. }
  165. EditorInspector *e = Object::cast_to<EditorInspector>(control);
  166. if (e) {
  167. e->set_read_only(p_count > 0);
  168. }
  169. }
  170. }
  171. void ConnectDialog::_method_selected() {
  172. TreeItem *selected_item = method_tree->get_selected();
  173. dst_method->set_text(selected_item->get_metadata(0));
  174. }
  175. /*
  176. * Adds a new parameter bind to connection.
  177. */
  178. void ConnectDialog::_add_bind() {
  179. Variant::Type type = (Variant::Type)type_list->get_item_id(type_list->get_selected());
  180. Variant value;
  181. Callable::CallError err;
  182. Variant::construct(type, value, nullptr, 0, err);
  183. cdbinds->params.push_back(value);
  184. cdbinds->notify_changed();
  185. }
  186. /*
  187. * Remove parameter bind from connection.
  188. */
  189. void ConnectDialog::_remove_bind() {
  190. String st = bind_editor->get_selected_path();
  191. if (st.is_empty()) {
  192. return;
  193. }
  194. int idx = st.get_slice("/", 1).to_int() - 1;
  195. ERR_FAIL_INDEX(idx, cdbinds->params.size());
  196. cdbinds->params.remove_at(idx);
  197. cdbinds->notify_changed();
  198. }
  199. /*
  200. * Automatically generates a name for the callback method.
  201. */
  202. StringName ConnectDialog::generate_method_callback_name(Node *p_source, String p_signal_name, Node *p_target) {
  203. String node_name = p_source->get_name();
  204. for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.
  205. char32_t c = node_name[i];
  206. if ((i == 0 && !is_unicode_identifier_start(c)) || (i > 0 && !is_unicode_identifier_continue(c))) {
  207. if (c == ' ') {
  208. // Replace spaces with underlines.
  209. c = '_';
  210. } else {
  211. // Remove any other characters.
  212. node_name.remove_at(i);
  213. i--;
  214. continue;
  215. }
  216. }
  217. node_name[i] = c;
  218. }
  219. Dictionary subst;
  220. subst["NodeName"] = node_name.to_pascal_case();
  221. subst["nodeName"] = node_name.to_camel_case();
  222. subst["node_name"] = node_name.to_snake_case();
  223. subst["SignalName"] = p_signal_name.to_pascal_case();
  224. subst["signalName"] = p_signal_name.to_camel_case();
  225. subst["signal_name"] = p_signal_name.to_snake_case();
  226. String dst_method;
  227. if (p_source == p_target) {
  228. dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_to_self_name")).format(subst);
  229. } else {
  230. dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_name")).format(subst);
  231. }
  232. return dst_method;
  233. }
  234. void ConnectDialog::_create_method_tree_items(const List<MethodInfo> &p_methods, TreeItem *p_parent_item) {
  235. for (const MethodInfo &mi : p_methods) {
  236. TreeItem *method_item = method_tree->create_item(p_parent_item);
  237. method_item->set_text(0, get_signature(mi));
  238. method_item->set_metadata(0, mi.name);
  239. }
  240. }
  241. List<MethodInfo> ConnectDialog::_filter_method_list(const List<MethodInfo> &p_methods, const MethodInfo &p_signal, const String &p_search_string) const {
  242. bool check_signal = compatible_methods_only->is_pressed();
  243. List<MethodInfo> ret;
  244. for (const MethodInfo &mi : p_methods) {
  245. if (!p_search_string.is_empty() && !mi.name.contains(p_search_string)) {
  246. continue;
  247. }
  248. if (check_signal) {
  249. if (mi.arguments.size() != p_signal.arguments.size()) {
  250. continue;
  251. }
  252. bool type_mismatch = false;
  253. const List<PropertyInfo>::Element *E = p_signal.arguments.front();
  254. for (const List<PropertyInfo>::Element *F = mi.arguments.front(); F; F = F->next(), E = E->next()) {
  255. Variant::Type stype = E->get().type;
  256. Variant::Type mtype = F->get().type;
  257. if (stype != Variant::NIL && mtype != Variant::NIL && stype != mtype) {
  258. type_mismatch = true;
  259. break;
  260. }
  261. if (stype == Variant::OBJECT && mtype == Variant::OBJECT && !ClassDB::is_parent_class(E->get().class_name, F->get().class_name)) {
  262. type_mismatch = true;
  263. break;
  264. }
  265. }
  266. if (type_mismatch) {
  267. continue;
  268. }
  269. }
  270. ret.push_back(mi);
  271. }
  272. return ret;
  273. }
  274. void ConnectDialog::_update_method_tree() {
  275. method_tree->clear();
  276. Color disabled_color = get_theme_color(SNAME("accent_color"), SNAME("Editor")) * 0.7;
  277. String search_string = method_search->get_text();
  278. Node *target = tree->get_selected();
  279. if (!target) {
  280. return;
  281. }
  282. MethodInfo signal_info;
  283. if (compatible_methods_only->is_pressed()) {
  284. List<MethodInfo> signals;
  285. source->get_signal_list(&signals);
  286. for (const MethodInfo &mi : signals) {
  287. if (mi.name == signal) {
  288. signal_info = mi;
  289. break;
  290. }
  291. }
  292. }
  293. TreeItem *root_item = method_tree->create_item();
  294. root_item->set_text(0, TTR("Methods"));
  295. root_item->set_selectable(0, false);
  296. // If a script is attached, get methods from it.
  297. ScriptInstance *si = target->get_script_instance();
  298. if (si) {
  299. if (si->get_script()->is_built_in()) {
  300. si->get_script()->reload();
  301. }
  302. List<MethodInfo> methods;
  303. si->get_method_list(&methods);
  304. methods = _filter_method_list(methods, signal_info, search_string);
  305. if (!methods.is_empty()) {
  306. TreeItem *si_item = method_tree->create_item(root_item);
  307. si_item->set_text(0, TTR("Attached Script"));
  308. si_item->set_icon(0, get_theme_icon(SNAME("Script"), SNAME("EditorIcons")));
  309. si_item->set_selectable(0, false);
  310. _create_method_tree_items(methods, si_item);
  311. }
  312. }
  313. if (script_methods_only->is_pressed()) {
  314. empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
  315. return;
  316. }
  317. // Get methods from each class in the hierarchy.
  318. StringName current_class = target->get_class_name();
  319. do {
  320. TreeItem *class_item = method_tree->create_item(root_item);
  321. class_item->set_text(0, current_class);
  322. Ref<Texture2D> icon = get_theme_icon(SNAME("Node"), SNAME("EditorIcons"));
  323. if (has_theme_icon(current_class, SNAME("EditorIcons"))) {
  324. icon = get_theme_icon(current_class, SNAME("EditorIcons"));
  325. }
  326. class_item->set_icon(0, icon);
  327. class_item->set_selectable(0, false);
  328. List<MethodInfo> methods;
  329. ClassDB::get_method_list(current_class, &methods, true);
  330. methods = _filter_method_list(methods, signal_info, search_string);
  331. if (methods.is_empty()) {
  332. class_item->set_custom_color(0, disabled_color);
  333. } else {
  334. _create_method_tree_items(methods, class_item);
  335. }
  336. current_class = ClassDB::get_parent_class_nocheck(current_class);
  337. } while (current_class != StringName());
  338. empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
  339. }
  340. void ConnectDialog::_method_check_button_pressed(const CheckButton *p_button) {
  341. if (p_button == script_methods_only) {
  342. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_script_methods_only", p_button->is_pressed());
  343. } else if (p_button == compatible_methods_only) {
  344. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_compatible_methods_only", p_button->is_pressed());
  345. }
  346. _update_method_tree();
  347. }
  348. void ConnectDialog::_open_method_popup() {
  349. method_popup->popup_centered();
  350. method_search->clear();
  351. method_search->grab_focus();
  352. }
  353. /*
  354. * Enables or disables the connect button. The connect button is enabled if a
  355. * node is selected and valid in the selected mode.
  356. */
  357. void ConnectDialog::_update_ok_enabled() {
  358. Node *target = tree->get_selected();
  359. if (target == nullptr) {
  360. get_ok_button()->set_disabled(true);
  361. return;
  362. }
  363. if (dst_method->get_text().is_empty()) {
  364. get_ok_button()->set_disabled(true);
  365. return;
  366. }
  367. get_ok_button()->set_disabled(false);
  368. }
  369. void ConnectDialog::_notification(int p_what) {
  370. switch (p_what) {
  371. case NOTIFICATION_ENTER_TREE: {
  372. bind_editor->edit(cdbinds);
  373. [[fallthrough]];
  374. }
  375. case NOTIFICATION_THEME_CHANGED: {
  376. for (int i = 0; i < type_list->get_item_count(); i++) {
  377. String type_name = Variant::get_type_name((Variant::Type)type_list->get_item_id(i));
  378. type_list->set_item_icon(i, get_theme_icon(type_name, SNAME("EditorIcons")));
  379. }
  380. Ref<StyleBox> style = get_theme_stylebox("normal", "LineEdit")->duplicate();
  381. if (style.is_valid()) {
  382. style->set_content_margin(SIDE_TOP, style->get_content_margin(SIDE_TOP) + 1.0);
  383. from_signal->add_theme_style_override("normal", style);
  384. }
  385. method_search->set_right_icon(get_theme_icon("Search", "EditorIcons"));
  386. open_method_tree->set_icon(get_theme_icon("Edit", "EditorIcons"));
  387. } break;
  388. }
  389. }
  390. void ConnectDialog::_bind_methods() {
  391. ADD_SIGNAL(MethodInfo("connected"));
  392. }
  393. Node *ConnectDialog::get_source() const {
  394. return source;
  395. }
  396. ConnectDialog::ConnectionData ConnectDialog::get_source_connection_data() const {
  397. return source_connection_data;
  398. }
  399. StringName ConnectDialog::get_signal_name() const {
  400. return signal;
  401. }
  402. PackedStringArray ConnectDialog::get_signal_args() const {
  403. return signal_args;
  404. }
  405. NodePath ConnectDialog::get_dst_path() const {
  406. return dst_path;
  407. }
  408. void ConnectDialog::set_dst_node(Node *p_node) {
  409. tree->set_selected(p_node);
  410. }
  411. StringName ConnectDialog::get_dst_method_name() const {
  412. String txt = dst_method->get_text();
  413. if (txt.contains("(")) {
  414. txt = txt.left(txt.find("(")).strip_edges();
  415. }
  416. return txt;
  417. }
  418. void ConnectDialog::set_dst_method(const StringName &p_method) {
  419. dst_method->set_text(p_method);
  420. }
  421. int ConnectDialog::get_unbinds() const {
  422. return int(unbind_count->get_value());
  423. }
  424. Vector<Variant> ConnectDialog::get_binds() const {
  425. return cdbinds->params;
  426. }
  427. String ConnectDialog::get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names) {
  428. PackedStringArray signature;
  429. signature.append(p_method.name);
  430. signature.append("(");
  431. for (int i = 0; i < p_method.arguments.size(); i++) {
  432. if (i > 0) {
  433. signature.append(", ");
  434. }
  435. const PropertyInfo &pi = p_method.arguments[i];
  436. String tname = "var";
  437. if (pi.type == Variant::OBJECT && pi.class_name != StringName()) {
  438. tname = pi.class_name.operator String();
  439. } else if (pi.type != Variant::NIL) {
  440. tname = Variant::get_type_name(pi.type);
  441. }
  442. signature.append((pi.name.is_empty() ? String("arg " + itos(i)) : pi.name) + ": " + tname);
  443. if (r_arg_names) {
  444. r_arg_names->push_back(pi.name + ":" + tname);
  445. }
  446. }
  447. signature.append(")");
  448. return String().join(signature);
  449. }
  450. bool ConnectDialog::get_deferred() const {
  451. return deferred->is_pressed();
  452. }
  453. bool ConnectDialog::get_one_shot() const {
  454. return one_shot->is_pressed();
  455. }
  456. /*
  457. * Returns true if ConnectDialog is being used to edit an existing connection.
  458. */
  459. bool ConnectDialog::is_editing() const {
  460. return edit_mode;
  461. }
  462. /*
  463. * Initialize ConnectDialog and populate fields with expected data.
  464. * If creating a connection from scratch, sensible defaults are used.
  465. * If editing an existing connection, previous data is retained.
  466. */
  467. void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit) {
  468. set_hide_on_ok(false);
  469. source = static_cast<Node *>(p_cd.source);
  470. signal = p_cd.signal;
  471. signal_args = p_signal_args;
  472. tree->set_selected(nullptr);
  473. tree->set_marked(source, true);
  474. if (p_cd.target) {
  475. set_dst_node(static_cast<Node *>(p_cd.target));
  476. set_dst_method(p_cd.method);
  477. }
  478. _update_ok_enabled();
  479. bool b_deferred = (p_cd.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
  480. bool b_oneshot = (p_cd.flags & CONNECT_ONE_SHOT) == CONNECT_ONE_SHOT;
  481. deferred->set_pressed(b_deferred);
  482. one_shot->set_pressed(b_oneshot);
  483. unbind_count->set_max(p_signal_args.size());
  484. unbind_count->set_value(p_cd.unbinds);
  485. _unbind_count_changed(p_cd.unbinds);
  486. cdbinds->params.clear();
  487. cdbinds->params = p_cd.binds;
  488. cdbinds->notify_changed();
  489. edit_mode = p_edit;
  490. source_connection_data = p_cd;
  491. }
  492. void ConnectDialog::popup_dialog(const String p_for_signal) {
  493. from_signal->set_text(p_for_signal);
  494. error_label->add_theme_color_override("font_color", error_label->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  495. filter_nodes->clear();
  496. if (!advanced->is_pressed()) {
  497. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  498. }
  499. if (first_popup) {
  500. first_popup = false;
  501. _advanced_pressed();
  502. }
  503. popup_centered();
  504. }
  505. void ConnectDialog::_advanced_pressed() {
  506. if (advanced->is_pressed()) {
  507. connect_to_label->set_text(TTR("Connect to Node:"));
  508. tree->set_connect_to_script_mode(false);
  509. vbc_right->show();
  510. error_label->hide();
  511. } else {
  512. reset_size();
  513. connect_to_label->set_text(TTR("Connect to Script:"));
  514. tree->set_connect_to_script_mode(true);
  515. vbc_right->hide();
  516. error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
  517. }
  518. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "use_advanced_connections", advanced->is_pressed());
  519. popup_centered();
  520. }
  521. ConnectDialog::ConnectDialog() {
  522. set_min_size(Size2(0, 500) * EDSCALE);
  523. HBoxContainer *main_hb = memnew(HBoxContainer);
  524. add_child(main_hb);
  525. VBoxContainer *vbc_left = memnew(VBoxContainer);
  526. main_hb->add_child(vbc_left);
  527. vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  528. vbc_left->set_custom_minimum_size(Vector2(400 * EDSCALE, 0));
  529. from_signal = memnew(LineEdit);
  530. vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
  531. from_signal->set_editable(false);
  532. tree = memnew(SceneTreeEditor(false));
  533. tree->set_connecting_signal(true);
  534. tree->set_show_enabled_subscene(true);
  535. tree->set_v_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
  536. tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
  537. tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
  538. tree->set_connect_to_script_mode(true);
  539. HBoxContainer *hbc_filter = memnew(HBoxContainer);
  540. filter_nodes = memnew(LineEdit);
  541. hbc_filter->add_child(filter_nodes);
  542. filter_nodes->set_h_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
  543. filter_nodes->set_placeholder(TTR("Filter Nodes"));
  544. filter_nodes->set_clear_button_enabled(true);
  545. filter_nodes->connect("text_changed", callable_mp(tree, &SceneTreeEditor::set_filter));
  546. Button *focus_current = memnew(Button);
  547. hbc_filter->add_child(focus_current);
  548. focus_current->set_text(TTR("Go to Source"));
  549. focus_current->connect("pressed", callable_mp(this, &ConnectDialog::_focus_currently_connected));
  550. Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), hbc_filter, false);
  551. connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
  552. vbc_left->add_child(tree);
  553. error_label = memnew(Label);
  554. error_label->set_text(TTR("Scene does not contain any script."));
  555. vbc_left->add_child(error_label);
  556. error_label->hide();
  557. method_popup = memnew(AcceptDialog);
  558. method_popup->set_title(TTR("Select Method"));
  559. method_popup->set_min_size(Vector2(400, 600) * EDSCALE);
  560. add_child(method_popup);
  561. VBoxContainer *method_vbc = memnew(VBoxContainer);
  562. method_popup->add_child(method_vbc);
  563. method_search = memnew(LineEdit);
  564. method_vbc->add_child(method_search);
  565. method_search->set_placeholder(TTR("Filter Methods"));
  566. method_search->set_clear_button_enabled(true);
  567. method_search->connect("text_changed", callable_mp(this, &ConnectDialog::_update_method_tree).unbind(1));
  568. method_tree = memnew(Tree);
  569. method_vbc->add_child(method_tree);
  570. method_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  571. method_tree->set_hide_root(true);
  572. method_tree->connect("item_selected", callable_mp(this, &ConnectDialog::_method_selected));
  573. method_tree->connect("item_activated", callable_mp((Window *)method_popup, &Window::hide));
  574. empty_tree_label = memnew(Label(TTR("No method found matching given filters.")));
  575. method_popup->add_child(empty_tree_label);
  576. empty_tree_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  577. empty_tree_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  578. empty_tree_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
  579. script_methods_only = memnew(CheckButton(TTR("Script Methods Only")));
  580. method_vbc->add_child(script_methods_only);
  581. script_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
  582. script_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_script_methods_only", true));
  583. script_methods_only->connect("pressed", callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(script_methods_only));
  584. compatible_methods_only = memnew(CheckButton(TTR("Compatible Methods Only")));
  585. method_vbc->add_child(compatible_methods_only);
  586. compatible_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
  587. compatible_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_compatible_methods_only", true));
  588. compatible_methods_only->connect("pressed", callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(compatible_methods_only));
  589. vbc_right = memnew(VBoxContainer);
  590. main_hb->add_child(vbc_right);
  591. vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  592. vbc_right->set_custom_minimum_size(Vector2(150 * EDSCALE, 0));
  593. vbc_right->hide();
  594. HBoxContainer *add_bind_hb = memnew(HBoxContainer);
  595. type_list = memnew(OptionButton);
  596. type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  597. add_bind_hb->add_child(type_list);
  598. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  599. if (i == Variant::NIL || i == Variant::OBJECT || i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  600. // These types can't be constructed or serialized properly, so skip them.
  601. continue;
  602. }
  603. type_list->add_item(Variant::get_type_name(Variant::Type(i)), i);
  604. }
  605. bind_controls.push_back(type_list);
  606. Button *add_bind = memnew(Button);
  607. add_bind->set_text(TTR("Add"));
  608. add_bind_hb->add_child(add_bind);
  609. add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind));
  610. bind_controls.push_back(add_bind);
  611. Button *del_bind = memnew(Button);
  612. del_bind->set_text(TTR("Remove"));
  613. add_bind_hb->add_child(del_bind);
  614. del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind));
  615. bind_controls.push_back(del_bind);
  616. vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
  617. bind_editor = memnew(EditorInspector);
  618. bind_controls.push_back(bind_editor);
  619. vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
  620. unbind_count = memnew(SpinBox);
  621. unbind_count->set_tooltip_text(TTR("Allows to drop arguments sent by signal emitter."));
  622. unbind_count->connect("value_changed", callable_mp(this, &ConnectDialog::_unbind_count_changed));
  623. vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);
  624. HBoxContainer *hbc_method = memnew(HBoxContainer);
  625. vbc_left->add_margin_child(TTR("Receiver Method:"), hbc_method);
  626. dst_method = memnew(LineEdit);
  627. dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  628. dst_method->connect("text_changed", callable_mp(method_tree, &Tree::deselect_all).unbind(1));
  629. dst_method->connect("text_submitted", callable_mp(this, &ConnectDialog::_text_submitted));
  630. hbc_method->add_child(dst_method);
  631. open_method_tree = memnew(Button);
  632. hbc_method->add_child(open_method_tree);
  633. open_method_tree->set_text("Pick");
  634. open_method_tree->connect("pressed", callable_mp(this, &ConnectDialog::_open_method_popup));
  635. advanced = memnew(CheckButton(TTR("Advanced")));
  636. vbc_left->add_child(advanced);
  637. advanced->set_h_size_flags(Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND);
  638. advanced->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "use_advanced_connections", false));
  639. advanced->connect("pressed", callable_mp(this, &ConnectDialog::_advanced_pressed));
  640. HBoxContainer *hbox = memnew(HBoxContainer);
  641. vbc_right->add_child(hbox);
  642. deferred = memnew(CheckBox);
  643. deferred->set_h_size_flags(0);
  644. deferred->set_text(TTR("Deferred"));
  645. deferred->set_tooltip_text(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
  646. hbox->add_child(deferred);
  647. one_shot = memnew(CheckBox);
  648. one_shot->set_h_size_flags(0);
  649. one_shot->set_text(TTR("One Shot"));
  650. one_shot->set_tooltip_text(TTR("Disconnects the signal after its first emission."));
  651. hbox->add_child(one_shot);
  652. cdbinds = memnew(ConnectDialogBinds);
  653. error = memnew(AcceptDialog);
  654. add_child(error);
  655. error->set_title(TTR("Cannot connect signal"));
  656. error->set_ok_button_text(TTR("Close"));
  657. set_ok_button_text(TTR("Connect"));
  658. }
  659. ConnectDialog::~ConnectDialog() {
  660. memdelete(cdbinds);
  661. }
  662. //////////////////////////////////////////
  663. // Originally copied and adapted from EditorProperty, try to keep style in sync.
  664. Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
  665. EditorHelpBit *help_bit = memnew(EditorHelpBit);
  666. help_bit->get_rich_text()->set_custom_minimum_size(Size2(360 * EDSCALE, 1));
  667. // p_text is expected to be something like this:
  668. // "gui_input::(event: InputEvent)::<Signal description>"
  669. // with the latter being possibly empty.
  670. PackedStringArray slices = p_text.split("::", false);
  671. if (slices.size() < 2) {
  672. // Shouldn't happen here, but just in case pass the text along.
  673. help_bit->set_text(p_text);
  674. return help_bit;
  675. }
  676. String text = TTR("Signal:") + " [u][b]" + slices[0] + "[/b][/u]";
  677. text += slices[1].strip_edges() + "\n";
  678. if (slices.size() > 2) {
  679. text += slices[2].strip_edges();
  680. } else {
  681. text += "[i]" + TTR("No description.") + "[/i]";
  682. }
  683. help_bit->set_text(text);
  684. return help_bit;
  685. }
  686. struct _ConnectionsDockMethodInfoSort {
  687. _FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
  688. return a.name < b.name;
  689. }
  690. };
  691. void ConnectionsDock::_filter_changed(const String &p_text) {
  692. update_tree();
  693. }
  694. /*
  695. * Post-ConnectDialog callback for creating/editing connections.
  696. * Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
  697. */
  698. void ConnectionsDock::_make_or_edit_connection() {
  699. NodePath dst_path = connect_dialog->get_dst_path();
  700. Node *target = selected_node->get_node(dst_path);
  701. ERR_FAIL_COND(!target);
  702. ConnectDialog::ConnectionData cd;
  703. cd.source = connect_dialog->get_source();
  704. cd.target = target;
  705. cd.signal = connect_dialog->get_signal_name();
  706. cd.method = connect_dialog->get_dst_method_name();
  707. cd.unbinds = connect_dialog->get_unbinds();
  708. if (cd.unbinds == 0) {
  709. cd.binds = connect_dialog->get_binds();
  710. }
  711. bool b_deferred = connect_dialog->get_deferred();
  712. bool b_oneshot = connect_dialog->get_one_shot();
  713. cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONE_SHOT : 0);
  714. // Conditions to add function: must have a script and must not have the method already
  715. // (in the class, the script itself, or inherited).
  716. bool add_script_function = false;
  717. Ref<Script> scr = target->get_script();
  718. if (!scr.is_null() && !ClassDB::has_method(target->get_class(), cd.method)) {
  719. // There is a chance that the method is inherited from another script.
  720. bool found_inherited_function = false;
  721. Ref<Script> inherited_scr = scr->get_base_script();
  722. while (!inherited_scr.is_null()) {
  723. int line = inherited_scr->get_language()->find_function(cd.method, inherited_scr->get_source_code());
  724. if (line != -1) {
  725. found_inherited_function = true;
  726. break;
  727. }
  728. inherited_scr = inherited_scr->get_base_script();
  729. }
  730. add_script_function = !found_inherited_function;
  731. }
  732. if (connect_dialog->is_editing()) {
  733. _disconnect(connect_dialog->get_source_connection_data());
  734. _connect(cd);
  735. } else {
  736. _connect(cd);
  737. }
  738. if (add_script_function) {
  739. PackedStringArray script_function_args = connect_dialog->get_signal_args();
  740. script_function_args.resize(script_function_args.size() - cd.unbinds);
  741. for (int i = 0; i < cd.binds.size(); i++) {
  742. script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cd.binds[i].get_type()));
  743. }
  744. EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args);
  745. hide();
  746. }
  747. update_tree();
  748. }
  749. /*
  750. * Creates single connection w/ undo-redo functionality.
  751. */
  752. void ConnectionsDock::_connect(const ConnectDialog::ConnectionData &p_cd) {
  753. Node *source = Object::cast_to<Node>(p_cd.source);
  754. Node *target = Object::cast_to<Node>(p_cd.target);
  755. if (!source || !target) {
  756. return;
  757. }
  758. Callable callable = p_cd.get_callable();
  759. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  760. undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));
  761. undo_redo->add_do_method(source, "connect", p_cd.signal, callable, p_cd.flags);
  762. undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);
  763. undo_redo->add_do_method(this, "update_tree");
  764. undo_redo->add_undo_method(this, "update_tree");
  765. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  766. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  767. undo_redo->commit_action();
  768. }
  769. /*
  770. * Break single connection w/ undo-redo functionality.
  771. */
  772. void ConnectionsDock::_disconnect(const ConnectDialog::ConnectionData &p_cd) {
  773. ERR_FAIL_COND(p_cd.source != selected_node); // Shouldn't happen but... Bugcheck.
  774. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  775. undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), p_cd.signal, p_cd.method));
  776. Callable callable = p_cd.get_callable();
  777. undo_redo->add_do_method(selected_node, "disconnect", p_cd.signal, callable);
  778. undo_redo->add_undo_method(selected_node, "connect", p_cd.signal, callable, p_cd.flags);
  779. undo_redo->add_do_method(this, "update_tree");
  780. undo_redo->add_undo_method(this, "update_tree");
  781. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
  782. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  783. undo_redo->commit_action();
  784. }
  785. /*
  786. * Break all connections of currently selected signal.
  787. * Can undo-redo as a single action.
  788. */
  789. void ConnectionsDock::_disconnect_all() {
  790. TreeItem *item = tree->get_selected();
  791. if (!_is_item_signal(*item)) {
  792. return;
  793. }
  794. TreeItem *child = item->get_first_child();
  795. String signal_name = item->get_metadata(0).operator Dictionary()["name"];
  796. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  797. undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));
  798. while (child) {
  799. Connection connection = child->get_metadata(0);
  800. if (!_is_connection_inherited(connection)) {
  801. ConnectDialog::ConnectionData cd = connection;
  802. undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable());
  803. undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.flags);
  804. }
  805. child = child->get_next();
  806. }
  807. undo_redo->add_do_method(this, "update_tree");
  808. undo_redo->add_undo_method(this, "update_tree");
  809. undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  810. undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
  811. undo_redo->commit_action();
  812. }
  813. void ConnectionsDock::_tree_item_selected() {
  814. TreeItem *item = tree->get_selected();
  815. if (!item) { // Unlikely. Disable button just in case.
  816. connect_button->set_text(TTR("Connect..."));
  817. connect_button->set_disabled(true);
  818. } else if (_is_item_signal(*item)) {
  819. connect_button->set_text(TTR("Connect..."));
  820. connect_button->set_disabled(false);
  821. } else {
  822. connect_button->set_text(TTR("Disconnect"));
  823. connect_button->set_disabled(false);
  824. }
  825. }
  826. void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
  827. TreeItem *item = tree->get_selected();
  828. if (!item) {
  829. return;
  830. }
  831. if (_is_item_signal(*item)) {
  832. _open_connection_dialog(*item);
  833. } else {
  834. _go_to_script(*item);
  835. }
  836. }
  837. bool ConnectionsDock::_is_item_signal(TreeItem &p_item) {
  838. return (p_item.get_parent() == tree->get_root() || p_item.get_parent()->get_parent() == tree->get_root());
  839. }
  840. bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {
  841. return bool(p_connection.flags & CONNECT_INHERITED);
  842. }
  843. /*
  844. * Open connection dialog with TreeItem data to CREATE a brand-new connection.
  845. */
  846. void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
  847. Dictionary sinfo = p_item.get_metadata(0);
  848. String signal_name = sinfo["name"];
  849. PackedStringArray signal_args = sinfo["args"];
  850. Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
  851. if (!dst_node || dst_node->get_script().is_null()) {
  852. dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
  853. }
  854. ConnectDialog::ConnectionData cd;
  855. cd.source = selected_node;
  856. cd.signal = StringName(signal_name);
  857. cd.target = dst_node;
  858. cd.method = ConnectDialog::generate_method_callback_name(cd.source, signal_name, cd.target);
  859. connect_dialog->popup_dialog(signal_name + "(" + String(", ").join(signal_args) + ")");
  860. connect_dialog->init(cd, signal_args);
  861. connect_dialog->set_title(TTR("Connect a Signal to a Method"));
  862. }
  863. /*
  864. * Open connection dialog with Connection data to EDIT an existing connection.
  865. */
  866. void ConnectionsDock::_open_edit_connection_dialog(TreeItem &p_item) {
  867. TreeItem *signal_item = p_item.get_parent();
  868. ERR_FAIL_COND(!signal_item);
  869. Connection connection = p_item.get_metadata(0);
  870. ConnectDialog::ConnectionData cd = connection;
  871. Node *src = Object::cast_to<Node>(cd.source);
  872. Node *dst = Object::cast_to<Node>(cd.target);
  873. if (src && dst) {
  874. const String &signal_name_ref = cd.signal;
  875. PackedStringArray signal_args = signal_item->get_metadata(0).operator Dictionary()["args"];
  876. connect_dialog->set_title(vformat(TTR("Edit Connection: '%s'"), cd.signal));
  877. connect_dialog->popup_dialog(signal_name_ref);
  878. connect_dialog->init(cd, signal_args, true);
  879. }
  880. }
  881. /*
  882. * Open slot method location in script editor.
  883. */
  884. void ConnectionsDock::_go_to_script(TreeItem &p_item) {
  885. if (_is_item_signal(p_item)) {
  886. return;
  887. }
  888. Connection connection = p_item.get_metadata(0);
  889. ConnectDialog::ConnectionData cd = connection;
  890. ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck.
  891. if (!cd.target) {
  892. return;
  893. }
  894. Ref<Script> scr = cd.target->get_script();
  895. if (scr.is_null()) {
  896. return;
  897. }
  898. if (scr.is_valid() && ScriptEditor::get_singleton()->script_goto_method(scr, cd.method)) {
  899. EditorNode::get_singleton()->editor_select(EditorNode::EDITOR_SCRIPT);
  900. }
  901. }
  902. void ConnectionsDock::_handle_signal_menu_option(int p_option) {
  903. TreeItem *item = tree->get_selected();
  904. if (!item) {
  905. return;
  906. }
  907. switch (p_option) {
  908. case CONNECT: {
  909. _open_connection_dialog(*item);
  910. } break;
  911. case DISCONNECT_ALL: {
  912. StringName signal_name = item->get_metadata(0).operator Dictionary()["name"];
  913. disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), signal_name));
  914. disconnect_all_dialog->popup_centered();
  915. } break;
  916. case COPY_NAME: {
  917. DisplayServer::get_singleton()->clipboard_set(item->get_metadata(0).operator Dictionary()["name"]);
  918. } break;
  919. }
  920. }
  921. void ConnectionsDock::_signal_menu_about_to_popup() {
  922. TreeItem *signal_item = tree->get_selected();
  923. bool disable_disconnect_all = true;
  924. for (int i = 0; i < signal_item->get_child_count(); i++) {
  925. if (!signal_item->get_child(i)->has_meta("_inherited_connection")) {
  926. disable_disconnect_all = false;
  927. }
  928. }
  929. signal_menu->set_item_disabled(slot_menu->get_item_index(DISCONNECT_ALL), disable_disconnect_all);
  930. }
  931. void ConnectionsDock::_handle_slot_menu_option(int p_option) {
  932. TreeItem *item = tree->get_selected();
  933. if (!item) {
  934. return;
  935. }
  936. switch (p_option) {
  937. case EDIT: {
  938. _open_edit_connection_dialog(*item);
  939. } break;
  940. case GO_TO_SCRIPT: {
  941. _go_to_script(*item);
  942. } break;
  943. case DISCONNECT: {
  944. Connection connection = item->get_metadata(0);
  945. _disconnect(connection);
  946. update_tree();
  947. } break;
  948. }
  949. }
  950. void ConnectionsDock::_slot_menu_about_to_popup() {
  951. bool connection_is_inherited = tree->get_selected()->has_meta("_inherited_connection");
  952. slot_menu->set_item_disabled(slot_menu->get_item_index(EDIT), connection_is_inherited);
  953. slot_menu->set_item_disabled(slot_menu->get_item_index(DISCONNECT), connection_is_inherited);
  954. }
  955. void ConnectionsDock::_rmb_pressed(Vector2 p_position, MouseButton p_button) {
  956. if (p_button != MouseButton::RIGHT) {
  957. return;
  958. }
  959. TreeItem *item = tree->get_selected();
  960. if (!item) {
  961. return;
  962. }
  963. Vector2 screen_position = tree->get_screen_position() + p_position;
  964. if (_is_item_signal(*item)) {
  965. signal_menu->set_position(screen_position);
  966. signal_menu->reset_size();
  967. signal_menu->popup();
  968. } else {
  969. slot_menu->set_position(screen_position);
  970. slot_menu->reset_size();
  971. slot_menu->popup();
  972. }
  973. }
  974. void ConnectionsDock::_close() {
  975. hide();
  976. }
  977. void ConnectionsDock::_connect_pressed() {
  978. TreeItem *item = tree->get_selected();
  979. if (!item) {
  980. connect_button->set_disabled(true);
  981. return;
  982. }
  983. if (_is_item_signal(*item)) {
  984. _open_connection_dialog(*item);
  985. } else {
  986. Connection connection = item->get_metadata(0);
  987. _disconnect(connection);
  988. update_tree();
  989. }
  990. }
  991. void ConnectionsDock::_notification(int p_what) {
  992. switch (p_what) {
  993. case NOTIFICATION_ENTER_TREE:
  994. case NOTIFICATION_THEME_CHANGED: {
  995. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  996. } break;
  997. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  998. update_tree();
  999. } break;
  1000. }
  1001. }
  1002. void ConnectionsDock::_bind_methods() {
  1003. ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
  1004. }
  1005. void ConnectionsDock::set_node(Node *p_node) {
  1006. selected_node = p_node;
  1007. update_tree();
  1008. }
  1009. void ConnectionsDock::update_tree() {
  1010. String prev_selected;
  1011. if (tree->is_anything_selected()) {
  1012. prev_selected = tree->get_selected()->get_text(0);
  1013. }
  1014. tree->clear();
  1015. if (!selected_node) {
  1016. return;
  1017. }
  1018. TreeItem *root = tree->create_item();
  1019. List<MethodInfo> node_signals;
  1020. selected_node->get_signal_list(&node_signals);
  1021. bool did_script = false;
  1022. StringName base = selected_node->get_class();
  1023. while (base) {
  1024. List<MethodInfo> node_signals2;
  1025. Ref<Texture2D> icon;
  1026. String name;
  1027. if (!did_script) {
  1028. // Get script signals (including signals from any base scripts).
  1029. Ref<Script> scr = selected_node->get_script();
  1030. if (scr.is_valid()) {
  1031. scr->get_script_signal_list(&node_signals2);
  1032. if (scr->get_path().is_resource_file()) {
  1033. name = scr->get_path().get_file();
  1034. } else {
  1035. name = scr->get_class();
  1036. }
  1037. if (has_theme_icon(scr->get_class(), SNAME("EditorIcons"))) {
  1038. icon = get_theme_icon(scr->get_class(), SNAME("EditorIcons"));
  1039. }
  1040. }
  1041. } else {
  1042. ClassDB::get_signal_list(base, &node_signals2, true);
  1043. if (has_theme_icon(base, SNAME("EditorIcons"))) {
  1044. icon = get_theme_icon(base, SNAME("EditorIcons"));
  1045. }
  1046. name = base;
  1047. }
  1048. if (icon.is_null()) {
  1049. icon = get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  1050. }
  1051. TreeItem *section_item = nullptr;
  1052. // Create subsections.
  1053. if (node_signals2.size()) {
  1054. section_item = tree->create_item(root);
  1055. section_item->set_text(0, name);
  1056. section_item->set_icon(0, icon);
  1057. section_item->set_selectable(0, false);
  1058. section_item->set_editable(0, false);
  1059. section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  1060. node_signals2.sort();
  1061. }
  1062. for (MethodInfo &mi : node_signals2) {
  1063. const StringName signal_name = mi.name;
  1064. if (!search_box->get_text().is_subsequence_ofn(signal_name)) {
  1065. continue;
  1066. }
  1067. PackedStringArray argnames;
  1068. // Create the children of the subsection - the actual list of signals.
  1069. TreeItem *signal_item = tree->create_item(section_item);
  1070. String signame = connect_dialog->get_signature(mi, &argnames);
  1071. signal_item->set_text(0, signame);
  1072. if (signame == prev_selected) {
  1073. signal_item->select(0);
  1074. prev_selected = "";
  1075. }
  1076. Dictionary sinfo;
  1077. sinfo["name"] = signal_name;
  1078. sinfo["args"] = argnames;
  1079. signal_item->set_metadata(0, sinfo);
  1080. signal_item->set_icon(0, get_theme_icon(SNAME("Signal"), SNAME("EditorIcons")));
  1081. // Set tooltip with the signal's documentation.
  1082. {
  1083. String descr;
  1084. bool found = false;
  1085. HashMap<StringName, HashMap<StringName, String>>::Iterator G = descr_cache.find(base);
  1086. if (G) {
  1087. HashMap<StringName, String>::Iterator F = G->value.find(signal_name);
  1088. if (F) {
  1089. found = true;
  1090. descr = F->value;
  1091. }
  1092. }
  1093. if (!found) {
  1094. DocTools *dd = EditorHelp::get_doc_data();
  1095. HashMap<String, DocData::ClassDoc>::Iterator F = dd->class_list.find(base);
  1096. while (F && descr.is_empty()) {
  1097. for (int i = 0; i < F->value.signals.size(); i++) {
  1098. if (F->value.signals[i].name == signal_name.operator String()) {
  1099. descr = DTR(F->value.signals[i].description);
  1100. break;
  1101. }
  1102. }
  1103. if (!F->value.inherits.is_empty()) {
  1104. F = dd->class_list.find(F->value.inherits);
  1105. } else {
  1106. break;
  1107. }
  1108. }
  1109. descr_cache[base][signal_name] = descr;
  1110. }
  1111. // "::" separators used in make_custom_tooltip for formatting.
  1112. signal_item->set_tooltip_text(0, String(signal_name) + "::" + signame.trim_prefix(mi.name) + "::" + descr);
  1113. }
  1114. // List existing connections.
  1115. List<Object::Connection> existing_connections;
  1116. selected_node->get_signal_connection_list(signal_name, &existing_connections);
  1117. for (const Object::Connection &F : existing_connections) {
  1118. Connection connection = F;
  1119. if (!(connection.flags & CONNECT_PERSIST)) {
  1120. continue;
  1121. }
  1122. ConnectDialog::ConnectionData cd = connection;
  1123. Node *target = Object::cast_to<Node>(cd.target);
  1124. if (!target) {
  1125. continue;
  1126. }
  1127. String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()";
  1128. if (cd.flags & CONNECT_DEFERRED) {
  1129. path += " (deferred)";
  1130. }
  1131. if (cd.flags & CONNECT_ONE_SHOT) {
  1132. path += " (one-shot)";
  1133. }
  1134. if (cd.unbinds > 0) {
  1135. path += " unbinds(" + itos(cd.unbinds) + ")";
  1136. } else if (!cd.binds.is_empty()) {
  1137. path += " binds(";
  1138. for (int i = 0; i < cd.binds.size(); i++) {
  1139. if (i > 0) {
  1140. path += ", ";
  1141. }
  1142. path += cd.binds[i].operator String();
  1143. }
  1144. path += ")";
  1145. }
  1146. TreeItem *connection_item = tree->create_item(signal_item);
  1147. connection_item->set_text(0, path);
  1148. connection_item->set_metadata(0, connection);
  1149. connection_item->set_icon(0, get_theme_icon(SNAME("Slot"), SNAME("EditorIcons")));
  1150. if (_is_connection_inherited(connection)) {
  1151. // The scene inherits this connection.
  1152. connection_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  1153. connection_item->set_meta("_inherited_connection", true);
  1154. }
  1155. }
  1156. }
  1157. if (!did_script) {
  1158. did_script = true;
  1159. } else {
  1160. base = ClassDB::get_parent_class(base);
  1161. }
  1162. }
  1163. connect_button->set_text(TTR("Connect..."));
  1164. connect_button->set_disabled(true);
  1165. }
  1166. ConnectionsDock::ConnectionsDock() {
  1167. set_name(TTR("Signals"));
  1168. VBoxContainer *vbc = this;
  1169. search_box = memnew(LineEdit);
  1170. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1171. search_box->set_placeholder(TTR("Filter Signals"));
  1172. search_box->set_clear_button_enabled(true);
  1173. search_box->connect("text_changed", callable_mp(this, &ConnectionsDock::_filter_changed));
  1174. vbc->add_child(search_box);
  1175. tree = memnew(ConnectionsDockTree);
  1176. tree->set_columns(1);
  1177. tree->set_select_mode(Tree::SELECT_ROW);
  1178. tree->set_hide_root(true);
  1179. tree->set_column_clip_content(0, true);
  1180. vbc->add_child(tree);
  1181. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1182. tree->set_allow_rmb_select(true);
  1183. connect_button = memnew(Button);
  1184. HBoxContainer *hb = memnew(HBoxContainer);
  1185. vbc->add_child(hb);
  1186. hb->add_spacer();
  1187. hb->add_child(connect_button);
  1188. connect_button->connect("pressed", callable_mp(this, &ConnectionsDock::_connect_pressed));
  1189. connect_dialog = memnew(ConnectDialog);
  1190. connect_dialog->connect("connected", callable_mp(NodeDock::get_singleton(), &NodeDock::restore_last_valid_node), CONNECT_DEFERRED);
  1191. add_child(connect_dialog);
  1192. disconnect_all_dialog = memnew(ConfirmationDialog);
  1193. add_child(disconnect_all_dialog);
  1194. disconnect_all_dialog->connect("confirmed", callable_mp(this, &ConnectionsDock::_disconnect_all));
  1195. disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
  1196. signal_menu = memnew(PopupMenu);
  1197. add_child(signal_menu);
  1198. signal_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
  1199. signal_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_signal_menu_about_to_popup));
  1200. signal_menu->add_item(TTR("Connect..."), CONNECT);
  1201. signal_menu->add_item(TTR("Disconnect All"), DISCONNECT_ALL);
  1202. signal_menu->add_item(TTR("Copy Name"), COPY_NAME);
  1203. slot_menu = memnew(PopupMenu);
  1204. add_child(slot_menu);
  1205. slot_menu->connect("id_pressed", callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
  1206. slot_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_slot_menu_about_to_popup));
  1207. slot_menu->add_item(TTR("Edit..."), EDIT);
  1208. slot_menu->add_item(TTR("Go to Method"), GO_TO_SCRIPT);
  1209. slot_menu->add_item(TTR("Disconnect"), DISCONNECT);
  1210. connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
  1211. tree->connect("item_selected", callable_mp(this, &ConnectionsDock::_tree_item_selected));
  1212. tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
  1213. tree->connect("item_mouse_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed));
  1214. add_theme_constant_override("separation", 3 * EDSCALE);
  1215. }
  1216. ConnectionsDock::~ConnectionsDock() {
  1217. }