src/video/uikit/SDL_uikitviewcontroller.m
author Sam Lantinga
Sun, 23 Sep 2012 03:46:49 -0700
changeset 6451 52e5a05ca795
parent 6440 f0ca76958984
child 6452 1895319021e4
permissions -rwxr-xr-x
Fixed autorotate / resize support on iOS 6.0, tested with iPad and iPhone simulators with iOS 5.1 and 6.0
kees@5640
     1
/*
slouken@6044
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
kees@6001
     4
slouken@6044
     5
  This software is provided 'as-is', without any express or implied
slouken@6044
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6044
     7
  arising from the use of this software.
slouken@6044
     8
slouken@6044
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6044
    10
  including commercial applications, and to alter it and redistribute it
slouken@6044
    11
  freely, subject to the following restrictions:
kees@6001
    12
slouken@6044
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6044
    14
     claim that you wrote the original software. If you use this software
slouken@6044
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6044
    16
     appreciated but is not required.
slouken@6044
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6044
    18
     misrepresented as being the original software.
slouken@6044
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6044
    20
*/
slouken@6044
    21
#include "SDL_config.h"
kees@6001
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
kees@5640
    24
kees@5640
    25
#include "SDL_video.h"
kees@5640
    26
#include "SDL_assert.h"
kees@5640
    27
#include "SDL_hints.h"
kees@5640
    28
#include "../SDL_sysvideo.h"
kees@5640
    29
#include "../../events/SDL_events_c.h"
kees@5640
    30
kees@5640
    31
#include "SDL_uikitwindow.h"
icculus@6093
    32
#include "SDL_uikitviewcontroller.h"
icculus@6093
    33
#include "SDL_uikitvideo.h"
kees@5640
    34
kees@5640
    35
@implementation SDL_uikitviewcontroller
kees@5640
    36
kees@6011
    37
@synthesize window;
kees@6011
    38
kees@6003
    39
- (id)initWithSDLWindow:(SDL_Window *)_window
kees@6003
    40
{
kees@6011
    41
    self = [self init];
kees@6011
    42
    if (self == nil) {
kees@5640
    43
        return nil;
kees@5640
    44
    }
kees@6011
    45
    self.window = _window;
slouken@6440
    46
kees@5640
    47
    return self;
kees@5640
    48
}
kees@5640
    49
slouken@6440
    50
- (void)loadView
slouken@6440
    51
{
slouken@6440
    52
    // do nothing.
slouken@6440
    53
}
slouken@6440
    54
slouken@6451
    55
- (void)viewDidLayoutSubviews
slouken@6440
    56
{
slouken@6440
    57
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
slouken@6440
    58
        SDL_WindowData *data = self->window->driverdata;
slouken@6440
    59
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
slouken@6440
    60
        SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
slouken@6440
    61
        const CGSize size = data->view.bounds.size;
slouken@6440
    62
        int w, h;
slouken@6451
    63
        
slouken@6440
    64
        w = (int)(size.width * displaymodedata->scale);
slouken@6440
    65
        h = (int)(size.height * displaymodedata->scale);
slouken@6451
    66
        
slouken@6440
    67
        SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@6440
    68
    }
slouken@6440
    69
}
slouken@6440
    70
slouken@6451
    71
- (NSUInteger)supportedInterfaceOrientations
kees@6003
    72
{
slouken@6451
    73
    NSUInteger orientationMask = 0;
slouken@6451
    74
    
kees@5640
    75
    const char *orientationsCString;
kees@5640
    76
    if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
kees@5640
    77
        BOOL rotate = NO;
kees@5640
    78
        NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
kees@5640
    79
                                                            encoding:NSUTF8StringEncoding];
kees@5640
    80
        NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
kees@5640
    81
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
slouken@6451
    82
        
slouken@6451
    83
        if ([orientations containsObject:@"LandscapeLeft"]) {
slouken@6451
    84
            orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
slouken@6451
    85
        }
slouken@6451
    86
        if ([orientations containsObject:@"LandscapeRight"]) {
slouken@6451
    87
            orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
slouken@6451
    88
        }
slouken@6451
    89
        if ([orientations containsObject:@"Portrait"]) {
slouken@6451
    90
            orientationMask |= UIInterfaceOrientationMaskPortrait;
kees@5640
    91
        }
slouken@6451
    92
        if ([orientations containsObject:@"PortraitUpsideDown"]) {
slouken@6451
    93
            orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
slouken@6451
    94
        }
slouken@6451
    95
        
slouken@6451
    96
    } else if (self->window->flags & SDL_WINDOW_RESIZABLE) {
slouken@6451
    97
        orientationMask = UIInterfaceOrientationMaskAll;  // any orientation is okay.
slouken@6451
    98
    } else {
slouken@6451
    99
        if (self->window->w >= self->window->h) {
slouken@6451
   100
            orientationMask |= UIInterfaceOrientationMaskLandscape;
slouken@6451
   101
        }
slouken@6451
   102
        if (self->window->h >= self->window->w) {
slouken@6451
   103
            orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
slouken@6451
   104
        }
kees@5640
   105
    }
slouken@6451
   106
    
slouken@6451
   107
    // Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation
slouken@6451
   108
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
slouken@6451
   109
        orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
kees@5640
   110
    }
slouken@6451
   111
    return orientationMask;
kees@5640
   112
}
kees@5640
   113
slouken@6451
   114
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
   115
{
slouken@6451
   116
    NSUInteger orientationMask = [self supportedInterfaceOrientations];
slouken@6451
   117
    return (orientationMask & (1 << orient));
kees@5640
   118
}
kees@5640
   119
slouken@6451
   120
@end
slouken@6451
   121
slouken@6044
   122
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   123
slouken@6440
   124
/* vi: set ts=4 sw=4 expandtab: */