mirror of
https://github.com/eliasstepanik/egui_node_graph.git
synced 2026-01-16 00:08:27 +00:00
Encapsulated snap_to_ports
This commit is contained in:
parent
b97a567eb7
commit
a11c6c623d
@ -176,38 +176,34 @@ where
|
|||||||
let port_type = self.graph.any_param_type(*locator).unwrap();
|
let port_type = self.graph.any_param_type(*locator).unwrap();
|
||||||
let connection_color = port_type.data_type_color(&self.user_state);
|
let connection_color = port_type.data_type_color(&self.user_state);
|
||||||
let start_pos = port_locations[locator];
|
let start_pos = port_locations[locator];
|
||||||
|
|
||||||
|
// Find a port to connect to
|
||||||
|
fn snap_to_ports<Key: slotmap::Key + Into<AnyParameterId>, Value>(
|
||||||
|
ports: &SlotMap<Key, Value>,
|
||||||
|
port_locations: &PortLocations,
|
||||||
|
cursor_pos: Pos2,
|
||||||
|
) -> Pos2 {
|
||||||
|
ports
|
||||||
|
.iter()
|
||||||
|
.find_map(|(port_id, _)| {
|
||||||
|
let port_pos = port_locations[&port_id.into()];
|
||||||
|
if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT {
|
||||||
|
Some(port_pos)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or(cursor_pos)
|
||||||
|
}
|
||||||
let (src_pos, dst_pos) = match locator {
|
let (src_pos, dst_pos) = match locator {
|
||||||
AnyParameterId::Output(_) => {
|
AnyParameterId::Output(_) => (
|
||||||
// Find a port to connect to
|
start_pos,
|
||||||
let port = self.graph.inputs.iter().find_map(|(input_id, _)| {
|
snap_to_ports(&self.graph.inputs, &port_locations, cursor_pos),
|
||||||
let port_pos = port_locations[&AnyParameterId::Input(input_id)];
|
),
|
||||||
if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT {
|
AnyParameterId::Input(_) => (
|
||||||
Some(port_pos)
|
snap_to_ports(&self.graph.outputs, &port_locations, cursor_pos),
|
||||||
} else {
|
start_pos,
|
||||||
None
|
),
|
||||||
}
|
|
||||||
});
|
|
||||||
if let Some(port_pos) = port {
|
|
||||||
(start_pos, port_pos)
|
|
||||||
} else {
|
|
||||||
(start_pos, cursor_pos)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AnyParameterId::Input(_) => {
|
|
||||||
let port = self.graph.outputs.iter().find_map(|(output_id, _)| {
|
|
||||||
let port_pos = port_locations[&AnyParameterId::Output(output_id)];
|
|
||||||
if port_pos.distance(cursor_pos) < DISTANCE_TO_CONNECT {
|
|
||||||
Some(port_pos)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if let Some(port_pos) = port {
|
|
||||||
(port_pos, start_pos)
|
|
||||||
} else {
|
|
||||||
(cursor_pos, start_pos)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
draw_connection(ui.painter(), src_pos, dst_pos, connection_color);
|
draw_connection(ui.painter(), src_pos, dst_pos, connection_color);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,3 +23,15 @@ impl AnyParameterId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<OutputId> for AnyParameterId {
|
||||||
|
fn from(output: OutputId) -> Self {
|
||||||
|
Self::Output(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<InputId> for AnyParameterId {
|
||||||
|
fn from(input: InputId) -> Self {
|
||||||
|
Self::Input(input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user