Skip to main content

repose_core/
cursor.rs

1/// Pointer confinement for the host window, the `winit` and Bevy
2/// `CursorGrabMode` set. Apps declare it per frame through
3/// [`cursor_grab`](crate::runtime::Scheduler::cursor_grab).
4///
5/// `Confined` is the one that means "the mouse cannot escape the
6/// window": the OS keeps the pointer inside its bounds while the
7/// cursor stays visible. `Locked` additionally hides it and reports
8/// movement without bounds (FPS mouselook); platforms that cannot
9/// lock fall back to confining in the runners.
10///
11/// The web is the odd one out: `Locked` drives pointer lock (which a
12/// browser grants only after a user gesture) and `Confined` has no
13/// equivalent, so it stays free.
14#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
15pub enum CursorGrabMode {
16    /// The pointer moves freely, in and out of the window.
17    #[default]
18    None,
19    /// The pointer cannot leave the window. Still drawn and still
20    /// reaches every pixel inside, so absolute-position controls
21    /// (a paddle, a drag) keep working at the window edge.
22    Confined,
23    /// The pointer is hidden and pinned to the window, reporting
24    /// relative movement only.
25    Locked,
26}
27
28#[derive(Clone, Debug, PartialEq, Eq)]
29pub enum CursorIcon {
30    Default,
31    Pointer,
32    Text,
33    EwResize,
34    NsResize,
35    NwseResize,
36    NeswResize,
37    Grab,
38    Grabbing,
39    /// Hide the OS cursor entirely. Games draw their own crosshair
40    /// (e.g. twin-stick shooters in keyboard mode) and the OS arrow
41    /// would double-paint next to it. Platform runners implement this
42    /// with `set_cursor_visible(false)`, not an icon.
43    Hidden,
44    Custom(std::sync::Arc<CustomCursorImage>),
45}
46
47/// Pixel payload for [`CursorIcon::Custom`].
48#[derive(Clone, PartialEq, Eq)]
49pub struct CustomCursorImage {
50    /// Straight-alpha RGBA bytes, `w * h * 4` long.
51    pub rgba: std::sync::Arc<[u8]>,
52    /// Image dimensions in px.
53    pub size: [u16; 2],
54    /// Hotspot (click point) in px from the top-left.
55    pub hotspot: [u16; 2],
56}
57
58impl std::fmt::Debug for CustomCursorImage {
59    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60        f.debug_struct("CustomCursorImage")
61            .field("size", &self.size)
62            .field("hotspot", &self.hotspot)
63            .field("rgba_len", &self.rgba.len())
64            .finish()
65    }
66}