src/video/uikit/SDL_uikitviewcontroller.m
author Kees Bakker <kees@tweeler.com>
Wed, 17 Aug 2011 23:26:58 +0200
changeset 5640 04b5b11985c8
child 6001 3ae3d1e58d59
permissions -rw-r--r--
Split off SDL_uikitviewcontroller in its own module
     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 - (id)initWithSDLWindow:(SDL_Window *)_window {
    37     if ((self = [self init]) == nil) {
    38         return nil;
    39     }
    40     self->window = _window;
    41     return self;
    42 }
    43 
    44 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
    45     const char *orientationsCString;
    46     if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
    47         BOOL rotate = NO;
    48         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    49                                                             encoding:NSUTF8StringEncoding];
    50         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    51                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    52         
    53         switch (orient) {
    54             case UIInterfaceOrientationLandscapeLeft:
    55                 rotate = [orientations containsObject:@"LandscapeLeft"];
    56                 break;
    57                 
    58             case UIInterfaceOrientationLandscapeRight:
    59                 rotate = [orientations containsObject:@"LandscapeRight"];
    60                 break;
    61                 
    62             case UIInterfaceOrientationPortrait:
    63                 rotate = [orientations containsObject:@"Portrait"];
    64                 break;
    65                 
    66             case UIInterfaceOrientationPortraitUpsideDown:
    67                 rotate = [orientations containsObject:@"PortraitUpsideDown"];
    68                 break;
    69                 
    70             default: break;
    71         }
    72         
    73         return rotate;
    74     }
    75     
    76     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    77         return YES;  // any orientation is okay.
    78     }
    79     
    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
    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).
    84     switch (orient) {
    85         case UIInterfaceOrientationLandscapeLeft:
    86         case UIInterfaceOrientationLandscapeRight:
    87             return (self->window->w >= self->window->h);
    88             
    89         case UIInterfaceOrientationPortrait:
    90         case UIInterfaceOrientationPortraitUpsideDown:
    91             return (self->window->h >= self->window->w);
    92             
    93         default: break;
    94     }
    95     
    96     return NO;  // Nothing else is acceptable.
    97 }
    98 
    99 - (void)loadView  {
   100     // do nothing.
   101 }
   102 
   103 // Send a resized event when the orientation changes.
   104 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
   105     if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
   106         return;   // don't care, we're just flipping over in this case.
   107     }
   108     
   109     const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   110     SDL_WindowData *data = self->window->driverdata;
   111     UIWindow *uiwindow = data->uiwindow;
   112     UIScreen *uiscreen = [uiwindow screen];
   113     const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
   114     CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   115     const CGSize size = frame.size;
   116     int w, h;
   117     
   118     switch (toInterfaceOrientation) {
   119         case UIInterfaceOrientationPortrait:
   120         case UIInterfaceOrientationPortraitUpsideDown:
   121             w = (size.width < size.height) ? size.width : size.height;
   122             h = (size.width > size.height) ? size.width : size.height;
   123             break;
   124             
   125         case UIInterfaceOrientationLandscapeLeft:
   126         case UIInterfaceOrientationLandscapeRight:
   127             w = (size.width > size.height) ? size.width : size.height;
   128             h = (size.width < size.height) ? size.width : size.height;
   129             break;
   130             
   131         default:
   132             SDL_assert(0 && "Unexpected interface orientation!");
   133             return;
   134     }
   135     
   136     frame.size.width = w;
   137     frame.size.height = h;
   138     frame.origin.x = 0;
   139     frame.origin.y = 0;
   140     
   141     [uiwindow setFrame:frame];
   142     [data->view updateFrame];
   143     SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
   144 }
   145 
   146 @end