From c0ba6fe227bf033ff2c5d2dbe05dd6b9bb128edc Mon Sep 17 00:00:00 2001 From: Setzer22 Date: Thu, 15 Sep 2022 11:38:02 +0200 Subject: [PATCH 1/2] Do not snap incompatible ports This takes care of the second half of #54, where ports snapped to incompatible colors even when the connection was not possible. --- egui_node_graph/src/editor_ui.rs | 51 +++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index 5ecfc60..b1fc5fd 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -178,7 +178,16 @@ where let start_pos = port_locations[locator]; // Find a port to connect to - fn snap_to_ports, Value>( + fn snap_to_ports< + NodeData, + UserState, + DataType: DataTypeTrait, + ValueType, + Key: slotmap::Key + Into, + Value, + >( + graph: &Graph, + port_type: &DataType, ports: &SlotMap, port_locations: &PortLocations, cursor_pos: Pos2, @@ -186,23 +195,45 @@ where ports .iter() .find_map(|(port_id, _)| { - port_locations.get(&port_id.into()).and_then(|port_pos| { - if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT { - Some(*port_pos) - } else { - None - } - }) + let compatible_ports = graph + .any_param_type(port_id.into()) + .map(|other| other == port_type) + .unwrap_or(false); + + if compatible_ports { + port_locations.get(&port_id.into()).and_then(|port_pos| { + if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT { + Some(*port_pos) + } else { + None + } + }) + } else { + None + } }) .unwrap_or(cursor_pos) } + let (src_pos, dst_pos) = match locator { AnyParameterId::Output(_) => ( start_pos, - snap_to_ports(&self.graph.inputs, &port_locations, cursor_pos), + snap_to_ports( + &self.graph, + port_type, + &self.graph.inputs, + &port_locations, + cursor_pos, + ), ), AnyParameterId::Input(_) => ( - snap_to_ports(&self.graph.outputs, &port_locations, cursor_pos), + snap_to_ports( + &self.graph, + port_type, + &self.graph.outputs, + &port_locations, + cursor_pos, + ), start_pos, ), }; From 234d377302da9c4313af3ce895da484912b59384 Mon Sep 17 00:00:00 2001 From: Setzer22 Date: Thu, 15 Sep 2022 12:03:37 +0200 Subject: [PATCH 2/2] Fix bug when reconnecting port There was a bug where removing an existing connection and re-wiring it to the same port would not work. This is now fixed --- egui_node_graph/src/editor_ui.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/egui_node_graph/src/editor_ui.rs b/egui_node_graph/src/editor_ui.rs index b1fc5fd..b955111 100644 --- a/egui_node_graph/src/editor_ui.rs +++ b/egui_node_graph/src/editor_ui.rs @@ -292,7 +292,7 @@ where self.node_order.retain(|id| *id != *node_id); } NodeResponse::DisconnectEvent { input, output } => { - let other_node = self.graph.get_input(*input).node(); + let other_node = self.graph.get_output(*output).node; self.graph.remove_connection(*input); self.connection_in_progress = Some((other_node, AnyParameterId::Output(*output)));