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