aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authorArne Dußin2020-11-09 21:15:35 +0100
committerArne Dußin2020-11-09 21:15:35 +0100
commitb06e0075bf4dfd51f8ad5df801f9c43fbd73df1f (patch)
treebb9de75363ade9f2f27ebdb60507dbefb36afc35 /src/config.rs
parente08cb85528e6b72d5f3b2fbeb79b581fa7a4e461 (diff)
downloadgraf_karto-b06e0075bf4dfd51f8ad5df801f9c43fbd73df1f.tar.gz
graf_karto-b06e0075bf4dfd51f8ad5df801f9c43fbd73df1f.zip
Add configuration options
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..4e90b21
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,136 @@
+use crate::button::*;
+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;
+
+#[derive(Deserialize, Serialize)]
+pub struct Config {
+ pub deletion_keybindings: DeletionToolKeybindings,
+ pub icon_keybindings: IconToolKeybindings,
+ pub room_keybindings: RoomToolKeybindings,
+ pub wall_keybindings: WallToolKeybindings,
+}
+
+pub trait ToolKeybindings {
+ /// Get the key which will activate the tool it is made for.
+ fn activation_key(&self) -> Button;
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct DeletionToolKeybindings {
+ pub activation: Button,
+ pub start_selection: Button,
+ pub do_delete: Button,
+ pub abort_deletion: Button,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct IconToolKeybindings {
+ pub activation: Button,
+ pub next: Button,
+ pub previous: Button,
+ pub rotate_clockwise: Button,
+ pub rotate_counterclockwise: Button,
+ pub place: Button,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct RoomToolKeybindings {
+ pub activation: Button,
+ pub start_draw: Button,
+ pub finish_draw: Button,
+ pub abort_draw: Button,
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct WallToolKeybindings {
+ pub activation: Button,
+ pub start_wall: Button,
+ pub finish_segment: Button,
+ pub abort_segment: Button,
+}
+
+impl Config {
+ pub fn from_file<P: AsRef<Path>>(path: P) -> io::Result<Config> {
+ let file = File::open(&path)?;
+ match from_reader(file) {
+ Ok(data) => Ok(data),
+ Err(err) => Err(io::Error::new(io::ErrorKind::InvalidData, err)),
+ }
+ }
+
+ 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())
+ }
+}
+
+impl Default for Config {
+ fn default() -> Self {
+ Config {
+ deletion_keybindings: DeletionToolKeybindings {
+ activation: Button::Keyboard(KeyboardKey::D),
+ start_selection: Button::Mouse(MouseButton::Left),
+ do_delete: Button::Mouse(MouseButton::Left),
+ abort_deletion: Button::Mouse(MouseButton::Right),
+ },
+ icon_keybindings: IconToolKeybindings {
+ activation: Button::Keyboard(KeyboardKey::I),
+ next: Button::Keyboard(KeyboardKey::I),
+ previous: Button::Keyboard(KeyboardKey::J),
+ rotate_clockwise: Button::Mouse(MouseButton::Right),
+ rotate_counterclockwise: Button::Keyboard(KeyboardKey::Minus),
+ place: Button::Mouse(MouseButton::Left),
+ },
+ room_keybindings: RoomToolKeybindings {
+ activation: Button::Keyboard(KeyboardKey::R),
+ start_draw: Button::Mouse(MouseButton::Left),
+ finish_draw: Button::Mouse(MouseButton::Left),
+ abort_draw: Button::Mouse(MouseButton::Right),
+ },
+ wall_keybindings: WallToolKeybindings {
+ activation: Button::Keyboard(KeyboardKey::W),
+ start_wall: Button::Mouse(MouseButton::Left),
+ finish_segment: Button::Mouse(MouseButton::Left),
+ abort_segment: Button::Mouse(MouseButton::Right),
+ },
+ }
+ }
+}
+
+impl ToolKeybindings for DeletionToolKeybindings {
+ fn activation_key(&self) -> Button {
+ self.activation
+ }
+}
+impl ToolKeybindings for IconToolKeybindings {
+ fn activation_key(&self) -> Button {
+ self.activation
+ }
+}
+impl ToolKeybindings for RoomToolKeybindings {
+ fn activation_key(&self) -> Button {
+ self.activation
+ }
+}
+impl ToolKeybindings for WallToolKeybindings {
+ fn activation_key(&self) -> Button {
+ self.activation
+ }
+}