Merge pull request #1897 from vlesmentie/fix/pointing-hand-cursor
🤖 Agent: Thanks vlesmentie! Matches vanilla 26.3's AbstractWidget.handleCursor: buttons show the pointing hand. lan_e2e's cursor check passes. Tested together with the other wave 2 contributor PRs: fmt, clippy -D warnings, and the horizon-client, horizon-world and horizon-protocol tests pass. Of the 127 harnesses the batch affects, 124 pass; the other three are a player-head mismatch fixed in #1934 and two worldgen timeouts on a loaded machine with no chunk differing. lan_e2e chat, fireball, cursor, looks, thrown, eat, shield, lava, contacts, projectiles and mobs pass with the host in-frame and on its server thread.
This commit is contained in:
@@ -1215,6 +1215,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`).
|
||||
@@ -4746,6 +4748,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.
|
||||
@@ -11739,6 +11759,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()
|
||||
{
|
||||
@@ -12047,6 +12068,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(),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -317,6 +317,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 {
|
||||
|
||||
@@ -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<Cursor> = 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) {
|
||||
|
||||
Reference in New Issue
Block a user