Load and draw nodes in egui
This commit is contained in:
parent
e97598b69f
commit
fb1daa4912
@ -1,9 +1,11 @@
|
||||
use eframe::egui;
|
||||
use egui::*;
|
||||
|
||||
use image::DynamicImage;
|
||||
|
||||
use rfd::FileDialog;
|
||||
|
||||
use board_builder::Board;
|
||||
use board_builder::{ Board, CoordTransformer, read_board_from_file };
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
@ -29,7 +31,18 @@ impl eframe::App for BoardBuilderApp {
|
||||
ui.menu_button("File", |ui| {
|
||||
ui.button("New");
|
||||
if ui.button("Open...").clicked() {
|
||||
choose_file();
|
||||
if let Some(board_file) = FileDialog::new().pick_file() {
|
||||
match read_board_from_file(&board_file) {
|
||||
Ok((board, image)) => {
|
||||
self.board = board;
|
||||
match image {
|
||||
None => { self.image = None; self.texture = None }
|
||||
Some(image) => self.load_image(ctx, image),
|
||||
}
|
||||
}
|
||||
Err(_) => panic!("Could not open file!"),
|
||||
}
|
||||
}
|
||||
}
|
||||
if ui.button("Save As...").clicked() {
|
||||
choose_file();
|
||||
@ -39,7 +52,7 @@ impl eframe::App for BoardBuilderApp {
|
||||
.add_filter("Image", &["png", "jpg", "jpeg", "gif", "webp", "bmp", "tiff"])
|
||||
.pick_file();
|
||||
if let Some(image_file) = image_file {
|
||||
self.load_image(ctx, &image_file);
|
||||
self.load_image_file(ctx, &image_file);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -53,8 +66,9 @@ impl eframe::App for BoardBuilderApp {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
if let Some(texture) = self.texture.as_ref() {
|
||||
let size = ui.available_size();
|
||||
ui.image(texture, size);
|
||||
let (response, painter) = ui.allocate_painter(size, Sense::click());
|
||||
let image = widgets::Image::new(texture, size);
|
||||
image.paint_at(ui, response.rect);
|
||||
let view = View(response.rect);
|
||||
self.draw_board(&painter, view);
|
||||
}
|
||||
@ -63,8 +77,13 @@ impl eframe::App for BoardBuilderApp {
|
||||
}
|
||||
|
||||
impl BoardBuilderApp {
|
||||
fn load_image(&mut self, ctx: &Context, image_file: &Path) -> Result<(), image::ImageError> {
|
||||
fn load_image_file(&mut self, ctx: &Context, image_file: &Path) -> Result<(), image::ImageError> {
|
||||
let image = image::io::Reader::open(image_file)?.decode()?;
|
||||
self.load_image(ctx, image);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_image(&mut self, ctx: &Context, image: DynamicImage) {
|
||||
let egui_image = egui::ColorImage::from_rgba_unmultiplied(
|
||||
[image.width() as _, image.height() as _],
|
||||
image.to_rgba8().as_flat_samples().as_slice(),
|
||||
@ -72,19 +91,23 @@ impl BoardBuilderApp {
|
||||
|
||||
self.image = Some(image);
|
||||
self.texture = Some(ctx.load_texture("board-image", egui_image));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw_board(&mut self, painter: &Painter, view: View) {
|
||||
for node in &self.board.nodes {
|
||||
todo!();
|
||||
fn draw_board(&self, painter: &Painter, view: View) {
|
||||
for node in self.board.nodes.values() {
|
||||
painter.text(
|
||||
view.from_coords(node.x, node.y),
|
||||
Align2::CENTER_CENTER,
|
||||
&node.name,
|
||||
FontId::proportional(12.0),
|
||||
Color32::BLACK,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct View(Rect);
|
||||
|
||||
impl board_builder::CoordTransformer<Pos2> for View {
|
||||
impl CoordTransformer<Pos2> for View {
|
||||
fn origin(&self) -> Pos2 { self.0.min }
|
||||
fn extremes(&self) -> Pos2 { self.0.max }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user