src/video/uikit/SDL_uikitviewcontroller.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 10 Nov 2011 03:48:59 -0500
changeset 6077 ee00e4b80f79
parent 6044 35448a5ea044
child 6079 d7135957b216
permissions -rw-r--r--
Work in progress fixing support for rotated video modes
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     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
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    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
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_UIKIT
    24 
    25 #include "SDL_video.h"
    26 #include "SDL_assert.h"
    27 #include "SDL_hints.h"
    28 #include "../SDL_sysvideo.h"
    29 #include "../../events/SDL_events_c.h"
    30 
    31 #include "SDL_uikitwindow.h"
    32 
    33 #import "SDL_uikitviewcontroller.h"
    34 
    35 
    36 @implementation SDL_uikitviewcontroller
    37 
    38 @synthesize window;
    39 
    40 - (id)initWithSDLWindow:(SDL_Window *)_window
    41 {
    42     self = [self init];
    43     if (self == nil) {
    44         return nil;
    45     }
    46     self.window = _window;
    47     return self;
    48 }
    49 
    50 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
    51 {
    52     const char *orientationsCString;
    53     if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
    54         BOOL rotate = NO;
    55         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    56                                                             encoding:NSUTF8StringEncoding];
    57         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    58                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    59 
    60         switch (orient) {
    61             case UIInterfaceOrientationLandscapeLeft:
    62                 rotate = [orientations containsObject:@"LandscapeLeft"];
    63                 break;
    64 
    65             case UIInterfaceOrientationLandscapeRight:
    66                 rotate = [orientations containsObject:@"LandscapeRight"];
    67                 break;
    68 
    69             case UIInterfaceOrientationPortrait:
    70                 rotate = [orientations containsObject:@"Portrait"];
    71                 break;
    72 
    73             case UIInterfaceOrientationPortraitUpsideDown:
    74                 rotate = [orientations containsObject:@"PortraitUpsideDown"];
    75                 break;
    76 
    77             default: break;
    78         }
    79 
    80         return rotate;
    81     }
    82 
    83     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    84         return YES;  // any orientation is okay.
    85     }
    86 
    87     // If not resizable, allow device to orient to other matching sizes
    88     //  (that is, let the user turn the device upside down...same screen
    89     //   dimensions, but it lets the user place the device where it's most
    90     //   comfortable in relation to its physical buttons, headphone jack, etc).
    91     switch (orient) {
    92         case UIInterfaceOrientationLandscapeLeft:
    93         case UIInterfaceOrientationLandscapeRight:
    94             return (self->window->w >= self->window->h);
    95 
    96         case UIInterfaceOrientationPortrait:
    97         case UIInterfaceOrientationPortraitUpsideDown:
    98             return (self->window->h >= self->window->w);
    99 
   100         default: break;
   101     }
   102 
   103     return NO;  // Nothing else is acceptable.
   104 }
   105 
   106 - (void)loadView
   107 {
   108     // do nothing.
   109 }
   110 
   111 // Send a resized event when the orientation changes.
   112 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
   113 {
   114     const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   115     SDL_WindowData *data = self->window->driverdata;
   116     UIWindow *uiwindow = data->uiwindow;
   117     UIScreen *uiscreen = [uiwindow screen];
   118     const int noborder = (self->window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS));
   119     CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   120     const CGSize size = frame.size;
   121     int w, h;
   122 
   123     switch (toInterfaceOrientation) {
   124         case UIInterfaceOrientationPortrait:
   125         case UIInterfaceOrientationPortraitUpsideDown:
   126             w = (size.width < size.height) ? size.width : size.height;
   127             h = (size.width > size.height) ? size.width : size.height;
   128             break;
   129 
   130         case UIInterfaceOrientationLandscapeLeft:
   131         case UIInterfaceOrientationLandscapeRight:
   132             w = (size.width > size.height) ? size.width : size.height;
   133             h = (size.width < size.height) ? size.width : size.height;
   134             break;
   135 
   136         default:
   137             SDL_assert(0 && "Unexpected interface orientation!");
   138             return;
   139     }
   140 
   141     if (w == frame.size.width && h == frame.size.height) {
   142         return;
   143     }
   144 
   145     frame.size.width = w;
   146     frame.size.height = h;
   147     frame.origin.x = 0;
   148     frame.origin.y = 0;
   149 
   150     [uiwindow setFrame:frame];
   151     [data->view updateFrame];
   152     SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
   153 }
   154 
   155 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   156 
   157 @end