Path: blob/master/editor/scene/connections_dialog.cpp
9898 views
/**************************************************************************/1/* connections_dialog.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "connections_dialog.h"3132#include "core/config/project_settings.h"33#include "core/templates/hash_set.h"34#include "editor/doc/editor_help.h"35#include "editor/docks/node_dock.h"36#include "editor/docks/scene_tree_dock.h"37#include "editor/editor_main_screen.h"38#include "editor/editor_node.h"39#include "editor/editor_string_names.h"40#include "editor/editor_undo_redo_manager.h"41#include "editor/gui/editor_variant_type_selectors.h"42#include "editor/inspector/editor_inspector.h"43#include "editor/scene/scene_tree_editor.h"44#include "editor/script/script_editor_plugin.h"45#include "editor/settings/editor_settings.h"46#include "editor/themes/editor_scale.h"47#include "scene/gui/button.h"48#include "scene/gui/check_box.h"49#include "scene/gui/check_button.h"50#include "scene/gui/flow_container.h"51#include "scene/gui/label.h"52#include "scene/gui/line_edit.h"53#include "scene/gui/margin_container.h"54#include "scene/gui/popup_menu.h"55#include "scene/gui/spin_box.h"5657static Node *_find_first_script(Node *p_root, Node *p_node) {58if (p_node != p_root && p_node->get_owner() != p_root) {59return nullptr;60}61if (!p_node->get_script().is_null()) {62return p_node;63}6465for (int i = 0; i < p_node->get_child_count(); i++) {66Node *ret = _find_first_script(p_root, p_node->get_child(i));67if (ret) {68return ret;69}70}7172return nullptr;73}7475class ConnectDialogBinds : public Object {76GDCLASS(ConnectDialogBinds, Object);7778public:79Vector<Variant> params;8081bool _set(const StringName &p_name, const Variant &p_value) {82String name = p_name;8384if (name.begins_with("bind/argument_")) {85int which = name.get_slicec('_', 1).to_int() - 1;86ERR_FAIL_INDEX_V(which, params.size(), false);87params.write[which] = p_value;88} else {89return false;90}9192return true;93}9495bool _get(const StringName &p_name, Variant &r_ret) const {96String name = p_name;9798if (name.begins_with("bind/argument_")) {99int which = name.get_slicec('_', 1).to_int() - 1;100ERR_FAIL_INDEX_V(which, params.size(), false);101r_ret = params[which];102} else {103return false;104}105106return true;107}108109void _get_property_list(List<PropertyInfo> *p_list) const {110for (int i = 0; i < params.size(); i++) {111p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));112}113}114115void notify_changed() {116notify_property_list_changed();117}118119ConnectDialogBinds() {120}121};122123/*124* Signal automatically called by parent dialog.125*/126void ConnectDialog::ok_pressed() {127String method_name = dst_method->get_text();128129if (method_name.is_empty()) {130error->set_text(TTR("Method in target node must be specified."));131error->popup_centered();132return;133}134135if (!TS->is_valid_identifier(method_name.strip_edges())) {136error->set_text(TTR("Method name must be a valid identifier."));137error->popup_centered();138return;139}140141Node *target = tree->get_selected();142if (!target) {143return; // Nothing selected in the tree, not an error.144}145if (target->get_script().is_null()) {146if (!target->has_method(method_name)) {147error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));148error->popup_centered();149return;150}151}152emit_signal(SNAME("connected"));153hide();154}155156void ConnectDialog::_cancel_pressed() {157hide();158}159160void ConnectDialog::_item_activated() {161_ok_pressed(); // From AcceptDialog.162}163164/*165* Called each time a target node is selected within the target node tree.166*/167void ConnectDialog::_tree_node_selected() {168Node *current = tree->get_selected();169170if (!current) {171return;172}173174dst_path = source->get_path_to(current);175if (!edit_mode) {176set_dst_method(generate_method_callback_name(source, signal, current));177}178_update_method_tree();179_update_warning_label();180_update_ok_enabled();181}182183void ConnectDialog::_focus_currently_connected() {184tree->set_selected(source);185}186187void ConnectDialog::_unbind_count_changed(double p_count) {188for (Control *control : bind_controls) {189BaseButton *b = Object::cast_to<BaseButton>(control);190if (b) {191b->set_disabled(p_count > 0);192}193194EditorInspector *e = Object::cast_to<EditorInspector>(control);195if (e) {196e->set_read_only(p_count > 0);197}198}199200append_source->set_disabled(p_count > 0);201}202203void ConnectDialog::_method_selected() {204TreeItem *selected_item = method_tree->get_selected();205dst_method->set_text(selected_item->get_metadata(0));206}207208/*209* Adds a new parameter bind to connection.210*/211void ConnectDialog::_add_bind() {212Variant::Type type = type_list->get_selected_type();213214Variant value;215Callable::CallError err;216Variant::construct(type, value, nullptr, 0, err);217218cdbinds->params.push_back(value);219cdbinds->notify_changed();220}221222/*223* Remove parameter bind from connection.224*/225void ConnectDialog::_remove_bind() {226String st = bind_editor->get_selected_path();227if (st.is_empty()) {228return;229}230int idx = st.get_slicec('/', 1).to_int() - 1;231232ERR_FAIL_INDEX(idx, cdbinds->params.size());233cdbinds->params.remove_at(idx);234cdbinds->notify_changed();235}236/*237* Automatically generates a name for the callback method.238*/239StringName ConnectDialog::generate_method_callback_name(Node *p_source, const String &p_signal_name, Node *p_target) {240String node_name = p_source->get_name();241for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.242char32_t c = node_name[i];243if ((i == 0 && !is_unicode_identifier_start(c)) || (i > 0 && !is_unicode_identifier_continue(c))) {244if (c == ' ') {245// Replace spaces with underlines.246c = '_';247} else {248// Remove any other characters.249node_name.remove_at(i);250i--;251continue;252}253}254node_name[i] = c;255}256257Dictionary subst;258subst["NodeName"] = node_name.to_pascal_case();259subst["nodeName"] = node_name.to_camel_case();260subst["node_name"] = node_name.to_snake_case();261subst["node-name"] = node_name.to_kebab_case();262263subst["SignalName"] = p_signal_name.to_pascal_case();264subst["signalName"] = p_signal_name.to_camel_case();265subst["signal_name"] = p_signal_name.to_snake_case();266subst["signal-name"] = p_signal_name.to_kebab_case();267268String dst_method;269if (p_source == p_target) {270dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_to_self_name")).format(subst);271} else {272dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_name")).format(subst);273}274275return dst_method;276}277278void ConnectDialog::_create_method_tree_items(const List<MethodInfo> &p_methods, TreeItem *p_parent_item) {279for (const MethodInfo &mi : p_methods) {280TreeItem *method_item = method_tree->create_item(p_parent_item);281method_item->set_text(0, get_signature(mi));282method_item->set_metadata(0, mi.name);283}284}285286List<MethodInfo> ConnectDialog::_filter_method_list(const List<MethodInfo> &p_methods, const MethodInfo &p_signal, const String &p_search_string) const {287bool check_signal = compatible_methods_only->is_pressed();288List<MethodInfo> ret;289290LocalVector<Pair<Variant::Type, StringName>> effective_args;291int unbind = get_unbinds();292effective_args.reserve(MAX(p_signal.arguments.size() - unbind, 0));293for (int64_t i = 0; i < p_signal.arguments.size() - unbind; i++) {294PropertyInfo pi = p_signal.arguments[i];295effective_args.push_back(Pair(pi.type, pi.class_name));296}297if (unbind == 0) {298for (const Variant &variant : get_binds()) {299effective_args.push_back(Pair(variant.get_type(), StringName()));300}301}302303for (const MethodInfo &mi : p_methods) {304if (mi.name.begins_with("@")) {305// GH-92782. GDScript inline setters/getters are historically present in `get_method_list()`306// and can be called using `Object.call()`. However, these functions are meant to be internal307// and their names are not valid identifiers, so let's hide them from the user.308continue;309}310311if (!p_search_string.is_empty() && !mi.name.containsn(p_search_string)) {312continue;313}314315if (check_signal) {316const unsigned min_argc = mi.arguments.size() - mi.default_arguments.size();317const unsigned max_argc = (mi.flags & METHOD_FLAG_VARARG) ? UINT_MAX : mi.arguments.size();318319if (effective_args.size() < min_argc || effective_args.size() > max_argc) {320continue;321}322323bool type_mismatch = false;324for (int64_t i = 0; i < effective_args.size() && i < mi.arguments.size(); ++i) {325Variant::Type stype = effective_args[i].first;326Variant::Type mtype = mi.arguments[i].type;327328if (stype != Variant::NIL && mtype != Variant::NIL && stype != mtype) {329type_mismatch = true;330break;331}332333if (stype == Variant::OBJECT && mtype == Variant::OBJECT && !ClassDB::is_parent_class(effective_args[i].second, mi.arguments[i].class_name)) {334type_mismatch = true;335break;336}337}338339if (type_mismatch) {340continue;341}342}343344ret.push_back(mi);345}346347return ret;348}349350void ConnectDialog::_update_method_tree() {351method_tree->clear();352353Color disabled_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)) * 0.7;354String search_string = method_search->get_text();355Node *target = tree->get_selected();356if (!target) {357return;358}359360MethodInfo signal_info;361if (compatible_methods_only->is_pressed()) {362List<MethodInfo> signals;363source->get_signal_list(&signals);364for (const MethodInfo &mi : signals) {365if (mi.name == signal) {366signal_info = mi;367break;368}369}370}371372TreeItem *root_item = method_tree->create_item();373root_item->set_text(0, TTR("Methods"));374root_item->set_selectable(0, false);375376// If a script is attached, get methods from it.377ScriptInstance *si = target->get_script_instance();378if (si) {379if (si->get_script()->is_built_in()) {380si->get_script()->reload();381}382List<MethodInfo> methods;383si->get_method_list(&methods);384methods = _filter_method_list(methods, signal_info, search_string);385386if (!methods.is_empty()) {387TreeItem *si_item = method_tree->create_item(root_item);388si_item->set_text(0, TTR("Attached Script"));389si_item->set_icon(0, get_editor_theme_icon(SNAME("Script")));390si_item->set_selectable(0, false);391392_create_method_tree_items(methods, si_item);393}394}395396if (script_methods_only->is_pressed()) {397empty_tree_label->set_visible(root_item->get_first_child() == nullptr);398return;399}400401// Get methods from each class in the hierarchy.402StringName current_class = target->get_class_name();403do {404TreeItem *class_item = method_tree->create_item(root_item);405class_item->set_text(0, current_class);406Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Node"));407if (has_theme_icon(current_class, EditorStringName(EditorIcons))) {408icon = get_editor_theme_icon(current_class);409}410class_item->set_icon(0, icon);411class_item->set_selectable(0, false);412413List<MethodInfo> methods;414ClassDB::get_method_list(current_class, &methods, true);415methods = _filter_method_list(methods, signal_info, search_string);416417if (methods.is_empty()) {418class_item->set_custom_color(0, disabled_color);419} else {420_create_method_tree_items(methods, class_item);421}422current_class = ClassDB::get_parent_class_nocheck(current_class);423} while (current_class != StringName());424425empty_tree_label->set_visible(root_item->get_first_child() == nullptr);426}427428void ConnectDialog::_method_check_button_pressed(const CheckButton *p_button) {429if (p_button == script_methods_only) {430EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_script_methods_only", p_button->is_pressed());431} else if (p_button == compatible_methods_only) {432EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_compatible_methods_only", p_button->is_pressed());433}434_update_method_tree();435}436437void ConnectDialog::_open_method_popup() {438method_popup->popup_centered();439method_search->clear();440method_search->grab_focus();441}442443/*444* Enables or disables the connect button. The connect button is enabled if a445* node is selected and valid in the selected mode.446*/447void ConnectDialog::_update_ok_enabled() {448Node *target = tree->get_selected();449450if (target == nullptr) {451get_ok_button()->set_disabled(true);452return;453}454455if (dst_method->get_text().is_empty()) {456get_ok_button()->set_disabled(true);457return;458}459460get_ok_button()->set_disabled(false);461}462463void ConnectDialog::_update_warning_label() {464Node *dst = source->get_node(dst_path);465if (dst == nullptr) {466warning_label->set_visible(false);467return;468}469470Ref<Script> scr = dst->get_script();471if (scr.is_null()) {472warning_label->set_visible(false);473return;474}475476ScriptLanguage *language = scr->get_language();477if (language->can_make_function()) {478warning_label->set_visible(false);479return;480}481482warning_label->set_text(vformat(TTR("%s: Callback code won't be generated, please add it manually."), language->get_name()));483warning_label->set_visible(true);484}485486void ConnectDialog::_post_popup() {487callable_mp((Control *)dst_method, &Control::grab_focus).call_deferred();488callable_mp(dst_method, &LineEdit::select_all).call_deferred();489}490491void ConnectDialog::_notification(int p_what) {492switch (p_what) {493case NOTIFICATION_ENTER_TREE: {494bind_editor->edit(cdbinds);495496[[fallthrough]];497}498case NOTIFICATION_THEME_CHANGED: {499method_search->set_right_icon(get_editor_theme_icon("Search"));500open_method_tree->set_button_icon(get_editor_theme_icon("Edit"));501} break;502}503}504505void ConnectDialog::_bind_methods() {506ADD_SIGNAL(MethodInfo("connected"));507}508509Node *ConnectDialog::get_source() const {510return source;511}512513ConnectDialog::ConnectionData ConnectDialog::get_source_connection_data() const {514return source_connection_data;515}516517StringName ConnectDialog::get_signal_name() const {518return signal;519}520521PackedStringArray ConnectDialog::get_signal_args() const {522return signal_args;523}524525NodePath ConnectDialog::get_dst_path() const {526return dst_path;527}528529void ConnectDialog::set_dst_node(Node *p_node) {530tree->set_selected(p_node);531}532533StringName ConnectDialog::get_dst_method_name() const {534String txt = dst_method->get_text();535if (txt.contains_char('(')) {536txt = txt.left(txt.find_char('(')).strip_edges();537}538return txt;539}540541void ConnectDialog::set_dst_method(const StringName &p_method) {542dst_method->set_text(p_method);543}544545int ConnectDialog::get_unbinds() const {546return int(unbind_count->get_value());547}548549Vector<Variant> ConnectDialog::get_binds() const {550return cdbinds->params;551}552553String ConnectDialog::get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names) {554PackedStringArray signature;555signature.append(p_method.name);556signature.append("(");557558for (int64_t i = 0; i < p_method.arguments.size(); ++i) {559if (i > 0) {560signature.append(", ");561}562563const PropertyInfo &pi = p_method.arguments[i];564String type_name;565switch (pi.type) {566case Variant::NIL:567type_name = "Variant";568break;569case Variant::INT:570if ((pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && pi.class_name != StringName() && !String(pi.class_name).begins_with("res://")) {571type_name = pi.class_name;572} else {573type_name = "int";574}575break;576case Variant::ARRAY:577if (pi.hint == PROPERTY_HINT_ARRAY_TYPE && !pi.hint_string.is_empty() && !pi.hint_string.begins_with("res://")) {578type_name = "Array[" + pi.hint_string + "]";579} else {580type_name = "Array";581}582break;583case Variant::DICTIONARY:584type_name = "Dictionary";585if (pi.hint == PROPERTY_HINT_DICTIONARY_TYPE && !pi.hint_string.is_empty()) {586String key_hint = pi.hint_string.get_slicec(';', 0);587String value_hint = pi.hint_string.get_slicec(';', 1);588if (key_hint.is_empty() || key_hint.begins_with("res://")) {589key_hint = "Variant";590}591if (value_hint.is_empty() || value_hint.begins_with("res://")) {592value_hint = "Variant";593}594if (key_hint != "Variant" || value_hint != "Variant") {595type_name += "[" + key_hint + ", " + value_hint + "]";596}597}598break;599case Variant::OBJECT:600if (pi.class_name != StringName()) {601type_name = pi.class_name;602} else {603type_name = "Object";604}605break;606default:607type_name = Variant::get_type_name(pi.type);608break;609}610611String arg_name = pi.name.is_empty() ? "arg" + itos(i) : pi.name;612signature.append(arg_name + ": " + type_name);613if (r_arg_names) {614r_arg_names->push_back(arg_name + ": " + type_name);615}616}617618if (p_method.flags & METHOD_FLAG_VARARG) {619signature.append(p_method.arguments.is_empty() ? "..." : ", ...");620}621622signature.append(")");623return String().join(signature);624}625626bool ConnectDialog::get_deferred() const {627return deferred->is_pressed();628}629630bool ConnectDialog::get_one_shot() const {631return one_shot->is_pressed();632}633634bool ConnectDialog::get_append_source() const {635return !append_source->is_disabled() && append_source->is_pressed();636}637638/*639* Returns true if ConnectDialog is being used to edit an existing connection.640*/641bool ConnectDialog::is_editing() const {642return edit_mode;643}644645void ConnectDialog::shortcut_input(const Ref<InputEvent> &p_event) {646const Ref<InputEventKey> &key = p_event;647648if (key.is_valid() && key->is_pressed() && !key->is_echo()) {649if (ED_IS_SHORTCUT("editor/open_search", p_event)) {650filter_nodes->grab_focus();651filter_nodes->select_all();652filter_nodes->accept_event();653}654}655}656657/*658* Initialize ConnectDialog and populate fields with expected data.659* If creating a connection from scratch, sensible defaults are used.660* If editing an existing connection, previous data is retained.661*/662void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit) {663set_hide_on_ok(false);664665source = static_cast<Node *>(p_cd.source);666signal = p_cd.signal;667signal_args = p_signal_args;668669tree->set_selected(nullptr);670tree->set_marked(source);671672if (p_cd.target) {673set_dst_node(static_cast<Node *>(p_cd.target));674set_dst_method(p_cd.method);675}676677_update_ok_enabled();678679bool b_deferred = (p_cd.flags & CONNECT_DEFERRED);680bool b_oneshot = (p_cd.flags & CONNECT_ONE_SHOT);681bool b_append_source = (p_cd.flags & CONNECT_APPEND_SOURCE_OBJECT);682683deferred->set_pressed(b_deferred);684one_shot->set_pressed(b_oneshot);685append_source->set_pressed(b_append_source);686687unbind_count->set_max(p_signal_args.size());688unbind_count->set_value(p_cd.unbinds);689_unbind_count_changed(p_cd.unbinds);690691cdbinds->params.clear();692cdbinds->params = p_cd.binds;693cdbinds->notify_changed();694695edit_mode = p_edit;696697source_connection_data = p_cd;698}699700void ConnectDialog::popup_dialog(const String &p_for_signal) {701from_signal->set_text(p_for_signal);702warning_label->add_theme_color_override(SceneStringName(font_color), warning_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));703error_label->add_theme_color_override(SceneStringName(font_color), error_label->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));704filter_nodes->clear();705706if (!advanced->is_pressed()) {707error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));708}709710if (first_popup) {711first_popup = false;712_advanced_pressed();713}714715popup_centered();716}717718void ConnectDialog::_advanced_pressed() {719if (advanced->is_pressed()) {720connect_to_label->set_text(TTR("Connect to Node:"));721tree->set_connect_to_script_mode(false);722723vbc_right->show();724error_label->hide();725} else {726reset_size();727connect_to_label->set_text(TTR("Connect to Script:"));728tree->set_connect_to_script_mode(true);729730vbc_right->hide();731error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));732}733734EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "use_advanced_connections", advanced->is_pressed());735736popup_centered();737}738739ConnectDialog::ConnectDialog() {740set_min_size(Size2(0, 500) * EDSCALE);741742HBoxContainer *main_hb = memnew(HBoxContainer);743add_child(main_hb);744745VBoxContainer *vbc_left = memnew(VBoxContainer);746main_hb->add_child(vbc_left);747vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);748vbc_left->set_custom_minimum_size(Vector2(400 * EDSCALE, 0));749750from_signal = memnew(LineEdit);751from_signal->set_accessibility_name(TTRC("From Signal:"));752vbc_left->add_margin_child(TTR("From Signal:"), from_signal);753from_signal->set_editable(false);754755tree = memnew(SceneTreeEditor(false));756tree->set_update_when_invisible(false);757tree->set_connecting_signal(true);758tree->set_show_enabled_subscene(true);759tree->set_v_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);760tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));761tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));762tree->set_connect_to_script_mode(true);763764HBoxContainer *hbc_filter = memnew(HBoxContainer);765766filter_nodes = memnew(LineEdit);767hbc_filter->add_child(filter_nodes);768filter_nodes->set_h_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);769filter_nodes->set_placeholder(TTR("Filter Nodes"));770filter_nodes->set_accessibility_name(TTRC("Filter Nodes"));771filter_nodes->set_clear_button_enabled(true);772filter_nodes->connect(SceneStringName(text_changed), callable_mp(tree, &SceneTreeEditor::set_filter));773774Button *focus_current = memnew(Button);775hbc_filter->add_child(focus_current);776focus_current->set_text(TTR("Go to Source"));777focus_current->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_focus_currently_connected));778779Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), hbc_filter, false);780connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));781vbc_left->add_child(tree);782783warning_label = memnew(Label);784warning_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);785vbc_left->add_child(warning_label);786warning_label->hide();787788error_label = memnew(Label);789error_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);790error_label->set_text(TTR("Scene does not contain any script."));791vbc_left->add_child(error_label);792error_label->hide();793794method_popup = memnew(AcceptDialog);795method_popup->set_title(TTR("Select Method"));796method_popup->set_min_size(Vector2(400, 600) * EDSCALE);797add_child(method_popup);798799VBoxContainer *method_vbc = memnew(VBoxContainer);800method_popup->add_child(method_vbc);801802method_search = memnew(LineEdit);803method_vbc->add_child(method_search);804method_search->set_placeholder(TTR("Filter Methods"));805method_search->set_accessibility_name(TTRC("Filter Methods"));806method_search->set_clear_button_enabled(true);807method_search->connect(SceneStringName(text_changed), callable_mp(this, &ConnectDialog::_update_method_tree).unbind(1));808809method_tree = memnew(Tree);810method_vbc->add_child(method_tree);811method_tree->set_accessibility_name(TTRC("Methods"));812method_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);813method_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);814method_tree->set_hide_root(true);815method_tree->connect(SceneStringName(item_selected), callable_mp(this, &ConnectDialog::_method_selected));816method_tree->connect("item_activated", callable_mp((Window *)method_popup, &Window::hide));817818empty_tree_label = memnew(Label(TTR("No method found matching given filters.")));819method_popup->add_child(empty_tree_label);820empty_tree_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);821empty_tree_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);822empty_tree_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);823824script_methods_only = memnew(CheckButton(TTR("Script Methods Only")));825method_vbc->add_child(script_methods_only);826script_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);827script_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_script_methods_only", true));828script_methods_only->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(script_methods_only));829830compatible_methods_only = memnew(CheckButton(TTR("Compatible Methods Only")));831method_vbc->add_child(compatible_methods_only);832compatible_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);833compatible_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_compatible_methods_only", true));834compatible_methods_only->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(compatible_methods_only));835836vbc_right = memnew(VBoxContainer);837main_hb->add_child(vbc_right);838vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);839vbc_right->set_custom_minimum_size(Vector2(150 * EDSCALE, 0));840vbc_right->hide();841842HBoxContainer *add_bind_hb = memnew(HBoxContainer);843844type_list = memnew(EditorVariantTypeOptionButton);845type_list->set_accessibility_name(TTRC("Type"));846type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);847type_list->populate({ Variant::NIL, Variant::OBJECT });848add_bind_hb->add_child(type_list);849bind_controls.push_back(type_list);850851Button *add_bind = memnew(Button);852add_bind->set_text(TTR("Add"));853add_bind_hb->add_child(add_bind);854add_bind->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_add_bind));855bind_controls.push_back(add_bind);856857Button *del_bind = memnew(Button);858del_bind->set_text(TTR("Remove"));859add_bind_hb->add_child(del_bind);860del_bind->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_remove_bind));861bind_controls.push_back(del_bind);862863vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);864865bind_editor = memnew(EditorInspector);866bind_editor->set_accessibility_name(TTRC("Extra Call Arguments:"));867bind_controls.push_back(bind_editor);868869vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);870871unbind_count = memnew(SpinBox);872unbind_count->set_tooltip_text(TTR("Allows to drop arguments sent by signal emitter."));873unbind_count->set_accessibility_name(TTRC("Unbind Signal Arguments:"));874unbind_count->connect(SceneStringName(value_changed), callable_mp(this, &ConnectDialog::_unbind_count_changed));875876vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);877878HBoxContainer *hbc_method = memnew(HBoxContainer);879vbc_left->add_margin_child(TTR("Receiver Method:"), hbc_method);880881dst_method = memnew(LineEdit);882dst_method->set_accessibility_name(TTRC("Receiver Method"));883dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);884dst_method->connect(SceneStringName(text_changed), callable_mp(method_tree, &Tree::deselect_all).unbind(1));885hbc_method->add_child(dst_method);886register_text_enter(dst_method);887888open_method_tree = memnew(Button(TTRC("Pick")));889hbc_method->add_child(open_method_tree);890open_method_tree->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_open_method_popup));891892advanced = memnew(CheckButton(TTR("Advanced")));893vbc_left->add_child(advanced);894advanced->set_h_size_flags(Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND);895advanced->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "use_advanced_connections", false));896advanced->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_advanced_pressed));897898FlowContainer *fc_flags = memnew(FlowContainer);899vbc_right->add_child(fc_flags);900901deferred = memnew(CheckBox);902deferred->set_text(TTR("Deferred"));903deferred->set_tooltip_text(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));904fc_flags->add_child(deferred);905906one_shot = memnew(CheckBox);907one_shot->set_text(TTR("One Shot"));908one_shot->set_tooltip_text(TTR("Disconnects the signal after its first emission."));909fc_flags->add_child(one_shot);910911append_source = memnew(CheckBox);912append_source->set_text(TTRC("Append Source"));913append_source->set_tooltip_text(TTRC("The source object is automatically sent when the signal is emitted."));914fc_flags->add_child(append_source);915916cdbinds = memnew(ConnectDialogBinds);917918error = memnew(AcceptDialog);919add_child(error);920error->set_title(TTR("Cannot connect signal"));921error->set_ok_button_text(TTR("Close"));922set_ok_button_text(TTR("Connect"));923}924925ConnectDialog::~ConnectDialog() {926memdelete(cdbinds);927}928929//////////////////////////////////////////930931Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {932// If it's not a doc tooltip, fallback to the default one.933if (p_text.is_empty() || p_text.contains(" :: ")) {934return nullptr;935}936937return EditorHelpBitTooltip::show_tooltip(const_cast<ConnectionsDockTree *>(this), p_text);938}939940struct _ConnectionsDockMethodInfoSort {941_FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {942return a.name < b.name;943}944};945946void ConnectionsDock::_filter_changed(const String &p_text) {947update_tree();948}949950/*951* Post-ConnectDialog callback for creating/editing connections.952* Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.953*/954void ConnectionsDock::_make_or_edit_connection() {955NodePath dst_path = connect_dialog->get_dst_path();956Node *target = selected_node->get_node(dst_path);957ERR_FAIL_NULL(target);958959ConnectDialog::ConnectionData cd;960cd.source = connect_dialog->get_source();961cd.target = target;962cd.signal = connect_dialog->get_signal_name();963cd.method = connect_dialog->get_dst_method_name();964cd.unbinds = connect_dialog->get_unbinds();965if (cd.unbinds == 0) {966cd.binds = connect_dialog->get_binds();967}968bool b_deferred = connect_dialog->get_deferred();969bool b_oneshot = connect_dialog->get_one_shot();970bool b_append_source = connect_dialog->get_append_source();971cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONE_SHOT : 0) | (b_append_source ? CONNECT_APPEND_SOURCE_OBJECT : 0);972973// If the function is found in target's own script, check the editor setting974// to determine if the script should be opened.975// If the function is found in an inherited class or script no need to do anything976// except making a connection.977bool add_script_function_request = false;978Ref<Script> scr = target->get_script();979980if (scr.is_valid() && !ClassDB::has_method(target->get_class(), cd.method)) {981// Check in target's own script.982int line = scr->get_language()->find_function(cd.method, scr->get_source_code());983if (line != -1) {984add_script_function_request = EDITOR_GET("text_editor/behavior/navigation/open_script_when_connecting_signal_to_existing_method");985} else {986// There is a chance that the method is inherited from another script.987bool found_inherited_function = false;988Ref<Script> inherited_scr = scr->get_base_script();989while (inherited_scr.is_valid()) {990int inherited_line = inherited_scr->get_language()->find_function(cd.method, inherited_scr->get_source_code());991if (inherited_line != -1) {992found_inherited_function = true;993break;994}995996inherited_scr = inherited_scr->get_base_script();997}998999add_script_function_request = !found_inherited_function;1000}1001}10021003if (add_script_function_request) {1004PackedStringArray script_function_args = connect_dialog->get_signal_args();1005script_function_args.resize(script_function_args.size() - cd.unbinds);10061007// Append the source.1008if (b_append_source) {1009String class_name = cd.source->get_class();1010bool found = false;10111012Ref<Script> source_script = cd.source->get_script();1013if (source_script.is_valid()) {1014found = source_script->has_script_signal(cd.signal);1015if (found) {1016// Check global name in script inheritance chain.1017bool need_check = found;1018Ref<Script> base_script = source_script->get_base_script();1019while (base_script.is_valid()) {1020need_check = base_script->has_script_signal(cd.signal);1021if (!need_check) {1022break;1023}1024source_script = base_script;1025base_script = source_script->get_base_script();1026}1027class_name = source_script->get_global_name();1028}1029}10301031if (!found) {1032while (!class_name.is_empty()) {1033// Search in ClassDB according to the inheritance chain.1034found = ClassDB::has_signal(class_name, cd.signal, true);1035if (found) {1036break;1037}1038class_name = ClassDB::get_parent_class(class_name);1039}1040}10411042script_function_args.push_back("source:" + class_name);1043}10441045for (int i = 0; i < cd.binds.size(); i++) {1046script_function_args.push_back("extra_arg_" + itos(i) + ": " + Variant::get_type_name(cd.binds[i].get_type()));1047}10481049EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args);1050}10511052if (connect_dialog->is_editing()) {1053_disconnect(connect_dialog->get_source_connection_data());1054_connect(cd);1055} else {1056_connect(cd);1057}10581059update_tree();1060}10611062/*1063* Creates single connection w/ undo-redo functionality.1064*/1065void ConnectionsDock::_connect(const ConnectDialog::ConnectionData &p_cd) {1066Node *source = Object::cast_to<Node>(p_cd.source);1067Node *target = Object::cast_to<Node>(p_cd.target);10681069if (!source || !target) {1070return;1071}10721073Callable callable = p_cd.get_callable();1074EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1075undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));1076undo_redo->add_do_method(source, "connect", p_cd.signal, callable, p_cd.flags);1077undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);1078undo_redo->add_do_method(this, "update_tree");1079undo_redo->add_undo_method(this, "update_tree");1080undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.1081undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");10821083undo_redo->commit_action();1084}10851086/*1087* Break single connection w/ undo-redo functionality.1088*/1089void ConnectionsDock::_disconnect(const ConnectDialog::ConnectionData &p_cd) {1090ERR_FAIL_COND(p_cd.source != selected_node); // Shouldn't happen but... Bugcheck.10911092EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1093undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), p_cd.signal, p_cd.method));10941095Callable callable = p_cd.get_callable();1096undo_redo->add_do_method(selected_node, "disconnect", p_cd.signal, callable);1097undo_redo->add_undo_method(selected_node, "connect", p_cd.signal, callable, p_cd.flags);1098undo_redo->add_do_method(this, "update_tree");1099undo_redo->add_undo_method(this, "update_tree");1100undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.1101undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");11021103undo_redo->commit_action();1104}11051106/*1107* Break all connections of currently selected signal.1108* Can undo-redo as a single action.1109*/1110void ConnectionsDock::_disconnect_all() {1111TreeItem *item = tree->get_selected();1112if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {1113return;1114}11151116TreeItem *child = item->get_first_child();1117String signal_name = item->get_metadata(0).operator Dictionary()["name"];1118EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1119undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));11201121while (child) {1122Connection connection = child->get_metadata(0);1123if (!_is_connection_inherited(connection)) {1124ConnectDialog::ConnectionData cd = connection;1125undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable());1126undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.flags);1127}1128child = child->get_next();1129}11301131undo_redo->add_do_method(this, "update_tree");1132undo_redo->add_undo_method(this, "update_tree");1133undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");1134undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");11351136undo_redo->commit_action();1137}11381139void ConnectionsDock::_tree_item_selected() {1140TreeItem *item = tree->get_selected();1141if (item && _get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {1142connect_button->set_text(TTR("Connect..."));1143connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));1144connect_button->set_disabled(false);1145} else if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {1146connect_button->set_text(TTR("Disconnect"));1147connect_button->set_button_icon(get_editor_theme_icon(SNAME("Unlinked")));11481149Object::Connection connection = item->get_metadata(0);1150connect_button->set_disabled(_is_connection_inherited(connection));1151} else {1152connect_button->set_text(TTR("Connect..."));1153connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));1154connect_button->set_disabled(true);1155}1156}11571158void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.1159TreeItem *item = tree->get_selected();1160if (!item) {1161return;1162}11631164if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {1165_open_connection_dialog(*item);1166} else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {1167_go_to_method(*item);1168}1169}11701171ConnectionsDock::TreeItemType ConnectionsDock::_get_item_type(const TreeItem &p_item) const {1172if (&p_item == tree->get_root()) {1173return TREE_ITEM_TYPE_ROOT;1174} else if (p_item.get_parent() == tree->get_root()) {1175return TREE_ITEM_TYPE_CLASS;1176} else if (p_item.get_parent()->get_parent() == tree->get_root()) {1177return TREE_ITEM_TYPE_SIGNAL;1178} else {1179return TREE_ITEM_TYPE_CONNECTION;1180}1181}11821183bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {1184return bool(p_connection.flags & CONNECT_INHERITED);1185}11861187/*1188* Open connection dialog with TreeItem data to CREATE a brand-new connection.1189*/1190void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {1191const Dictionary sinfo = p_item.get_metadata(0);1192const StringName signal_name = sinfo["name"];1193const PackedStringArray signal_args = sinfo["args"];11941195Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;1196if (!dst_node || dst_node->get_script().is_null()) {1197dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());1198}11991200ConnectDialog::ConnectionData cd;1201cd.source = selected_node;1202cd.signal = signal_name;1203cd.target = dst_node;1204cd.method = ConnectDialog::generate_method_callback_name(cd.source, signal_name, cd.target);1205connect_dialog->init(cd, signal_args);1206connect_dialog->set_title(TTR("Connect a Signal to a Method"));1207connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");1208}12091210/*1211* Open connection dialog with Connection data to EDIT an existing connection.1212*/1213void ConnectionsDock::_open_edit_connection_dialog(TreeItem &p_item) {1214TreeItem *signal_item = p_item.get_parent();1215ERR_FAIL_NULL(signal_item);12161217Connection connection = p_item.get_metadata(0);1218ConnectDialog::ConnectionData cd = connection;12191220Node *src = Object::cast_to<Node>(cd.source);1221Node *dst = Object::cast_to<Node>(cd.target);12221223if (src && dst) {1224const StringName &signal_name = cd.signal;1225const PackedStringArray signal_args = signal_item->get_metadata(0).operator Dictionary()["args"];12261227connect_dialog->init(cd, signal_args, true);1228connect_dialog->set_title(vformat(TTR("Edit Connection: '%s'"), cd.signal));1229connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");1230}1231}12321233/*1234* Open slot method location in script editor.1235*/1236void ConnectionsDock::_go_to_method(TreeItem &p_item) {1237if (_get_item_type(p_item) != TREE_ITEM_TYPE_CONNECTION) {1238return;1239}12401241Connection connection = p_item.get_metadata(0);1242ConnectDialog::ConnectionData cd = connection;1243ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck.12441245if (!cd.target) {1246return;1247}12481249Ref<Script> scr = cd.target->get_script();12501251if (scr.is_null()) {1252return;1253}12541255if (scr.is_valid() && ScriptEditor::get_singleton()->script_goto_method(scr, cd.method)) {1256EditorNode::get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);1257}1258}12591260void ConnectionsDock::_handle_class_menu_option(int p_option) {1261switch (p_option) {1262case CLASS_MENU_OPEN_DOCS:1263ScriptEditor::get_singleton()->goto_help("class:" + class_menu_doc_class_name);1264EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);1265break;1266}1267}12681269void ConnectionsDock::_class_menu_about_to_popup() {1270class_menu->set_item_disabled(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), class_menu_doc_class_name.is_empty());1271}12721273void ConnectionsDock::_handle_signal_menu_option(int p_option) {1274TreeItem *item = tree->get_selected();1275if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {1276return;1277}12781279Dictionary meta = item->get_metadata(0);12801281switch (p_option) {1282case SIGNAL_MENU_CONNECT: {1283_open_connection_dialog(*item);1284} break;1285case SIGNAL_MENU_DISCONNECT_ALL: {1286disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), meta["name"]));1287disconnect_all_dialog->popup_centered();1288} break;1289case SIGNAL_MENU_COPY_NAME: {1290DisplayServer::get_singleton()->clipboard_set(meta["name"]);1291} break;1292case SIGNAL_MENU_OPEN_DOCS: {1293ScriptEditor::get_singleton()->goto_help("class_signal:" + String(meta["class"]) + ":" + String(meta["name"]));1294EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);1295} break;1296}1297}12981299void ConnectionsDock::_signal_menu_about_to_popup() {1300TreeItem *item = tree->get_selected();1301if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {1302return;1303}13041305Dictionary meta = item->get_metadata(0);13061307bool disable_disconnect_all = true;1308for (int i = 0; i < item->get_child_count(); i++) {1309if (!item->get_child(i)->has_meta("_inherited_connection")) {1310disable_disconnect_all = false;1311}1312}13131314signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), disable_disconnect_all);1315signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), String(meta["class"]).is_empty());1316}13171318void ConnectionsDock::_handle_slot_menu_option(int p_option) {1319TreeItem *item = tree->get_selected();1320if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {1321return;1322}13231324switch (p_option) {1325case SLOT_MENU_EDIT: {1326_open_edit_connection_dialog(*item);1327} break;1328case SLOT_MENU_GO_TO_METHOD: {1329_go_to_method(*item);1330} break;1331case SLOT_MENU_DISCONNECT: {1332Connection connection = item->get_metadata(0);1333_disconnect(connection);1334update_tree();1335} break;1336}1337}13381339void ConnectionsDock::_slot_menu_about_to_popup() {1340TreeItem *item = tree->get_selected();1341if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {1342return;1343}13441345bool connection_is_inherited = item->has_meta("_inherited_connection");13461347slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_EDIT), connection_is_inherited);1348slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), connection_is_inherited);1349}13501351void ConnectionsDock::_tree_gui_input(const Ref<InputEvent> &p_event) {1352TreeItem *item = nullptr;1353Point2 item_pos;13541355const Ref<InputEventKey> &key = p_event;13561357if (key.is_valid() && key->is_pressed() && !key->is_echo()) {1358if (ED_IS_SHORTCUT("connections_editor/disconnect", p_event)) {1359item = tree->get_selected();1360if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {1361Connection connection = item->get_metadata(0);1362_disconnect(connection);1363update_tree();13641365// Stop the Delete input from propagating elsewhere.1366accept_event();1367return;1368}1369} else if (ED_IS_SHORTCUT("editor/open_search", p_event)) {1370search_box->grab_focus();1371search_box->select_all();13721373accept_event();1374return;1375}1376}1377if (key.is_valid() && key->is_pressed() && key->is_action("ui_menu", true)) {1378item = tree->get_selected();1379if (!item) {1380return;1381}1382item_pos = tree->get_item_rect(item).position;1383}13841385// Handle RMB press.1386const Ref<InputEventMouseButton> &mb_event = p_event;13871388if (mb_event.is_valid() && mb_event->is_pressed() && mb_event->get_button_index() == MouseButton::RIGHT) {1389item = tree->get_item_at_position(mb_event->get_position());1390if (!item) {1391return;1392}1393item_pos = mb_event->get_position();1394}13951396if (item) {1397if (item->is_selectable(0)) {1398// Update selection now, before `about_to_popup` signal. Needed for SIGNAL and CONNECTION context menus.1399tree->set_selected(item);1400}14011402Vector2 screen_position = tree->get_screen_position() + item_pos;14031404switch (_get_item_type(*item)) {1405case TREE_ITEM_TYPE_ROOT:1406break;1407case TREE_ITEM_TYPE_CLASS:1408class_menu_doc_class_name = item->get_metadata(0);1409class_menu->set_position(screen_position);1410class_menu->reset_size();1411class_menu->popup();1412accept_event(); // Don't collapse item.1413break;1414case TREE_ITEM_TYPE_SIGNAL:1415signal_menu->set_position(screen_position);1416signal_menu->reset_size();1417signal_menu->popup();1418break;1419case TREE_ITEM_TYPE_CONNECTION:1420slot_menu->set_position(screen_position);1421slot_menu->reset_size();1422slot_menu->popup();1423break;1424}1425}1426}14271428void ConnectionsDock::_close() {1429hide();1430}14311432void ConnectionsDock::_connect_pressed() {1433TreeItem *item = tree->get_selected();1434if (!item) {1435connect_button->set_disabled(true);1436return;1437}14381439if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {1440_open_connection_dialog(*item);1441} else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {1442Connection connection = item->get_metadata(0);1443_disconnect(connection);1444update_tree();1445}1446}14471448void ConnectionsDock::_notification(int p_what) {1449switch (p_what) {1450case NOTIFICATION_THEME_CHANGED: {1451search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));14521453class_menu->set_item_icon(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));14541455signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_CONNECT), get_editor_theme_icon(SNAME("Instance")));1456signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), get_editor_theme_icon(SNAME("Unlinked")));1457signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_COPY_NAME), get_editor_theme_icon(SNAME("ActionCopy")));1458signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));14591460slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_EDIT), get_editor_theme_icon(SNAME("Edit")));1461slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_GO_TO_METHOD), get_editor_theme_icon(SNAME("ArrowRight")));1462slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), get_editor_theme_icon(SNAME("Unlinked")));14631464tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));14651466update_tree();1467} break;14681469case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {1470if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editors")) {1471update_tree();1472}1473} break;1474}1475}14761477void ConnectionsDock::_bind_methods() {1478ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);1479}14801481void ConnectionsDock::set_node(Node *p_node) {1482selected_node = p_node;1483update_tree();1484}14851486void ConnectionsDock::update_tree() {1487String prev_selected;1488if (tree->is_anything_selected()) {1489prev_selected = tree->get_selected()->get_text(0);1490}1491tree->clear();14921493if (!selected_node) {1494return;1495}14961497TreeItem *root = tree->create_item();1498DocTools *doc_data = EditorHelp::get_doc_data();1499EditorData &editor_data = EditorNode::get_editor_data();1500StringName native_base = selected_node->get_class();1501Ref<Script> script_base = selected_node->get_script();15021503while (native_base != StringName()) {1504String class_name;1505String doc_class_name;1506Ref<Texture2D> class_icon;1507List<MethodInfo> class_signals;15081509if (script_base.is_valid()) {1510class_name = script_base->get_global_name();1511if (class_name.is_empty()) {1512class_name = script_base->get_path().get_file();1513}15141515doc_class_name = script_base->get_global_name();1516if (doc_class_name.is_empty()) {1517doc_class_name = script_base->get_path().trim_prefix("res://").quote();1518}1519if (!doc_class_name.is_empty() && !doc_data->class_list.find(doc_class_name)) {1520doc_class_name = String();1521}15221523class_icon = editor_data.get_script_icon(script_base->get_path());1524if (class_icon.is_null() && has_theme_icon(native_base, EditorStringName(EditorIcons))) {1525class_icon = get_editor_theme_icon(native_base);1526}15271528script_base->get_script_signal_list(&class_signals);15291530// TODO: Core: Add optional parameter to ignore base classes (no_inheritance like in ClassDB).1531Ref<Script> base = script_base->get_base_script();1532if (base.is_valid()) {1533List<MethodInfo> base_signals;1534base->get_script_signal_list(&base_signals);1535HashSet<String> base_signal_names;1536for (const MethodInfo &signal : base_signals) {1537base_signal_names.insert(signal.name);1538}1539for (List<MethodInfo>::Element *F = class_signals.front(); F;) {1540List<MethodInfo>::Element *N = F->next();1541if (base_signal_names.has(F->get().name)) {1542class_signals.erase(F);1543}1544F = N;1545}1546}15471548script_base = base;1549} else {1550class_name = native_base;1551doc_class_name = native_base;15521553if (!doc_data->class_list.find(doc_class_name)) {1554doc_class_name = String();1555}15561557if (has_theme_icon(native_base, EditorStringName(EditorIcons))) {1558class_icon = get_editor_theme_icon(native_base);1559}15601561ClassDB::get_signal_list(native_base, &class_signals, true);15621563native_base = ClassDB::get_parent_class(native_base);1564}15651566if (class_icon.is_null()) {1567class_icon = get_editor_theme_icon(SNAME("Object"));1568}15691570TreeItem *section_item = nullptr;15711572// Create subsections.1573if (!class_signals.is_empty()) {1574class_signals.sort();15751576section_item = tree->create_item(root);1577section_item->set_text(0, class_name);1578// `|` separators used in `EditorHelpBit`.1579section_item->set_tooltip_text(0, "class|" + doc_class_name + "|");1580section_item->set_icon(0, class_icon);1581section_item->set_selectable(0, false);1582section_item->set_editable(0, false);1583section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));1584section_item->set_metadata(0, doc_class_name);1585}15861587for (MethodInfo &mi : class_signals) {1588const StringName &signal_name = mi.name;1589if (!search_box->get_text().is_subsequence_ofn(signal_name)) {1590continue;1591}1592PackedStringArray argnames;15931594// Create the children of the subsection - the actual list of signals.1595TreeItem *signal_item = tree->create_item(section_item);1596String signame = connect_dialog->get_signature(mi, &argnames);1597signal_item->set_text(0, signame);15981599if (signame == prev_selected) {1600signal_item->select(0);1601prev_selected = "";1602}16031604Dictionary sinfo;1605sinfo["class"] = doc_class_name;1606sinfo["name"] = signal_name;1607sinfo["args"] = argnames;1608signal_item->set_metadata(0, sinfo);1609signal_item->set_icon(0, get_editor_theme_icon(SNAME("Signal")));1610// `|` separators used in `EditorHelpBit`.1611signal_item->set_tooltip_text(0, "signal|" + doc_class_name + "|" + String(signal_name));16121613// List existing connections.1614List<Object::Connection> existing_connections;1615selected_node->get_signal_connection_list(signal_name, &existing_connections);16161617for (const Object::Connection &F : existing_connections) {1618Connection connection = F;1619if (!(connection.flags & CONNECT_PERSIST)) {1620continue;1621}1622ConnectDialog::ConnectionData cd = connection;16231624Node *target = Object::cast_to<Node>(cd.target);1625if (!target) {1626continue;1627}16281629String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()";1630if (cd.flags & CONNECT_DEFERRED) {1631path += " (deferred)";1632}1633if (cd.flags & CONNECT_ONE_SHOT) {1634path += " (one-shot)";1635}1636if (cd.flags & CONNECT_APPEND_SOURCE_OBJECT) {1637path += " (source)";1638}1639if (cd.unbinds > 0) {1640path += " unbinds(" + itos(cd.unbinds) + ")";1641} else if (!cd.binds.is_empty()) {1642path += " binds(";1643for (int i = 0; i < cd.binds.size(); i++) {1644if (i > 0) {1645path += ", ";1646}1647path += cd.binds[i].operator String();1648}1649path += ")";1650}16511652TreeItem *connection_item = tree->create_item(signal_item);1653connection_item->set_text(0, path);1654connection_item->set_metadata(0, connection);1655connection_item->set_icon(0, get_editor_theme_icon(SNAME("Slot")));16561657if (_is_connection_inherited(connection)) {1658// The scene inherits this connection.1659connection_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));1660connection_item->set_meta("_inherited_connection", true);1661}1662}1663}1664}16651666connect_button->set_text(TTR("Connect..."));1667connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));1668connect_button->set_disabled(true);1669}16701671ConnectionsDock::ConnectionsDock() {1672set_name(TTR("Signals"));16731674VBoxContainer *vbc = this;16751676search_box = memnew(LineEdit);1677search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);1678search_box->set_placeholder(TTR("Filter Signals"));1679search_box->set_accessibility_name(TTRC("Filter Signals"));1680search_box->set_clear_button_enabled(true);1681search_box->connect(SceneStringName(text_changed), callable_mp(this, &ConnectionsDock::_filter_changed));1682vbc->add_child(search_box);16831684tree = memnew(ConnectionsDockTree);1685tree->set_accessibility_name(TTRC("Connections"));1686tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);1687tree->set_columns(1);1688tree->set_select_mode(Tree::SELECT_ROW);1689tree->set_hide_root(true);1690tree->set_column_clip_content(0, true);1691vbc->add_child(tree);1692tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);1693tree->set_allow_rmb_select(true);16941695connect_button = memnew(Button);1696connect_button->set_accessibility_name(TTRC("Connect"));1697HBoxContainer *hb = memnew(HBoxContainer);1698vbc->add_child(hb);1699hb->add_spacer();1700hb->add_child(connect_button);1701connect_button->connect(SceneStringName(pressed), callable_mp(this, &ConnectionsDock::_connect_pressed));17021703connect_dialog = memnew(ConnectDialog);1704connect_dialog->set_process_shortcut_input(true);1705add_child(connect_dialog);17061707disconnect_all_dialog = memnew(ConfirmationDialog);1708add_child(disconnect_all_dialog);1709disconnect_all_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ConnectionsDock::_disconnect_all));1710disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));17111712class_menu = memnew(PopupMenu);1713class_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_class_menu_option));1714class_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_class_menu_about_to_popup));1715class_menu->add_item(TTR("Open Documentation"), CLASS_MENU_OPEN_DOCS);1716add_child(class_menu);17171718signal_menu = memnew(PopupMenu);1719signal_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));1720signal_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_signal_menu_about_to_popup));1721signal_menu->add_item(TTR("Connect..."), SIGNAL_MENU_CONNECT);1722signal_menu->add_item(TTR("Disconnect All"), SIGNAL_MENU_DISCONNECT_ALL);1723signal_menu->add_item(TTR("Copy Name"), SIGNAL_MENU_COPY_NAME);1724signal_menu->add_separator();1725signal_menu->add_item(TTR("Open Documentation"), SIGNAL_MENU_OPEN_DOCS);1726add_child(signal_menu);17271728slot_menu = memnew(PopupMenu);1729slot_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));1730slot_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_slot_menu_about_to_popup));1731slot_menu->add_item(TTR("Edit..."), SLOT_MENU_EDIT);1732slot_menu->add_item(TTR("Go to Method"), SLOT_MENU_GO_TO_METHOD);1733slot_menu->add_shortcut(ED_SHORTCUT("connections_editor/disconnect", TTRC("Disconnect"), Key::KEY_DELETE), SLOT_MENU_DISCONNECT);1734add_child(slot_menu);17351736connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));1737tree->connect(SceneStringName(item_selected), callable_mp(this, &ConnectionsDock::_tree_item_selected));1738tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));1739tree->connect(SceneStringName(gui_input), callable_mp(this, &ConnectionsDock::_tree_gui_input));17401741add_theme_constant_override("separation", 3 * EDSCALE);1742}174317441745