Преглед на файлове

Merge pull request #51405 from bruvzg/macos_recenter_mouse

[macOS] Move captured mouse cursor to the center when window gain focus.
Rémi Verschelde преди 4 години
родител
ревизия
7fc570b5ac
променени са 1 файла, в които са добавени 10 реда и са изтрити 2 реда
  1. 10 2
      platform/osx/display_server_osx.mm

+ 10 - 2
platform/osx/display_server_osx.mm

@@ -318,8 +318,16 @@ static NSCursor *_cursorFromSelector(SEL selector, SEL fallback = nil) {
 	}
 	}
 	DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
 	DisplayServerOSX::WindowData &wd = DS_OSX->windows[window_id];
 
 
-	_get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
-	Input::get_singleton()->set_mouse_position(wd.mouse_pos);
+	if (DS_OSX->mouse_mode == DisplayServer::MOUSE_MODE_CAPTURED) {
+		const NSRect contentRect = [wd.window_view frame];
+		NSRect pointInWindowRect = NSMakeRect(contentRect.size.width / 2, contentRect.size.height / 2, 0, 0);
+		NSPoint pointOnScreen = [[wd.window_view window] convertRectToScreen:pointInWindowRect].origin;
+		CGPoint lMouseWarpPos = { pointOnScreen.x, CGDisplayBounds(CGMainDisplayID()).size.height - pointOnScreen.y };
+		CGWarpMouseCursorPosition(lMouseWarpPos);
+	} else {
+		_get_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
+		Input::get_singleton()->set_mouse_position(wd.mouse_pos);
+	}
 
 
 	DS_OSX->window_focused = true;
 	DS_OSX->window_focused = true;
 	DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_FOCUS_IN);
 	DS_OSX->_send_window_event(wd, DisplayServerOSX::WINDOW_EVENT_FOCUS_IN);