Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed autorotate / resize support on iOS 6.0, tested with iPad and iP…
Browse files Browse the repository at this point in the history
…hone simulators with iOS 5.1 and 6.0
  • Loading branch information
slouken committed Sep 23, 2012
1 parent 11f100d commit e1617d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 71 deletions.
10 changes: 7 additions & 3 deletions src/video/uikit/SDL_uikitopengles.m
Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions src/video/uikit/SDL_uikitviewcontroller.h
Expand Up @@ -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
103 changes: 38 additions & 65 deletions src/video/uikit/SDL_uikitviewcontroller.m
Expand Up @@ -44,9 +44,6 @@ - (id)initWithSDLWindow:(SDL_Window *)_window
}
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;
}

Expand All @@ -55,97 +52,73 @@ - (void)loadView
// 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;
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
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;
NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
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.
}

// 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;

// Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
}

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: */

0 comments on commit e1617d8

Please sign in to comment.