diff --git a/crates/horizon-client/src/game/beacon.rs b/crates/horizon-client/src/game/beacon.rs index 943fb9e9d..fafb2787d 100644 --- a/crates/horizon-client/src/game/beacon.rs +++ b/crates/horizon-client/src/game/beacon.rs @@ -274,6 +274,17 @@ impl Game<'_> { } } /// Clicks on the power, confirm and cancel buttons. + /// The effect, upgrade, confirm and cancel buttons a controller can + /// focus (GUI origin relative). + pub(super) fn beacon_targets(&self) -> Vec<[f32; 4]> { + let (ux, uy) = upgrade_button(); + buttons() + .into_iter() + .map(|(_, x, y, _)| (x, y)) + .chain([(ux, uy), (164.0, 107.0), (190.0, 107.0)]) + .map(|(x, y)| [x + SHIFT, y, 22.0, 22.0]) + .collect() + } pub(super) fn beacon_click(&mut self, local: (f32, f32)) -> bool { let l = (local.0 - SHIFT, local.1); let levels = self.beacon_levels(); 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/loom.rs b/crates/horizon-client/src/game/loom.rs index f6648c43b..c0827b9a6 100644 --- a/crates/horizon-client/src/game/loom.rs +++ b/crates/horizon-client/src/game/loom.rs @@ -128,6 +128,24 @@ impl Game<'_> { } false } + /// The pattern buttons a controller can focus (GUI origin relative). + pub(super) fn loom_targets(&self) -> Vec<[f32; 4]> { + let Some(loom) = self.station.as_ref().map(|s| &s.loom).filter(|l| l.display) else { + return Vec::new(); + }; + (0..16) + .filter(|i| (i / 4 + loom.start_row) * 4 + i % 4 < loom.patterns.len()) + .map(|i| { + let (row, column) = (i / 4, i % 4); + [ + 60.0 + column as f32 * BUTTON, + 13.0 + row as f32 * BUTTON, + BUTTON, + BUTTON, + ] + }) + .collect() + } /// `LoomScreen.mouseDragged` on the scroller. pub(super) fn loom_drag(&mut self) { let Some(size) = self diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index 2f050fdec..a9009db35 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); @@ -315,6 +315,11 @@ impl Game<'_> { /// B and Start: what Escape does where the pad is. pub(super) fn pad_back(&mut self) -> Result<()> { + // The options open over the title screen too: they come first, as + // for the keyboard's Escape. + if self.screen == Screen::Options { + return self.options_key(KeyCode::Escape, None); + } if let Some(title) = self.title.as_mut() { title.escape(); return Ok(()); @@ -340,11 +345,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 +354,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. @@ -478,6 +474,7 @@ impl Game<'_> { "reason": why, "strong": p.strong, "weak": p.weak, "ms": p.millis, })), "in_use": self.pad.in_use, + "menu": self.pad_menu_status(), "held": held, "left": s.left, "right": s.right, diff --git a/crates/horizon-client/src/game/pad_menu.rs b/crates/horizon-client/src/game/pad_menu.rs index 8047e889a..46a6d66b1 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,23 @@ 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. When the +/// targets didn't move (nothing scrolled), the step takes vanilla's vague +/// pass instead. +#[derive(Clone)] +struct Pending { + button: Button, + from: Rect, + generation: u64, + tries: u8, + before: Vec, +} #[derive(Default)] pub(super) struct MenuCursor { @@ -45,12 +74,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