macroquad icon indicating copy to clipboard operation
macroquad copied to clipboard

Issues with set_cursor_grab(true)

Open vdrn opened this issue 2 years ago • 6 comments

Trying out set_cursor_grab on X11 (Ubuntu 20.04) I've encountered a couple of issues:

  1. mouse_position() coordinates are wrong:
    • they are not changing by appropriate amount as the mouse moves
    • they keep on changing even when the mouse is stuck (ie: if you keep dragging mouse into top-left corner, they will keep on decreasing to negative values)
  2. Alt-tabbing to another window, so the macroquad window is no longer focused, will keep the mouse locked to macroquad's window.

How to reproduce: Add set_cursor_grab(true); to ui.rs example. This will make all ui elements not clickable by mouse.

vdrn avatar Feb 02 '23 20:02 vdrn

Can reproduce. One can still click the UI elements, but not where one would expect them to be. Like you said the mouse position has the wrong values.

GRASBOCK avatar Feb 25 '23 08:02 GRASBOCK

I tried to figure this out.

In miniquad it works just fine. Meaning it is not an issue with x11, because there is no platform specifics in macroquad as far as I know. Here an example

use miniquad::*;

struct Stage {}
impl EventHandler for Stage {
    fn update(&mut self, _ctx: &mut Context) {}

    fn raw_mouse_motion(&mut self, ctx: &mut Context, dx: f32, dy: f32) {
        println!("relative mouse motion: {dx}, {dy}");
    }

    fn mouse_motion_event(&mut self, ctx: &mut Context, x: f32, y: f32){
        println!("absolute mouse position: {x}, {y}");
    }

    fn draw(&mut self, ctx: &mut Context) {
        ctx.clear(Some((0., 1., 0., 1.)), None, None);
    }
}

fn main() {
    miniquad::start(
        conf::Conf {
            window_title: "Miniquad".to_string(),
            window_width: 100,
            window_height: 100,
            ..Default::default()
        },
        |_ctx| {
            _ctx.set_cursor_grab(true); // <--- constraining the cursor
            Box::new(Stage {})
        }
    );
}

Looking at the definition of set_cursor_grab() I can see that someone has already considered the second problem.

/// Capture mouse cursor to the current window
/// On WASM this will automatically hide cursor
/// On desktop this will bound cursor to windows border
/// NOTICE: on desktop cursor will not be automatically released after window lost focus
///         so set_cursor_grab(false) on window's focus lost is recommended.
/// TODO: implement window focus events
pub fn set_cursor_grab(&mut self, grab: bool) {
...

So that requires window focus events being implemented.

So the problem lies somewhere in macroquad. Inside the mouse_motion_event() callback function one can see, that the mouse position is only updated in absolute values if the cursor is not grabbed.

fn mouse_motion_event(&mut self, _: &mut miniquad::Context, x: f32, y: f32) {
    let context = get_context();
    // context.mouse_position = Vec2::new(x, y); // adding this line fixes the problem
    if !context.cursor_grabbed {
        context.mouse_position = Vec2::new(x, y);

The relative motion is however only used if it is grabbed.

fn raw_mouse_motion(&mut self, _: &mut miniquad::Context, x: f32, y: f32) {
    let context = get_context();
    if context.cursor_grabbed {
        context.mouse_position += Vec2::new(x, y);

cursor_grabbed is used at a lot of places like on mouse down where I didn't expect it to be used. Could it be, that someone misunderstood cursor_grabbed for "mouse is being held down"? I am confused as to why the context.mouse_position is not set independent of cursor_grabbed.

GRASBOCK avatar Feb 25 '23 11:02 GRASBOCK

This was added along with the support for capture mouse in this commit 5c8a93529ee82688c99471f206ce9d8b719b2f2a by @Pebaz but I have no idea why he added these cursor_grabbed check there

siddharthroy12 avatar Mar 19 '23 19:03 siddharthroy12

In raylib when cursor is grabbed it also hide it and to un-grab the cursor when window is un-focused miniquad needs to add this feature to check if window is focused or not

siddharthroy12 avatar Mar 19 '23 20:03 siddharthroy12

Related to https://github.com/not-fl3/miniquad/pull/27 https://github.com/not-fl3/miniquad/issues/28

siddharthroy12 avatar Mar 19 '23 20:03 siddharthroy12

This issue still appears in version 0.4.13 of macroquad on linux at least.

CoherentThought avatar Feb 19 '25 14:02 CoherentThought