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