From 0b7384621e2f5e94621c6fcb301b88786ad12d2e Mon Sep 17 00:00:00 2001 From: Yuri Roubinsky Date: Sat, 4 Jul 2020 08:00:17 +0300 Subject: [PATCH] Prevents incorrect connection attempt on port clicking in GraphEdit, cherry-picked for 3.x --- scene/gui/graph_edit.cpp | 115 +++++++++++++++++++-------------------- scene/gui/graph_edit.h | 2 + 2 files changed, 57 insertions(+), 60 deletions(-) diff --git a/scene/gui/graph_edit.cpp b/scene/gui/graph_edit.cpp index a49e54e9986..73ca838e596 100644 --- a/scene/gui/graph_edit.cpp +++ b/scene/gui/graph_edit.cpp @@ -567,9 +567,9 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { Ref mb = p_ev; if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) { - + connecting_valid = false; Ref port = get_icon("port", "GraphNode"); - Vector2 mpos(mb->get_position().x, mb->get_position().y); + click_pos = mb->get_position(); for (int i = get_child_count() - 1; i >= 0; i--) { GraphNode *gn = Object::cast_to(get_child(i)); @@ -579,8 +579,7 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { for (int j = 0; j < gn->get_connection_output_count(); j++) { Vector2 pos = gn->get_connection_output_position(j) + gn->get_position(); - if (is_in_hot_zone(pos, mpos)) { - + if (is_in_hot_zone(pos, click_pos)) { if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) { //check disconnect for (List::Element *E = connections.front(); E; E = E->next()) { @@ -626,8 +625,7 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { for (int j = 0; j < gn->get_connection_input_count(); j++) { Vector2 pos = gn->get_connection_input_position(j) + gn->get_position(); - if (is_in_hot_zone(pos, mpos)) { - + if (is_in_hot_zone(pos, click_pos)) { if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) { //check disconnect for (List::Element *E = connections.front(); E; E = E->next()) { @@ -679,42 +677,40 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { connecting_to = mm->get_position(); connecting_target = false; top_layer->update(); - minimap->update(); + connecting_valid = click_pos.distance_to(connecting_to) > 20.0 * zoom; - Ref port = get_icon("port", "GraphNode"); - Vector2 mpos = mm->get_position(); - for (int i = get_child_count() - 1; i >= 0; i--) { - - GraphNode *gn = Object::cast_to(get_child(i)); - if (!gn) - continue; - - if (!connecting_out) { - for (int j = 0; j < gn->get_connection_output_count(); j++) { - - Vector2 pos = gn->get_connection_output_position(j) + gn->get_position(); - int type = gn->get_connection_output_type(j); - if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) { - - connecting_target = true; - connecting_to = pos; - connecting_target_to = gn->get_name(); - connecting_target_index = j; - return; - } + if (connecting_valid) { + Ref port = get_icon("port", "GraphNode"); + Vector2 mpos = mm->get_position(); + for (int i = get_child_count() - 1; i >= 0; i--) { + GraphNode *gn = Object::cast_to(get_child(i)); + if (!gn) { + continue; } - } else { - for (int j = 0; j < gn->get_connection_input_count(); j++) { - - Vector2 pos = gn->get_connection_input_position(j) + gn->get_position(); - int type = gn->get_connection_input_type(j); - if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) { - connecting_target = true; - connecting_to = pos; - connecting_target_to = gn->get_name(); - connecting_target_index = j; - return; + if (!connecting_out) { + for (int j = 0; j < gn->get_connection_output_count(); j++) { + Vector2 pos = gn->get_connection_output_position(j) + gn->get_position(); + int type = gn->get_connection_output_type(j); + if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) { + connecting_target = true; + connecting_to = pos; + connecting_target_to = gn->get_name(); + connecting_target_index = j; + return; + } + } + } else { + for (int j = 0; j < gn->get_connection_input_count(); j++) { + Vector2 pos = gn->get_connection_input_position(j) + gn->get_position(); + int type = gn->get_connection_input_type(j); + if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) { + connecting_target = true; + connecting_to = pos; + connecting_target_to = gn->get_name(); + connecting_target_index = j; + return; + } } } } @@ -722,30 +718,29 @@ void GraphEdit::_top_layer_input(const Ref &p_ev) { } if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) { + if (connecting_valid) { + if (connecting && connecting_target) { + String from = connecting_from; + int from_slot = connecting_index; + String to = connecting_target_to; + int to_slot = connecting_target_index; - if (connecting && connecting_target) { + if (!connecting_out) { + SWAP(from, to); + SWAP(from_slot, to_slot); + } + emit_signal("connection_request", from, from_slot, to, to_slot); - String from = connecting_from; - int from_slot = connecting_index; - String to = connecting_target_to; - int to_slot = connecting_target_index; + } else if (!just_disconnected) { + String from = connecting_from; + int from_slot = connecting_index; + Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y); - if (!connecting_out) { - SWAP(from, to); - SWAP(from_slot, to_slot); - } - emit_signal("connection_request", from, from_slot, to, to_slot); - - } else if (!just_disconnected) { - - String from = connecting_from; - int from_slot = connecting_index; - Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y); - - if (!connecting_out) { - emit_signal("connection_from_empty", from, from_slot, ofs); - } else { - emit_signal("connection_to_empty", from, from_slot, ofs); + if (!connecting_out) { + emit_signal("connection_from_empty", from, from_slot, ofs); + } else { + emit_signal("connection_to_empty", from, from_slot, ofs); + } } } diff --git a/scene/gui/graph_edit.h b/scene/gui/graph_edit.h index 155e390fb37..3be65224f8b 100644 --- a/scene/gui/graph_edit.h +++ b/scene/gui/graph_edit.h @@ -137,6 +137,8 @@ private: String connecting_target_to; int connecting_target_index; bool just_disconnected; + bool connecting_valid; + Vector2 click_pos; bool dragging; bool just_selected;