diff --git a/src/main.rs b/src/main.rs index f8119a1..4eab1c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,37 @@ use eframe::{App, CreationContext, NativeOptions, run_native}; -use egui::Context; -use egui_graphs::{DefaultGraphView, Graph}; -use petgraph::stable_graph::StableGraph; +use egui::{Context, Key}; +use egui_graphs::{DefaultGraphView, Graph, SettingsInteraction}; +use petgraph::prelude::{EdgeIndex, NodeIndex, StableGraph}; -pub struct BasicApp { +pub struct BrainWave { g: Graph, } -impl BasicApp { +impl BrainWave { fn new(_: &CreationContext<'_>) -> Self { let g = generate_graph(); Self { g: Graph::from(&g) } } } -impl App for BasicApp { +impl App for BrainWave { fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) { 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(b, c, ()); - g.add_edge(c, a, ()); 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() { run_native( "basic", NativeOptions::default(), - Box::new(|cc| Ok(Box::new(BasicApp::new(cc)))), + Box::new(|cc| Ok(Box::new(BrainWave::new(cc)))), ) .unwrap(); }