aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: b5abb1e0b1ef678d89edc1d6e79148355b7fad89 (plain) (blame)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! Home of the user configuratable content of graf karto, like keybindings and (TODO) colours etc.

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;

/// All configuration parameters the user can set are contained in this struct.
#[allow(missing_docs)]
#[derive(Deserialize, Serialize)]
pub struct Config {
    pub tool_activation_keys: ToolActivationKeys,
    pub tool_general_keys: ToolGeneralKeys,
    pub icon_keys: IconToolKeys,
}

#[allow(missing_docs)]
#[derive(Deserialize, Serialize)]
/// The keys used to activate the individual tools. These keystrokes will not be sent to the tools,
/// but instead will be handled by the editor where the tools are registered.
pub struct ToolActivationKeys {
    pub deletion: Button,
    pub icon: Button,
    pub polygon_room: Button,
    pub rect_room: Button,
    pub selection: Button,
    pub wall: Button,
}

#[derive(Deserialize, Serialize)]
/// Keys that are useful to most tools. These are packaged so that not every tool has the same n keys
/// and then some more.
pub struct ToolGeneralKeys {
    /// Keybinding to, where applicable, place a single node (usually a vertex) for the tool in
    /// question.
    pub place_single: Button,
    /// Finish up whatever one is doing with the current tool, without removing information.
    pub finish: Button,
    /// Abort whatever one is doing with the current tool which means the last atomic action will not
    /// be pushed into the map items.
    pub abort: Button,
}

#[derive(Clone, Serialize, Deserialize)]
/// Key bindings that are individually interesting to the icon tool.
pub struct IconToolKeys {
    /// Key to change to the next icon of the icon list.
    pub next: Button,
    /// Key to change to the previous icon of the icon list.
    pub previous: Button,
    /// Rotate the working icon clockwise by a certain amount (currently 45 degrees)
    pub rotate_clockwise: Button,
    /// Rotate the working icon counterclockwise by a certain amount (currently 45 degrees)
    pub rotate_counterclockwise: Button,
}

impl Config {
    /// Try to parse a configuration from the file located at path.
    ///
    /// # Errors
    /// If the file is not found or can not be read or parsed for a different reason, an IO-Error is
    /// returned.
    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)),
        }
    }

    /// Try to write the configuration to the file at path. If the file exists, it will be overwritten.
    ///
    /// # Errors
    /// If the file can not be written, for example for lack of permissions, an IO-Error is returned.
    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 {
            tool_activation_keys: ToolActivationKeys {
                deletion: Button::Keyboard(KeyboardKey::D),
                icon: Button::Keyboard(KeyboardKey::I),
                polygon_room: Button::Keyboard(KeyboardKey::P),
                rect_room: Button::Keyboard(KeyboardKey::R),
                selection: Button::Keyboard(KeyboardKey::S),
                wall: Button::Keyboard(KeyboardKey::W),
            },
            tool_general_keys: ToolGeneralKeys {
                place_single: Button::Mouse(MouseButton::Left),
                finish: Button::Keyboard(KeyboardKey::Enter),
                abort: Button::Mouse(MouseButton::Right),
            },
            icon_keys: IconToolKeys {
                next: Button::Keyboard(KeyboardKey::I),
                previous: Button::Keyboard(KeyboardKey::J),
                rotate_clockwise: Button::Mouse(MouseButton::Right),
                rotate_counterclockwise: Button::Keyboard(KeyboardKey::Minus),
            },
        }
    }
}