/*************************************************************************/ /* connections_dialog.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "connections_dialog.h" #include "editor/doc_tools.h" #include "editor/editor_node.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" #include "editor/editor_undo_redo_manager.h" #include "editor/scene_tree_dock.h" #include "plugins/script_editor_plugin.h" #include "scene/resources/packed_scene.h" static Node *_find_first_script(Node *p_root, Node *p_node) { if (p_node != p_root && p_node->get_owner() != p_root) { return nullptr; } if (!p_node->get_script().is_null()) { return p_node; } for (int i = 0; i < p_node->get_child_count(); i++) { Node *ret = _find_first_script(p_root, p_node->get_child(i)); if (ret) { return ret; } } return nullptr; } class ConnectDialogBinds : public Object { GDCLASS(ConnectDialogBinds, Object); public: Vector params; bool _set(const StringName &p_name, const Variant &p_value) { String name = p_name; if (name.begins_with("bind/argument_")) { int which = name.get_slice("_", 1).to_int() - 1; ERR_FAIL_INDEX_V(which, params.size(), false); params.write[which] = p_value; } else { return false; } return true; } bool _get(const StringName &p_name, Variant &r_ret) const { String name = p_name; if (name.begins_with("bind/argument_")) { int which = name.get_slice("_", 1).to_int() - 1; ERR_FAIL_INDEX_V(which, params.size(), false); r_ret = params[which]; } else { return false; } return true; } void _get_property_list(List *p_list) const { for (int i = 0; i < params.size(); i++) { p_list->push_back(PropertyInfo(params[i].get_type(), "bind/argument_" + itos(i + 1))); } } void notify_changed() { notify_property_list_changed(); } ConnectDialogBinds() { } }; /* * Signal automatically called by parent dialog. */ void ConnectDialog::ok_pressed() { String method_name = dst_method->get_text(); if (method_name.is_empty()) { error->set_text(TTR("Method in target node must be specified.")); error->popup_centered(); return; } if (!method_name.strip_edges().is_valid_identifier()) { error->set_text(TTR("Method name must be a valid identifier.")); error->popup_centered(); return; } Node *target = tree->get_selected(); if (!target) { return; // Nothing selected in the tree, not an error. } if (target->get_script().is_null()) { if (!target->has_method(method_name)) { error->set_text(TTR("Target method not found. Specify a valid method or attach a script to the target node.")); error->popup_centered(); return; } } emit_signal(SNAME("connected")); hide(); } void ConnectDialog::_cancel_pressed() { hide(); } void ConnectDialog::_item_activated() { _ok_pressed(); // From AcceptDialog. } void ConnectDialog::_text_submitted(const String &p_text) { _ok_pressed(); // From AcceptDialog. } /* * Called each time a target node is selected within the target node tree. */ void ConnectDialog::_tree_node_selected() { Node *current = tree->get_selected(); if (!current) { return; } dst_path = source->get_path_to(current); _update_ok_enabled(); } void ConnectDialog::_unbind_count_changed(double p_count) { for (Control *control : bind_controls) { BaseButton *b = Object::cast_to(control); if (b) { b->set_disabled(p_count > 0); } EditorInspector *e = Object::cast_to(control); if (e) { e->set_read_only(p_count > 0); } } } /* * Adds a new parameter bind to connection. */ void ConnectDialog::_add_bind() { Variant::Type type = (Variant::Type)type_list->get_item_id(type_list->get_selected()); Variant value; Callable::CallError error; Variant::construct(type, value, nullptr, 0, error); cdbinds->params.push_back(value); cdbinds->notify_changed(); } /* * Remove parameter bind from connection. */ void ConnectDialog::_remove_bind() { String st = bind_editor->get_selected_path(); if (st.is_empty()) { return; } int idx = st.get_slice("/", 1).to_int() - 1; ERR_FAIL_INDEX(idx, cdbinds->params.size()); cdbinds->params.remove_at(idx); cdbinds->notify_changed(); } /* * Enables or disables the connect button. The connect button is enabled if a * node is selected and valid in the selected mode. */ void ConnectDialog::_update_ok_enabled() { Node *target = tree->get_selected(); if (target == nullptr) { get_ok_button()->set_disabled(true); return; } if (!advanced->is_pressed() && target->get_script().is_null()) { get_ok_button()->set_disabled(true); return; } get_ok_button()->set_disabled(false); } void ConnectDialog::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { bind_editor->edit(cdbinds); [[fallthrough]]; } case NOTIFICATION_THEME_CHANGED: { for (int i = 0; i < type_list->get_item_count(); i++) { String type_name = Variant::get_type_name((Variant::Type)type_list->get_item_id(i)); type_list->set_item_icon(i, get_theme_icon(type_name, SNAME("EditorIcons"))); } Ref style = get_theme_stylebox("normal", "LineEdit")->duplicate(); if (style.is_valid()) { style->set_default_margin(SIDE_TOP, style->get_default_margin(SIDE_TOP) + 1.0); from_signal->add_theme_style_override("normal", style); } } break; } } void ConnectDialog::_bind_methods() { ClassDB::bind_method("_cancel", &ConnectDialog::_cancel_pressed); ClassDB::bind_method("_update_ok_enabled", &ConnectDialog::_update_ok_enabled); ADD_SIGNAL(MethodInfo("connected")); } Node *ConnectDialog::get_source() const { return source; } StringName ConnectDialog::get_signal_name() const { return signal; } NodePath ConnectDialog::get_dst_path() const { return dst_path; } void ConnectDialog::set_dst_node(Node *p_node) { tree->set_selected(p_node); } StringName ConnectDialog::get_dst_method_name() const { String txt = dst_method->get_text(); if (txt.contains("(")) { txt = txt.left(txt.find("(")).strip_edges(); } return txt; } void ConnectDialog::set_dst_method(const StringName &p_method) { dst_method->set_text(p_method); } int ConnectDialog::get_unbinds() const { return int(unbind_count->get_value()); } Vector ConnectDialog::get_binds() const { return cdbinds->params; } bool ConnectDialog::get_deferred() const { return deferred->is_pressed(); } bool ConnectDialog::get_one_shot() const { return one_shot->is_pressed(); } /* * Returns true if ConnectDialog is being used to edit an existing connection. */ bool ConnectDialog::is_editing() const { return edit_mode; } /* * Initialize ConnectDialog and populate fields with expected data. * If creating a connection from scratch, sensible defaults are used. * If editing an existing connection, previous data is retained. */ void ConnectDialog::init(ConnectionData p_cd, bool p_edit) { set_hide_on_ok(false); source = static_cast(p_cd.source); signal = p_cd.signal; tree->set_selected(nullptr); tree->set_marked(source, true); if (p_cd.target) { set_dst_node(static_cast(p_cd.target)); set_dst_method(p_cd.method); } _update_ok_enabled(); bool b_deferred = (p_cd.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED; bool b_oneshot = (p_cd.flags & CONNECT_ONE_SHOT) == CONNECT_ONE_SHOT; deferred->set_pressed(b_deferred); one_shot->set_pressed(b_oneshot); MethodInfo r_signal; Ref