Fixed window/view bounds management with autorotation.

The trick is not to mess with the window frame and let iOS handle resizing the view automatically when the rotation occurs.
This commit is contained in:
Sam Lantinga 2012-09-18 22:24:08 -07:00
parent f74173a3d3
commit 4684591de2
3 changed files with 27 additions and 50 deletions

View file

@ -127,7 +127,7 @@ SDL_GLContext UIKit_GL_CreateContext(_THIS, SDL_Window * window)
}
/* add the view to our window */
[uiwindow addSubview: view ];
[uiwindow addSubview: view];
if ( UIKit_GL_MakeCurrent(_this, window, view) < 0 ) {
UIKit_GL_DeleteContext(_this, view);

View file

@ -112,39 +112,14 @@
{
const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
SDL_WindowData *data = self->window->driverdata;
UIWindow *uiwindow = data->uiwindow;
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
SDL_DisplayData *displaydata = (SDL_DisplayData *) display->driverdata;
SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
UIScreen *uiscreen = displaydata->uiscreen;
const int noborder = (self->window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS));
CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
const CGSize size = frame.size;
const CGSize size = data->view.bounds.size;
int w, h;
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
w = (size.width < size.height) ? size.width : size.height;
h = (size.width > size.height) ? size.width : size.height;
break;
w = (int)(size.width * displaymodedata->scale);
h = (int)(size.height * displaymodedata->scale);
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
w = (size.width > size.height) ? size.width : size.height;
h = (size.width < size.height) ? size.width : size.height;
break;
default:
SDL_assert(0 && "Unexpected interface orientation!");
return;
}
w = (int)(w * displaymodedata->scale);
h = (int)(h * displaymodedata->scale);
[uiwindow setFrame:frame];
[data->view setFrame:frame];
SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
}

View file

@ -65,15 +65,22 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
window->x = 0;
window->y = 0;
CGRect bounds;
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
bounds = [displaydata->uiscreen bounds];
} else {
bounds = [displaydata->uiscreen applicationFrame];
}
/* Get frame dimensions in pixels */
int width = (int)(uiwindow.frame.size.width * displaymodedata->scale);
int height = (int)(uiwindow.frame.size.height * displaymodedata->scale);
int width = (int)(bounds.size.width * displaymodedata->scale);
int height = (int)(bounds.size.height * displaymodedata->scale);
/* We can pick either width or height here and we'll rotate the
screen to match, so we pick the closest to what we wanted.
*/
if (window->w >= window->h) {
if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
if (width > height) {
window->w = width;
window->h = height;
} else {
@ -81,7 +88,7 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
window->h = width;
}
} else {
if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
if (width > height) {
window->w = height;
window->h = width;
} else {
@ -112,12 +119,6 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
[UIApplication sharedApplication].statusBarHidden = NO;
}
//const UIDeviceOrientation o = [[UIDevice currentDevice] orientation];
//const BOOL landscape = (o == UIDeviceOrientationLandscapeLeft) ||
// (o == UIDeviceOrientationLandscapeRight);
//const BOOL rotate = ( ((window->w > window->h) && (!landscape)) ||
// ((window->w < window->h) && (landscape)) );
// The View Controller will handle rotating the view when the
// device orientation changes. This will trigger resize events, if
// appropriate.
@ -125,7 +126,6 @@ static int SetupWindowData(_THIS, SDL_Window *window, UIWindow *uiwindow, SDL_bo
controller = [SDL_uikitviewcontroller alloc];
data->viewcontroller = [controller initWithSDLWindow:window];
[data->viewcontroller setTitle:@"SDL App"]; // !!! FIXME: hook up SDL_SetWindowTitle()
// !!! FIXME: if (rotate), force a "resize" right at the start
}
return 0;
@ -187,10 +187,7 @@ UIKit_CreateWindow(_THIS, SDL_Window *window)
/* ignore the size user requested, and make a fullscreen window */
// !!! FIXME: can we have a smaller view?
UIWindow *uiwindow = [UIWindow alloc];
if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS))
uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]];
else
uiwindow = [uiwindow initWithFrame:[data->uiscreen applicationFrame]];
uiwindow = [uiwindow initWithFrame:[data->uiscreen bounds]];
// put the window on an external display if appropriate. This implicitly
// does [uiwindow setframe:[uiscreen bounds]], so don't do it on the
@ -244,21 +241,26 @@ UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display
if (fullscreen) {
[UIApplication sharedApplication].statusBarHidden = YES;
uiwindow.frame = [displaydata->uiscreen bounds];
} else {
[UIApplication sharedApplication].statusBarHidden = NO;
uiwindow.frame = [displaydata->uiscreen applicationFrame];
}
CGRect bounds;
if (fullscreen) {
bounds = [displaydata->uiscreen bounds];
} else {
bounds = [displaydata->uiscreen applicationFrame];
}
/* Get frame dimensions in pixels */
int width = (int)(uiwindow.frame.size.width * displaymodedata->scale);
int height = (int)(uiwindow.frame.size.height * displaymodedata->scale);
int width = (int)(bounds.size.width * displaymodedata->scale);
int height = (int)(bounds.size.height * displaymodedata->scale);
/* We can pick either width or height here and we'll rotate the
screen to match, so we pick the closest to what we wanted.
*/
if (window->w >= window->h) {
if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
if (width > height) {
window->w = width;
window->h = height;
} else {
@ -266,7 +268,7 @@ UIKit_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display
window->h = width;
}
} else {
if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
if (width > height) {
window->w = height;
window->h = width;
} else {