diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 53aab1884..cae85bc6c 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -433,9 +433,24 @@ impl Game<'_> { } else { (TriggerFeel::Free, TriggerFeel::Free) }; + // The lightbar follows the player; menus keep the last colour. + let in_world = self.title.is_none(); + let lightbar = match self.player.as_mut().filter(|_| in_world) { + Some(p) => { + let max = p.attribute("minecraft:max_health").unwrap_or(20.0) as f32; + let health = p.health.health() / max.max(1.0); + self.settings + .controller + .lightbar + .colour(health, self.dim.id()) + } + None => self.pad.shown.and_then(|s| s.lightbar), + }; + let lights = self.settings.controller.lightbar != gamepad::Lightbar::Off; let state = horizon_dualsense::device::State { triggers: self.settings.controller.trigger_effects(using, attacking), - ..Default::default() + lightbar: lightbar.filter(|_| lights), + player_leds: lights.then_some(horizon_dualsense::PLAYER_1), }; if self.pad.shown != Some(state) && let Some(output) = &self.pad.output diff --git a/crates/horizon-client/src/gamepad.rs b/crates/horizon-client/src/gamepad.rs index c1be6a90b..a250f5e8c 100644 --- a/crates/horizon-client/src/gamepad.rs +++ b/crates/horizon-client/src/gamepad.rs @@ -387,6 +387,7 @@ pub struct PadSettings { pub vibration: bool, /// A DualSense's triggers push back (Horizon's; docs/controllers.md). pub adaptive_triggers: bool, + pub lightbar: Lightbar, /// 0..1; 0.5 is the default speed, 1 twice that. pub look_sensitivity: f32, pub invert_x: bool, @@ -410,6 +411,7 @@ impl Default for PadSettings { enabled: true, vibration: true, adaptive_triggers: true, + lightbar: Lightbar::Health, look_sensitivity: 0.5, invert_x: false, invert_y: false, @@ -464,6 +466,7 @@ impl PadSettings { "enabled" => s.enabled = flag(s.enabled), "vibration" => s.vibration = flag(s.vibration), "adaptiveTriggers" => s.adaptive_triggers = flag(s.adaptive_triggers), + "lightbar" => s.lightbar = Lightbar::parse(value), "lookSensitivity" => s.look_sensitivity = unit(s.look_sensitivity), "invertX" => s.invert_x = flag(s.invert_x), "invertY" => s.invert_y = flag(s.invert_y), @@ -494,10 +497,11 @@ impl PadSettings { } pub fn serialize(&self) -> String { let mut text = format!( - "enabled:{}\nvibration:{}\nadaptiveTriggers:{}\nlookSensitivity:{}\ninvertX:{}\ninvertY:{}\nmoveDeadzone:{}\nlookDeadzone:{}\ntriggerThreshold:{}\ntoggleSneak:{}\nglyphs:{}\nmenuNavigation:{}\nkeyboard:{}\n", + "enabled:{}\nvibration:{}\nadaptiveTriggers:{}\nlightbar:{}\nlookSensitivity:{}\ninvertX:{}\ninvertY:{}\nmoveDeadzone:{}\nlookDeadzone:{}\ntriggerThreshold:{}\ntoggleSneak:{}\nglyphs:{}\nmenuNavigation:{}\nkeyboard:{}\n", bool_text(self.enabled), bool_text(self.vibration), bool_text(self.adaptive_triggers), + self.lightbar.name(), self.look_sensitivity, bool_text(self.invert_x), bool_text(self.invert_y), @@ -1063,6 +1067,62 @@ impl PadSettings { }) } } +/// What a DualSense's lightbar shows (`lightbar:`). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Lightbar { + /// Green at full health through amber to red. + Health, + /// A colour per dimension. + Dimension, + /// The pad's own. + Off, +} +impl Lightbar { + pub const ALL: [Lightbar; 3] = [Lightbar::Health, Lightbar::Dimension, Lightbar::Off]; + pub fn name(self) -> &'static str { + match self { + Lightbar::Health => "health", + Lightbar::Dimension => "dimension", + Lightbar::Off => "off", + } + } + pub fn parse(name: &str) -> Self { + match name.trim() { + "dimension" => Lightbar::Dimension, + "off" => Lightbar::Off, + _ => Lightbar::Health, + } + } + /// The colour for a player with this share of their health in this dimension; at half + /// brightness, as the pad's full light is glaring. + pub fn colour(self, health: f32, dimension: &str) -> Option<[u8; 3]> { + let rgb = match self { + Lightbar::Off => return None, + Lightbar::Health => { + // Green to amber over the top half, amber to red below it. + let (green, amber, red) = ( + [34.0, 197.0, 94.0], + [245.0, 158.0, 11.0], + [239.0, 68.0, 68.0], + ); + let h = health.clamp(0.0, 1.0); + let (from, to, k) = if h >= 0.5 { + (amber, green, (h - 0.5) * 2.0) + } else { + (red, amber, h * 2.0) + }; + [0, 1, 2].map(|i| from[i] + (to[i] - from[i]) * k) + } + Lightbar::Dimension => match dimension { + "minecraft:the_nether" => [234.0, 88.0, 12.0], + "minecraft:the_end" => [139.0, 92.0, 246.0], + _ => [14.0, 165.0, 233.0], + }, + }; + Some(rgb.map(|v| (v * 0.5).round() as u8)) + } +} + /// `BowItem.getPowerForTime`: a bow's draw after this many ticks, 0..1. pub fn bow_power(ticks: u32) -> f32 { let f = ticks as f32 / 20.0; @@ -1389,6 +1449,23 @@ mod tests { assert!(!PadSettings::parse(&s.serialize()).adaptive_triggers); } + #[test] + fn lightbar_colours() { + let full = Lightbar::Health.colour(1.0, "minecraft:overworld").unwrap(); + let empty = Lightbar::Health.colour(0.0, "minecraft:overworld").unwrap(); + assert!(full[1] > full[0], "green at full health: {full:?}"); + assert!(empty[0] > empty[1], "red when dying: {empty:?}"); + assert_eq!(Lightbar::Health.colour(0.5, ""), Some([123, 79, 6])); + assert_ne!( + Lightbar::Dimension.colour(1.0, "minecraft:the_nether"), + Lightbar::Dimension.colour(1.0, "minecraft:overworld") + ); + assert_eq!(Lightbar::Off.colour(1.0, ""), None); + for l in Lightbar::ALL { + assert_eq!(Lightbar::parse(l.name()), l); + } + } + #[test] fn bow_draw() { assert_eq!(bow_power(0), 0.0); diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index 013333332..4c67f0688 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -102,6 +102,7 @@ pub const SECTIONS: &[Section] = &[ Opt::PadEnabled, Opt::PadVibration, Opt::PadAdaptiveTriggers, + Opt::PadLightbar, Opt::PadLookSensitivity, Opt::PadToggleSneak, Opt::PadInvertX, diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 5852a61b4..775fe2002 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -72,6 +72,7 @@ pub enum Opt { PadEnabled, PadVibration, PadAdaptiveTriggers, + PadLightbar, PadLookSensitivity, PadInvertX, PadInvertY, @@ -171,6 +172,7 @@ impl Opt { Opt::PadEnabled => "Controller", Opt::PadVibration => "Vibration", Opt::PadAdaptiveTriggers => "Adaptive Triggers", + Opt::PadLightbar => "Lightbar", Opt::PadLookSensitivity => "Look Sensitivity", Opt::PadInvertX => "Invert Look X", Opt::PadInvertY => "Invert Look Y", @@ -433,6 +435,11 @@ impl Opt { } else { "options.key.hold" }), + Opt::PadLightbar => Text::literal(match s.controller.lightbar { + gamepad::Lightbar::Health => "Health", + gamepad::Lightbar::Dimension => "Dimension", + gamepad::Lightbar::Off => "Off", + }), Opt::PadGlyphs => Text::literal(match s.controller.glyphs { GlyphStyle::Auto => "Auto", GlyphStyle::Xbox => "Xbox", @@ -575,6 +582,14 @@ impl Opt { Opt::PadInvertX => s.controller.invert_x = !s.controller.invert_x, Opt::PadInvertY => s.controller.invert_y = !s.controller.invert_y, Opt::PadToggleSneak => s.controller.toggle_sneak = !s.controller.toggle_sneak, + Opt::PadLightbar => { + let all = gamepad::Lightbar::ALL; + let i = all + .iter() + .position(|l| *l == s.controller.lightbar) + .unwrap_or(0) as u8; + s.controller.lightbar = all[usize::from(next(i, 3))]; + } Opt::PadGlyphs => { let i = GlyphStyle::ALL .iter() @@ -1035,6 +1050,7 @@ impl OptionsUi { Opt::PadEnabled, Opt::PadVibration, Opt::PadAdaptiveTriggers, + Opt::PadLightbar, Opt::PadLookSensitivity, Opt::PadToggleSneak, Opt::PadInvertX, diff --git a/crates/horizon-dualsense/src/device.rs b/crates/horizon-dualsense/src/device.rs index 2ed168292..97c6ec72b 100644 --- a/crates/horizon-dualsense/src/device.rs +++ b/crates/horizon-dualsense/src/device.rs @@ -1,6 +1,8 @@ //! The pad over hidapi, and the thread that writes to it: writes block (a Bluetooth report can //! take milliseconds), so the game only hands over what the pad should do. -use crate::{Output, TRIGGER_OFF, TriggerEffect, Vibration, is_dualsense}; +use crate::{ + DEFAULT_LIGHTBAR, Output, PLAYER_1, TRIGGER_OFF, TriggerEffect, Vibration, is_dualsense, +}; use hidapi::{BusType, HidApi, HidDevice, HidResult}; use std::sync::Arc; use std::sync::atomic::{AtomicBool, AtomicU8, Ordering}; @@ -303,7 +305,13 @@ impl Writer { self.flush(); } if let Some(pad) = self.pad.as_mut() { - let _ = pad.write(&Output::release()); + // The lights go back to a host's defaults, if they were changed. + let release = Output { + lightbar: self.state.lightbar.map(|_| DEFAULT_LIGHTBAR), + player_leds: self.state.player_leds.map(|_| PLAYER_1), + ..Output::release() + }; + let _ = pad.write(&release); } } diff --git a/crates/horizon-dualsense/src/lib.rs b/crates/horizon-dualsense/src/lib.rs index 227fba6b0..2027b4584 100644 --- a/crates/horizon-dualsense/src/lib.rs +++ b/crates/horizon-dualsense/src/lib.rs @@ -61,6 +61,10 @@ const FLAG2_VIBRATION_V2: u8 = 1 << 2; const LIGHTBAR_LIGHT_OUT: u8 = 1 << 1; /// Player LEDs: switch at once instead of fading. pub const PLAYER_LEDS_INSTANT: u8 = 1 << 5; +/// The middle LED: player 1. +pub const PLAYER_1: u8 = 1 << 2; +/// The blue a host gives a first pad (`hid-playstation.c`), restored when Horizon lets go. +pub const DEFAULT_LIGHTBAR: [u8; 3] = [0, 0, 128]; /// An adaptive trigger's effect: a mode byte and ten parameters. pub type TriggerEffect = [u8; 11]; diff --git a/docs/controllers.md b/docs/controllers.md index 095914a02..ec0015790 100644 --- a/docs/controllers.md +++ b/docs/controllers.md @@ -220,6 +220,7 @@ exactly vanilla's, so a vanilla client can share the game folder. enabled:true vibration:true adaptiveTriggers:true +lightbar:health lookSensitivity:0.5 invertX:false invertY:false @@ -233,7 +234,7 @@ bind_pad.hotbarNext:rb ``` Only bindings that differ from the defaults are written. The controls -options get a Controller page: on/off, vibration, adaptive triggers, look sensitivity, invert +options get a Controller page: on/off, vibration, adaptive triggers, lightbar, look sensitivity, invert X/Y, deadzones, glyph style, sneak toggle, and a binding list like the key binds screen (pick an action, press a button; conflicts marked as vanilla does; reset). @@ -436,6 +437,17 @@ done: there the motors' emulation is used. HD haptics are opt-in for now games started for tests (`HORIZON_NO_AUDIO`) never open them, and the pad's audio device is only ever opened by name, never the system's default output. +### Lightbar and player LEDs + +"Lightbar: Health / Dimension / Off" in the controller options +(`lightbar:`, health by default). Health runs from green at full through +amber at half to red; Dimension is blue in the Overworld, orange in the +Nether, violet in the End. Both at half brightness (the pad's full light is +glaring); menus keep the last colour. The player LEDs show player 1 (the +middle LED). Off leaves both to the pad. When Horizon lets go of the pad, a +lightbar or LEDs it changed go back to a host's defaults: blue (0, 0, 128) +and the middle LED, as Linux's driver gives a first pad. + Byte layout from public references, read for facts; no code is copied from them (the kernel and dualsensectl are GPL):