Another change to Coord trait stuff

This commit is contained in:
2022-05-09 15:47:00 -05:00
parent 401560d35e
commit e97598b69f
7 changed files with 1745 additions and 75 deletions

View File

@@ -133,13 +133,38 @@ mod dispatch {
}
struct FrameView<'a>(&'a frame::Frame);
struct Pos(i32, i32);
impl<'a> CoordTransformer<i32> for FrameView<'a> {
fn origin(&self) -> (i32, i32) { (self.0.x(), self.0.y()) }
fn extremes(&self) -> (i32, i32) { (self.0.x()+self.0.w(), self.0.y()+self.0.h()) }
impl<'a> FrameView<'a> {
fn origin(&self) -> Pos {
Pos(self.0.x(), self.0.y())
}
fn extremes(&self) -> Pos {
Pos(self.0.x() + self.0.w(), self.0.y() + self.0.h())
}
}
impl<'a> CoordTransformer<Pos> for FrameView<'a> {
fn origin(&self) -> Pos { self.origin() }
fn extremes(&self) -> Pos { self.extremes() }
}
impl From<(f32, f32)> for Pos {
fn from((x, y): (f32, f32)) -> Self {
Pos(x as i32, y as i32)
}
}
impl From<Pos> for (f32, f32) {
fn from(Pos(x, y): Pos) -> Self {
(x as f32, y as f32)
}
}
//////////////////// App State ////////////////////
#[derive(Clone, Copy)]
enum EditMode {
Node,
@@ -253,7 +278,7 @@ fn main() {
let f = FrameView(f);
for (&id, node) in &board.nodes {
// Draw the node
let (x, y) = f.from_coords(node.x, node.y);
let Pos(x, y) = f.from_coords(node.x, node.y);
if state.selected_node == Some(id) {
set_draw_color(Color::Red);
draw_text(&node.name, x, y);
@@ -264,7 +289,7 @@ fn main() {
// Draw edges
for other_id in &node.edges {
let other = board.nodes.get(other_id).unwrap();
let (x1, y1) = f.from_coords(other.x, other.y);
let Pos(x1, y1) = f.from_coords(other.x, other.y);
draw_line(x, y, x1, y1);
}
}
@@ -274,7 +299,7 @@ fn main() {
Event::Push => {
let f = FrameView(f);
let edit_mode = STATE.get().lock().unwrap().edit_mode;
let coords = f.to_coords(app::event_x(), app::event_y());
let coords = f.to_coords(Pos(app::event_x(), app::event_y()));
match edit_mode {
EditMode::Node => dispatch::node_press(coords),