Skip to content

Commit

Permalink
user location prioritization
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzarx1 committed Oct 6, 2024
1 parent 1c49928 commit 4c11040
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 27 deletions.
6 changes: 3 additions & 3 deletions examples/animated_nodes/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl NodeShapeAnimated {
impl<N: Clone + IsClockwise> From<NodeProps<N>> for NodeShapeAnimated {
fn from(node_props: NodeProps<N>) -> 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(),

Expand Down Expand Up @@ -145,7 +145,7 @@ impl<N: Clone + IsClockwise, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<

fn update(&mut self, state: &NodeProps<N>) {
self.label = state.label.clone();
self.loc = state.location;
self.loc = state.location();
self.dragged = state.dragged;
self.clockwise = state.payload.get_is_clockwise();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 3 additions & 3 deletions examples/flex_nodes/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub struct NodeShapeFlex {
impl<N: Clone> From<NodeProps<N>> for NodeShapeFlex {
fn from(node_props: NodeProps<N>) -> 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.,
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>

fn update(&mut self, state: &NodeProps<N>) {
self.label = state.label.clone();
self.loc = state.location;
self.loc = state.location();
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/wasm_custom_draw/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub struct NodeShapeAnimated {
impl<N: Clone> From<NodeProps<N>> for NodeShapeAnimated {
fn from(node_props: NodeProps<N>) -> 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(),
Expand Down Expand Up @@ -120,7 +120,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>

fn update(&mut self, state: &NodeProps<N>) {
self.label = state.label.clone();
self.loc = state.location;
self.loc = state.location();
self.dragged = state.dragged;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/draw/displays_default/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct DefaultNodeShape {
impl<N: Clone> From<NodeProps<N>> for DefaultNodeShape {
fn from(node_props: NodeProps<N>) -> 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(),
Expand Down Expand Up @@ -94,7 +94,7 @@ impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType> DisplayNode<N, E, Ty, Ix>
}

fn update(&mut self, state: &NodeProps<N>) {
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();
Expand Down
2 changes: 1 addition & 1 deletion src/draw/drawer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
25 changes: 22 additions & 3 deletions src/elements/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@ use crate::{DefaultNodeShape, DisplayNode};

/// Stores properties of a [Node]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NodeProps<N: Clone> {
pub struct NodeProps<N>
where
N: Clone,
{
pub payload: N,
pub location: Pos2,
pub label: String,
pub selected: bool,
pub dragged: bool,

location: Pos2,
location_user: Option<Pos2>,
}

impl<N> NodeProps<N>
where
N: Clone,
{
pub fn location(&self) -> Pos2 {
self.location_user.unwrap_or(self.location)
}
}

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 0 additions & 9 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ where

#[cfg(test)]
mod tests {
use crate::layouts;
use crate::DefaultEdgeShape;
use crate::DefaultNodeShape;

Expand All @@ -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());
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/random/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Layout<State> 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),
));
Expand Down

0 comments on commit 4c11040

Please sign in to comment.