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