Skip to content

Commit 3e46fe7

Browse files
authored
Fixes for mouse capture (Kenix3#779)
* fix cursor centering on dxgi * fix cursor warp in unfocused window
1 parent b1447cd commit 3e46fe7

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/graphic/Fast3D/gfx_dxgi.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ static struct {
9292
float mouse_wheel[2];
9393
LARGE_INTEGER previous_present_time;
9494
bool is_mouse_captured;
95+
bool in_focus;
9596

9697
void (*on_fullscreen_changed)(bool is_now_fullscreen);
9798
bool (*on_key_down)(int scancode);
@@ -438,10 +439,14 @@ static LRESULT CALLBACK gfx_dxgi_wnd_proc(HWND h_wnd, UINT message, WPARAM w_par
438439
GetMonitorHzPeriod(dxgi.h_Monitor, dxgi.detected_hz, dxgi.display_period);
439440
break;
440441
case WM_SETFOCUS:
442+
dxgi.in_focus = true;
441443
if (dxgi.is_mouse_captured) {
442444
apply_mouse_capture_clip();
443445
}
444446
break;
447+
case WM_KILLFOCUS:
448+
dxgi.in_focus = false;
449+
break;
445450
default:
446451
return DefWindowProcW(h_wnd, message, w_param, l_param);
447452
}
@@ -567,13 +572,16 @@ static void gfx_dxgi_get_mouse_pos(int32_t* x, int32_t* y) {
567572
}
568573

569574
static void gfx_dxgi_get_mouse_delta(int32_t* x, int32_t* y) {
570-
if (dxgi.is_mouse_captured) {
575+
if (dxgi.is_mouse_captured && dxgi.in_focus) {
571576
POINT p;
572577
GetCursorPos(&p);
573578
ScreenToClient(dxgi.h_wnd, &p);
574-
*x = p.x - dxgi.current_width / 2;
575-
*y = p.y - dxgi.current_height / 2;
576-
SetCursorPos(dxgi.current_width / 2, dxgi.current_height / 2);
579+
int32_t centerX, centerY;
580+
centerX = dxgi.current_width / 2;
581+
centerY = dxgi.current_height / 2;
582+
*x = p.x - centerX;
583+
*y = p.y - centerY;
584+
SetCursorPos(dxgi.posX + centerX, dxgi.posY + centerY);
577585
} else {
578586
*x = 0;
579587
*y = 0;

0 commit comments

Comments
 (0)