Improved -setWindowPositionToX:Y:

This commit is contained in:
Marco Antognini 2012-04-21 19:26:26 +02:00
parent 3e8961d05f
commit d37065e4de
3 changed files with 35 additions and 57 deletions

View File

@ -154,34 +154,46 @@ NSUInteger keepOnlyMaskFromData(NSUInteger data, NSUInteger mask);
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y -(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y
{ {
// Flip for SFML window coordinate system if (m_requester == 0) return;
y = NSHeight([[self window] frame]) - y;
// Adjust for view reference instead of window // Create a SFML event.
y -= NSHeight([[self window] frame]) - NSHeight([self frame]); m_requester->mouseMovedAt(x, y);
// 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. // Recompute the mouse pos if required.
if (!NSEqualSizes(m_realSize, NSZeroSize)) { if (!NSEqualSizes(m_realSize, NSZeroSize)) {
screenCoord.x = screenCoord.x / m_realSize.width * [self frame].size.width; x = x / m_realSize.width * [self frame].size.width;
screenCoord.y = screenCoord.y / m_realSize.height * [self frame].size.height; y = y / m_realSize.height * [self frame].size.height;
} }
// Note : we use rect here because some conversion methods are deprecated
// with point.
// Flip SFML coordinates to match window coordinates
y = [self frame].size.height - y;
// Get the position of (x, y) in the coordinate system of the window.
NSRect r = [self convertRect:NSMakeRect(x, y, 1, 1) toView:self];
r = [self convertRect:r toView:nil]; // nil means window
// Converte it to screen coordinates
r = [[self window] convertRectToScreen:r];
// Flip screen coodinates to match CGDisplayMoveCursorToPoint referential.
float const screenHeight = [[[self window] screen] frame].size.height;
r.origin.y = screenHeight - r.origin.y;
// Get the id of the screen
CGDirectDisplayID screenNumber = (CGDirectDisplayID)[[[[[self window] screen] deviceDescription] valueForKey:@"NSScreenNumber"] intValue];
// Place the cursor. // Place the cursor.
CGEventRef event = CGEventCreateMouseEvent(NULL, CGDisplayMoveCursorToPoint(screenNumber, CGPointMake(r.origin.x, r.origin.y));
kCGEventMouseMoved, /*
CGPointMake(screenCoord.x, * CGDisplayMoveCursorToPoint -- Discussion :
screenCoord.y), *
/*we don't care about this : */0); * No events are generated as a result of this move.
CGEventPost(kCGHIDEventTap, event); * Points that lie outside the desktop are clipped to the desktop.
CFRelease(event); */
// This is a workaround to deprecated CGSetLocalEventsSuppressionInterval function
} }

View File

@ -124,36 +124,8 @@
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y -(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y
{ {
// TODO remove this ? use ogl view selector instead ? // Forward to...
[m_oglView setCursorPositionToX:x Y:y];
if (m_requester == 0) return;
// Create a SFML event.
m_requester->mouseMovedAt(x, y);
// Flip for SFML window coordinate system
y = NSHeight([[m_view window] frame]) - y;
// Adjust for view reference instead of window
y -= NSHeight([[m_view window] frame]) - NSHeight([m_oglView frame]);
// Convert to screen coordinates
NSPoint screenCoord = [[m_view window] convertBaseToScreen:NSMakePoint(x, y)];
// Flip screen coodinates
float const screenHeight = NSHeight([[[m_view window] screen] frame]);
screenCoord.y = screenHeight - screenCoord.y;
CGDirectDisplayID screenNumber = (CGDirectDisplayID)[[[[[m_view window] screen] deviceDescription] valueForKey:@"NSScreenNumber"] intValue];
// Place the cursor.
CGDisplayMoveCursorToPoint(screenNumber, CGPointMake(screenCoord.x, screenCoord.y));
/*
CGDisplayMoveCursorToPoint -- Discussion :
No events are generated as a result of this move.
Points that lie outside the desktop are clipped to the desktop.
*/
} }
@ -175,7 +147,6 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
-(NSSize)size -(NSSize)size
{ {
// TODO scaleUnitSquareToSize: ?
return [m_oglView frame].size; return [m_oglView frame].size;
} }
@ -183,8 +154,6 @@
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)resizeTo:(unsigned int)width by:(unsigned int)height -(void)resizeTo:(unsigned int)width by:(unsigned int)height
{ {
// TODO scaleUnitSquareToSize: ?
NSRect frame = NSMakeRect([m_view frame].origin.x, NSRect frame = NSMakeRect([m_view frame].origin.x,
[m_view frame].origin.y, [m_view frame].origin.y,
width, width,

View File

@ -317,7 +317,6 @@
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(NSSize)size -(NSSize)size
{ {
// TODO scaleUnitSquareToSize: ?
return [m_oglView frame].size; return [m_oglView frame].size;
} }
@ -325,8 +324,6 @@
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)resizeTo:(unsigned int)width by:(unsigned int)height -(void)resizeTo:(unsigned int)width by:(unsigned int)height
{ {
// TODO scaleUnitSquareToSize: ?
// Add titlebar height. // Add titlebar height.
NSRect frame = NSMakeRect([m_window frame].origin.x, NSRect frame = NSMakeRect([m_window frame].origin.x,
[m_window frame].origin.y, [m_window frame].origin.y,