src/video/uikit/SDL_uikitviewcontroller.m
author Kees Bakker <kees@ijzerbout.nl>
Tue, 27 Sep 2011 22:51:26 +0200
changeset 6003 fddf81967e2d
parent 6001 3ae3d1e58d59
child 6011 83a21d3bd5c1
permissions -rw-r--r--
Use a consistent source code format in src/video/uikit

For example:
* Opening bracket of a method/function on a new line at column 0
* space after "if", "while", etc
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@6003
    36
- (id)initWithSDLWindow:(SDL_Window *)_window
kees@6003
    37
{
kees@5640
    38
    if ((self = [self init]) == nil) {
kees@5640
    39
        return nil;
kees@5640
    40
    }
kees@5640
    41
    self->window = _window;
kees@5640
    42
    return self;
kees@5640
    43
}
kees@5640
    44
kees@6003
    45
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
    46
{
kees@5640
    47
    const char *orientationsCString;
kees@5640
    48
    if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
kees@5640
    49
        BOOL rotate = NO;
kees@5640
    50
        NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
kees@5640
    51
                                                            encoding:NSUTF8StringEncoding];
kees@5640
    52
        NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
kees@5640
    53
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
kees@6001
    54
kees@5640
    55
        switch (orient) {
kees@5640
    56
            case UIInterfaceOrientationLandscapeLeft:
kees@5640
    57
                rotate = [orientations containsObject:@"LandscapeLeft"];
kees@5640
    58
                break;
kees@6001
    59
kees@5640
    60
            case UIInterfaceOrientationLandscapeRight:
kees@5640
    61
                rotate = [orientations containsObject:@"LandscapeRight"];
kees@5640
    62
                break;
kees@6001
    63
kees@5640
    64
            case UIInterfaceOrientationPortrait:
kees@5640
    65
                rotate = [orientations containsObject:@"Portrait"];
kees@5640
    66
                break;
kees@6001
    67
kees@5640
    68
            case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
    69
                rotate = [orientations containsObject:@"PortraitUpsideDown"];
kees@5640
    70
                break;
kees@6001
    71
kees@5640
    72
            default: break;
kees@5640
    73
        }
kees@6001
    74
kees@5640
    75
        return rotate;
kees@5640
    76
    }
kees@6001
    77
kees@5640
    78
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
kees@5640
    79
        return YES;  // any orientation is okay.
kees@5640
    80
    }
kees@6001
    81
kees@5640
    82
    // If not resizable, allow device to orient to other matching sizes
kees@5640
    83
    //  (that is, let the user turn the device upside down...same screen
kees@5640
    84
    //   dimensions, but it lets the user place the device where it's most
kees@5640
    85
    //   comfortable in relation to its physical buttons, headphone jack, etc).
kees@5640
    86
    switch (orient) {
kees@5640
    87
        case UIInterfaceOrientationLandscapeLeft:
kees@5640
    88
        case UIInterfaceOrientationLandscapeRight:
kees@5640
    89
            return (self->window->w >= self->window->h);
kees@6001
    90
kees@5640
    91
        case UIInterfaceOrientationPortrait:
kees@5640
    92
        case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
    93
            return (self->window->h >= self->window->w);
kees@6001
    94
kees@5640
    95
        default: break;
kees@5640
    96
    }
kees@6001
    97
kees@5640
    98
    return NO;  // Nothing else is acceptable.
kees@5640
    99
}
kees@5640
   100
kees@6003
   101
- (void)loadView
kees@6003
   102
{
kees@5640
   103
    // do nothing.
kees@5640
   104
}
kees@5640
   105
kees@5640
   106
// Send a resized event when the orientation changes.
kees@6003
   107
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
kees@6003
   108
{
kees@5640
   109
    if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
kees@5640
   110
        return;   // don't care, we're just flipping over in this case.
kees@5640
   111
    }
kees@6001
   112
kees@5640
   113
    const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
kees@5640
   114
    SDL_WindowData *data = self->window->driverdata;
kees@5640
   115
    UIWindow *uiwindow = data->uiwindow;
kees@5640
   116
    UIScreen *uiscreen = [uiwindow screen];
kees@5640
   117
    const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
kees@5640
   118
    CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
kees@5640
   119
    const CGSize size = frame.size;
kees@5640
   120
    int w, h;
kees@6001
   121
kees@5640
   122
    switch (toInterfaceOrientation) {
kees@5640
   123
        case UIInterfaceOrientationPortrait:
kees@5640
   124
        case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
   125
            w = (size.width < size.height) ? size.width : size.height;
kees@5640
   126
            h = (size.width > size.height) ? size.width : size.height;
kees@5640
   127
            break;
kees@6001
   128
kees@5640
   129
        case UIInterfaceOrientationLandscapeLeft:
kees@5640
   130
        case UIInterfaceOrientationLandscapeRight:
kees@5640
   131
            w = (size.width > size.height) ? size.width : size.height;
kees@5640
   132
            h = (size.width < size.height) ? size.width : size.height;
kees@5640
   133
            break;
kees@6001
   134
kees@5640
   135
        default:
kees@5640
   136
            SDL_assert(0 && "Unexpected interface orientation!");
kees@5640
   137
            return;
kees@5640
   138
    }
kees@6001
   139
kees@5640
   140
    frame.size.width = w;
kees@5640
   141
    frame.size.height = h;
kees@5640
   142
    frame.origin.x = 0;
kees@5640
   143
    frame.origin.y = 0;
kees@6001
   144
kees@5640
   145
    [uiwindow setFrame:frame];
kees@5640
   146
    [data->view updateFrame];
kees@5640
   147
    SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
kees@5640
   148
}
kees@5640
   149
kees@5640
   150
@end