From 749e6da2e8819ca52f585d6a085b1f14a0020945 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 23 Sep 2012 03:46:49 -0700 Subject: [PATCH] Fixed autorotate / resize support on iOS 6.0, tested with iPad and iPhone simulators with iOS 5.1 and 6.0 --- src/video/uikit/SDL_uikitopengles.m | 10 ++- src/video/uikit/SDL_uikitviewcontroller.h | 5 +- src/video/uikit/SDL_uikitviewcontroller.m | 103 ++++++++-------------- 3 files changed, 47 insertions(+), 71 deletions(-) diff --git a/src/video/uikit/SDL_uikitopengles.m b/src/video/uikit/SDL_uikitopengles.m index 70721d2c8..a65407b49 100755 --- a/src/video/uikit/SDL_uikitopengles.m +++ b/src/video/uikit/SDL_uikitopengles.m @@ -131,9 +131,13 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window) [view->viewcontroller setView:view]; [view->viewcontroller retain]; } - - /* add the view to our window */ - [uiwindow addSubview: view]; + + // The view controller needs to be the root in order to control rotation on iOS 6.0 + if (uiwindow.rootViewController == nil) { + uiwindow.rootViewController = view->viewcontroller; + } else { + [uiwindow addSubview: view]; + } if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) { UIKit_GL_DeleteContext(_this, view); diff --git a/src/video/uikit/SDL_uikitviewcontroller.h b/src/video/uikit/SDL_uikitviewcontroller.h index a5a90428d..2c4ea7efe 100755 --- a/src/video/uikit/SDL_uikitviewcontroller.h +++ b/src/video/uikit/SDL_uikitviewcontroller.h @@ -32,9 +32,8 @@ - (id)initWithSDLWindow:(SDL_Window *)_window; - (void)loadView; -- (void)statusBarFrameChanged:(NSNotification*)notification; -- (void)onWindowSizeChanged; +- (void)viewDidLayoutSubviews; +- (NSUInteger)supportedInterfaceOrientations; - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient; -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation; @end diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m index be508b40c..0c966a339 100755 --- a/src/video/uikit/SDL_uikitviewcontroller.m +++ b/src/video/uikit/SDL_uikitviewcontroller.m @@ -44,9 +44,6 @@ } self.window = _window; - // Register for notification when the status bar size changes - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; - return self; } @@ -55,12 +52,7 @@ // do nothing. } -- (void)statusBarFrameChanged:(NSNotification*)notification -{ - [self onWindowSizeChanged]; -} - -- (void)onWindowSizeChanged +- (void)viewDidLayoutSubviews { if (self->window->flags & SDL_WINDOW_RESIZABLE) { SDL_WindowData *data = self->window->driverdata; @@ -68,22 +60,18 @@ SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata; const CGSize size = data->view.bounds.size; int w, h; - + w = (int)(size.width * displaymodedata->scale); h = (int)(size.height * displaymodedata->scale); - + SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h); } } -- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient +- (NSUInteger)supportedInterfaceOrientations { - // Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation - if (orient == UIInterfaceOrientationPortraitUpsideDown) { - if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) - return NO; - } - + NSUInteger orientationMask = 0; + const char *orientationsCString; if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) { BOOL rotate = NO; @@ -91,61 +79,46 @@ encoding:NSUTF8StringEncoding]; NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:@" "]]; - - switch (orient) { - case UIInterfaceOrientationLandscapeLeft: - rotate = [orientations containsObject:@"LandscapeLeft"]; - break; - - case UIInterfaceOrientationLandscapeRight: - rotate = [orientations containsObject:@"LandscapeRight"]; - break; - - case UIInterfaceOrientationPortrait: - rotate = [orientations containsObject:@"Portrait"]; - break; - - case UIInterfaceOrientationPortraitUpsideDown: - rotate = [orientations containsObject:@"PortraitUpsideDown"]; - break; - - default: break; + + if ([orientations containsObject:@"LandscapeLeft"]) { + orientationMask |= UIInterfaceOrientationMaskLandscapeLeft; + } + if ([orientations containsObject:@"LandscapeRight"]) { + orientationMask |= UIInterfaceOrientationMaskLandscapeRight; + } + if ([orientations containsObject:@"Portrait"]) { + orientationMask |= UIInterfaceOrientationMaskPortrait; + } + if ([orientations containsObject:@"PortraitUpsideDown"]) { + orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown; + } + + } else if (self->window->flags & SDL_WINDOW_RESIZABLE) { + orientationMask = UIInterfaceOrientationMaskAll; // any orientation is okay. + } else { + if (self->window->w >= self->window->h) { + orientationMask |= UIInterfaceOrientationMaskLandscape; + } + if (self->window->h >= self->window->w) { + orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown); } - - return rotate; } - - if (self->window->flags & SDL_WINDOW_RESIZABLE) { - return YES; // any orientation is okay. + + // Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation + if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { + orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown; } - - // If not resizable, allow device to orient to other matching sizes - // (that is, let the user turn the device upside down...same screen - // dimensions, but it lets the user place the device where it's most - // comfortable in relation to its physical buttons, headphone jack, etc). - switch (orient) { - case UIInterfaceOrientationLandscapeLeft: - case UIInterfaceOrientationLandscapeRight: - return (self->window->w >= self->window->h); - - case UIInterfaceOrientationPortrait: - case UIInterfaceOrientationPortraitUpsideDown: - return (self->window->h >= self->window->w); - - default: break; - } - - return NO; // Nothing else is acceptable. + return orientationMask; } -// Send a resized event when the orientation changes. -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient { - [self onWindowSizeChanged]; + NSUInteger orientationMask = [self supportedInterfaceOrientations]; + return (orientationMask & (1 << orient)); } -#endif /* SDL_VIDEO_DRIVER_UIKIT */ - @end +#endif /* SDL_VIDEO_DRIVER_UIKIT */ + /* vi: set ts=4 sw=4 expandtab: */