From 9927d06049c27fcf0c4da50fca901be80f4daf7e Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 00:41:42 +0200 Subject: [PATCH] Controllers: focus navigation in menus, on-screen keyboard, menu navigation option Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game/books.rs | 9 +- crates/horizon-client/src/game/lan_screen.rs | 27 +- crates/horizon-client/src/game/pad.rs | 15 +- crates/horizon-client/src/game/pad_menu.rs | 714 +++++++++++++++++-- crates/horizon-client/src/game/remote.rs | 33 +- crates/horizon-client/src/game/sign_edit.rs | 21 +- crates/horizon-client/src/gamepad.rs | 300 +++++++- crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/multiplayer.rs | 8 + crates/horizon-client/src/options_horizon.rs | 2 + crates/horizon-client/src/options_ui.rs | 30 + crates/horizon-client/src/osk.rs | 412 +++++++++++ crates/horizon-client/src/screen_draw.rs | 9 + crates/horizon-client/src/title.rs | 8 + crates/horizon-client/src/ui.rs | 12 +- crates/horizon-client/src/ui_theme.rs | 40 +- crates/horizon-client/src/widgets.rs | 85 ++- 17 files changed, 1596 insertions(+), 130 deletions(-) create mode 100644 crates/horizon-client/src/osk.rs diff --git a/crates/horizon-client/src/game/books.rs b/crates/horizon-client/src/game/books.rs index dc5c7cdf1..7bf687899 100644 --- a/crates/horizon-client/src/game/books.rs +++ b/crates/horizon-client/src/game/books.rs @@ -139,6 +139,11 @@ impl Game<'_> { PhysicalKey::Code(k) => Some(k), _ => None, }; + self.book_input(key, event.text.as_deref()); + } + /// A key and the text it typed in the open book (the keyboard, or a + /// controller's on-screen keyboard). + pub(super) fn book_input(&mut self, key: Option, text: Option<&str>) { match self.book.as_mut() { Some(BookScreen::View { .. }) => match key { Some(KeyCode::Escape) => self.close_book(), @@ -167,7 +172,7 @@ impl Game<'_> { } } _ => { - if let Some(typed) = event.text.as_ref() { + if let Some(typed) = text { self.text_selection.take(BOOK_TITLE, title); for c in typed.chars().filter(|c| !c.is_control()) { if title.chars().count() < 15 { @@ -193,7 +198,7 @@ impl Game<'_> { self.type_into_page("\n") } _ => { - if let Some(typed) = event.text.as_ref().map(|t| t.to_string()) { + if let Some(typed) = text { let typed: String = typed.chars().filter(|c| !c.is_control()).collect(); if !typed.is_empty() { self.take_page_selection(); diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs index c36ce5d84..f0eb57b3e 100644 --- a/crates/horizon-client/src/game/lan_screen.rs +++ b/crates/horizon-client/src/game/lan_screen.rs @@ -333,13 +333,26 @@ impl Game<'_> { o.port.shortcut(shortcut, &mut self.clipboard); return; } - if !o.port.key(key, ctrl, false) - && !ctrl - && let Some(text) = &event.text - { - let digits: String = text.chars().filter(char::is_ascii_digit).collect(); - if !digits.is_empty() { - o.port.insert_text(&digits); + if !o.port.key(key, ctrl, false) && !ctrl { + self.world_options_text(event.text.as_deref()); + } + } + /// Digits typed into the port field (the keyboard, or a controller's + /// on-screen keyboard), and its backspace. + pub(super) fn world_options_text(&mut self, text: Option<&str>) { + let Some(o) = self.world_options.as_mut().filter(|o| o.port.focused) else { + return; + }; + match text { + Some(text) => { + let digits: String = text.chars().filter(char::is_ascii_digit).collect(); + if !digits.is_empty() { + o.port.insert_text(&digits); + } + } + None => { + o.port + .key(winit::keyboard::KeyCode::Backspace, false, false); } } } diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 2f050fdec..111567117 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -255,7 +255,7 @@ impl Game<'_> { if down && !self.pad_focused() { return Ok(()); } - let playing = self.title.is_none() && self.screen == Screen::Playing; + let playing = !self.pad_in_menu(); if down && playing && !self.captured && self.focused { // Like a click into the window: take the pointer. self.capture(true); @@ -340,11 +340,7 @@ impl Game<'_> { /// The left stick as the move vector, while playing. pub(super) fn pad_move(&self) -> Option<[f32; 2]> { let settings = &self.settings.controller; - if !settings.enabled - || self.title.is_some() - || self.screen != Screen::Playing - || !self.pad_focused() - { + if !settings.enabled || self.pad_in_menu() || !self.pad_focused() { return None; } gamepad::move_vector(self.pad.state.left, settings.move_deadzone) @@ -353,12 +349,7 @@ impl Game<'_> { /// The right stick turns the view, once per frame (`Entity.turn`). pub(super) fn pad_look(&mut self, seconds: f32) { let settings = &self.settings.controller; - if !settings.enabled - || self.title.is_some() - || self.screen != Screen::Playing - || self.player.is_none() - || !self.pad_focused() - { + if !settings.enabled || self.pad_in_menu() || self.player.is_none() || !self.pad_focused() { return; } // A long frame (a hitch) must not spin the view. diff --git a/crates/horizon-client/src/game/pad_menu.rs b/crates/horizon-client/src/game/pad_menu.rs index 8047e889a..a321697ef 100644 --- a/crates/horizon-client/src/game/pad_menu.rs +++ b/crates/horizon-client/src/game/pad_menu.rs @@ -1,17 +1,29 @@ //! A controller in menus, screens and inventories (Horizon's own; -//! docs/controllers.md): a virtual cursor the left stick moves and the -//! D-pad steps between slots and buttons, with A, X and the stick feeding -//! the screens the same pointer events a mouse sends. Every screen keeps -//! its mouse behaviour (drags, sliders, scrollbars) with no pad code of its -//! own. +//! docs/controllers.md). Two ways to drive them, picked in the controller +//! options: +//! +//! - **Focus** (console-style): a highlight steps between the widgets, list +//! rows, fields and slots each screen registers (`widgets::target_as`) +//! with vanilla's arrow-key navigation; A presses the focused one, the +//! bumpers switch tabs, left/right move a focused slider, A on a text field +//! opens the on-screen keyboard (`osk`). +//! - **Pointer**: a virtual cursor the left stick moves and the D-pad steps +//! between slots and buttons. +//! +//! Both feed the screens the same pointer events a mouse sends (the pointer +//! sits on the focused widget, hidden), so every screen keeps its mouse +//! behaviour (drags, sliders, scrollbars, hover highlights) with no pad code +//! of its own. A screen with nothing registered falls back to the pointer, +//! and a right stick click swaps modes for the open screen. use super::{Game, Screen}; use horizon_client::{ Result, creative::Creative, - gamepad::{self, Button}, + gamepad::{self, Button, MenuNav, Rect}, hud::{HudBatch, rgba}, options_ui::{Event, Page}, - widgets, + osk::{self, Keyboard}, + widgets::{self, Kind, Target}, }; use std::time::Instant; use winit::{ @@ -19,7 +31,7 @@ use winit::{ dpi::PhysicalPosition, event::{DeviceId, ElementState, MouseButton, MouseScrollDelta, TouchPhase, WindowEvent}, event_loop::ActiveEventLoop, - keyboard::KeyCode, + keyboard::{KeyCode, NamedKey}, }; /// D-pad auto-repeat: the first step at once, the next after a pause. @@ -27,6 +39,20 @@ const REPEAT_DELAY: f32 = 0.35; const REPEAT_EVERY: f32 = 0.09; /// Lines per second the right stick scrolls at full tilt. const SCROLL_SPEED: f32 = 12.0; +/// How far the stick must lean to step focus. +const STICK_STEP: f32 = 0.55; +/// Scrolls a step may try to reveal the next row of a list. +const SCROLL_TRIES: u8 = 4; + +/// A focus step that ran off the visible part of a list: the list was +/// scrolled and the step runs again once a new frame is drawn. +#[derive(Clone, Copy)] +struct Pending { + button: Button, + from: Rect, + generation: u64, + tries: u8, +} #[derive(Default)] pub(super) struct MenuCursor { @@ -45,12 +71,31 @@ pub(super) struct MenuCursor { os_hidden: bool, /// Polls since a menu opened (its widgets are drawn after one frame). open_polls: u32, + /// Focus mode: the focused target as last drawn. + focus: Option, + /// The left stick's step direction last poll. + stick: Option