This commit is contained in:
Dane Johnson 2022-05-11 15:41:31 -05:00
parent 8300f2a254
commit 8c6f54054c
3 changed files with 28 additions and 29 deletions

View File

@ -8,7 +8,6 @@ use rfd::FileDialog;
use board_builder::{ Board, CoordTransformer, read_board_from_file, write_board_to_file };
use std::path::Path;
use std::collections::HashMap;
use std::rc::Rc;
use std::cell::RefCell;
@ -109,7 +108,7 @@ impl eframe::App for BoardBuilderApp {
} else {
PointerButton::Middle
};
let (x, y) = view.to_coords(pos);
let (x, y) = view.xform(pos);
self.dispatch_click(btn, x, y);
}
}
@ -171,7 +170,7 @@ impl BoardBuilderApp {
Color32::BLACK
};
painter.text(
view.from_coords(node.x, node.y),
view.inv_xform(node.x, node.y),
Align2::CENTER_CENTER,
&node.name,
FontId::proportional(16.0),
@ -181,7 +180,7 @@ impl BoardBuilderApp {
for edge in &node.edges {
let other_node = &board.nodes[edge];
painter.line_segment(
[view.from_coords(node.x, node.y), view.from_coords(other_node.x, other_node.y)],
[view.inv_xform(node.x, node.y), view.inv_xform(other_node.x, other_node.y)],
stroke,
);
}
@ -294,7 +293,7 @@ impl EditLabelsDialog {
});
col[1].horizontal(|ui| {
if ui.button("+").clicked() {
todo!();
self.add_value_dialog.show();
}
if ui.button("-").clicked() {
todo!();
@ -311,25 +310,6 @@ impl EditLabelsDialog {
}
}
#[derive(Default)]
struct EditNodeDialog {
open: bool,
name: String,
id: usize,
labels: HashMap<String, String>,
}
impl EditNodeDialog {
fn show(&mut self, id: usize, board: &Board) {
self.open = true;
let node = board.nodes.get(&id).unwrap();
self.id = id;
self.name = node.name.clone();
self.labels = node.labels.clone();
}
}
#[derive(Default)]
struct StringDialog {
@ -373,3 +353,22 @@ impl StringDialog {
}
}
}
// #[derive(Default)]
// struct EditNodeDialog {
// open: bool,
// name: String,
// id: usize,
// labels: HashMap<String, String>,
// }
// impl EditNodeDialog {
// fn show(&mut self, id: usize, board: &Board) {
// self.open = true;
// let node = board.nodes.get(&id).unwrap();
// self.id = id;
// self.name = node.name.clone();
// self.labels = node.labels.clone();
// }
// }

View File

@ -277,7 +277,7 @@ fn main() {
let f = FrameView(f);
for (&id, node) in &board.nodes {
// Draw the node
let Pos(x, y) = f.from_coords(node.x, node.y);
let Pos(x, y) = f.inv_xform(node.x, node.y);
if state.selected_node == Some(id) {
set_draw_color(Color::Red);
draw_text(&node.name, x, y);
@ -288,7 +288,7 @@ fn main() {
// Draw edges
for other_id in &node.edges {
let other = board.nodes.get(other_id).unwrap();
let Pos(x1, y1) = f.from_coords(other.x, other.y);
let Pos(x1, y1) = f.inv_xform(other.x, other.y);
draw_line(x, y, x1, y1);
}
}
@ -298,7 +298,7 @@ fn main() {
Event::Push => {
let f = FrameView(f);
let edit_mode = STATE.get().lock().unwrap().edit_mode;
let coords = f.to_coords(Pos(app::event_x(), app::event_y()));
let coords = f.xform(Pos(app::event_x(), app::event_y()));
match edit_mode {
EditMode::Node => dispatch::node_press(coords),

View File

@ -147,7 +147,7 @@ fn inv_lerp(v0: f32, v1: f32, a: f32) -> f32 {
pub trait CoordTransformer<I: Into<(f32, f32)> + From<(f32, f32)>> {
fn origin(&self) -> I;
fn extremes(&self) -> I;
fn to_coords(&self, pos: I) -> (f32, f32) {
fn xform(&self, pos: I) -> (f32, f32) {
let (sx, sy) = self.origin().into();
let (ex, ey) = self.extremes().into();
let (x, y) = pos.into();
@ -156,7 +156,7 @@ pub trait CoordTransformer<I: Into<(f32, f32)> + From<(f32, f32)>> {
inv_lerp(sy, ey, y),
)
}
fn from_coords(&self, x: f32, y: f32) -> I {
fn inv_xform(&self, x: f32, y: f32) -> I {
let (sx, sy) = self.origin().into();
let (ex, ey) = self.extremes().into();
(lerp(sx, ex, x), lerp(sy, ey, y)).into()