Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Do not snap incompatible ports
Browse files Browse the repository at this point in the history
This takes care of the second half of #54, where ports snapped
to incompatible colors even when the connection was not possible.
  • Loading branch information
setzer22 committed Sep 15, 2022
1 parent 3eef3ac commit c0ba6fe
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions egui_node_graph/src/editor_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,31 +178,62 @@ where
let start_pos = port_locations[locator];

// Find a port to connect to
fn snap_to_ports<Key: slotmap::Key + Into<AnyParameterId>, Value>(
fn snap_to_ports<
NodeData,
UserState,
DataType: DataTypeTrait<UserState>,
ValueType,
Key: slotmap::Key + Into<AnyParameterId>,
Value,
>(
graph: &Graph<NodeData, DataType, ValueType>,
port_type: &DataType,
ports: &SlotMap<Key, Value>,
port_locations: &PortLocations,
cursor_pos: Pos2,
) -> Pos2 {
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,
),
};
Expand Down

0 comments on commit c0ba6fe

Please sign in to comment.