From b2f0af559b3244dc07caedd1cfcbd33b0cc92bb1 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:34:18 -0400 Subject: [PATCH] Accept and handle mouse bindings for Extras --- crates/horizon-client/src/game.rs | 3 + crates/horizon-client/src/game/extras.rs | 6 +- crates/horizon-client/src/game/options.rs | 11 +-- crates/horizon-client/src/game/tweaks.rs | 10 ++- crates/horizon-client/src/options_horizon.rs | 11 ++- crates/horizon-client/src/options_ui.rs | 83 +++++++++++++++++--- 6 files changed, 100 insertions(+), 24 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index e552eb118..4496e6e73 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -9360,6 +9360,9 @@ impl ApplicationHandler for Game<'_> { } WindowEvent::MouseInput { state, button, .. } => { let pressed = state == ElementState::Pressed; + if pressed && self.sort_mouse_press(button) { + return Ok(()); + } match self.screen { Screen::Playing => { if pressed && !self.captured { diff --git a/crates/horizon-client/src/game/extras.rs b/crates/horizon-client/src/game/extras.rs index c6ac6d25e..712796a81 100644 --- a/crates/horizon-client/src/game/extras.rs +++ b/crates/horizon-client/src/game/extras.rs @@ -58,8 +58,12 @@ impl Game<'_> { pub(super) fn extras_scroll(&mut self, steps: f32) -> bool { self.settings.extras.zoom && self.extras.zoom.scroll(steps) } - /// Mouse buttons while playing, for the keystroke overlay. + /// Mouse buttons while playing, for zoom and the keystroke overlay. pub(super) fn extras_mouse(&mut self, button: MouseButton, pressed: bool) { + let e = &self.settings.extras; + if e.zoom && e.zoom_key == keybinds::mouse_name(button) { + self.extras.zoom.key(pressed, false, true, e.zoom_level); + } let i = match button { MouseButton::Left => 0, MouseButton::Right => 1, diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index dcc7bb23b..e8057cf31 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -186,16 +186,11 @@ impl Game<'_> { } /// Mouse buttons pressed while a key is being bound. pub(super) fn options_waiting_for_key(&self) -> bool { - self.ui.options.selected_key.is_some() && self.ui.options.page() == Page::KeyBinds + self.ui.options.selected_key.is_some() } pub(super) fn options_bind_mouse(&mut self, button: MouseButton) -> Result<()> { - if let Some(name) = self.ui.options.selected_key.take() { - self.settings - .keys - .set(name, &horizon_client::keybinds::mouse_name(button)); - self.handle_option_events(vec![Event::Changed])?; - } - Ok(()) + let events = self.ui.options.bind_mouse(&mut self.settings, button); + self.handle_option_events(events) } pub(super) fn handle_option_events(&mut self, events: Vec) -> Result<()> { for e in events { diff --git a/crates/horizon-client/src/game/tweaks.rs b/crates/horizon-client/src/game/tweaks.rs index dc87a13bf..018b6640b 100644 --- a/crates/horizon-client/src/game/tweaks.rs +++ b/crates/horizon-client/src/game/tweaks.rs @@ -28,7 +28,7 @@ use horizon_client::{ menu::Input, stations::hover_name, }; -use winit::keyboard::KeyCode; +use winit::{event::MouseButton, keyboard::KeyCode}; /// A sort button's side in GUI pixels. const SORT_BUTTON: f32 = 9.0; @@ -377,8 +377,14 @@ impl Game<'_> { /// The sort key: sorts the part under the cursor (the container when /// the cursor is over no slot). pub(super) fn sort_key_press(&mut self, key: KeyCode) -> bool { + keybinds::key_name(key).is_some_and(|name| self.sort_input(name)) + } + pub(super) fn sort_mouse_press(&mut self, button: MouseButton) -> bool { + self.sort_input(&keybinds::mouse_name(button)) + } + fn sort_input(&mut self, name: &str) -> bool { let e = &self.settings.extras; - if !e.sort || keybinds::key_code(&e.sort_key) != Some(key) || self.recipe_search_focused() { + if !e.sort || e.sort_key != name || self.recipe_search_focused() { return false; } let buttons = self.sort_buttons(); diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index bfaf05c9b..634bb8d75 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -587,9 +587,16 @@ impl OptionsUi { } return events; } - // A click anywhere stops waiting for an extra's key. + // Rebinding consumes the click before sidebar or widget actions. if matches!(self.selected_key, Some(extras::ZOOM_KEY | extras::SORT_KEY)) { - self.selected_key = None; + return self.bind_mouse( + s, + if right { + winit::event::MouseButton::Right + } else { + winit::event::MouseButton::Left + }, + ); } match self.horizon_target(s, atlas, x, y) { Some(Target::Nav(i)) => { diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 38be29576..6606d31fd 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -1876,15 +1876,13 @@ impl OptionsUi { return events; } // Key Binds: a mouse button pressed while waiting binds it. - if let Some(name) = self.selected_key.take() { + if self.selected_key.is_some() { let button = if right { - "key.mouse.right" + winit::event::MouseButton::Right } else { - "key.mouse.left" + winit::event::MouseButton::Left }; - s.keys.set(name, button); - events.push(Event::Changed); - return events; + return self.bind_mouse(s, button); } // AbstractWidget accepts only the primary button. Rebinding above // accepts other mouse buttons, but is not a widget activation. @@ -2126,6 +2124,23 @@ impl OptionsUi { } false } + fn bind_key(&mut self, s: &mut Settings, value: String) -> Vec { + let Some(name) = self.selected_key.take() else { + return Vec::new(); + }; + if !s.extras.set_key(name, value.clone()) { + s.keys.set(name, &value); + } + vec![Event::Changed] + } + /// Mouse bindings use the same settings and persistence as keyboard bindings. + pub fn bind_mouse( + &mut self, + s: &mut Settings, + button: winit::event::MouseButton, + ) -> Vec { + self.bind_key(s, keybinds::mouse_name(button)) + } /// Keys while the options are open. Returns events; `handled` false lets /// Escape fall through. pub fn key( @@ -2160,7 +2175,7 @@ impl OptionsUi { } return vec![Event::Changed]; } - if let Some(name) = self.selected_key.take() { + if self.selected_key.is_some() { // KeyBindsScreen.keyPressed: Escape unbinds. let value = if key == K::Escape { "key.keyboard.unknown".to_owned() @@ -2169,10 +2184,7 @@ impl OptionsUi { .unwrap_or("key.keyboard.unknown") .to_owned() }; - if !s.extras.set_key(name, value.clone()) { - s.keys.set(name, &value); - } - return vec![Event::Changed]; + return self.bind_key(s, value); } if key == K::Escape { return self.back(s); @@ -2831,6 +2843,55 @@ mod tests { } } + #[test] + fn extras_and_vanilla_bindings_accept_mouse_buttons() { + use crate::extras::{Extras, SORT_KEY, ZOOM_KEY}; + use winit::event::MouseButton; + for button in [ + MouseButton::Left, + MouseButton::Right, + MouseButton::Middle, + MouseButton::Back, + MouseButton::Forward, + ] { + for name in [SORT_KEY, ZOOM_KEY, "key.jump"] { + let mut ui = OptionsUi::default(); + let mut s = Settings::default(); + ui.selected_key = Some(name); + assert_eq!(ui.bind_mouse(&mut s, button), vec![Event::Changed]); + assert_eq!(ui.selected_key, None); + let value = keybinds::mouse_name(button); + match name { + SORT_KEY => assert_eq!(s.extras.sort_key, value), + ZOOM_KEY => assert_eq!(s.extras.zoom_key, value), + _ => assert_eq!(s.keys.get(name), value), + } + assert_eq!(Extras::parse(&s.extras.serialize()), s.extras); + if name != "key.jump" { + assert_eq!(s.keys, Settings::default().keys); + } + assert!(ui.bind_mouse(&mut s, button).is_empty()); + } + } + } + + #[test] + fn horizon_extra_mouse_binding_consumes_the_click() { + let atlas = atlas(); + let mut s = Settings::default(); + let mut ui = OptionsUi::default(); + for (right, expected) in [(false, "key.mouse.left"), (true, "key.mouse.right")] { + ui.selected_key = Some(crate::extras::SORT_KEY); + assert_eq!( + ui.click_horizon(&mut s, &atlas, 2.0, 2.0, [960, 480], false, right), + vec![Event::Changed] + ); + assert_eq!(s.extras.sort_key, expected); + assert_eq!(ui.selected_key, None); + assert_eq!(ui.section, 0); + } + } + #[test] fn key_rebinding_takes_precedence_over_scrollbar_dragging() { let atlas = atlas();