From cdac60759e931b07811025adf053b1e88495ad80 Mon Sep 17 00:00:00 2001 From: kobewi Date: Fri, 1 Oct 2021 02:40:07 +0200 Subject: [PATCH] Add support for unbinding in connection dialog --- core/variant/callable_bind.h | 6 ++ doc/classes/SceneState.xml | 7 ++ editor/connections_dialog.cpp | 98 ++++++++++++++++-------- editor/connections_dialog.h | 49 +++++++++--- editor/inspector_dock.h | 1 - editor/node_dock.cpp | 1 + editor/node_dock.h | 3 +- editor/scene_tree_dock.h | 1 - scene/resources/packed_scene.cpp | 63 ++++++++++++--- scene/resources/packed_scene.h | 4 +- scene/resources/resource_format_text.cpp | 11 +++ 11 files changed, 184 insertions(+), 60 deletions(-) diff --git a/core/variant/callable_bind.h b/core/variant/callable_bind.h index feb40d1de99..1cfb943678d 100644 --- a/core/variant/callable_bind.h +++ b/core/variant/callable_bind.h @@ -51,6 +51,9 @@ public: virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const; virtual const Callable *get_base_comparator() const; + Callable get_callable() { return callable; } + Vector get_binds() { return binds; } + CallableCustomBind(const Callable &p_callable, const Vector &p_binds); virtual ~CallableCustomBind(); }; @@ -72,6 +75,9 @@ public: virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const; virtual const Callable *get_base_comparator() const; + Callable get_callable() { return callable; } + int get_unbinds() { return argcount; } + CallableCustomUnbind(const Callable &p_callable, int p_argcount); virtual ~CallableCustomUnbind(); }; diff --git a/doc/classes/SceneState.xml b/doc/classes/SceneState.xml index 1c3bac92706..b29cf0d968e 100644 --- a/doc/classes/SceneState.xml +++ b/doc/classes/SceneState.xml @@ -59,6 +59,13 @@ Returns the path to the node that owns the method connected to the signal at [code]idx[/code], relative to the root node. + + + + + Returns the number of unbound parameters for the signal at [code]idx[/code]. + + diff --git a/editor/connections_dialog.cpp b/editor/connections_dialog.cpp index c773f51342e..fafd2bb195d 100644 --- a/editor/connections_dialog.cpp +++ b/editor/connections_dialog.cpp @@ -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(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. */ @@ -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 ConnectDialog::get_binds() const { return cdbinds->params; } @@ -350,6 +369,8 @@ void ConnectDialog::init(ConnectionData c, bool bEdit) { deferred->set_pressed(bDeferred); oneshot->set_pressed(bOneshot); + unbind_count->set_value(c.unbinds); + _unbind_count_changed(c.unbinds); cdbinds->params.clear(); cdbinds->params = c.binds; @@ -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); @@ -541,26 +572,29 @@ void ConnectionsDock::_make_or_edit_connection() { Node *target = selectedNode->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(); + ConnectDialog::ConnectionData connection; + connection.source = connect_dialog->get_source(); + connection.target = target; + connection.signal = connect_dialog->get_signal_name(); + connection.method = connect_dialog->get_dst_method_name(); + connection.unbinds = connect_dialog->get_unbinds(); + if (connection.unbinds == 0) { + connection.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); + connection.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? 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