Fixed mismatching orientations for the window width and height on iOS when the window is created or the app is brought to the foreground, when SDL_HINT_ORIENTATIONS or SDL_WINDOW_FULLSCREEN is used. iOS-improvements
authorAlex Szpakowski <slime73@gmail.com>
Thu, 20 Nov 2014 17:19:26 -0400
branchiOS-improvements
changeset 95239cbd69cac7ed
parent 9522 0d0026c1805a
child 9524 9de159f9cefd
Fixed mismatching orientations for the window width and height on iOS when the window is created or the app is brought to the foreground, when SDL_HINT_ORIENTATIONS or SDL_WINDOW_FULLSCREEN is used.
src/video/uikit/SDL_uikitviewcontroller.m
src/video/uikit/SDL_uikitwindow.h
src/video/uikit/SDL_uikitwindow.m
     1.1 --- a/src/video/uikit/SDL_uikitviewcontroller.m	Sat Nov 15 22:59:16 2014 -0400
     1.2 +++ b/src/video/uikit/SDL_uikitviewcontroller.m	Thu Nov 20 17:19:26 2014 -0400
     1.3 @@ -62,44 +62,7 @@
     1.4  
     1.5  - (NSUInteger)supportedInterfaceOrientations
     1.6  {
     1.7 -    NSUInteger orientationMask = 0;
     1.8 -    const char *hint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
     1.9 -
    1.10 -    if (hint != NULL) {
    1.11 -        NSArray *orientations = [@(hint) componentsSeparatedByString:@" "];
    1.12 -
    1.13 -        if ([orientations containsObject:@"LandscapeLeft"]) {
    1.14 -            orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
    1.15 -        }
    1.16 -        if ([orientations containsObject:@"LandscapeRight"]) {
    1.17 -            orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
    1.18 -        }
    1.19 -        if ([orientations containsObject:@"Portrait"]) {
    1.20 -            orientationMask |= UIInterfaceOrientationMaskPortrait;
    1.21 -        }
    1.22 -        if ([orientations containsObject:@"PortraitUpsideDown"]) {
    1.23 -            orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
    1.24 -        }
    1.25 -    }
    1.26 -
    1.27 -    if (orientationMask == 0 && (window->flags & SDL_WINDOW_RESIZABLE)) {
    1.28 -        orientationMask = UIInterfaceOrientationMaskAll;  /* any orientation is okay. */
    1.29 -    }
    1.30 -
    1.31 -    if (orientationMask == 0) {
    1.32 -        if (window->w >= window->h) {
    1.33 -            orientationMask |= UIInterfaceOrientationMaskLandscape;
    1.34 -        }
    1.35 -        if (window->h >= window->w) {
    1.36 -            orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
    1.37 -        }
    1.38 -    }
    1.39 -
    1.40 -    /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
    1.41 -    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    1.42 -        orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
    1.43 -    }
    1.44 -    return orientationMask;
    1.45 +    return UIKit_GetSupportedOrientations(window);
    1.46  }
    1.47  
    1.48  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
    1.49 @@ -119,7 +82,7 @@
    1.50  
    1.51  - (UIStatusBarStyle)preferredStatusBarStyle
    1.52  {
    1.53 -    /* We assume most games don't have a bright white background. */
    1.54 +    /* We assume most SDL apps don't have a bright white background. */
    1.55      return UIStatusBarStyleLightContent;
    1.56  }
    1.57  
     2.1 --- a/src/video/uikit/SDL_uikitwindow.h	Sat Nov 15 22:59:16 2014 -0400
     2.2 +++ b/src/video/uikit/SDL_uikitwindow.h	Thu Nov 20 17:19:26 2014 -0400
     2.3 @@ -36,6 +36,8 @@
     2.4  extern SDL_bool UIKit_GetWindowWMInfo(_THIS, SDL_Window * window,
     2.5                                        struct SDL_SysWMinfo * info);
     2.6  
     2.7 +extern NSUInteger UIKit_GetSupportedOrientations(SDL_Window * window);
     2.8 +
     2.9  @class UIWindow;
    2.10  
    2.11  @interface SDL_uikitwindow : UIWindow
     3.1 --- a/src/video/uikit/SDL_uikitwindow.m	Sat Nov 15 22:59:16 2014 -0400
     3.2 +++ b/src/video/uikit/SDL_uikitwindow.m	Thu Nov 20 17:19:26 2014 -0400
     3.3 @@ -80,9 +80,6 @@
     3.4  
     3.5      /* Fill in the SDL window with the window data */
     3.6      {
     3.7 -        window->x = 0;
     3.8 -        window->y = 0;
     3.9 -
    3.10          CGRect frame = UIKit_ComputeViewFrame(window, displaydata.uiscreen);
    3.11  
    3.12          /* Get frame dimensions */
    3.13 @@ -96,6 +93,8 @@
    3.14              height = temp;
    3.15          }
    3.16  
    3.17 +        window->x = 0;
    3.18 +        window->y = 0;
    3.19          window->w = width;
    3.20          window->h = height;
    3.21      }
    3.22 @@ -182,21 +181,32 @@
    3.23          }
    3.24  
    3.25          if (data.uiscreen == [UIScreen mainScreen]) {
    3.26 +            NSUInteger orientations = UIKit_GetSupportedOrientations(window);
    3.27 +            UIApplication *app = [UIApplication sharedApplication];
    3.28 +
    3.29              if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
    3.30 -                [UIApplication sharedApplication].statusBarHidden = YES;
    3.31 +                app.statusBarHidden = YES;
    3.32              } else {
    3.33 -                [UIApplication sharedApplication].statusBarHidden = NO;
    3.34 +                app.statusBarHidden = NO;
    3.35              }
    3.36 -        }
    3.37  
    3.38 -        if (!(window->flags & SDL_WINDOW_RESIZABLE)) {
    3.39 -            if (window->w > window->h) {
    3.40 -                if (!UIKit_IsDisplayLandscape(data.uiscreen)) {
    3.41 -                    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
    3.42 +            /* Make sure the screen is using a supported orientation. We do it
    3.43 +             * now so that SetupWindowData assigns the properly oriented width
    3.44 +             * and height to the window's w and h variables.
    3.45 +             */
    3.46 +            if (UIKit_IsDisplayLandscape(data.uiscreen)) {
    3.47 +                if (!(orientations & UIInterfaceOrientationMaskLandscape)) {
    3.48 +                    [app setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    3.49                  }
    3.50 -            } else if (window->w < window->h) {
    3.51 -                if (UIKit_IsDisplayLandscape(data.uiscreen)) {
    3.52 -                    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
    3.53 +            } else {
    3.54 +                if (!(orientations & (UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown))) {
    3.55 +                    UIInterfaceOrientation orient = UIInterfaceOrientationLandscapeLeft;
    3.56 +
    3.57 +                    if (orientations & UIInterfaceOrientationMaskLandscapeRight) {
    3.58 +                        orient = UIInterfaceOrientationLandscapeRight;
    3.59 +                    }
    3.60 +
    3.61 +                    [app setStatusBarOrientation:orient animated:NO];
    3.62                  }
    3.63              }
    3.64          }
    3.65 @@ -228,7 +238,6 @@
    3.66  {
    3.67      @autoreleasepool {
    3.68          UIWindow *uiwindow = ((__bridge SDL_WindowData *) window->driverdata).uiwindow;
    3.69 -
    3.70          [uiwindow makeKeyAndVisible];
    3.71      }
    3.72  }
    3.73 @@ -238,7 +247,6 @@
    3.74  {
    3.75      @autoreleasepool {
    3.76          UIWindow *uiwindow = ((__bridge SDL_WindowData *) window->driverdata).uiwindow;
    3.77 -
    3.78          uiwindow.hidden = YES;
    3.79      }
    3.80  }
    3.81 @@ -341,6 +349,53 @@
    3.82      }
    3.83  }
    3.84  
    3.85 +NSUInteger
    3.86 +UIKit_GetSupportedOrientations(SDL_Window * window)
    3.87 +{
    3.88 +    const char *hint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
    3.89 +    NSUInteger orientationMask = 0;
    3.90 +
    3.91 +    @autoreleasepool {
    3.92 +        if (hint != NULL) {
    3.93 +            NSArray *orientations = [@(hint) componentsSeparatedByString:@" "];
    3.94 +
    3.95 +            if ([orientations containsObject:@"LandscapeLeft"]) {
    3.96 +                orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
    3.97 +            }
    3.98 +            if ([orientations containsObject:@"LandscapeRight"]) {
    3.99 +                orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
   3.100 +            }
   3.101 +            if ([orientations containsObject:@"Portrait"]) {
   3.102 +                orientationMask |= UIInterfaceOrientationMaskPortrait;
   3.103 +            }
   3.104 +            if ([orientations containsObject:@"PortraitUpsideDown"]) {
   3.105 +                orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
   3.106 +            }
   3.107 +        }
   3.108 +
   3.109 +        if (orientationMask == 0 && (window->flags & SDL_WINDOW_RESIZABLE)) {
   3.110 +            /* any orientation is okay. */
   3.111 +            orientationMask = UIInterfaceOrientationMaskAll;
   3.112 +        }
   3.113 +
   3.114 +        if (orientationMask == 0) {
   3.115 +            if (window->w >= window->h) {
   3.116 +                orientationMask |= UIInterfaceOrientationMaskLandscape;
   3.117 +            }
   3.118 +            if (window->h >= window->w) {
   3.119 +                orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
   3.120 +            }
   3.121 +        }
   3.122 +
   3.123 +        /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
   3.124 +        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
   3.125 +            orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
   3.126 +        }
   3.127 +    }
   3.128 +
   3.129 +    return orientationMask;
   3.130 +}
   3.131 +
   3.132  int
   3.133  SDL_iPhoneSetAnimationCallback(SDL_Window * window, int interval, void (*callback)(void*), void *callbackParam)
   3.134  {