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