diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 0502753db..1a862193d 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1203,6 +1203,8 @@ struct Game<'a> { left: bool, right: bool, cursor: (f32, f32), + /// The cursor icon the window shows (`widgets::take_cursor` each frame). + cursor_icon: horizon_client::widgets::Cursor, captured: bool, focused: bool, /// The keyboard's modifier state (`KeyEvent.modifiers`). @@ -4659,6 +4661,24 @@ impl Game<'_> { } window.set_cursor_visible(!self.captured); } + /// `GuiGraphicsExtractor.applyCursor` -> `Window.selectCursor` at the end + /// of a frame: the cursor the frame's widgets asked for, set on the window + /// only when it changes. + fn apply_cursor_icon(&mut self) { + use horizon_client::widgets::Cursor; + let wanted = horizon_client::widgets::take_cursor(); + if wanted == self.cursor_icon { + return; + } + if let Some(window) = &self.window { + window.set_cursor(match wanted { + Cursor::Arrow => winit::window::CursorIcon::Default, + Cursor::PointingHand => winit::window::CursorIcon::Pointer, + Cursor::NotAllowed => winit::window::CursorIcon::NotAllowed, + }); + } + self.cursor_icon = wanted; + } /// LevelLoadingScreen.onClose -> Gui.setScreen(null) -> grabMouse. fn capture_loaded_world(&mut self) { // Loading may finish after focus was lost or another screen opened. @@ -11472,6 +11492,7 @@ impl ApplicationHandler for Game<'_> { self.advance_flight(); let ticked = Instant::now(); let rendered = self.render()?; + self.apply_cursor_icon(); if let Some(threshold) = self.options.spikes && let Some(r) = self.renderer.as_ref() { @@ -11779,6 +11800,7 @@ impl<'a> Game<'a> { left: false, right: false, cursor: (0.0, 0.0), + cursor_icon: horizon_client::widgets::Cursor::Arrow, captured: false, focused: true, modifiers: winit::keyboard::ModifiersState::empty(), diff --git a/crates/horizon-client/src/pack_screen.rs b/crates/horizon-client/src/pack_screen.rs index 2929fd663..03906030a 100644 --- a/crates/horizon-client/src/pack_screen.rs +++ b/crates/horizon-client/src/pack_screen.rs @@ -831,6 +831,7 @@ impl PackScreen { d::label(&mut out, &lines, mx, my, self.width - 50, true); for ((bx, by), key) in buttons.into_iter().zip(["gui.yes", "gui.no"]) { let hover = d::inside(mouse, bx, by, 150, 20); + crate::widgets::widget_cursor(hover, true); d::button(&mut out, m, &tr(key), bx, by, 150, 20, true, hover); } return out; @@ -856,6 +857,8 @@ impl PackScreen { } else { "gui.done" }); + // The cursor follows the mouse; the highlight also keyboard focus. + crate::widgets::widget_cursor(hover, true); d::button( &mut out, m, diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index 1c6e93067..df72ba45b 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -309,6 +309,7 @@ pub fn button( hover: bool, ) { crate::widgets::record(&label.plain(), [x, y, w, h], active); + crate::widgets::widget_cursor(hover, active); let sprite = if !active { "gui/sprites/widget/button_disabled" } else if hover { diff --git a/crates/horizon-client/src/widgets.rs b/crates/horizon-client/src/widgets.rs index 8bd80d3d1..c7c7eb2db 100644 --- a/crates/horizon-client/src/widgets.rs +++ b/crates/horizon-client/src/widgets.rs @@ -1,8 +1,47 @@ //! The labelled widgets and focus targets drawn in the last frame, for the //! control socket: a script clicks a button by its label (`horizon-ctl //! click-button Respawn`) or any widget by its id in [`tree`] (`horizon-ctl -//! click done`), whatever screen drew it. -use std::cell::RefCell; +//! click done`), whatever screen drew it. Also the mouse cursor the widget +//! under the mouse asks for. +use std::cell::{Cell, RefCell}; + +/// The mouse cursor a frame's widgets asked for (`CursorTypes`; the others +/// aren't asked for yet). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Cursor { + #[default] + Arrow, + PointingHand, + NotAllowed, +} + +thread_local! { + /// `GuiGraphicsExtractor.requestCursor`: the last request since [`take_cursor`]. + static CURSOR: Cell = const { Cell::new(Cursor::Arrow) }; +} + +/// `GuiGraphicsExtractor.requestCursor`: the last request of a frame wins. +pub fn request_cursor(cursor: Cursor) { + CURSOR.set(cursor); +} + +/// `AbstractWidget.handleCursor`: a hovered widget asks for the pointing +/// hand, or for not-allowed when it is inactive. +pub fn widget_cursor(hovered: bool, active: bool) { + if hovered { + request_cursor(if active { + Cursor::PointingHand + } else { + Cursor::NotAllowed + }); + } +} + +/// The cursor the frame asked for, and back to the arrow for the next one +/// (the window shows the arrow unless a widget asks). +pub fn take_cursor() -> Cursor { + CURSOR.replace(Cursor::Arrow) +} /// A labelled widget in GUI pixels (the coordinates screens take input in). #[derive(Clone, Debug)] @@ -133,6 +172,29 @@ pub fn record_as(label: &str, [x, y, w, h]: [f32; 4], enabled: bool, kind: Kind) }); } +#[cfg(test)] +mod cursor_tests { + use super::*; + + #[test] + fn hovered_widgets_ask_for_the_hand_or_not_allowed() { + let _ = take_cursor(); + widget_cursor(false, true); + assert_eq!(take_cursor(), Cursor::Arrow); + widget_cursor(true, true); + assert_eq!(take_cursor(), Cursor::PointingHand); + // Taken: the next frame starts from the arrow. + assert_eq!(take_cursor(), Cursor::Arrow); + widget_cursor(true, false); + assert_eq!(take_cursor(), Cursor::NotAllowed); + // The last request of a frame wins. + widget_cursor(true, false); + widget_cursor(false, true); + widget_cursor(true, true); + assert_eq!(take_cursor(), Cursor::PointingHand); + } +} + /// Once per event loop turn with the count of rendered frames: when a new /// frame was drawn since the last call, its buttons become the last frame's. pub fn end_frame(frames: u64) {