diff --git a/src/SFML/Window/OSX/SFOpenGLView.h b/src/SFML/Window/OSX/SFOpenGLView.h index f65da562..d48d6f76 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.h +++ b/src/SFML/Window/OSX/SFOpenGLView.h @@ -133,4 +133,12 @@ namespace sf { //////////////////////////////////////////////////////////// -(NSPoint)cursorPositionFromEvent:(NSEvent*)eventOrNil; +//////////////////////////////////////////////////////////// +/// \brief Determine where the mouse is +/// +/// \return true when the mouse is inside the OpenGL view, false otherwise +/// +//////////////////////////////////////////////////////////// +-(BOOL)isMouseInside; + @end diff --git a/src/SFML/Window/OSX/SFOpenGLView.mm b/src/SFML/Window/OSX/SFOpenGLView.mm index d08c63ca..3381df65 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.mm +++ b/src/SFML/Window/OSX/SFOpenGLView.mm @@ -67,14 +67,6 @@ BOOL isValidTextUnicode(NSEvent* event); //////////////////////////////////////////////////////////// -(void)viewDidEndLiveResize; -//////////////////////////////////////////////////////////// -/// \brief Determine where the mouse is -/// -/// \return true when the mouse is inside the OpenGL view, false otherwise -/// -//////////////////////////////////////////////////////////// --(BOOL)isMouseInside; - //////////////////////////////////////////////////////////// /// \brief Update the mouse state (in or out) /// diff --git a/src/SFML/Window/OSX/SFViewController.mm b/src/SFML/Window/OSX/SFViewController.mm index 05e2cccf..2b767691 100644 --- a/src/SFML/Window/OSX/SFViewController.mm +++ b/src/SFML/Window/OSX/SFViewController.mm @@ -115,16 +115,9 @@ //////////////////////////////////////////////////////// --(void)hideMouseCursor +-(BOOL)isMouseInside { - [NSCursor hide]; -} - - -//////////////////////////////////////////////////////// --(void)showMouseCursor -{ - [NSCursor unhide]; + return [m_oglView isMouseInside]; } diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index 3caf1258..a98b4038 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -332,16 +332,9 @@ //////////////////////////////////////////////////////// --(void)hideMouseCursor +-(BOOL)isMouseInside { - [NSCursor hide]; -} - - -//////////////////////////////////////////////////////// --(void)showMouseCursor -{ - [NSCursor unhide]; + return [m_oglView isMouseInside]; } diff --git a/src/SFML/Window/OSX/WindowImplCocoa.mm b/src/SFML/Window/OSX/WindowImplCocoa.mm index 6de74d23..adff0225 100644 --- a/src/SFML/Window/OSX/WindowImplCocoa.mm +++ b/src/SFML/Window/OSX/WindowImplCocoa.mm @@ -112,6 +112,41 @@ void scaleOutXY(T& out, id delegate) scaleOut(out.y, delegate); } + +//////////////////////////////////////////////////////////// +/// According to Apple's documentation, each invocation of +/// unhide must be balanced by an invocation of hide in +/// order for the cursor display to be correct. +/// So we keep track of those calls ourself. +//////////////////////////////////////////////////////////// + +namespace +{ + bool isCursorHidden = false; // initially, the cursor is visible +} + + +//////////////////////////////////////////////////////// +void hideMouseCursor() +{ + if (!isCursorHidden) + { + [NSCursor hide]; + isCursorHidden = true; + } +} + + +//////////////////////////////////////////////////////// +void showMouseCursor() +{ + if (isCursorHidden) + { + [NSCursor unhide]; + isCursorHidden = false; + } +} + #pragma mark #pragma mark WindowImplCocoa's ctor/dtor @@ -268,8 +303,8 @@ void WindowImplCocoa::windowResized(unsigned int width, unsigned int height) //////////////////////////////////////////////////////////// void WindowImplCocoa::windowLostFocus(void) { - if (!m_showCursor) - [m_delegate showMouseCursor]; // Make sure the cursor is visible + if (!m_showCursor && [m_delegate isMouseInside]) + showMouseCursor(); // Make sure the cursor is visible Event event; event.type = Event::LostFocus; @@ -281,8 +316,8 @@ void WindowImplCocoa::windowLostFocus(void) //////////////////////////////////////////////////////////// void WindowImplCocoa::windowGainedFocus(void) { - if (!m_showCursor) - [m_delegate hideMouseCursor]; // Restore user's setting + if (!m_showCursor && [m_delegate isMouseInside]) + hideMouseCursor(); // Restore user's setting Event event; event.type = Event::GainedFocus; @@ -351,7 +386,7 @@ void WindowImplCocoa::mouseWheelScrolledAt(float delta, int x, int y) void WindowImplCocoa::mouseMovedIn(void) { if (!m_showCursor) - [m_delegate hideMouseCursor]; // Restore user's setting + hideMouseCursor(); // Restore user's setting Event event; event.type = Event::MouseEntered; @@ -363,7 +398,7 @@ void WindowImplCocoa::mouseMovedIn(void) void WindowImplCocoa::mouseMovedOut(void) { if (!m_showCursor) - [m_delegate showMouseCursor]; // Make sure the cursor is visible + showMouseCursor(); // Make sure the cursor is visible Event event; event.type = Event::MouseLeft; @@ -496,10 +531,14 @@ void WindowImplCocoa::setMouseCursorVisible(bool visible) { m_showCursor = visible; - if (m_showCursor) - [m_delegate showMouseCursor]; - else - [m_delegate hideMouseCursor]; + // If the mouse is over the window, we apply the new setting + if ([m_delegate isMouseInside]) + { + if (m_showCursor) + showMouseCursor(); + else + hideMouseCursor(); + } } diff --git a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h index 0ffc0ffb..b43805a6 100644 --- a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h +++ b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h @@ -89,16 +89,12 @@ namespace sf { -(sf::WindowHandle)getSystemHandle; //////////////////////////////////////////////////////////// -/// \brief Hide the mouse cursor +/// \brief Determine where the mouse is +/// +/// \return true when the mouse is inside the OpenGL view, false otherwise /// //////////////////////////////////////////////////////////// --(void)hideMouseCursor; - -//////////////////////////////////////////////////////////// -/// \brief Show the mouse cursor -/// -//////////////////////////////////////////////////////////// --(void)showMouseCursor; +-(BOOL)isMouseInside; //////////////////////////////////////////////////////////// /// \brief Get window position