src/video/uikit/SDL_uikitviewcontroller.m
author Sam Lantinga
Sat, 29 Sep 2012 17:23:40 -0700
changeset 6518 42948831d16e
parent 6453 f71e3fdba072
child 6556 d5e854f0d9a9
permissions -rw-r--r--
Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
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
icculus@6093
    31
#include "SDL_uikitviewcontroller.h"
icculus@6093
    32
#include "SDL_uikitvideo.h"
slouken@6518
    33
#include "SDL_uikitmodes.h"
slouken@6518
    34
#include "SDL_uikitwindow.h"
slouken@6518
    35
kees@5640
    36
slouken@6452
    37
#ifndef __IPHONE_6_0
slouken@6452
    38
// This enum isn't available in older SDKs, but we use it for our own purposes on iOS 5.1 and for the system on iOS 6.0
slouken@6453
    39
enum UIInterfaceOrientationMask
slouken@6453
    40
{
slouken@6452
    41
    UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
slouken@6452
    42
    UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
slouken@6452
    43
    UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
slouken@6452
    44
    UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
slouken@6452
    45
    UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
slouken@6452
    46
    UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
slouken@6452
    47
    UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
slouken@6452
    48
};
slouken@6452
    49
#endif // !__IPHONE_6_0
slouken@6452
    50
slouken@6452
    51
    
kees@5640
    52
@implementation SDL_uikitviewcontroller
kees@5640
    53
kees@6011
    54
@synthesize window;
kees@6011
    55
kees@6003
    56
- (id)initWithSDLWindow:(SDL_Window *)_window
kees@6003
    57
{
kees@6011
    58
    self = [self init];
kees@6011
    59
    if (self == nil) {
kees@5640
    60
        return nil;
kees@5640
    61
    }
kees@6011
    62
    self.window = _window;
slouken@6440
    63
kees@5640
    64
    return self;
kees@5640
    65
}
kees@5640
    66
slouken@6440
    67
- (void)loadView
slouken@6440
    68
{
slouken@6440
    69
    // do nothing.
slouken@6440
    70
}
slouken@6440
    71
slouken@6451
    72
- (void)viewDidLayoutSubviews
slouken@6440
    73
{
slouken@6440
    74
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
slouken@6440
    75
        SDL_WindowData *data = self->window->driverdata;
slouken@6440
    76
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
slouken@6440
    77
        SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
slouken@6440
    78
        const CGSize size = data->view.bounds.size;
slouken@6440
    79
        int w, h;
slouken@6451
    80
        
slouken@6440
    81
        w = (int)(size.width * displaymodedata->scale);
slouken@6440
    82
        h = (int)(size.height * displaymodedata->scale);
slouken@6451
    83
        
slouken@6440
    84
        SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@6440
    85
    }
slouken@6440
    86
}
slouken@6440
    87
slouken@6451
    88
- (NSUInteger)supportedInterfaceOrientations
kees@6003
    89
{
slouken@6451
    90
    NSUInteger orientationMask = 0;
slouken@6451
    91
    
kees@5640
    92
    const char *orientationsCString;
kees@5640
    93
    if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
kees@5640
    94
        BOOL rotate = NO;
kees@5640
    95
        NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
kees@5640
    96
                                                            encoding:NSUTF8StringEncoding];
kees@5640
    97
        NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
kees@5640
    98
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
slouken@6451
    99
        
slouken@6451
   100
        if ([orientations containsObject:@"LandscapeLeft"]) {
slouken@6451
   101
            orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
slouken@6451
   102
        }
slouken@6451
   103
        if ([orientations containsObject:@"LandscapeRight"]) {
slouken@6451
   104
            orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
slouken@6451
   105
        }
slouken@6451
   106
        if ([orientations containsObject:@"Portrait"]) {
slouken@6451
   107
            orientationMask |= UIInterfaceOrientationMaskPortrait;
kees@5640
   108
        }
slouken@6451
   109
        if ([orientations containsObject:@"PortraitUpsideDown"]) {
slouken@6451
   110
            orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
slouken@6451
   111
        }
slouken@6451
   112
        
slouken@6451
   113
    } else if (self->window->flags & SDL_WINDOW_RESIZABLE) {
slouken@6451
   114
        orientationMask = UIInterfaceOrientationMaskAll;  // any orientation is okay.
slouken@6451
   115
    } else {
slouken@6451
   116
        if (self->window->w >= self->window->h) {
slouken@6451
   117
            orientationMask |= UIInterfaceOrientationMaskLandscape;
slouken@6451
   118
        }
slouken@6451
   119
        if (self->window->h >= self->window->w) {
slouken@6451
   120
            orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
slouken@6451
   121
        }
kees@5640
   122
    }
slouken@6451
   123
    
slouken@6451
   124
    // Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation
slouken@6451
   125
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
slouken@6451
   126
        orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
kees@5640
   127
    }
slouken@6451
   128
    return orientationMask;
kees@5640
   129
}
kees@5640
   130
slouken@6451
   131
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
   132
{
slouken@6451
   133
    NSUInteger orientationMask = [self supportedInterfaceOrientations];
slouken@6451
   134
    return (orientationMask & (1 << orient));
kees@5640
   135
}
kees@5640
   136
slouken@6451
   137
@end
slouken@6451
   138
slouken@6044
   139
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   140
slouken@6440
   141
/* vi: set ts=4 sw=4 expandtab: */