src/video/uikit/SDL_uikitviewcontroller.m
author Kees Bakker <kees@ijzerbout.nl>
Wed, 28 Sep 2011 21:42:02 +0200
changeset 6011 83a21d3bd5c1
parent 6003 fddf81967e2d
child 6044 35448a5ea044
permissions -rw-r--r--
Make SDL_uikitviewcontroller.window a real Objective-C property and synthesize
     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 #include "SDL_video.h"
    24 #include "SDL_assert.h"
    25 #include "SDL_hints.h"
    26 #include "../SDL_sysvideo.h"
    27 #include "../../events/SDL_events_c.h"
    28 
    29 #include "SDL_uikitwindow.h"
    30 
    31 #import "SDL_uikitviewcontroller.h"
    32 
    33 
    34 @implementation SDL_uikitviewcontroller
    35 
    36 @synthesize window;
    37 
    38 - (id)initWithSDLWindow:(SDL_Window *)_window
    39 {
    40     self = [self init];
    41     if (self == nil) {
    42         return nil;
    43     }
    44     self.window = _window;
    45     return self;
    46 }
    47 
    48 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
    49 {
    50     const char *orientationsCString;
    51     if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
    52         BOOL rotate = NO;
    53         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    54                                                             encoding:NSUTF8StringEncoding];
    55         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    56                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    57 
    58         switch (orient) {
    59             case UIInterfaceOrientationLandscapeLeft:
    60                 rotate = [orientations containsObject:@"LandscapeLeft"];
    61                 break;
    62 
    63             case UIInterfaceOrientationLandscapeRight:
    64                 rotate = [orientations containsObject:@"LandscapeRight"];
    65                 break;
    66 
    67             case UIInterfaceOrientationPortrait:
    68                 rotate = [orientations containsObject:@"Portrait"];
    69                 break;
    70 
    71             case UIInterfaceOrientationPortraitUpsideDown:
    72                 rotate = [orientations containsObject:@"PortraitUpsideDown"];
    73                 break;
    74 
    75             default: break;
    76         }
    77 
    78         return rotate;
    79     }
    80 
    81     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    82         return YES;  // any orientation is okay.
    83     }
    84 
    85     // If not resizable, allow device to orient to other matching sizes
    86     //  (that is, let the user turn the device upside down...same screen
    87     //   dimensions, but it lets the user place the device where it's most
    88     //   comfortable in relation to its physical buttons, headphone jack, etc).
    89     switch (orient) {
    90         case UIInterfaceOrientationLandscapeLeft:
    91         case UIInterfaceOrientationLandscapeRight:
    92             return (self->window->w >= self->window->h);
    93 
    94         case UIInterfaceOrientationPortrait:
    95         case UIInterfaceOrientationPortraitUpsideDown:
    96             return (self->window->h >= self->window->w);
    97 
    98         default: break;
    99     }
   100 
   101     return NO;  // Nothing else is acceptable.
   102 }
   103 
   104 - (void)loadView
   105 {
   106     // do nothing.
   107 }
   108 
   109 // Send a resized event when the orientation changes.
   110 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
   111 {
   112     if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
   113         return;   // don't care, we're just flipping over in this case.
   114     }
   115 
   116     const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   117     SDL_WindowData *data = self->window->driverdata;
   118     UIWindow *uiwindow = data->uiwindow;
   119     UIScreen *uiscreen = [uiwindow screen];
   120     const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
   121     CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   122     const CGSize size = frame.size;
   123     int w, h;
   124 
   125     switch (toInterfaceOrientation) {
   126         case UIInterfaceOrientationPortrait:
   127         case UIInterfaceOrientationPortraitUpsideDown:
   128             w = (size.width < size.height) ? size.width : size.height;
   129             h = (size.width > size.height) ? size.width : size.height;
   130             break;
   131 
   132         case UIInterfaceOrientationLandscapeLeft:
   133         case UIInterfaceOrientationLandscapeRight:
   134             w = (size.width > size.height) ? size.width : size.height;
   135             h = (size.width < size.height) ? size.width : size.height;
   136             break;
   137 
   138         default:
   139             SDL_assert(0 && "Unexpected interface orientation!");
   140             return;
   141     }
   142 
   143     frame.size.width = w;
   144     frame.size.height = h;
   145     frame.origin.x = 0;
   146     frame.origin.y = 0;
   147 
   148     [uiwindow setFrame:frame];
   149     [data->view updateFrame];
   150     SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
   151 }
   152 
   153 @end