diff --git a/bindings.ini b/bindings.ini index 6c1170e6..11f953b2 100644 --- a/bindings.ini +++ b/bindings.ini @@ -1,6 +1,8 @@ # Edit while the game is closed, or save for your next launch. # Multiple keys for one button are separated by commas. Mouse1 = left, Mouse2 = right. # A-Z, 0-9, named keys below, or Windows virtual key codes such as 0xA2. +# WheelUp and WheelDown are the mouse wheel: each notch is one short press (about 70 ms, +# 40 ms apart when queued), e.g. "DpadUp = Up, WheelUp". Bound to nothing by default. # Sensitivity is stick deflection per cursor pixel over an 8 ms polling interval. Sensitivity = 0.135 MoveUp = W diff --git a/crates/victory-hle/src/modules/lifecycle.rs b/crates/victory-hle/src/modules/lifecycle.rs index 05716cbd..03d8a912 100644 --- a/crates/victory-hle/src/modules/lifecycle.rs +++ b/crates/victory-hle/src/modules/lifecycle.rs @@ -527,6 +527,10 @@ extern "system" fn window_proc( if message == wm::WM_SETFOCUS { english_input(); } + if message == wm::WM_MOUSEWHEEL { + // The wheel as WheelUp / WheelDown in the mouse and keyboard bindings. + super::gameinput::mnk::wheel_scroll(hwnd, ((wparam >> 16) as u16 as i16).into()); + } match message { wm::WM_KILLFOCUS | wm::WM_NCDESTROY => capture_mouse(hwnd, false), wm::WM_ACTIVATE => { diff --git a/crates/victory-hle/src/modules/mnk.rs b/crates/victory-hle/src/modules/mnk.rs index 16a0cea4..9d480f95 100644 --- a/crates/victory-hle/src/modules/mnk.rs +++ b/crates/victory-hle/src/modules/mnk.rs @@ -28,6 +28,89 @@ const BUTTONS: &[(&str, u32)] = &[ ]; const AXES: [&str; 6] = ["LT", "RT", "MoveLeft", "MoveRight", "MoveDown", "MoveUp"]; +/// The mouse wheel as two pseudo virtual keys. Real keyboards have no VK 0x0E or 0x0F (both +/// unassigned, above the mouse buttons 0x01-0x06), so they cannot collide with a key. +const WHEEL_UP: u8 = 0x0E; +const WHEEL_DOWN: u8 = 0x0F; +/// One wheel notch, as `WM_MOUSEWHEEL` counts (`WHEEL_DELTA`). +const WHEEL_DELTA: i32 = 120; +/// A notch presses its pseudo key this long, then lets go for `WHEEL_GAP_MS`: a few polls of +/// a title that reads the pad once a frame, so a press is not missed, and notches queued +/// behind each other are separate presses. +const WHEEL_PULSE_MS: u64 = 70; +const WHEEL_GAP_MS: u64 = 40; +/// Notches waiting to be pressed, at most: a wild spin is not played back for seconds. +const WHEEL_QUEUE_MAX: usize = 16; + +/// Turns wheel deltas into key presses. A wheel has no held state, so each whole notch is one +/// short press of its pseudo key, played one after another. +#[derive(Default)] +struct Wheel { + /// Delta short of a whole notch (smooth-scroll wheels and touchpads send fractions). + rest: i32, + queue: std::collections::VecDeque, + /// The key being pressed, and until when (ms). + now: Option<(u8, u64)>, + /// No new press before this time (ms). + free_at: u64, +} + +impl Wheel { + /// Takes a wheel delta (positive: away from the user, up) and queues the notches it completes. + /// A turn the other way drops what was left of the first. + fn scroll(&mut self, delta: i32) { + if delta != 0 && self.rest != 0 && (delta < 0) != (self.rest < 0) { + self.rest = 0; + } + self.rest = self.rest.saturating_add(delta); + let key = if self.rest > 0 { WHEEL_UP } else { WHEEL_DOWN }; + let n = (self.rest / WHEEL_DELTA).unsigned_abs() as usize; + self.rest %= WHEEL_DELTA; + for _ in 0..n { + if self.queue.len() < WHEEL_QUEUE_MAX { + self.queue.push_back(key); + } + } + } + + /// The pseudo key pressed at time `ms` (rising), if any. + fn poll(&mut self, ms: u64) -> Option { + if let Some((key, until)) = self.now { + if ms < until { + return Some(key); + } + self.now = None; + self.free_at = until + WHEEL_GAP_MS; + } + if ms >= self.free_at + && let Some(key) = self.queue.pop_front() + { + self.now = Some((key, ms + WHEEL_PULSE_MS)); + return Some(key); + } + None + } +} + +fn wheel() -> std::sync::MutexGuard<'static, Wheel> { + static WHEEL: OnceLock> = OnceLock::new(); + WHEEL + .get_or_init(Default::default) + .lock() + .unwrap_or_else(|e| e.into_inner()) +} + +/// A `WM_MOUSEWHEEL` delta from the window procedure. Only counted while mouse and keyboard +/// are on, the game window has the focus and F1 is closed. +pub(super) fn wheel_scroll(hwnd: HWND, delta: i32) { + if enabled() + && !victory_gpu::SETTINGS_MENU_OPEN.load(Relaxed) + && unsafe { wm::GetForegroundWindow() } == hwnd + { + wheel().scroll(delta); + } +} + struct Bindings { buttons: Vec<(u8, u32)>, axes: [Vec; 6], @@ -50,6 +133,8 @@ fn key(name: &str) -> Option { "MOUSE3" => 4, "MOUSE4" => 5, "MOUSE5" => 6, + "WHEELUP" => WHEEL_UP, + "WHEELDOWN" => WHEEL_DOWN, "BACKSPACE" => 8, "TAB" => 9, "ENTER" => 13, @@ -226,6 +311,7 @@ pub(super) fn sample() -> Option { // F1 open: the mouse is left free and no key reaches the game. if victory_gpu::SETTINGS_MENU_OPEN.load(Relaxed) { *m = Mouse::default(); + *wheel() = Wheel::default(); return Some(PadState::default()); } let hwnd = unsafe { wm::GetForegroundWindow() }; @@ -233,6 +319,7 @@ pub(super) fn sample() -> Option { unsafe { wm::GetWindowThreadProcessId(hwnd, &mut pid) }; if hwnd.is_null() || pid != std::process::id() { *m = Mouse::default(); + *wheel() = Wheel::default(); return Some(PadState::default()); } if m.window != hwnd as usize { @@ -256,7 +343,13 @@ pub(super) fn sample() -> Option { m.update_look(delta, now, elapsed, b.sensitivity); m.last = Some(now); } - let held = |vk: u8| (unsafe { GetAsyncKeyState(vk.into()) }) < 0; + static START: OnceLock = OnceLock::new(); + let ms = now.duration_since(*START.get_or_init(|| now)).as_millis() as u64; + let wheel_key = wheel().poll(ms); + let held = |vk: u8| match vk { + WHEEL_UP | WHEEL_DOWN => wheel_key == Some(vk), + _ => (unsafe { GetAsyncKeyState(vk.into()) }) < 0, + }; let axes = b .axes .each_ref() @@ -305,4 +398,50 @@ mod tests { assert!(b.buttons.contains(&(b'F', Y))); assert!(parse("RT=NotAKey").is_err()); } + + #[test] + fn wheel_names_parse_and_bind() { + let b = parse("DpadUp = Up, WheelUp +DpadDown = wheeldown").unwrap(); + assert!(b.buttons.contains(&(WHEEL_UP, UP))); + assert!(b.buttons.contains(&(WHEEL_DOWN, DOWN))); + assert!(b.buttons.contains(&(38, UP))); + // No real key is 0x0E or 0x0F. + assert!(key("0x0E").is_some_and(|v| v == WHEEL_UP)); + } + + #[test] + fn wheel_notch_is_one_pulse_then_gap() { + let mut w = Wheel::default(); + w.scroll(120); + assert_eq!(w.poll(1000), Some(WHEEL_UP)); + assert_eq!(w.poll(1069), Some(WHEEL_UP)); + assert_eq!(w.poll(1070), None); + w.scroll(-240); + // Two queued notches are two presses, with a gap between. + assert_eq!(w.poll(1080), None); + assert_eq!(w.poll(1110), Some(WHEEL_DOWN)); + assert_eq!(w.poll(1179), Some(WHEEL_DOWN)); + assert_eq!(w.poll(1180), None); + assert_eq!(w.poll(1219), None); + assert_eq!(w.poll(1220), Some(WHEEL_DOWN)); + assert_eq!(w.poll(1290), None); + assert_eq!(w.poll(2000), None); + } + + #[test] + fn wheel_fractions_add_up_and_reversal_drops_them() { + let mut w = Wheel::default(); + w.scroll(60); + assert_eq!(w.poll(0), None); + w.scroll(60); + assert_eq!(w.poll(0), Some(WHEEL_UP)); + let mut w = Wheel::default(); + w.scroll(100); + w.scroll(-100); + assert_eq!(w.poll(0), None); + let mut w = Wheel::default(); + w.scroll(120 * 100); + assert_eq!(w.queue.len(), WHEEL_QUEUE_MAX); + } }