From cd1918b93d4af8d84f2f8af32c3b286a731edc6d Mon Sep 17 00:00:00 2001 From: Marco Antognini Date: Fri, 8 Jul 2011 13:35:38 +0200 Subject: [PATCH] Improved consistency in SFWindowController and added security check in SFOpenGLView --- src/SFML/Window/OSX/SFOpenGLView.mm | 98 +++++++++++++---------- src/SFML/Window/OSX/SFWindowController.mm | 2 +- 2 files changed, 57 insertions(+), 43 deletions(-) diff --git a/src/SFML/Window/OSX/SFOpenGLView.mm b/src/SFML/Window/OSX/SFOpenGLView.mm index 2b25185d..cb10daaf 100644 --- a/src/SFML/Window/OSX/SFOpenGLView.mm +++ b/src/SFML/Window/OSX/SFOpenGLView.mm @@ -379,7 +379,14 @@ sf::Keyboard::Key NonLocalizedKeys(unsigned short keycode); sf::Mouse::Button button = [self mouseButtonFromEvent:theEvent]; - myRequester->MouseDownAt(button, loc.x, loc.y); + if (button != sf::Mouse::ButtonCount) { + myRequester->MouseDownAt(button, loc.x, loc.y); + } +#ifdef SFML_DEBUG + else { + sf::Err() << "Unknown mouse button released." << std::endl; + } +#endif } @@ -392,7 +399,14 @@ sf::Keyboard::Key NonLocalizedKeys(unsigned short keycode); sf::Mouse::Button button = [self mouseButtonFromEvent:theEvent]; - myRequester->MouseUpAt(button, loc.x, loc.y); + if (button != sf::Mouse::ButtonCount) { + myRequester->MouseUpAt(button, loc.x, loc.y); + } +#ifdef SFML_DEBUG + else { + sf::Err() << "Unknown mouse button released." << std::endl; + } +#endif } @@ -426,6 +440,46 @@ sf::Keyboard::Key NonLocalizedKeys(unsigned short keycode); } +//////////////////////////////////////////////////////// +-(NSPoint)cursorPositionFromEvent:(NSEvent *)eventOrNil +{ + 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; + loc.y = h - loc.y; + + // Recompute the mouse pos if required. + if (!NSEqualSizes(myRealSize, NSZeroSize)) { + loc.x = loc.x * myRealSize.width / [self frame].size.width; + loc.y = loc.y * myRealSize.height / [self frame].size.height; + } + + return loc; +} + + +//////////////////////////////////////////////////////// +-(sf::Mouse::Button)mouseButtonFromEvent:(NSEvent *)event +{ + switch ([event buttonNumber]) { + case 0: return sf::Mouse::Left; + case 1: return sf::Mouse::Right; + case 2: return sf::Mouse::Middle; + case 3: return sf::Mouse::XButton1; + case 4: return sf::Mouse::XButton2; + default: return sf::Mouse::ButtonCount; // Never happens! (hopefully) + } +} + + #pragma mark #pragma mark Key-event methods @@ -898,46 +952,6 @@ sf::Keyboard::Key NonLocalizedKeys(unsigned short keycode); } -//////////////////////////////////////////////////////// --(NSPoint)cursorPositionFromEvent:(NSEvent *)eventOrNil -{ - 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; - loc.y = h - loc.y; - - // Recompute the mouse pos if required. - if (!NSEqualSizes(myRealSize, NSZeroSize)) { - loc.x = loc.x * myRealSize.width / [self frame].size.width; - loc.y = loc.y * myRealSize.height / [self frame].size.height; - } - - return loc; -} - - -//////////////////////////////////////////////////////// --(sf::Mouse::Button)mouseButtonFromEvent:(NSEvent *)event -{ - switch ([event buttonNumber]) { - case 0: return sf::Mouse::Left; - case 1: return sf::Mouse::Right; - case 2: return sf::Mouse::Middle; - case 3: return sf::Mouse::XButton1; - case 4: return sf::Mouse::XButton2; - default: return sf::Mouse::ButtonCount; // Never happens! (hopefully) - } -} - - //////////////////////////////////////////////////////// +(sf::Event::KeyEvent)convertNSKeyEventToSFMLEvent:(NSEvent *)anEvent { diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm index dcc4e012..4046041c 100644 --- a/src/SFML/Window/OSX/SFWindowController.mm +++ b/src/SFML/Window/OSX/SFWindowController.mm @@ -97,7 +97,7 @@ } // Set the view to the window as its content view. - [[myWindow contentView] addSubview:myOGLView]; + [myWindow setContentView:myOGLView]; } return self;