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