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