Open BraedonWooding opened 5 years ago
Looks same/related to #2151, thanks for the additional information. Could you specify which version of SDL you are using? Thank you!
I'm using SDL 2.0.9_1 which is the most recent version of SDL2. It also didn't work with 2.0.8 (I upgraded to see if it was an old bug of SDL2)
I have a similar problem. I'm using Imgui with SDL 2.0.9 on a Mac running Catalina. I have displays arranged like this:
and Imgui is unusable for windows in the upper monitor. io.MousePos
ends up with negative Y coordinates when the mouse is in the window.
I think the true bug is in SDL, where Cocoa_GetGlobalMouseState
(the backend for SDL_GetGlobalMouseState
) performs this contortion:
static Uint32
Cocoa_GetGlobalMouseState(int *x, int *y)
{
const NSUInteger cocoaButtons = [NSEvent pressedMouseButtons];
const NSPoint cocoaLocation = [NSEvent mouseLocation];
for (NSScreen *screen in [NSScreen screens]) {
NSRect frame = [screen frame];
if (NSMouseInRect(cocoaLocation, frame, NO)) {
*x = (int) cocoaLocation.x;
*y = (int) ((frame.origin.y + frame.size.height) - cocoaLocation.y);
break;
}
}
...
}
The code where it searches the screens is because Cocoa reports coordinates starting at the BOTTOM left. So it tries to subtract it from the screen height. But it's wrong on a second monitor, or at least reports numbers incompatible with SDL_GetWindowPosition
(which gives the expected negative numbers for the top monitor.)
Turning off g_MouseCanUseGlobalState
(which is normally only off on Wayland) works much better.
The whole global mouse thing seems like a questionable idea. Why not just use SDL_GetMouseState
?
It seems that if on a mac you align the monitors a little offset (attached photo)
Then the mouse position that is used by imgui is off by the amount the window is offset by. This is a problem inside
imgui_impl_sdl.cpp
I had a look around and couldn't find any information on previous issues with this. The problem is only with SDL_GetGlobalMouseState the normal SDL_GetMouseState is fine (which is why I think no one has reported it). This makes it seem like its a Mac issueThe fix is relatively simple, it seems that the
x
coordinate is always represented right and only they
coordinate is off. The fix I applied was to get the current window's display bounds and correct the y position of the cursor by that.I didn't create a PR because I'm concerned this will cause havoc with other systems. I don't know enough about how IMGUI to know if this is expected or not :). My view is that when we get the SDL_GetWindowPosition we expect the screen to be at 0, 0 which it isn't always (i.e. if we misalign them as shown). What is really giving me odd thoughts is how I can't replicate it on the 'x' axis it always gives the right result regardless.