-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.rs
154 lines (148 loc) · 4.75 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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..=250))
.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..=250))
.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();
}