2019-05-25 14:39:27 +00:00
|
|
|
use crate::window::KeyMouse;
|
2019-04-25 16:24:00 +00:00
|
|
|
use directories::ProjectDirs;
|
2019-05-25 14:39:27 +00:00
|
|
|
use glutin::{MouseButton, VirtualKeyCode};
|
2019-04-25 16:24:00 +00:00
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2019-05-20 21:32:16 +00:00
|
|
|
use std::{fs, io::prelude::*, path::PathBuf};
|
2019-04-15 17:51:26 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
/// `ControlSettings` contains keybindings.
|
2019-04-15 17:51:26 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-06-08 23:35:23 +00:00
|
|
|
#[serde(default)]
|
2019-04-15 17:51:26 +00:00
|
|
|
pub struct ControlSettings {
|
2019-05-25 14:39:27 +00:00
|
|
|
pub toggle_cursor: KeyMouse,
|
|
|
|
pub escape: KeyMouse,
|
|
|
|
pub enter: KeyMouse,
|
|
|
|
pub move_forward: KeyMouse,
|
|
|
|
pub move_left: KeyMouse,
|
|
|
|
pub move_back: KeyMouse,
|
|
|
|
pub move_right: KeyMouse,
|
|
|
|
pub jump: KeyMouse,
|
|
|
|
pub glide: KeyMouse,
|
|
|
|
pub map: KeyMouse,
|
|
|
|
pub bag: KeyMouse,
|
|
|
|
pub quest_log: KeyMouse,
|
|
|
|
pub character_window: KeyMouse,
|
|
|
|
pub social: KeyMouse,
|
|
|
|
pub spellbook: KeyMouse,
|
|
|
|
pub settings: KeyMouse,
|
|
|
|
pub help: KeyMouse,
|
|
|
|
pub toggle_interface: KeyMouse,
|
|
|
|
pub toggle_debug: KeyMouse,
|
|
|
|
pub fullscreen: KeyMouse,
|
|
|
|
pub screenshot: KeyMouse,
|
2019-05-25 22:00:38 +00:00
|
|
|
pub toggle_ingame_ui: KeyMouse,
|
2019-06-05 15:57:48 +00:00
|
|
|
pub attack: KeyMouse,
|
2019-06-11 04:08:55 +00:00
|
|
|
pub roll: KeyMouse,
|
2019-06-05 15:57:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 23:35:23 +00:00
|
|
|
impl Default for ControlSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
toggle_cursor: KeyMouse::Key(VirtualKeyCode::Tab),
|
|
|
|
escape: KeyMouse::Key(VirtualKeyCode::Escape),
|
|
|
|
enter: KeyMouse::Key(VirtualKeyCode::Return),
|
|
|
|
move_forward: KeyMouse::Key(VirtualKeyCode::W),
|
|
|
|
move_left: KeyMouse::Key(VirtualKeyCode::A),
|
|
|
|
move_back: KeyMouse::Key(VirtualKeyCode::S),
|
|
|
|
move_right: KeyMouse::Key(VirtualKeyCode::D),
|
|
|
|
jump: KeyMouse::Key(VirtualKeyCode::Space),
|
|
|
|
glide: KeyMouse::Key(VirtualKeyCode::LShift),
|
|
|
|
map: KeyMouse::Key(VirtualKeyCode::M),
|
|
|
|
bag: KeyMouse::Key(VirtualKeyCode::B),
|
|
|
|
quest_log: KeyMouse::Key(VirtualKeyCode::L),
|
|
|
|
character_window: KeyMouse::Key(VirtualKeyCode::C),
|
|
|
|
social: KeyMouse::Key(VirtualKeyCode::O),
|
|
|
|
spellbook: KeyMouse::Key(VirtualKeyCode::P),
|
|
|
|
settings: KeyMouse::Key(VirtualKeyCode::N),
|
|
|
|
help: KeyMouse::Key(VirtualKeyCode::F1),
|
|
|
|
toggle_interface: KeyMouse::Key(VirtualKeyCode::F2),
|
|
|
|
toggle_debug: KeyMouse::Key(VirtualKeyCode::F3),
|
|
|
|
fullscreen: KeyMouse::Key(VirtualKeyCode::F11),
|
|
|
|
screenshot: KeyMouse::Key(VirtualKeyCode::F4),
|
|
|
|
toggle_ingame_ui: KeyMouse::Key(VirtualKeyCode::F6),
|
|
|
|
attack: KeyMouse::Mouse(MouseButton::Left),
|
2019-06-11 04:08:55 +00:00
|
|
|
roll: KeyMouse::Mouse(MouseButton::Middle),
|
2019-06-08 23:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-05 15:57:48 +00:00
|
|
|
/// `GameplaySettings` contains sensitivity and gameplay options.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-06-08 23:35:23 +00:00
|
|
|
#[serde(default)]
|
2019-06-05 15:57:48 +00:00
|
|
|
pub struct GameplaySettings {
|
2019-06-06 17:42:13 +00:00
|
|
|
pub pan_sensitivity: u32,
|
|
|
|
pub zoom_sensitivity: u32,
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 23:35:23 +00:00
|
|
|
impl Default for GameplaySettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
pan_sensitivity: 100,
|
|
|
|
zoom_sensitivity: 100,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `NetworkingSettings` stores server and networking settings.
|
2019-04-18 17:40:29 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-06-08 23:35:23 +00:00
|
|
|
#[serde(default)]
|
2019-04-18 17:40:29 +00:00
|
|
|
pub struct NetworkingSettings {
|
|
|
|
pub username: String,
|
|
|
|
pub servers: Vec<String>,
|
|
|
|
pub default_server: usize,
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:35:23 +00:00
|
|
|
impl Default for NetworkingSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
username: "Username".to_string(),
|
|
|
|
servers: vec!["server.veloren.net".to_string()],
|
|
|
|
default_server: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `Log` stores the name to the log file.
|
2019-04-25 11:49:36 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-06-08 23:35:23 +00:00
|
|
|
#[serde(default)]
|
2019-04-25 11:49:36 +00:00
|
|
|
pub struct Log {
|
|
|
|
pub file: PathBuf,
|
|
|
|
}
|
|
|
|
|
2019-06-08 23:35:23 +00:00
|
|
|
impl Default for Log {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
file: "voxygen.log".into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 19:11:39 +00:00
|
|
|
/// `GraphicsSettings` contains settings related to framerate and in-game visuals.
|
2019-05-20 17:40:35 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-06-08 23:35:23 +00:00
|
|
|
#[serde(default)]
|
2019-05-20 17:40:35 +00:00
|
|
|
pub struct GraphicsSettings {
|
|
|
|
pub view_distance: u32,
|
2019-06-06 19:11:39 +00:00
|
|
|
pub max_fps: u32,
|
2019-05-20 17:40:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 23:35:23 +00:00
|
|
|
impl Default for GraphicsSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
view_distance: 5,
|
|
|
|
max_fps: 60,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 19:11:39 +00:00
|
|
|
/// `AudioSettings` controls the volume of different audio subsystems and which
|
2019-05-22 11:29:38 +00:00
|
|
|
/// device is used.
|
2019-05-20 16:54:54 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2019-06-08 23:35:23 +00:00
|
|
|
#[serde(default)]
|
2019-05-20 16:54:54 +00:00
|
|
|
pub struct AudioSettings {
|
2019-07-02 04:10:55 +00:00
|
|
|
pub master_volume: f32,
|
2019-05-20 16:54:54 +00:00
|
|
|
pub music_volume: f32,
|
|
|
|
pub sfx_volume: f32,
|
|
|
|
|
2019-05-22 11:29:38 +00:00
|
|
|
/// Audio Device that Voxygen will use to play audio.
|
|
|
|
pub audio_device: Option<String>,
|
2019-07-03 19:13:38 +00:00
|
|
|
pub audio_on: bool,
|
2019-05-20 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 23:35:23 +00:00
|
|
|
impl Default for AudioSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2019-07-02 04:10:55 +00:00
|
|
|
master_volume: 1.0,
|
2019-06-08 23:35:23 +00:00
|
|
|
music_volume: 0.5,
|
|
|
|
sfx_volume: 0.5,
|
|
|
|
audio_device: None,
|
2019-07-03 19:13:38 +00:00
|
|
|
audio_on: true,
|
2019-06-08 23:35:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `Settings` contains everything that can be configured in the settings.ron file.
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(default)]
|
|
|
|
pub struct Settings {
|
|
|
|
pub controls: ControlSettings,
|
|
|
|
pub gameplay: GameplaySettings,
|
|
|
|
pub networking: NetworkingSettings,
|
|
|
|
pub log: Log,
|
|
|
|
pub graphics: GraphicsSettings,
|
|
|
|
pub audio: AudioSettings,
|
|
|
|
pub show_disclaimer: bool,
|
|
|
|
}
|
|
|
|
|
2019-04-16 17:41:39 +00:00
|
|
|
impl Default for Settings {
|
|
|
|
fn default() -> Self {
|
2019-04-16 08:56:47 +00:00
|
|
|
Settings {
|
2019-06-08 23:35:23 +00:00
|
|
|
controls: ControlSettings::default(),
|
|
|
|
gameplay: GameplaySettings::default(),
|
|
|
|
networking: NetworkingSettings::default(),
|
|
|
|
log: Log::default(),
|
|
|
|
graphics: GraphicsSettings::default(),
|
|
|
|
audio: AudioSettings::default(),
|
2019-05-26 20:42:45 +00:00
|
|
|
show_disclaimer: true,
|
2019-04-16 08:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-16 17:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
2019-05-17 07:55:51 +00:00
|
|
|
pub fn load() -> Self {
|
2019-04-25 16:24:00 +00:00
|
|
|
let path = Settings::get_settings_path();
|
|
|
|
|
2019-05-26 09:21:51 +00:00
|
|
|
// If file doesn't exist, use the default settings.
|
|
|
|
if let Ok(file) = fs::File::open(path) {
|
|
|
|
ron::de::from_reader(file).expect("Error parsing settings")
|
|
|
|
} else {
|
|
|
|
Self::default()
|
2019-05-17 07:55:51 +00:00
|
|
|
}
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|
2019-04-16 16:06:23 +00:00
|
|
|
|
|
|
|
pub fn save_to_file(&self) -> std::io::Result<()> {
|
2019-04-25 16:24:00 +00:00
|
|
|
let path = Settings::get_settings_path();
|
2019-05-20 21:32:16 +00:00
|
|
|
if let Some(dir) = path.parent() {
|
|
|
|
fs::create_dir_all(dir)?;
|
|
|
|
}
|
|
|
|
let mut config_file = fs::File::create(path)?;
|
2019-06-08 23:35:23 +00:00
|
|
|
|
2019-05-26 09:21:51 +00:00
|
|
|
let s: &str = &ron::ser::to_string_pretty(self, ron::ser::PrettyConfig::default()).unwrap();
|
2019-04-16 16:06:23 +00:00
|
|
|
config_file.write_all(s.as_bytes()).unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-04-25 16:24:00 +00:00
|
|
|
|
|
|
|
fn get_settings_path() -> PathBuf {
|
2019-06-08 23:35:23 +00:00
|
|
|
let proj_dirs = ProjectDirs::from("net", "veloren", "voxygen")
|
|
|
|
.expect("System's $HOME directory path not found!");
|
|
|
|
proj_dirs
|
|
|
|
.config_dir()
|
|
|
|
.join("settings")
|
|
|
|
.with_extension("ron")
|
2019-04-25 16:24:00 +00:00
|
|
|
}
|
2019-04-15 17:51:26 +00:00
|
|
|
}
|