diff options
Diffstat (limited to 'editor/connections_dialog.cpp')
-rw-r--r-- | editor/connections_dialog.cpp | 288 |
1 files changed, 171 insertions, 117 deletions
diff --git a/editor/connections_dialog.cpp b/editor/connections_dialog.cpp index 4945414237..0edbb182e1 100644 --- a/editor/connections_dialog.cpp +++ b/editor/connections_dialog.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* 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 */ @@ -38,6 +38,7 @@ #include "plugins/script_editor_plugin.h" #include "scene/gui/label.h" #include "scene/gui/popup_menu.h" +#include "scene/gui/spin_box.h" static Node *_find_first_script(Node *p_root, Node *p_node) { if (p_node != p_root && p_node->get_owner() != p_root) { @@ -164,6 +165,20 @@ void ConnectDialog::_tree_node_selected() { _update_ok_enabled(); } +void ConnectDialog::_unbind_count_changed(double p_count) { + for (Control *control : bind_controls) { + BaseButton *b = Object::cast_to<BaseButton>(control); + if (b) { + b->set_disabled(p_count > 0); + } + + EditorInspector *e = Object::cast_to<EditorInspector>(control); + if (e) { + e->set_read_only(p_count > 0); + } + } +} + /* * Adds a new parameter bind to connection. */ @@ -305,6 +320,10 @@ 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<Variant> ConnectDialog::get_binds() const { return cdbinds->params; } @@ -321,7 +340,7 @@ bool ConnectDialog::get_oneshot() const { * Returns true if ConnectDialog is being used to edit an existing connection. */ bool ConnectDialog::is_editing() const { - return bEditMode; + return edit_mode; } /* @@ -329,33 +348,35 @@ bool ConnectDialog::is_editing() const { * If creating a connection from scratch, sensible defaults are used. * If editing an existing connection, previous data is retained. */ -void ConnectDialog::init(ConnectionData c, bool bEdit) { +void ConnectDialog::init(ConnectionData p_cd, bool p_edit) { set_hide_on_ok(false); - source = static_cast<Node *>(c.source); - signal = c.signal; + source = static_cast<Node *>(p_cd.source); + signal = p_cd.signal; tree->set_selected(nullptr); tree->set_marked(source, true); - if (c.target) { - set_dst_node(static_cast<Node *>(c.target)); - set_dst_method(c.method); + if (p_cd.target) { + set_dst_node(static_cast<Node *>(p_cd.target)); + set_dst_method(p_cd.method); } _update_ok_enabled(); - bool bDeferred = (c.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED; - bool bOneshot = (c.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT; + bool b_deferred = (p_cd.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED; + bool b_oneshot = (p_cd.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT; - deferred->set_pressed(bDeferred); - oneshot->set_pressed(bOneshot); + deferred->set_pressed(b_deferred); + oneshot->set_pressed(b_oneshot); + unbind_count->set_value(p_cd.unbinds); + _unbind_count_changed(p_cd.unbinds); cdbinds->params.clear(); - cdbinds->params = c.binds; + cdbinds->params = p_cd.binds; cdbinds->notify_changed(); - bEditMode = bEdit; + edit_mode = p_edit; } void ConnectDialog::popup_dialog(const String &p_for_signal) { @@ -449,23 +470,33 @@ ConnectDialog::ConnectDialog() { type_list->add_item("Transform3D", Variant::TRANSFORM3D); type_list->add_item("Color", Variant::COLOR); type_list->select(0); + bind_controls.push_back(type_list); Button *add_bind = memnew(Button); add_bind->set_text(TTR("Add")); add_bind_hb->add_child(add_bind); add_bind->connect("pressed", callable_mp(this, &ConnectDialog::_add_bind)); + bind_controls.push_back(add_bind); Button *del_bind = memnew(Button); del_bind->set_text(TTR("Remove")); add_bind_hb->add_child(del_bind); del_bind->connect("pressed", callable_mp(this, &ConnectDialog::_remove_bind)); + bind_controls.push_back(del_bind); vbc_right->add_margin_child(TTR("Add Extra Call Argument:"), add_bind_hb); bind_editor = memnew(EditorInspector); + bind_controls.push_back(bind_editor); vbc_right->add_margin_child(TTR("Extra Call Arguments:"), bind_editor, true); + unbind_count = memnew(SpinBox); + unbind_count->set_tooltip(TTR("Allows to drop arguments sent by signal emitter.")); + unbind_count->connect("value_changed", callable_mp(this, &ConnectDialog::_unbind_count_changed)); + + vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count); + HBoxContainer *dstm_hb = memnew(HBoxContainer); vbc_left->add_margin_child(TTR("Receiver Method:"), dstm_hb); @@ -515,7 +546,7 @@ Control *ConnectionsDockTree::make_custom_tooltip(const String &p_text) const { String text = TTR("Signal:") + " [u][b]" + p_text.get_slice("::", 0) + "[/b][/u]"; text += p_text.get_slice("::", 1).strip_edges() + "\n"; text += p_text.get_slice("::", 2).strip_edges(); - help_bit->call_deferred(SNAME("set_text"), text); //hack so it uses proper theme once inside scene + help_bit->call_deferred(SNAME("set_text"), text); // Hack so it uses proper theme once inside scene. return help_bit; } @@ -538,29 +569,32 @@ void ConnectionsDock::_make_or_edit_connection() { ERR_FAIL_COND(!it); NodePath dst_path = connect_dialog->get_dst_path(); - Node *target = selectedNode->get_node(dst_path); + Node *target = selected_node->get_node(dst_path); ERR_FAIL_COND(!target); - ConnectDialog::ConnectionData cToMake; - cToMake.source = connect_dialog->get_source(); - cToMake.target = target; - cToMake.signal = connect_dialog->get_signal_name(); - cToMake.method = connect_dialog->get_dst_method_name(); - cToMake.binds = connect_dialog->get_binds(); - bool defer = connect_dialog->get_deferred(); - bool oshot = connect_dialog->get_oneshot(); - cToMake.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0); + ConnectDialog::ConnectionData cd; + cd.source = connect_dialog->get_source(); + cd.target = target; + cd.signal = connect_dialog->get_signal_name(); + cd.method = connect_dialog->get_dst_method_name(); + cd.unbinds = connect_dialog->get_unbinds(); + if (cd.unbinds == 0) { + cd.binds = connect_dialog->get_binds(); + } + bool b_deferred = connect_dialog->get_deferred(); + bool b_oneshot = connect_dialog->get_oneshot(); + cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONESHOT : 0); // Conditions to add function: must have a script and must not have the method already // (in the class, the script itself, or inherited). bool add_script_function = false; Ref<Script> script = target->get_script(); - if (!target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cToMake.method)) { + if (!target->get_script().is_null() && !ClassDB::has_method(target->get_class(), cd.method)) { // There is a chance that the method is inherited from another script. bool found_inherited_function = false; Ref<Script> inherited_script = script->get_base_script(); while (!inherited_script.is_null()) { - int line = inherited_script->get_language()->find_function(cToMake.method, inherited_script->get_source_code()); + int line = inherited_script->get_language()->find_function(cd.method, inherited_script->get_source_code()); if (line != -1) { found_inherited_function = true; break; @@ -575,23 +609,23 @@ void ConnectionsDock::_make_or_edit_connection() { if (add_script_function) { // Pick up args here before "it" is deleted by update_tree. script_function_args = it->get_metadata(0).operator Dictionary()["args"]; - for (int i = 0; i < cToMake.binds.size(); i++) { - script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cToMake.binds[i].get_type())); + for (int i = 0; i < cd.binds.size(); i++) { + script_function_args.push_back("extra_arg_" + itos(i) + ":" + Variant::get_type_name(cd.binds[i].get_type())); } } if (connect_dialog->is_editing()) { _disconnect(*it); - _connect(cToMake); + _connect(cd); } else { - _connect(cToMake); + _connect(cd); } // IMPORTANT NOTE: _disconnect and _connect cause an update_tree, which will delete the object "it" is pointing to. it = nullptr; if (add_script_function) { - editor->emit_signal(SNAME("script_add_function_request"), target, cToMake.method, script_function_args); + editor->emit_signal(SNAME("script_add_function_request"), target, cd.method, script_function_args); hide(); } @@ -601,23 +635,21 @@ void ConnectionsDock::_make_or_edit_connection() { /* * Creates single connection w/ undo-redo functionality. */ -void ConnectionsDock::_connect(ConnectDialog::ConnectionData cToMake) { - Node *source = static_cast<Node *>(cToMake.source); - Node *target = static_cast<Node *>(cToMake.target); +void ConnectionsDock::_connect(ConnectDialog::ConnectionData p_cd) { + Node *source = Object::cast_to<Node>(p_cd.source); + Node *target = Object::cast_to<Node>(p_cd.target); if (!source || !target) { return; } - undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(cToMake.signal), String(cToMake.method))); - - Callable c(target, cToMake.method); - - undo_redo->add_do_method(source, "connect", cToMake.signal, c, cToMake.binds, cToMake.flags); - undo_redo->add_undo_method(source, "disconnect", cToMake.signal, c); + Callable callable = p_cd.get_callable(); + undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(p_cd.signal), String(p_cd.method))); + undo_redo->add_do_method(source, "connect", p_cd.signal, callable, varray(), p_cd.flags); + undo_redo->add_undo_method(source, "disconnect", p_cd.signal, callable); undo_redo->add_do_method(this, "update_tree"); undo_redo->add_undo_method(this, "update_tree"); - undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree + undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); // To force redraw of scene tree. undo_redo->add_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); undo_redo->commit_action(); @@ -626,16 +658,17 @@ void ConnectionsDock::_connect(ConnectDialog::ConnectionData cToMake) { /* * Break single connection w/ undo-redo functionality. */ -void ConnectionsDock::_disconnect(TreeItem &item) { - Connection cd = item.get_metadata(0); - ConnectDialog::ConnectionData c = cd; +void ConnectionsDock::_disconnect(TreeItem &p_item) { + Connection connection = p_item.get_metadata(0); + ConnectDialog::ConnectionData cd = connection; - ERR_FAIL_COND(c.source != selectedNode); // Shouldn't happen but... Bugcheck. + ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... Bugcheck. - undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method)); + undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), cd.signal, cd.method)); - undo_redo->add_do_method(selectedNode, "disconnect", c.signal, Callable(c.target, c.method)); - undo_redo->add_undo_method(selectedNode, "connect", c.signal, Callable(c.target, c.method), c.binds, c.flags); + Callable callable = cd.get_callable(); + undo_redo->add_do_method(selected_node, "disconnect", cd.signal, callable); + undo_redo->add_undo_method(selected_node, "connect", cd.signal, callable, cd.binds, cd.flags); undo_redo->add_do_method(this, "update_tree"); undo_redo->add_undo_method(this, "update_tree"); undo_redo->add_do_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); // To force redraw of scene tree. @@ -656,14 +689,14 @@ void ConnectionsDock::_disconnect_all() { } TreeItem *child = item->get_first_child(); - String signalName = item->get_metadata(0).operator Dictionary()["name"]; - undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signalName)); + String signal_name = item->get_metadata(0).operator Dictionary()["name"]; + undo_redo->create_action(vformat(TTR("Disconnect all from signal: '%s'"), signal_name)); while (child) { - Connection cd = child->get_metadata(0); - ConnectDialog::ConnectionData c = cd; - undo_redo->add_do_method(selectedNode, "disconnect", c.signal, Callable(c.target, c.method)); - undo_redo->add_undo_method(selectedNode, "connect", c.signal, Callable(c.target, c.method), c.binds, c.flags); + Connection connection = child->get_metadata(0); + ConnectDialog::ConnectionData cd = connection; + undo_redo->add_do_method(selected_node, "disconnect", cd.signal, cd.get_callable()); + undo_redo->add_undo_method(selected_node, "connect", cd.signal, cd.get_callable(), cd.binds, cd.flags); child = child->get_next(); } @@ -704,100 +737,118 @@ void ConnectionsDock::_tree_item_activated() { // "Activation" on double-click. } } -bool ConnectionsDock::_is_item_signal(TreeItem &item) { - return (item.get_parent() == tree->get_root() || item.get_parent()->get_parent() == tree->get_root()); +bool ConnectionsDock::_is_item_signal(TreeItem &p_item) { + return (p_item.get_parent() == tree->get_root() || p_item.get_parent()->get_parent() == tree->get_root()); } /* * Open connection dialog with TreeItem data to CREATE a brand-new connection. */ -void ConnectionsDock::_open_connection_dialog(TreeItem &item) { - String signal = item.get_metadata(0).operator Dictionary()["name"]; - const String &signalname = signal; - String midname = selectedNode->get_name(); - for (int i = 0; i < midname.length(); i++) { //TODO: Regex filter may be cleaner. - char32_t c = midname[i]; +void ConnectionsDock::_open_connection_dialog(TreeItem &p_item) { + String signal_name = p_item.get_metadata(0).operator Dictionary()["name"]; + const String &signal_name_ref = signal_name; + String node_name = selected_node->get_name(); + for (int i = 0; i < node_name.length(); i++) { // TODO: Regex filter may be cleaner. + char32_t c = node_name[i]; if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_')) { if (c == ' ') { // Replace spaces with underlines. c = '_'; } else { // Remove any other characters. - midname.remove_at(i); + node_name.remove_at(i); i--; continue; } } - midname[i] = c; + node_name[i] = c; } - Node *dst_node = selectedNode->get_owner() ? selectedNode->get_owner() : selectedNode; + Node *dst_node = selected_node->get_owner() ? selected_node->get_owner() : selected_node; if (!dst_node || dst_node->get_script().is_null()) { dst_node = _find_first_script(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root()); } - StringName dst_method = "_on_" + midname + "_" + signal; + Dictionary subst; - ConnectDialog::ConnectionData c; - c.source = selectedNode; - c.signal = StringName(signalname); - c.target = dst_node; - c.method = dst_method; - connect_dialog->popup_dialog(signalname); - connect_dialog->init(c); + String s = node_name.capitalize().replace(" ", ""); + subst["NodeName"] = s; + if (!s.is_empty()) { + s[0] = s.to_lower()[0]; + } + subst["nodeName"] = s; + subst["node_name"] = node_name.capitalize().replace(" ", "_").to_lower(); + + s = signal_name.capitalize().replace(" ", ""); + subst["SignalName"] = s; + if (!s.is_empty()) { + s[0] = s.to_lower()[0]; + } + subst["signalName"] = s; + subst["signal_name"] = signal_name.capitalize().replace(" ", "_").to_lower(); + + String dst_method = String(EDITOR_GET("interface/editors/default_signal_callback_name")).format(subst); + + ConnectDialog::ConnectionData cd; + cd.source = selected_node; + cd.signal = StringName(signal_name_ref); + cd.target = dst_node; + cd.method = StringName(dst_method); + connect_dialog->popup_dialog(signal_name_ref); + connect_dialog->init(cd); connect_dialog->set_title(TTR("Connect a Signal to a Method")); } /* * Open connection dialog with Connection data to EDIT an existing connection. */ -void ConnectionsDock::_open_connection_dialog(ConnectDialog::ConnectionData cToEdit) { - Node *src = static_cast<Node *>(cToEdit.source); - Node *dst = static_cast<Node *>(cToEdit.target); +void ConnectionsDock::_open_connection_dialog(ConnectDialog::ConnectionData p_cd) { + Node *src = Object::cast_to<Node>(p_cd.source); + Node *dst = Object::cast_to<Node>(p_cd.target); if (src && dst) { - const String &signalname = cToEdit.signal; - connect_dialog->set_title(TTR("Edit Connection:") + cToEdit.signal); - connect_dialog->popup_dialog(signalname); - connect_dialog->init(cToEdit, true); + const String &signal_name_ref = p_cd.signal; + connect_dialog->set_title(TTR("Edit Connection:") + p_cd.signal); + connect_dialog->popup_dialog(signal_name_ref); + connect_dialog->init(p_cd, true); } } /* * Open slot method location in script editor. */ -void ConnectionsDock::_go_to_script(TreeItem &item) { - if (_is_item_signal(item)) { +void ConnectionsDock::_go_to_script(TreeItem &p_item) { + if (_is_item_signal(p_item)) { return; } - Connection cd = item.get_metadata(0); - ConnectDialog::ConnectionData c = cd; - ERR_FAIL_COND(c.source != selectedNode); //shouldn't happen but...bugcheck + Connection connection = p_item.get_metadata(0); + ConnectDialog::ConnectionData cd = connection; + ERR_FAIL_COND(cd.source != selected_node); // Shouldn't happen but... bugcheck. - if (!c.target) { + if (!cd.target) { return; } - Ref<Script> script = c.target->get_script(); + Ref<Script> script = cd.target->get_script(); if (script.is_null()) { return; } - if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, c.method)) { + if (script.is_valid() && ScriptEditor::get_singleton()->script_goto_method(script, cd.method)) { editor->call("_editor_select", EditorNode::EDITOR_SCRIPT); } } -void ConnectionsDock::_handle_signal_menu_option(int option) { +void ConnectionsDock::_handle_signal_menu_option(int p_option) { TreeItem *item = tree->get_selected(); if (!item) { return; } - switch (option) { + switch (p_option) { case CONNECT: { _open_connection_dialog(*item); } break; @@ -809,17 +860,17 @@ void ConnectionsDock::_handle_signal_menu_option(int option) { } } -void ConnectionsDock::_handle_slot_menu_option(int option) { +void ConnectionsDock::_handle_slot_menu_option(int p_option) { TreeItem *item = tree->get_selected(); if (!item) { return; } - switch (option) { + switch (p_option) { case EDIT: { - Connection c = item->get_metadata(0); - _open_connection_dialog(c); + Connection connection = item->get_metadata(0); + _open_connection_dialog(connection); } break; case GO_TO_SCRIPT: { _go_to_script(*item); @@ -831,14 +882,14 @@ void ConnectionsDock::_handle_slot_menu_option(int option) { } } -void ConnectionsDock::_rmb_pressed(Vector2 position) { +void ConnectionsDock::_rmb_pressed(Vector2 p_position) { TreeItem *item = tree->get_selected(); if (!item) { return; } - Vector2 screen_position = tree->get_screen_position() + position; + Vector2 screen_position = tree->get_screen_position() + p_position; if (_is_item_signal(*item)) { signal_menu->set_position(screen_position); @@ -881,14 +932,14 @@ void ConnectionsDock::_bind_methods() { } void ConnectionsDock::set_node(Node *p_node) { - selectedNode = p_node; + selected_node = p_node; update_tree(); } void ConnectionsDock::update_tree() { tree->clear(); - if (!selectedNode) { + if (!selected_node) { return; } @@ -896,10 +947,10 @@ void ConnectionsDock::update_tree() { List<MethodInfo> node_signals; - selectedNode->get_signal_list(&node_signals); + selected_node->get_signal_list(&node_signals); bool did_script = false; - StringName base = selectedNode->get_class(); + StringName base = selected_node->get_class(); while (base) { List<MethodInfo> node_signals2; @@ -908,7 +959,7 @@ void ConnectionsDock::update_tree() { if (!did_script) { // Get script signals (including signals from any base scripts). - Ref<Script> scr = selectedNode->get_script(); + Ref<Script> scr = selected_node->get_script(); if (scr.is_valid()) { scr->get_script_signal_list(&node_signals2); if (scr->get_path().is_resource_file()) { @@ -1021,44 +1072,45 @@ void ConnectionsDock::update_tree() { signal_item->set_tooltip(0, String(signal_name) + "::" + signaldesc + "::" + descr); } - // List existing connections + // List existing connections. List<Object::Connection> connections; - selectedNode->get_signal_connection_list(signal_name, &connections); + selected_node->get_signal_connection_list(signal_name, &connections); for (const Object::Connection &F : connections) { - Connection cn = F; - if (!(cn.flags & CONNECT_PERSIST)) { + Connection connection = F; + if (!(connection.flags & CONNECT_PERSIST)) { continue; } - ConnectDialog::ConnectionData c = cn; + ConnectDialog::ConnectionData cd = connection; - Node *target = Object::cast_to<Node>(c.target); + Node *target = Object::cast_to<Node>(cd.target); if (!target) { continue; } - String path = String(selectedNode->get_path_to(target)) + " :: " + c.method + "()"; - if (c.flags & CONNECT_DEFERRED) { + String path = String(selected_node->get_path_to(target)) + " :: " + cd.method + "()"; + if (cd.flags & CONNECT_DEFERRED) { path += " (deferred)"; } - if (c.flags & CONNECT_ONESHOT) { + if (cd.flags & CONNECT_ONESHOT) { path += " (oneshot)"; } - if (c.binds.size()) { + if (cd.unbinds > 0) { + path += " unbinds(" + itos(cd.unbinds) + ")"; + } else if (!cd.binds.is_empty()) { path += " binds("; - for (int i = 0; i < c.binds.size(); i++) { + for (int i = 0; i < cd.binds.size(); i++) { if (i > 0) { path += ", "; } - path += c.binds[i].operator String(); + path += cd.binds[i].operator String(); } path += ")"; } TreeItem *connection_item = tree->create_item(signal_item); connection_item->set_text(0, path); - Connection cd = c; - connection_item->set_metadata(0, cd); + connection_item->set_metadata(0, connection); connection_item->set_icon(0, get_theme_icon(SNAME("Slot"), SNAME("EditorIcons"))); } } @@ -1130,6 +1182,8 @@ ConnectionsDock::ConnectionsDock(EditorNode *p_editor) { tree->connect("item_rmb_selected", callable_mp(this, &ConnectionsDock::_rmb_pressed)); add_theme_constant_override("separation", 3 * EDSCALE); + + EDITOR_DEF("interface/editors/default_signal_callback_name", "_on_{node_name}_{signal_name}"); } ConnectionsDock::~ConnectionsDock() { |