Added connection editing. Some refactoring.

This commit is contained in:
Eoin O'Neill 2018-04-28 15:11:51 -07:00
parent 62a858d4c9
commit 6cef0eaa09
2 changed files with 113 additions and 47 deletions

View file

@ -110,23 +110,30 @@ void ConnectDialog::_tree_node_selected() {
else else
make_callback->show(); make_callback->show();
dst_path->set_text(node->get_path_to(current)); dst_path->set_text(source->get_path_to(current));
} }
void ConnectDialog::edit(Node *p_node) { void ConnectDialog::init(Connection c, bool bEdit) {
source = static_cast<Node*>(c.source);
node = p_node; signal = c.signal;
//dst_method_list->get_popup()->clear();
tree->set_selected(NULL); tree->set_selected(NULL);
tree->set_marked(node, true); tree->set_marked(source, true);
dst_path->set_text("");
dst_method->set_text(""); set_dst_node( static_cast<Node*>(c.target) );
deferred->set_pressed(false); set_dst_method( c.method );
oneshot->set_pressed(false);
bool bDeferred = (c.flags & CONNECT_DEFERRED) == CONNECT_DEFERRED;
bool bOneshot = (c.flags & CONNECT_ONESHOT) == CONNECT_ONESHOT;
deferred->set_pressed(bDeferred);
oneshot->set_pressed(bOneshot);
cdbinds->params.clear(); cdbinds->params.clear();
cdbinds->params = c.binds;
cdbinds->notify_changed(); cdbinds->notify_changed();
bEditMode = bEdit;
} }
void ConnectDialog::ok_pressed() { void ConnectDialog::ok_pressed() {
@ -168,6 +175,11 @@ bool ConnectDialog::get_oneshot() const {
return oneshot->is_pressed(); return oneshot->is_pressed();
} }
bool ConnectDialog::is_editing() const
{
return bEditMode;
}
StringName ConnectDialog::get_dst_method() const { StringName ConnectDialog::get_dst_method() const {
String txt = dst_method->get_text(); String txt = dst_method->get_text();
@ -176,6 +188,16 @@ StringName ConnectDialog::get_dst_method() const {
return txt; return txt;
} }
Node * ConnectDialog::get_source() const
{
return source;
}
StringName ConnectDialog::get_signal() const
{
return signal;
}
Vector<Variant> ConnectDialog::get_binds() const { Vector<Variant> ConnectDialog::get_binds() const {
return cdbinds->params; return cdbinds->params;
@ -372,43 +394,62 @@ void ConnectionsDock::_close() {
hide(); hide();
} }
void ConnectionsDock::_connect() { void ConnectionsDock::_make_or_edit_connection() {
TreeItem *it = tree->get_selected(); TreeItem *it = tree->get_selected();
ERR_FAIL_COND(!it); ERR_FAIL_COND(!it);
String signal = it->get_metadata(0).operator Dictionary()["name"];
NodePath dst_path = connect_dialog->get_dst_path(); NodePath dst_path = connect_dialog->get_dst_path();
Node *target = node->get_node(dst_path); Node *target = node->get_node(dst_path);
ERR_FAIL_COND(!target); ERR_FAIL_COND(!target);
StringName dst_method = connect_dialog->get_dst_method(); Connection cToMake;
cToMake.source = connect_dialog->get_source();
cToMake.target = target;
cToMake.signal = connect_dialog->get_signal(); //it->get_metadata(0).operator Dictionary()["name"]
cToMake.method = connect_dialog->get_dst_method();
cToMake.binds = connect_dialog->get_binds();
bool defer = connect_dialog->get_deferred(); bool defer = connect_dialog->get_deferred();
bool oshot = connect_dialog->get_oneshot(); bool oshot = connect_dialog->get_oneshot();
Vector<Variant> binds = connect_dialog->get_binds(); cToMake.flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0);
PoolStringArray args = it->get_metadata(0).operator Dictionary()["args"];
int flags = CONNECT_PERSIST | (defer ? CONNECT_DEFERRED : 0) | (oshot ? CONNECT_ONESHOT : 0); if (connect_dialog->is_editing()) {
_disconnect(it);
undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), signal, String(dst_method))); _connect(cToMake);
undo_redo->add_do_method(node, "connect", signal, target, dst_method, binds, flags); }
undo_redo->add_undo_method(node, "disconnect", signal, target, dst_method); else {
undo_redo->add_do_method(this, "update_tree"); _connect(cToMake);
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_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree"); //to force redraw of scene tree
undo_redo->commit_action();
if (connect_dialog->get_make_callback()) { if (connect_dialog->get_make_callback()) {
PoolStringArray args = it->get_metadata(0).operator Dictionary()["args"];
print_line("request connect"); print_line("request connect");
editor->emit_signal("script_add_function_request", target, dst_method, args); editor->emit_signal("script_add_function_request", target, cToMake.method, args);
hide(); hide();
} }
update_tree(); update_tree();
} }
void ConnectionsDock::_connect(Connection cToMake)
{
Node* source = static_cast<Node*>(cToMake.source);
Node* target = static_cast<Node*>(cToMake.target);
if (!source || !target)
return;
undo_redo->create_action(vformat(TTR("Connect '%s' to '%s'"), String(cToMake.signal), String(cToMake.method)));
undo_redo->add_do_method(source, "connect", cToMake.signal, target, cToMake.method, cToMake.binds, cToMake.flags);
undo_redo->add_undo_method(source, "disconnect", cToMake.signal, target, cToMake.method);
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_undo_method(EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor(), "update_tree");
undo_redo->commit_action();
}
void ConnectionsDock::_disconnect( TreeItem *item ) void ConnectionsDock::_disconnect( TreeItem *item )
{ {
Connection c = item->get_metadata(0); Connection c = item->get_metadata(0);
@ -416,23 +457,22 @@ void ConnectionsDock::_disconnect( TreeItem *item )
undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method)); undo_redo->create_action(vformat(TTR("Disconnect '%s' from '%s'"), c.signal, c.method));
undo_redo->add_do_method(node, "disconnect", c.signal, c.target, c.method); undo_redo->add_do_method(node, "disconnect", c.signal, c.target, c.method);
undo_redo->add_undo_method(node, "connect", c.signal, c.target, c.method, Vector<Variant>(), c.flags); undo_redo->add_undo_method(node, "connect", c.signal, c.target, c.method, Vector<Variant>(), c.flags); //Empty binds?
undo_redo->add_do_method(this, "update_tree"); undo_redo->add_do_method(this, "update_tree");
undo_redo->add_undo_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"); //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(); undo_redo->commit_action();
c.source->disconnect(c.signal, c.target, c.method);
update_tree(); update_tree();
} }
void ConnectionsDock::_open_connection_dialog( TreeItem *item ) void ConnectionsDock::_open_connection_dialog( TreeItem *item ) {
{
String signal = item->get_metadata(0).operator Dictionary()["name"]; String signal = item->get_metadata(0).operator Dictionary()["name"];
String signalname = signal; String signalname = signal;
String midname = node->get_name(); String midname = node->get_name();
for (int i = 0; i < midname.length(); i++) { for (int i = 0; i < midname.length(); i++) { //TODO: Regex filter is cleaner.
CharType c = midname[i]; CharType c = midname[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_') {
//all good //all good
@ -443,15 +483,33 @@ void ConnectionsDock::_open_connection_dialog( TreeItem *item )
i--; i--;
continue; continue;
} }
midname[i] = c; midname[i] = c;
} }
connect_dialog->edit(node); Node *dst_node = node->get_owner() ? node->get_owner() : node;
StringName dst_method = "_on_" + midname + "_" + signal;
Connection c;
c.source = node;
c.signal = StringName(signalname);
c.target = dst_node;
c.method = dst_method;
connect_dialog->init(c);
connect_dialog->set_title(TTR("Connect Signal: ") + signalname);
connect_dialog->popup_centered_ratio(); connect_dialog->popup_centered_ratio();
connect_dialog->set_title(TTR("Connecting Signal:") + " " + signalname); }
connect_dialog->set_dst_method("_on_" + midname + "_" + signal);
connect_dialog->set_dst_node(node->get_owner() ? node->get_owner() : node); void ConnectionsDock::_open_connection_dialog( Connection cToEdit ) {
Node *src = static_cast<Node*>(cToEdit.source);
Node *dst = static_cast<Node*>(cToEdit.target);
if(src && dst) {
connect_dialog->init(cToEdit, true);
connect_dialog->set_title(TTR("Edit Connection: ") + cToEdit.signal);
connect_dialog->popup_centered_ratio();
}
} }
void ConnectionsDock::_connect_pressed() { void ConnectionsDock::_connect_pressed() {
@ -633,7 +691,6 @@ void ConnectionsDock::_something_selected() {
} else if (_is_item_signal( item )) { } else if (_is_item_signal( item )) {
connect_button->set_text(TTR("Connect...")); connect_button->set_text(TTR("Connect..."));
connect_button->set_disabled(false); connect_button->set_disabled(false);
} else { } else {
connect_button->set_text(TTR("Disconnect")); connect_button->set_text(TTR("Disconnect"));
connect_button->set_disabled(false); connect_button->set_disabled(false);
@ -688,7 +745,8 @@ void ConnectionsDock::_handle_slot_option( int option ) {
{ {
case SlotMenuOption::EDIT: case SlotMenuOption::EDIT:
{ {
//TODO: add edit functionality Connection c = item->get_metadata(0);
_open_connection_dialog(c);
} break; } break;
case SlotMenuOption::DISCONNECT: case SlotMenuOption::DISCONNECT:
{ {
@ -718,7 +776,7 @@ void ConnectionsDock::_rmb_pressed( Vector2 position ) {
} }
void ConnectionsDock::_bind_methods() { void ConnectionsDock::_bind_methods() {
ClassDB::bind_method("_connect", &ConnectionsDock::_connect); ClassDB::bind_method("_connect", &ConnectionsDock::_make_or_edit_connection);
ClassDB::bind_method("_something_selected", &ConnectionsDock::_something_selected); ClassDB::bind_method("_something_selected", &ConnectionsDock::_something_selected);
ClassDB::bind_method("_something_activated", &ConnectionsDock::_something_activated); ClassDB::bind_method("_something_activated", &ConnectionsDock::_something_activated);
ClassDB::bind_method("_handle_signal_option", &ConnectionsDock::_handle_signal_option); ClassDB::bind_method("_handle_signal_option", &ConnectionsDock::_handle_signal_option);

View file

@ -62,8 +62,11 @@ class ConnectDialog : public ConfirmationDialog {
CheckButton *oneshot; CheckButton *oneshot;
CheckButton *make_callback; CheckButton *make_callback;
PropertyEditor *bind_editor; PropertyEditor *bind_editor;
Node *node; Node *source;
StringName signal;
ConnectDialogBinds *cdbinds; ConnectDialogBinds *cdbinds;
bool bEditMode;
void ok_pressed(); void ok_pressed();
void _cancel_pressed(); void _cancel_pressed();
void _tree_node_selected(); void _tree_node_selected();
@ -78,13 +81,16 @@ public:
bool get_make_callback() { return make_callback->is_visible() && make_callback->is_pressed(); } bool get_make_callback() { return make_callback->is_visible() && make_callback->is_pressed(); }
NodePath get_dst_path() const; NodePath get_dst_path() const;
StringName get_dst_method() const; StringName get_dst_method() const;
Node *get_source() const;
StringName get_signal() const;
bool get_deferred() const; bool get_deferred() const;
bool get_oneshot() const; bool get_oneshot() const;
bool is_editing() const;
Vector<Variant> get_binds() const; Vector<Variant> get_binds() const;
void set_dst_method(const StringName &p_method); void set_dst_method(const StringName &p_method);
void set_dst_node(Node *p_node); void set_dst_node(Node *p_node);
void edit(Node *p_node); void init(Connection c, bool bEdit = false);
ConnectDialog(); ConnectDialog();
~ConnectDialog(); ~ConnectDialog();
@ -114,8 +120,10 @@ class ConnectionsDock : public VBoxContainer {
PopupMenu *slot_menu; PopupMenu *slot_menu;
void _close(); void _close();
void _connect(); void _make_or_edit_connection();
void _connect( Connection cToMake );
void _disconnect( TreeItem *item ); void _disconnect( TreeItem *item );
void _edit( TreeItem *item );
void _something_selected(); void _something_selected();
void _something_activated(); void _something_activated();
void _handle_signal_option( int option ); void _handle_signal_option( int option );
@ -131,7 +139,7 @@ protected:
private: private:
bool _is_item_signal( TreeItem *item ); bool _is_item_signal( TreeItem *item );
void _open_connection_dialog( TreeItem *item ); void _open_connection_dialog( TreeItem *item );
void _open_connection_dialog( Connection cToEdit );
public: public:
void set_undoredo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; } void set_undoredo(UndoRedo *p_undo_redo) { undo_redo = p_undo_redo; }