Add nodes from egui
This commit is contained in:
parent
974d21d167
commit
5bb5e09901
@ -20,8 +20,10 @@ struct BoardBuilderApp {
|
||||
texture: Option<TextureHandle>,
|
||||
image: Option<image::DynamicImage>,
|
||||
edit_mode: EditMode,
|
||||
create_node_dialog: CreateNodeDialog,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum EditMode {
|
||||
Nodes,
|
||||
Edges,
|
||||
@ -92,8 +94,35 @@ impl eframe::App for BoardBuilderApp {
|
||||
image.paint_at(ui, response.rect);
|
||||
let view = View(response.rect);
|
||||
self.draw_board(&painter, view);
|
||||
if let Some(pos) = response.interact_pointer_pos() {
|
||||
let btn = if response.clicked() {
|
||||
PointerButton::Primary
|
||||
} else if response.secondary_clicked() {
|
||||
PointerButton::Secondary
|
||||
} else {
|
||||
PointerButton::Middle
|
||||
};
|
||||
let (x, y) = view.to_coords(pos);
|
||||
self.dispatch_click(btn, x, y);
|
||||
}
|
||||
}
|
||||
});
|
||||
if self.create_node_dialog.open {
|
||||
Window::new("Create Node")
|
||||
.collapsible(false)
|
||||
.show(ctx, |ui| {
|
||||
ui.label("Name:");
|
||||
ui.text_edit_singleline(&mut self.create_node_dialog.name);
|
||||
if ui.button("Add").clicked() {
|
||||
self.create_node_dialog.close();
|
||||
self.board.add_node(
|
||||
self.create_node_dialog.x,
|
||||
self.create_node_dialog.y,
|
||||
self.create_node_dialog.name.clone(),
|
||||
)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,10 +171,41 @@ impl BoardBuilderApp {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dispatch_click(&mut self, btn: PointerButton, x: f32, y: f32) {
|
||||
use EditMode::*;
|
||||
use PointerButton::*;
|
||||
match (btn, self.edit_mode) {
|
||||
(Primary, Nodes) => self.create_node_dialog.show(x, y),
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct View(Rect);
|
||||
impl CoordTransformer<Pos2> for View {
|
||||
fn origin(&self) -> Pos2 { self.0.min }
|
||||
fn extremes(&self) -> Pos2 { self.0.max }
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct CreateNodeDialog {
|
||||
open: bool,
|
||||
name: String,
|
||||
x: f32,
|
||||
y: f32,
|
||||
}
|
||||
|
||||
impl CreateNodeDialog {
|
||||
fn show(&mut self, x: f32, y: f32) {
|
||||
self.open = true;
|
||||
self.x = x;
|
||||
self.y = y;
|
||||
self.name = String::new();
|
||||
}
|
||||
|
||||
fn close(&mut self) {
|
||||
self.open = false;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user