src/video/uikit/SDL_uikitview.m
author Edward Rudd <urkle@outoforder.cc>
Thu, 25 Apr 2013 18:05:08 -0400
changeset 7111 f4ae9f1233df
parent 7050 aa1f44908402
child 7191 75360622e65f
permissions -rw-r--r--
Allow disabling SDL_IPHONE_KEYBOARD in iOS builds
slouken@6079
     1
 /*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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"
urkle@7111
    33
#endif
slouken@6518
    34
#include "SDL_uikitappdelegate.h"
slouken@6518
    35
#include "SDL_uikitmodes.h"
slouken@6518
    36
#include "SDL_uikitwindow.h"
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
slouken@5445
    53
    self.multipleTouchEnabled = YES;
slouken@5445
    54
slouken@6951
    55
    touchId = 1;
slouken@6951
    56
    SDL_AddTouch(touchId, "");
slouken@4661
    57
slouken@5131
    58
    return self;
slouken@2765
    59
slouken@2765
    60
}
slouken@2765
    61
slouken@6436
    62
- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
slouken@6079
    63
{
slouken@6079
    64
    CGPoint point = [touch locationInView: self];
slouken@6436
    65
slouken@6436
    66
    // Get the display scale and apply that to the input coordinates
slouken@6436
    67
    SDL_Window *window = self->viewcontroller.window;
slouken@6436
    68
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6436
    69
    SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
slouken@6436
    70
    
slouken@6436
    71
    if (normalize) {
slouken@6439
    72
        CGRect bounds = [self bounds];
slouken@6439
    73
        point.x /= bounds.size.width;
slouken@6439
    74
        point.y /= bounds.size.height;
slouken@6519
    75
    } else {
slouken@6519
    76
        point.x *= displaymodedata->scale;
slouken@6519
    77
        point.y *= displaymodedata->scale;
slouken@6436
    78
    }
slouken@6079
    79
    return point;
slouken@6079
    80
}
slouken@6079
    81
kees@6003
    82
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
    83
{
slouken@5131
    84
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
    85
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5459
    86
slouken@6597
    87
    while (touch) {
slouken@6597
    88
        if (!leftFingerDown) {
slouken@6597
    89
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
kees@6001
    90
slouken@6597
    91
            /* send moved event */
slouken@6950
    92
            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
slouken@4488
    93
slouken@6597
    94
            /* send mouse down event */
slouken@6950
    95
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
jim@4660
    96
icculus@7050
    97
            leftFingerDown = touch;
slouken@6597
    98
        }
slouken@6597
    99
slouken@6436
   100
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
jim@4662
   101
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@6597
   102
        // FIXME: TODO: Using touch as the fingerId is potentially dangerous
slouken@6597
   103
        // It is also much more efficient than storing the UITouch pointer
slouken@6597
   104
        // and comparing it to the incoming event.
icculus@7050
   105
        SDL_SendTouch(touchId, (SDL_FingerID)((size_t)touch),
slouken@6951
   106
                      SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
jim@4662
   107
#else
kees@6003
   108
        int i;
kees@6003
   109
        for(i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   110
            if (finger[i] == NULL) {
kees@6003
   111
                finger[i] = touch;
slouken@6951
   112
                SDL_SendTouch(touchId, i,
slouken@6951
   113
                              SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
kees@6003
   114
                break;
kees@6003
   115
            }
slouken@5131
   116
        }
jim@4662
   117
#endif
kees@6003
   118
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   119
    }
slouken@2765
   120
}
slouken@2765
   121
kees@6003
   122
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   123
{
slouken@5131
   124
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   125
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   126
slouken@6597
   127
    while(touch) {
icculus@7050
   128
        if (touch == leftFingerDown) {
slouken@6597
   129
            /* send mouse up */
slouken@6950
   130
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
icculus@7050
   131
            leftFingerDown = nil;
slouken@6597
   132
        }
slouken@4661
   133
slouken@6436
   134
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
jim@4662
   135
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@6951
   136
        SDL_SendTouch(touchId, (long)touch,
slouken@6951
   137
                      SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
jim@4662
   138
#else
kees@6003
   139
        int i;
kees@6003
   140
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   141
            if (finger[i] == touch) {
slouken@6951
   142
                SDL_SendTouch(touchId, i,
slouken@6951
   143
                              SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
kees@6003
   144
                finger[i] = NULL;
kees@6003
   145
                break;
kees@6003
   146
            }
slouken@5131
   147
        }
jim@4662
   148
#endif
kees@6003
   149
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   150
    }
slouken@2765
   151
}
slouken@2765
   152
kees@6003
   153
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   154
{
slouken@5131
   155
    /*
slouken@5131
   156
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   157
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   158
        all active touches are canceled.
slouken@5131
   159
    */
slouken@5131
   160
    [self touchesEnded: touches withEvent: event];
slouken@2765
   161
}
slouken@2765
   162
kees@6003
   163
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   164
{
slouken@5131
   165
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   166
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   167
slouken@6597
   168
    while (touch) {
icculus@7050
   169
        if (touch == leftFingerDown) {
slouken@6597
   170
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
slouken@4488
   171
slouken@6597
   172
            /* send moved event */
slouken@6950
   173
            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
slouken@6597
   174
        }
jim@4660
   175
slouken@6436
   176
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
jim@4662
   177
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
   178
        SDL_SendTouchMotion(touchId, (long)touch,
slouken@6951
   179
                            locationInView.x, locationInView.y, 1.0f);
jim@4662
   180
#else
kees@6003
   181
        int i;
kees@6003
   182
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   183
            if (finger[i] == touch) {
kees@6003
   184
                SDL_SendTouchMotion(touchId, i,
slouken@6951
   185
                                    locationInView.x, locationInView.y, 1.0f);
kees@6003
   186
                break;
kees@6003
   187
            }
slouken@5131
   188
        }
jim@4662
   189
#endif
kees@6003
   190
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   191
    }
slouken@2765
   192
}
slouken@2765
   193
slouken@2765
   194
/*
slouken@5131
   195
    ---- Keyboard related functionality below this line ----
slouken@2765
   196
*/
slouken@2765
   197
#if SDL_IPHONE_KEYBOARD
slouken@2765
   198
slouken@2765
   199
/* Is the iPhone virtual keyboard visible onscreen? */
kees@6003
   200
- (BOOL)keyboardVisible
kees@6003
   201
{
slouken@5131
   202
    return keyboardVisible;
slouken@2765
   203
}
slouken@2765
   204
slouken@2765
   205
/* Set ourselves up as a UITextFieldDelegate */
kees@6003
   206
- (void)initializeKeyboard
kees@6003
   207
{
slouken@5131
   208
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
slouken@5131
   209
    textField.delegate = self;
slouken@5131
   210
    /* placeholder so there is something to delete! */
kees@6001
   211
    textField.text = @" ";
kees@6001
   212
slouken@5131
   213
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   214
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   215
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   216
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   217
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   218
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   219
    textField.returnKeyType = UIReturnKeyDefault;
kees@6001
   220
    textField.secureTextEntry = NO;
kees@6001
   221
slouken@5131
   222
    textField.hidden = YES;
slouken@5131
   223
    keyboardVisible = NO;
slouken@5131
   224
    /* add the UITextField (hidden) to our view */
slouken@5131
   225
    [self addSubview: textField];
slouken@5131
   226
    [textField release];
slouken@2765
   227
}
slouken@2765
   228
slouken@2765
   229
/* reveal onscreen virtual keyboard */
kees@6003
   230
- (void)showKeyboard
kees@6003
   231
{
slouken@5131
   232
    keyboardVisible = YES;
slouken@5131
   233
    [textField becomeFirstResponder];
slouken@2765
   234
}
slouken@2765
   235
slouken@2765
   236
/* hide onscreen virtual keyboard */
kees@6003
   237
- (void)hideKeyboard
kees@6003
   238
{
slouken@5131
   239
    keyboardVisible = NO;
slouken@5131
   240
    [textField resignFirstResponder];
slouken@2765
   241
}
slouken@2765
   242
slouken@2765
   243
/* UITextFieldDelegate method.  Invoked when user types something. */
kees@6003
   244
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
kees@6003
   245
{
slouken@5131
   246
    if ([string length] == 0) {
slouken@5131
   247
        /* it wants to replace text with nothing, ie a delete */
slouken@5131
   248
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@5131
   249
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@5131
   250
    }
slouken@5131
   251
    else {
slouken@5131
   252
        /* go through all the characters in the string we've been sent
slouken@5131
   253
           and convert them to key presses */
slouken@5131
   254
        int i;
kees@6003
   255
        for (i = 0; i < [string length]; i++) {
kees@6001
   256
slouken@5131
   257
            unichar c = [string characterAtIndex: i];
kees@6001
   258
slouken@5131
   259
            Uint16 mod = 0;
slouken@5218
   260
            SDL_Scancode code;
kees@6001
   261
slouken@5131
   262
            if (c < 127) {
slouken@5218
   263
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   264
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   265
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   266
            }
slouken@5131
   267
            else {
slouken@5131
   268
                /* we only deal with ASCII right now */
slouken@5131
   269
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   270
                mod = 0;
slouken@5131
   271
            }
kees@6001
   272
slouken@5131
   273
            if (mod & KMOD_SHIFT) {
slouken@5131
   274
                /* If character uses shift, press shift down */
slouken@5131
   275
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   276
            }
slouken@5131
   277
            /* send a keydown and keyup even for the character */
slouken@5131
   278
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   279
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   280
            if (mod & KMOD_SHIFT) {
slouken@5131
   281
                /* If character uses shift, press shift back up */
slouken@5131
   282
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
kees@6001
   283
            }
slouken@5131
   284
        }
slouken@5461
   285
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   286
    }
slouken@5131
   287
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   288
}
slouken@2765
   289
slouken@2765
   290
/* Terminates the editing session */
kees@6003
   291
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
kees@6003
   292
{
slouken@5134
   293
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@6054
   294
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
slouken@6654
   295
    SDL_StopTextInput();
slouken@5131
   296
    return YES;
slouken@2765
   297
}
slouken@2765
   298
slouken@2765
   299
#endif
slouken@2765
   300
slouken@2765
   301
@end
slouken@2765
   302
slouken@2765
   303
/* iPhone keyboard addition functions */
slouken@2765
   304
#if SDL_IPHONE_KEYBOARD
slouken@2765
   305
slouken@6044
   306
static SDL_uikitview * getWindowView(SDL_Window * window)
kees@6010
   307
{
kees@6010
   308
    if (window == NULL) {
kees@6010
   309
        SDL_SetError("Window does not exist");
kees@6010
   310
        return nil;
kees@6010
   311
    }
kees@6010
   312
kees@6010
   313
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
kees@6010
   314
    SDL_uikitview *view = data != NULL ? data->view : nil;
kees@6010
   315
kees@6010
   316
    if (view == nil) {
kees@6010
   317
        SDL_SetError("Window has no view");
kees@6010
   318
    }
kees@6010
   319
kees@6010
   320
    return view;
kees@6010
   321
}
kees@6010
   322
slouken@6654
   323
SDL_bool UIKit_HasScreenKeyboardSupport(_THIS)
slouken@6392
   324
{
slouken@6392
   325
    return SDL_TRUE;
slouken@6392
   326
}
slouken@6392
   327
slouken@6654
   328
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   329
{
kees@6010
   330
    SDL_uikitview *view = getWindowView(window);
slouken@6654
   331
    if (view != nil) {
slouken@6654
   332
        [view showKeyboard];
slouken@5131
   333
    }
slouken@2765
   334
}
slouken@2765
   335
slouken@6654
   336
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   337
{
kees@6010
   338
    SDL_uikitview *view = getWindowView(window);
slouken@6654
   339
    if (view != nil) {
slouken@6654
   340
        [view hideKeyboard];
kees@6001
   341
    }
slouken@2765
   342
}
slouken@2765
   343
slouken@6392
   344
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
kees@6003
   345
{
kees@6010
   346
    SDL_uikitview *view = getWindowView(window);
kees@6010
   347
    if (view == nil) {
kees@6010
   348
        return 0;
kees@6001
   349
    }
kees@6001
   350
kees@6010
   351
    return view.keyboardVisible;
slouken@2765
   352
}
slouken@2765
   353
slouken@5132
   354
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   355
slouken@6044
   356
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   357
slouken@5132
   358
/* vi: set ts=4 sw=4 expandtab: */