diff --git a/examples/animated_nodes/src/node.rs b/examples/animated_nodes/src/node.rs index e38cd18..c8d93e4 100644 --- a/examples/animated_nodes/src/node.rs +++ b/examples/animated_nodes/src/node.rs @@ -61,8 +61,8 @@ impl NodeShapeAnimated { impl From> for NodeShapeAnimated { fn from(node_props: NodeProps) -> Self { Self { - label: node_props.label, - loc: node_props.location, + label: node_props.label.clone(), + loc: node_props.location(), dragged: node_props.dragged, clockwise: node_props.payload.get_is_clockwise(), @@ -145,7 +145,7 @@ impl DisplayNode< fn update(&mut self, state: &NodeProps) { self.label = state.label.clone(); - self.loc = state.location; + self.loc = state.location(); self.dragged = state.dragged; self.clockwise = state.payload.get_is_clockwise(); } diff --git a/examples/basic/src/main.rs b/examples/basic/src/main.rs index 199a93b..79ffe84 100644 --- a/examples/basic/src/main.rs +++ b/examples/basic/src/main.rs @@ -1,7 +1,7 @@ use eframe::{run_native, App, CreationContext, NativeOptions}; use egui::Context; -use egui_graphs::{DefaultEdgeShape, DefaultGraphView, DefaultNodeShape, Graph, GraphView}; -use petgraph::{csr::DefaultIx, stable_graph::StableGraph, Directed}; +use egui_graphs::{DefaultGraphView, Graph}; +use petgraph::stable_graph::StableGraph; pub struct BasicApp { g: Graph, diff --git a/examples/flex_nodes/src/node.rs b/examples/flex_nodes/src/node.rs index d52e9a7..3470640 100644 --- a/examples/flex_nodes/src/node.rs +++ b/examples/flex_nodes/src/node.rs @@ -14,8 +14,8 @@ pub struct NodeShapeFlex { impl From> for NodeShapeFlex { fn from(node_props: NodeProps) -> Self { Self { - label: node_props.label, - loc: node_props.location, + label: node_props.label.clone(), + loc: node_props.location(), size_x: 0., size_y: 0., @@ -67,7 +67,7 @@ impl DisplayNode fn update(&mut self, state: &NodeProps) { self.label = state.label.clone(); - self.loc = state.location; + self.loc = state.location(); } } diff --git a/examples/wasm_custom_draw/src/node.rs b/examples/wasm_custom_draw/src/node.rs index fb3d337..636e4a1 100644 --- a/examples/wasm_custom_draw/src/node.rs +++ b/examples/wasm_custom_draw/src/node.rs @@ -24,8 +24,8 @@ pub struct NodeShapeAnimated { impl From> for NodeShapeAnimated { fn from(node_props: NodeProps) -> Self { Self { - label: node_props.label, - loc: node_props.location, + label: node_props.label.clone(), + loc: node_props.location(), dragged: node_props.dragged, angle_rad: Default::default(), @@ -120,7 +120,7 @@ impl DisplayNode fn update(&mut self, state: &NodeProps) { self.label = state.label.clone(); - self.loc = state.location; + self.loc = state.location(); self.dragged = state.dragged; } } diff --git a/src/draw/displays_default/node.rs b/src/draw/displays_default/node.rs index 032740e..879ba56 100644 --- a/src/draw/displays_default/node.rs +++ b/src/draw/displays_default/node.rs @@ -25,7 +25,7 @@ pub struct DefaultNodeShape { impl From> for DefaultNodeShape { fn from(node_props: NodeProps) -> Self { DefaultNodeShape { - pos: node_props.location, // can be null if not set on creation explicitly + pos: node_props.location(), selected: node_props.selected, dragged: node_props.dragged, label_text: node_props.label.to_string(), @@ -94,7 +94,7 @@ impl DisplayNode } fn update(&mut self, state: &NodeProps) { - self.pos = state.location; // should be set by the layout before rendering + self.pos = state.location(); self.selected = state.selected; self.dragged = state.dragged; self.label_text = state.label.to_string(); diff --git a/src/draw/drawer.rs b/src/draw/drawer.rs index 1fc3bc9..f7be370 100644 --- a/src/draw/drawer.rs +++ b/src/draw/drawer.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use egui::{util::id_type_map::SerializableAny, Context, Painter, Shape}; +use egui::{Context, Painter, Shape}; use petgraph::graph::IndexType; use petgraph::EdgeType; diff --git a/src/elements/node.rs b/src/elements/node.rs index 05f2be4..28054dd 100644 --- a/src/elements/node.rs +++ b/src/elements/node.rs @@ -12,12 +12,26 @@ use crate::{DefaultNodeShape, DisplayNode}; /// Stores properties of a [Node] #[derive(Clone, Debug, Serialize, Deserialize)] -pub struct NodeProps { +pub struct NodeProps +where + N: Clone, +{ pub payload: N, - pub location: Pos2, pub label: String, pub selected: bool, pub dragged: bool, + + location: Pos2, + location_user: Option, +} + +impl NodeProps +where + N: Clone, +{ + pub fn location(&self) -> Pos2 { + self.location_user.unwrap_or(self.location) + } } #[derive(Serialize, Deserialize)] @@ -83,6 +97,7 @@ where let props = NodeProps { payload, location: Pos2::default(), + location_user: Option::default(), label: String::default(), selected: bool::default(), dragged: bool::default(), @@ -133,10 +148,14 @@ where } pub fn location(&self) -> Pos2 { - self.props.location + self.props.location() } pub fn set_location(&mut self, loc: Pos2) { + self.props.location_user = Some(loc); + } + + pub(crate) fn set_layout_location(&mut self, loc: Pos2) { self.props.location = loc; } diff --git a/src/helpers.rs b/src/helpers.rs index dcda108..c7616df 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -241,7 +241,6 @@ where #[cfg(test)] mod tests { - use crate::layouts; use crate::DefaultEdgeShape; use crate::DefaultNodeShape; @@ -267,10 +266,6 @@ mod tests { let input_n = input_g.g.node_weight(input_idx).unwrap(); assert_eq!(*input_n.payload(), *user_n); - - // assert!(input_n.location().x >= 0.0 && input_n.location().x <= DEFAULT_SPAWN_SIZE); - // assert!(input_n.location().y >= 0.0 && input_n.location().y <= DEFAULT_SPAWN_SIZE); - assert_eq!(*input_n.label(), format!("node {}", user_idx.index())); assert!(!input_n.selected()); @@ -296,10 +291,6 @@ mod tests { let input_n = input_g.g.node_weight(input_idx).unwrap(); assert_eq!(*input_n.payload(), *user_n); - - // assert!(input_n.location().x >= 0.0 && input_n.location().x <= DEFAULT_SPAWN_SIZE); - // assert!(input_n.location().y >= 0.0 && input_n.location().y <= DEFAULT_SPAWN_SIZE); - assert_eq!(*input_n.label(), format!("node {}", user_idx.index())); assert!(!input_n.selected()); diff --git a/src/layouts/random/layout.rs b/src/layouts/random/layout.rs index e4876ac..f1581e7 100644 --- a/src/layouts/random/layout.rs +++ b/src/layouts/random/layout.rs @@ -38,7 +38,7 @@ impl Layout for Random { let mut rng = rand::thread_rng(); for node in g.g.node_weights_mut() { - node.set_location(Pos2::new( + node.set_layout_location(Pos2::new( rng.gen_range(0. ..SPAWN_SIZE), rng.gen_range(0. ..SPAWN_SIZE), ));