//! Controllers: Horizon's own feature (vanilla 26.3 has no gamepad input). //! The default layout is Bedrock Edition's; see docs/controllers.md. //! //! This module is the backend-free part: positional buttons, the action //! bindings, deadzones and the look curve, button edges from successive pad //! states, and `horizon-controller.txt`, kept next to `options.txt` so that //! file stays exactly vanilla's. The game reads the pads with gilrs. use std::{collections::BTreeMap, fs, path::Path}; pub const FILE: &str = "horizon-controller.txt"; /// A button by its position on the pad (gilrs' and SDL's layout): `South` /// is Xbox A, PlayStation ✕, Nintendo B. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum Button { South, East, West, North, LeftBumper, RightBumper, LeftTrigger, RightTrigger, Select, Start, LeftStick, RightStick, DPadUp, DPadDown, DPadLeft, DPadRight, } impl Button { pub const ALL: [Button; 16] = [ Button::South, Button::East, Button::West, Button::North, Button::LeftBumper, Button::RightBumper, Button::LeftTrigger, Button::RightTrigger, Button::Select, Button::Start, Button::LeftStick, Button::RightStick, Button::DPadUp, Button::DPadDown, Button::DPadLeft, Button::DPadRight, ]; /// The name in `horizon-controller.txt` and `horizon-ctl pad`. pub fn name(self) -> &'static str { match self { Button::South => "south", Button::East => "east", Button::West => "west", Button::North => "north", Button::LeftBumper => "lb", Button::RightBumper => "rb", Button::LeftTrigger => "lt", Button::RightTrigger => "rt", Button::Select => "select", Button::Start => "start", Button::LeftStick => "ls", Button::RightStick => "rs", Button::DPadUp => "up", Button::DPadDown => "down", Button::DPadLeft => "left", Button::DPadRight => "right", } } pub fn parse(name: &str) -> Option { Self::ALL.into_iter().find(|b| b.name() == name) } fn bit(self) -> u32 { 1 << self as u32 } /// What is printed on the button for a pad family (`Auto` reads as Xbox). pub fn label(self, style: GlyphStyle) -> &'static str { let i = self as usize; match style { GlyphStyle::PlayStation => [ "×", "○", "□", "△", "L1", "R1", "L2", "R2", "Create", "Options", "L3", "R3", "↑", "↓", "←", "→", ][i], GlyphStyle::Nintendo => [ "B", "A", "Y", "X", "L", "R", "ZL", "ZR", "−", "+", "LS", "RS", "↑", "↓", "←", "→", ][i], GlyphStyle::Xbox | GlyphStyle::Auto => [ "A", "B", "X", "Y", "LB", "RB", "LT", "RT", "View", "Menu", "LS", "RS", "↑", "↓", "←", "→", ][i], } } /// The glyph's colour (RGB): face buttons in their family's colours, /// the rest white. pub fn color(self, style: GlyphStyle) -> [u8; 3] { let face = match self { Button::South => 0, Button::East => 1, Button::West => 2, Button::North => 3, _ => return [255, 255, 255], }; match style { GlyphStyle::PlayStation => [ [124, 178, 232], [235, 110, 110], [222, 145, 205], [92, 205, 170], ][face], GlyphStyle::Nintendo => [255, 255, 255], GlyphStyle::Xbox | GlyphStyle::Auto => [ [110, 200, 90], [235, 90, 80], [80, 150, 240], [240, 200, 60], ][face], } } } /// A pad's inputs at one moment. Sticks are `[x, y]` with right and up /// positive; triggers run 0..1. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct PadState { /// Digital buttons as a bit set (triggers are in `triggers`). pub buttons: u32, pub left: [f32; 2], pub right: [f32; 2], pub triggers: [f32; 2], } impl PadState { pub fn set(&mut self, button: Button, down: bool) { if down { self.buttons |= button.bit(); } else { self.buttons &= !button.bit(); } } pub fn held(&self, button: Button) -> bool { self.buttons & button.bit() != 0 } /// Two sources at once (a real pad and `horizon-ctl pad`): buttons held on /// either, sticks and triggers added. pub fn merge(&self, other: &PadState) -> PadState { let add = |a: [f32; 2], b: [f32; 2]| { [ (a[0] + b[0]).clamp(-1.0, 1.0), (a[1] + b[1]).clamp(-1.0, 1.0), ] }; PadState { buttons: self.buttons | other.buttons, left: add(self.left, other.left), right: add(self.right, other.right), triggers: [ (self.triggers[0] + other.triggers[0]).clamp(0.0, 1.0), (self.triggers[1] + other.triggers[1]).clamp(0.0, 1.0), ], } } /// Anything pushed past the deadzones (the pad is in use). pub fn active(&self, settings: &PadSettings) -> bool { self.buttons != 0 || length(self.left) > settings.move_deadzone || length(self.right) > settings.look_deadzone || self .triggers .iter() .any(|t| *t >= settings.trigger_threshold) } } /// Button presses and releases between successive states. Triggers count as /// buttons past `threshold`, with a little hysteresis so a trigger resting /// near the threshold doesn't chatter. #[derive(Clone, Debug, Default)] pub struct Edges { held: u32, } impl Edges { pub fn update(&mut self, state: &PadState, threshold: f32) -> Vec<(Button, bool)> { let mut now = state.buttons; for (trigger, button) in [(0, Button::LeftTrigger), (1, Button::RightTrigger)] { let was = self.held & button.bit() != 0; let value = state.triggers[trigger]; let on = if was { value >= (threshold - 0.1).max(0.05) } else { value >= threshold }; if on { now |= button.bit(); } else { now &= !button.bit(); } } let changed = now ^ self.held; self.held = now; Button::ALL .into_iter() .filter(|b| changed & b.bit() != 0) .map(|b| (b, now & b.bit() != 0)) .collect() } /// Release everything (the pad was unplugged or turned off). pub fn release_all(&mut self) -> Vec<(Button, bool)> { let held = std::mem::take(&mut self.held); Button::ALL .into_iter() .filter(|b| held & b.bit() != 0) .map(|b| (b, false)) .collect() } pub fn is_held(&self, button: Button) -> bool { self.held & button.bit() != 0 } } /// Horizon's pad-only actions (no vanilla key mapping). pub const HOTBAR_PREVIOUS: &str = "pad.hotbarPrevious"; pub const HOTBAR_NEXT: &str = "pad.hotbarNext"; /// Bindable actions and their default buttons: vanilla key mappings (the /// game acts as their default key) and Horizon's pad-only actions. Start is /// always pause / back and isn't listed. pub const ACTIONS: &[(&str, Option