This commit is contained in:
Rachael Alexanderson 2017-07-08 09:31:43 -04:00
commit 15d92b346d

View file

@ -109,7 +109,7 @@ void CheckGUICapture()
} }
} }
void CenterCursor() void SetCursorPosition(const NSPoint position)
{ {
NSWindow* window = [NSApp keyWindow]; NSWindow* window = [NSApp keyWindow];
if (nil == window) if (nil == window)
@ -118,15 +118,14 @@ void CenterCursor()
} }
const NSRect displayRect = [[window screen] frame]; const NSRect displayRect = [[window screen] frame];
const NSRect windowRect = [window frame]; const CGPoint eventPoint = CGPointMake(position.x, displayRect.size.height - position.y);
const CGPoint centerPoint = CGPointMake(NSMidX(windowRect), displayRect.size.height - NSMidY(windowRect));
CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
if (NULL != eventSource) if (NULL != eventSource)
{ {
CGEventRef mouseMoveEvent = CGEventCreateMouseEvent(eventSource, CGEventRef mouseMoveEvent = CGEventCreateMouseEvent(eventSource,
kCGEventMouseMoved, centerPoint, kCGMouseButtonLeft); kCGEventMouseMoved, eventPoint, kCGMouseButtonLeft);
if (NULL != mouseMoveEvent) if (NULL != mouseMoveEvent)
{ {
@ -141,6 +140,20 @@ void CenterCursor()
s_skipMouseMoves = 2; s_skipMouseMoves = 2;
} }
void CenterCursor()
{
NSWindow* window = [NSApp keyWindow];
if (nil == window)
{
return;
}
const NSRect displayRect = [[window screen] frame];
const NSRect windowRect = [window frame];
const NSPoint centerPoint = { NSMidX(windowRect), NSMidY(windowRect) };
SetCursorPosition(centerPoint);
}
bool IsInGame() bool IsInGame()
{ {
@ -227,6 +240,7 @@ void I_ReleaseMouseCapture()
void I_SetNativeMouse(bool wantNative) void I_SetNativeMouse(bool wantNative)
{ {
static bool nativeMouse = true; static bool nativeMouse = true;
static NSPoint mouseLocation;
if (wantNative != nativeMouse) if (wantNative != nativeMouse)
{ {
@ -234,6 +248,7 @@ void I_SetNativeMouse(bool wantNative)
if (!wantNative) if (!wantNative)
{ {
mouseLocation = [NSEvent mouseLocation];
CenterCursor(); CenterCursor();
} }
@ -241,6 +256,8 @@ void I_SetNativeMouse(bool wantNative)
if (wantNative) if (wantNative)
{ {
SetCursorPosition(mouseLocation);
[NSCursor unhide]; [NSCursor unhide];
} }
else else
@ -492,19 +509,6 @@ void ProcessMouseMoveInMenu(NSEvent* theEvent)
void ProcessMouseMoveInGame(NSEvent* theEvent) void ProcessMouseMoveInGame(NSEvent* theEvent)
{ {
if (!use_mouse)
{
return;
}
// TODO: remove this magic!
if (s_skipMouseMoves > 0)
{
--s_skipMouseMoves;
return;
}
int x([theEvent deltaX]); int x([theEvent deltaX]);
int y(-[theEvent deltaY]); int y(-[theEvent deltaY]);
@ -631,6 +635,12 @@ void ProcessMouseMoveEvent(NSEvent* theEvent)
return; return;
} }
if (s_skipMouseMoves > 0)
{
--s_skipMouseMoves;
return;
}
if (GUICapture) if (GUICapture)
{ {
ProcessMouseMoveInMenu(theEvent); ProcessMouseMoveInMenu(theEvent);