Merge pull request #40112 from Chaosus/graphedit_connection_enchancement

Prevents incorrect connection attempt on port clicking in GraphEdit
This commit is contained in:
Rémi Verschelde 2020-07-05 00:55:03 +02:00 committed by GitHub
commit e4657bd724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 50 deletions

View File

@ -370,8 +370,9 @@ bool GraphEdit::_filter_input(const Point2 &p_point) {
void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) { void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
Ref<InputEventMouseButton> mb = p_ev; Ref<InputEventMouseButton> mb = p_ev;
if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) { if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
connecting_valid = false;
Ref<Texture2D> port = get_theme_icon("port", "GraphNode"); Ref<Texture2D> port = get_theme_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--) { for (int i = get_child_count() - 1; i >= 0; i--) {
GraphNode *gn = Object::cast_to<GraphNode>(get_child(i)); GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
if (!gn) { if (!gn) {
@ -380,7 +381,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
for (int j = 0; j < gn->get_connection_output_count(); j++) { for (int j = 0; j < gn->get_connection_output_count(); j++) {
Vector2 pos = gn->get_connection_output_position(j) + gn->get_position(); 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))) { if (valid_left_disconnect_types.has(gn->get_connection_output_type(j))) {
//check disconnect //check disconnect
for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
@ -422,7 +423,7 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
for (int j = 0; j < gn->get_connection_input_count(); j++) { for (int j = 0; j < gn->get_connection_input_count(); j++) {
Vector2 pos = gn->get_connection_input_position(j) + gn->get_position(); 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))) { if (right_disconnects || valid_right_disconnect_types.has(gn->get_connection_input_type(j))) {
//check disconnect //check disconnect
for (List<Connection>::Element *E = connections.front(); E; E = E->next()) { for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
@ -470,37 +471,40 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
connecting_to = mm->get_position(); connecting_to = mm->get_position();
connecting_target = false; connecting_target = false;
top_layer->update(); top_layer->update();
connecting_valid = click_pos.distance_to(connecting_to) > 20.0 * zoom;
Ref<Texture2D> port = get_theme_icon("port", "GraphNode"); if (connecting_valid) {
Vector2 mpos = mm->get_position(); Ref<Texture2D> port = get_theme_icon("port", "GraphNode");
for (int i = get_child_count() - 1; i >= 0; i--) { Vector2 mpos = mm->get_position();
GraphNode *gn = Object::cast_to<GraphNode>(get_child(i)); for (int i = get_child_count() - 1; i >= 0; i--) {
if (!gn) { GraphNode *gn = Object::cast_to<GraphNode>(get_child(i));
continue; 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;
}
} }
} else {
for (int j = 0; j < gn->get_connection_input_count(); j++) { if (!connecting_out) {
Vector2 pos = gn->get_connection_input_position(j) + gn->get_position(); for (int j = 0; j < gn->get_connection_output_count(); j++) {
int type = gn->get_connection_input_type(j); Vector2 pos = gn->get_connection_output_position(j) + gn->get_position();
if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) { int type = gn->get_connection_output_type(j);
connecting_target = true; if ((type == connecting_type || valid_connection_types.has(ConnType(type, connecting_type))) && is_in_hot_zone(pos, mpos)) {
connecting_to = pos; connecting_target = true;
connecting_target_to = gn->get_name(); connecting_to = pos;
connecting_target_index = j; connecting_target_to = gn->get_name();
return; 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;
}
} }
} }
} }
@ -508,27 +512,29 @@ void GraphEdit::_top_layer_input(const Ref<InputEvent> &p_ev) {
} }
if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) { if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
if (connecting && connecting_target) { if (connecting_valid) {
String from = connecting_from; if (connecting && connecting_target) {
int from_slot = connecting_index; String from = connecting_from;
String to = connecting_target_to; int from_slot = connecting_index;
int to_slot = connecting_target_index; String to = connecting_target_to;
int to_slot = connecting_target_index;
if (!connecting_out) { if (!connecting_out) {
SWAP(from, to); SWAP(from, to);
SWAP(from_slot, to_slot); SWAP(from_slot, to_slot);
} }
emit_signal("connection_request", from, from_slot, to, to_slot); emit_signal("connection_request", from, from_slot, to, to_slot);
} else if (!just_disconnected) { } else if (!just_disconnected) {
String from = connecting_from; String from = connecting_from;
int from_slot = connecting_index; int from_slot = connecting_index;
Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y); Vector2 ofs = Vector2(mb->get_position().x, mb->get_position().y);
if (!connecting_out) { if (!connecting_out) {
emit_signal("connection_from_empty", from, from_slot, ofs); emit_signal("connection_from_empty", from, from_slot, ofs);
} else { } else {
emit_signal("connection_to_empty", from, from_slot, ofs); emit_signal("connection_to_empty", from, from_slot, ofs);
}
} }
} }

View File

@ -93,6 +93,8 @@ private:
String connecting_target_to; String connecting_target_to;
int connecting_target_index; int connecting_target_index;
bool just_disconnected; bool just_disconnected;
bool connecting_valid;
Vector2 click_pos;
bool dragging; bool dragging;
bool just_selected; bool just_selected;