src/video/uikit/SDL_uikitview.m
changeset 7962 e4d2f3375868
parent 7774 c5d6c8c001be
child 8093 b43765095a6f
child 8543 b9dd3cf38585
equal deleted inserted replaced
7961:c629882e0a81 7962:e4d2f3375868
    32 #include "keyinfotable.h"
    32 #include "keyinfotable.h"
    33 #endif
    33 #endif
    34 #include "SDL_uikitappdelegate.h"
    34 #include "SDL_uikitappdelegate.h"
    35 #include "SDL_uikitmodes.h"
    35 #include "SDL_uikitmodes.h"
    36 #include "SDL_uikitwindow.h"
    36 #include "SDL_uikitwindow.h"
       
    37 
       
    38 void _uikit_keyboard_init() ;
    37 
    39 
    38 @implementation SDL_uikitview
    40 @implementation SDL_uikitview
    39 
    41 
    40 - (void)dealloc
    42 - (void)dealloc
    41 {
    43 {
   195 /*
   197 /*
   196     ---- Keyboard related functionality below this line ----
   198     ---- Keyboard related functionality below this line ----
   197 */
   199 */
   198 #if SDL_IPHONE_KEYBOARD
   200 #if SDL_IPHONE_KEYBOARD
   199 
   201 
       
   202 @synthesize textInputRect = textInputRect;
       
   203 @synthesize keyboardHeight = keyboardHeight;
       
   204 
   200 /* Is the iPhone virtual keyboard visible onscreen? */
   205 /* Is the iPhone virtual keyboard visible onscreen? */
   201 - (BOOL)keyboardVisible
   206 - (BOOL)keyboardVisible
   202 {
   207 {
   203     return keyboardVisible;
   208     return keyboardVisible;
   204 }
   209 }
   223     textField.hidden = YES;
   228     textField.hidden = YES;
   224     keyboardVisible = NO;
   229     keyboardVisible = NO;
   225     /* add the UITextField (hidden) to our view */
   230     /* add the UITextField (hidden) to our view */
   226     [self addSubview: textField];
   231     [self addSubview: textField];
   227     [textField release];
   232     [textField release];
       
   233     
       
   234     _uikit_keyboard_init();
   228 }
   235 }
   229 
   236 
   230 /* reveal onscreen virtual keyboard */
   237 /* reveal onscreen virtual keyboard */
   231 - (void)showKeyboard
   238 - (void)showKeyboard
   232 {
   239 {
   350     }
   357     }
   351 
   358 
   352     return view.keyboardVisible;
   359     return view.keyboardVisible;
   353 }
   360 }
   354 
   361 
       
   362 
       
   363 void _uikit_keyboard_update() {
       
   364     SDL_Window *window = SDL_GetFocusWindow();
       
   365     if (!window) { return; }
       
   366     SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
       
   367     if (!data) { return; }
       
   368     SDL_uikitview *view = data->view;
       
   369     if (!view) { return; }
       
   370     
       
   371     SDL_Rect r = view.textInputRect;
       
   372     int height = view.keyboardHeight;
       
   373     int offsetx = 0;
       
   374     int offsety = 0;
       
   375     if (height) {
       
   376         int sw,sh;
       
   377         SDL_GetWindowSize(window,&sw,&sh);
       
   378         int bottom = (r.y + r.h);
       
   379         int kbottom = sh - height;
       
   380         if (kbottom < bottom) {
       
   381             offsety = kbottom-bottom;
       
   382         }
       
   383     }
       
   384     UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
       
   385     if (ui_orient == UIInterfaceOrientationLandscapeLeft) {
       
   386         int tmp = offsetx; offsetx = offsety; offsety = tmp;
       
   387     }
       
   388     if (ui_orient == UIInterfaceOrientationLandscapeRight) {
       
   389         offsety = -offsety;
       
   390         int tmp = offsetx; offsetx = offsety; offsety = tmp;
       
   391     }
       
   392     if (ui_orient == UIInterfaceOrientationPortraitUpsideDown) {
       
   393         offsety = -offsety;
       
   394     }
       
   395     if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
       
   396         float scale = [UIScreen mainScreen].scale;
       
   397         offsetx /= scale;
       
   398         offsety /= scale;
       
   399     }
       
   400     view.frame = CGRectMake(offsetx,offsety,view.frame.size.width,view.frame.size.height);
       
   401 }
       
   402 
       
   403 void _uikit_keyboard_set_height(int height) {
       
   404     SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
       
   405     if (view == nil) {
       
   406         return ;
       
   407     }
       
   408     
       
   409     view.keyboardHeight = height;
       
   410     _uikit_keyboard_update();
       
   411 }
       
   412 
       
   413 void _uikit_keyboard_init() {
       
   414     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
       
   415     NSOperationQueue *queue = [NSOperationQueue mainQueue];
       
   416     [center addObserverForName:UIKeyboardWillShowNotification
       
   417                         object:nil
       
   418                          queue:queue
       
   419                     usingBlock:^(NSNotification *notification) {
       
   420                         int height = 0;
       
   421                         CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
       
   422                         height = keyboardSize.height;
       
   423                         UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
       
   424                         if (ui_orient == UIInterfaceOrientationLandscapeRight || ui_orient == UIInterfaceOrientationLandscapeLeft) {
       
   425                             height = keyboardSize.width;
       
   426                         }
       
   427                         if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)]) {
       
   428                             height *= [UIScreen mainScreen].scale;
       
   429                         }
       
   430                         _uikit_keyboard_set_height(height);
       
   431                     }
       
   432      ];
       
   433     [center addObserverForName:UIKeyboardDidHideNotification
       
   434                         object:nil
       
   435                          queue:queue
       
   436                     usingBlock:^(NSNotification *notification) {
       
   437                         _uikit_keyboard_set_height(0);
       
   438                     }
       
   439      ];
       
   440 }
       
   441 
       
   442 void
       
   443 UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
       
   444 {
       
   445     if (!rect) {
       
   446         SDL_InvalidParamError("rect");
       
   447         return;
       
   448     }
       
   449     
       
   450     SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
       
   451     if (view == nil) {
       
   452         return ;
       
   453     }
       
   454 
       
   455     view.textInputRect = *rect;
       
   456 }
       
   457 
       
   458 
   355 #endif /* SDL_IPHONE_KEYBOARD */
   459 #endif /* SDL_IPHONE_KEYBOARD */
   356 
   460 
   357 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   461 #endif /* SDL_VIDEO_DRIVER_UIKIT */
   358 
   462 
   359 /* vi: set ts=4 sw=4 expandtab: */
   463 /* vi: set ts=4 sw=4 expandtab: */