2
0

connections_dialog.cpp 46 KB

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