Save and load boards
This commit is contained in:
parent
e9138a9d31
commit
0c0cc7a01d
67
src/main.rs
67
src/main.rs
@ -11,29 +11,36 @@ use image::{ DynamicImage };
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use std::io::{ Write, Read, Cursor };
|
||||
use std::fs::File;
|
||||
|
||||
type Id = usize;
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Node {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
|
||||
pub name: String,
|
||||
pub edges: Vec<Id>,
|
||||
pub edges: Vec<usize>,
|
||||
}
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct Board {
|
||||
nodes: HashMap<Id, Node>,
|
||||
nodes: Vec<Node>,
|
||||
}
|
||||
impl Board {
|
||||
pub fn new() -> Self {
|
||||
let nodes = HashMap::new();
|
||||
let nodes = Vec::new();
|
||||
Board { nodes }
|
||||
}
|
||||
|
||||
pub fn add_node(&mut self, x: f32, y: f32) {
|
||||
self.nodes.push(Node {
|
||||
x,
|
||||
y,
|
||||
name: "Canada".to_string(),
|
||||
edges: vec![],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct AppState {
|
||||
@ -69,6 +76,35 @@ fn decode_png(buf: &[u8]) -> DynamicImage {
|
||||
reader.decode().unwrap()
|
||||
}
|
||||
|
||||
fn lerp(v0: f32, v1: f32, t: f32) -> f32 {
|
||||
v0 + t * (v1 - v0)
|
||||
}
|
||||
|
||||
fn inv_lerp(v0: f32, v1: f32, a: f32) -> f32 {
|
||||
(a - v0) / (v1 - v0)
|
||||
}
|
||||
|
||||
trait CoordTransformer {
|
||||
fn to_coords(&self, x: i32, y: i32) -> (f32, f32);
|
||||
fn from_coords(&self, x: f32, y: f32) -> (i32, i32);
|
||||
}
|
||||
|
||||
impl CoordTransformer for frame::Frame {
|
||||
fn to_coords(&self, x: i32, y: i32) -> (f32, f32) {
|
||||
(
|
||||
inv_lerp(self.x() as f32, (self.x() + self.w()) as f32, x as f32),
|
||||
inv_lerp(self.y() as f32, (self.y() + self.h()) as f32, y as f32),
|
||||
)
|
||||
}
|
||||
|
||||
fn from_coords(&self, x: f32, y: f32) -> (i32, i32) {
|
||||
(
|
||||
lerp(self.x() as f32, (self.x() + self.w()) as f32, x) as i32,
|
||||
lerp(self.y() as f32, (self.y() + self.h()) as f32, y) as i32,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let app = app::App::default()
|
||||
.with_scheme(app::Scheme::Gtk);
|
||||
@ -156,12 +192,31 @@ fn main() {
|
||||
let mut frame = frame::Frame::default();
|
||||
let state_clone = Rc::clone(&state);
|
||||
frame.draw(move |f| {
|
||||
use draw::*;
|
||||
let mut state = state_clone.borrow_mut();
|
||||
let image = &mut state.image;
|
||||
if let Some(image) = image.as_mut() {
|
||||
image.scale(f.w(), f.h(), true, true);
|
||||
image.scale(f.w(), f.h(), false, true);
|
||||
image.draw(f.x(), f.y(), f.w(), f.h());
|
||||
}
|
||||
let board = &state.board;
|
||||
for node in &board.nodes {
|
||||
let (x, y) = f.from_coords(node.x, node.y);
|
||||
draw_text(&node.name, x, y);
|
||||
}
|
||||
});
|
||||
let state_clone = Rc::clone(&state);
|
||||
frame.handle(move |f, e| {
|
||||
match e {
|
||||
Event::Push => {
|
||||
let mut state = state_clone.borrow_mut();
|
||||
let (pos_x, pos_y) = f.to_coords(app::event_x(), app::event_y());
|
||||
state.board.add_node(pos_x, pos_y);
|
||||
app::redraw();
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
});
|
||||
|
||||
flex.end();
|
||||
|
Loading…
Reference in New Issue
Block a user