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

Commit a0f01e1

Browse files
committed
[WIP] many-to-many: change ConnLocations to match PortLocations
1 parent 7b0d018 commit a0f01e1

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

egui_node_graph/src/editor_ui.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ use super::*;
88
use egui::epaint::{CubicBezierShape, RectShape};
99
use egui::*;
1010

11+
/// Mapping from parameter id to positions of hooks it contains.
12+
///
13+
/// Outputs and short inputs always only have one hook, so the value is
14+
/// just `vec![port_position]`. Wide inputs may have multiple hooks.
1115
pub type PortLocations = std::collections::HashMap<AnyParameterId, Vec<Pos2>>;
12-
pub type ConnLocations = std::collections::HashMap<(InputId, usize), Pos2>;
16+
17+
/// Destination positions of connections made to a given input.
18+
///
19+
/// This is not equivalent to [`PortLocations`] because connections may be moved
20+
/// around (e.g. while an in-progress connection is hovered over a wide port),
21+
/// while hooks within a port are strictly a function of the port.
22+
pub type ConnLocations = std::collections::HashMap<InputId, Vec<Pos2>>;
23+
24+
/// Rectangle containing each node.
1325
pub type NodeRects = std::collections::HashMap<NodeId, Rect>;
1426

1527
const DISTANCE_TO_CONNECT: f32 = 10.0;
@@ -23,6 +35,9 @@ pub enum NodeResponse<UserResponse: UserResponseTrait, NodeData: NodeDataTrait>
2335
ConnectEventEnded {
2436
output: OutputId,
2537
input: InputId,
38+
/// Index of the connection in wide input ports.
39+
///
40+
/// If the input isn't a wide port this is always 0 and may be ignored.
2641
input_hook: usize,
2742
},
2843
CreatedNode(NodeId),
@@ -310,7 +325,7 @@ where
310325
let connection_color = port_type.data_type_color(user_state);
311326
// outputs can't be wide yet so this is fine.
312327
let src_pos = port_locations[&AnyParameterId::Output(output)][0];
313-
let dst_pos = conn_locations[&(input, hook_n)];
328+
let dst_pos = conn_locations[&input][hook_n];
314329
draw_connection(ui.painter(), src_pos, dst_pos, connection_color);
315330
}
316331
}
@@ -802,7 +817,7 @@ where
802817
.iter()
803818
.enumerate()
804819
{
805-
conn_locations.insert((input, k), *dst_pos);
820+
conn_locations.entry(input).or_default().insert(k, *dst_pos);
806821
}
807822
}
808823
}
@@ -816,8 +831,8 @@ where
816831
.and_then(|(mouse_pos, input)| {
817832
let hooks = 0..inner_ports;
818833
hooks.min_by(|&hook1, &hook2| {
819-
let out1_dist = conn_locations[&(input, hook1)].distance(mouse_pos);
820-
let out2_dist = conn_locations[&(input, hook2)].distance(mouse_pos);
834+
let out1_dist = conn_locations[&input][hook1].distance(mouse_pos);
835+
let out2_dist = conn_locations[&input][hook2].distance(mouse_pos);
821836

822837
out1_dist.partial_cmp(&out2_dist).unwrap()
823838
})
@@ -863,7 +878,7 @@ where
863878
} else if wide_port && !port_full {
864879
// move connections below the in-progress one to a lower position
865880
for k in input_hook..graph.connections(input).len() {
866-
conn_locations.get_mut(&(input, k)).unwrap().y += 7.5;
881+
conn_locations.get_mut(&input).unwrap()[k].y += 7.5;
867882
}
868883
}
869884
}

0 commit comments

Comments
 (0)