Better delete

i think
This commit is contained in:
Breval Ferrari 2025-10-13 13:02:55 +02:00
parent f3258682a1
commit c7c77058fe
Signed by: breval
GPG key ID: 8ACFA4FD718E479F

View file

@ -1,22 +1,15 @@
use eframe::{App, CreationContext, NativeOptions, run_native}; use eframe::{App, NativeOptions, run_native};
use egui::{Context, Key}; use egui::{Context, Key};
use egui_graphs::{DefaultGraphView, Graph, SettingsInteraction}; use egui_graphs::{DefaultGraphView, Graph, SettingsInteraction};
use petgraph::prelude::{EdgeIndex, NodeIndex, StableGraph}; use petgraph::prelude::{EdgeIndex, NodeIndex};
pub struct BrainWave { mod r#static;
g: Graph, pub use r#static::BrainWave;
}
impl BrainWave {
fn new(_: &CreationContext<'_>) -> Self {
let g = generate_graph();
Self { g: Graph::from(&g) }
}
}
impl App for BrainWave { 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| {
// graph viewer
ui.add( ui.add(
&mut DefaultGraphView::new(&mut self.g).with_interactions( &mut DefaultGraphView::new(&mut self.g).with_interactions(
&SettingsInteraction::new() &SettingsInteraction::new()
@ -27,7 +20,9 @@ impl App for BrainWave {
.with_edge_selection_multi_enabled(true), .with_edge_selection_multi_enabled(true),
), ),
); );
// input event handler
ui.input(|input| { ui.input(|input| {
// delete
if input.key_pressed(Key::Delete) { if input.key_pressed(Key::Delete) {
handle_graph_delete(&mut self.g); handle_graph_delete(&mut self.g);
} }
@ -36,54 +31,39 @@ impl App for BrainWave {
} }
} }
fn generate_graph() -> StableGraph<(), ()> {
let mut g = StableGraph::new();
let a = g.add_node(());
let b = g.add_node(());
let c = g.add_node(());
g.add_edge(a, b, ());
g.add_edge(b, c, ());
g
}
fn handle_graph_delete(graph: &mut Graph) { fn handle_graph_delete(graph: &mut Graph) {
enum EdgeOrNode { enum EdgeOrNodeIndex {
Edge(EdgeIndex), Edge(EdgeIndex),
Node(NodeIndex), Node(NodeIndex),
} }
let mut to_remove = Vec::new(); graph
{ .selected_nodes()
let snodes = graph.selected_nodes(); .iter()
let sedges = graph.selected_edges(); .copied()
if snodes.len() + sedges.len() != 0 { .map(EdgeOrNodeIndex::Node)
for node in snodes { .chain(
to_remove.push(EdgeOrNode::Node(*node)); graph
} .selected_edges()
for edge in sedges { .iter()
to_remove.push(EdgeOrNode::Edge(*edge)); .copied()
} .map(EdgeOrNodeIndex::Edge),
} )
} .collect::<Vec<EdgeOrNodeIndex>>()
for e in to_remove { .into_iter()
match e { .for_each(|e| match e {
EdgeOrNode::Edge(edge_index) => { EdgeOrNodeIndex::Node(node) => {
graph.remove_edge(edge_index); graph.remove_node(node);
}
EdgeOrNode::Node(node_index) => {
graph.remove_node(node_index);
}
} }
EdgeOrNodeIndex::Edge(edge) => {
graph.remove_edge(edge);
} }
})
} }
fn main() { fn main() -> eframe::Result {
run_native( run_native(
"basic", "basic",
NativeOptions::default(), NativeOptions::default(),
Box::new(|cc| Ok(Box::new(BrainWave::new(cc)))), Box::new(|cc| Ok(Box::new(BrainWave::new(cc)))),
) )
.unwrap();
} }