Skip to content

Commit

Permalink
layout example
Browse files Browse the repository at this point in the history
  • Loading branch information
blitzarx1 committed Oct 11, 2024
1 parent c80e052 commit b6fe596
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use egui_graphs::{random_graph, DefaultGraphView, Edge, Graph, Node};
use fdg::fruchterman_reingold::{FruchtermanReingold, FruchtermanReingoldConfiguration};
use fdg::nalgebra::{Const, OPoint};
use fdg::{Force, ForceGraph};
use petgraph::stable_graph::{DefaultIx, EdgeIndex, NodeIndex, StableGraph};
use petgraph::stable_graph::{DefaultIx, EdgeIndex, NodeIndex};
use petgraph::Directed;
use rand::Rng;

Expand Down
12 changes: 12 additions & 0 deletions examples/layouts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "layouts"
version = "0.1.0"
authors = ["Dmitrii Samsonov <[email protected]>"]
license = "MIT"
edition = "2021"

[dependencies]
egui_graphs = { path = "../../" }
egui = "0.29"
eframe = "0.29"
petgraph = "0.6"
7 changes: 7 additions & 0 deletions examples/layouts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Layouts
Example of usage of different lyouts implemented by `egui_graphs`.

## run
```bash
cargo run --release -p layouts
```
154 changes: 154 additions & 0 deletions examples/layouts/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use eframe::{run_native, App, CreationContext, NativeOptions};
use egui::Context;
use egui_graphs::{
random_graph, DefaultEdgeShape, DefaultNodeShape, Graph, GraphView, LayoutHierarchical,
LayoutRandom, LayoutStateHierarchical, LayoutStateRandom,
};
use petgraph::{stable_graph::DefaultIx, Directed};

#[derive(Clone, PartialEq)]
enum Layout {
Hierarchical,
Random,
}

#[derive(Clone)]
struct Settings {
layout: Layout,
num_nodes: usize,
num_edges: usize,
}
pub struct LayoutsApp {
settings: Settings,
g: Graph,
}

impl LayoutsApp {
fn new(_: &CreationContext<'_>) -> Self {
let settings = Settings {
layout: Layout::Hierarchical,
num_nodes: 25,
num_edges: 25,
};
Self {
settings: settings.clone(),
g: random_graph(settings.num_nodes, settings.num_edges),
}
}

fn clear_cache(&mut self, ui: &mut egui::Ui) {
match self.settings.layout {
Layout::Hierarchical => {
GraphView::<
(),
(),
Directed,
DefaultIx,
DefaultNodeShape,
DefaultEdgeShape,
LayoutStateHierarchical,
LayoutHierarchical,
>::clear_cache(ui);
}
Layout::Random => {
GraphView::<
(),
(),
Directed,
DefaultIx,
DefaultNodeShape,
DefaultEdgeShape,
LayoutStateRandom,
LayoutRandom,
>::clear_cache(ui);
}
};
}
}

impl App for LayoutsApp {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
egui::SidePanel::right("right_panel")
.min_width(250.)
.show(ctx, |ui| {
ui.vertical(|ui| {
ui.horizontal(|ui| {
ui.label("Layout");
if ui
.radio_value(
&mut self.settings.layout,
Layout::Hierarchical,
"Hierarchical",
)
.changed()
{
self.clear_cache(ui);
};
if ui
.radio_value(&mut self.settings.layout, Layout::Random, "Random")
.changed()
{
self.clear_cache(ui);
};
});
ui.horizontal(|ui| {
ui.label("Number of nodes");
if ui
.add(egui::Slider::new(&mut self.settings.num_nodes, 1..=100))
.changed()
{
self.clear_cache(ui);
self.g = random_graph(self.settings.num_nodes, self.settings.num_edges);
};
});
ui.horizontal(|ui| {
ui.label("Number of edges");
if ui
.add(egui::Slider::new(&mut self.settings.num_edges, 1..=100))
.changed()
{
self.clear_cache(ui);
self.g = random_graph(self.settings.num_nodes, self.settings.num_edges);
};
});
});
});
egui::CentralPanel::default().show(ctx, |ui| {
match self.settings.layout {
Layout::Hierarchical => {
ui.add(&mut GraphView::<
_,
_,
_,
_,
_,
_,
LayoutStateHierarchical,
LayoutHierarchical,
>::new(&mut self.g));
}
Layout::Random => {
ui.add(&mut GraphView::<
_,
_,
_,
_,
_,
_,
LayoutStateRandom,
LayoutRandom,
>::new(&mut self.g));
}
};
});
}
}

fn main() {
run_native(
"egui_graphs_layouts_demo",
NativeOptions::default(),
Box::new(|cc| Ok(Box::new(LayoutsApp::new(cc)))),
)
.unwrap();
}
13 changes: 13 additions & 0 deletions src/graph_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,24 @@ where
self
}

/// Clears cached values of layout and metadata.
pub fn clear_cache(ui: &mut Ui) {
GraphView::<N, E, Ty, Ix, Dn, De, S, L>::reset_metadata(ui);
GraphView::<N, E, Ty, Ix, Dn, De, S, L>::reset_layout(ui);
}

/// Resets navigation metadata
pub fn reset_metadata(ui: &mut Ui) {
Metadata::default().save(ui);
}

/// Resets layout state
pub fn reset_layout(ui: &mut Ui) {
ui.data_mut(|data| {
data.insert_persisted(Id::new(KEY_LAYOUT), S::default());
});
}

#[cfg(feature = "events")]
/// Allows to supply channel where events happening in the graph will be reported.
pub fn with_events(mut self, events_publisher: &'a Sender<Event>) -> Self {
Expand Down
10 changes: 5 additions & 5 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,17 @@ pub fn node_size<N: Clone, E: Clone, Ty: EdgeType, Ix: IndexType, D: DisplayNode
((connector_right.to_vec2() - connector_left.to_vec2()) / 2.).length()
}

pub fn random_graph(node_count: usize, edge_count: usize) -> Graph {
pub fn random_graph(num_nodes: usize, num_edges: usize) -> Graph {
let mut rng = rand::thread_rng();
let mut graph = StableGraph::new();

for _ in 0..node_count {
for _ in 0..num_nodes {
graph.add_node(());
}

for _ in 0..edge_count {
let source = rng.gen_range(0..node_count);
let target = rng.gen_range(0..node_count);
for _ in 0..num_edges {
let source = rng.gen_range(0..num_nodes);
let target = rng.gen_range(0..num_nodes);

graph.add_edge(NodeIndex::new(source), NodeIndex::new(target), ());
}
Expand Down

0 comments on commit b6fe596

Please sign in to comment.