Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored graph state, removed computed #144

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"

[dependencies]
egui_graphs = { path = "../../" }
egui = "0.23"
eframe = "0.23"
egui = "0.23.0"
eframe = "0.23.0"
petgraph = "0.6"
1 change: 0 additions & 1 deletion examples/configurable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
egui_graphs = { path = "../../", features = ["events"] }
egui = "0.23"
egui_plot = "0.23"
serde_json = "1.0"
eframe = "0.23"
petgraph = "0.6"
Expand Down
25 changes: 4 additions & 21 deletions examples/configurable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pub struct ConfigurableApp {
settings_navigation: SettingsNavigation,
settings_style: SettingsStyle,

selected_nodes: Vec<NodeIndex>,
selected_edges: Vec<EdgeIndex>,
last_events: Vec<String>,

simulation_stopped: bool,
Expand Down Expand Up @@ -62,8 +60,6 @@ impl ConfigurableApp {
settings_navigation: SettingsNavigation::default(),
settings_style: SettingsStyle::default(),

selected_nodes: Vec::default(),
selected_edges: Vec::default(),
last_events: Vec::default(),

simulation_stopped: false,
Expand Down Expand Up @@ -127,27 +123,13 @@ impl ConfigurableApp {
///
/// If node or edge is selected it is added to the corresponding selected field in `self`.
fn sync_graph_with_simulation(&mut self) {
self.selected_nodes = vec![];
self.selected_edges = vec![];

let g_indices = self.g.g.node_indices().collect::<Vec<_>>();
for g_n_idx in &g_indices {
let g_n = self.g.g.node_weight_mut(*g_n_idx).unwrap();
let sim_n = self.sim.get_graph_mut().node_weight_mut(*g_n_idx).unwrap();

let loc = sim_n.location;
g_n.set_location(Pos2::new(loc.x, loc.y));

if g_n.selected() {
self.selected_nodes.push(*g_n_idx);
}
}

for g_e_idx in self.g.g.edge_indices() {
let g_e = self.g.g.edge_weight(g_e_idx).unwrap();
if g_e.selected() {
self.selected_edges.push(g_e_idx);
}
}

// reset the weights of the edges
Expand Down Expand Up @@ -261,7 +243,8 @@ impl ConfigurableApp {
self.g.g[idx].bind(idx, location);

let n = self.g.g.node_weight_mut(idx).unwrap();
n.set_label(format!("{idx:?}"));
let idx_string = idx.index().to_string();
n.set_label(idx_string);

let mut sim_node = fdg_sim::Node::new(idx.index().to_string().as_str(), ());
sim_node.location = Vec3::new(location.x, location.y, 0.);
Expand Down Expand Up @@ -496,10 +479,10 @@ impl ConfigurableApp {

CollapsingHeader::new("Selected").default_open(true).show(ui, |ui| {
ScrollArea::vertical().auto_shrink([false, true]).max_height(200.).show(ui, |ui| {
self.selected_nodes.iter().for_each(|node| {
self.g.selected_nodes().iter().for_each(|node| {
ui.label(format!("{node:?}"));
});
self.selected_edges.iter().for_each(|edge| {
self.g.selected_edges().iter().for_each(|edge| {
ui.label(format!("{edge:?}"));
});
});
Expand Down
7 changes: 2 additions & 5 deletions examples/interactive/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ use egui::Context;
use egui_graphs::{
DefaultEdgeShape, DefaultNodeShape, Graph, GraphView, SettingsInteraction, SettingsStyle,
};
use petgraph::{
stable_graph::{DefaultIx, StableGraph},
Directed,
};
use petgraph::stable_graph::StableGraph;

pub struct InteractiveApp {
g: Graph<(), (), Directed, DefaultIx>,
g: Graph<(), ()>,
}

impl InteractiveApp {
Expand Down
63 changes: 0 additions & 63 deletions src/computed.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/elements/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::marker::PhantomData;

use petgraph::{
stable_graph::{DefaultIx, EdgeIndex, IndexType},
EdgeType,
Directed, EdgeType,
};

use crate::{DefaultEdgeShape, DefaultNodeShape, DisplayEdge, DisplayNode};
Expand All @@ -22,7 +22,7 @@ pub struct EdgeProps<E: Clone> {
pub struct Edge<
N: Clone,
E: Clone,
Ty: EdgeType,
Ty: EdgeType = Directed,
Ix: IndexType = DefaultIx,
Dn: DisplayNode<N, E, Ty, Ix> = DefaultNodeShape,
D: DisplayEdge<N, E, Ty, Ix, Dn> = DefaultEdgeShape,
Expand Down
4 changes: 2 additions & 2 deletions src/elements/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::marker::PhantomData;
use egui::Pos2;
use petgraph::{
stable_graph::{DefaultIx, IndexType, NodeIndex},
EdgeType,
EdgeType, Directed,
};

use crate::{DefaultNodeShape, DisplayNode};
Expand All @@ -21,7 +21,7 @@ pub struct NodeProps<N: Clone> {
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Node<N, E, Ty, Ix = DefaultIx, D = DefaultNodeShape>
pub struct Node<N, E, Ty = Directed, Ix = DefaultIx, D = DefaultNodeShape>
where
N: Clone,
E: Clone,
Expand Down
37 changes: 35 additions & 2 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use crate::{DefaultEdgeShape, DefaultNodeShape};
type StableGraphType<N, E, Ty, Ix, Dn, De> =
StableGraph<Node<N, E, Ty, Ix, Dn>, Edge<N, E, Ty, Ix, Dn, De>, Ty, Ix>;

/// Graph type compatible with [`super::GraphView`].
/// Wrapper around [`petgraph::stable_graph::StableGraph`] compatible with [`super::GraphView`].
/// It is used to store graph data and provide access to it.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct Graph<
Expand All @@ -28,6 +29,9 @@ pub struct Graph<
De: DisplayEdge<N, E, Ty, Ix, Dn> = DefaultEdgeShape,
> {
pub g: StableGraphType<N, E, Ty, Ix, Dn, De>,
selected_nodes: Vec<NodeIndex<Ix>>,
selected_edges: Vec<EdgeIndex<Ix>>,
dragged_node: Option<NodeIndex<Ix>>,
}

impl<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode<N, E, Ty, Ix>>
Expand All @@ -48,7 +52,12 @@ impl<
> Graph<N, E, Ty, Ix, Dn, De>
{
pub fn new(g: StableGraphType<N, E, Ty, Ix, Dn, De>) -> Self {
Self { g }
Self {
g,
selected_nodes: Vec::default(),
selected_edges: Vec::default(),
dragged_node: Option::default(),
}
}

/// Finds node by position. Can be optimized by using a spatial index like quad-tree if needed.
Expand Down Expand Up @@ -127,4 +136,28 @@ impl<
) -> impl Iterator<Item = EdgeReference<Edge<N, E, Ty, Ix, Dn, De>, Ix>> {
self.g.edges_directed(idx, dir)
}

pub fn selected_nodes(&self) -> &[NodeIndex<Ix>] {
&self.selected_nodes
}

pub fn set_selected_nodes(&mut self, nodes: Vec<NodeIndex<Ix>>) {
self.selected_nodes = nodes;
}

pub fn selected_edges(&self) -> &[EdgeIndex<Ix>] {
&self.selected_edges
}

pub fn set_selected_edges(&mut self, edges: Vec<EdgeIndex<Ix>>) {
self.selected_edges = edges;
}

pub fn dragged_node(&self) -> Option<NodeIndex<Ix>> {
self.dragged_node
}

pub fn set_dragged_node(&mut self, node: Option<NodeIndex<Ix>>) {
self.dragged_node = node;
}
}
Loading