From afeffb01010af3536bcda0f90704cbadc843d6e5 Mon Sep 17 00:00:00 2001 From: starlord Date: Wed, 22 Nov 2023 02:00:32 +0400 Subject: [PATCH] Added payload to props (#129) Added user's payload to `NodeProps` and `EdgeProps` for user to have access to them in the drawing shapes implementation. --- examples/custom_draw/src/node.rs | 6 +++--- src/draw/default_edge.rs | 6 +++--- src/draw/default_node.rs | 6 +++--- src/draw/displays.rs | 8 ++++---- src/elements/edge.rs | 25 ++++++++++++++----------- src/elements/node.rs | 30 ++++++++++++++++-------------- src/transform.rs | 4 ++-- 7 files changed, 45 insertions(+), 40 deletions(-) diff --git a/examples/custom_draw/src/node.rs b/examples/custom_draw/src/node.rs index 5ea94fe..d8502f6 100644 --- a/examples/custom_draw/src/node.rs +++ b/examples/custom_draw/src/node.rs @@ -21,8 +21,8 @@ pub struct NodeShapeAnimated { size: f32, } -impl From for NodeShapeAnimated { - fn from(node_props: NodeProps) -> Self { +impl From> for NodeShapeAnimated { + fn from(node_props: NodeProps) -> Self { Self { label: node_props.label, loc: node_props.location, @@ -118,7 +118,7 @@ impl DisplayNode vec![shape_rect, shape_label.into()] } - fn update(&mut self, state: &NodeProps) { + fn update(&mut self, state: &NodeProps) { self.label = state.label.clone(); self.loc = state.location; self.dragged = state.dragged; diff --git a/src/draw/default_edge.rs b/src/draw/default_edge.rs index e6d8a99..d606b95 100644 --- a/src/draw/default_edge.rs +++ b/src/draw/default_edge.rs @@ -22,8 +22,8 @@ pub struct DefaultEdgeShape { pub loop_size: f32, } -impl From for DefaultEdgeShape { - fn from(edge: EdgeProps) -> Self { +impl From> for DefaultEdgeShape { + fn from(edge: EdgeProps) -> Self { Self { order: edge.order, selected: edge.selected, @@ -172,7 +172,7 @@ impl) { self.order = state.order; self.selected = state.selected; } diff --git a/src/draw/default_node.rs b/src/draw/default_node.rs index 1b6f3ff..5d42eac 100644 --- a/src/draw/default_node.rs +++ b/src/draw/default_node.rs @@ -24,8 +24,8 @@ pub struct DefaultNodeShape { pub radius: f32, } -impl From for DefaultNodeShape { - fn from(node_props: NodeProps) -> Self { +impl From> for DefaultNodeShape { + fn from(node_props: NodeProps) -> Self { DefaultNodeShape { pos: node_props.location, selected: node_props.selected, @@ -90,7 +90,7 @@ impl DisplayNode res } - fn update(&mut self, state: &NodeProps) { + fn update(&mut self, state: &NodeProps) { self.pos = state.location; self.pos = state.location; self.selected = state.selected; diff --git a/src/draw/displays.rs b/src/draw/displays.rs index 89f3045..82dda20 100644 --- a/src/draw/displays.rs +++ b/src/draw/displays.rs @@ -3,7 +3,7 @@ use petgraph::{stable_graph::IndexType, EdgeType}; use crate::{draw::drawer::DrawContext, elements::EdgeProps, Node, NodeProps}; -pub trait DisplayNode: Clone + From +pub trait DisplayNode: Clone + From> where N: Clone, E: Clone, @@ -27,7 +27,7 @@ where fn shapes(&mut self, ctx: &DrawContext) -> Vec; /// Is called on every framte. Can be used for updating state of the implementation of [DisplayNode] - fn update(&mut self, state: &NodeProps); + fn update(&mut self, state: &NodeProps); /// Checks if the provided `pos` is inside the shape. /// @@ -37,7 +37,7 @@ where fn is_inside(&self, pos: Pos2) -> bool; } -pub trait DisplayEdge: Clone + From +pub trait DisplayEdge: Clone + From> where N: Clone, E: Clone, @@ -63,7 +63,7 @@ where ) -> Vec; /// Is called on every frame. Can be used for updating state of the implementation of [DisplayNode] - fn update(&mut self, state: &EdgeProps); + fn update(&mut self, state: &EdgeProps); /// Checks if the provided `pos` is inside the shape. /// diff --git a/src/elements/edge.rs b/src/elements/edge.rs index e9aa6af..fa87d91 100644 --- a/src/elements/edge.rs +++ b/src/elements/edge.rs @@ -9,8 +9,9 @@ use crate::{DefaultEdgeShape, DefaultNodeShape, DisplayEdge, DisplayNode}; /// Stores properties of an [Edge] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[derive(Clone, Debug, Default)] -pub struct EdgeProps { +#[derive(Clone, Debug)] +pub struct EdgeProps { + pub payload: E, pub order: usize, pub selected: bool, } @@ -28,11 +29,9 @@ pub struct Edge< > { id: Option>, - /// Client data - payload: E, display: D, - props: EdgeProps, + props: EdgeProps, _marker: PhantomData<(N, Ty, Dn)>, } @@ -46,11 +45,15 @@ impl< > Edge { pub fn new(payload: E) -> Self { - let props = EdgeProps::default(); - let display = D::from(props.clone()); - Self { + let props = EdgeProps { payload, + order: usize::default(), + selected: bool::default(), + }; + + let display = D::from(props.clone()); + Self { props, display, @@ -59,7 +62,7 @@ impl< } } - pub fn props(&self) -> &EdgeProps { + pub fn props(&self) -> &EdgeProps { &self.props } @@ -90,11 +93,11 @@ impl< } pub fn payload(&self) -> &E { - &self.payload + &self.props.payload } pub fn payload_mut(&mut self) -> &mut E { - &mut self.payload + &mut self.props.payload } pub fn set_selected(&mut self, selected: bool) { diff --git a/src/elements/node.rs b/src/elements/node.rs index 4f7a017..80d5e4b 100644 --- a/src/elements/node.rs +++ b/src/elements/node.rs @@ -11,8 +11,9 @@ use crate::{DefaultNodeShape, DisplayNode}; /// Stores properties of a [Node] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[derive(Clone, Debug, Default)] -pub struct NodeProps { +#[derive(Clone, Debug)] +pub struct NodeProps { + pub payload: N, pub location: Pos2, pub label: String, pub selected: bool, @@ -29,9 +30,8 @@ where D: DisplayNode, { id: Option>, - payload: N, - props: NodeProps, + props: NodeProps, display: D, _marker: PhantomData<(E, Ty)>, @@ -46,10 +46,7 @@ where D: DisplayNode, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut res = String::default(); - res += format!("id : {:?}", self.id).as_str(); - res += format!(", props: {:?}", self.props()).as_str(); - f.write_str(format!("Node {{{}}}", res).as_str()) + f.debug_struct("Node").field("id", &self.id).finish() } } @@ -65,7 +62,6 @@ where let idx = self.id().index(); Self { id: Some(NodeIndex::new(idx)), - payload: self.payload().clone(), props: self.props.clone(), display: self.display.clone(), _marker: PhantomData, @@ -82,10 +78,16 @@ where D: DisplayNode, { pub fn new(payload: N) -> Self { - let props = NodeProps::default(); + let props = NodeProps { + payload, + location: Pos2::default(), + label: String::default(), + selected: bool::default(), + dragged: bool::default(), + }; + let display = D::from(props.clone()); Self { - payload, props, display, @@ -94,7 +96,7 @@ where } } - pub fn props(&self) -> &NodeProps { + pub fn props(&self) -> &NodeProps { &self.props } @@ -119,11 +121,11 @@ where } pub fn payload(&self) -> &N { - &self.payload + &self.props.payload } pub fn payload_mut(&mut self) -> &mut N { - &mut self.payload + &mut self.props.payload } pub fn location(&self) -> Pos2 { diff --git a/src/transform.rs b/src/transform.rs index 3ba95d2..b0fba10 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -108,7 +108,7 @@ pub fn add_edge_custom< /// # Example /// ``` /// use petgraph::stable_graph::StableGraph; -/// use egui_graphs::to_graph; +/// use egui_graphs::{to_graph, DefaultNodeShape, DefaultEdgeShape, Graph}; /// use egui::Pos2; /// /// let mut user_graph: StableGraph<&str, &str> = StableGraph::new(); @@ -116,7 +116,7 @@ pub fn add_edge_custom< /// let node2 = user_graph.add_node("B"); /// user_graph.add_edge(node1, node2, "edge1"); /// -/// let input_graph: egui_graphs::Graph<_, _, _, _, egui_graphs::DefaultNodeShape, egui_graphs::DefaultEdgeShape> = to_graph(&user_graph); +/// let input_graph: Graph<_, _, _, _, DefaultNodeShape, DefaultEdgeShape> = to_graph(&user_graph); /// /// assert_eq!(input_graph.g.node_count(), 2); /// assert_eq!(input_graph.g.edge_count(), 1);