src/video/uikit/SDL_uikitview.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Mar 2011 09:58:21 -0800
changeset 5445 b2bcc3c36fa3
parent 5262 b530ef003506
child 5459 881d1e03e381
permissions -rw-r--r--
Enabled multi-touch
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@5131
    80
    
slouken@5131
    81
    //NSLog("Click");
slouken@5131
    82
    
slouken@5131
    83
    if (touch) {
slouken@5131
    84
        CGPoint locationInView = [touch locationInView: self];
slouken@5131
    85
            
slouken@5131
    86
        /* send moved event */
slouken@5131
    87
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@4488
    88
slouken@5131
    89
        /* send mouse down event */
slouken@5131
    90
        SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@5131
    91
    }
jim@4660
    92
jimtla@4677
    93
#ifdef FIXED_MULTITOUCH
slouken@5131
    94
    while(touch) {
slouken@5131
    95
      CGPoint locationInView = [touch locationInView: self];
jim@4662
    96
jim@4662
    97
jim@4662
    98
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
    99
      //FIXME: TODO: Using touch as the fingerId is potentially dangerous
slouken@5131
   100
      //It is also much more efficient than storing the UITouch pointer
slouken@5131
   101
      //and comparing it to the incoming event.
slouken@5131
   102
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   103
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   104
                 1);
jim@4662
   105
#else
slouken@5131
   106
      int i;
slouken@5131
   107
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   108
        if(finger[i] == NULL) {
slouken@5131
   109
          finger[i] = touch;
slouken@5131
   110
          SDL_SendFingerDown(touchId,i,
slouken@5131
   111
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   112
                 1);
slouken@5131
   113
          break;
slouken@5131
   114
        }
slouken@5131
   115
      }
jim@4662
   116
#endif
slouken@5131
   117
      
jim@4662
   118
slouken@5131
   119
      
jim@4660
   120
slouken@5131
   121
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   122
    }
slouken@4465
   123
#endif
slouken@2765
   124
}
slouken@2765
   125
slouken@2765
   126
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   127
    
slouken@5131
   128
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   129
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5131
   130
    
slouken@5131
   131
    if (touch) {
slouken@5131
   132
        /* send mouse up */
slouken@5131
   133
        SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@5131
   134
    }
slouken@4661
   135
jimtla@4677
   136
#ifdef FIXED_MULTITOUCH
slouken@5131
   137
    while(touch) {
slouken@5131
   138
      CGPoint locationInView = [touch locationInView: self];
slouken@5131
   139
      
jim@4662
   140
jim@4662
   141
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   142
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   143
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   144
                 1);
jim@4662
   145
#else
slouken@5131
   146
      int i;
slouken@5131
   147
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   148
        if(finger[i] == touch) {
slouken@5131
   149
          SDL_SendFingerDown(touchId,i,
slouken@5131
   150
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   151
                 1);
slouken@5131
   152
          break;
slouken@5131
   153
        }
slouken@5131
   154
      }
jim@4662
   155
#endif
jim@4660
   156
slouken@5131
   157
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   158
    }
slouken@4465
   159
#endif
slouken@2765
   160
}
slouken@2765
   161
slouken@2765
   162
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   163
    /*
slouken@5131
   164
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   165
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   166
        all active touches are canceled.
slouken@5131
   167
    */
slouken@5131
   168
    [self touchesEnded: touches withEvent: event];
slouken@2765
   169
}
slouken@2765
   170
slouken@2765
   171
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   172
    
slouken@5131
   173
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   174
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5131
   175
    
slouken@5131
   176
    if (touch) {
slouken@5131
   177
        CGPoint locationInView = [touch locationInView: self];
slouken@4488
   178
slouken@5131
   179
        /* send moved event */
slouken@5131
   180
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@5131
   181
    }
jim@4660
   182
jimtla@4677
   183
#ifdef FIXED_MULTITOUCH
slouken@5131
   184
    while(touch) {
slouken@5131
   185
      CGPoint locationInView = [touch locationInView: self];
slouken@5131
   186
      
jim@4662
   187
jim@4662
   188
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   189
      SDL_SendTouchMotion(touchId,(long)touch,
slouken@5131
   190
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   191
                  1);
jim@4662
   192
#else
slouken@5131
   193
      int i;
slouken@5131
   194
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   195
        if(finger[i] == touch) {
slouken@5131
   196
          SDL_SendTouchMotion(touchId,i,
slouken@5131
   197
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   198
                  1);
slouken@5131
   199
          break;
slouken@5131
   200
        }
slouken@5131
   201
      }
jim@4662
   202
#endif
jim@4660
   203
slouken@5131
   204
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   205
    }
slouken@4465
   206
#endif
slouken@2765
   207
}
slouken@2765
   208
slouken@2765
   209
/*
slouken@5131
   210
    ---- Keyboard related functionality below this line ----
slouken@2765
   211
*/
slouken@2765
   212
#if SDL_IPHONE_KEYBOARD
slouken@2765
   213
slouken@2765
   214
/* Is the iPhone virtual keyboard visible onscreen? */
slouken@2765
   215
- (BOOL)keyboardVisible {
slouken@5131
   216
    return keyboardVisible;
slouken@2765
   217
}
slouken@2765
   218
slouken@2765
   219
/* Set ourselves up as a UITextFieldDelegate */
slouken@2765
   220
- (void)initializeKeyboard {
slouken@5131
   221
        
slouken@5131
   222
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
slouken@5131
   223
    textField.delegate = self;
slouken@5131
   224
    /* placeholder so there is something to delete! */
slouken@5131
   225
    textField.text = @" ";    
slouken@5131
   226
    
slouken@5131
   227
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   228
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   229
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   230
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   231
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   232
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   233
    textField.returnKeyType = UIReturnKeyDefault;
slouken@5131
   234
    textField.secureTextEntry = NO;    
slouken@5131
   235
    
slouken@5131
   236
    textField.hidden = YES;
slouken@5131
   237
    keyboardVisible = NO;
slouken@5131
   238
    /* add the UITextField (hidden) to our view */
slouken@5131
   239
    [self addSubview: textField];
slouken@5131
   240
    [textField release];
slouken@2765
   241
}
slouken@2765
   242
slouken@2765
   243
/* reveal onscreen virtual keyboard */
slouken@2765
   244
- (void)showKeyboard {
slouken@5131
   245
    keyboardVisible = YES;
slouken@5131
   246
    [textField becomeFirstResponder];
slouken@2765
   247
}
slouken@2765
   248
slouken@2765
   249
/* hide onscreen virtual keyboard */
slouken@2765
   250
- (void)hideKeyboard {
slouken@5131
   251
    keyboardVisible = NO;
slouken@5131
   252
    [textField resignFirstResponder];
slouken@2765
   253
}
slouken@2765
   254
slouken@2765
   255
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   256
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
slouken@5131
   257
    
slouken@5131
   258
    if ([string length] == 0) {
slouken@5131
   259
        /* it wants to replace text with nothing, ie a delete */
slouken@5131
   260
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@5131
   261
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@5131
   262
    }
slouken@5131
   263
    else {
slouken@5131
   264
        /* go through all the characters in the string we've been sent
slouken@5131
   265
           and convert them to key presses */
slouken@5131
   266
        int i;
slouken@5131
   267
        for (i=0; i<[string length]; i++) {
slouken@5131
   268
            
slouken@5131
   269
            unichar c = [string characterAtIndex: i];
slouken@5131
   270
            
slouken@5131
   271
            Uint16 mod = 0;
slouken@5218
   272
            SDL_Scancode code;
slouken@5131
   273
            
slouken@5131
   274
            if (c < 127) {
slouken@5218
   275
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   276
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   277
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   278
            }
slouken@5131
   279
            else {
slouken@5131
   280
                /* we only deal with ASCII right now */
slouken@5131
   281
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   282
                mod = 0;
slouken@5131
   283
            }
slouken@5131
   284
            
slouken@5131
   285
            if (mod & KMOD_SHIFT) {
slouken@5131
   286
                /* If character uses shift, press shift down */
slouken@5131
   287
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   288
            }
slouken@5131
   289
            /* send a keydown and keyup even for the character */
slouken@5131
   290
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   291
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   292
            if (mod & KMOD_SHIFT) {
slouken@5131
   293
                /* If character uses shift, press shift back up */
slouken@5131
   294
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slouken@5131
   295
            }            
slouken@5131
   296
        }
slouken@5131
   297
    }
slouken@5131
   298
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   299
}
slouken@2765
   300
slouken@2765
   301
/* Terminates the editing session */
slouken@2765
   302
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@5134
   303
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@5131
   304
    [self hideKeyboard];
slouken@5131
   305
    return YES;
slouken@2765
   306
}
slouken@2765
   307
slouken@2765
   308
#endif
slouken@2765
   309
slouken@2765
   310
@end
slouken@2765
   311
slouken@2765
   312
/* iPhone keyboard addition functions */
slouken@2765
   313
#if SDL_IPHONE_KEYBOARD
slouken@2765
   314
slouken@3685
   315
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@5131
   316
    
slouken@5131
   317
    SDL_WindowData *data;
slouken@5131
   318
    SDL_uikitview *view;
slouken@5131
   319
    
slouken@5131
   320
    if (NULL == window) {
slouken@5131
   321
        SDL_SetError("Window does not exist");
slouken@5131
   322
        return -1;
slouken@5131
   323
    }
slouken@5131
   324
    
slouken@5131
   325
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   326
    view = data->view;
slouken@5131
   327
    
slouken@5131
   328
    if (nil == view) {
slouken@5131
   329
        SDL_SetError("Window has no view");
slouken@5131
   330
        return -1;
slouken@5131
   331
    }
slouken@5131
   332
    else {
slouken@5131
   333
        [view showKeyboard];
slouken@5131
   334
        return 0;
slouken@5131
   335
    }
slouken@2765
   336
}
slouken@2765
   337
slouken@3685
   338
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@5131
   339
    
slouken@5131
   340
    SDL_WindowData *data;
slouken@5131
   341
    SDL_uikitview *view;
slouken@5131
   342
    
slouken@5131
   343
    if (NULL == window) {
slouken@5131
   344
        SDL_SetError("Window does not exist");
slouken@5131
   345
        return -1;
slouken@5131
   346
    }    
slouken@5131
   347
    
slouken@5131
   348
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   349
    view = data->view;
slouken@5131
   350
    
slouken@5131
   351
    if (NULL == view) {
slouken@5131
   352
        SDL_SetError("Window has no view");
slouken@5131
   353
        return -1;
slouken@5131
   354
    }
slouken@5131
   355
    else {
slouken@5131
   356
        [view hideKeyboard];
slouken@5131
   357
        return 0;
slouken@5131
   358
    }
slouken@2765
   359
}
slouken@2765
   360
slouken@3685
   361
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@5131
   362
    
slouken@5131
   363
    SDL_WindowData *data;
slouken@5131
   364
    SDL_uikitview *view;
slouken@5131
   365
    
slouken@5131
   366
    if (NULL == window) {
slouken@5131
   367
        SDL_SetError("Window does not exist");
slouken@5131
   368
        return -1;
slouken@5131
   369
    }    
slouken@5131
   370
    
slouken@5131
   371
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   372
    view = data->view;
slouken@5131
   373
    
slouken@5131
   374
    if (NULL == view) {
slouken@5131
   375
        SDL_SetError("Window has no view");
slouken@5131
   376
        return 0;
slouken@5131
   377
    }
slouken@5131
   378
    else {
slouken@5131
   379
        return view.keyboardVisible;
slouken@5131
   380
    }
slouken@2765
   381
}
slouken@2765
   382
slouken@3685
   383
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@5131
   384
    
slouken@5131
   385
    SDL_WindowData *data;
slouken@5131
   386
    SDL_uikitview *view;
slouken@5131
   387
    
slouken@5131
   388
    if (NULL == window) {
slouken@5131
   389
        SDL_SetError("Window does not exist");
slouken@5131
   390
        return -1;
slouken@5131
   391
    }    
slouken@5131
   392
    
slouken@5131
   393
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   394
    view = data->view;
slouken@5131
   395
    
slouken@5131
   396
    if (NULL == view) {
slouken@5131
   397
        SDL_SetError("Window has no view");
slouken@5131
   398
        return -1;
slouken@5131
   399
    }
slouken@5131
   400
    else {
slouken@5131
   401
        if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@5131
   402
            SDL_iPhoneKeyboardHide(window);
slouken@5131
   403
        }
slouken@5131
   404
        else {
slouken@5131
   405
            SDL_iPhoneKeyboardShow(window);
slouken@5131
   406
        }
slouken@5131
   407
        return 0;
slouken@5131
   408
    }
slouken@2765
   409
}
slouken@2765
   410
slouken@2765
   411
#else
slouken@2765
   412
slouken@2765
   413
/* stubs, used if compiled without keyboard support */
slouken@2765
   414
slouken@3685
   415
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@5131
   416
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   417
    return -1;
slouken@2765
   418
}
slouken@2765
   419
slouken@3685
   420
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@5131
   421
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   422
    return -1;
slouken@2765
   423
}
slouken@2765
   424
slouken@3685
   425
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@5131
   426
    return 0;
slouken@2765
   427
}
slouken@2765
   428
slouken@3685
   429
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@5131
   430
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   431
    return -1;
slouken@2765
   432
}
slouken@2765
   433
slouken@5132
   434
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   435
slouken@5132
   436
/* vi: set ts=4 sw=4 expandtab: */