src/video/uikit/SDL_uikitview.m
changeset 7962 e4d2f3375868
parent 7774 c5d6c8c001be
child 8093 b43765095a6f
child 8543 b9dd3cf38585
     1.1 --- a/src/video/uikit/SDL_uikitview.m	Mon Nov 11 20:42:59 2013 -0800
     1.2 +++ b/src/video/uikit/SDL_uikitview.m	Mon Nov 11 20:51:19 2013 -0800
     1.3 @@ -35,6 +35,8 @@
     1.4  #include "SDL_uikitmodes.h"
     1.5  #include "SDL_uikitwindow.h"
     1.6  
     1.7 +void _uikit_keyboard_init() ;
     1.8 +
     1.9  @implementation SDL_uikitview
    1.10  
    1.11  - (void)dealloc
    1.12 @@ -197,6 +199,9 @@
    1.13  */
    1.14  #if SDL_IPHONE_KEYBOARD
    1.15  
    1.16 +@synthesize textInputRect = textInputRect;
    1.17 +@synthesize keyboardHeight = keyboardHeight;
    1.18 +
    1.19  /* Is the iPhone virtual keyboard visible onscreen? */
    1.20  - (BOOL)keyboardVisible
    1.21  {
    1.22 @@ -225,6 +230,8 @@
    1.23      /* add the UITextField (hidden) to our view */
    1.24      [self addSubview: textField];
    1.25      [textField release];
    1.26 +    
    1.27 +    _uikit_keyboard_init();
    1.28  }
    1.29  
    1.30  /* reveal onscreen virtual keyboard */
    1.31 @@ -352,6 +359,103 @@
    1.32      return view.keyboardVisible;
    1.33  }
    1.34  
    1.35 +
    1.36 +void _uikit_keyboard_update() {
    1.37 +    SDL_Window *window = SDL_GetFocusWindow();
    1.38 +    if (!window) { return; }
    1.39 +    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
    1.40 +    if (!data) { return; }
    1.41 +    SDL_uikitview *view = data->view;
    1.42 +    if (!view) { return; }
    1.43 +    
    1.44 +    SDL_Rect r = view.textInputRect;
    1.45 +    int height = view.keyboardHeight;
    1.46 +    int offsetx = 0;
    1.47 +    int offsety = 0;
    1.48 +    if (height) {
    1.49 +        int sw,sh;
    1.50 +        SDL_GetWindowSize(window,&sw,&sh);
    1.51 +        int bottom = (r.y + r.h);
    1.52 +        int kbottom = sh - height;
    1.53 +        if (kbottom < bottom) {
    1.54 +            offsety = kbottom-bottom;
    1.55 +        }
    1.56 +    }
    1.57 +    UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
    1.58 +    if (ui_orient == UIInterfaceOrientationLandscapeLeft) {
    1.59 +        int tmp = offsetx; offsetx = offsety; offsety = tmp;
    1.60 +    }
    1.61 +    if (ui_orient == UIInterfaceOrientationLandscapeRight) {
    1.62 +        offsety = -offsety;
    1.63 +        int tmp = offsetx; offsetx = offsety; offsety = tmp;
    1.64 +    }
    1.65 +    if (ui_orient == UIInterfaceOrientationPortraitUpsideDown) {
    1.66 +        offsety = -offsety;
    1.67 +    }
    1.68 +    if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
    1.69 +        float scale = [UIScreen mainScreen].scale;
    1.70 +        offsetx /= scale;
    1.71 +        offsety /= scale;
    1.72 +    }
    1.73 +    view.frame = CGRectMake(offsetx,offsety,view.frame.size.width,view.frame.size.height);
    1.74 +}
    1.75 +
    1.76 +void _uikit_keyboard_set_height(int height) {
    1.77 +    SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
    1.78 +    if (view == nil) {
    1.79 +        return ;
    1.80 +    }
    1.81 +    
    1.82 +    view.keyboardHeight = height;
    1.83 +    _uikit_keyboard_update();
    1.84 +}
    1.85 +
    1.86 +void _uikit_keyboard_init() {
    1.87 +    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    1.88 +    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    1.89 +    [center addObserverForName:UIKeyboardWillShowNotification
    1.90 +                        object:nil
    1.91 +                         queue:queue
    1.92 +                    usingBlock:^(NSNotification *notification) {
    1.93 +                        int height = 0;
    1.94 +                        CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    1.95 +                        height = keyboardSize.height;
    1.96 +                        UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
    1.97 +                        if (ui_orient == UIInterfaceOrientationLandscapeRight || ui_orient == UIInterfaceOrientationLandscapeLeft) {
    1.98 +                            height = keyboardSize.width;
    1.99 +                        }
   1.100 +                        if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
   1.101 +                            height *= [UIScreen mainScreen].scale;
   1.102 +                        }
   1.103 +                        _uikit_keyboard_set_height(height);
   1.104 +                    }
   1.105 +     ];
   1.106 +    [center addObserverForName:UIKeyboardDidHideNotification
   1.107 +                        object:nil
   1.108 +                         queue:queue
   1.109 +                    usingBlock:^(NSNotification *notification) {
   1.110 +                        _uikit_keyboard_set_height(0);
   1.111 +                    }
   1.112 +     ];
   1.113 +}
   1.114 +
   1.115 +void
   1.116 +UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
   1.117 +{
   1.118 +    if (!rect) {
   1.119 +        SDL_InvalidParamError("rect");
   1.120 +        return;
   1.121 +    }
   1.122 +    
   1.123 +    SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
   1.124 +    if (view == nil) {
   1.125 +        return ;
   1.126 +    }
   1.127 +
   1.128 +    view.textInputRect = *rect;
   1.129 +}
   1.130 +
   1.131 +
   1.132  #endif /* SDL_IPHONE_KEYBOARD */
   1.133  
   1.134  #endif /* SDL_VIDEO_DRIVER_UIKIT */