Move common node creation/editing ui to own function, can edit whole node from creation menu

This commit is contained in:
Dane Johnson 2022-05-12 15:44:09 -05:00
parent 079c4c9004
commit 9f4520e2c5
2 changed files with 54 additions and 40 deletions

View File

@ -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,22 +216,38 @@ impl CoordTransformer<Pos2> for View {
#[derive(Default)]
struct CreateNodeDialog {
open: bool,
name: String,
x: f32,
y: f32,
node: Node,
board: Rc<RefCell<Board>>,
}
impl CreateNodeDialog {
fn show(&mut self, x: f32, y: f32) {
fn show(&mut self, x: f32, y: f32, board: Rc<RefCell<Board>>) {
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) {
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;
}
});
}
}
#[derive(Default)]
@ -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);
}
});
}
}

View File

@ -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.