Fixed window/view bounds management with autorotation.
authorSam Lantinga <slouken@libsdl.org>
Tue, 18 Sep 2012 22:24:08 -0700
changeset 6437 3e52c17e2a07
parent 6436 29a35c72905a
child 6438 2c0fa683b18c
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.
src/video/uikit/SDL_uikitopengles.m
src/video/uikit/SDL_uikitviewcontroller.m
src/video/uikit/SDL_uikitwindow.m
--- a/src/video/uikit/SDL_uikitopengles.m	Tue Sep 18 01:45:15 2012 -0700
+++ b/src/video/uikit/SDL_uikitopengles.m	Tue Sep 18 22:24:08 2012 -0700
@@ -127,7 +127,7 @@
     }
 
     /* 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);
--- a/src/video/uikit/SDL_uikitviewcontroller.m	Tue Sep 18 01:45:15 2012 -0700
+++ b/src/video/uikit/SDL_uikitviewcontroller.m	Tue Sep 18 22:24:08 2012 -0700
@@ -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);
 }
 
--- a/src/video/uikit/SDL_uikitwindow.m	Tue Sep 18 01:45:15 2012 -0700
+++ b/src/video/uikit/SDL_uikitwindow.m	Tue Sep 18 22:24:08 2012 -0700
@@ -65,15 +65,22 @@
         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 @@
                 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 @@
             [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 @@
         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 @@
     /* 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 @@
 
     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 @@
             window->h = width;
         }
     } else {
-        if (uiwindow.frame.size.width > uiwindow.frame.size.height) {
+        if (width > height) {
             window->w = height;
             window->h = width;
         } else {