Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/connections_dialog.cpp
9898 views
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
31
#include "connections_dialog.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/templates/hash_set.h"
35
#include "editor/doc/editor_help.h"
36
#include "editor/docks/node_dock.h"
37
#include "editor/docks/scene_tree_dock.h"
38
#include "editor/editor_main_screen.h"
39
#include "editor/editor_node.h"
40
#include "editor/editor_string_names.h"
41
#include "editor/editor_undo_redo_manager.h"
42
#include "editor/gui/editor_variant_type_selectors.h"
43
#include "editor/inspector/editor_inspector.h"
44
#include "editor/scene/scene_tree_editor.h"
45
#include "editor/script/script_editor_plugin.h"
46
#include "editor/settings/editor_settings.h"
47
#include "editor/themes/editor_scale.h"
48
#include "scene/gui/button.h"
49
#include "scene/gui/check_box.h"
50
#include "scene/gui/check_button.h"
51
#include "scene/gui/flow_container.h"
52
#include "scene/gui/label.h"
53
#include "scene/gui/line_edit.h"
54
#include "scene/gui/margin_container.h"
55
#include "scene/gui/popup_menu.h"
56
#include "scene/gui/spin_box.h"
57
58
static Node *_find_first_script(Node *p_root, Node *p_node) {
59
if (p_node != p_root && p_node->get_owner() != p_root) {
60
return nullptr;
61
}
62
if (!p_node->get_script().is_null()) {
63
return p_node;
64
}
65
66
for (int i = 0; i < p_node->get_child_count(); i++) {
67
Node *ret = _find_first_script(p_root, p_node->get_child(i));
68
if (ret) {
69
return ret;
70
}
71
}
72
73
return nullptr;
74
}
75
76
class ConnectDialogBinds : public Object {
77
GDCLASS(ConnectDialogBinds, Object);
78
79
public:
80
Vector<Variant> params;
81
82
bool _set(const StringName &p_name, const Variant &p_value) {
83
String name = p_name;
84
85
if (name.begins_with("bind/argument_")) {
86
int which = name.get_slicec('_', 1).to_int() - 1;
87
ERR_FAIL_INDEX_V(which, params.size(), false);
88
params.write[which] = p_value;
89
} else {
90
return false;
91
}
92
93
return true;
94
}
95
96
bool _get(const StringName &p_name, Variant &r_ret) const {
97
String name = p_name;
98
99
if (name.begins_with("bind/argument_")) {
100
int which = name.get_slicec('_', 1).to_int() - 1;
101
ERR_FAIL_INDEX_V(which, params.size(), false);
102
r_ret = params[which];
103
} else {
104
return false;
105
}
106
107
return true;
108
}
109
110
void _get_property_list(List<PropertyInfo> *p_list) const {
111
for (int i = 0; i < params.size(); i++) {
112
p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1)));
113
}
114
}
115
116
void notify_changed() {
117
notify_property_list_changed();
118
}
119
120
ConnectDialogBinds() {
121
}
122
};
123
124
/*
125
* Signal automatically called by parent dialog.
126
*/
127
void ConnectDialog::ok_pressed() {
128
String method_name = dst_method->get_text();
129
130
if (method_name.is_empty()) {
131
error->set_text(TTR("Method in target node must be specified."));
132
error->popup_centered();
133
return;
134
}
135
136
if (!TS->is_valid_identifier(method_name.strip_edges())) {
137
error->set_text(TTR("Method name must be a valid identifier."));
138
error->popup_centered();
139
return;
140
}
141
142
Node *target = tree->get_selected();
143
if (!target) {
144
return; // Nothing selected in the tree, not an error.
145
}
146
if (target->get_script().is_null()) {
147
if (!target->has_method(method_name)) {
148
error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node."));
149
error->popup_centered();
150
return;
151
}
152
}
153
emit_signal(SNAME("connected"));
154
hide();
155
}
156
157
void ConnectDialog::_cancel_pressed() {
158
hide();
159
}
160
161
void ConnectDialog::_item_activated() {
162
_ok_pressed(); // From AcceptDialog.
163
}
164
165
/*
166
* Called each time a target node is selected within the target node tree.
167
*/
168
void ConnectDialog::_tree_node_selected() {
169
Node *current = tree->get_selected();
170
171
if (!current) {
172
return;
173
}
174
175
dst_path = source->get_path_to(current);
176
if (!edit_mode) {
177
set_dst_method(generate_method_callback_name(source, signal, current));
178
}
179
_update_method_tree();
180
_update_warning_label();
181
_update_ok_enabled();
182
}
183
184
void ConnectDialog::_focus_currently_connected() {
185
tree->set_selected(source);
186
}
187
188
void ConnectDialog::_unbind_count_changed(double p_count) {
189
for (Control *control : bind_controls) {
190
BaseButton *b = Object::cast_to<BaseButton>(control);
191
if (b) {
192
b->set_disabled(p_count > 0);
193
}
194
195
EditorInspector *e = Object::cast_to<EditorInspector>(control);
196
if (e) {
197
e->set_read_only(p_count > 0);
198
}
199
}
200
201
append_source->set_disabled(p_count > 0);
202
}
203
204
void ConnectDialog::_method_selected() {
205
TreeItem *selected_item = method_tree->get_selected();
206
dst_method->set_text(selected_item->get_metadata(0));
207
}
208
209
/*
210
* Adds a new parameter bind to connection.
211
*/
212
void ConnectDialog::_add_bind() {
213
Variant::Type type = type_list->get_selected_type();
214
215
Variant value;
216
Callable::CallError err;
217
Variant::construct(type, value, nullptr, 0, err);
218
219
cdbinds->params.push_back(value);
220
cdbinds->notify_changed();
221
}
222
223
/*
224
* Remove parameter bind from connection.
225
*/
226
void ConnectDialog::_remove_bind() {
227
String st = bind_editor->get_selected_path();
228
if (st.is_empty()) {
229
return;
230
}
231
int idx = st.get_slicec('/', 1).to_int() - 1;
232
233
ERR_FAIL_INDEX(idx, cdbinds->params.size());
234
cdbinds->params.remove_at(idx);
235
cdbinds->notify_changed();
236
}
237
/*
238
* Automatically generates a name for the callback method.
239
*/
240
StringName ConnectDialog::generate_method_callback_name(Node *p_source, const String &p_signal_name, Node *p_target) {
241
String node_name = p_source->get_name();
242
for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner.
243
char32_t c = node_name[i];
244
if ((i == 0 && !is_unicode_identifier_start(c)) || (i > 0 && !is_unicode_identifier_continue(c))) {
245
if (c == ' ') {
246
// Replace spaces with underlines.
247
c = '_';
248
} else {
249
// Remove any other characters.
250
node_name.remove_at(i);
251
i--;
252
continue;
253
}
254
}
255
node_name[i] = c;
256
}
257
258
Dictionary subst;
259
subst["NodeName"] = node_name.to_pascal_case();
260
subst["nodeName"] = node_name.to_camel_case();
261
subst["node_name"] = node_name.to_snake_case();
262
subst["node-name"] = node_name.to_kebab_case();
263
264
subst["SignalName"] = p_signal_name.to_pascal_case();
265
subst["signalName"] = p_signal_name.to_camel_case();
266
subst["signal_name"] = p_signal_name.to_snake_case();
267
subst["signal-name"] = p_signal_name.to_kebab_case();
268
269
String dst_method;
270
if (p_source == p_target) {
271
dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_to_self_name")).format(subst);
272
} else {
273
dst_method = String(GLOBAL_GET("editor/naming/default_signal_callback_name")).format(subst);
274
}
275
276
return dst_method;
277
}
278
279
void ConnectDialog::_create_method_tree_items(const List<MethodInfo> &p_methods, TreeItem *p_parent_item) {
280
for (const MethodInfo &mi : p_methods) {
281
TreeItem *method_item = method_tree->create_item(p_parent_item);
282
method_item->set_text(0, get_signature(mi));
283
method_item->set_metadata(0, mi.name);
284
}
285
}
286
287
List<MethodInfo> ConnectDialog::_filter_method_list(const List<MethodInfo> &p_methods, const MethodInfo &p_signal, const String &p_search_string) const {
288
bool check_signal = compatible_methods_only->is_pressed();
289
List<MethodInfo> ret;
290
291
LocalVector<Pair<Variant::Type, StringName>> effective_args;
292
int unbind = get_unbinds();
293
effective_args.reserve(MAX(p_signal.arguments.size() - unbind, 0));
294
for (int64_t i = 0; i < p_signal.arguments.size() - unbind; i++) {
295
PropertyInfo pi = p_signal.arguments[i];
296
effective_args.push_back(Pair(pi.type, pi.class_name));
297
}
298
if (unbind == 0) {
299
for (const Variant &variant : get_binds()) {
300
effective_args.push_back(Pair(variant.get_type(), StringName()));
301
}
302
}
303
304
for (const MethodInfo &mi : p_methods) {
305
if (mi.name.begins_with("@")) {
306
// GH-92782. GDScript inline setters/getters are historically present in `get_method_list()`
307
// and can be called using `Object.call()`. However, these functions are meant to be internal
308
// and their names are not valid identifiers, so let's hide them from the user.
309
continue;
310
}
311
312
if (!p_search_string.is_empty() && !mi.name.containsn(p_search_string)) {
313
continue;
314
}
315
316
if (check_signal) {
317
const unsigned min_argc = mi.arguments.size() - mi.default_arguments.size();
318
const unsigned max_argc = (mi.flags & METHOD_FLAG_VARARG) ? UINT_MAX : mi.arguments.size();
319
320
if (effective_args.size() < min_argc || effective_args.size() > max_argc) {
321
continue;
322
}
323
324
bool type_mismatch = false;
325
for (int64_t i = 0; i < effective_args.size() && i < mi.arguments.size(); ++i) {
326
Variant::Type stype = effective_args[i].first;
327
Variant::Type mtype = mi.arguments[i].type;
328
329
if (stype != Variant::NIL && mtype != Variant::NIL && stype != mtype) {
330
type_mismatch = true;
331
break;
332
}
333
334
if (stype == Variant::OBJECT && mtype == Variant::OBJECT && !ClassDB::is_parent_class(effective_args[i].second, mi.arguments[i].class_name)) {
335
type_mismatch = true;
336
break;
337
}
338
}
339
340
if (type_mismatch) {
341
continue;
342
}
343
}
344
345
ret.push_back(mi);
346
}
347
348
return ret;
349
}
350
351
void ConnectDialog::_update_method_tree() {
352
method_tree->clear();
353
354
Color disabled_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor)) * 0.7;
355
String search_string = method_search->get_text();
356
Node *target = tree->get_selected();
357
if (!target) {
358
return;
359
}
360
361
MethodInfo signal_info;
362
if (compatible_methods_only->is_pressed()) {
363
List<MethodInfo> signals;
364
source->get_signal_list(&signals);
365
for (const MethodInfo &mi : signals) {
366
if (mi.name == signal) {
367
signal_info = mi;
368
break;
369
}
370
}
371
}
372
373
TreeItem *root_item = method_tree->create_item();
374
root_item->set_text(0, TTR("Methods"));
375
root_item->set_selectable(0, false);
376
377
// If a script is attached, get methods from it.
378
ScriptInstance *si = target->get_script_instance();
379
if (si) {
380
if (si->get_script()->is_built_in()) {
381
si->get_script()->reload();
382
}
383
List<MethodInfo> methods;
384
si->get_method_list(&methods);
385
methods = _filter_method_list(methods, signal_info, search_string);
386
387
if (!methods.is_empty()) {
388
TreeItem *si_item = method_tree->create_item(root_item);
389
si_item->set_text(0, TTR("Attached Script"));
390
si_item->set_icon(0, get_editor_theme_icon(SNAME("Script")));
391
si_item->set_selectable(0, false);
392
393
_create_method_tree_items(methods, si_item);
394
}
395
}
396
397
if (script_methods_only->is_pressed()) {
398
empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
399
return;
400
}
401
402
// Get methods from each class in the hierarchy.
403
StringName current_class = target->get_class_name();
404
do {
405
TreeItem *class_item = method_tree->create_item(root_item);
406
class_item->set_text(0, current_class);
407
Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Node"));
408
if (has_theme_icon(current_class, EditorStringName(EditorIcons))) {
409
icon = get_editor_theme_icon(current_class);
410
}
411
class_item->set_icon(0, icon);
412
class_item->set_selectable(0, false);
413
414
List<MethodInfo> methods;
415
ClassDB::get_method_list(current_class, &methods, true);
416
methods = _filter_method_list(methods, signal_info, search_string);
417
418
if (methods.is_empty()) {
419
class_item->set_custom_color(0, disabled_color);
420
} else {
421
_create_method_tree_items(methods, class_item);
422
}
423
current_class = ClassDB::get_parent_class_nocheck(current_class);
424
} while (current_class != StringName());
425
426
empty_tree_label->set_visible(root_item->get_first_child() == nullptr);
427
}
428
429
void ConnectDialog::_method_check_button_pressed(const CheckButton *p_button) {
430
if (p_button == script_methods_only) {
431
EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_script_methods_only", p_button->is_pressed());
432
} else if (p_button == compatible_methods_only) {
433
EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_compatible_methods_only", p_button->is_pressed());
434
}
435
_update_method_tree();
436
}
437
438
void ConnectDialog::_open_method_popup() {
439
method_popup->popup_centered();
440
method_search->clear();
441
method_search->grab_focus();
442
}
443
444
/*
445
* Enables or disables the connect button. The connect button is enabled if a
446
* node is selected and valid in the selected mode.
447
*/
448
void ConnectDialog::_update_ok_enabled() {
449
Node *target = tree->get_selected();
450
451
if (target == nullptr) {
452
get_ok_button()->set_disabled(true);
453
return;
454
}
455
456
if (dst_method->get_text().is_empty()) {
457
get_ok_button()->set_disabled(true);
458
return;
459
}
460
461
get_ok_button()->set_disabled(false);
462
}
463
464
void ConnectDialog::_update_warning_label() {
465
Node *dst = source->get_node(dst_path);
466
if (dst == nullptr) {
467
warning_label->set_visible(false);
468
return;
469
}
470
471
Ref<Script> scr = dst->get_script();
472
if (scr.is_null()) {
473
warning_label->set_visible(false);
474
return;
475
}
476
477
ScriptLanguage *language = scr->get_language();
478
if (language->can_make_function()) {
479
warning_label->set_visible(false);
480
return;
481
}
482
483
warning_label->set_text(vformat(TTR("%s: Callback code won't be generated, please add it manually."), language->get_name()));
484
warning_label->set_visible(true);
485
}
486
487
void ConnectDialog::_post_popup() {
488
callable_mp((Control *)dst_method, &Control::grab_focus).call_deferred();
489
callable_mp(dst_method, &LineEdit::select_all).call_deferred();
490
}
491
492
void ConnectDialog::_notification(int p_what) {
493
switch (p_what) {
494
case NOTIFICATION_ENTER_TREE: {
495
bind_editor->edit(cdbinds);
496
497
[[fallthrough]];
498
}
499
case NOTIFICATION_THEME_CHANGED: {
500
method_search->set_right_icon(get_editor_theme_icon("Search"));
501
open_method_tree->set_button_icon(get_editor_theme_icon("Edit"));
502
} break;
503
}
504
}
505
506
void ConnectDialog::_bind_methods() {
507
ADD_SIGNAL(MethodInfo("connected"));
508
}
509
510
Node *ConnectDialog::get_source() const {
511
return source;
512
}
513
514
ConnectDialog::ConnectionData ConnectDialog::get_source_connection_data() const {
515
return source_connection_data;
516
}
517
518
StringName ConnectDialog::get_signal_name() const {
519
return signal;
520
}
521
522
PackedStringArray ConnectDialog::get_signal_args() const {
523
return signal_args;
524
}
525
526
NodePath ConnectDialog::get_dst_path() const {
527
return dst_path;
528
}
529
530
void ConnectDialog::set_dst_node(Node *p_node) {
531
tree->set_selected(p_node);
532
}
533
534
StringName ConnectDialog::get_dst_method_name() const {
535
String txt = dst_method->get_text();
536
if (txt.contains_char('(')) {
537
txt = txt.left(txt.find_char('(')).strip_edges();
538
}
539
return txt;
540
}
541
542
void ConnectDialog::set_dst_method(const StringName &p_method) {
543
dst_method->set_text(p_method);
544
}
545
546
int ConnectDialog::get_unbinds() const {
547
return int(unbind_count->get_value());
548
}
549
550
Vector<Variant> ConnectDialog::get_binds() const {
551
return cdbinds->params;
552
}
553
554
String ConnectDialog::get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names) {
555
PackedStringArray signature;
556
signature.append(p_method.name);
557
signature.append("(");
558
559
for (int64_t i = 0; i < p_method.arguments.size(); ++i) {
560
if (i > 0) {
561
signature.append(", ");
562
}
563
564
const PropertyInfo &pi = p_method.arguments[i];
565
String type_name;
566
switch (pi.type) {
567
case Variant::NIL:
568
type_name = "Variant";
569
break;
570
case Variant::INT:
571
if ((pi.usage & PROPERTY_USAGE_CLASS_IS_ENUM) && pi.class_name != StringName() && !String(pi.class_name).begins_with("res://")) {
572
type_name = pi.class_name;
573
} else {
574
type_name = "int";
575
}
576
break;
577
case Variant::ARRAY:
578
if (pi.hint == PROPERTY_HINT_ARRAY_TYPE && !pi.hint_string.is_empty() && !pi.hint_string.begins_with("res://")) {
579
type_name = "Array[" + pi.hint_string + "]";
580
} else {
581
type_name = "Array";
582
}
583
break;
584
case Variant::DICTIONARY:
585
type_name = "Dictionary";
586
if (pi.hint == PROPERTY_HINT_DICTIONARY_TYPE && !pi.hint_string.is_empty()) {
587
String key_hint = pi.hint_string.get_slicec(';', 0);
588
String value_hint = pi.hint_string.get_slicec(';', 1);
589
if (key_hint.is_empty() || key_hint.begins_with("res://")) {
590
key_hint = "Variant";
591
}
592
if (value_hint.is_empty() || value_hint.begins_with("res://")) {
593
value_hint = "Variant";
594
}
595
if (key_hint != "Variant" || value_hint != "Variant") {
596
type_name += "[" + key_hint + ", " + value_hint + "]";
597
}
598
}
599
break;
600
case Variant::OBJECT:
601
if (pi.class_name != StringName()) {
602
type_name = pi.class_name;
603
} else {
604
type_name = "Object";
605
}
606
break;
607
default:
608
type_name = Variant::get_type_name(pi.type);
609
break;
610
}
611
612
String arg_name = pi.name.is_empty() ? "arg" + itos(i) : pi.name;
613
signature.append(arg_name + ": " + type_name);
614
if (r_arg_names) {
615
r_arg_names->push_back(arg_name + ": " + type_name);
616
}
617
}
618
619
if (p_method.flags & METHOD_FLAG_VARARG) {
620
signature.append(p_method.arguments.is_empty() ? "..." : ", ...");
621
}
622
623
signature.append(")");
624
return String().join(signature);
625
}
626
627
bool ConnectDialog::get_deferred() const {
628
return deferred->is_pressed();
629
}
630
631
bool ConnectDialog::get_one_shot() const {
632
return one_shot->is_pressed();
633
}
634
635
bool ConnectDialog::get_append_source() const {
636
return !append_source->is_disabled() && append_source->is_pressed();
637
}
638
639
/*
640
* Returns true if ConnectDialog is being used to edit an existing connection.
641
*/
642
bool ConnectDialog::is_editing() const {
643
return edit_mode;
644
}
645
646
void ConnectDialog::shortcut_input(const Ref<InputEvent> &p_event) {
647
const Ref<InputEventKey> &key = p_event;
648
649
if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
650
if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
651
filter_nodes->grab_focus();
652
filter_nodes->select_all();
653
filter_nodes->accept_event();
654
}
655
}
656
}
657
658
/*
659
* Initialize ConnectDialog and populate fields with expected data.
660
* If creating a connection from scratch, sensible defaults are used.
661
* If editing an existing connection, previous data is retained.
662
*/
663
void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit) {
664
set_hide_on_ok(false);
665
666
source = static_cast<Node *>(p_cd.source);
667
signal = p_cd.signal;
668
signal_args = p_signal_args;
669
670
tree->set_selected(nullptr);
671
tree->set_marked(source);
672
673
if (p_cd.target) {
674
set_dst_node(static_cast<Node *>(p_cd.target));
675
set_dst_method(p_cd.method);
676
}
677
678
_update_ok_enabled();
679
680
bool b_deferred = (p_cd.flags & CONNECT_DEFERRED);
681
bool b_oneshot = (p_cd.flags & CONNECT_ONE_SHOT);
682
bool b_append_source = (p_cd.flags & CONNECT_APPEND_SOURCE_OBJECT);
683
684
deferred->set_pressed(b_deferred);
685
one_shot->set_pressed(b_oneshot);
686
append_source->set_pressed(b_append_source);
687
688
unbind_count->set_max(p_signal_args.size());
689
unbind_count->set_value(p_cd.unbinds);
690
_unbind_count_changed(p_cd.unbinds);
691
692
cdbinds->params.clear();
693
cdbinds->params = p_cd.binds;
694
cdbinds->notify_changed();
695
696
edit_mode = p_edit;
697
698
source_connection_data = p_cd;
699
}
700
701
void ConnectDialog::popup_dialog(const String &p_for_signal) {
702
from_signal->set_text(p_for_signal);
703
warning_label->add_theme_color_override(SceneStringName(font_color), warning_label->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
704
error_label->add_theme_color_override(SceneStringName(font_color), error_label->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
705
filter_nodes->clear();
706
707
if (!advanced->is_pressed()) {
708
error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
709
}
710
711
if (first_popup) {
712
first_popup = false;
713
_advanced_pressed();
714
}
715
716
popup_centered();
717
}
718
719
void ConnectDialog::_advanced_pressed() {
720
if (advanced->is_pressed()) {
721
connect_to_label->set_text(TTR("Connect to Node:"));
722
tree->set_connect_to_script_mode(false);
723
724
vbc_right->show();
725
error_label->hide();
726
} else {
727
reset_size();
728
connect_to_label->set_text(TTR("Connect to Script:"));
729
tree->set_connect_to_script_mode(true);
730
731
vbc_right->hide();
732
error_label->set_visible(!_find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()));
733
}
734
735
EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "use_advanced_connections", advanced->is_pressed());
736
737
popup_centered();
738
}
739
740
ConnectDialog::ConnectDialog() {
741
set_min_size(Size2(0, 500) * EDSCALE);
742
743
HBoxContainer *main_hb = memnew(HBoxContainer);
744
add_child(main_hb);
745
746
VBoxContainer *vbc_left = memnew(VBoxContainer);
747
main_hb->add_child(vbc_left);
748
vbc_left->set_h_size_flags(Control::SIZE_EXPAND_FILL);
749
vbc_left->set_custom_minimum_size(Vector2(400 * EDSCALE, 0));
750
751
from_signal = memnew(LineEdit);
752
from_signal->set_accessibility_name(TTRC("From Signal:"));
753
vbc_left->add_margin_child(TTR("From Signal:"), from_signal);
754
from_signal->set_editable(false);
755
756
tree = memnew(SceneTreeEditor(false));
757
tree->set_update_when_invisible(false);
758
tree->set_connecting_signal(true);
759
tree->set_show_enabled_subscene(true);
760
tree->set_v_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
761
tree->get_scene_tree()->connect("item_activated", callable_mp(this, &ConnectDialog::_item_activated));
762
tree->connect("node_selected", callable_mp(this, &ConnectDialog::_tree_node_selected));
763
tree->set_connect_to_script_mode(true);
764
765
HBoxContainer *hbc_filter = memnew(HBoxContainer);
766
767
filter_nodes = memnew(LineEdit);
768
hbc_filter->add_child(filter_nodes);
769
filter_nodes->set_h_size_flags(Control::SIZE_FILL | Control::SIZE_EXPAND);
770
filter_nodes->set_placeholder(TTR("Filter Nodes"));
771
filter_nodes->set_accessibility_name(TTRC("Filter Nodes"));
772
filter_nodes->set_clear_button_enabled(true);
773
filter_nodes->connect(SceneStringName(text_changed), callable_mp(tree, &SceneTreeEditor::set_filter));
774
775
Button *focus_current = memnew(Button);
776
hbc_filter->add_child(focus_current);
777
focus_current->set_text(TTR("Go to Source"));
778
focus_current->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_focus_currently_connected));
779
780
Node *mc = vbc_left->add_margin_child(TTR("Connect to Script:"), hbc_filter, false);
781
connect_to_label = Object::cast_to<Label>(vbc_left->get_child(mc->get_index() - 1));
782
vbc_left->add_child(tree);
783
784
warning_label = memnew(Label);
785
warning_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
786
vbc_left->add_child(warning_label);
787
warning_label->hide();
788
789
error_label = memnew(Label);
790
error_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
791
error_label->set_text(TTR("Scene does not contain any script."));
792
vbc_left->add_child(error_label);
793
error_label->hide();
794
795
method_popup = memnew(AcceptDialog);
796
method_popup->set_title(TTR("Select Method"));
797
method_popup->set_min_size(Vector2(400, 600) * EDSCALE);
798
add_child(method_popup);
799
800
VBoxContainer *method_vbc = memnew(VBoxContainer);
801
method_popup->add_child(method_vbc);
802
803
method_search = memnew(LineEdit);
804
method_vbc->add_child(method_search);
805
method_search->set_placeholder(TTR("Filter Methods"));
806
method_search->set_accessibility_name(TTRC("Filter Methods"));
807
method_search->set_clear_button_enabled(true);
808
method_search->connect(SceneStringName(text_changed), callable_mp(this, &ConnectDialog::_update_method_tree).unbind(1));
809
810
method_tree = memnew(Tree);
811
method_vbc->add_child(method_tree);
812
method_tree->set_accessibility_name(TTRC("Methods"));
813
method_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
814
method_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
815
method_tree->set_hide_root(true);
816
method_tree->connect(SceneStringName(item_selected), callable_mp(this, &ConnectDialog::_method_selected));
817
method_tree->connect("item_activated", callable_mp((Window *)method_popup, &Window::hide));
818
819
empty_tree_label = memnew(Label(TTR("No method found matching given filters.")));
820
method_popup->add_child(empty_tree_label);
821
empty_tree_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
822
empty_tree_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
823
empty_tree_label->set_autowrap_mode(TextServer::AUTOWRAP_WORD);
824
825
script_methods_only = memnew(CheckButton(TTR("Script Methods Only")));
826
method_vbc->add_child(script_methods_only);
827
script_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
828
script_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_script_methods_only", true));
829
script_methods_only->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(script_methods_only));
830
831
compatible_methods_only = memnew(CheckButton(TTR("Compatible Methods Only")));
832
method_vbc->add_child(compatible_methods_only);
833
compatible_methods_only->set_h_size_flags(Control::SIZE_SHRINK_END);
834
compatible_methods_only->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_compatible_methods_only", true));
835
compatible_methods_only->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_method_check_button_pressed).bind(compatible_methods_only));
836
837
vbc_right = memnew(VBoxContainer);
838
main_hb->add_child(vbc_right);
839
vbc_right->set_h_size_flags(Control::SIZE_EXPAND_FILL);
840
vbc_right->set_custom_minimum_size(Vector2(150 * EDSCALE, 0));
841
vbc_right->hide();
842
843
HBoxContainer *add_bind_hb = memnew(HBoxContainer);
844
845
type_list = memnew(EditorVariantTypeOptionButton);
846
type_list->set_accessibility_name(TTRC("Type"));
847
type_list->set_h_size_flags(Control::SIZE_EXPAND_FILL);
848
type_list->populate({ Variant::NIL, Variant::OBJECT });
849
add_bind_hb->add_child(type_list);
850
bind_controls.push_back(type_list);
851
852
Button *add_bind = memnew(Button);
853
add_bind->set_text(TTR("Add"));
854
add_bind_hb->add_child(add_bind);
855
add_bind->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_add_bind));
856
bind_controls.push_back(add_bind);
857
858
Button *del_bind = memnew(Button);
859
del_bind->set_text(TTR("Remove"));
860
add_bind_hb->add_child(del_bind);
861
del_bind->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_remove_bind));
862
bind_controls.push_back(del_bind);
863
864
vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb);
865
866
bind_editor = memnew(EditorInspector);
867
bind_editor->set_accessibility_name(TTRC("Extra Call Arguments:"));
868
bind_controls.push_back(bind_editor);
869
870
vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true);
871
872
unbind_count = memnew(SpinBox);
873
unbind_count->set_tooltip_text(TTR("Allows to drop arguments sent by signal emitter."));
874
unbind_count->set_accessibility_name(TTRC("Unbind Signal Arguments:"));
875
unbind_count->connect(SceneStringName(value_changed), callable_mp(this, &ConnectDialog::_unbind_count_changed));
876
877
vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);
878
879
HBoxContainer *hbc_method = memnew(HBoxContainer);
880
vbc_left->add_margin_child(TTR("Receiver Method:"), hbc_method);
881
882
dst_method = memnew(LineEdit);
883
dst_method->set_accessibility_name(TTRC("Receiver Method"));
884
dst_method->set_h_size_flags(Control::SIZE_EXPAND_FILL);
885
dst_method->connect(SceneStringName(text_changed), callable_mp(method_tree, &Tree::deselect_all).unbind(1));
886
hbc_method->add_child(dst_method);
887
register_text_enter(dst_method);
888
889
open_method_tree = memnew(Button(TTRC("Pick")));
890
hbc_method->add_child(open_method_tree);
891
open_method_tree->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_open_method_popup));
892
893
advanced = memnew(CheckButton(TTR("Advanced")));
894
vbc_left->add_child(advanced);
895
advanced->set_h_size_flags(Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND);
896
advanced->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "use_advanced_connections", false));
897
advanced->connect(SceneStringName(pressed), callable_mp(this, &ConnectDialog::_advanced_pressed));
898
899
FlowContainer *fc_flags = memnew(FlowContainer);
900
vbc_right->add_child(fc_flags);
901
902
deferred = memnew(CheckBox);
903
deferred->set_text(TTR("Deferred"));
904
deferred->set_tooltip_text(TTR("Defers the signal, storing it in a queue and only firing it at idle time."));
905
fc_flags->add_child(deferred);
906
907
one_shot = memnew(CheckBox);
908
one_shot->set_text(TTR("One Shot"));
909
one_shot->set_tooltip_text(TTR("Disconnects the signal after its first emission."));
910
fc_flags->add_child(one_shot);
911
912
append_source = memnew(CheckBox);
913
append_source->set_text(TTRC("Append Source"));
914
append_source->set_tooltip_text(TTRC("The source object is automatically sent when the signal is emitted."));
915
fc_flags->add_child(append_source);
916
917
cdbinds = memnew(ConnectDialogBinds);
918
919
error = memnew(AcceptDialog);
920
add_child(error);
921
error->set_title(TTR("Cannot connect signal"));
922
error->set_ok_button_text(TTR("Close"));
923
set_ok_button_text(TTR("Connect"));
924
}
925
926
ConnectDialog::~ConnectDialog() {
927
memdelete(cdbinds);
928
}
929
930
//////////////////////////////////////////
931
932
Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const {
933
// If it's not a doc tooltip, fallback to the default one.
934
if (p_text.is_empty() || p_text.contains(" :: ")) {
935
return nullptr;
936
}
937
938
return EditorHelpBitTooltip::show_tooltip(const_cast<ConnectionsDockTree *>(this), p_text);
939
}
940
941
struct _ConnectionsDockMethodInfoSort {
942
_FORCE_INLINE_ bool operator()(const MethodInfo &a, const MethodInfo &b) const {
943
return a.name < b.name;
944
}
945
};
946
947
void ConnectionsDock::_filter_changed(const String &p_text) {
948
update_tree();
949
}
950
951
/*
952
* Post-ConnectDialog callback for creating/editing connections.
953
* Creates or edits connections based on state of the ConnectDialog when "Connect" is pressed.
954
*/
955
void ConnectionsDock::_make_or_edit_connection() {
956
NodePath dst_path = connect_dialog->get_dst_path();
957
Node *target = selected_node->get_node(dst_path);
958
ERR_FAIL_NULL(target);
959
960
ConnectDialog::ConnectionData cd;
961
cd.source = connect_dialog->get_source();
962
cd.target = target;
963
cd.signal = connect_dialog->get_signal_name();
964
cd.method = connect_dialog->get_dst_method_name();
965
cd.unbinds = connect_dialog->get_unbinds();
966
if (cd.unbinds == 0) {
967
cd.binds = connect_dialog->get_binds();
968
}
969
bool b_deferred = connect_dialog->get_deferred();
970
bool b_oneshot = connect_dialog->get_one_shot();
971
bool b_append_source = connect_dialog->get_append_source();
972
cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONE_SHOT : 0) | (b_append_source ? CONNECT_APPEND_SOURCE_OBJECT : 0);
973
974
// If the function is found in target's own script, check the editor setting
975
// to determine if the script should be opened.
976
// If the function is found in an inherited class or script no need to do anything
977
// except making a connection.
978
bool add_script_function_request = false;
979
Ref<Script> scr = target->get_script();
980
981
if (scr.is_valid() && !ClassDB::has_method(target->get_class(), cd.method)) {
982
// Check in target's own script.
983
int line = scr->get_language()->find_function(cd.method, scr->get_source_code());
984
if (line != -1) {
985
add_script_function_request = EDITOR_GET("text_editor/behavior/navigation/open_script_when_connecting_signal_to_existing_method");
986
} else {
987
// There is a chance that the method is inherited from another script.
988
bool found_inherited_function = false;
989
Ref<Script> inherited_scr = scr->get_base_script();
990
while (inherited_scr.is_valid()) {
991
int inherited_line = inherited_scr->get_language()->find_function(cd.method, inherited_scr->get_source_code());
992
if (inherited_line != -1) {
993
found_inherited_function = true;
994
break;
995
}
996
997
inherited_scr = inherited_scr->get_base_script();
998
}
999
1000
add_script_function_request = !found_inherited_function;
1001
}
1002
}
1003
1004
if (add_script_function_request) {
1005
PackedStringArray script_function_args = connect_dialog->get_signal_args();
1006
script_function_args.resize(script_function_args.size() - cd.unbinds);
1007
1008
// Append the source.
1009
if (b_append_source) {
1010
String class_name = cd.source->get_class();
1011
bool found = false;
1012
1013
Ref<Script> source_script = cd.source->get_script();
1014
if (source_script.is_valid()) {
1015
found = source_script->has_script_signal(cd.signal);
1016
if (found) {
1017
// Check global name in script inheritance chain.
1018
bool need_check = found;
1019
Ref<Script> base_script = source_script->get_base_script();
1020
while (base_script.is_valid()) {
1021
need_check = base_script->has_script_signal(cd.signal);
1022
if (!need_check) {
1023
break;
1024
}
1025
source_script = base_script;
1026
base_script = source_script->get_base_script();
1027
}
1028
class_name = source_script->get_global_name();
1029
}
1030
}
1031
1032
if (!found) {
1033
while (!class_name.is_empty()) {
1034
// Search in ClassDB according to the inheritance chain.
1035
found = ClassDB::has_signal(class_name, cd.signal, true);
1036
if (found) {
1037
break;
1038
}
1039
class_name = ClassDB::get_parent_class(class_name);
1040
}
1041
}
1042
1043
script_function_args.push_back("source:" + class_name);
1044
}
1045
1046
for (int i = 0; i < cd.binds.size(); i++) {
1047
script_function_args.push_back("extra_arg_" + itos(i) + ": " + Variant::get_type_name(cd.binds[i].get_type()));
1048
}
1049
1050
EditorNode::get_singleton()->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args);
1051
}
1052
1053
if (connect_dialog->is_editing()) {
1054
_disconnect(connect_dialog->get_source_connection_data());
1055
_connect(cd);
1056
} else {
1057
_connect(cd);
1058
}
1059
1060
update_tree();
1061
}
1062
1063
/*
1064
* Creates single connection w/ undo-redo functionality.
1065
*/
1066
void ConnectionsDock::_connect(const ConnectDialog::ConnectionData &p_cd) {
1067
Node *source = Object::cast_to<Node>(p_cd.source);
1068
Node *target = Object::cast_to<Node>(p_cd.target);
1069
1070
if (!source || !target) {
1071
return;
1072
}
1073
1074
Callable callable = p_cd.get_callable();
1075
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1076
undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method)));
1077
undo_redo->add_do_method(source, "connect", p_cd.signal, callable, p_cd.flags);
1078
undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable);
1079
undo_redo->add_do_method(this, "update_tree");
1080
undo_redo->add_undo_method(this, "update_tree");
1081
undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
1082
undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
1083
1084
undo_redo->commit_action();
1085
}
1086
1087
/*
1088
* Break single connection w/ undo-redo functionality.
1089
*/
1090
void ConnectionsDock::_disconnect(const ConnectDialog::ConnectionData &p_cd) {
1091
ERR_FAIL_COND(p_cd.source != selected_node); // Shouldn't happen but... Bugcheck.
1092
1093
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1094
undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), p_cd.signal, p_cd.method));
1095
1096
Callable callable = p_cd.get_callable();
1097
undo_redo->add_do_method(selected_node, "disconnect", p_cd.signal, callable);
1098
undo_redo->add_undo_method(selected_node, "connect", p_cd.signal, callable, p_cd.flags);
1099
undo_redo->add_do_method(this, "update_tree");
1100
undo_redo->add_undo_method(this, "update_tree");
1101
undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree"); // To force redraw of scene tree.
1102
undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
1103
1104
undo_redo->commit_action();
1105
}
1106
1107
/*
1108
* Break all connections of currently selected signal.
1109
* Can undo-redo as a single action.
1110
*/
1111
void ConnectionsDock::_disconnect_all() {
1112
TreeItem *item = tree->get_selected();
1113
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
1114
return;
1115
}
1116
1117
TreeItem *child = item->get_first_child();
1118
String signal_name = item->get_metadata(0).operator Dictionary()["name"];
1119
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1120
undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name));
1121
1122
while (child) {
1123
Connection connection = child->get_metadata(0);
1124
if (!_is_connection_inherited(connection)) {
1125
ConnectDialog::ConnectionData cd = connection;
1126
undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable());
1127
undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.flags);
1128
}
1129
child = child->get_next();
1130
}
1131
1132
undo_redo->add_do_method(this, "update_tree");
1133
undo_redo->add_undo_method(this, "update_tree");
1134
undo_redo->add_do_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
1135
undo_redo->add_undo_method(SceneTreeDock::get_singleton()->get_tree_editor(), "update_tree");
1136
1137
undo_redo->commit_action();
1138
}
1139
1140
void ConnectionsDock::_tree_item_selected() {
1141
TreeItem *item = tree->get_selected();
1142
if (item && _get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
1143
connect_button->set_text(TTR("Connect..."));
1144
connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));
1145
connect_button->set_disabled(false);
1146
} else if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
1147
connect_button->set_text(TTR("Disconnect"));
1148
connect_button->set_button_icon(get_editor_theme_icon(SNAME("Unlinked")));
1149
1150
Object::Connection connection = item->get_metadata(0);
1151
connect_button->set_disabled(_is_connection_inherited(connection));
1152
} else {
1153
connect_button->set_text(TTR("Connect..."));
1154
connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));
1155
connect_button->set_disabled(true);
1156
}
1157
}
1158
1159
void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click.
1160
TreeItem *item = tree->get_selected();
1161
if (!item) {
1162
return;
1163
}
1164
1165
if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
1166
_open_connection_dialog(*item);
1167
} else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
1168
_go_to_method(*item);
1169
}
1170
}
1171
1172
ConnectionsDock::TreeItemType ConnectionsDock::_get_item_type(const TreeItem &p_item) const {
1173
if (&p_item == tree->get_root()) {
1174
return TREE_ITEM_TYPE_ROOT;
1175
} else if (p_item.get_parent() == tree->get_root()) {
1176
return TREE_ITEM_TYPE_CLASS;
1177
} else if (p_item.get_parent()->get_parent() == tree->get_root()) {
1178
return TREE_ITEM_TYPE_SIGNAL;
1179
} else {
1180
return TREE_ITEM_TYPE_CONNECTION;
1181
}
1182
}
1183
1184
bool ConnectionsDock::_is_connection_inherited(Connection &p_connection) {
1185
return bool(p_connection.flags & CONNECT_INHERITED);
1186
}
1187
1188
/*
1189
* Open connection dialog with TreeItem data to CREATE a brand-new connection.
1190
*/
1191
void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) {
1192
const Dictionary sinfo = p_item.get_metadata(0);
1193
const StringName signal_name = sinfo["name"];
1194
const PackedStringArray signal_args = sinfo["args"];
1195
1196
Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node;
1197
if (!dst_node || dst_node->get_script().is_null()) {
1198
dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root());
1199
}
1200
1201
ConnectDialog::ConnectionData cd;
1202
cd.source = selected_node;
1203
cd.signal = signal_name;
1204
cd.target = dst_node;
1205
cd.method = ConnectDialog::generate_method_callback_name(cd.source, signal_name, cd.target);
1206
connect_dialog->init(cd, signal_args);
1207
connect_dialog->set_title(TTR("Connect a Signal to a Method"));
1208
connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");
1209
}
1210
1211
/*
1212
* Open connection dialog with Connection data to EDIT an existing connection.
1213
*/
1214
void ConnectionsDock::_open_edit_connection_dialog(TreeItem &p_item) {
1215
TreeItem *signal_item = p_item.get_parent();
1216
ERR_FAIL_NULL(signal_item);
1217
1218
Connection connection = p_item.get_metadata(0);
1219
ConnectDialog::ConnectionData cd = connection;
1220
1221
Node *src = Object::cast_to<Node>(cd.source);
1222
Node *dst = Object::cast_to<Node>(cd.target);
1223
1224
if (src && dst) {
1225
const StringName &signal_name = cd.signal;
1226
const PackedStringArray signal_args = signal_item->get_metadata(0).operator Dictionary()["args"];
1227
1228
connect_dialog->init(cd, signal_args, true);
1229
connect_dialog->set_title(vformat(TTR("Edit Connection: '%s'"), cd.signal));
1230
connect_dialog->popup_dialog(signal_name.operator String() + "(" + String(", ").join(signal_args) + ")");
1231
}
1232
}
1233
1234
/*
1235
* Open slot method location in script editor.
1236
*/
1237
void ConnectionsDock::_go_to_method(TreeItem &p_item) {
1238
if (_get_item_type(p_item) != TREE_ITEM_TYPE_CONNECTION) {
1239
return;
1240
}
1241
1242
Connection connection = p_item.get_metadata(0);
1243
ConnectDialog::ConnectionData cd = connection;
1244
ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck.
1245
1246
if (!cd.target) {
1247
return;
1248
}
1249
1250
Ref<Script> scr = cd.target->get_script();
1251
1252
if (scr.is_null()) {
1253
return;
1254
}
1255
1256
if (scr.is_valid() && ScriptEditor::get_singleton()->script_goto_method(scr, cd.method)) {
1257
EditorNode::get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
1258
}
1259
}
1260
1261
void ConnectionsDock::_handle_class_menu_option(int p_option) {
1262
switch (p_option) {
1263
case CLASS_MENU_OPEN_DOCS:
1264
ScriptEditor::get_singleton()->goto_help("class:" + class_menu_doc_class_name);
1265
EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
1266
break;
1267
}
1268
}
1269
1270
void ConnectionsDock::_class_menu_about_to_popup() {
1271
class_menu->set_item_disabled(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), class_menu_doc_class_name.is_empty());
1272
}
1273
1274
void ConnectionsDock::_handle_signal_menu_option(int p_option) {
1275
TreeItem *item = tree->get_selected();
1276
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
1277
return;
1278
}
1279
1280
Dictionary meta = item->get_metadata(0);
1281
1282
switch (p_option) {
1283
case SIGNAL_MENU_CONNECT: {
1284
_open_connection_dialog(*item);
1285
} break;
1286
case SIGNAL_MENU_DISCONNECT_ALL: {
1287
disconnect_all_dialog->set_text(vformat(TTR("Are you sure you want to remove all connections from the \"%s\" signal?"), meta["name"]));
1288
disconnect_all_dialog->popup_centered();
1289
} break;
1290
case SIGNAL_MENU_COPY_NAME: {
1291
DisplayServer::get_singleton()->clipboard_set(meta["name"]);
1292
} break;
1293
case SIGNAL_MENU_OPEN_DOCS: {
1294
ScriptEditor::get_singleton()->goto_help("class_signal:" + String(meta["class"]) + ":" + String(meta["name"]));
1295
EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
1296
} break;
1297
}
1298
}
1299
1300
void ConnectionsDock::_signal_menu_about_to_popup() {
1301
TreeItem *item = tree->get_selected();
1302
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_SIGNAL) {
1303
return;
1304
}
1305
1306
Dictionary meta = item->get_metadata(0);
1307
1308
bool disable_disconnect_all = true;
1309
for (int i = 0; i < item->get_child_count(); i++) {
1310
if (!item->get_child(i)->has_meta("_inherited_connection")) {
1311
disable_disconnect_all = false;
1312
}
1313
}
1314
1315
signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), disable_disconnect_all);
1316
signal_menu->set_item_disabled(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), String(meta["class"]).is_empty());
1317
}
1318
1319
void ConnectionsDock::_handle_slot_menu_option(int p_option) {
1320
TreeItem *item = tree->get_selected();
1321
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
1322
return;
1323
}
1324
1325
switch (p_option) {
1326
case SLOT_MENU_EDIT: {
1327
_open_edit_connection_dialog(*item);
1328
} break;
1329
case SLOT_MENU_GO_TO_METHOD: {
1330
_go_to_method(*item);
1331
} break;
1332
case SLOT_MENU_DISCONNECT: {
1333
Connection connection = item->get_metadata(0);
1334
_disconnect(connection);
1335
update_tree();
1336
} break;
1337
}
1338
}
1339
1340
void ConnectionsDock::_slot_menu_about_to_popup() {
1341
TreeItem *item = tree->get_selected();
1342
if (!item || _get_item_type(*item) != TREE_ITEM_TYPE_CONNECTION) {
1343
return;
1344
}
1345
1346
bool connection_is_inherited = item->has_meta("_inherited_connection");
1347
1348
slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_EDIT), connection_is_inherited);
1349
slot_menu->set_item_disabled(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), connection_is_inherited);
1350
}
1351
1352
void ConnectionsDock::_tree_gui_input(const Ref<InputEvent> &p_event) {
1353
TreeItem *item = nullptr;
1354
Point2 item_pos;
1355
1356
const Ref<InputEventKey> &key = p_event;
1357
1358
if (key.is_valid() && key->is_pressed() && !key->is_echo()) {
1359
if (ED_IS_SHORTCUT("connections_editor/disconnect", p_event)) {
1360
item = tree->get_selected();
1361
if (item && _get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
1362
Connection connection = item->get_metadata(0);
1363
_disconnect(connection);
1364
update_tree();
1365
1366
// Stop the Delete input from propagating elsewhere.
1367
accept_event();
1368
return;
1369
}
1370
} else if (ED_IS_SHORTCUT("editor/open_search", p_event)) {
1371
search_box->grab_focus();
1372
search_box->select_all();
1373
1374
accept_event();
1375
return;
1376
}
1377
}
1378
if (key.is_valid() && key->is_pressed() && key->is_action("ui_menu", true)) {
1379
item = tree->get_selected();
1380
if (!item) {
1381
return;
1382
}
1383
item_pos = tree->get_item_rect(item).position;
1384
}
1385
1386
// Handle RMB press.
1387
const Ref<InputEventMouseButton> &mb_event = p_event;
1388
1389
if (mb_event.is_valid() && mb_event->is_pressed() && mb_event->get_button_index() == MouseButton::RIGHT) {
1390
item = tree->get_item_at_position(mb_event->get_position());
1391
if (!item) {
1392
return;
1393
}
1394
item_pos = mb_event->get_position();
1395
}
1396
1397
if (item) {
1398
if (item->is_selectable(0)) {
1399
// Update selection now, before `about_to_popup` signal. Needed for SIGNAL and CONNECTION context menus.
1400
tree->set_selected(item);
1401
}
1402
1403
Vector2 screen_position = tree->get_screen_position() + item_pos;
1404
1405
switch (_get_item_type(*item)) {
1406
case TREE_ITEM_TYPE_ROOT:
1407
break;
1408
case TREE_ITEM_TYPE_CLASS:
1409
class_menu_doc_class_name = item->get_metadata(0);
1410
class_menu->set_position(screen_position);
1411
class_menu->reset_size();
1412
class_menu->popup();
1413
accept_event(); // Don't collapse item.
1414
break;
1415
case TREE_ITEM_TYPE_SIGNAL:
1416
signal_menu->set_position(screen_position);
1417
signal_menu->reset_size();
1418
signal_menu->popup();
1419
break;
1420
case TREE_ITEM_TYPE_CONNECTION:
1421
slot_menu->set_position(screen_position);
1422
slot_menu->reset_size();
1423
slot_menu->popup();
1424
break;
1425
}
1426
}
1427
}
1428
1429
void ConnectionsDock::_close() {
1430
hide();
1431
}
1432
1433
void ConnectionsDock::_connect_pressed() {
1434
TreeItem *item = tree->get_selected();
1435
if (!item) {
1436
connect_button->set_disabled(true);
1437
return;
1438
}
1439
1440
if (_get_item_type(*item) == TREE_ITEM_TYPE_SIGNAL) {
1441
_open_connection_dialog(*item);
1442
} else if (_get_item_type(*item) == TREE_ITEM_TYPE_CONNECTION) {
1443
Connection connection = item->get_metadata(0);
1444
_disconnect(connection);
1445
update_tree();
1446
}
1447
}
1448
1449
void ConnectionsDock::_notification(int p_what) {
1450
switch (p_what) {
1451
case NOTIFICATION_THEME_CHANGED: {
1452
search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
1453
1454
class_menu->set_item_icon(class_menu->get_item_index(CLASS_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
1455
1456
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_CONNECT), get_editor_theme_icon(SNAME("Instance")));
1457
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_DISCONNECT_ALL), get_editor_theme_icon(SNAME("Unlinked")));
1458
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_COPY_NAME), get_editor_theme_icon(SNAME("ActionCopy")));
1459
signal_menu->set_item_icon(signal_menu->get_item_index(SIGNAL_MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
1460
1461
slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_EDIT), get_editor_theme_icon(SNAME("Edit")));
1462
slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_GO_TO_METHOD), get_editor_theme_icon(SNAME("ArrowRight")));
1463
slot_menu->set_item_icon(slot_menu->get_item_index(SLOT_MENU_DISCONNECT), get_editor_theme_icon(SNAME("Unlinked")));
1464
1465
tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
1466
1467
update_tree();
1468
} break;
1469
1470
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
1471
if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editors")) {
1472
update_tree();
1473
}
1474
} break;
1475
}
1476
}
1477
1478
void ConnectionsDock::_bind_methods() {
1479
ClassDB::bind_method("update_tree", &ConnectionsDock::update_tree);
1480
}
1481
1482
void ConnectionsDock::set_node(Node *p_node) {
1483
selected_node = p_node;
1484
update_tree();
1485
}
1486
1487
void ConnectionsDock::update_tree() {
1488
String prev_selected;
1489
if (tree->is_anything_selected()) {
1490
prev_selected = tree->get_selected()->get_text(0);
1491
}
1492
tree->clear();
1493
1494
if (!selected_node) {
1495
return;
1496
}
1497
1498
TreeItem *root = tree->create_item();
1499
DocTools *doc_data = EditorHelp::get_doc_data();
1500
EditorData &editor_data = EditorNode::get_editor_data();
1501
StringName native_base = selected_node->get_class();
1502
Ref<Script> script_base = selected_node->get_script();
1503
1504
while (native_base != StringName()) {
1505
String class_name;
1506
String doc_class_name;
1507
Ref<Texture2D> class_icon;
1508
List<MethodInfo> class_signals;
1509
1510
if (script_base.is_valid()) {
1511
class_name = script_base->get_global_name();
1512
if (class_name.is_empty()) {
1513
class_name = script_base->get_path().get_file();
1514
}
1515
1516
doc_class_name = script_base->get_global_name();
1517
if (doc_class_name.is_empty()) {
1518
doc_class_name = script_base->get_path().trim_prefix("res://").quote();
1519
}
1520
if (!doc_class_name.is_empty() && !doc_data->class_list.find(doc_class_name)) {
1521
doc_class_name = String();
1522
}
1523
1524
class_icon = editor_data.get_script_icon(script_base->get_path());
1525
if (class_icon.is_null() && has_theme_icon(native_base, EditorStringName(EditorIcons))) {
1526
class_icon = get_editor_theme_icon(native_base);
1527
}
1528
1529
script_base->get_script_signal_list(&class_signals);
1530
1531
// TODO: Core: Add optional parameter to ignore base classes (no_inheritance like in ClassDB).
1532
Ref<Script> base = script_base->get_base_script();
1533
if (base.is_valid()) {
1534
List<MethodInfo> base_signals;
1535
base->get_script_signal_list(&base_signals);
1536
HashSet<String> base_signal_names;
1537
for (const MethodInfo &signal : base_signals) {
1538
base_signal_names.insert(signal.name);
1539
}
1540
for (List<MethodInfo>::Element *F = class_signals.front(); F;) {
1541
List<MethodInfo>::Element *N = F->next();
1542
if (base_signal_names.has(F->get().name)) {
1543
class_signals.erase(F);
1544
}
1545
F = N;
1546
}
1547
}
1548
1549
script_base = base;
1550
} else {
1551
class_name = native_base;
1552
doc_class_name = native_base;
1553
1554
if (!doc_data->class_list.find(doc_class_name)) {
1555
doc_class_name = String();
1556
}
1557
1558
if (has_theme_icon(native_base, EditorStringName(EditorIcons))) {
1559
class_icon = get_editor_theme_icon(native_base);
1560
}
1561
1562
ClassDB::get_signal_list(native_base, &class_signals, true);
1563
1564
native_base = ClassDB::get_parent_class(native_base);
1565
}
1566
1567
if (class_icon.is_null()) {
1568
class_icon = get_editor_theme_icon(SNAME("Object"));
1569
}
1570
1571
TreeItem *section_item = nullptr;
1572
1573
// Create subsections.
1574
if (!class_signals.is_empty()) {
1575
class_signals.sort();
1576
1577
section_item = tree->create_item(root);
1578
section_item->set_text(0, class_name);
1579
// `|` separators used in `EditorHelpBit`.
1580
section_item->set_tooltip_text(0, "class|" + doc_class_name + "|");
1581
section_item->set_icon(0, class_icon);
1582
section_item->set_selectable(0, false);
1583
section_item->set_editable(0, false);
1584
section_item->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
1585
section_item->set_metadata(0, doc_class_name);
1586
}
1587
1588
for (MethodInfo &mi : class_signals) {
1589
const StringName &signal_name = mi.name;
1590
if (!search_box->get_text().is_subsequence_ofn(signal_name)) {
1591
continue;
1592
}
1593
PackedStringArray argnames;
1594
1595
// Create the children of the subsection - the actual list of signals.
1596
TreeItem *signal_item = tree->create_item(section_item);
1597
String signame = connect_dialog->get_signature(mi, &argnames);
1598
signal_item->set_text(0, signame);
1599
1600
if (signame == prev_selected) {
1601
signal_item->select(0);
1602
prev_selected = "";
1603
}
1604
1605
Dictionary sinfo;
1606
sinfo["class"] = doc_class_name;
1607
sinfo["name"] = signal_name;
1608
sinfo["args"] = argnames;
1609
signal_item->set_metadata(0, sinfo);
1610
signal_item->set_icon(0, get_editor_theme_icon(SNAME("Signal")));
1611
// `|` separators used in `EditorHelpBit`.
1612
signal_item->set_tooltip_text(0, "signal|" + doc_class_name + "|" + String(signal_name));
1613
1614
// List existing connections.
1615
List<Object::Connection> existing_connections;
1616
selected_node->get_signal_connection_list(signal_name, &existing_connections);
1617
1618
for (const Object::Connection &F : existing_connections) {
1619
Connection connection = F;
1620
if (!(connection.flags & CONNECT_PERSIST)) {
1621
continue;
1622
}
1623
ConnectDialog::ConnectionData cd = connection;
1624
1625
Node *target = Object::cast_to<Node>(cd.target);
1626
if (!target) {
1627
continue;
1628
}
1629
1630
String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()";
1631
if (cd.flags & CONNECT_DEFERRED) {
1632
path += " (deferred)";
1633
}
1634
if (cd.flags & CONNECT_ONE_SHOT) {
1635
path += " (one-shot)";
1636
}
1637
if (cd.flags & CONNECT_APPEND_SOURCE_OBJECT) {
1638
path += " (source)";
1639
}
1640
if (cd.unbinds > 0) {
1641
path += " unbinds(" + itos(cd.unbinds) + ")";
1642
} else if (!cd.binds.is_empty()) {
1643
path += " binds(";
1644
for (int i = 0; i < cd.binds.size(); i++) {
1645
if (i > 0) {
1646
path += ", ";
1647
}
1648
path += cd.binds[i].operator String();
1649
}
1650
path += ")";
1651
}
1652
1653
TreeItem *connection_item = tree->create_item(signal_item);
1654
connection_item->set_text(0, path);
1655
connection_item->set_metadata(0, connection);
1656
connection_item->set_icon(0, get_editor_theme_icon(SNAME("Slot")));
1657
1658
if (_is_connection_inherited(connection)) {
1659
// The scene inherits this connection.
1660
connection_item->set_custom_color(0, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
1661
connection_item->set_meta("_inherited_connection", true);
1662
}
1663
}
1664
}
1665
}
1666
1667
connect_button->set_text(TTR("Connect..."));
1668
connect_button->set_button_icon(get_editor_theme_icon(SNAME("Instance")));
1669
connect_button->set_disabled(true);
1670
}
1671
1672
ConnectionsDock::ConnectionsDock() {
1673
set_name(TTR("Signals"));
1674
1675
VBoxContainer *vbc = this;
1676
1677
search_box = memnew(LineEdit);
1678
search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1679
search_box->set_placeholder(TTR("Filter Signals"));
1680
search_box->set_accessibility_name(TTRC("Filter Signals"));
1681
search_box->set_clear_button_enabled(true);
1682
search_box->connect(SceneStringName(text_changed), callable_mp(this, &ConnectionsDock::_filter_changed));
1683
vbc->add_child(search_box);
1684
1685
tree = memnew(ConnectionsDockTree);
1686
tree->set_accessibility_name(TTRC("Connections"));
1687
tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1688
tree->set_columns(1);
1689
tree->set_select_mode(Tree::SELECT_ROW);
1690
tree->set_hide_root(true);
1691
tree->set_column_clip_content(0, true);
1692
vbc->add_child(tree);
1693
tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1694
tree->set_allow_rmb_select(true);
1695
1696
connect_button = memnew(Button);
1697
connect_button->set_accessibility_name(TTRC("Connect"));
1698
HBoxContainer *hb = memnew(HBoxContainer);
1699
vbc->add_child(hb);
1700
hb->add_spacer();
1701
hb->add_child(connect_button);
1702
connect_button->connect(SceneStringName(pressed), callable_mp(this, &ConnectionsDock::_connect_pressed));
1703
1704
connect_dialog = memnew(ConnectDialog);
1705
connect_dialog->set_process_shortcut_input(true);
1706
add_child(connect_dialog);
1707
1708
disconnect_all_dialog = memnew(ConfirmationDialog);
1709
add_child(disconnect_all_dialog);
1710
disconnect_all_dialog->connect(SceneStringName(confirmed), callable_mp(this, &ConnectionsDock::_disconnect_all));
1711
disconnect_all_dialog->set_text(TTR("Are you sure you want to remove all connections from this signal?"));
1712
1713
class_menu = memnew(PopupMenu);
1714
class_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_class_menu_option));
1715
class_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_class_menu_about_to_popup));
1716
class_menu->add_item(TTR("Open Documentation"), CLASS_MENU_OPEN_DOCS);
1717
add_child(class_menu);
1718
1719
signal_menu = memnew(PopupMenu);
1720
signal_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_signal_menu_option));
1721
signal_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_signal_menu_about_to_popup));
1722
signal_menu->add_item(TTR("Connect..."), SIGNAL_MENU_CONNECT);
1723
signal_menu->add_item(TTR("Disconnect All"), SIGNAL_MENU_DISCONNECT_ALL);
1724
signal_menu->add_item(TTR("Copy Name"), SIGNAL_MENU_COPY_NAME);
1725
signal_menu->add_separator();
1726
signal_menu->add_item(TTR("Open Documentation"), SIGNAL_MENU_OPEN_DOCS);
1727
add_child(signal_menu);
1728
1729
slot_menu = memnew(PopupMenu);
1730
slot_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ConnectionsDock::_handle_slot_menu_option));
1731
slot_menu->connect("about_to_popup", callable_mp(this, &ConnectionsDock::_slot_menu_about_to_popup));
1732
slot_menu->add_item(TTR("Edit..."), SLOT_MENU_EDIT);
1733
slot_menu->add_item(TTR("Go to Method"), SLOT_MENU_GO_TO_METHOD);
1734
slot_menu->add_shortcut(ED_SHORTCUT("connections_editor/disconnect", TTRC("Disconnect"), Key::KEY_DELETE), SLOT_MENU_DISCONNECT);
1735
add_child(slot_menu);
1736
1737
connect_dialog->connect("connected", callable_mp(this, &ConnectionsDock::_make_or_edit_connection));
1738
tree->connect(SceneStringName(item_selected), callable_mp(this, &ConnectionsDock::_tree_item_selected));
1739
tree->connect("item_activated", callable_mp(this, &ConnectionsDock::_tree_item_activated));
1740
tree->connect(SceneStringName(gui_input), callable_mp(this, &ConnectionsDock::_tree_gui_input));
1741
1742
add_theme_constant_override("separation", 3 * EDSCALE);
1743
}
1744
1745