delete nodes & edges

This commit is contained in:
Breval Ferrari 2025-10-13 12:35:45 +02:00
parent 98f2698ee6
commit f3258682a1
Signed by: breval
GPG key ID: 8ACFA4FD718E479F

View file

@ -1,23 +1,37 @@
use eframe::{App, CreationContext, NativeOptions, run_native}; use eframe::{App, CreationContext, NativeOptions, run_native};
use egui::Context; use egui::{Context, Key};
use egui_graphs::{DefaultGraphView, Graph}; use egui_graphs::{DefaultGraphView, Graph, SettingsInteraction};
use petgraph::stable_graph::StableGraph; use petgraph::prelude::{EdgeIndex, NodeIndex, StableGraph};
pub struct BasicApp { pub struct BrainWave {
g: Graph, g: Graph,
} }
impl BasicApp { impl BrainWave {
fn new(_: &CreationContext<'_>) -> Self { fn new(_: &CreationContext<'_>) -> Self {
let g = generate_graph(); let g = generate_graph();
Self { g: Graph::from(&g) } Self { g: Graph::from(&g) }
} }
} }
impl App for BasicApp { impl App for BrainWave {
fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) { fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.add(&mut DefaultGraphView::new(&mut self.g)); ui.add(
&mut DefaultGraphView::new(&mut self.g).with_interactions(
&SettingsInteraction::new()
.with_dragging_enabled(true)
.with_node_selection_enabled(true)
.with_edge_selection_enabled(true)
.with_node_selection_multi_enabled(true)
.with_edge_selection_multi_enabled(true),
),
);
ui.input(|input| {
if input.key_pressed(Key::Delete) {
handle_graph_delete(&mut self.g);
}
})
}); });
} }
} }
@ -31,16 +45,45 @@ fn generate_graph() -> StableGraph<(), ()> {
g.add_edge(a, b, ()); g.add_edge(a, b, ());
g.add_edge(b, c, ()); g.add_edge(b, c, ());
g.add_edge(c, a, ());
g g
} }
fn handle_graph_delete(graph: &mut Graph) {
enum EdgeOrNode {
Edge(EdgeIndex),
Node(NodeIndex),
}
let mut to_remove = Vec::new();
{
let snodes = graph.selected_nodes();
let sedges = graph.selected_edges();
if snodes.len() + sedges.len() != 0 {
for node in snodes {
to_remove.push(EdgeOrNode::Node(*node));
}
for edge in sedges {
to_remove.push(EdgeOrNode::Edge(*edge));
}
}
}
for e in to_remove {
match e {
EdgeOrNode::Edge(edge_index) => {
graph.remove_edge(edge_index);
}
EdgeOrNode::Node(node_index) => {
graph.remove_node(node_index);
}
}
}
}
fn main() { fn main() {
run_native( run_native(
"basic", "basic",
NativeOptions::default(), NativeOptions::default(),
Box::new(|cc| Ok(Box::new(BasicApp::new(cc)))), Box::new(|cc| Ok(Box::new(BrainWave::new(cc)))),
) )
.unwrap(); .unwrap();
} }