From a609a7114e5f56ca1fa9e703d23a51bae6f4813a Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Wed, 6 Jul 2011 03:05:58 +0200 Subject: [PATCH] Implemented WindowImplCocoa::GetCursorPosition() --- src/SFML/Window/OSX/SFOpenGLView.h | 6 ++++++ src/SFML/Window/OSX/SFOpenGLView.mm | 17 +++++++++-------- src/SFML/Window/OSX/SFViewController.mm | 7 +++++++ src/SFML/Window/OSX/SFWindowController.mm | 6 ++++++ src/SFML/Window/OSX/WindowImplCocoa.mm | 4 ++-- .../Window/OSX/WindowImplDelegateProtocol.h | 6 ++++++ 6 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/SFML/Window/OSX/SFOpenGLView.h b/src/SFML/Window/OSX/SFOpenGLView.h index c6021a662..e35c149e1 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.h +++ b/src/SFML/Window/OSX/SFOpenGLView.h @@ -100,4 +100,10 @@ namespace sf { -(void)enableKeyRepeat; -(void)disableKeyRepeat; +//////////////////////////////////////////////////////////// +/// Compute the position of the cursor. +/// +//////////////////////////////////////////////////////////// +-(NSPoint)cursorPositionFromEvent:(NSEvent *)eventOrNil; + @end diff --git a/src/SFML/Window/OSX/SFOpenGLView.mm b/src/SFML/Window/OSX/SFOpenGLView.mm index 6a01204f6..d9704b249 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.mm +++ b/src/SFML/Window/OSX/SFOpenGLView.mm @@ -103,12 +103,6 @@ sf::Keyboard::Key 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. /// @@ -904,9 +898,16 @@ sf::Keyboard::Key NonLocalizedKeys(unsigned short keycode); //////////////////////////////////////////////////////// --(NSPoint)cursorPositionFromEvent:(NSEvent *)event +-(NSPoint)cursorPositionFromEvent:(NSEvent *)eventOrNil { - NSPoint loc = [self convertPoint:[event locationInWindow] fromView:nil]; + NSPoint loc; + // If no event given then get current mouse pos. + if (eventOrNil == nil) { + NSPoint rawPos = [[self window] mouseLocationOutsideOfEventStream]; + loc = [self convertPoint:rawPos fromView:nil]; + } else { + loc = [self convertPoint:[eventOrNil locationInWindow] fromView:nil]; + } // Don't forget to change to SFML coord system. float h = [self frame].size.height; diff --git a/src/SFML/Window/OSX/SFViewController.mm b/src/SFML/Window/OSX/SFViewController.mm index 00cd51743..2efac8b94 100644 --- a/src/SFML/Window/OSX/SFViewController.mm +++ b/src/SFML/Window/OSX/SFViewController.mm @@ -195,6 +195,13 @@ } +//////////////////////////////////////////////////////// +-(NSPoint)cursorPosition +{ + return [myOGLView cursorPositionFromEvent:nil]; +} + + ////////////////////////////////////////////////////////. -(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y { diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index 1f5a1fa7a..dcc4e0123 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -287,6 +287,12 @@ [myOGLView setCursorPositionToX:x Y:y]; } +//////////////////////////////////////////////////////// +-(NSPoint)cursorPosition +{ + return [myOGLView cursorPositionFromEvent:nil]; +} + ////////////////////////////////////////////////////////. -(void)setWindowPositionToX:(unsigned int)x Y:(unsigned int)y diff --git a/src/SFML/Window/OSX/WindowImplCocoa.mm b/src/SFML/Window/OSX/WindowImplCocoa.mm index 578666234..6557f82d4 100644 --- a/src/SFML/Window/OSX/WindowImplCocoa.mm +++ b/src/SFML/Window/OSX/WindowImplCocoa.mm @@ -348,8 +348,8 @@ void WindowImplCocoa::SetCursorPosition(unsigned int x, unsigned int y) //////////////////////////////////////////////////////////// Vector2i WindowImplCocoa::GetCursorPosition() const { - // @to be implemented - return Vector2i(); + NSPoint pos = [myDelegate cursorPosition]; + return Vector2i(pos.x, pos.y); } diff --git a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h index e644eb1f2..94b296554 100644 --- a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h +++ b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h @@ -89,6 +89,12 @@ namespace sf { //////////////////////////////////////////////////////////// -(void)setCursorPositionToX:(unsigned int)x Y:(unsigned int)y; +//////////////////////////////////////////////////////////// +/// Get the mouse cursor position (SFML Coordinates). +/// +//////////////////////////////////////////////////////////// +-(NSPoint)cursorPosition; + //////////////////////////////////////////////////////////// /// Move the window (not the view if we handle not a window) (SFML Coordinates). ///