aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArne Dußin2020-11-01 18:45:42 +0100
committerArne Dußin2020-11-01 18:45:42 +0100
commit521805eb2fbe17061fb957c2970c6865d86656fc (patch)
treee03490eebdf3727266dd872f873681cc75786b7f /src
parentb87b10a03e98c757aad72ac79a32f2117b318375 (diff)
downloadgraf_karto-521805eb2fbe17061fb957c2970c6865d86656fc.tar.gz
graf_karto-521805eb2fbe17061fb957c2970c6865d86656fc.zip
Add feature to load and save to a test swap-file
Diffstat (limited to 'src')
-rw-r--r--src/editor.rs11
-rw-r--r--src/map_data.rs38
2 files changed, 45 insertions, 4 deletions
diff --git a/src/editor.rs b/src/editor.rs
index 9dcbefc..09f7e0d 100644
--- a/src/editor.rs
+++ b/src/editor.rs
@@ -40,6 +40,17 @@ impl Editor {
self.active
};
+ // Handle saving and loading the editor contents to the swap file
+ if rl.is_key_pressed(KeyboardKey::KEY_S) {
+ self.map_data
+ .write_file("swap.ron")
+ .expect("Unable to write buffer file");
+ } else if rl.is_key_pressed(KeyboardKey::KEY_L) {
+ self.map_data
+ .load_file("swap.ron")
+ .expect("Unable to read buffer file");
+ }
+
for tool in &mut self.tools {
tool.update(&self.map_data, rl, transform);
}
diff --git a/src/map_data.rs b/src/map_data.rs
index d958736..62229c6 100644
--- a/src/map_data.rs
+++ b/src/map_data.rs
@@ -1,6 +1,9 @@
use crate::math::{Rect, Vec2};
+use ron::de::from_reader;
+use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
-use std::io;
+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
@@ -20,12 +23,39 @@ impl MapData {
}
}
- pub fn load_file<P: AsRef<Path>>(&mut self, path: P) -> io::Result<MapData> {
- todo!()
+ 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.walls.append(&mut data.walls);
+
+ Ok(())
}
pub fn write_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
- todo!()
+ let mut file = File::create(&path)?;
+
+ let pretty_conf = PrettyConfig::new()
+ .with_depth_limit(4)
+ .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<f32>> {