Improve reactivity of setMousePosition on OS X (fix #290)

This commit is contained in:
Marco Antognini 2013-06-28 18:01:18 +02:00
parent 5aa87f7719
commit aa4203fd54
3 changed files with 17 additions and 33 deletions

View File

@ -172,8 +172,10 @@ void InputImpl::setMousePosition(const Vector2i& position, const Window& relativ
return; return;
} }
// Use -setCursorPositionToX:Y:. // Let SFOpenGLView compute the position in global coordinate
[view setCursorPositionToX:position.x Y:position.y]; NSPoint p = NSMakePoint(position.x, position.y);
p = [view computeGlobalPositionOfRelativePoint:p];
setMousePosition(sf::Vector2i(p.x, p.y));
} }
} // namespace priv } // namespace priv

View File

@ -86,10 +86,11 @@ namespace sf {
-(void)setRealSize:(NSSize)newSize; -(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. /// Adjust key repeat configuration.

View File

@ -141,17 +141,12 @@ BOOL isValidTextUnicode(NSEvent* event);
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y -(NSPoint)computeGlobalPositionOfRelativePoint:(NSPoint)point
{ {
if (m_requester == 0) return;
// Create a SFML event.
m_requester->mouseMovedAt(x, y);
// Recompute the mouse pos if required. // Recompute the mouse pos if required.
if (!NSEqualSizes(m_realSize, NSZeroSize)) { if (!NSEqualSizes(m_realSize, NSZeroSize)) {
x = x / m_realSize.width * [self frame].size.width; point.x = point.x / m_realSize.width * [self frame].size.width;
y = y / m_realSize.height * [self frame].size.height; point.y = point.y / m_realSize.height * [self frame].size.height;
} }
// Note : -[NSWindow convertBaseToScreen:] is deprecated on 10.7 // Note : -[NSWindow convertBaseToScreen:] is deprecated on 10.7
@ -162,34 +157,20 @@ BOOL isValidTextUnicode(NSEvent* event);
// Flip SFML coordinates to match window coordinates // 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. // Get the position of (x, y) in the coordinate system of the window.
NSPoint p = [self convertPoint:NSMakePoint(x, y) toView:self]; point = [self convertPoint:point toView:self];
p = [self convertPoint:p toView:nil]; // nil means window point = [self convertPoint:point toView:nil]; // nil means window
// Convert it to screen coordinates // Convert it to screen coordinates
p = [[self window] convertBaseToScreen:p]; point = [[self window] convertBaseToScreen:point];
// Flip screen coodinates to match CGDisplayMoveCursorToPoint referential. // Flip screen coodinates to match CGDisplayMoveCursorToPoint referential.
float const screenHeight = [[[self window] screen] frame].size.height; float const screenHeight = [[[self window] screen] frame].size.height;
p.y = screenHeight - p.y; point.y = screenHeight - point.y;
x = p.x; return point;
y = p.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.
*/
} }