src/video/uikit/SDL_uikitviewcontroller.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 6011 83a21d3bd5c1
child 6077 ee00e4b80f79
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
kees@5640
     1
/*
slouken@6044
     2
  Simple DirectMedia Layer
slouken@6044
     3
  Copyright (C) 1997-2011 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.
kees@6001
     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
*/
kees@5640
    21
#include "SDL_config.h"
kees@5640
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
slouken@6044
    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
kees@5640
    31
#include "SDL_uikitwindow.h"
kees@5640
    32
kees@5640
    33
#import "SDL_uikitviewcontroller.h"
kees@5640
    34
kees@5640
    35
kees@5640
    36
@implementation SDL_uikitviewcontroller
kees@5640
    37
kees@6011
    38
@synthesize window;
kees@6011
    39
kees@6003
    40
- (id)initWithSDLWindow:(SDL_Window *)_window
kees@6003
    41
{
kees@6011
    42
    self = [self init];
kees@6011
    43
    if (self == nil) {
kees@5640
    44
        return nil;
kees@5640
    45
    }
kees@6011
    46
    self.window = _window;
kees@5640
    47
    return self;
kees@5640
    48
}
kees@5640
    49
kees@6003
    50
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
    51
{
kees@5640
    52
    const char *orientationsCString;
kees@5640
    53
    if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
kees@5640
    54
        BOOL rotate = NO;
kees@5640
    55
        NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
kees@5640
    56
                                                            encoding:NSUTF8StringEncoding];
kees@5640
    57
        NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
kees@5640
    58
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
kees@6001
    59
kees@5640
    60
        switch (orient) {
kees@5640
    61
            case UIInterfaceOrientationLandscapeLeft:
kees@5640
    62
                rotate = [orientations containsObject:@"LandscapeLeft"];
kees@5640
    63
                break;
kees@6001
    64
kees@5640
    65
            case UIInterfaceOrientationLandscapeRight:
kees@5640
    66
                rotate = [orientations containsObject:@"LandscapeRight"];
kees@5640
    67
                break;
kees@6001
    68
kees@5640
    69
            case UIInterfaceOrientationPortrait:
kees@5640
    70
                rotate = [orientations containsObject:@"Portrait"];
kees@5640
    71
                break;
kees@6001
    72
kees@5640
    73
            case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
    74
                rotate = [orientations containsObject:@"PortraitUpsideDown"];
kees@5640
    75
                break;
kees@6001
    76
kees@5640
    77
            default: break;
kees@5640
    78
        }
kees@6001
    79
kees@5640
    80
        return rotate;
kees@5640
    81
    }
kees@6001
    82
kees@5640
    83
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
kees@5640
    84
        return YES;  // any orientation is okay.
kees@5640
    85
    }
kees@6001
    86
kees@5640
    87
    // If not resizable, allow device to orient to other matching sizes
kees@5640
    88
    //  (that is, let the user turn the device upside down...same screen
kees@5640
    89
    //   dimensions, but it lets the user place the device where it's most
kees@5640
    90
    //   comfortable in relation to its physical buttons, headphone jack, etc).
kees@5640
    91
    switch (orient) {
kees@5640
    92
        case UIInterfaceOrientationLandscapeLeft:
kees@5640
    93
        case UIInterfaceOrientationLandscapeRight:
kees@5640
    94
            return (self->window->w >= self->window->h);
kees@6001
    95
kees@5640
    96
        case UIInterfaceOrientationPortrait:
kees@5640
    97
        case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
    98
            return (self->window->h >= self->window->w);
kees@6001
    99
kees@5640
   100
        default: break;
kees@5640
   101
    }
kees@6001
   102
kees@5640
   103
    return NO;  // Nothing else is acceptable.
kees@5640
   104
}
kees@5640
   105
kees@6003
   106
- (void)loadView
kees@6003
   107
{
kees@5640
   108
    // do nothing.
kees@5640
   109
}
kees@5640
   110
kees@5640
   111
// Send a resized event when the orientation changes.
kees@6003
   112
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
kees@6003
   113
{
kees@5640
   114
    if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
kees@5640
   115
        return;   // don't care, we're just flipping over in this case.
kees@5640
   116
    }
kees@6001
   117
kees@5640
   118
    const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
kees@5640
   119
    SDL_WindowData *data = self->window->driverdata;
kees@5640
   120
    UIWindow *uiwindow = data->uiwindow;
kees@5640
   121
    UIScreen *uiscreen = [uiwindow screen];
kees@5640
   122
    const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
kees@5640
   123
    CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
kees@5640
   124
    const CGSize size = frame.size;
kees@5640
   125
    int w, h;
kees@6001
   126
kees@5640
   127
    switch (toInterfaceOrientation) {
kees@5640
   128
        case UIInterfaceOrientationPortrait:
kees@5640
   129
        case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
   130
            w = (size.width < size.height) ? size.width : size.height;
kees@5640
   131
            h = (size.width > size.height) ? size.width : size.height;
kees@5640
   132
            break;
kees@6001
   133
kees@5640
   134
        case UIInterfaceOrientationLandscapeLeft:
kees@5640
   135
        case UIInterfaceOrientationLandscapeRight:
kees@5640
   136
            w = (size.width > size.height) ? size.width : size.height;
kees@5640
   137
            h = (size.width < size.height) ? size.width : size.height;
kees@5640
   138
            break;
kees@6001
   139
kees@5640
   140
        default:
kees@5640
   141
            SDL_assert(0 && "Unexpected interface orientation!");
kees@5640
   142
            return;
kees@5640
   143
    }
kees@6001
   144
kees@5640
   145
    frame.size.width = w;
kees@5640
   146
    frame.size.height = h;
kees@5640
   147
    frame.origin.x = 0;
kees@5640
   148
    frame.origin.y = 0;
kees@6001
   149
kees@5640
   150
    [uiwindow setFrame:frame];
kees@5640
   151
    [data->view updateFrame];
kees@5640
   152
    SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
kees@5640
   153
}
kees@5640
   154
slouken@6044
   155
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   156
kees@5640
   157
@end