src/video/uikit/SDL_uikitview.m
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Mar 2011 17:38:12 -0800
changeset 5461 94f742ce580a
parent 5459 881d1e03e381
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
slouken@2765
     1
/*
slouken@5262
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@5262
     4
slouken@5262
     5
    This library is free software; you can redistribute it and/or
slouken@5262
     6
    modify it under the terms of the GNU Lesser General Public
slouken@5262
     7
    License as published by the Free Software Foundation; either
slouken@5262
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@5262
     9
slouken@5262
    10
    This library is distributed in the hope that it will be useful,
slouken@5262
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@5262
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@5262
    13
    Lesser General Public License for more details.
slouken@5262
    14
slouken@5262
    15
    You should have received a copy of the GNU Lesser General Public
slouken@5262
    16
    License along with this library; if not, write to the Free Software
slouken@5262
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@5262
    18
slouken@5262
    19
    Sam Lantinga
slouken@5262
    20
    slouken@libsdl.org
slouken@5262
    21
*/
slouken@2765
    22
slouken@2765
    23
#import "SDL_uikitview.h"
slouken@2765
    24
slouken@4490
    25
#include "../../events/SDL_keyboard_c.h"
slouken@4490
    26
#include "../../events/SDL_mouse_c.h"
jimtla@4677
    27
#include "../../events/SDL_touch_c.h"
slouken@4490
    28
slouken@2765
    29
#if SDL_IPHONE_KEYBOARD
slouken@2765
    30
#import "keyinfotable.h"
slouken@2765
    31
#import "SDL_uikitappdelegate.h"
slouken@2765
    32
#import "SDL_uikitwindow.h"
slouken@2765
    33
#endif
slouken@2765
    34
slouken@2765
    35
@implementation SDL_uikitview
slouken@2765
    36
slouken@2765
    37
- (void)dealloc {
slouken@5131
    38
    [super dealloc];
slouken@2765
    39
}
slouken@2765
    40
slouken@2765
    41
- (id)initWithFrame:(CGRect)frame {
slouken@2765
    42
slouken@5131
    43
    self = [super initWithFrame: frame];
slouken@5131
    44
    
slouken@2765
    45
#if SDL_IPHONE_KEYBOARD
slouken@5131
    46
    [self initializeKeyboard];
slouken@5131
    47
#endif    
slouken@2765
    48
jimtla@4677
    49
#ifdef FIXED_MULTITOUCH
slouken@5445
    50
    self.multipleTouchEnabled = YES;
slouken@5445
    51
slouken@5131
    52
    SDL_Touch touch;
slouken@5131
    53
    touch.id = 0; //TODO: Should be -1?
jim@4660
    54
slouken@5131
    55
    //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
slouken@5131
    56
    //EventTouchData* data = (EventTouchData*)(touch.driverdata);
slouken@5131
    57
    
slouken@5131
    58
    touch.x_min = 0;
slouken@5131
    59
    touch.x_max = frame.size.width;
slouken@5131
    60
    touch.native_xres = touch.x_max - touch.x_min;
slouken@5131
    61
    touch.y_min = 0;
slouken@5131
    62
    touch.y_max = frame.size.height;
slouken@5131
    63
    touch.native_yres = touch.y_max - touch.y_min;
slouken@5131
    64
    touch.pressure_min = 0;
slouken@5131
    65
    touch.pressure_max = 1;
slouken@5131
    66
    touch.native_pressureres = touch.pressure_max - touch.pressure_min;
jim@4660
    67
jim@4660
    68
slouken@5131
    69
    touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
slouken@4465
    70
#endif
slouken@4661
    71
slouken@5131
    72
    return self;
slouken@2765
    73
slouken@2765
    74
}
slouken@2765
    75
slouken@2765
    76
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
    77
slouken@5131
    78
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
    79
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5459
    80
slouken@5131
    81
    if (touch) {
slouken@5131
    82
        CGPoint locationInView = [touch locationInView: self];
slouken@5131
    83
            
slouken@5131
    84
        /* send moved event */
slouken@5131
    85
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@4488
    86
slouken@5131
    87
        /* send mouse down event */
slouken@5131
    88
        SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@5131
    89
    }
jim@4660
    90
jimtla@4677
    91
#ifdef FIXED_MULTITOUCH
slouken@5131
    92
    while(touch) {
slouken@5131
    93
      CGPoint locationInView = [touch locationInView: self];
jim@4662
    94
jim@4662
    95
jim@4662
    96
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
    97
      //FIXME: TODO: Using touch as the fingerId is potentially dangerous
slouken@5131
    98
      //It is also much more efficient than storing the UITouch pointer
slouken@5131
    99
      //and comparing it to the incoming event.
slouken@5131
   100
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   101
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   102
                 1);
jim@4662
   103
#else
slouken@5131
   104
      int i;
slouken@5131
   105
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   106
        if(finger[i] == NULL) {
slouken@5131
   107
          finger[i] = touch;
slouken@5131
   108
          SDL_SendFingerDown(touchId,i,
slouken@5131
   109
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   110
                 1);
slouken@5131
   111
          break;
slouken@5131
   112
        }
slouken@5131
   113
      }
jim@4662
   114
#endif
jim@4660
   115
slouken@5131
   116
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   117
    }
slouken@4465
   118
#endif
slouken@2765
   119
}
slouken@2765
   120
slouken@2765
   121
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   122
    
slouken@5131
   123
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   124
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5131
   125
    
slouken@5131
   126
    if (touch) {
slouken@5131
   127
        /* send mouse up */
slouken@5131
   128
        SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@5131
   129
    }
slouken@4661
   130
jimtla@4677
   131
#ifdef FIXED_MULTITOUCH
slouken@5131
   132
    while(touch) {
slouken@5131
   133
      CGPoint locationInView = [touch locationInView: self];
slouken@5131
   134
      
jim@4662
   135
jim@4662
   136
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   137
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   138
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   139
                 1);
jim@4662
   140
#else
slouken@5131
   141
      int i;
slouken@5131
   142
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   143
        if(finger[i] == touch) {
slouken@5131
   144
          SDL_SendFingerDown(touchId,i,
slouken@5131
   145
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   146
                 1);
slouken@5459
   147
          finger[i] = NULL;
slouken@5131
   148
          break;
slouken@5131
   149
        }
slouken@5131
   150
      }
jim@4662
   151
#endif
jim@4660
   152
slouken@5131
   153
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   154
    }
slouken@4465
   155
#endif
slouken@2765
   156
}
slouken@2765
   157
slouken@2765
   158
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   159
    /*
slouken@5131
   160
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   161
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   162
        all active touches are canceled.
slouken@5131
   163
    */
slouken@5131
   164
    [self touchesEnded: touches withEvent: event];
slouken@2765
   165
}
slouken@2765
   166
slouken@2765
   167
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   168
    
slouken@5131
   169
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   170
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5131
   171
    
slouken@5131
   172
    if (touch) {
slouken@5131
   173
        CGPoint locationInView = [touch locationInView: self];
slouken@4488
   174
slouken@5131
   175
        /* send moved event */
slouken@5131
   176
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@5131
   177
    }
jim@4660
   178
jimtla@4677
   179
#ifdef FIXED_MULTITOUCH
slouken@5131
   180
    while(touch) {
slouken@5131
   181
      CGPoint locationInView = [touch locationInView: self];
slouken@5131
   182
      
jim@4662
   183
jim@4662
   184
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   185
      SDL_SendTouchMotion(touchId,(long)touch,
slouken@5131
   186
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   187
                  1);
jim@4662
   188
#else
slouken@5131
   189
      int i;
slouken@5131
   190
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   191
        if(finger[i] == touch) {
slouken@5131
   192
          SDL_SendTouchMotion(touchId,i,
slouken@5131
   193
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   194
                  1);
slouken@5131
   195
          break;
slouken@5131
   196
        }
slouken@5131
   197
      }
jim@4662
   198
#endif
jim@4660
   199
slouken@5131
   200
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   201
    }
slouken@4465
   202
#endif
slouken@2765
   203
}
slouken@2765
   204
slouken@2765
   205
/*
slouken@5131
   206
    ---- Keyboard related functionality below this line ----
slouken@2765
   207
*/
slouken@2765
   208
#if SDL_IPHONE_KEYBOARD
slouken@2765
   209
slouken@2765
   210
/* Is the iPhone virtual keyboard visible onscreen? */
slouken@2765
   211
- (BOOL)keyboardVisible {
slouken@5131
   212
    return keyboardVisible;
slouken@2765
   213
}
slouken@2765
   214
slouken@2765
   215
/* Set ourselves up as a UITextFieldDelegate */
slouken@2765
   216
- (void)initializeKeyboard {
slouken@5131
   217
        
slouken@5131
   218
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
slouken@5131
   219
    textField.delegate = self;
slouken@5131
   220
    /* placeholder so there is something to delete! */
slouken@5131
   221
    textField.text = @" ";    
slouken@5131
   222
    
slouken@5131
   223
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   224
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   225
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   226
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   227
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   228
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   229
    textField.returnKeyType = UIReturnKeyDefault;
slouken@5131
   230
    textField.secureTextEntry = NO;    
slouken@5131
   231
    
slouken@5131
   232
    textField.hidden = YES;
slouken@5131
   233
    keyboardVisible = NO;
slouken@5131
   234
    /* add the UITextField (hidden) to our view */
slouken@5131
   235
    [self addSubview: textField];
slouken@5131
   236
    [textField release];
slouken@2765
   237
}
slouken@2765
   238
slouken@2765
   239
/* reveal onscreen virtual keyboard */
slouken@2765
   240
- (void)showKeyboard {
slouken@5131
   241
    keyboardVisible = YES;
slouken@5131
   242
    [textField becomeFirstResponder];
slouken@2765
   243
}
slouken@2765
   244
slouken@2765
   245
/* hide onscreen virtual keyboard */
slouken@2765
   246
- (void)hideKeyboard {
slouken@5131
   247
    keyboardVisible = NO;
slouken@5131
   248
    [textField resignFirstResponder];
slouken@2765
   249
}
slouken@2765
   250
slouken@2765
   251
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   252
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
slouken@5131
   253
    
slouken@5131
   254
    if ([string length] == 0) {
slouken@5131
   255
        /* it wants to replace text with nothing, ie a delete */
slouken@5131
   256
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@5131
   257
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@5131
   258
    }
slouken@5131
   259
    else {
slouken@5131
   260
        /* go through all the characters in the string we've been sent
slouken@5131
   261
           and convert them to key presses */
slouken@5131
   262
        int i;
slouken@5131
   263
        for (i=0; i<[string length]; i++) {
slouken@5131
   264
            
slouken@5131
   265
            unichar c = [string characterAtIndex: i];
slouken@5131
   266
            
slouken@5131
   267
            Uint16 mod = 0;
slouken@5218
   268
            SDL_Scancode code;
slouken@5131
   269
            
slouken@5131
   270
            if (c < 127) {
slouken@5218
   271
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   272
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   273
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   274
            }
slouken@5131
   275
            else {
slouken@5131
   276
                /* we only deal with ASCII right now */
slouken@5131
   277
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   278
                mod = 0;
slouken@5131
   279
            }
slouken@5131
   280
            
slouken@5131
   281
            if (mod & KMOD_SHIFT) {
slouken@5131
   282
                /* If character uses shift, press shift down */
slouken@5131
   283
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   284
            }
slouken@5131
   285
            /* send a keydown and keyup even for the character */
slouken@5131
   286
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   287
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   288
            if (mod & KMOD_SHIFT) {
slouken@5131
   289
                /* If character uses shift, press shift back up */
slouken@5131
   290
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slouken@5131
   291
            }            
slouken@5131
   292
        }
slouken@5461
   293
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   294
    }
slouken@5131
   295
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   296
}
slouken@2765
   297
slouken@2765
   298
/* Terminates the editing session */
slouken@2765
   299
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@5134
   300
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@5131
   301
    [self hideKeyboard];
slouken@5131
   302
    return YES;
slouken@2765
   303
}
slouken@2765
   304
slouken@2765
   305
#endif
slouken@2765
   306
slouken@2765
   307
@end
slouken@2765
   308
slouken@2765
   309
/* iPhone keyboard addition functions */
slouken@2765
   310
#if SDL_IPHONE_KEYBOARD
slouken@2765
   311
slouken@3685
   312
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@5131
   313
    
slouken@5131
   314
    SDL_WindowData *data;
slouken@5131
   315
    SDL_uikitview *view;
slouken@5131
   316
    
slouken@5131
   317
    if (NULL == window) {
slouken@5131
   318
        SDL_SetError("Window does not exist");
slouken@5131
   319
        return -1;
slouken@5131
   320
    }
slouken@5131
   321
    
slouken@5131
   322
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   323
    view = data->view;
slouken@5131
   324
    
slouken@5131
   325
    if (nil == view) {
slouken@5131
   326
        SDL_SetError("Window has no view");
slouken@5131
   327
        return -1;
slouken@5131
   328
    }
slouken@5131
   329
    else {
slouken@5131
   330
        [view showKeyboard];
slouken@5131
   331
        return 0;
slouken@5131
   332
    }
slouken@2765
   333
}
slouken@2765
   334
slouken@3685
   335
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@5131
   336
    
slouken@5131
   337
    SDL_WindowData *data;
slouken@5131
   338
    SDL_uikitview *view;
slouken@5131
   339
    
slouken@5131
   340
    if (NULL == window) {
slouken@5131
   341
        SDL_SetError("Window does not exist");
slouken@5131
   342
        return -1;
slouken@5131
   343
    }    
slouken@5131
   344
    
slouken@5131
   345
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   346
    view = data->view;
slouken@5131
   347
    
slouken@5131
   348
    if (NULL == view) {
slouken@5131
   349
        SDL_SetError("Window has no view");
slouken@5131
   350
        return -1;
slouken@5131
   351
    }
slouken@5131
   352
    else {
slouken@5131
   353
        [view hideKeyboard];
slouken@5131
   354
        return 0;
slouken@5131
   355
    }
slouken@2765
   356
}
slouken@2765
   357
slouken@3685
   358
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@5131
   359
    
slouken@5131
   360
    SDL_WindowData *data;
slouken@5131
   361
    SDL_uikitview *view;
slouken@5131
   362
    
slouken@5131
   363
    if (NULL == window) {
slouken@5131
   364
        SDL_SetError("Window does not exist");
slouken@5131
   365
        return -1;
slouken@5131
   366
    }    
slouken@5131
   367
    
slouken@5131
   368
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   369
    view = data->view;
slouken@5131
   370
    
slouken@5131
   371
    if (NULL == view) {
slouken@5131
   372
        SDL_SetError("Window has no view");
slouken@5131
   373
        return 0;
slouken@5131
   374
    }
slouken@5131
   375
    else {
slouken@5131
   376
        return view.keyboardVisible;
slouken@5131
   377
    }
slouken@2765
   378
}
slouken@2765
   379
slouken@3685
   380
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@5131
   381
    
slouken@5131
   382
    SDL_WindowData *data;
slouken@5131
   383
    SDL_uikitview *view;
slouken@5131
   384
    
slouken@5131
   385
    if (NULL == window) {
slouken@5131
   386
        SDL_SetError("Window does not exist");
slouken@5131
   387
        return -1;
slouken@5131
   388
    }    
slouken@5131
   389
    
slouken@5131
   390
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   391
    view = data->view;
slouken@5131
   392
    
slouken@5131
   393
    if (NULL == view) {
slouken@5131
   394
        SDL_SetError("Window has no view");
slouken@5131
   395
        return -1;
slouken@5131
   396
    }
slouken@5131
   397
    else {
slouken@5131
   398
        if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@5131
   399
            SDL_iPhoneKeyboardHide(window);
slouken@5131
   400
        }
slouken@5131
   401
        else {
slouken@5131
   402
            SDL_iPhoneKeyboardShow(window);
slouken@5131
   403
        }
slouken@5131
   404
        return 0;
slouken@5131
   405
    }
slouken@2765
   406
}
slouken@2765
   407
slouken@2765
   408
#else
slouken@2765
   409
slouken@2765
   410
/* stubs, used if compiled without keyboard support */
slouken@2765
   411
slouken@3685
   412
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@5131
   413
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   414
    return -1;
slouken@2765
   415
}
slouken@2765
   416
slouken@3685
   417
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@5131
   418
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   419
    return -1;
slouken@2765
   420
}
slouken@2765
   421
slouken@3685
   422
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@5131
   423
    return 0;
slouken@2765
   424
}
slouken@2765
   425
slouken@3685
   426
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@5131
   427
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   428
    return -1;
slouken@2765
   429
}
slouken@2765
   430
slouken@5132
   431
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   432
slouken@5132
   433
/* vi: set ts=4 sw=4 expandtab: */