Skip to content

Commit

Permalink
poc persisted layout
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzarx1 committed Oct 6, 2024
1 parent 8d8af7a commit 15c4285
Show file tree
Hide file tree
Showing 26 changed files with 293 additions and 212 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rand = "0.8"
petgraph = { version = "0.6", default-features = false, features = [
"stable_graph",
"matrix_graph",
"serde-1",
] }
serde = { version = "1.0", features = ["derive"] }

Expand Down
6 changes: 3 additions & 3 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::{Graph, GraphView};
use petgraph::stable_graph::StableGraph;
use egui_graphs::{DefaultEdgeShape, DefaultGraphView, DefaultNodeShape, Graph, GraphView};
use petgraph::{csr::DefaultIx, stable_graph::StableGraph, Directed};

pub struct BasicApp {
g: Graph,
Expand All @@ -17,7 +17,7 @@ impl BasicApp {
impl App for BasicApp {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(&mut GraphView::new(&mut self.g));
ui.add(&mut DefaultGraphView::new(&mut self.g));
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions examples/basic_custom/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::{run_native, App, CreationContext, NativeOptions};
use egui::{Context, Pos2};
use egui_graphs::{add_edge, add_node_custom, Graph, GraphView};
use egui_graphs::{add_edge, add_node_custom, DefaultGraphView, Graph, SettingsStyle};
use petgraph::stable_graph::StableGraph;

pub struct BasicCustomApp {
Expand All @@ -16,6 +16,7 @@ impl BasicCustomApp {
for position in positions {
idxs.push(add_node_custom(&mut g, &(), |g_node| {
g_node.set_location(position);
g_node.set_label(position.to_string());
}));
}

Expand All @@ -30,7 +31,10 @@ impl BasicCustomApp {
impl App for BasicCustomApp {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(&mut GraphView::new(&mut self.g));
ui.add(
&mut DefaultGraphView::new(&mut self.g)
.with_styles(&SettingsStyle::default().with_labels_always(true)),
);
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use drawers::ValuesSectionDebug;
use eframe::{run_native, App, CreationContext};
use egui::{CollapsingHeader, Context, Pos2, ScrollArea, Ui, Vec2};
use egui_graphs::events::Event;
use egui_graphs::{to_graph, Edge, Graph, GraphView, Node};
use egui_graphs::{to_graph, DefaultGraphView, Edge, Graph, Node};
use fdg::fruchterman_reingold::{FruchtermanReingold, FruchtermanReingoldConfiguration};
use fdg::nalgebra::{Const, OPoint};
use fdg::{Force, ForceGraph};
Expand Down Expand Up @@ -511,7 +511,7 @@ impl App for DemoApp {
let settings_style = &egui_graphs::SettingsStyle::new()
.with_labels_always(self.settings_style.labels_always);
ui.add(
&mut GraphView::new(&mut self.g)
&mut DefaultGraphView::new(&mut self.g)
.with_interactions(settings_interaction)
.with_navigations(settings_navigation)
.with_styles(settings_style)
Expand Down
2 changes: 1 addition & 1 deletion examples/flex_nodes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl FlexNodesApp {
}
});
CentralPanel::default().show(ctx, |ui| {
let widget = &mut GraphView::new(&mut self.g)
let widget = &mut GraphView::<_, _, _, _, _, _>::new(&mut self.g)
.with_interactions(
&SettingsInteraction::default()
.with_dragging_enabled(true)
Expand Down
4 changes: 2 additions & 2 deletions examples/interactive/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::{run_native, App, CreationContext};
use egui::Context;
use egui_graphs::{layouts, Graph, GraphView, SettingsInteraction, SettingsStyle};
use egui_graphs::{DefaultGraphView, Graph, SettingsInteraction, SettingsStyle};
use petgraph::stable_graph::StableGraph;

pub struct InteractiveApp {
Expand All @@ -27,7 +27,7 @@ impl App for InteractiveApp {
.with_edge_selection_multi_enabled(true);
let style_settings = &SettingsStyle::new().with_labels_always(true);
ui.add(
&mut GraphView::<_, _, _, _, _, _, layouts::Default>::new(&mut self.g)
&mut DefaultGraphView::new(&mut self.g)
.with_styles(style_settings)
.with_interactions(interaction_settings),
);
Expand Down
4 changes: 2 additions & 2 deletions examples/label_change/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::{run_native, App, CreationContext};
use egui::{CentralPanel, Context, SidePanel, TextEdit};
use egui_graphs::{layouts, Graph, GraphView, SettingsInteraction, SettingsStyle};
use egui_graphs::{DefaultGraphView, Graph, SettingsInteraction, SettingsStyle};
use petgraph::stable_graph::{EdgeIndex, NodeIndex, StableGraph};

pub struct LabelChangeApp {
Expand Down Expand Up @@ -52,7 +52,7 @@ impl LabelChangeApp {
}
});
CentralPanel::default().show(ctx, |ui| {
let widget = &mut GraphView::<_, _, _, _, _, _, layouts::Default>::new(&mut self.g)
let widget = &mut DefaultGraphView::new(&mut self.g)
.with_interactions(
&SettingsInteraction::default()
.with_node_selection_enabled(true)
Expand Down
8 changes: 4 additions & 4 deletions examples/multiple/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::{run_native, App, CreationContext, Frame};
use egui::{CentralPanel, Context, Layout, SidePanel};
use egui_graphs::{Graph, GraphView, SettingsInteraction, SettingsNavigation};
use egui_graphs::{DefaultGraphView, Graph, SettingsInteraction, SettingsNavigation};
use petgraph::stable_graph::StableGraph;

pub struct BasicApp {
Expand All @@ -23,7 +23,7 @@ impl App for BasicApp {
.show(ctx, |ui| {
ui.allocate_ui_with_layout(ui.max_rect().size(), Layout::default(), |ui| {
ui.add(
&mut GraphView::new(&mut self.g)
&mut DefaultGraphView::new(&mut self.g)
.with_navigations(
&SettingsNavigation::default()
.with_fit_to_screen_enabled(false)
Expand All @@ -45,7 +45,7 @@ impl App for BasicApp {
.resizable(true)
.show(ctx, |ui| {
ui.add(
&mut GraphView::new(&mut self.g)
&mut DefaultGraphView::new(&mut self.g)
.with_navigations(
&SettingsNavigation::default()
.with_fit_to_screen_enabled(false)
Expand All @@ -63,7 +63,7 @@ impl App for BasicApp {
});
CentralPanel::default().show(ctx, |ui| {
ui.add(
&mut GraphView::new(&mut self.g)
&mut DefaultGraphView::new(&mut self.g)
.with_navigations(
&SettingsNavigation::default()
.with_fit_to_screen_enabled(false)
Expand Down
13 changes: 11 additions & 2 deletions examples/undirected/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eframe::{run_native, App, CreationContext};
use egui::Context;
use egui_graphs::{Graph, GraphView};
use egui_graphs::{Graph, GraphView, LayoutRandom, LayoutStateRandom};
use petgraph::{
stable_graph::{StableGraph, StableUnGraph},
Undirected,
Expand All @@ -20,7 +20,16 @@ impl UndirectedApp {
impl App for UndirectedApp {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(&mut GraphView::new(&mut self.g));
ui.add(&mut GraphView::<
_,
_,
_,
_,
_,
_,
LayoutStateRandom,
LayoutRandom,
>::new(&mut self.g));
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/window/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use eframe::{run_native, App, CreationContext};
use egui::{Context, Window};
use egui_graphs::{Graph, GraphView};
use egui_graphs::{DefaultGraphView, Graph};
use petgraph::stable_graph::StableGraph;

pub struct WindowApp {
g: Graph<(), ()>,
g: Graph,
}

impl WindowApp {
Expand All @@ -17,7 +17,7 @@ impl WindowApp {
impl App for WindowApp {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
Window::new("graph").show(ctx, |ui| {
ui.add(&mut GraphView::new(&mut self.g));
ui.add(&mut DefaultGraphView::new(&mut self.g));
});
}
}
Expand Down
35 changes: 21 additions & 14 deletions src/draw/drawer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::marker::PhantomData;

use egui::{Context, Painter, Shape};
use egui::{util::id_type_map::SerializableAny, Context, Painter, Shape};
use petgraph::graph::IndexType;
use petgraph::EdgeType;

use crate::{layouts::Layout, settings::SettingsStyle, Graph, Metadata};
use crate::{
layouts::{Layout, LayoutState},
settings::SettingsStyle,
Graph, Metadata,
};

use super::{DisplayEdge, DisplayNode};

Expand All @@ -17,37 +21,40 @@ pub struct DrawContext<'a> {
pub meta: &'a Metadata,
}

pub struct Drawer<'a, N, E, Ty, Ix, Nd, Ed, L>
pub struct Drawer<'a, N, E, Ty, Ix, Nd, Ed, S, L>
where
N: Clone,
E: Clone,
Ty: EdgeType,
Ix: IndexType,
Nd: DisplayNode<N, E, Ty, Ix>,
Ed: DisplayEdge<N, E, Ty, Ix, Nd>,
L: Layout,
S: LayoutState,
L: Layout<S>,
{
ctx: &'a DrawContext<'a>,
g: &'a mut Graph<N, E, Ty, Ix, Nd, Ed, L>,
postponed: Vec<Shape>,
_marker: PhantomData<(Nd, Ed, L)>,
g: &'a mut Graph<N, E, Ty, Ix, Nd, Ed>,
delayed: Vec<Shape>,

_marker: PhantomData<(Nd, Ed, L, S)>,
}

impl<'a, N, E, Ty, Ix, Nd, Ed, L> Drawer<'a, N, E, Ty, Ix, Nd, Ed, L>
impl<'a, N, E, Ty, Ix, Nd, Ed, S, L> Drawer<'a, N, E, Ty, Ix, Nd, Ed, S, L>
where
N: Clone,
E: Clone,
Ty: EdgeType,
Ix: IndexType,
Nd: DisplayNode<N, E, Ty, Ix>,
Ed: DisplayEdge<N, E, Ty, Ix, Nd>,
L: Layout,
S: LayoutState,
L: Layout<S>,
{
pub fn new(g: &'a mut Graph<N, E, Ty, Ix, Nd, Ed, L>, ctx: &'a DrawContext<'a>) -> Self {
pub fn new(g: &'a mut Graph<N, E, Ty, Ix, Nd, Ed>, ctx: &'a DrawContext<'a>) -> Self {
Drawer {
ctx,
g,
postponed: Vec::new(),
delayed: Vec::new(),
_marker: PhantomData,
}
}
Expand All @@ -59,7 +66,7 @@ where
}

fn draw_postponed(&mut self) {
self.postponed.iter().for_each(|s| {
self.delayed.iter().for_each(|s| {
self.ctx.painter.add(s.clone());
});
}
Expand All @@ -80,7 +87,7 @@ where

if n.selected() || n.dragged() {
for s in shapes {
self.postponed.push(s);
self.delayed.push(s);
}
} else {
for s in shapes {
Expand Down Expand Up @@ -112,7 +119,7 @@ where

if e.selected() {
for s in shapes {
self.postponed.push(s);
self.delayed.push(s);
}
} else {
for s in shapes {
Expand Down
7 changes: 3 additions & 4 deletions src/elements/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use petgraph::{
stable_graph::{DefaultIx, EdgeIndex, IndexType},
Directed, EdgeType,
};
use serde::{Deserialize, Serialize};

use crate::{DefaultEdgeShape, DefaultNodeShape, DisplayEdge, DisplayNode};

/// Stores properties of an [Edge]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct EdgeProps<E: Clone> {
pub payload: E,
pub order: usize,
Expand All @@ -18,8 +18,7 @@ pub struct EdgeProps<E: Clone> {
}

/// Stores properties of an edge that can be changed. Used to apply changes to the graph.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Edge<
N: Clone,
E: Clone,
Expand Down
6 changes: 3 additions & 3 deletions src/elements/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use petgraph::{
stable_graph::{DefaultIx, IndexType, NodeIndex},
Directed, EdgeType,
};
use serde::{Deserialize, Serialize};

use crate::{DefaultNodeShape, DisplayNode};

/// Stores properties of a [Node]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct NodeProps<N: Clone> {
pub payload: N,
pub location: Pos2,
Expand All @@ -20,7 +20,7 @@ pub struct NodeProps<N: Clone> {
pub dragged: bool,
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Serialize, Deserialize)]
pub struct Node<N, E, Ty = Directed, Ix = DefaultIx, D = DefaultNodeShape>
where
N: Clone,
Expand Down
Loading

0 comments on commit 15c4285

Please sign in to comment.