src/video/uikit/SDL_uikitviewcontroller.m
author Alex Szpakowski
Thu, 17 Jul 2014 18:05:12 -0300
branchiOS-improvements
changeset 9494 435b95e7b712
parent 9493 961d593034b5
child 9495 76e878c8ca80
permissions -rw-r--r--
Properly send a window resize event in all cases when viewDidLayoutSubviews is triggered.
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
{
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
{
slime73@9494
    59
    SDL_WindowData *data = window->driverdata;
slime73@9494
    60
    const CGSize size = data->view.bounds.size;
slime73@9494
    61
    int w = (int) size.width;
slime73@9494
    62
    int h = (int) size.height;
slouken@7191
    63
slime73@9494
    64
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@6440
    65
}
slouken@6440
    66
slouken@6451
    67
- (NSUInteger)supportedInterfaceOrientations
kees@6003
    68
{
slouken@6451
    69
    NSUInteger orientationMask = 0;
slime73@9493
    70
    const char *orientationsHint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
slouken@7191
    71
slime73@9493
    72
    if (orientationsHint != NULL) {
slime73@9493
    73
        NSString *orientationsString = [NSString stringWithCString:orientationsHint
slime73@9493
    74
                                                          encoding:NSUTF8StringEncoding];
slime73@9493
    75
        NSArray *orientations = [orientationsString componentsSeparatedByCharactersInSet:
kees@5640
    76
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
slouken@7191
    77
slouken@6451
    78
        if ([orientations containsObject:@"LandscapeLeft"]) {
slouken@6451
    79
            orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
slouken@6451
    80
        }
slouken@6451
    81
        if ([orientations containsObject:@"LandscapeRight"]) {
slouken@6451
    82
            orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
slouken@6451
    83
        }
slouken@6451
    84
        if ([orientations containsObject:@"Portrait"]) {
slouken@6451
    85
            orientationMask |= UIInterfaceOrientationMaskPortrait;
kees@5640
    86
        }
slouken@6451
    87
        if ([orientations containsObject:@"PortraitUpsideDown"]) {
slouken@6451
    88
            orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
slouken@6451
    89
        }
slime73@9493
    90
    }
slouken@7191
    91
slime73@9494
    92
    if (orientationMask == 0 && (window->flags & SDL_WINDOW_RESIZABLE)) {
slouken@7191
    93
        orientationMask = UIInterfaceOrientationMaskAll;  /* any orientation is okay. */
slime73@9493
    94
    }
slime73@9493
    95
slime73@9493
    96
    if (orientationMask == 0) {
slime73@9493
    97
        if (window->w >= window->h) {
slouken@6451
    98
            orientationMask |= UIInterfaceOrientationMaskLandscape;
slouken@6451
    99
        }
slime73@9493
   100
        if (window->h >= window->w) {
slouken@6451
   101
            orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
slouken@6451
   102
        }
kees@5640
   103
    }
slouken@7191
   104
slouken@7191
   105
    /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
slouken@6451
   106
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
slouken@6451
   107
        orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
kees@5640
   108
    }
slouken@6451
   109
    return orientationMask;
kees@5640
   110
}
kees@5640
   111
slouken@6451
   112
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
   113
{
slouken@6451
   114
    NSUInteger orientationMask = [self supportedInterfaceOrientations];
slouken@6451
   115
    return (orientationMask & (1 << orient));
kees@5640
   116
}
kees@5640
   117
slouken@7862
   118
- (BOOL)prefersStatusBarHidden
slouken@7862
   119
{
slime73@9493
   120
    if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
slouken@7862
   121
        return YES;
slouken@7862
   122
    } else {
slouken@7862
   123
        return NO;
slouken@7862
   124
    }
slouken@7862
   125
}
slouken@7862
   126
slime73@9492
   127
- (UIStatusBarStyle)preferredStatusBarStyle
slime73@9492
   128
{
slime73@9492
   129
#ifdef __IPHONE_7_0
slime73@9492
   130
    return UIStatusBarStyleLightContent;
slime73@9492
   131
#else
slime73@9493
   132
    /* This method is only used in iOS 7+, so the return value here isn't important. */
slime73@9492
   133
    return UIStatusBarStyleBlackTranslucent;
slime73@9492
   134
#endif
slime73@9492
   135
}
slime73@9492
   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: */