Skip to content

Commit

Permalink
Added payload to props (#129)
Browse files Browse the repository at this point in the history
Added user's payload to `NodeProps` and `EdgeProps` for user to have
access to them in the drawing shapes implementation.
  • Loading branch information
blitzarx1 authored Nov 21, 2023
1 parent d242c42 commit afeffb0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
6 changes: 3 additions & 3 deletions examples/custom_draw/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct NodeShapeAnimated {
size: f32,
}

impl From<NodeProps> for NodeShapeAnimated {
fn from(node_props: NodeProps) -> Self {
impl<N: Clone> From<NodeProps<N>> for NodeShapeAnimated {
fn from(node_props: NodeProps<N>) -> Self {
Self {
label: node_props.label,
loc: node_props.location,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>
vec![shape_rect, shape_label.into()]
}

fn update(&mut self, state: &NodeProps) {
fn update(&mut self, state: &NodeProps<N>) {
self.label = state.label.clone();
self.loc = state.location;
self.dragged = state.dragged;
Expand Down
6 changes: 3 additions & 3 deletions src/draw/default_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub struct DefaultEdgeShape {
pub loop_size: f32,
}

impl From<EdgeProps> for DefaultEdgeShape {
fn from(edge: EdgeProps) -> Self {
impl<E: Clone> From<EdgeProps<E>> for DefaultEdgeShape {
fn from(edge: EdgeProps<E>) -> Self {
Self {
order: edge.order,
selected: edge.selected,
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, I
vec![line_curved.into(), line_curved_tip]
}

fn update(&mut self, state: &EdgeProps) {
fn update(&mut self, state: &EdgeProps<E>) {
self.order = state.order;
self.selected = state.selected;
}
Expand Down
6 changes: 3 additions & 3 deletions src/draw/default_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub struct DefaultNodeShape {
pub radius: f32,
}

impl From<NodeProps> for DefaultNodeShape {
fn from(node_props: NodeProps) -> Self {
impl<N: Clone> From<NodeProps<N>> for DefaultNodeShape {
fn from(node_props: NodeProps<N>) -> Self {
DefaultNodeShape {
pos: node_props.location,
selected: node_props.selected,
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>
res
}

fn update(&mut self, state: &NodeProps) {
fn update(&mut self, state: &NodeProps<N>) {
self.pos = state.location;
self.pos = state.location;
self.selected = state.selected;
Expand Down
8 changes: 4 additions & 4 deletions src/draw/displays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use petgraph::{stable_graph::IndexType, EdgeType};

use crate::{draw::drawer::DrawContext, elements::EdgeProps, Node, NodeProps};

pub trait DisplayNode<N, E, Ty, Ix>: Clone + From<NodeProps>
pub trait DisplayNode<N, E, Ty, Ix>: Clone + From<NodeProps<N>>
where
N: Clone,
E: Clone,
Expand All @@ -27,7 +27,7 @@ where
fn shapes(&mut self, ctx: &DrawContext) -> Vec<Shape>;

/// 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<N>);

/// Checks if the provided `pos` is inside the shape.
///
Expand All @@ -37,7 +37,7 @@ where
fn is_inside(&self, pos: Pos2) -> bool;
}

pub trait DisplayEdge<N, E, Ty, Ix, D>: Clone + From<EdgeProps>
pub trait DisplayEdge<N, E, Ty, Ix, D>: Clone + From<EdgeProps<E>>
where
N: Clone,
E: Clone,
Expand All @@ -63,7 +63,7 @@ where
) -> Vec<Shape>;

/// 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<E>);

/// Checks if the provided `pos` is inside the shape.
///
Expand Down
25 changes: 14 additions & 11 deletions src/elements/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E: Clone> {
pub payload: E,
pub order: usize,
pub selected: bool,
}
Expand All @@ -28,11 +29,9 @@ pub struct Edge<
> {
id: Option<EdgeIndex<Ix>>,

/// Client data
payload: E,
display: D,

props: EdgeProps,
props: EdgeProps<E>,
_marker: PhantomData<(N, Ty, Dn)>,
}

Expand All @@ -46,11 +45,15 @@ impl<
> Edge<N, E, Ty, Ix, Dn, D>
{
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,

Expand All @@ -59,7 +62,7 @@ impl<
}
}

pub fn props(&self) -> &EdgeProps {
pub fn props(&self) -> &EdgeProps<E> {
&self.props
}

Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 16 additions & 14 deletions src/elements/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<N: Clone> {
pub payload: N,
pub location: Pos2,
pub label: String,
pub selected: bool,
Expand All @@ -29,9 +30,8 @@ where
D: DisplayNode<N, E, Ty, Ix>,
{
id: Option<NodeIndex<Ix>>,
payload: N,

props: NodeProps,
props: NodeProps<N>,
display: D,

_marker: PhantomData<(E, Ty)>,
Expand All @@ -46,10 +46,7 @@ where
D: DisplayNode<N, E, Ty, Ix>,
{
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()
}
}

Expand All @@ -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,
Expand All @@ -82,10 +78,16 @@ where
D: DisplayNode<N, E, Ty, Ix>,
{
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,

Expand All @@ -94,7 +96,7 @@ where
}
}

pub fn props(&self) -> &NodeProps {
pub fn props(&self) -> &NodeProps<N> {
&self.props
}

Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ 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();
/// let node1 = user_graph.add_node("A");
/// 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);
Expand Down

0 comments on commit afeffb0

Please sign in to comment.