2019-04-15 17:51:26 +00:00
|
|
|
use config::{
|
|
|
|
Config,
|
|
|
|
ConfigError,
|
|
|
|
};
|
|
|
|
use serde_derive::{Serialize, Deserialize};
|
|
|
|
use glutin::VirtualKeyCode;
|
2019-04-16 16:06:23 +00:00
|
|
|
use toml;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
2019-04-15 17:51:26 +00:00
|
|
|
|
2019-04-16 08:56:47 +00:00
|
|
|
/// Settings contains everything that can be configured in the Settings.toml file
|
2019-04-15 17:51:26 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Settings {
|
|
|
|
pub controls: ControlSettings,
|
|
|
|
}
|
|
|
|
|
2019-04-16 08:56:47 +00:00
|
|
|
/// ControlSettings contains keybindings
|
2019-04-15 17:51:26 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct ControlSettings {
|
|
|
|
pub toggle_cursor: VirtualKeyCode,
|
|
|
|
pub escape: VirtualKeyCode,
|
|
|
|
pub enter: VirtualKeyCode,
|
|
|
|
pub move_forward: VirtualKeyCode,
|
|
|
|
pub move_left: VirtualKeyCode,
|
|
|
|
pub move_back: VirtualKeyCode,
|
|
|
|
pub move_right: VirtualKeyCode,
|
|
|
|
pub map: VirtualKeyCode,
|
|
|
|
pub bag: VirtualKeyCode,
|
|
|
|
pub quest_log: VirtualKeyCode,
|
|
|
|
pub character_window: VirtualKeyCode,
|
|
|
|
pub social: VirtualKeyCode,
|
|
|
|
pub spellbook: VirtualKeyCode,
|
|
|
|
pub settings: VirtualKeyCode,
|
|
|
|
pub help: VirtualKeyCode,
|
|
|
|
pub interface: VirtualKeyCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2019-04-16 08:56:47 +00:00
|
|
|
pub fn default() -> Self {
|
|
|
|
Settings {
|
|
|
|
controls: ControlSettings {
|
|
|
|
toggle_cursor: VirtualKeyCode::Tab,
|
|
|
|
escape: VirtualKeyCode::Escape,
|
|
|
|
enter: VirtualKeyCode::Return,
|
|
|
|
move_forward: VirtualKeyCode::W,
|
|
|
|
move_left: VirtualKeyCode::A,
|
|
|
|
move_back: VirtualKeyCode::S,
|
|
|
|
move_right: VirtualKeyCode::D,
|
|
|
|
map: VirtualKeyCode::M,
|
|
|
|
bag: VirtualKeyCode::B,
|
|
|
|
quest_log: VirtualKeyCode::L,
|
|
|
|
character_window: VirtualKeyCode::C,
|
|
|
|
social: VirtualKeyCode::O,
|
|
|
|
spellbook: VirtualKeyCode::P,
|
|
|
|
settings: VirtualKeyCode::N,
|
|
|
|
help: VirtualKeyCode::F2,
|
|
|
|
interface: VirtualKeyCode::F2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 16:06:23 +00:00
|
|
|
|
2019-04-15 17:51:26 +00:00
|
|
|
pub fn load() -> Result<Self, ConfigError> {
|
|
|
|
let mut config = Config::new();
|
2019-04-16 16:06:23 +00:00
|
|
|
config.merge(config::File::with_name("settings"))?;
|
2019-04-15 17:51:26 +00:00
|
|
|
config.try_into()
|
|
|
|
}
|
2019-04-16 16:06:23 +00:00
|
|
|
|
|
|
|
pub fn save_to_file(&self) -> std::io::Result<()> {
|
|
|
|
let mut config_file = File::create("settings.toml")?;
|
|
|
|
let s: &str = &toml::to_string_pretty(self).unwrap();
|
|
|
|
config_file.write_all(s.as_bytes()).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|