2011-08-17 23:26:58 +02:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
|
|
|
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
#include "SDL_config.h"
|
|
|
|
|
|
|
|
#include "SDL_video.h"
|
|
|
|
#include "SDL_assert.h"
|
|
|
|
#include "SDL_hints.h"
|
|
|
|
#include "../SDL_sysvideo.h"
|
|
|
|
#include "../../events/SDL_events_c.h"
|
|
|
|
|
|
|
|
#include "SDL_uikitwindow.h"
|
|
|
|
|
|
|
|
#import "SDL_uikitviewcontroller.h"
|
|
|
|
|
|
|
|
|
|
|
|
@implementation SDL_uikitviewcontroller
|
|
|
|
|
2011-09-28 21:42:02 +02:00
|
|
|
@synthesize window;
|
|
|
|
|
2011-09-27 22:51:26 +02:00
|
|
|
- (id)initWithSDLWindow:(SDL_Window *)_window
|
|
|
|
{
|
2011-09-28 21:42:02 +02:00
|
|
|
self = [self init];
|
|
|
|
if (self == nil) {
|
2011-08-17 23:26:58 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2011-09-28 21:42:02 +02:00
|
|
|
self.window = _window;
|
2011-08-17 23:26:58 +02:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2011-09-27 22:51:26 +02:00
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
|
|
|
|
{
|
2011-08-17 23:26:58 +02:00
|
|
|
const char *orientationsCString;
|
|
|
|
if ((orientationsCString = SDL_GetHint(SDL_HINT_ORIENTATIONS)) != NULL) {
|
|
|
|
BOOL rotate = NO;
|
|
|
|
NSString *orientationsNSString = [NSString stringWithCString:orientationsCString
|
|
|
|
encoding:NSUTF8StringEncoding];
|
|
|
|
NSArray *orientations = [orientationsNSString componentsSeparatedByCharactersInSet:
|
|
|
|
[NSCharacterSet characterSetWithCharactersInString:@" "]];
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
switch (orient) {
|
|
|
|
case UIInterfaceOrientationLandscapeLeft:
|
|
|
|
rotate = [orientations containsObject:@"LandscapeLeft"];
|
|
|
|
break;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
case UIInterfaceOrientationLandscapeRight:
|
|
|
|
rotate = [orientations containsObject:@"LandscapeRight"];
|
|
|
|
break;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
case UIInterfaceOrientationPortrait:
|
|
|
|
rotate = [orientations containsObject:@"Portrait"];
|
|
|
|
break;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
case UIInterfaceOrientationPortraitUpsideDown:
|
|
|
|
rotate = [orientations containsObject:@"PortraitUpsideDown"];
|
|
|
|
break;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
default: break;
|
|
|
|
}
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
return rotate;
|
|
|
|
}
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
if (self->window->flags & SDL_WINDOW_RESIZABLE) {
|
|
|
|
return YES; // any orientation is okay.
|
|
|
|
}
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
// If not resizable, allow device to orient to other matching sizes
|
|
|
|
// (that is, let the user turn the device upside down...same screen
|
|
|
|
// dimensions, but it lets the user place the device where it's most
|
|
|
|
// comfortable in relation to its physical buttons, headphone jack, etc).
|
|
|
|
switch (orient) {
|
|
|
|
case UIInterfaceOrientationLandscapeLeft:
|
|
|
|
case UIInterfaceOrientationLandscapeRight:
|
|
|
|
return (self->window->w >= self->window->h);
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
case UIInterfaceOrientationPortrait:
|
|
|
|
case UIInterfaceOrientationPortraitUpsideDown:
|
|
|
|
return (self->window->h >= self->window->w);
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
default: break;
|
|
|
|
}
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
return NO; // Nothing else is acceptable.
|
|
|
|
}
|
|
|
|
|
2011-09-27 22:51:26 +02:00
|
|
|
- (void)loadView
|
|
|
|
{
|
2011-08-17 23:26:58 +02:00
|
|
|
// do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a resized event when the orientation changes.
|
2011-09-27 22:51:26 +02:00
|
|
|
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
|
|
|
|
{
|
2011-08-17 23:26:58 +02:00
|
|
|
if ((self->window->flags & SDL_WINDOW_RESIZABLE) == 0) {
|
|
|
|
return; // don't care, we're just flipping over in this case.
|
|
|
|
}
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
const UIInterfaceOrientation toInterfaceOrientation = [self interfaceOrientation];
|
|
|
|
SDL_WindowData *data = self->window->driverdata;
|
|
|
|
UIWindow *uiwindow = data->uiwindow;
|
|
|
|
UIScreen *uiscreen = [uiwindow screen];
|
|
|
|
const int noborder = self->window->flags & SDL_WINDOW_BORDERLESS;
|
|
|
|
CGRect frame = noborder ? [uiscreen bounds] : [uiscreen applicationFrame];
|
|
|
|
const CGSize size = frame.size;
|
|
|
|
int w, h;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
switch (toInterfaceOrientation) {
|
|
|
|
case UIInterfaceOrientationPortrait:
|
|
|
|
case UIInterfaceOrientationPortraitUpsideDown:
|
|
|
|
w = (size.width < size.height) ? size.width : size.height;
|
|
|
|
h = (size.width > size.height) ? size.width : size.height;
|
|
|
|
break;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
case UIInterfaceOrientationLandscapeLeft:
|
|
|
|
case UIInterfaceOrientationLandscapeRight:
|
|
|
|
w = (size.width > size.height) ? size.width : size.height;
|
|
|
|
h = (size.width < size.height) ? size.width : size.height;
|
|
|
|
break;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
default:
|
|
|
|
SDL_assert(0 && "Unexpected interface orientation!");
|
|
|
|
return;
|
|
|
|
}
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
frame.size.width = w;
|
|
|
|
frame.size.height = h;
|
|
|
|
frame.origin.x = 0;
|
|
|
|
frame.origin.y = 0;
|
2011-09-27 21:02:26 +02:00
|
|
|
|
2011-08-17 23:26:58 +02:00
|
|
|
[uiwindow setFrame:frame];
|
|
|
|
[data->view updateFrame];
|
|
|
|
SDL_SendWindowEvent(self->window, SDL_WINDOWEVENT_RESIZED, w, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|