1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
use crate::math::{Polygon, Rect, Vec2};
use crate::tool::icon_tool::IconInfo;
use ron::de::from_reader;
use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
/// All the data of the currently opened map. This does not include things that are currently being
/// drawn, but not finished. It also does not contain Metadata, like the current position of the
/// transform, or the zoom-level
#[derive(Serialize, Deserialize)]
pub struct MapData {
rooms: Vec<Rect<f64>>,
polygons: Vec<Polygon<f64>>,
walls: Vec<(Vec2<f64>, Vec2<f64>)>,
icons: Vec<IconInfo>,
}
impl MapData {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
rooms: Vec::new(),
polygons: Vec::new(),
walls: Vec::new(),
icons: Vec::new(),
}
}
pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<()> {
let file = File::open(&path)?;
let mut data: Self = match from_reader(file) {
Ok(data) => data,
Err(err) => {
return Err(io::Error::new(io::ErrorKind::InvalidData, err));
}
};
/* Append map data to the currently loaded map. (Default behaviour might change in the
* future)
*/
self.rooms.append(&mut data.rooms);
self.polygons.append(&mut data.polygons);
self.walls.append(&mut data.walls);
self.icons.append(&mut data.icons);
Ok(())
}
pub fn write_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
let mut file = File::create(&path)?;
let pretty_conf = PrettyConfig::new()
.with_depth_limit(4)
.with_decimal_floats(true)
.with_separate_tuple_members(true)
.with_indentor("\t".to_owned());
let string = match to_string_pretty(&self, pretty_conf) {
Ok(string) => string,
Err(err) => {
return Err(io::Error::new(io::ErrorKind::InvalidInput, err));
}
};
file.write_all(&string.as_bytes())
}
pub fn rooms(&self) -> &Vec<Rect<f64>> {
&self.rooms
}
pub fn rooms_mut(&mut self) -> &mut Vec<Rect<f64>> {
&mut self.rooms
}
pub fn polygons(&self) -> &Vec<Polygon<f64>> {
&self.polygons
}
pub fn polygons_mut(&mut self) -> &mut Vec<Polygon<f64>> {
&mut self.polygons
}
pub fn walls(&self) -> &Vec<(Vec2<f64>, Vec2<f64>)> {
&self.walls
}
pub fn walls_mut(&mut self) -> &mut Vec<(Vec2<f64>, Vec2<f64>)> {
&mut self.walls
}
pub fn icons(&self) -> &Vec<IconInfo> {
&self.icons
}
pub fn icons_mut(&mut self) -> &mut Vec<IconInfo> {
&mut self.icons
}
}
|