Skip to content

Commit

Permalink
Let to_graph_custom accept generic closures (#131)
Browse files Browse the repository at this point in the history
This PR lets `egui_graphs::to_graph_custom` accept generic functions and
closures.

This was still possible in `0.15`, but unfortunately, starting from
`0.16` it only accepts bare function pointers. This has the downside
that it's no longer possible to reference external state while
transforming a graph. Well, unless you hack something together using
global variables but I was unwilling to go that route 😉.
  • Loading branch information
oisyn authored Nov 22, 2023
1 parent 3fc8fcb commit 75be8bf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/draw/displays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
/// Use `ctx.meta` to properly scale and translate the shape.
fn shapes(&mut self, ctx: &DrawContext) -> Vec<Shape>;

/// Is called on every framte. Can be used for updating state of the implementation of [DisplayNode]
/// Is called on every frame. Can be used for updating state of the implementation of [DisplayNode]
fn update(&mut self, state: &NodeProps<N>);

/// Checks if the provided `pos` is inside the shape.
Expand Down
20 changes: 8 additions & 12 deletions src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ use std::collections::HashMap;

pub const DEFAULT_SPAWN_SIZE: f32 = 250.;

pub type EdgeTransform<N, E, Ty, Ix, Dn, D> =
fn(EdgeIndex<Ix>, &E, usize) -> Edge<N, E, Ty, Ix, Dn, D>;
pub type NodeTransform<N, E, Ty, Ix, D> = fn(NodeIndex<Ix>, &N) -> Node<N, E, Ty, Ix, D>;

/// Helper function which adds user's node to the [`super::Graph`] instance.
///
/// If graph is not empty it picks any node position and adds new node in the vicinity of it.
Expand All @@ -37,7 +33,7 @@ pub fn add_node_custom<
>(
g: &mut Graph<N, E, Ty, Ix, D>,
n: &N,
node_transform: NodeTransform<N, E, Ty, Ix, D>,
node_transform: impl FnOnce(NodeIndex<Ix>, &N) -> Node<N, E, Ty, Ix, D>,
) -> NodeIndex<Ix> {
g.g.add_node(node_transform(
NodeIndex::<Ix>::new(g.g.node_count() + 1),
Expand Down Expand Up @@ -81,7 +77,7 @@ pub fn add_edge_custom<
start: NodeIndex<Ix>,
end: NodeIndex<Ix>,
e: &E,
edge_transform: EdgeTransform<N, E, Ty, Ix, Dn, De>,
edge_transform: impl FnOnce(EdgeIndex<Ix>, &E, usize) -> Edge<N, E, Ty, Ix, Dn, De>,
) -> EdgeIndex<Ix> {
let order = g.g.edges_connecting(start, end).count();
g.g.add_edge(
Expand Down Expand Up @@ -147,7 +143,7 @@ pub fn to_graph<
>(
g: &StableGraph<N, E, Ty, Ix>,
) -> Graph<N, E, Ty, Ix, Dn, De> {
transform(g, default_node_transform, default_edge_transform)
transform(g, &mut default_node_transform, &mut default_edge_transform)
}

/// The same as [`to_graph`], but allows to define custom transformation procedures for nodes and edges.
Expand All @@ -160,10 +156,10 @@ pub fn to_graph_custom<
De: DisplayEdge<N, E, Ty, Ix, Dn>,
>(
g: &StableGraph<N, E, Ty, Ix>,
node_transform: NodeTransform<N, E, Ty, Ix, Dn>,
edge_transform: EdgeTransform<N, E, Ty, Ix, Dn, De>,
mut node_transform: impl FnMut(NodeIndex<Ix>, &N) -> Node<N, E, Ty, Ix, Dn>,
mut edge_transform: impl FnMut(EdgeIndex<Ix>, &E, usize) -> Edge<N, E, Ty, Ix, Dn, De>,
) -> Graph<N, E, Ty, Ix, Dn, De> {
transform(g, node_transform, edge_transform)
transform(g, &mut node_transform, &mut edge_transform)
}

/// Default node transform function. Keeps original data and creates a new node with a random location and
Expand Down Expand Up @@ -218,8 +214,8 @@ fn transform<
De: DisplayEdge<N, E, Ty, Ix, Dn>,
>(
g: &StableGraph<N, E, Ty, Ix>,
node_transform: NodeTransform<N, E, Ty, Ix, Dn>,
edge_transform: EdgeTransform<N, E, Ty, Ix, Dn, De>,
node_transform: &mut impl FnMut(NodeIndex<Ix>, &N) -> Node<N, E, Ty, Ix, Dn>,
edge_transform: &mut impl FnMut(EdgeIndex<Ix>, &E, usize) -> Edge<N, E, Ty, Ix, Dn, De>,
) -> Graph<N, E, Ty, Ix, Dn, De> {
let mut input_g =
StableGraph::<Node<N, E, Ty, Ix, Dn>, Edge<N, E, Ty, Ix, Dn, De>, Ty, Ix>::default();
Expand Down

0 comments on commit 75be8bf

Please sign in to comment.