src/video/uikit/SDL_uikitviewcontroller.m
changeset 12184 02509665751f
parent 11842 9577835e5809
child 12503 806492103856
equal deleted inserted replaced
12183:364f514f94d8 12184:02509665751f
    72     void *animationCallbackParam;
    72     void *animationCallbackParam;
    73 
    73 
    74 #if SDL_IPHONE_KEYBOARD
    74 #if SDL_IPHONE_KEYBOARD
    75     UITextField *textField;
    75     UITextField *textField;
    76     BOOL rotatingOrientation;
    76     BOOL rotatingOrientation;
       
    77     NSString *changeText;
       
    78     NSString *obligateForBackspace;
    77 #endif
    79 #endif
    78 }
    80 }
    79 
    81 
    80 @synthesize window;
    82 @synthesize window;
    81 
    83 
   248 @synthesize keyboardVisible;
   250 @synthesize keyboardVisible;
   249 
   251 
   250 /* Set ourselves up as a UITextFieldDelegate */
   252 /* Set ourselves up as a UITextFieldDelegate */
   251 - (void)initKeyboard
   253 - (void)initKeyboard
   252 {
   254 {
       
   255     changeText = nil;
       
   256     obligateForBackspace = @"                                                                "; /* 64 space */
   253     textField = [[UITextField alloc] initWithFrame:CGRectZero];
   257     textField = [[UITextField alloc] initWithFrame:CGRectZero];
   254     textField.delegate = self;
   258     textField.delegate = self;
   255     /* placeholder so there is something to delete! */
   259     /* placeholder so there is something to delete! */
   256     textField.text = @" ";
   260     textField.text = obligateForBackspace;
   257 
   261 
   258     /* set UITextInputTrait properties, mostly to defaults */
   262     /* set UITextInputTrait properties, mostly to defaults */
   259     textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
   263     textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
   260     textField.autocorrectionType = UITextAutocorrectionTypeNo;
   264     textField.autocorrectionType = UITextAutocorrectionTypeNo;
   261     textField.enablesReturnKeyAutomatically = NO;
   265     textField.enablesReturnKeyAutomatically = NO;
   265     textField.secureTextEntry = NO;
   269     textField.secureTextEntry = NO;
   266 
   270 
   267     textField.hidden = YES;
   271     textField.hidden = YES;
   268     keyboardVisible = NO;
   272     keyboardVisible = NO;
   269 
   273 
   270 #if !TARGET_OS_TV
       
   271     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
   274     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
       
   275 #if !TARGET_OS_TV
   272     [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   276     [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
   273     [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   277     [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
   274 #endif
   278 #endif
       
   279     [center addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
   275 }
   280 }
   276 
   281 
   277 - (void)setView:(UIView *)view
   282 - (void)setView:(UIView *)view
   278 {
   283 {
   279     [super setView:view];
   284     [super setView:view];
   308 }
   313 }
   309 #endif /* TARGET_OS_TV || __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000 */
   314 #endif /* TARGET_OS_TV || __IPHONE_OS_VERSION_MIN_REQUIRED >= 80000 */
   310 
   315 
   311 - (void)deinitKeyboard
   316 - (void)deinitKeyboard
   312 {
   317 {
   313 #if !TARGET_OS_TV
       
   314     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
   318     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
       
   319 #if !TARGET_OS_TV
   315     [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
   320     [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
   316     [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
   321     [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
   317 #endif
   322 #endif
       
   323     [center removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
   318 }
   324 }
   319 
   325 
   320 /* reveal onscreen virtual keyboard */
   326 /* reveal onscreen virtual keyboard */
   321 - (void)showKeyboard
   327 - (void)showKeyboard
   322 {
   328 {
   350 {
   356 {
   351     if (!rotatingOrientation) {
   357     if (!rotatingOrientation) {
   352         SDL_StopTextInput();
   358         SDL_StopTextInput();
   353     }
   359     }
   354     [self setKeyboardHeight:0];
   360     [self setKeyboardHeight:0];
       
   361 }
       
   362 
       
   363 - (void)textFieldTextDidChange:(NSNotification *)notification
       
   364 {
       
   365     if (changeText!=nil && textField.markedTextRange == nil)
       
   366     {
       
   367         NSUInteger len = changeText.length;
       
   368         if (len > 0) {
       
   369             /* Go through all the characters in the string we've been sent and
       
   370              * convert them to key presses */
       
   371             int i;
       
   372             for (i = 0; i < len; i++) {
       
   373                 unichar c = [changeText characterAtIndex:i];
       
   374                 SDL_Scancode code;
       
   375                 Uint16 mod;
       
   376 
       
   377                 if (c < 127) {
       
   378                     /* Figure out the SDL_Scancode and SDL_keymod for this unichar */
       
   379                     code = unicharToUIKeyInfoTable[c].code;
       
   380                     mod  = unicharToUIKeyInfoTable[c].mod;
       
   381                 } else {
       
   382                     /* We only deal with ASCII right now */
       
   383                     code = SDL_SCANCODE_UNKNOWN;
       
   384                     mod = 0;
       
   385                 }
       
   386 
       
   387                 if (mod & KMOD_SHIFT) {
       
   388                     /* If character uses shift, press shift down */
       
   389                     SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
       
   390                 }
       
   391 
       
   392                 /* send a keydown and keyup even for the character */
       
   393                 SDL_SendKeyboardKey(SDL_PRESSED, code);
       
   394                 SDL_SendKeyboardKey(SDL_RELEASED, code);
       
   395 
       
   396                 if (mod & KMOD_SHIFT) {
       
   397                     /* If character uses shift, press shift back up */
       
   398                     SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
       
   399                 }
       
   400             }
       
   401             SDL_SendKeyboardText([changeText UTF8String]);
       
   402         }
       
   403         changeText = nil;
       
   404     }
   355 }
   405 }
   356 
   406 
   357 - (void)updateKeyboard
   407 - (void)updateKeyboard
   358 {
   408 {
   359     CGAffineTransform t = self.view.transform;
   409     CGAffineTransform t = self.view.transform;
   390 
   440 
   391 /* UITextFieldDelegate method.  Invoked when user types something. */
   441 /* UITextFieldDelegate method.  Invoked when user types something. */
   392 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
   442 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
   393 {
   443 {
   394     NSUInteger len = string.length;
   444     NSUInteger len = string.length;
   395 
       
   396     if (len == 0) {
   445     if (len == 0) {
   397         /* it wants to replace text with nothing, ie a delete */
   446         changeText = nil;
   398         SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
   447         if (textField.markedTextRange == nil) {
   399         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
   448             /* it wants to replace text with nothing, ie a delete */
       
   449             SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
       
   450             SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
       
   451         }
       
   452         if (textField.text.length < 16) {
       
   453             textField.text = obligateForBackspace;
       
   454         }
   400     } else {
   455     } else {
   401         /* go through all the characters in the string we've been sent and
   456         changeText = string;
   402          * convert them to key presses */
   457     }
   403         int i;
   458     return YES;
   404         for (i = 0; i < len; i++) {
       
   405             unichar c = [string characterAtIndex:i];
       
   406             Uint16 mod = 0;
       
   407             SDL_Scancode code;
       
   408 
       
   409             if (c < 127) {
       
   410                 /* figure out the SDL_Scancode and SDL_keymod for this unichar */
       
   411                 code = unicharToUIKeyInfoTable[c].code;
       
   412                 mod  = unicharToUIKeyInfoTable[c].mod;
       
   413             } else {
       
   414                 /* we only deal with ASCII right now */
       
   415                 code = SDL_SCANCODE_UNKNOWN;
       
   416                 mod = 0;
       
   417             }
       
   418 
       
   419             if (mod & KMOD_SHIFT) {
       
   420                 /* If character uses shift, press shift down */
       
   421                 SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
       
   422             }
       
   423 
       
   424             /* send a keydown and keyup even for the character */
       
   425             SDL_SendKeyboardKey(SDL_PRESSED, code);
       
   426             SDL_SendKeyboardKey(SDL_RELEASED, code);
       
   427 
       
   428             if (mod & KMOD_SHIFT) {
       
   429                 /* If character uses shift, press shift back up */
       
   430                 SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
       
   431             }
       
   432         }
       
   433 
       
   434         SDL_SendKeyboardText([string UTF8String]);
       
   435     }
       
   436 
       
   437     return NO; /* don't allow the edit! (keep placeholder text there) */
       
   438 }
   459 }
   439 
   460 
   440 /* Terminates the editing session */
   461 /* Terminates the editing session */
   441 - (BOOL)textFieldShouldReturn:(UITextField*)_textField
   462 - (BOOL)textFieldShouldReturn:(UITextField*)_textField
   442 {
   463 {
   496 UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
   517 UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
   497 {
   518 {
   498     @autoreleasepool {
   519     @autoreleasepool {
   499         SDL_uikitviewcontroller *vc = GetWindowViewController(window);
   520         SDL_uikitviewcontroller *vc = GetWindowViewController(window);
   500         if (vc != nil) {
   521         if (vc != nil) {
   501             return vc.isKeyboardVisible;
   522             return vc.keyboardVisible;
   502         }
   523         }
   503         return SDL_FALSE;
   524         return SDL_FALSE;
   504     }
   525     }
   505 }
   526 }
   506 
   527