src/video/uikit/SDL_uikitviewcontroller.m
changeset 5640 04b5b11985c8
child 6001 3ae3d1e58d59
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/uikit/SDL_uikitviewcontroller.m	Wed Aug 17 23:26:58 2011 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 + Simple DirectMedia Layer
     1.6 + Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.7 + 
     1.8 + This software is provided 'as-is', without any express or implied
     1.9 + warranty.  In no event will the authors be held liable for any damages
    1.10 + arising from the use of this software.
    1.11 + 
    1.12 + Permission is granted to anyone to use this software for any purpose,
    1.13 + including commercial applications, and to alter it and redistribute it
    1.14 + freely, subject to the following restrictions:
    1.15 + 
    1.16 + 1. The origin of this software must not be misrepresented; you must not
    1.17 + claim that you wrote the original software. If you use this software
    1.18 + in a product, an acknowledgment in the product documentation would be
    1.19 + appreciated but is not required.
    1.20 + 2. Altered source versions must be plainly marked as such, and must not be
    1.21 + misrepresented as being the original software.
    1.22 + 3. This notice may not be removed or altered from any source distribution.
    1.23 + */
    1.24 +#include "SDL_config.h"
    1.25 +
    1.26 +#include "SDL_video.h"
    1.27 +#include "SDL_assert.h"
    1.28 +#include "SDL_hints.h"
    1.29 +#include "../SDL_sysvideo.h"
    1.30 +#include "../../events/SDL_events_c.h"
    1.31 +
    1.32 +#include "SDL_uikitwindow.h"
    1.33 +
    1.34 +#import "SDL_uikitviewcontroller.h"
    1.35 +
    1.36 +
    1.37 +@implementation SDL_uikitviewcontroller
    1.38 +
    1.39 +- (id)initWithSDLWindow:(SDL_Window *)_window {
    1.40 +    if ((self = [self init]) == nil) {
    1.41 +        return nil;
    1.42 +    }
    1.43 +    self->window = _window;
    1.44 +    return self;
    1.45 +}
    1.46 +
    1.47 +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient {
    1.48 +    const char *orientationsCString;
    1.49 +    if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
    1.50 +        BOOL rotate = NO;
    1.51 +        NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
    1.52 +                                                            encoding:NSUTF8StringEncoding];
    1.53 +        NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
    1.54 +                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
    1.55 +        
    1.56 +        switch (orient) {
    1.57 +            case UIInterfaceOrientationLandscapeLeft:
    1.58 +                rotate = [orientations containsObject:@"LandscapeLeft"];
    1.59 +                break;
    1.60 +                
    1.61 +            case UIInterfaceOrientationLandscapeRight:
    1.62 +                rotate = [orientations containsObject:@"LandscapeRight"];
    1.63 +                break;
    1.64 +                
    1.65 +            case UIInterfaceOrientationPortrait:
    1.66 +                rotate = [orientations containsObject:@"Portrait"];
    1.67 +                break;
    1.68 +                
    1.69 +            case UIInterfaceOrientationPortraitUpsideDown:
    1.70 +                rotate = [orientations containsObject:@"PortraitUpsideDown"];
    1.71 +                break;
    1.72 +                
    1.73 +            default: break;
    1.74 +        }
    1.75 +        
    1.76 +        return rotate;
    1.77 +    }
    1.78 +    
    1.79 +    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
    1.80 +        return YES;  // any orientation is okay.
    1.81 +    }
    1.82 +    
    1.83 +    // If not resizable, allow device to orient to other matching sizes
    1.84 +    //  (that is, let the user turn the device upside down...same screen
    1.85 +    //   dimensions, but it lets the user place the device where it's most
    1.86 +    //   comfortable in relation to its physical buttons, headphone jack, etc).
    1.87 +    switch (orient) {
    1.88 +        case UIInterfaceOrientationLandscapeLeft:
    1.89 +        case UIInterfaceOrientationLandscapeRight:
    1.90 +            return (self->window->w >= self->window->h);
    1.91 +            
    1.92 +        case UIInterfaceOrientationPortrait:
    1.93 +        case UIInterfaceOrientationPortraitUpsideDown:
    1.94 +            return (self->window->h >= self->window->w);
    1.95 +            
    1.96 +        default: break;
    1.97 +    }
    1.98 +    
    1.99 +    return NO;  // Nothing else is acceptable.
   1.100 +}
   1.101 +
   1.102 +- (void)loadView  {
   1.103 +    // do nothing.
   1.104 +}
   1.105 +
   1.106 +// Send a resized event when the orientation changes.
   1.107 +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
   1.108 +    if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
   1.109 +        return;   // don't care, we're just flipping over in this case.
   1.110 +    }
   1.111 +    
   1.112 +    const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
   1.113 +    SDL_WindowData *data = self->window->driverdata;
   1.114 +    UIWindow *uiwindow = data->uiwindow;
   1.115 +    UIScreen *uiscreen = [uiwindow screen];
   1.116 +    const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
   1.117 +    CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
   1.118 +    const CGSize size = frame.size;
   1.119 +    int w, h;
   1.120 +    
   1.121 +    switch (toInterfaceOrientation) {
   1.122 +        case UIInterfaceOrientationPortrait:
   1.123 +        case UIInterfaceOrientationPortraitUpsideDown:
   1.124 +            w = (size.width < size.height) ? size.width : size.height;
   1.125 +            h = (size.width > size.height) ? size.width : size.height;
   1.126 +            break;
   1.127 +            
   1.128 +        case UIInterfaceOrientationLandscapeLeft:
   1.129 +        case UIInterfaceOrientationLandscapeRight:
   1.130 +            w = (size.width > size.height) ? size.width : size.height;
   1.131 +            h = (size.width < size.height) ? size.width : size.height;
   1.132 +            break;
   1.133 +            
   1.134 +        default:
   1.135 +            SDL_assert(0 && "Unexpected interface orientation!");
   1.136 +            return;
   1.137 +    }
   1.138 +    
   1.139 +    frame.size.width = w;
   1.140 +    frame.size.height = h;
   1.141 +    frame.origin.x = 0;
   1.142 +    frame.origin.y = 0;
   1.143 +    
   1.144 +    [uiwindow setFrame:frame];
   1.145 +    [data->view updateFrame];
   1.146 +    SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
   1.147 +}
   1.148 +
   1.149 +@end