diff --git a/board-builder-impl-egui/src/main.rs b/board-builder-impl-egui/src/main.rs index 0673a84..7b7e70c 100644 --- a/board-builder-impl-egui/src/main.rs +++ b/board-builder-impl-egui/src/main.rs @@ -115,23 +115,8 @@ impl eframe::App for BoardBuilderApp { } } }); - if self.create_node_dialog.open { - Window::new("Create Node") - .collapsible(false) - .show(ctx, |ui| { - let mut board = self.board.borrow_mut(); - ui.label("Name:"); - ui.text_edit_singleline(&mut self.create_node_dialog.name); - if ui.button("Add").clicked() { - self.create_node_dialog.close(); - board.add_node( - self.create_node_dialog.x, - self.create_node_dialog.y, - self.create_node_dialog.name.clone(), - ) - } - }); - } + + self.create_node_dialog.ui(ctx); self.edit_node_dialog.ui(ctx); self.edit_labels_dialog.ui(ctx); } @@ -194,7 +179,10 @@ impl BoardBuilderApp { use EditMode::*; use PointerButton::*; match (btn, self.edit_mode) { - (Primary, Nodes) => self.create_node_dialog.show(x, y), + (Primary, Nodes) => { + let board = Rc::clone(&self.board); + self.create_node_dialog.show(x, y, board); + } (Primary, Edges) => self.select_edge(x, y), (Secondary, Nodes) => { let board = Rc::clone(&self.board); @@ -228,21 +216,37 @@ impl CoordTransformer for View { #[derive(Default)] struct CreateNodeDialog { open: bool, - name: String, - x: f32, - y: f32, + node: Node, + board: Rc>, } impl CreateNodeDialog { - fn show(&mut self, x: f32, y: f32) { + fn show(&mut self, x: f32, y: f32, board: Rc>) { self.open = true; - self.x = x; - self.y = y; - self.name = String::new(); + self.board = board; + + self.node = Node { + x, + y, + ..Node::default() + } } - fn close(&mut self) { - self.open = false; + fn ui(&mut self, ctx: &Context) { + if !self.open { return } + Window::new("Create Node") + .collapsible(false) + .show(ctx, |ui| { + let mut board = self.board.borrow_mut(); + node_common_ui(ui, &mut self.node, &board); + if ui.button("Ok").clicked() { + board.insert_node(self.node.clone()); + self.open = false; + } + if ui.button("Cancel").clicked() { + self.open = false; + } + }); } } @@ -268,19 +272,7 @@ impl EditNodeDialog { .collapsible(false) .show(ctx, |ui| { let mut board = self.board.borrow_mut(); - ui.text_edit_singleline(&mut self.node.name); - for (key, choices) in &board.labels { - let choices = choices.clone(); - let default = choices.iter().next().unwrap().clone(); - let current = self.node.labels.entry(key.clone()).or_insert(default); - ComboBox::from_label(key) - .selected_text(current.to_string()) - .show_ui(ui, |ui| { - for choice in choices { - ui.selectable_value(current, choice.clone(), choice); - } - }); - } + node_common_ui(ui, &mut self.node, &board); if ui.button("Ok").clicked() { board.nodes.insert(self.id, self.node.clone()); self.open = false; @@ -412,3 +404,19 @@ impl StringDialog { } } } + +fn node_common_ui(ui: &mut Ui, node: &mut Node, board: &Board) { + ui.text_edit_singleline(&mut node.name); + for (key, choices) in &board.labels { + let choices = choices.clone(); + let default = choices.iter().next().unwrap().clone(); + let current = node.labels.entry(key.clone()).or_insert(default); + ComboBox::from_label(key) + .selected_text(current.to_string()) + .show_ui(ui, |ui| { + for choice in choices { + ui.selectable_value(current, choice.clone(), choice); + } + }); + } +} diff --git a/src/lib.rs b/src/lib.rs index dadb801..9442bcf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,6 +34,12 @@ impl Board { }); } + pub fn insert_node(&mut self, node: Node) -> usize { + let id = self.next_id(); + self.nodes.insert(id, node); + id + } + pub fn remove_node(&mut self, id: usize) { // We remove this node from the graph, then drop it from each // other nodes edge.