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