From f9435eb8812419dfa58b93bbe5ddb4f49dac4ea7 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Fri, 24 Jun 2011 00:10:04 +0200 Subject: [PATCH] Fixed SetCursorPosition in fullscreen mode and DRYed some code SetCursorPosition is now working properly in fullscreen mode. Some redundant code has been removed and refactored. --- src/SFML/Window/OSX/SFOpenGLView.h | 6 + src/SFML/Window/OSX/SFOpenGLView.mm | 276 ++++++++-------------- src/SFML/Window/OSX/SFWindowController.mm | 26 +- 3 files changed, 106 insertions(+), 202 deletions(-) diff --git a/src/SFML/Window/OSX/SFOpenGLView.h b/src/SFML/Window/OSX/SFOpenGLView.h index be2903ef..c6021a66 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.h +++ b/src/SFML/Window/OSX/SFOpenGLView.h @@ -87,6 +87,12 @@ namespace sf { //////////////////////////////////////////////////////////// -(void)setRealSize:(NSSize)newSize; +//////////////////////////////////////////////////////////// +/// Move the mouse cursor to (x,y) (SFML Coordinates). +/// +//////////////////////////////////////////////////////////// +-(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y; + //////////////////////////////////////////////////////////// /// Adjust key repeat configuration. /// diff --git a/src/SFML/Window/OSX/SFOpenGLView.mm b/src/SFML/Window/OSX/SFOpenGLView.mm index bd9c8477..b5f68708 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.mm +++ b/src/SFML/Window/OSX/SFOpenGLView.mm @@ -103,6 +103,18 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); //////////////////////////////////////////////////////////// -(void)initModifiersState; +//////////////////////////////////////////////////////////// +/// Compute the position of the cursor. +/// +//////////////////////////////////////////////////////////// +-(NSPoint)cursorPositionFromEvent:(NSEvent *)event; + +//////////////////////////////////////////////////////////// +/// Converte the NSEvent mouse button type to SFML type. +/// +//////////////////////////////////////////////////////////// +-(sf::Mouse::Button)mouseButtonFromEvent:(NSEvent *)event; + //////////////////////////////////////////////////////////// /// Convert a key down/up NSEvent into an SFML key event. /// Based on LocalizedKeys and NonLocalizedKeys function. @@ -161,6 +173,39 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); } +//////////////////////////////////////////////////////// +-(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y +{ + // Flip for SFML window coordinate system + y = NSHeight([[self window] frame]) - y; + + // Adjust for view reference instead of window + y -= NSHeight([[self window] frame]) - NSHeight([self frame]); + + // Convert to screen coordinates + NSPoint screenCoord = [[self window] convertBaseToScreen:NSMakePoint(x, y)]; + + // Flip screen coodinates + float const screenHeight = NSHeight([[[self window] screen] frame]); + screenCoord.y = screenHeight - screenCoord.y; + + // Recompute the mouse pos if required. + if (!NSEqualSizes(myRealSize, NSZeroSize)) { + screenCoord.x = screenCoord.x / myRealSize.width * [self frame].size.width; + screenCoord.y = screenCoord.y / myRealSize.height * [self frame].size.height; + } + + // Place the cursor. + CGEventRef event = CGEventCreateMouseEvent(NULL, + kCGEventMouseMoved, + CGPointMake(screenCoord.x, screenCoord.y), + /*we don't care about this : */0); + CGEventPost(kCGHIDEventTap, event); + CFRelease(event); + // This is a workaround to deprecated CGSetLocalEventsSuppressionInterval. +} + + //////////////////////////////////////////////////////// -(void)enableKeyRepeat { @@ -260,58 +305,24 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); //////////////////////////////////////////////////////// -(void)mouseDown:(NSEvent *)theEvent { - if (myRequester == 0) return; - - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - myRequester->MouseDownAt(sf::Mouse::Left, loc.x, h - loc.y); + // Forward to... + [self otherMouseDown:theEvent]; } //////////////////////////////////////////////////////// -(void)mouseUp:(NSEvent *)theEvent { - if (myRequester == 0) return; - - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } - - myRequester->MouseUpAt(sf::Mouse::Left, loc.x, loc.y); + // Forward to... + [self otherMouseUp:theEvent]; } //////////////////////////////////////////////////////// -(void)mouseMoved:(NSEvent *)theEvent { - if (myRequester == 0) return; - - // If the event is not useful. - if (!myMouseIsIn) return; - - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } - - myRequester->MouseMovedAt(loc.x, loc.y); + // Forward to... + [self otherMouseDragged:theEvent]; } @@ -320,17 +331,7 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); { if (myRequester == 0) return; - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } + NSPoint loc = [self cursorPositionFromEvent:theEvent]; myRequester->MouseWheelScrolledAt([theEvent deltaY], loc.x, loc.y); } @@ -361,42 +362,16 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); //////////////////////////////////////////////////////// -(void)rightMouseDown:(NSEvent *)theEvent { - if (myRequester == 0) return; - - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } - - myRequester->MouseDownAt(sf::Mouse::Right, loc.x, loc.y); + // Forward to... + [self otherMouseDown:theEvent]; } //////////////////////////////////////////////////////// -(void)rightMouseUp:(NSEvent *)theEvent { - if (myRequester == 0) return; - - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } - - myRequester->MouseUpAt(sf::Mouse::Right, loc.x, loc.y); + // Forward to... + [self otherMouseUp:theEvent]; } @@ -405,32 +380,9 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); { if (myRequester == 0) return; - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; + NSPoint loc = [self cursorPositionFromEvent:theEvent]; - sf::Mouse::Button button; - switch ([theEvent buttonNumber]) { - case 2: - button = sf::Mouse::Middle; - break; - case 3: - button = sf::Mouse::XButton1; - break; - case 4: - button = sf::Mouse::XButton2; - break; - default: - break; - } - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } + sf::Mouse::Button button = [self mouseButtonFromEvent:theEvent]; myRequester->MouseDownAt(button, loc.x, loc.y); } @@ -441,32 +393,9 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); { if (myRequester == 0) return; - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; + NSPoint loc = [self cursorPositionFromEvent:theEvent]; - sf::Mouse::Button button; - switch ([theEvent buttonNumber]) { - case 2: - button = sf::Mouse::Middle; - break; - case 3: - button = sf::Mouse::XButton1; - break; - case 4: - button = sf::Mouse::XButton2; - break; - default: - break; - } - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } + sf::Mouse::Button button = [self mouseButtonFromEvent:theEvent]; myRequester->MouseUpAt(button, loc.x, loc.y); } @@ -475,48 +404,16 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); //////////////////////////////////////////////////////// -(void)rightMouseDragged:(NSEvent *)theEvent { - if (myRequester == 0) return; - - // If the event is not useful. - if (!myMouseIsIn) return; - - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } - - myRequester->MouseMovedAt(loc.x, loc.y); + // Forward to... + [self otherMouseDragged:theEvent]; } //////////////////////////////////////////////////////// -(void)mouseDragged:(NSEvent *)theEvent { - if (myRequester == 0) return; - - // If the event is not useful. - if (!myMouseIsIn) return; - - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } - - myRequester->MouseMovedAt(loc.x, loc.y); + // Forward to... + [self otherMouseDragged:theEvent]; } @@ -528,17 +425,7 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); // If the event is not useful. if (!myMouseIsIn) return; - NSPoint loc = [self convertPoint:[theEvent locationInWindow] fromView:nil]; - - // Don't forget to change to SFML coord system. - float h = [self frame].size.height; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } + NSPoint loc = [self cursorPositionFromEvent:theEvent]; myRequester->MouseMovedAt(loc.x, loc.y); } @@ -1009,6 +896,39 @@ sf::Key::Code NonLocalizedKeys(unsigned short keycode); } +//////////////////////////////////////////////////////// +-(NSPoint)cursorPositionFromEvent:(NSEvent *)event +{ + NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; + + // Don't forget to change to SFML coord system. + float h = [self frame].size.height; + loc.y = h - loc.y; + + // Recompute the mouse pos if required. + if (!NSEqualSizes(myRealSize, NSZeroSize)) { + loc.x = loc.x * myRealSize.width / [self frame].size.width; + loc.y = loc.y * myRealSize.height / [self frame].size.height; + } + + return loc; +} + + +//////////////////////////////////////////////////////// +-(sf::Mouse::Button)mouseButtonFromEvent:(NSEvent *)event +{ + switch ([event buttonNumber]) { + case 0: return sf::Mouse::Left; + case 1: return sf::Mouse::Right; + case 2: return sf::Mouse::Middle; + case 3: return sf::Mouse::XButton1; + case 4: return sf::Mouse::XButton2; + default: return sf::Mouse::ButtonCount; // Never happens! (hopefully) + } +} + + //////////////////////////////////////////////////////// +(sf::Event::KeyEvent)convertNSKeyEventToSFMLEvent:(NSEvent *)anEvent { diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index 4be3b0e9..d9cecd1d 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -279,30 +279,8 @@ //////////////////////////////////////////////////////// -(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y { - if (myRequester == 0) return; - - // Flip for SFML window coordinate system - y = NSHeight([myWindow frame]) - y; - - // Adjust for view reference instead of window - y -= NSHeight([myWindow frame]) - NSHeight([myOGLView frame]); - - // Convert to screen coordinates - NSPoint screenCoord = [myWindow convertBaseToScreen:NSMakePoint(x, y)]; - - // Flip screen coodinates - float const screenHeight = NSHeight([[myWindow screen] frame]); - screenCoord.y = screenHeight - screenCoord.y; - - // Place the cursor. - CGEventRef event = CGEventCreateMouseEvent(NULL, - kCGEventMouseMoved, - CGPointMake(screenCoord.x, screenCoord.y), - /*we don't care about this : */0); - CGEventPost(kCGHIDEventTap, event); - CFRelease(event); - // This is a workaround to deprecated CGSetLocalEventsSuppressionInterval. - // The event produced will be catched by SFML in sf::Window::FilterEvent. + // Forward to... + [myOGLView setCursorPositionToX:x Y:y]; }