Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/scene/connections_dialog.h
9902 views
1
/**************************************************************************/
2
/* connections_dialog.h */
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
#pragma once
32
33
#include "scene/gui/dialogs.h"
34
#include "scene/gui/tree.h"
35
36
class Button;
37
class CheckBox;
38
class CheckButton;
39
class ConnectDialogBinds;
40
class EditorInspector;
41
class EditorVariantTypeOptionButton;
42
class Label;
43
class LineEdit;
44
class OptionButton;
45
class PopupMenu;
46
class SceneTreeEditor;
47
class SpinBox;
48
49
class ConnectDialog : public ConfirmationDialog {
50
GDCLASS(ConnectDialog, ConfirmationDialog);
51
52
public:
53
struct ConnectionData {
54
Node *source = nullptr;
55
Node *target = nullptr;
56
StringName signal;
57
StringName method;
58
uint32_t flags = 0;
59
int unbinds = 0;
60
Vector<Variant> binds;
61
62
ConnectionData() {}
63
64
ConnectionData(const Connection &p_connection) {
65
source = Object::cast_to<Node>(p_connection.signal.get_object());
66
signal = p_connection.signal.get_name();
67
target = Object::cast_to<Node>(p_connection.callable.get_object());
68
flags = p_connection.flags;
69
70
Callable base_callable;
71
if (p_connection.callable.is_custom()) {
72
CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(p_connection.callable.get_custom());
73
if (ccb) {
74
binds = ccb->get_binds();
75
76
// The source object may already be bound, ignore it to prevent display of the source object.
77
if ((flags & CONNECT_APPEND_SOURCE_OBJECT) && (source == binds[0])) {
78
binds.remove_at(0);
79
}
80
81
base_callable = ccb->get_callable();
82
}
83
84
CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(p_connection.callable.get_custom());
85
if (ccu) {
86
unbinds = ccu->get_unbinds();
87
base_callable = ccu->get_callable();
88
}
89
} else {
90
base_callable = p_connection.callable;
91
}
92
method = base_callable.get_method();
93
}
94
95
Callable get_callable() const {
96
if (unbinds > 0) {
97
return Callable(target, method).unbind(unbinds);
98
} else if (!binds.is_empty()) {
99
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * binds.size());
100
for (int i = 0; i < binds.size(); i++) {
101
argptrs[i] = &binds[i];
102
}
103
return Callable(target, method).bindp(argptrs, binds.size());
104
} else {
105
return Callable(target, method);
106
}
107
}
108
};
109
110
private:
111
Label *connect_to_label = nullptr;
112
LineEdit *from_signal = nullptr;
113
LineEdit *filter_nodes = nullptr;
114
Node *source = nullptr;
115
ConnectionData source_connection_data;
116
StringName signal;
117
PackedStringArray signal_args;
118
LineEdit *dst_method = nullptr;
119
ConnectDialogBinds *cdbinds = nullptr;
120
bool edit_mode = false;
121
bool first_popup = true;
122
NodePath dst_path;
123
VBoxContainer *vbc_right = nullptr;
124
SceneTreeEditor *tree = nullptr;
125
AcceptDialog *error = nullptr;
126
127
Button *open_method_tree = nullptr;
128
AcceptDialog *method_popup = nullptr;
129
Tree *method_tree = nullptr;
130
Label *empty_tree_label = nullptr;
131
LineEdit *method_search = nullptr;
132
CheckButton *script_methods_only = nullptr;
133
CheckButton *compatible_methods_only = nullptr;
134
135
SpinBox *unbind_count = nullptr;
136
EditorInspector *bind_editor = nullptr;
137
EditorVariantTypeOptionButton *type_list = nullptr;
138
CheckBox *deferred = nullptr;
139
CheckBox *one_shot = nullptr;
140
CheckBox *append_source = nullptr;
141
CheckButton *advanced = nullptr;
142
Vector<Control *> bind_controls;
143
144
Label *warning_label = nullptr;
145
Label *error_label = nullptr;
146
147
void ok_pressed() override;
148
void _cancel_pressed();
149
void _item_activated();
150
void _tree_node_selected();
151
void _focus_currently_connected();
152
153
void _method_selected();
154
void _create_method_tree_items(const List<MethodInfo> &p_methods, TreeItem *p_parent_item);
155
List<MethodInfo> _filter_method_list(const List<MethodInfo> &p_methods, const MethodInfo &p_signal, const String &p_search_string) const;
156
void _update_method_tree();
157
void _method_check_button_pressed(const CheckButton *p_button);
158
void _open_method_popup();
159
160
void _unbind_count_changed(double p_count);
161
void _add_bind();
162
void _remove_bind();
163
void _advanced_pressed();
164
void _update_ok_enabled();
165
void _update_warning_label();
166
167
protected:
168
virtual void _post_popup() override;
169
void _notification(int p_what);
170
static void _bind_methods();
171
172
public:
173
static StringName generate_method_callback_name(Node *p_source, const String &p_signal_name, Node *p_target);
174
Node *get_source() const;
175
ConnectionData get_source_connection_data() const;
176
StringName get_signal_name() const;
177
PackedStringArray get_signal_args() const;
178
NodePath get_dst_path() const;
179
void set_dst_node(Node *p_node);
180
StringName get_dst_method_name() const;
181
void set_dst_method(const StringName &p_method);
182
int get_unbinds() const;
183
Vector<Variant> get_binds() const;
184
String get_signature(const MethodInfo &p_method, PackedStringArray *r_arg_names = nullptr);
185
186
bool get_deferred() const;
187
bool get_one_shot() const;
188
bool get_append_source() const;
189
bool is_editing() const;
190
191
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
192
193
void init(const ConnectionData &p_cd, const PackedStringArray &p_signal_args, bool p_edit = false);
194
195
void popup_dialog(const String &p_for_signal);
196
ConnectDialog();
197
~ConnectDialog();
198
};
199
200
//////////////////////////////////////////
201
202
// Custom `Tree` needed to use `EditorHelpBit` to display signal documentation.
203
class ConnectionsDockTree : public Tree {
204
virtual Control *make_custom_tooltip(const String &p_text) const;
205
};
206
207
class ConnectionsDock : public VBoxContainer {
208
GDCLASS(ConnectionsDock, VBoxContainer);
209
210
enum TreeItemType {
211
TREE_ITEM_TYPE_ROOT,
212
TREE_ITEM_TYPE_CLASS,
213
TREE_ITEM_TYPE_SIGNAL,
214
TREE_ITEM_TYPE_CONNECTION,
215
};
216
217
// Right-click context menu options.
218
enum ClassMenuOption {
219
CLASS_MENU_OPEN_DOCS,
220
};
221
enum SignalMenuOption {
222
SIGNAL_MENU_CONNECT,
223
SIGNAL_MENU_DISCONNECT_ALL,
224
SIGNAL_MENU_COPY_NAME,
225
SIGNAL_MENU_OPEN_DOCS,
226
};
227
enum SlotMenuOption {
228
SLOT_MENU_EDIT,
229
SLOT_MENU_GO_TO_METHOD,
230
SLOT_MENU_DISCONNECT,
231
};
232
233
Node *selected_node = nullptr;
234
ConnectionsDockTree *tree = nullptr;
235
236
ConfirmationDialog *disconnect_all_dialog = nullptr;
237
ConnectDialog *connect_dialog = nullptr;
238
Button *connect_button = nullptr;
239
PopupMenu *class_menu = nullptr;
240
String class_menu_doc_class_name;
241
PopupMenu *signal_menu = nullptr;
242
PopupMenu *slot_menu = nullptr;
243
LineEdit *search_box = nullptr;
244
245
void _filter_changed(const String &p_text);
246
247
void _make_or_edit_connection();
248
void _connect(const ConnectDialog::ConnectionData &p_cd);
249
void _disconnect(const ConnectDialog::ConnectionData &p_cd);
250
void _disconnect_all();
251
252
void _tree_item_selected();
253
void _tree_item_activated();
254
TreeItemType _get_item_type(const TreeItem &p_item) const;
255
bool _is_connection_inherited(Connection &p_connection);
256
257
void _open_connection_dialog(TreeItem &p_item);
258
void _open_edit_connection_dialog(TreeItem &p_item);
259
void _go_to_method(TreeItem &p_item);
260
261
void _handle_class_menu_option(int p_option);
262
void _class_menu_about_to_popup();
263
void _handle_signal_menu_option(int p_option);
264
void _signal_menu_about_to_popup();
265
void _handle_slot_menu_option(int p_option);
266
void _slot_menu_about_to_popup();
267
void _tree_gui_input(const Ref<InputEvent> &p_event);
268
void _close();
269
270
protected:
271
void _connect_pressed();
272
void _notification(int p_what);
273
static void _bind_methods();
274
275
public:
276
void set_node(Node *p_node);
277
void update_tree();
278
279
ConnectionsDock();
280
};
281
282