src/video/uikit/SDL_uikitviewcontroller.m
author Kees Bakker <kees@ijzerbout.nl>
Tue, 27 Sep 2011 22:51:26 +0200
changeset 6003 fddf81967e2d
parent 6001 3ae3d1e58d59
child 6011 83a21d3bd5c1
permissions -rw-r--r--
Use a consistent source code format in src/video/uikit

For example:
* Opening bracket of a method/function on a new line at column 0
* space after "if", "while", etc
     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 {
    38     if ((self = [self init]) == nil) {
    39         return nil;
    40     }
    41     self->window = _window;
    42     return self;
    43 }
    44 
    45 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
    46 {
    47     const char *orientationsCString;
    48     if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
    49         BOOL rotate = NO;
    50         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    51                                                             encoding:NSUTF8StringEncoding];
    52         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    53                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    54 
    55         switch (orient) {
    56             case UIInterfaceOrientationLandscapeLeft:
    57                 rotate = [orientations containsObject:@"LandscapeLeft"];
    58                 break;
    59 
    60             case UIInterfaceOrientationLandscapeRight:
    61                 rotate = [orientations containsObject:@"LandscapeRight"];
    62                 break;
    63 
    64             case UIInterfaceOrientationPortrait:
    65                 rotate = [orientations containsObject:@"Portrait"];
    66                 break;
    67 
    68             case UIInterfaceOrientationPortraitUpsideDown:
    69                 rotate = [orientations containsObject:@"PortraitUpsideDown"];
    70                 break;
    71 
    72             default: break;
    73         }
    74 
    75         return rotate;
    76     }
    77 
    78     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    79         return YES;  // any orientation is okay.
    80     }
    81 
    82     // If not resizable, allow device to orient to other matching sizes
    83     //  (that is, let the user turn the device upside down...same screen
    84     //   dimensions, but it lets the user place the device where it's most
    85     //   comfortable in relation to its physical buttons, headphone jack, etc).
    86     switch (orient) {
    87         case UIInterfaceOrientationLandscapeLeft:
    88         case UIInterfaceOrientationLandscapeRight:
    89             return (self->window->w >= self->window->h);
    90 
    91         case UIInterfaceOrientationPortrait:
    92         case UIInterfaceOrientationPortraitUpsideDown:
    93             return (self->window->h >= self->window->w);
    94 
    95         default: break;
    96     }
    97 
    98     return NO;  // Nothing else is acceptable.
    99 }
   100 
   101 - (void)loadView
   102 {
   103     // do nothing.
   104 }
   105 
   106 // Send a resized event when the orientation changes.
   107 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
   108 {
   109     if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
   110         return;   // don't care, we're just flipping over in this case.
   111     }
   112 
   113     const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   114     SDL_WindowData *data = self->window->driverdata;
   115     UIWindow *uiwindow = data->uiwindow;
   116     UIScreen *uiscreen = [uiwindow screen];
   117     const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
   118     CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   119     const CGSize size = frame.size;
   120     int w, h;
   121 
   122     switch (toInterfaceOrientation) {
   123         case UIInterfaceOrientationPortrait:
   124         case UIInterfaceOrientationPortraitUpsideDown:
   125             w = (size.width < size.height) ? size.width : size.height;
   126             h = (size.width > size.height) ? size.width : size.height;
   127             break;
   128 
   129         case UIInterfaceOrientationLandscapeLeft:
   130         case UIInterfaceOrientationLandscapeRight:
   131             w = (size.width > size.height) ? size.width : size.height;
   132             h = (size.width < size.height) ? size.width : size.height;
   133             break;
   134 
   135         default:
   136             SDL_assert(0 && "Unexpected interface orientation!");
   137             return;
   138     }
   139 
   140     frame.size.width = w;
   141     frame.size.height = h;
   142     frame.origin.x = 0;
   143     frame.origin.y = 0;
   144 
   145     [uiwindow setFrame:frame];
   146     [data->view updateFrame];
   147     SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
   148 }
   149 
   150 @end