src/video/uikit/SDL_uikitviewcontroller.m
author Sam Lantinga
Tue, 18 Sep 2012 23:13:35 -0700
changeset 6440 f0ca76958984
parent 6438 2c0fa683b18c
child 6451 52e5a05ca795
permissions -rwxr-xr-x
Generate resize event when the status bar changes size (e.g. call in progress, etc.)
kees@5640
     1
/*
slouken@6044
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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.
slouken@6044
     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
*/
slouken@6044
    21
#include "SDL_config.h"
kees@6001
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
kees@5640
    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"
icculus@6093
    32
#include "SDL_uikitviewcontroller.h"
icculus@6093
    33
#include "SDL_uikitvideo.h"
kees@5640
    34
kees@5640
    35
@implementation SDL_uikitviewcontroller
kees@5640
    36
kees@6011
    37
@synthesize window;
kees@6011
    38
kees@6003
    39
- (id)initWithSDLWindow:(SDL_Window *)_window
kees@6003
    40
{
kees@6011
    41
    self = [self init];
kees@6011
    42
    if (self == nil) {
kees@5640
    43
        return nil;
kees@5640
    44
    }
kees@6011
    45
    self.window = _window;
slouken@6440
    46
slouken@6440
    47
    // Register for notification when the status bar size changes    
slouken@6440
    48
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
slouken@6440
    49
kees@5640
    50
    return self;
kees@5640
    51
}
kees@5640
    52
slouken@6440
    53
- (void)loadView
slouken@6440
    54
{
slouken@6440
    55
    // do nothing.
slouken@6440
    56
}
slouken@6440
    57
slouken@6440
    58
- (void)statusBarFrameChanged:(NSNotification*)notification
slouken@6440
    59
{
slouken@6440
    60
    [self onWindowSizeChanged];
slouken@6440
    61
}
slouken@6440
    62
slouken@6440
    63
- (void)onWindowSizeChanged
slouken@6440
    64
{
slouken@6440
    65
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
slouken@6440
    66
        SDL_WindowData *data = self->window->driverdata;
slouken@6440
    67
        SDL_VideoDisplay *display = SDL_GetDisplayForWindow(self->window);
slouken@6440
    68
        SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
slouken@6440
    69
        const CGSize size = data->view.bounds.size;
slouken@6440
    70
        int w, h;
slouken@6440
    71
slouken@6440
    72
        w = (int)(size.width * displaymodedata->scale);
slouken@6440
    73
        h = (int)(size.height * displaymodedata->scale);
slouken@6440
    74
slouken@6440
    75
        SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@6440
    76
    }
slouken@6440
    77
}
slouken@6440
    78
kees@6003
    79
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
    80
{
slouken@6438
    81
    // Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation
slouken@6438
    82
    if (orient == UIInterfaceOrientationPortraitUpsideDown) {
slouken@6438
    83
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
slouken@6438
    84
            return NO;
slouken@6438
    85
    }
slouken@6438
    86
kees@5640
    87
    const char *orientationsCString;
kees@5640
    88
    if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
kees@5640
    89
        BOOL rotate = NO;
kees@5640
    90
        NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
kees@5640
    91
                                                            encoding:NSUTF8StringEncoding];
kees@5640
    92
        NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
kees@5640
    93
                                 [NSCharacterSet characterSetWithCharactersInString:@" "]];
kees@6001
    94
kees@5640
    95
        switch (orient) {
kees@5640
    96
            case UIInterfaceOrientationLandscapeLeft:
kees@5640
    97
                rotate = [orientations containsObject:@"LandscapeLeft"];
kees@5640
    98
                break;
kees@6001
    99
kees@5640
   100
            case UIInterfaceOrientationLandscapeRight:
kees@5640
   101
                rotate = [orientations containsObject:@"LandscapeRight"];
kees@5640
   102
                break;
kees@6001
   103
kees@5640
   104
            case UIInterfaceOrientationPortrait:
kees@5640
   105
                rotate = [orientations containsObject:@"Portrait"];
kees@5640
   106
                break;
kees@6001
   107
kees@5640
   108
            case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
   109
                rotate = [orientations containsObject:@"PortraitUpsideDown"];
kees@5640
   110
                break;
kees@6001
   111
kees@5640
   112
            default: break;
kees@5640
   113
        }
kees@6001
   114
kees@5640
   115
        return rotate;
kees@5640
   116
    }
kees@6001
   117
kees@5640
   118
    if (self->window->flags & SDL_WINDOW_RESIZABLE) {
kees@5640
   119
        return YES;  // any orientation is okay.
kees@5640
   120
    }
kees@6001
   121
kees@5640
   122
    // If not resizable, allow device to orient to other matching sizes
kees@5640
   123
    //  (that is, let the user turn the device upside down...same screen
kees@5640
   124
    //   dimensions, but it lets the user place the device where it's most
kees@5640
   125
    //   comfortable in relation to its physical buttons, headphone jack, etc).
kees@5640
   126
    switch (orient) {
kees@5640
   127
        case UIInterfaceOrientationLandscapeLeft:
kees@5640
   128
        case UIInterfaceOrientationLandscapeRight:
kees@5640
   129
            return (self->window->w >= self->window->h);
kees@6001
   130
kees@5640
   131
        case UIInterfaceOrientationPortrait:
kees@5640
   132
        case UIInterfaceOrientationPortraitUpsideDown:
kees@5640
   133
            return (self->window->h >= self->window->w);
kees@6001
   134
kees@5640
   135
        default: break;
kees@5640
   136
    }
kees@6001
   137
kees@5640
   138
    return NO;  // Nothing else is acceptable.
kees@5640
   139
}
kees@5640
   140
kees@5640
   141
// Send a resized event when the orientation changes.
kees@6003
   142
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
kees@6003
   143
{
slouken@6440
   144
    [self onWindowSizeChanged];
kees@5640
   145
}
kees@5640
   146
slouken@6044
   147
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   148
kees@5640
   149
@end
slouken@6440
   150
slouken@6440
   151
/* vi: set ts=4 sw=4 expandtab: */