// Player settings: stored in the browser (localStorage "dc.settings") and in every savegame. use macroquad::prelude::KeyCode; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; // Rebindable keyboard actions. Esc (menu) and the arrow keys (camera) are fixed. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Action { Tool(usize), // 0..18, same order as ui::TOOLS CamForward, CamBack, CamLeft, CamRight, RotateLeft, RotateRight, TiltUp, TiltDown, Pause, Mute, VolumeUp, VolumeDown, Outlines, Help, } pub fn all_actions() -> Vec { let mut v: Vec = (0..18).map(Action::Tool).collect(); v.extend_from_slice(&[ Action::CamForward, Action::CamBack, Action::CamLeft, Action::CamRight, Action::RotateLeft, Action::RotateRight, Action::TiltUp, Action::TiltDown, Action::Pause, Action::Mute, Action::VolumeUp, Action::VolumeDown, Action::Outlines, Action::Help, ]); v } impl Action { // stable id used in settings files and as language key suffix (key.) pub fn id(self) -> String { match self { Action::Tool(i) => format!("tool{}", i), Action::CamForward => "cam_forward".into(), Action::CamBack => "cam_back".into(), Action::CamLeft => "cam_left".into(), Action::CamRight => "cam_right".into(), Action::RotateLeft => "rotate_left".into(), Action::RotateRight => "rotate_right".into(), Action::TiltUp => "tilt_up".into(), Action::TiltDown => "tilt_down".into(), Action::Pause => "pause".into(), Action::Mute => "mute".into(), Action::VolumeUp => "volume_up".into(), Action::VolumeDown => "volume_down".into(), Action::Outlines => "outlines".into(), Action::Help => "help".into(), } } fn default_key(self) -> KeyCode { use KeyCode::*; match self { Action::Tool(i) => [Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9, Key0, X, V, K, J, B, C, T, U][i.min(17)], Action::CamForward => W, Action::CamBack => S, Action::CamLeft => A, Action::CamRight => D, Action::RotateLeft => Q, Action::RotateRight => E, Action::TiltUp => R, Action::TiltDown => F, Action::Pause => P, Action::Mute => M, Action::VolumeUp => Equal, Action::VolumeDown => Minus, Action::Outlines => G, Action::Help => H, } } } // Keys that can be bound, with their display names const KEY_NAMES: &[(KeyCode, &str)] = &[ (KeyCode::A, "A"), (KeyCode::B, "B"), (KeyCode::C, "C"), (KeyCode::D, "D"), (KeyCode::E, "E"), (KeyCode::F, "F"), (KeyCode::G, "G"), (KeyCode::H, "H"), (KeyCode::I, "I"), (KeyCode::J, "J"), (KeyCode::K, "K"), (KeyCode::L, "L"), (KeyCode::M, "M"), (KeyCode::N, "N"), (KeyCode::O, "O"), (KeyCode::P, "P"), (KeyCode::Q, "Q"), (KeyCode::R, "R"), (KeyCode::S, "S"), (KeyCode::T, "T"), (KeyCode::U, "U"), (KeyCode::V, "V"), (KeyCode::W, "W"), (KeyCode::X, "X"), (KeyCode::Y, "Y"), (KeyCode::Z, "Z"), (KeyCode::Key0, "0"), (KeyCode::Key1, "1"), (KeyCode::Key2, "2"), (KeyCode::Key3, "3"), (KeyCode::Key4, "4"), (KeyCode::Key5, "5"), (KeyCode::Key6, "6"), (KeyCode::Key7, "7"), (KeyCode::Key8, "8"), (KeyCode::Key9, "9"), (KeyCode::Space, "Space"), (KeyCode::Minus, "-"), (KeyCode::Equal, "="), (KeyCode::Comma, ","), (KeyCode::Period, "."), (KeyCode::Slash, "/"), (KeyCode::Semicolon, ";"), (KeyCode::Apostrophe, "'"), (KeyCode::LeftBracket, "["), (KeyCode::RightBracket, "]"), (KeyCode::Backslash, "\\"), (KeyCode::Tab, "Tab"), (KeyCode::Backspace, "Backspace"), (KeyCode::Enter, "Enter"), (KeyCode::Insert, "Insert"), (KeyCode::Delete, "Delete"), (KeyCode::Home, "Home"), (KeyCode::End, "End"), (KeyCode::PageUp, "PageUp"), (KeyCode::PageDown, "PageDown"), (KeyCode::F1, "F1"), (KeyCode::F2, "F2"), (KeyCode::F3, "F3"), (KeyCode::F4, "F4"), (KeyCode::F6, "F6"), (KeyCode::F7, "F7"), (KeyCode::F8, "F8"), (KeyCode::F9, "F9"), (KeyCode::F10, "F10"), (KeyCode::Kp0, "Num0"), (KeyCode::Kp1, "Num1"), (KeyCode::Kp2, "Num2"), (KeyCode::Kp3, "Num3"), (KeyCode::Kp4, "Num4"), (KeyCode::Kp5, "Num5"), (KeyCode::Kp6, "Num6"), (KeyCode::Kp7, "Num7"), (KeyCode::Kp8, "Num8"), (KeyCode::Kp9, "Num9"), (KeyCode::KpAdd, "Num+"), (KeyCode::KpSubtract, "Num-"), (KeyCode::KpMultiply, "Num*"), (KeyCode::KpDivide, "Num/"), (KeyCode::LeftShift, "Shift"), (KeyCode::LeftControl, "Ctrl"), (KeyCode::LeftAlt, "Alt"), ]; pub fn key_name(k: KeyCode) -> String { KEY_NAMES.iter().find(|(c, _)| *c == k).map(|(_, n)| n.to_string()).unwrap_or_else(|| "?".into()) } fn key_from_name(n: &str) -> Option { KEY_NAMES.iter().find(|(_, name)| *name == n).map(|(c, _)| *c) } // All bindable keys (for the key capture dialog) pub fn bindable_keys() -> impl Iterator { KEY_NAMES.iter().map(|(k, _)| *k) } #[derive(Clone, Serialize, Deserialize, PartialEq)] #[serde(default)] pub struct Settings { // language & sound pub language: String, pub volume: f32, pub music_volume: f32, pub muted: bool, // camera pub rotate_speed: f32, pub zoom_speed: f32, pub edge_scroll: bool, pub invert_mouse: bool, // graphics pub outlines: bool, pub wall_detail: u8, // 1 low, 2 medium, 3 high pub max_lights: u8, // 8, 16 or 24 pub ui_factor: u8, // interface size: 0 = automatic, 1..4 = fixed integer factor pub render_scale: u8, // 3D world resolution divisor: 1 = full, 2..16 = pixel look pub touch_ui: u8, // touch interface: 0 = automatic (after the first touch), 1 = on, 2 = off // game pub game_speed: f32, // 1 or 2 pub hints: bool, pub map_w: usize, // size of new maps (64..256) pub map_h: usize, // key bindings: action id -> key name pub keys: BTreeMap, } impl Default for Settings { fn default() -> Settings { Settings { language: "en".into(), volume: 0.7, music_volume: 0.6, muted: false, rotate_speed: 1.0, zoom_speed: 1.0, edge_scroll: true, invert_mouse: false, outlines: true, wall_detail: 2, max_lights: 24, ui_factor: 0, render_scale: 1, touch_ui: 0, game_speed: 1.0, hints: true, map_w: 64, map_h: 64, keys: BTreeMap::new(), } } } impl Settings { pub fn key(&self, a: Action) -> KeyCode { self.keys.get(&a.id()).and_then(|n| key_from_name(n)).unwrap_or_else(|| a.default_key()) } // Bind a key; if another action already uses it, the two actions swap keys pub fn bind(&mut self, a: Action, k: KeyCode) { let old = self.key(a); for other in all_actions() { if other != a && self.key(other) == k { self.keys.insert(other.id(), key_name(old)); } } self.keys.insert(a.id(), key_name(k)); } pub fn reset_keys(&mut self) { self.keys.clear(); } pub fn wall_subdivisions(&self) -> usize { match self.wall_detail { 1 => 2, 3 => 4, _ => 3, } } pub fn to_json(&self) -> String { serde_json::to_string(self).unwrap_or_default() } pub fn from_json(s: &str) -> Option { serde_json::from_str(s).ok() } }