src/video/uikit/SDL_uikitviewcontroller.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 9488 f15520430c94
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

Is it that time already??
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 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_internal.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_uikitviewcontroller.h"
    32 #include "SDL_uikitvideo.h"
    33 #include "SDL_uikitmodes.h"
    34 #include "SDL_uikitwindow.h"
    35 
    36 
    37 @implementation SDL_uikitviewcontroller
    38 
    39 @synthesize window;
    40 
    41 - (id)initWithSDLWindow:(SDL_Window *)_window
    42 {
    43     self = [self init];
    44     if (self == nil) {
    45         return nil;
    46     }
    47     self.window = _window;
    48 
    49     return self;
    50 }
    51 
    52 - (void)loadView
    53 {
    54     /* do nothing. */
    55 }
    56 
    57 - (void)viewDidLayoutSubviews
    58 {
    59     if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    60         SDL_WindowData *data = self->window->driverdata;
    61         SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
    62         SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
    63         const CGSize size = data->view.bounds.size;
    64         int w, h;
    65 
    66         w = (int)(size.width * displaymodedata->scale);
    67         h = (int)(size.height * displaymodedata->scale);
    68 
    69         SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
    70     }
    71 }
    72 
    73 - (NSUInteger)supportedInterfaceOrientations
    74 {
    75     NSUInteger orientationMask = 0;
    76 
    77     const char *orientationsCString;
    78     if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
    79         BOOL rotate = NO;
    80         NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    81                                                             encoding:NSUTF8StringEncoding];
    82         NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    83                                  [NSCharacterSet characterSetWithCharactersInString:@" "]];
    84 
    85         if ([orientations containsObject:@"LandscapeLeft"]) {
    86             orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
    87         }
    88         if ([orientations containsObject:@"LandscapeRight"]) {
    89             orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
    90         }
    91         if ([orientations containsObject:@"Portrait"]) {
    92             orientationMask |= UIInterfaceOrientationMaskPortrait;
    93         }
    94         if ([orientations containsObject:@"PortraitUpsideDown"]) {
    95             orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
    96         }
    97 
    98     } else if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    99         orientationMask = UIInterfaceOrientationMaskAll;  /* any orientation is okay. */
   100     } else {
   101         if (self->window->w >= self->window->h) {
   102             orientationMask |= UIInterfaceOrientationMaskLandscape;
   103         }
   104         if (self->window->h >= self->window->w) {
   105             orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
   106         }
   107     }
   108 
   109     /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
   110     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
   111         orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
   112     }
   113     return orientationMask;
   114 }
   115 
   116 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
   117 {
   118     NSUInteger orientationMask = [self supportedInterfaceOrientations];
   119     return (orientationMask & (1 << orient));
   120 }
   121 
   122 - (BOOL)prefersStatusBarHidden
   123 {
   124     if (self->window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
   125         return YES;
   126     } else {
   127         return NO;
   128     }
   129 }
   130 
   131 @end
   132 
   133 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   134 
   135 /* vi: set ts=4 sw=4 expandtab: */