summaryrefslogtreecommitdiff
path: root/src/video/uikit/SDL_uikitviewcontroller.m
diff options
context:
space:
mode:
authorAlex Szpakowski <slime73@gmail.com>2015-01-15 01:06:14 -0400
committerAlex Szpakowski <slime73@gmail.com>2015-01-15 01:06:14 -0400
commit477e710f9bc80d896fcccaf13ba7be458d3d4138 (patch)
tree3800cde9be13e1f539c2bc6feae750a01ed10f39 /src/video/uikit/SDL_uikitviewcontroller.m
parent1890b2b28c1401ac28c85994820bcc310d831057 (diff)
downloadsdl-477e710f9bc80d896fcccaf13ba7be458d3d4138.tar.gz
Several improvements to the iOS backend:
- Added new custom launch screen code. It uses the launch screen nib when available on iOS 8+, the launch images dictionary if the launch screen nib isn't available, and the old standard image names if the launch image dictionary isn't in the plist. The launch screen is now hidden during the first call to SDL_PumpEvents rather than SDL_CreateWindow so apps can have the launch screen still visible if they do time-consuming loading after creating their window. It also fades out in roughly the same way as the system launch screen behavior. It can be disabled by setting the SDL_IPHONE_LAUNCHSCREEN define in SDL_config_iphoneos.h to 0. - A blank UIView is now created and displayed when the window is first created. The old behavior was to defer creating any view until SDL_GL_CreateContext, which prevented rotation, touch events, and other windowing-related things from working until then. This also makes it easier to use SDL_GetWindowWMInfo after creating a window. - Moved the keyboard and animation callback code from SDL's UIView subclasses to its UIViewController subclass, which lets them work properly in all cases when a SDL window is valid, even before SDL_GL_CreateContext is called and after SDL_GL_DeleteContext is called. - SDL_GL_CreateContext, SDL_GL_SwapWindow, SDL_GL_MakeCurrent, and SDL_GL_DeleteContext are more robust. - Fixed some edge cases where SDL windows weren't rotating properly or their reported sizes were out of sync with their actual sizes. - Removed all calls to [UIApplication setStatusBarOrientation:]. It doesn't seem to work as expected in all cases in recent iOS versions. - Some code style cleanup.
Diffstat (limited to 'src/video/uikit/SDL_uikitviewcontroller.m')
-rw-r--r--src/video/uikit/SDL_uikitviewcontroller.m339
1 files changed, 333 insertions, 6 deletions
diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m
index 77cbc0ba5..af1f68dc1 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.m
+++ b/src/video/uikit/SDL_uikitviewcontroller.m
@@ -28,27 +28,87 @@
#include "../SDL_sysvideo.h"
#include "../../events/SDL_events_c.h"
-#include "SDL_uikitviewcontroller.h"
+#import "SDL_uikitviewcontroller.h"
+#import "SDL_uikitmessagebox.h"
#include "SDL_uikitvideo.h"
#include "SDL_uikitmodes.h"
#include "SDL_uikitwindow.h"
+#if SDL_IPHONE_KEYBOARD
+#include "keyinfotable.h"
+#endif
-@implementation SDL_uikitviewcontroller
+@implementation SDL_uikitviewcontroller {
+ CADisplayLink *displayLink;
+ int animationInterval;
+ void (*animationCallback)(void*);
+ void *animationCallbackParam;
+
+#if SDL_IPHONE_KEYBOARD
+ UITextField *textField;
+#endif
+}
@synthesize window;
-- (id)initWithSDLWindow:(SDL_Window *)_window
+- (instancetype)initWithSDLWindow:(SDL_Window *)_window
{
if (self = [super initWithNibName:nil bundle:nil]) {
self.window = _window;
+
+#if SDL_IPHONE_KEYBOARD
+ [self initKeyboard];
+#endif
}
return self;
}
+- (void)dealloc
+{
+#if SDL_IPHONE_KEYBOARD
+ [self deinitKeyboard];
+#endif
+}
+
+- (void)setAnimationCallback:(int)interval
+ callback:(void (*)(void*))callback
+ callbackParam:(void*)callbackParam
+{
+ [self stopAnimation];
+
+ animationInterval = interval;
+ animationCallback = callback;
+ animationCallbackParam = callbackParam;
+
+ if (animationCallback) {
+ [self startAnimation];
+ }
+}
+
+- (void)startAnimation
+{
+ displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(doLoop:)];
+ [displayLink setFrameInterval:animationInterval];
+ [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
+}
+
+- (void)stopAnimation
+{
+ [displayLink invalidate];
+ displayLink = nil;
+}
+
+- (void)doLoop:(CADisplayLink*)sender
+{
+ /* Don't run the game loop while a messagebox is up */
+ if (!UIKit_ShowingMessageBox()) {
+ animationCallback(animationCallbackParam);
+ }
+}
+
- (void)loadView
{
- /* do nothing. */
+ /* Do nothing. */
}
- (void)viewDidLayoutSubviews
@@ -67,8 +127,7 @@
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
{
- NSUInteger orientationMask = [self supportedInterfaceOrientations];
- return (orientationMask & (1 << orient));
+ return ([self supportedInterfaceOrientations] & (1 << orient)) != 0;
}
- (BOOL)prefersStatusBarHidden
@@ -82,8 +141,276 @@
return UIStatusBarStyleLightContent;
}
+/*
+ ---- Keyboard related functionality below this line ----
+ */
+#if SDL_IPHONE_KEYBOARD
+
+@synthesize textInputRect;
+@synthesize keyboardHeight;
+@synthesize keyboardVisible;
+
+/* Set ourselves up as a UITextFieldDelegate */
+- (void)initKeyboard
+{
+ textField = [[UITextField alloc] initWithFrame:CGRectZero];
+ textField.delegate = self;
+ /* placeholder so there is something to delete! */
+ textField.text = @" ";
+
+ /* set UITextInputTrait properties, mostly to defaults */
+ textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
+ textField.autocorrectionType = UITextAutocorrectionTypeNo;
+ textField.enablesReturnKeyAutomatically = NO;
+ textField.keyboardAppearance = UIKeyboardAppearanceDefault;
+ textField.keyboardType = UIKeyboardTypeDefault;
+ textField.returnKeyType = UIReturnKeyDefault;
+ textField.secureTextEntry = NO;
+
+ textField.hidden = YES;
+ keyboardVisible = NO;
+
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+ [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
+ [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
+}
+
+- (void)setView:(UIView *)view
+{
+ [super setView:view];
+
+ [view addSubview:textField];
+
+ if (keyboardVisible) {
+ [self showKeyboard];
+ }
+}
+
+- (void)deinitKeyboard
+{
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+ [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
+ [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
+}
+
+/* reveal onscreen virtual keyboard */
+- (void)showKeyboard
+{
+ keyboardVisible = YES;
+ if (textField.window) {
+ [textField becomeFirstResponder];
+ }
+}
+
+/* hide onscreen virtual keyboard */
+- (void)hideKeyboard
+{
+ keyboardVisible = NO;
+ [textField resignFirstResponder];
+}
+
+- (void)keyboardWillShow:(NSNotification *)notification
+{
+ CGRect kbrect = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue];
+ UIView *view = self.view;
+ int height = 0;
+
+ /* The keyboard rect is in the coordinate space of the screen, but we want
+ * its height in the view's coordinate space. */
+#ifdef __IPHONE_8_0
+ if ([view respondsToSelector:@selector(convertRect:fromCoordinateSpace:)]) {
+ UIScreen *screen = view.window.screen;
+ kbrect = [view convertRect:kbrect fromCoordinateSpace:screen.coordinateSpace];
+ height = kbrect.size.height;
+ } else
+#endif
+ {
+ /* In iOS 7 and below, the screen's coordinate space is never rotated. */
+ if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
+ height = kbrect.size.width;
+ } else {
+ height = kbrect.size.height;
+ }
+ }
+
+ [self setKeyboardHeight:height];
+}
+
+- (void)keyboardWillHide:(NSNotification *)notification
+{
+ [self setKeyboardHeight:0];
+}
+
+- (void)updateKeyboard
+{
+ SDL_Rect textrect = self.textInputRect;
+ CGAffineTransform t = self.view.transform;
+ CGPoint offset = CGPointMake(0.0, 0.0);
+
+ if (self.keyboardHeight) {
+ int rectbottom = textrect.y + textrect.h;
+ int kbottom = self.view.bounds.size.height - self.keyboardHeight;
+ if (kbottom < rectbottom) {
+ offset.y = kbottom - rectbottom;
+ }
+ }
+
+ /* Put the offset into the this view transform's coordinate space. */
+ t.tx = 0.0;
+ t.ty = 0.0;
+ offset = CGPointApplyAffineTransform(offset, t);
+
+ t.tx = offset.x;
+ t.ty = offset.y;
+
+ /* Move the view by applying the updated transform. */
+ self.view.transform = t;
+}
+
+- (void)setKeyboardHeight:(int)height
+{
+ keyboardVisible = height > 0;
+ keyboardHeight = height;
+ [self updateKeyboard];
+}
+
+/* UITextFieldDelegate method. Invoked when user types something. */
+- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
+{
+ NSUInteger len = string.length;
+
+ if (len == 0) {
+ /* it wants to replace text with nothing, ie a delete */
+ SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
+ SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
+ } else {
+ /* go through all the characters in the string we've been sent and
+ * convert them to key presses */
+ int i;
+ for (i = 0; i < len; i++) {
+ unichar c = [string characterAtIndex:i];
+ Uint16 mod = 0;
+ SDL_Scancode code;
+
+ if (c < 127) {
+ /* figure out the SDL_Scancode and SDL_keymod for this unichar */
+ code = unicharToUIKeyInfoTable[c].code;
+ mod = unicharToUIKeyInfoTable[c].mod;
+ } else {
+ /* we only deal with ASCII right now */
+ code = SDL_SCANCODE_UNKNOWN;
+ mod = 0;
+ }
+
+ if (mod & KMOD_SHIFT) {
+ /* If character uses shift, press shift down */
+ SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
+ }
+
+ /* send a keydown and keyup even for the character */
+ SDL_SendKeyboardKey(SDL_PRESSED, code);
+ SDL_SendKeyboardKey(SDL_RELEASED, code);
+
+ if (mod & KMOD_SHIFT) {
+ /* If character uses shift, press shift back up */
+ SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
+ }
+ }
+
+ SDL_SendKeyboardText([string UTF8String]);
+ }
+
+ return NO; /* don't allow the edit! (keep placeholder text there) */
+}
+
+/* Terminates the editing session */
+- (BOOL)textFieldShouldReturn:(UITextField*)_textField
+{
+ SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
+ SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
+ SDL_StopTextInput();
+ return YES;
+}
+
+#endif
+
@end
+/* iPhone keyboard addition functions */
+#if SDL_IPHONE_KEYBOARD
+
+static SDL_uikitviewcontroller *
+GetWindowViewController(SDL_Window * window)
+{
+ if (!window || !window->driverdata) {
+ SDL_SetError("Invalid window");
+ return nil;
+ }
+
+ SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
+
+ return data.viewcontroller;
+}
+
+SDL_bool
+UIKit_HasScreenKeyboardSupport(_THIS)
+{
+ return SDL_TRUE;
+}
+
+void
+UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
+{
+ @autoreleasepool {
+ SDL_uikitviewcontroller *vc = GetWindowViewController(window);
+ [vc showKeyboard];
+ }
+}
+
+void
+UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
+{
+ @autoreleasepool {
+ SDL_uikitviewcontroller *vc = GetWindowViewController(window);
+ [vc hideKeyboard];
+ }
+}
+
+SDL_bool
+UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
+{
+ @autoreleasepool {
+ SDL_uikitviewcontroller *vc = GetWindowViewController(window);
+ if (vc != nil) {
+ return vc.isKeyboardVisible;
+ }
+ return SDL_FALSE;
+ }
+}
+
+void
+UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
+{
+ if (!rect) {
+ SDL_InvalidParamError("rect");
+ return;
+ }
+
+ @autoreleasepool {
+ SDL_uikitviewcontroller *vc = GetWindowViewController(SDL_GetFocusWindow());
+ if (vc != nil) {
+ vc.textInputRect = *rect;
+
+ if (vc.keyboardVisible) {
+ [vc updateKeyboard];
+ }
+ }
+ }
+}
+
+
+#endif /* SDL_IPHONE_KEYBOARD */
+
#endif /* SDL_VIDEO_DRIVER_UIKIT */
/* vi: set ts=4 sw=4 expandtab: */