use crate::math::{Rect, Vec2}; 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>, walls: Vec<(Vec2, Vec2)>, icons: Vec<(usize, Vec2)>, } impl MapData { pub fn new() -> Self { Self { rooms: Vec::new(), walls: Vec::new(), icons: Vec::new(), } } pub fn load_file>(&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.walls.append(&mut data.walls); self.icons.append(&mut data.icons); Ok(()) } pub fn write_file>(&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> { &self.rooms } pub fn rooms_mut(&mut self) -> &mut Vec> { &mut self.rooms } pub fn walls(&self) -> &Vec<(Vec2, Vec2)> { &self.walls } pub fn walls_mut(&mut self) -> &mut Vec<(Vec2, Vec2)> { &mut self.walls } pub fn icons(&self) -> &Vec<(usize, Vec2)> { &self.icons } pub fn icons_mut(&mut self) -> &mut Vec<(usize, Vec2)> { &mut self.icons } }