Skip to content

Commit fc160b2

Browse files
author
Ryan Fields
committed
Brings sf::Window getPosition and setPosition closer to parity on OS X.
The coordinate system transforation that occurs when dealing with window positions on OS X requires (or at least warrants) some fuzzy logic in dealing with getting and setting window positions. In any windowing system, requesting a position should effectively yield the components for an identity transform of the window's current position. In other words, coordinates obtained by a call to getPosition should have no impact in the window's position when immediately applied to setPosition. This change brings getPosition and setPosition much closer to acheiving that identity transform in the OS X implementation, correcting the error in most cases, and producing far less unpredictable results in the cases that remain affected.
1 parent d648f76 commit fc160b2

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/SFML/Window/OSX/SFViewController.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ -(NSPoint)position
138138

139139

140140
////////////////////////////////////////////////////////.
141-
-(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y
141+
-(void)setWindowPositionToX:(int)x Y:(int)y
142142
{
143143
sf::err() << "Cannot move SFML area when SFML is integrated in a NSView. Use the view hanlder directly instead." << std::endl;
144144
}

src/SFML/Window/OSX/SFWindowController.mm

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,25 +292,34 @@ -(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y
292292
////////////////////////////////////////////////////////////
293293
-(NSPoint)position
294294
{
295-
NSPoint pos = [m_oglView frame].origin;
295+
NSRect frame = m_window.frame;
296+
NSPoint pos = frame.origin;
297+
NSRect screenFrame = m_window.screen.frame;
296298

297299
// Flip for SFML window coordinate system.
298-
pos.y = [self screenHeight] - pos.y;
300+
pos.y += frame.size.height;
301+
pos.y = screenFrame.size.height - pos.y;
299302

300303
return pos;
301304
}
302305

303306

304307
////////////////////////////////////////////////////////.
305-
-(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y
308+
-(void)setWindowPositionToX:(int)x Y:(int)y
306309
{
307310
NSPoint point = NSMakePoint(x, y);
311+
NSScreen *screen = m_window.screen;
308312

309313
// Flip for SFML window coordinate system.
310-
point.y = [self screenHeight] - point.y;
314+
point.y = m_window.screen.frame.size.height - point.y;
311315

312316
// Place the window.
313317
[m_window setFrameTopLeftPoint:point];
318+
319+
if (screen != [m_window screen]) {
320+
// Oops. Coordinates were for a different screen. Do-over.
321+
[self setWindowPositionToX:x Y:y];
322+
}
314323
}
315324

316325

src/SFML/Window/OSX/WindowImplDelegateProtocol.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ namespace sf {
104104
/// Move the window (not the view if we handle not a window) (SFML Coordinates).
105105
///
106106
////////////////////////////////////////////////////////////
107-
-(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y;
107+
-(void)setWindowPositionToX:(int)x Y:(int)y;
108108

109109
////////////////////////////////////////////////////////////
110110
/// Get window's size.

0 commit comments

Comments
 (0)