src/video/uikit/SDL_uikitviewcontroller.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 6011 83a21d3bd5c1
child 6077 ee00e4b80f79
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
     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     if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
   115         return;   // don't care, we're just flipping over in this case.
   116     }
   117 
   118     const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   119     SDL_WindowData *data = self->window->driverdata;
   120     UIWindow *uiwindow = data->uiwindow;
   121     UIScreen *uiscreen = [uiwindow screen];
   122     const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
   123     CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   124     const CGSize size = frame.size;
   125     int w, h;
   126 
   127     switch (toInterfaceOrientation) {
   128         case UIInterfaceOrientationPortrait:
   129         case UIInterfaceOrientationPortraitUpsideDown:
   130             w = (size.width < size.height) ? size.width : size.height;
   131             h = (size.width > size.height) ? size.width : size.height;
   132             break;
   133 
   134         case UIInterfaceOrientationLandscapeLeft:
   135         case UIInterfaceOrientationLandscapeRight:
   136             w = (size.width > size.height) ? size.width : size.height;
   137             h = (size.width < size.height) ? size.width : size.height;
   138             break;
   139 
   140         default:
   141             SDL_assert(0 && "Unexpected interface orientation!");
   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