aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/editor.rs11
-rw-r--r--src/map_data.rs38
-rw-r--r--swap.ron11
3 files changed, 56 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>> {
diff --git a/swap.ron b/swap.ron
new file mode 100644
index 0000000..b1c9073
--- /dev/null
+++ b/swap.ron
@@ -0,0 +1,11 @@
+(
+ rooms: [
+ (
+ x: 5,
+ y: 9,
+ w: 4,
+ h: 2,
+ ),
+ ],
+ walls: [],
+) \ No newline at end of file