Browse Source

Setting the cursor position when not in full-screen.

David Piuva 7 months ago
parent
commit
07b951c1c2
3 changed files with 15 additions and 15 deletions
  1. 0 1
      Source/DFPSR/api/guiAPI.h
  2. 0 1
      Source/SDK/camera/main.cpp
  3. 15 13
      Source/windowManagers/CocoaWindow.mm

+ 0 - 1
Source/DFPSR/api/guiAPI.h

@@ -192,7 +192,6 @@ namespace dsr {
 	// WARNING!
 	//   On MS-Windows the cursor will always move, but on X11 the cursor must be within the window's region.
 	//   Only use this in full-screen mode to prevent getting stuck outside of the window.
-	//   On MacOS, the call will always fail if fullscreen is not active.
 	// WARNING!
 	//   Due to potential race-conditions, you can not assume that the next mouse move event is generated by this call.
 	bool window_setCursorPosition(const Window& window, int x, int y);

+ 0 - 1
Source/SDK/camera/main.cpp

@@ -96,7 +96,6 @@ void dsrMain(List<String> args) {
 				window_setPixelScale(window, key - DsrKey_0);
 			} else if (key == DsrKey_F11) {
 				window_setFullScreen(window, !window_isFullScreen(window));
-				window_setCursorVisibility(window, !window_isFullScreen(window));
 			} else if (key == DsrKey_Escape) {
 				running = false;
 			} else if (key == DsrKey_C) {

+ 15 - 13
Source/windowManagers/CocoaWindow.mm

@@ -151,19 +151,21 @@ bool CocoaWindow::setCursorVisibility(bool visible) {
 }
 
 bool CocoaWindow::setCursorPosition(int x, int y) {
-	if (this->windowState == 2) {
-		NSRect viewBounds = [this->view bounds];
-		NSRect viewInWindow = [this->view convertRect:viewBounds toView:nil];
-		CGWarpMouseCursorPosition(CGPointMake(viewInWindow.origin.x + x, viewInWindow.origin.y + y));
-		// Prevent stalling after the move.
-		CGAssociateMouseAndMouseCursorPosition(true);
-		// TODO: How can the mouse move event be sent in the correct order in case of already having move events waiting?
-		this->receivedMouseEvent(dsr::MouseEventType::MouseMove, dsr::MouseKeyEnum::NoKey, dsr::IVector2D(x, y));
-		return true;
-	} else {
-		// Setting the cursor position is not reliable in windowed mode, because the fetching the window location often returns outdated coordinates.
-		return false;
-	}
+	// Get the offset from window pixels to screen pixels.
+	NSWindow *window = [this->view window];
+	NSRect viewBounds = [this->view bounds];
+	NSRect viewRectInScreenCoords = [window convertRectToScreen:viewBounds];
+	CGPoint windowToScreenOffset = CGPointMake(
+	  viewRectInScreenCoords.origin.x,
+	  [[NSScreen mainScreen] frame].size.height - (viewRectInScreenCoords.origin.y + viewRectInScreenCoords.size.height)
+	);
+	// Set the cursor position using screen coordinates.
+	CGWarpMouseCursorPosition(CGPointMake(windowToScreenOffset.x + x, windowToScreenOffset.y + y));
+	// Prevent stalling after the move.
+	CGAssociateMouseAndMouseCursorPosition(true);
+	// TODO: How can the mouse move event be sent in the correct order in case of already having move events waiting?
+	this->receivedMouseEvent(dsr::MouseEventType::MouseMove, dsr::MouseKeyEnum::NoKey, dsr::IVector2D(x, y));
+	return true;
 }
 
 void CocoaWindow::setFullScreen(bool enabled) {