src/video/uikit/SDL_uikitviewcontroller.m
author Alex Szpakowski
Tue, 29 Jul 2014 00:05:48 -0300
branchiOS-improvements
changeset 9505 6fc615dfc93f
parent 9500 cbf5c5ecf5ac
child 9509 350f38eb3d0f
permissions -rw-r--r--
Added support for SDL_SetWindowBordered on iOS. Worked around a bug with rotating the device on iOS 8.
kees@5640
     1
/*
slouken@6044
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.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
{
slime73@9505
    43
    if (self = [super initWithNibName:nil bundle:nil]) {
slime73@9505
    44
        self.window = _window;
kees@5640
    45
    }
kees@5640
    46
    return self;
kees@5640
    47
}
kees@5640
    48
slouken@6440
    49
- (void)loadView
slouken@6440
    50
{
slouken@7191
    51
    /* do nothing. */
slouken@6440
    52
}
slouken@6440
    53
slouken@6451
    54
- (void)viewDidLayoutSubviews
slouken@6440
    55
{
slime73@9505
    56
    const CGSize size = self.view.bounds.size;
slime73@9494
    57
    int w = (int) size.width;
slime73@9494
    58
    int h = (int) size.height;
slouken@7191
    59
slime73@9494
    60
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@6440
    61
}
slouken@6440
    62
slouken@6451
    63
- (NSUInteger)supportedInterfaceOrientations
kees@6003
    64
{
slouken@6451
    65
    NSUInteger orientationMask = 0;
slime73@9493
    66
    const char *orientationsHint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
slouken@7191
    67
slime73@9493
    68
    if (orientationsHint != NULL) {
slime73@9499
    69
        NSArray *orientations = [@(orientationsHint) componentsSeparatedByCharactersInSet:
kees@5640
    70
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
slouken@7191
    71
slouken@6451
    72
        if ([orientations containsObject:@"LandscapeLeft"]) {
slouken@6451
    73
            orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
slouken@6451
    74
        }
slouken@6451
    75
        if ([orientations containsObject:@"LandscapeRight"]) {
slouken@6451
    76
            orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
slouken@6451
    77
        }
slouken@6451
    78
        if ([orientations containsObject:@"Portrait"]) {
slouken@6451
    79
            orientationMask |= UIInterfaceOrientationMaskPortrait;
kees@5640
    80
        }
slouken@6451
    81
        if ([orientations containsObject:@"PortraitUpsideDown"]) {
slouken@6451
    82
            orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
slouken@6451
    83
        }
slime73@9493
    84
    }
slouken@7191
    85
slime73@9494
    86
    if (orientationMask == 0 && (window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@7191
    87
        orientationMask = UIInterfaceOrientationMaskAll;  /* any orientation is okay. */
slime73@9493
    88
    }
slime73@9493
    89
slime73@9493
    90
    if (orientationMask == 0) {
slime73@9493
    91
        if (window->w >= window->h) {
slouken@6451
    92
            orientationMask |= UIInterfaceOrientationMaskLandscape;
slouken@6451
    93
        }
slime73@9493
    94
        if (window->h >= window->w) {
slouken@6451
    95
            orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
slouken@6451
    96
        }
kees@5640
    97
    }
slouken@7191
    98
slouken@7191
    99
    /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
slouken@6451
   100
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
slouken@6451
   101
        orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
kees@5640
   102
    }
slouken@6451
   103
    return orientationMask;
kees@5640
   104
}
kees@5640
   105
slouken@6451
   106
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
   107
{
slouken@6451
   108
    NSUInteger orientationMask = [self supportedInterfaceOrientations];
slouken@6451
   109
    return (orientationMask & (1 << orient));
kees@5640
   110
}
kees@5640
   111
slouken@7862
   112
- (BOOL)prefersStatusBarHidden
slouken@7862
   113
{
slime73@9493
   114
    if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
slouken@7862
   115
        return YES;
slouken@7862
   116
    } else {
slouken@7862
   117
        return NO;
slouken@7862
   118
    }
slouken@7862
   119
}
slouken@7862
   120
slime73@9492
   121
- (UIStatusBarStyle)preferredStatusBarStyle
slime73@9492
   122
{
slime73@9495
   123
    /* We assume most games don't have a bright white background. */
slime73@9492
   124
    return UIStatusBarStyleLightContent;
slime73@9492
   125
}
slime73@9492
   126
slouken@6451
   127
@end
slouken@6451
   128
slouken@6044
   129
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   130
slouken@6440
   131
/* vi: set ts=4 sw=4 expandtab: */