Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow binds and unbinds to be used at the same time in the signal connection dialog. #100554

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/variant/callable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "core/object/object.h"
#include "core/object/ref_counted.h"
#include "core/object/script_language.h"
#include "core/templates/list.h"
#include "core/templates/vector.h"
#include "core/variant/callable_bind.h"
#include "core/variant/variant_callable.h"

Expand Down
1 change: 0 additions & 1 deletion core/variant/callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#include "core/object/object_id.h"
#include "core/string/string_name.h"
#include "core/templates/list.h"

class Object;
class Variant;
Expand Down
32 changes: 5 additions & 27 deletions editor/connections_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,6 @@ void ConnectDialog::_focus_currently_connected() {
tree->set_selected(source);
}

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);
}
}
}

void ConnectDialog::_method_selected() {
TreeItem *selected_item = method_tree->get_selected();
dst_method->set_text(selected_item->get_metadata(0));
Expand Down Expand Up @@ -667,9 +653,7 @@ void ConnectDialog::init(const ConnectionData &p_cd, const PackedStringArray &p_
one_shot->set_pressed(b_oneshot);

unbind_count->set_max(p_signal_args.size());

unbind_count->set_value(p_cd.unbinds);
_unbind_count_changed(p_cd.unbinds);

cdbinds->params.clear();
cdbinds->params = p_cd.binds;
Expand Down Expand Up @@ -829,30 +813,25 @@ ConnectDialog::ConnectDialog() {

type_list->add_item(Variant::get_type_name(Variant::Type(i)), i);
}
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(SceneStringName(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(SceneStringName(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_text(TTR("Allows to drop arguments sent by signal emitter."));
unbind_count->connect(SceneStringName(value_changed), callable_mp(this, &ConnectDialog::_unbind_count_changed));

vbc_right->add_margin_child(TTR("Unbind Signal Arguments:"), unbind_count);

Expand Down Expand Up @@ -940,9 +919,7 @@ void ConnectionsDock::_make_or_edit_connection() {
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();
}
cd.binds = connect_dialog->get_binds();
bool b_deferred = connect_dialog->get_deferred();
bool b_oneshot = connect_dialog->get_one_shot();
cd.flags = CONNECT_PERSIST | (b_deferred ? CONNECT_DEFERRED : 0) | (b_oneshot ? CONNECT_ONE_SHOT : 0);
Expand Down Expand Up @@ -1557,9 +1534,7 @@ void ConnectionsDock::update_tree() {
if (cd.flags & CONNECT_ONE_SHOT) {
path += " (one-shot)";
}
if (cd.unbinds > 0) {
path += " unbinds(" + itos(cd.unbinds) + ")";
} else if (!cd.binds.is_empty()) {
if (!cd.binds.is_empty()) {
path += " binds(";
for (int i = 0; i < cd.binds.size(); i++) {
if (i > 0) {
Expand All @@ -1569,6 +1544,9 @@ void ConnectionsDock::update_tree() {
}
path += ")";
}
if (cd.unbinds > 0) {
path += " unbinds(" + itos(cd.unbinds) + ")";
}

TreeItem *connection_item = tree->create_item(signal_item);
connection_item->set_text(0, path);
Expand Down
35 changes: 22 additions & 13 deletions editor/connections_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,22 @@ class ConnectDialog : public ConfirmationDialog {

Callable base_callable;
if (p_connection.callable.is_custom()) {
CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(p_connection.callable.get_custom());
if (ccb) {
binds = ccb->get_binds();
base_callable = ccb->get_callable();
}
CallableCustom *next_callable = p_connection.callable.get_custom();

CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(p_connection.callable.get_custom());
CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(next_callable);
if (ccu) {
unbinds = ccu->get_unbinds();
base_callable = ccu->get_callable();

if (base_callable.is_custom()) {
next_callable = base_callable.get_custom();
}
}

CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(next_callable);
if (ccb) {
binds = ccb->get_binds();
base_callable = ccb->get_callable();
}
} else {
base_callable = p_connection.callable;
Expand All @@ -87,17 +93,21 @@ class ConnectDialog : public ConfirmationDialog {
}

Callable get_callable() const {
if (unbinds > 0) {
return Callable(target, method).unbind(unbinds);
} else if (!binds.is_empty()) {
Callable callable = Callable(target, method);

if (!binds.is_empty()) {
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * binds.size());
for (int i = 0; i < binds.size(); i++) {
argptrs[i] = &binds[i];
}
return Callable(target, method).bindp(argptrs, binds.size());
} else {
return Callable(target, method);
callable = callable.bindp(argptrs, binds.size());
}

if (unbinds > 0) {
callable = callable.unbind(unbinds);
}

return callable;
}
};

Expand Down Expand Up @@ -132,7 +142,6 @@ class ConnectDialog : public ConfirmationDialog {
CheckBox *deferred = nullptr;
CheckBox *one_shot = nullptr;
CheckButton *advanced = nullptr;
Vector<Control *> bind_controls;

Label *warning_label = nullptr;
Label *error_label = nullptr;
Expand Down
41 changes: 21 additions & 20 deletions scene/resources/packed_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,22 +588,17 @@ Node *SceneState::instantiate(GenEditState p_edit_state) const {
}

Callable callable(cto, snames[c.method]);
if (c.unbinds > 0) {
callable = callable.unbind(c.unbinds);
} else if (!c.binds.is_empty()) {
Vector<Variant> binds;
if (c.binds.size()) {
binds.resize(c.binds.size());
for (int j = 0; j < c.binds.size(); j++) {
binds.write[j] = props[c.binds[j]];
}
}

const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * binds.size());
for (int j = 0; j < binds.size(); j++) {
argptrs[j] = &binds[j];
if (!c.binds.is_empty()) {
const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * c.binds.size());
for (int j = 0; j < c.binds.size(); j++) {
argptrs[j] = &props[c.binds[j]];
}
callable = callable.bindp(argptrs, binds.size());
callable = callable.bindp(argptrs, c.binds.size());
}

if (c.unbinds > 0) {
callable = callable.unbind(c.unbinds);
}

cfrom->connect(snames[c.signal], callable, CONNECT_PERSIST | c.flags | (p_edit_state == GEN_EDIT_STATE_MAIN ? 0 : CONNECT_INHERITED));
Expand Down Expand Up @@ -1074,16 +1069,22 @@ Error SceneState::_parse_connections(Node *p_owner, Node *p_node, HashMap<String
Callable base_callable;

if (c.callable.is_custom()) {
CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(c.callable.get_custom());
if (ccb) {
binds = ccb->get_binds();
base_callable = ccb->get_callable();
}
CallableCustom *next_callable = c.callable.get_custom();

CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(c.callable.get_custom());
CallableCustomUnbind *ccu = dynamic_cast<CallableCustomUnbind *>(next_callable);
if (ccu) {
unbinds = ccu->get_unbinds();
base_callable = ccu->get_callable();

if (base_callable.is_custom()) {
next_callable = base_callable.get_custom();
}
}

CallableCustomBind *ccb = dynamic_cast<CallableCustomBind *>(next_callable);
if (ccb) {
binds = ccb->get_binds();
base_callable = ccb->get_callable();
}
} else {
base_callable = c.callable;
Expand Down
2 changes: 1 addition & 1 deletion scene/resources/resource_format_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2064,7 +2064,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path, const Ref<Reso
if (binds.size()) {
String vars;
VariantWriter::write_to_string(binds, vars, _write_resources, this, use_compat);
f->store_string(" binds= " + vars);
f->store_string(" binds=" + vars);
}

f->store_line("]");
Expand Down
Loading