src/video/uikit/SDL_uikitviewcontroller.m
author Kees Bakker <kees@ijzerbout.nl>
Wed, 28 Sep 2011 21:42:02 +0200
changeset 6011 83a21d3bd5c1
parent 6003 fddf81967e2d
child 6044 35448a5ea044
permissions -rw-r--r--
Make SDL_uikitviewcontroller.window a real Objective-C property and synthesize
kees@5640
     1
/*
kees@5640
     2
 Simple DirectMedia Layer
kees@5640
     3
 Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
kees@6001
     4
kees@5640
     5
 This software is provided 'as-is', without any express or implied
kees@5640
     6
 warranty.  In no event will the authors be held liable for any damages
kees@5640
     7
 arising from the use of this software.
kees@6001
     8
kees@5640
     9
 Permission is granted to anyone to use this software for any purpose,
kees@5640
    10
 including commercial applications, and to alter it and redistribute it
kees@5640
    11
 freely, subject to the following restrictions:
kees@6001
    12
kees@5640
    13
 1. The origin of this software must not be misrepresented; you must not
kees@5640
    14
 claim that you wrote the original software. If you use this software
kees@5640
    15
 in a product, an acknowledgment in the product documentation would be
kees@5640
    16
 appreciated but is not required.
kees@5640
    17
 2. Altered source versions must be plainly marked as such, and must not be
kees@5640
    18
 misrepresented as being the original software.
kees@5640
    19
 3. This notice may not be removed or altered from any source distribution.
kees@5640
    20
 */
kees@5640
    21
#include "SDL_config.h"
kees@5640
    22
kees@5640
    23
#include "SDL_video.h"
kees@5640
    24
#include "SDL_assert.h"
kees@5640
    25
#include "SDL_hints.h"
kees@5640
    26
#include "../SDL_sysvideo.h"
kees@5640
    27
#include "../../events/SDL_events_c.h"
kees@5640
    28
kees@5640
    29
#include "SDL_uikitwindow.h"
kees@5640
    30
kees@5640
    31
#import "SDL_uikitviewcontroller.h"
kees@5640
    32
kees@5640
    33
kees@5640
    34
@implementation SDL_uikitviewcontroller
kees@5640
    35
kees@6011
    36
@synthesize window;
kees@6011
    37
kees@6003
    38
- (id)initWithSDLWindow:(SDL_Window *)_window
kees@6003
    39
{
kees@6011
    40
    self = [self init];
kees@6011
    41
    if (self == nil) {
kees@5640
    42
        return nil;
kees@5640
    43
    }
kees@6011
    44
    self.window = _window;
kees@5640
    45
    return self;
kees@5640
    46
}
kees@5640
    47
kees@6003
    48
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
    49
{
kees@5640
    50
    const char *orientationsCString;
kees@5640
    51
    if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
kees@5640
    52
        BOOL rotate = NO;
kees@5640
    53
        NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
kees@5640
    54
                                                            encoding:NSUTF8StringEncoding];
kees@5640
    55
        NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
kees@5640
    56
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
kees@6001
    57
kees@5640
    58
        switch (orient) {
kees@5640
    59
            case UIInterfaceOrientationLandscapeLeft:
kees@5640
    60
                rotate = [orientations containsObject:@"LandscapeLeft"];
kees@5640
    61
                break;
kees@6001
    62
kees@5640
    63
            case UIInterfaceOrientationLandscapeRight:
kees@5640
    64
                rotate = [orientations containsObject:@"LandscapeRight"];
kees@5640
    65
                break;
kees@6001
    66
kees@5640
    67
            case UIInterfaceOrientationPortrait:
kees@5640
    68
                rotate = [orientations containsObject:@"Portrait"];
kees@5640
    69
                break;
kees@6001
    70
kees@5640
    71
            case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
    72
                rotate = [orientations containsObject:@"PortraitUpsideDown"];
kees@5640
    73
                break;
kees@6001
    74
kees@5640
    75
            default: break;
kees@5640
    76
        }
kees@6001
    77
kees@5640
    78
        return rotate;
kees@5640
    79
    }
kees@6001
    80
kees@5640
    81
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
kees@5640
    82
        return YES;  // any orientation is okay.
kees@5640
    83
    }
kees@6001
    84
kees@5640
    85
    // If not resizable, allow device to orient to other matching sizes
kees@5640
    86
    //  (that is, let the user turn the device upside down...same screen
kees@5640
    87
    //   dimensions, but it lets the user place the device where it's most
kees@5640
    88
    //   comfortable in relation to its physical buttons, headphone jack, etc).
kees@5640
    89
    switch (orient) {
kees@5640
    90
        case UIInterfaceOrientationLandscapeLeft:
kees@5640
    91
        case UIInterfaceOrientationLandscapeRight:
kees@5640
    92
            return (self->window->w >= self->window->h);
kees@6001
    93
kees@5640
    94
        case UIInterfaceOrientationPortrait:
kees@5640
    95
        case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
    96
            return (self->window->h >= self->window->w);
kees@6001
    97
kees@5640
    98
        default: break;
kees@5640
    99
    }
kees@6001
   100
kees@5640
   101
    return NO;  // Nothing else is acceptable.
kees@5640
   102
}
kees@5640
   103
kees@6003
   104
- (void)loadView
kees@6003
   105
{
kees@5640
   106
    // do nothing.
kees@5640
   107
}
kees@5640
   108
kees@5640
   109
// Send a resized event when the orientation changes.
kees@6003
   110
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
kees@6003
   111
{
kees@5640
   112
    if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
kees@5640
   113
        return;   // don't care, we're just flipping over in this case.
kees@5640
   114
    }
kees@6001
   115
kees@5640
   116
    const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
kees@5640
   117
    SDL_WindowData *data = self->window->driverdata;
kees@5640
   118
    UIWindow *uiwindow = data->uiwindow;
kees@5640
   119
    UIScreen *uiscreen = [uiwindow screen];
kees@5640
   120
    const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
kees@5640
   121
    CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
kees@5640
   122
    const CGSize size = frame.size;
kees@5640
   123
    int w, h;
kees@6001
   124
kees@5640
   125
    switch (toInterfaceOrientation) {
kees@5640
   126
        case UIInterfaceOrientationPortrait:
kees@5640
   127
        case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
   128
            w = (size.width < size.height) ? size.width : size.height;
kees@5640
   129
            h = (size.width > size.height) ? size.width : size.height;
kees@5640
   130
            break;
kees@6001
   131
kees@5640
   132
        case UIInterfaceOrientationLandscapeLeft:
kees@5640
   133
        case UIInterfaceOrientationLandscapeRight:
kees@5640
   134
            w = (size.width > size.height) ? size.width : size.height;
kees@5640
   135
            h = (size.width < size.height) ? size.width : size.height;
kees@5640
   136
            break;
kees@6001
   137
kees@5640
   138
        default:
kees@5640
   139
            SDL_assert(0 && "Unexpected interface orientation!");
kees@5640
   140
            return;
kees@5640
   141
    }
kees@6001
   142
kees@5640
   143
    frame.size.width = w;
kees@5640
   144
    frame.size.height = h;
kees@5640
   145
    frame.origin.x = 0;
kees@5640
   146
    frame.origin.y = 0;
kees@6001
   147
kees@5640
   148
    [uiwindow setFrame:frame];
kees@5640
   149
    [data->view updateFrame];
kees@5640
   150
    SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
kees@5640
   151
}
kees@5640
   152
kees@5640
   153
@end