src/video/uikit/SDL_uikitviewcontroller.m
changeset 6001 3ae3d1e58d59
parent 5640 04b5b11985c8
child 6003 fddf81967e2d
equal deleted inserted replaced
5965:dd0c84d3d846 6001:3ae3d1e58d59
     1 /*
     1 /*
     2  Simple DirectMedia Layer
     2  Simple DirectMedia Layer
     3  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4  
     4 
     5  This software is provided 'as-is', without any express or implied
     5  This software is provided 'as-is', without any express or implied
     6  warranty.  In no event will the authors be held liable for any damages
     6  warranty.  In no event will the authors be held liable for any damages
     7  arising from the use of this software.
     7  arising from the use of this software.
     8  
     8 
     9  Permission is granted to anyone to use this software for any purpose,
     9  Permission is granted to anyone to use this software for any purpose,
    10  including commercial applications, and to alter it and redistribute it
    10  including commercial applications, and to alter it and redistribute it
    11  freely, subject to the following restrictions:
    11  freely, subject to the following restrictions:
    12  
    12 
    13  1. The origin of this software must not be misrepresented; you must not
    13  1. The origin of this software must not be misrepresented; you must not
    14  claim that you wrote the original software. If you use this software
    14  claim that you wrote the original software. If you use this software
    15  in a product, an acknowledgment in the product documentation would be
    15  in a product, an acknowledgment in the product documentation would be
    16  appreciated but is not required.
    16  appreciated but is not required.
    17  2. Altered source versions must be plainly marked as such, and must not be
    17  2. Altered source versions must be plainly marked as such, and must not be
    47         BOOL rotate = NO;
    47         BOOL rotate = NO;
    48         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    48         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    49                                                             encoding:NSUTF8StringEncoding];
    49                                                             encoding:NSUTF8StringEncoding];
    50         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    50         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    51                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    51                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    52         
    52 
    53         switch (orient) {
    53         switch (orient) {
    54             case UIInterfaceOrientationLandscapeLeft:
    54             case UIInterfaceOrientationLandscapeLeft:
    55                 rotate = [orientations containsObject:@"LandscapeLeft"];
    55                 rotate = [orientations containsObject:@"LandscapeLeft"];
    56                 break;
    56                 break;
    57                 
    57 
    58             case UIInterfaceOrientationLandscapeRight:
    58             case UIInterfaceOrientationLandscapeRight:
    59                 rotate = [orientations containsObject:@"LandscapeRight"];
    59                 rotate = [orientations containsObject:@"LandscapeRight"];
    60                 break;
    60                 break;
    61                 
    61 
    62             case UIInterfaceOrientationPortrait:
    62             case UIInterfaceOrientationPortrait:
    63                 rotate = [orientations containsObject:@"Portrait"];
    63                 rotate = [orientations containsObject:@"Portrait"];
    64                 break;
    64                 break;
    65                 
    65 
    66             case UIInterfaceOrientationPortraitUpsideDown:
    66             case UIInterfaceOrientationPortraitUpsideDown:
    67                 rotate = [orientations containsObject:@"PortraitUpsideDown"];
    67                 rotate = [orientations containsObject:@"PortraitUpsideDown"];
    68                 break;
    68                 break;
    69                 
    69 
    70             default: break;
    70             default: break;
    71         }
    71         }
    72         
    72 
    73         return rotate;
    73         return rotate;
    74     }
    74     }
    75     
    75 
    76     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    76     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    77         return YES;  // any orientation is okay.
    77         return YES;  // any orientation is okay.
    78     }
    78     }
    79     
    79 
    80     // If not resizable, allow device to orient to other matching sizes
    80     // If not resizable, allow device to orient to other matching sizes
    81     //  (that is, let the user turn the device upside down...same screen
    81     //  (that is, let the user turn the device upside down...same screen
    82     //   dimensions, but it lets the user place the device where it's most
    82     //   dimensions, but it lets the user place the device where it's most
    83     //   comfortable in relation to its physical buttons, headphone jack, etc).
    83     //   comfortable in relation to its physical buttons, headphone jack, etc).
    84     switch (orient) {
    84     switch (orient) {
    85         case UIInterfaceOrientationLandscapeLeft:
    85         case UIInterfaceOrientationLandscapeLeft:
    86         case UIInterfaceOrientationLandscapeRight:
    86         case UIInterfaceOrientationLandscapeRight:
    87             return (self->window->w >= self->window->h);
    87             return (self->window->w >= self->window->h);
    88             
    88 
    89         case UIInterfaceOrientationPortrait:
    89         case UIInterfaceOrientationPortrait:
    90         case UIInterfaceOrientationPortraitUpsideDown:
    90         case UIInterfaceOrientationPortraitUpsideDown:
    91             return (self->window->h >= self->window->w);
    91             return (self->window->h >= self->window->w);
    92             
    92 
    93         default: break;
    93         default: break;
    94     }
    94     }
    95     
    95 
    96     return NO;  // Nothing else is acceptable.
    96     return NO;  // Nothing else is acceptable.
    97 }
    97 }
    98 
    98 
    99 - (void)loadView  {
    99 - (void)loadView  {
   100     // do nothing.
   100     // do nothing.
   103 // Send a resized event when the orientation changes.
   103 // Send a resized event when the orientation changes.
   104 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
   104 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
   105     if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
   105     if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
   106         return;   // don't care, we're just flipping over in this case.
   106         return;   // don't care, we're just flipping over in this case.
   107     }
   107     }
   108     
   108 
   109     const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   109     const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   110     SDL_WindowData *data = self->window->driverdata;
   110     SDL_WindowData *data = self->window->driverdata;
   111     UIWindow *uiwindow = data->uiwindow;
   111     UIWindow *uiwindow = data->uiwindow;
   112     UIScreen *uiscreen = [uiwindow screen];
   112     UIScreen *uiscreen = [uiwindow screen];
   113     const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
   113     const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
   114     CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   114     CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   115     const CGSize size = frame.size;
   115     const CGSize size = frame.size;
   116     int w, h;
   116     int w, h;
   117     
   117 
   118     switch (toInterfaceOrientation) {
   118     switch (toInterfaceOrientation) {
   119         case UIInterfaceOrientationPortrait:
   119         case UIInterfaceOrientationPortrait:
   120         case UIInterfaceOrientationPortraitUpsideDown:
   120         case UIInterfaceOrientationPortraitUpsideDown:
   121             w = (size.width < size.height) ? size.width : size.height;
   121             w = (size.width < size.height) ? size.width : size.height;
   122             h = (size.width > size.height) ? size.width : size.height;
   122             h = (size.width > size.height) ? size.width : size.height;
   123             break;
   123             break;
   124             
   124 
   125         case UIInterfaceOrientationLandscapeLeft:
   125         case UIInterfaceOrientationLandscapeLeft:
   126         case UIInterfaceOrientationLandscapeRight:
   126         case UIInterfaceOrientationLandscapeRight:
   127             w = (size.width > size.height) ? size.width : size.height;
   127             w = (size.width > size.height) ? size.width : size.height;
   128             h = (size.width < size.height) ? size.width : size.height;
   128             h = (size.width < size.height) ? size.width : size.height;
   129             break;
   129             break;
   130             
   130 
   131         default:
   131         default:
   132             SDL_assert(0 && "Unexpected interface orientation!");
   132             SDL_assert(0 && "Unexpected interface orientation!");
   133             return;
   133             return;
   134     }
   134     }
   135     
   135 
   136     frame.size.width = w;
   136     frame.size.width = w;
   137     frame.size.height = h;
   137     frame.size.height = h;
   138     frame.origin.x = 0;
   138     frame.origin.x = 0;
   139     frame.origin.y = 0;
   139     frame.origin.y = 0;
   140     
   140 
   141     [uiwindow setFrame:frame];
   141     [uiwindow setFrame:frame];
   142     [data->view updateFrame];
   142     [data->view updateFrame];
   143     SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
   143     SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
   144 }
   144 }
   145 
   145