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