save dialog, n=tabs y=spaces
This commit is contained in:
106
src/lib.rs
106
src/lib.rs
@@ -19,56 +19,56 @@ pub struct Board {
|
||||
}
|
||||
impl Board {
|
||||
pub fn new() -> Self {
|
||||
let nodes = HashMap::new();
|
||||
let labels = HashMap::new();
|
||||
Board { nodes, labels }
|
||||
let nodes = HashMap::new();
|
||||
let labels = HashMap::new();
|
||||
Board { nodes, labels }
|
||||
}
|
||||
|
||||
pub fn add_node(&mut self, x: f32, y: f32, name: String) {
|
||||
self.nodes.insert(self.next_id(), Node {
|
||||
x,
|
||||
y,
|
||||
name,
|
||||
edges: HashSet::new(),
|
||||
labels: HashMap::new(),
|
||||
});
|
||||
self.nodes.insert(self.next_id(), Node {
|
||||
x,
|
||||
y,
|
||||
name,
|
||||
edges: HashSet::new(),
|
||||
labels: HashMap::new(),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn remove_node(&mut self, id: usize) {
|
||||
// We remove this node from the graph, then drop it from each
|
||||
// other nodes edge.
|
||||
self.nodes.remove(&id);
|
||||
for node in self.nodes.values_mut() {
|
||||
node.edges.remove(&id);
|
||||
}
|
||||
// We remove this node from the graph, then drop it from each
|
||||
// other nodes edge.
|
||||
self.nodes.remove(&id);
|
||||
for node in self.nodes.values_mut() {
|
||||
node.edges.remove(&id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn nearest_node(&self, x: f32, y: f32) -> Option<usize> {
|
||||
let f = |n: &Node| dist_sq((n.x, n.y), (x, y));
|
||||
let mut iter = self.nodes.iter();
|
||||
if let Some((id, node)) = iter.next() {
|
||||
let mut min = *id;
|
||||
let mut min_val = f(node);
|
||||
for (id, node) in iter {
|
||||
let val = f(node);
|
||||
if val < min_val {
|
||||
min = *id;
|
||||
min_val = val;
|
||||
}
|
||||
}
|
||||
Some(min)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
let f = |n: &Node| dist_sq((n.x, n.y), (x, y));
|
||||
let mut iter = self.nodes.iter();
|
||||
if let Some((id, node)) = iter.next() {
|
||||
let mut min = *id;
|
||||
let mut min_val = f(node);
|
||||
for (id, node) in iter {
|
||||
let val = f(node);
|
||||
if val < min_val {
|
||||
min = *id;
|
||||
min_val = val;
|
||||
}
|
||||
}
|
||||
Some(min)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
fn next_id(&self) -> usize {
|
||||
for i in 0 .. {
|
||||
if !self.nodes.contains_key(&i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
unreachable!();
|
||||
for i in 0 .. {
|
||||
if !self.nodes.contains_key(&i) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
unreachable!();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,10 +92,10 @@ pub fn write_board_to_file(board: &Board, image: Option<&DynamicImage>, path: &P
|
||||
ar.start_file("graph.json", options)?;
|
||||
ar.write_all(&serde_json::to_vec(board)?)?;
|
||||
if let Some(image) = image {
|
||||
ar.start_file("image.png", options)?;
|
||||
let data = encode_png(image);
|
||||
ar.write_all(&data)?;
|
||||
ar.flush()?;
|
||||
ar.start_file("image.png", options)?;
|
||||
let data = encode_png(image);
|
||||
ar.write_all(&data)?;
|
||||
ar.flush()?;
|
||||
}
|
||||
ar.finish()?;
|
||||
Ok(())
|
||||
@@ -109,7 +109,7 @@ pub fn read_board_from_file(path: &Path) -> io::Result<(Board, Option<DynamicIma
|
||||
let board = serde_json::from_reader(json_file)?;
|
||||
let image = ar.by_name("image.png").ok();
|
||||
if image.is_none() {
|
||||
return Ok((board, None))
|
||||
return Ok((board, None))
|
||||
}
|
||||
let mut image_file = image.unwrap();
|
||||
let mut buf = Vec::new();
|
||||
@@ -143,17 +143,17 @@ 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) {
|
||||
let (sx, sy) = self.origin().into();
|
||||
let (ex, ey) = self.extremes().into();
|
||||
let (x, y) = pos.into();
|
||||
(
|
||||
inv_lerp(sx, ex, x),
|
||||
inv_lerp(sy, ey, y),
|
||||
)
|
||||
let (sx, sy) = self.origin().into();
|
||||
let (ex, ey) = self.extremes().into();
|
||||
let (x, y) = pos.into();
|
||||
(
|
||||
inv_lerp(sx, ex, x),
|
||||
inv_lerp(sy, ey, y),
|
||||
)
|
||||
}
|
||||
fn from_coords(&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()
|
||||
let (sx, sy) = self.origin().into();
|
||||
let (ex, ey) = self.extremes().into();
|
||||
(lerp(sx, ex, x), lerp(sy, ey, y)).into()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user