Commit 0b3a350c903c2bde462328642f03b4a0ab112b42

Sam Lantinga 2018-09-10T23:01:33

Fixed Chinese IME support (thanks ???!)

diff --git a/src/video/uikit/SDL_uikitviewcontroller.m b/src/video/uikit/SDL_uikitviewcontroller.m
index 2962742..49a39b6 100644
--- a/src/video/uikit/SDL_uikitviewcontroller.m
+++ b/src/video/uikit/SDL_uikitviewcontroller.m
@@ -74,6 +74,8 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
 #if SDL_IPHONE_KEYBOARD
     UITextField *textField;
     BOOL rotatingOrientation;
+    NSString *changeText;
+    NSString *obligateForBackspace;
 #endif
 }
 
@@ -250,10 +252,12 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
 /* Set ourselves up as a UITextFieldDelegate */
 - (void)initKeyboard
 {
+    changeText = nil;
+    obligateForBackspace = @"                                                                "; /* 64 space */
     textField = [[UITextField alloc] initWithFrame:CGRectZero];
     textField.delegate = self;
     /* placeholder so there is something to delete! */
-    textField.text = @" ";
+    textField.text = obligateForBackspace;
 
     /* set UITextInputTrait properties, mostly to defaults */
     textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
@@ -267,11 +271,12 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
     textField.hidden = YES;
     keyboardVisible = NO;
 
-#if !TARGET_OS_TV
     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+#if !TARGET_OS_TV
     [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
     [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
 #endif
+    [center addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
 }
 
 - (void)setView:(UIView *)view
@@ -310,11 +315,12 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
 
 - (void)deinitKeyboard
 {
-#if !TARGET_OS_TV
     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
+#if !TARGET_OS_TV
     [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
     [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
 #endif
+    [center removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
 }
 
 /* reveal onscreen virtual keyboard */
@@ -354,6 +360,50 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
     [self setKeyboardHeight:0];
 }
 
+- (void)textFieldTextDidChange:(NSNotification *)notification
+{
+    if (changeText!=nil && textField.markedTextRange == nil)
+    {
+        NSUInteger len = changeText.length;
+        if (len > 0) {
+            /* 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 = [changeText characterAtIndex:i];
+                SDL_Scancode code;
+                Uint16 mod;
+
+                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([changeText UTF8String]);
+        }
+        changeText = nil;
+    }
+}
+
 - (void)updateKeyboard
 {
     CGAffineTransform t = self.view.transform;
@@ -392,49 +442,20 @@ SDL_HideHomeIndicatorHintChanged(void *userdata, const char *name, const char *o
 - (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);
-            }
+        changeText = nil;
+        if (textField.markedTextRange == nil) {
+            /* it wants to replace text with nothing, ie a delete */
+            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
+            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
         }
-
-        SDL_SendKeyboardText([string UTF8String]);
+        if (textField.text.length < 16) {
+            textField.text = obligateForBackspace;
+        }
+    } else {
+        changeText = string;
     }
-
-    return NO; /* don't allow the edit! (keep placeholder text there) */
+    return YES;
 }
 
 /* Terminates the editing session */
@@ -498,7 +519,7 @@ UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
     @autoreleasepool {
         SDL_uikitviewcontroller *vc = GetWindowViewController(window);
         if (vc != nil) {
-            return vc.isKeyboardVisible;
+            return vc.keyboardVisible;
         }
         return SDL_FALSE;
     }