From aa4203fd543e9afae1c01cf60fff3f16e6f9ed24 Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Fri, 28 Jun 2013 18:01:18 +0200 Subject: [PATCH] Improve reactivity of setMousePosition on OS X (fix #290) --- src/SFML/Window/OSX/InputImpl.mm | 6 +++-- src/SFML/Window/OSX/SFOpenGLView.h | 5 ++-- src/SFML/Window/OSX/SFOpenGLView.mm | 39 ++++++++--------------------- 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/SFML/Window/OSX/InputImpl.mm b/src/SFML/Window/OSX/InputImpl.mm index 3b6921f9..37b5faf5 100644 --- a/src/SFML/Window/OSX/InputImpl.mm +++ b/src/SFML/Window/OSX/InputImpl.mm @@ -172,8 +172,10 @@ void InputImpl::setMousePosition(const Vector2i& position, const Window& relativ return; } - // Use -setCursorPositionToX:Y:. - [view setCursorPositionToX:position.x Y:position.y]; + // Let SFOpenGLView compute the position in global coordinate + NSPoint p = NSMakePoint(position.x, position.y); + p = [view computeGlobalPositionOfRelativePoint:p]; + setMousePosition(sf::Vector2i(p.x, p.y)); } } // namespace priv diff --git a/src/SFML/Window/OSX/SFOpenGLView.h b/src/SFML/Window/OSX/SFOpenGLView.h index b95a018d..6cb15895 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.h +++ b/src/SFML/Window/OSX/SFOpenGLView.h @@ -86,10 +86,11 @@ namespace sf { -(void)setRealSize:(NSSize)newSize; //////////////////////////////////////////////////////////// -/// Move the mouse cursor to (x,y) (SFML Coordinates). +/// Compute the position in global coordinate +/// of the given point in SFML coordinate. /// //////////////////////////////////////////////////////////// --(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y; +-(NSPoint)computeGlobalPositionOfRelativePoint:(NSPoint)point; //////////////////////////////////////////////////////////// /// Adjust key repeat configuration. diff --git a/src/SFML/Window/OSX/SFOpenGLView.mm b/src/SFML/Window/OSX/SFOpenGLView.mm index ad345d94..0e5d6f45 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.mm +++ b/src/SFML/Window/OSX/SFOpenGLView.mm @@ -141,17 +141,12 @@ BOOL isValidTextUnicode(NSEvent* event); //////////////////////////////////////////////////////// --(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y -{ - if (m_requester == 0) return; - - // Create a SFML event. - m_requester->mouseMovedAt(x, y); - +-(NSPoint)computeGlobalPositionOfRelativePoint:(NSPoint)point +{ // Recompute the mouse pos if required. if (!NSEqualSizes(m_realSize, NSZeroSize)) { - x = x / m_realSize.width * [self frame].size.width; - y = y / m_realSize.height * [self frame].size.height; + point.x = point.x / m_realSize.width * [self frame].size.width; + point.y = point.y / m_realSize.height * [self frame].size.height; } // Note : -[NSWindow convertBaseToScreen:] is deprecated on 10.7 @@ -162,34 +157,20 @@ BOOL isValidTextUnicode(NSEvent* event); // Flip SFML coordinates to match window coordinates - y = [self frame].size.height - y; + point.y = [self frame].size.height - point.y; // Get the position of (x, y) in the coordinate system of the window. - NSPoint p = [self convertPoint:NSMakePoint(x, y) toView:self]; - p = [self convertPoint:p toView:nil]; // nil means window + point = [self convertPoint:point toView:self]; + point = [self convertPoint:point toView:nil]; // nil means window // Convert it to screen coordinates - p = [[self window] convertBaseToScreen:p]; + point = [[self window] convertBaseToScreen:point]; // Flip screen coodinates to match CGDisplayMoveCursorToPoint referential. float const screenHeight = [[[self window] screen] frame].size.height; - p.y = screenHeight - p.y; - - x = p.x; - y = p.y; + point.y = screenHeight - point.y; - - // Get the id of the screen - CGDirectDisplayID screenNumber = (CGDirectDisplayID)[[[[[self window] screen] deviceDescription] valueForKey:@"NSScreenNumber"] intValue]; - - // Place the cursor. - CGDisplayMoveCursorToPoint(screenNumber, CGPointMake(x, y)); - /* - * CGDisplayMoveCursorToPoint -- Discussion : - * - * No events are generated as a result of this move. - * Points that lie outside the desktop are clipped to the desktop. - */ + return point; }