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