src/video/uikit/SDL_uikitview.m
author Kees Bakker <kees@ijzerbout.nl>
Tue, 27 Sep 2011 22:51:26 +0200
changeset 6003 fddf81967e2d
parent 6001 3ae3d1e58d59
child 6010 508aa078acc0
permissions -rw-r--r--
Use a consistent source code format in src/video/uikit

For example:
* Opening bracket of a method/function on a new line at column 0
* space after "if", "while", etc
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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@2765
    21
slouken@2765
    22
#import "SDL_uikitview.h"
slouken@2765
    23
slouken@4490
    24
#include "../../events/SDL_keyboard_c.h"
slouken@4490
    25
#include "../../events/SDL_mouse_c.h"
jimtla@4677
    26
#include "../../events/SDL_touch_c.h"
slouken@4490
    27
slouken@2765
    28
#if SDL_IPHONE_KEYBOARD
slouken@2765
    29
#import "keyinfotable.h"
slouken@2765
    30
#import "SDL_uikitappdelegate.h"
slouken@2765
    31
#import "SDL_uikitwindow.h"
slouken@2765
    32
#endif
slouken@2765
    33
slouken@2765
    34
@implementation SDL_uikitview
slouken@2765
    35
kees@6003
    36
- (void)dealloc
kees@6003
    37
{
slouken@5131
    38
    [super dealloc];
slouken@2765
    39
}
slouken@2765
    40
kees@6003
    41
- (id)initWithFrame:(CGRect)frame
kees@6003
    42
{
slouken@5131
    43
    self = [super initWithFrame: frame];
kees@6001
    44
slouken@2765
    45
#if SDL_IPHONE_KEYBOARD
slouken@5131
    46
    [self initializeKeyboard];
kees@6001
    47
#endif
slouken@2765
    48
jimtla@4677
    49
#ifdef FIXED_MULTITOUCH
slouken@5445
    50
    self.multipleTouchEnabled = YES;
slouken@5445
    51
slouken@5131
    52
    SDL_Touch touch;
slouken@5131
    53
    touch.id = 0; //TODO: Should be -1?
jim@4660
    54
slouken@5131
    55
    //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
slouken@5131
    56
    //EventTouchData* data = (EventTouchData*)(touch.driverdata);
kees@6001
    57
slouken@5131
    58
    touch.x_min = 0;
slouken@5131
    59
    touch.x_max = frame.size.width;
slouken@5131
    60
    touch.native_xres = touch.x_max - touch.x_min;
slouken@5131
    61
    touch.y_min = 0;
slouken@5131
    62
    touch.y_max = frame.size.height;
slouken@5131
    63
    touch.native_yres = touch.y_max - touch.y_min;
slouken@5131
    64
    touch.pressure_min = 0;
slouken@5131
    65
    touch.pressure_max = 1;
slouken@5131
    66
    touch.native_pressureres = touch.pressure_max - touch.pressure_min;
jim@4660
    67
jim@4660
    68
slouken@5131
    69
    touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
slouken@4465
    70
#endif
slouken@4661
    71
slouken@5131
    72
    return self;
slouken@2765
    73
slouken@2765
    74
}
slouken@2765
    75
kees@6003
    76
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
    77
{
slouken@5131
    78
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
    79
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5459
    80
slouken@5131
    81
    if (touch) {
slouken@5131
    82
        CGPoint locationInView = [touch locationInView: self];
kees@6001
    83
slouken@5131
    84
        /* send moved event */
slouken@5131
    85
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@4488
    86
slouken@5131
    87
        /* send mouse down event */
slouken@5131
    88
        SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@5131
    89
    }
jim@4660
    90
jimtla@4677
    91
#ifdef FIXED_MULTITOUCH
slouken@5131
    92
    while(touch) {
kees@6003
    93
        CGPoint locationInView = [touch locationInView: self];
jim@4662
    94
jim@4662
    95
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
    96
        //FIXME: TODO: Using touch as the fingerId is potentially dangerous
kees@6003
    97
        //It is also much more efficient than storing the UITouch pointer
kees@6003
    98
        //and comparing it to the incoming event.
kees@6003
    99
        SDL_SendFingerDown(touchId, (long)touch,
kees@6003
   100
                           SDL_TRUE, locationInView.x, locationInView.y,
kees@6003
   101
                           1);
jim@4662
   102
#else
kees@6003
   103
        int i;
kees@6003
   104
        for(i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   105
            if (finger[i] == NULL) {
kees@6003
   106
                finger[i] = touch;
kees@6003
   107
                SDL_SendFingerDown(touchId, i,
kees@6003
   108
                                   SDL_TRUE, locationInView.x, locationInView.y,
kees@6003
   109
                                   1);
kees@6003
   110
                break;
kees@6003
   111
            }
slouken@5131
   112
        }
jim@4662
   113
#endif
jim@4660
   114
kees@6003
   115
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   116
    }
slouken@4465
   117
#endif
slouken@2765
   118
}
slouken@2765
   119
kees@6003
   120
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   121
{
slouken@5131
   122
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   123
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   124
slouken@5131
   125
    if (touch) {
slouken@5131
   126
        /* send mouse up */
slouken@5131
   127
        SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@5131
   128
    }
slouken@4661
   129
jimtla@4677
   130
#ifdef FIXED_MULTITOUCH
slouken@5131
   131
    while(touch) {
kees@6003
   132
        CGPoint locationInView = [touch locationInView: self];
jim@4662
   133
jim@4662
   134
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
   135
        SDL_SendFingerDown(touchId, (long)touch,
kees@6003
   136
                           SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   137
                           1);
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) {
kees@6003
   142
                SDL_SendFingerDown(touchId, i,
kees@6003
   143
                                   SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   144
                                   1);
kees@6003
   145
                finger[i] = NULL;
kees@6003
   146
                break;
kees@6003
   147
            }
slouken@5131
   148
        }
jim@4662
   149
#endif
jim@4660
   150
kees@6003
   151
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   152
    }
slouken@4465
   153
#endif
slouken@2765
   154
}
slouken@2765
   155
kees@6003
   156
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   157
{
slouken@5131
   158
    /*
slouken@5131
   159
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   160
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   161
        all active touches are canceled.
slouken@5131
   162
    */
slouken@5131
   163
    [self touchesEnded: touches withEvent: event];
slouken@2765
   164
}
slouken@2765
   165
kees@6003
   166
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   167
{
slouken@5131
   168
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   169
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   170
slouken@5131
   171
    if (touch) {
slouken@5131
   172
        CGPoint locationInView = [touch locationInView: self];
slouken@4488
   173
slouken@5131
   174
        /* send moved event */
slouken@5131
   175
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@5131
   176
    }
jim@4660
   177
jimtla@4677
   178
#ifdef FIXED_MULTITOUCH
slouken@5131
   179
    while(touch) {
kees@6003
   180
        CGPoint locationInView = [touch locationInView: self];
jim@4662
   181
jim@4662
   182
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
   183
        SDL_SendTouchMotion(touchId, (long)touch,
kees@6003
   184
                            SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   185
                            1);
jim@4662
   186
#else
kees@6003
   187
        int i;
kees@6003
   188
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   189
            if (finger[i] == touch) {
kees@6003
   190
                SDL_SendTouchMotion(touchId, i,
kees@6003
   191
                                    SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   192
                                    1);
kees@6003
   193
                break;
kees@6003
   194
            }
slouken@5131
   195
        }
jim@4662
   196
#endif
jim@4660
   197
kees@6003
   198
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   199
    }
slouken@4465
   200
#endif
slouken@2765
   201
}
slouken@2765
   202
slouken@2765
   203
/*
slouken@5131
   204
    ---- Keyboard related functionality below this line ----
slouken@2765
   205
*/
slouken@2765
   206
#if SDL_IPHONE_KEYBOARD
slouken@2765
   207
slouken@2765
   208
/* Is the iPhone virtual keyboard visible onscreen? */
kees@6003
   209
- (BOOL)keyboardVisible
kees@6003
   210
{
slouken@5131
   211
    return keyboardVisible;
slouken@2765
   212
}
slouken@2765
   213
slouken@2765
   214
/* Set ourselves up as a UITextFieldDelegate */
kees@6003
   215
- (void)initializeKeyboard
kees@6003
   216
{
slouken@5131
   217
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
slouken@5131
   218
    textField.delegate = self;
slouken@5131
   219
    /* placeholder so there is something to delete! */
kees@6001
   220
    textField.text = @" ";
kees@6001
   221
slouken@5131
   222
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   223
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   224
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   225
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   226
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   227
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   228
    textField.returnKeyType = UIReturnKeyDefault;
kees@6001
   229
    textField.secureTextEntry = NO;
kees@6001
   230
slouken@5131
   231
    textField.hidden = YES;
slouken@5131
   232
    keyboardVisible = NO;
slouken@5131
   233
    /* add the UITextField (hidden) to our view */
slouken@5131
   234
    [self addSubview: textField];
slouken@5131
   235
    [textField release];
slouken@2765
   236
}
slouken@2765
   237
slouken@2765
   238
/* reveal onscreen virtual keyboard */
kees@6003
   239
- (void)showKeyboard
kees@6003
   240
{
slouken@5131
   241
    keyboardVisible = YES;
slouken@5131
   242
    [textField becomeFirstResponder];
slouken@2765
   243
}
slouken@2765
   244
slouken@2765
   245
/* hide onscreen virtual keyboard */
kees@6003
   246
- (void)hideKeyboard
kees@6003
   247
{
slouken@5131
   248
    keyboardVisible = NO;
slouken@5131
   249
    [textField resignFirstResponder];
slouken@2765
   250
}
slouken@2765
   251
slouken@2765
   252
/* UITextFieldDelegate method.  Invoked when user types something. */
kees@6003
   253
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
kees@6003
   254
{
slouken@5131
   255
    if ([string length] == 0) {
slouken@5131
   256
        /* it wants to replace text with nothing, ie a delete */
slouken@5131
   257
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@5131
   258
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@5131
   259
    }
slouken@5131
   260
    else {
slouken@5131
   261
        /* go through all the characters in the string we've been sent
slouken@5131
   262
           and convert them to key presses */
slouken@5131
   263
        int i;
kees@6003
   264
        for (i = 0; i < [string length]; i++) {
kees@6001
   265
slouken@5131
   266
            unichar c = [string characterAtIndex: i];
kees@6001
   267
slouken@5131
   268
            Uint16 mod = 0;
slouken@5218
   269
            SDL_Scancode code;
kees@6001
   270
slouken@5131
   271
            if (c < 127) {
slouken@5218
   272
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   273
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   274
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   275
            }
slouken@5131
   276
            else {
slouken@5131
   277
                /* we only deal with ASCII right now */
slouken@5131
   278
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   279
                mod = 0;
slouken@5131
   280
            }
kees@6001
   281
slouken@5131
   282
            if (mod & KMOD_SHIFT) {
slouken@5131
   283
                /* If character uses shift, press shift down */
slouken@5131
   284
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   285
            }
slouken@5131
   286
            /* send a keydown and keyup even for the character */
slouken@5131
   287
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   288
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   289
            if (mod & KMOD_SHIFT) {
slouken@5131
   290
                /* If character uses shift, press shift back up */
slouken@5131
   291
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
kees@6001
   292
            }
slouken@5131
   293
        }
slouken@5461
   294
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   295
    }
slouken@5131
   296
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   297
}
slouken@2765
   298
slouken@2765
   299
/* Terminates the editing session */
kees@6003
   300
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
kees@6003
   301
{
slouken@5134
   302
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@5131
   303
    [self hideKeyboard];
slouken@5131
   304
    return YES;
slouken@2765
   305
}
slouken@2765
   306
slouken@2765
   307
#endif
slouken@2765
   308
slouken@2765
   309
@end
slouken@2765
   310
slouken@2765
   311
/* iPhone keyboard addition functions */
slouken@2765
   312
#if SDL_IPHONE_KEYBOARD
slouken@2765
   313
kees@6003
   314
int SDL_iPhoneKeyboardShow(SDL_Window * window)
kees@6003
   315
{
slouken@5131
   316
    SDL_WindowData *data;
slouken@5131
   317
    SDL_uikitview *view;
kees@6001
   318
slouken@5131
   319
    if (NULL == window) {
slouken@5131
   320
        SDL_SetError("Window does not exist");
slouken@5131
   321
        return -1;
slouken@5131
   322
    }
kees@6001
   323
slouken@5131
   324
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   325
    view = data->view;
kees@6001
   326
slouken@5131
   327
    if (nil == view) {
slouken@5131
   328
        SDL_SetError("Window has no view");
slouken@5131
   329
        return -1;
slouken@5131
   330
    }
slouken@5131
   331
    else {
slouken@5131
   332
        [view showKeyboard];
slouken@5131
   333
        return 0;
slouken@5131
   334
    }
slouken@2765
   335
}
slouken@2765
   336
kees@6003
   337
int SDL_iPhoneKeyboardHide(SDL_Window * window)
kees@6003
   338
{
slouken@5131
   339
    SDL_WindowData *data;
slouken@5131
   340
    SDL_uikitview *view;
kees@6001
   341
slouken@5131
   342
    if (NULL == window) {
slouken@5131
   343
        SDL_SetError("Window does not exist");
slouken@5131
   344
        return -1;
kees@6001
   345
    }
kees@6001
   346
slouken@5131
   347
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   348
    view = data->view;
kees@6001
   349
slouken@5131
   350
    if (NULL == view) {
slouken@5131
   351
        SDL_SetError("Window has no view");
slouken@5131
   352
        return -1;
slouken@5131
   353
    }
slouken@5131
   354
    else {
slouken@5131
   355
        [view hideKeyboard];
slouken@5131
   356
        return 0;
slouken@5131
   357
    }
slouken@2765
   358
}
slouken@2765
   359
kees@6003
   360
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window)
kees@6003
   361
{
slouken@5131
   362
    SDL_WindowData *data;
slouken@5131
   363
    SDL_uikitview *view;
kees@6001
   364
slouken@5131
   365
    if (NULL == window) {
slouken@5131
   366
        SDL_SetError("Window does not exist");
slouken@5131
   367
        return -1;
kees@6001
   368
    }
kees@6001
   369
slouken@5131
   370
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   371
    view = data->view;
kees@6001
   372
slouken@5131
   373
    if (NULL == view) {
slouken@5131
   374
        SDL_SetError("Window has no view");
slouken@5131
   375
        return 0;
slouken@5131
   376
    }
slouken@5131
   377
    else {
slouken@5131
   378
        return view.keyboardVisible;
slouken@5131
   379
    }
slouken@2765
   380
}
slouken@2765
   381
kees@6003
   382
int SDL_iPhoneKeyboardToggle(SDL_Window * window)
kees@6003
   383
{
slouken@5131
   384
    SDL_WindowData *data;
slouken@5131
   385
    SDL_uikitview *view;
kees@6001
   386
slouken@5131
   387
    if (NULL == window) {
slouken@5131
   388
        SDL_SetError("Window does not exist");
slouken@5131
   389
        return -1;
kees@6001
   390
    }
kees@6001
   391
slouken@5131
   392
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   393
    view = data->view;
kees@6001
   394
slouken@5131
   395
    if (NULL == view) {
slouken@5131
   396
        SDL_SetError("Window has no view");
slouken@5131
   397
        return -1;
slouken@5131
   398
    }
slouken@5131
   399
    else {
slouken@5131
   400
        if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@5131
   401
            SDL_iPhoneKeyboardHide(window);
slouken@5131
   402
        }
slouken@5131
   403
        else {
slouken@5131
   404
            SDL_iPhoneKeyboardShow(window);
slouken@5131
   405
        }
slouken@5131
   406
        return 0;
slouken@5131
   407
    }
slouken@2765
   408
}
slouken@2765
   409
slouken@2765
   410
#else
slouken@2765
   411
slouken@2765
   412
/* stubs, used if compiled without keyboard support */
slouken@2765
   413
kees@6003
   414
int SDL_iPhoneKeyboardShow(SDL_Window * window)
kees@6003
   415
{
slouken@5131
   416
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   417
    return -1;
slouken@2765
   418
}
slouken@2765
   419
kees@6003
   420
int SDL_iPhoneKeyboardHide(SDL_Window * window)
kees@6003
   421
{
slouken@5131
   422
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   423
    return -1;
slouken@2765
   424
}
slouken@2765
   425
kees@6003
   426
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window)
kees@6003
   427
{
slouken@5131
   428
    return 0;
slouken@2765
   429
}
slouken@2765
   430
kees@6003
   431
int SDL_iPhoneKeyboardToggle(SDL_Window * window)
kees@6003
   432
{
slouken@5131
   433
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   434
    return -1;
slouken@2765
   435
}
slouken@2765
   436
slouken@5132
   437
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   438
slouken@5132
   439
/* vi: set ts=4 sw=4 expandtab: */