src/video/uikit/SDL_uikitview.m
author Kees Bakker <kees@ijzerbout.nl>
Wed, 28 Sep 2011 21:25:46 +0200
changeset 6010 508aa078acc0
parent 6003 fddf81967e2d
child 6044 35448a5ea044
permissions -rw-r--r--
Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
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@6010
   314
SDL_uikitview * getWindowView(SDL_Window * window)
kees@6010
   315
{
kees@6010
   316
    if (window == NULL) {
kees@6010
   317
        SDL_SetError("Window does not exist");
kees@6010
   318
        return nil;
kees@6010
   319
    }
kees@6010
   320
kees@6010
   321
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
kees@6010
   322
    SDL_uikitview *view = data != NULL ? data->view : nil;
kees@6010
   323
kees@6010
   324
    if (view == nil) {
kees@6010
   325
        SDL_SetError("Window has no view");
kees@6010
   326
    }
kees@6010
   327
kees@6010
   328
    return view;
kees@6010
   329
}
kees@6010
   330
kees@6003
   331
int SDL_iPhoneKeyboardShow(SDL_Window * window)
kees@6003
   332
{
kees@6010
   333
    SDL_uikitview *view = getWindowView(window);
kees@6010
   334
    if (view == nil) {
slouken@5131
   335
        return -1;
slouken@5131
   336
    }
kees@6001
   337
kees@6010
   338
    [view showKeyboard];
kees@6010
   339
    return 0;
slouken@2765
   340
}
slouken@2765
   341
kees@6003
   342
int SDL_iPhoneKeyboardHide(SDL_Window * window)
kees@6003
   343
{
kees@6010
   344
    SDL_uikitview *view = getWindowView(window);
kees@6010
   345
    if (view == nil) {
slouken@5131
   346
        return -1;
kees@6001
   347
    }
kees@6001
   348
kees@6010
   349
    [view hideKeyboard];
kees@6010
   350
    return 0;
slouken@2765
   351
}
slouken@2765
   352
kees@6003
   353
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window)
kees@6003
   354
{
kees@6010
   355
    SDL_uikitview *view = getWindowView(window);
kees@6010
   356
    if (view == nil) {
kees@6010
   357
        return 0;
kees@6001
   358
    }
kees@6001
   359
kees@6010
   360
    return view.keyboardVisible;
slouken@2765
   361
}
slouken@2765
   362
kees@6003
   363
int SDL_iPhoneKeyboardToggle(SDL_Window * window)
kees@6003
   364
{
kees@6010
   365
    SDL_uikitview *view = getWindowView(window);
kees@6010
   366
    if (view == nil) {
slouken@5131
   367
        return -1;
kees@6001
   368
    }
kees@6001
   369
kees@6010
   370
    if (SDL_iPhoneKeyboardIsShown(window)) {
kees@6010
   371
        SDL_iPhoneKeyboardHide(window);
slouken@5131
   372
    }
slouken@5131
   373
    else {
kees@6010
   374
        SDL_iPhoneKeyboardShow(window);
slouken@5131
   375
    }
kees@6010
   376
    return 0;
slouken@2765
   377
}
slouken@2765
   378
slouken@2765
   379
#else
slouken@2765
   380
slouken@2765
   381
/* stubs, used if compiled without keyboard support */
slouken@2765
   382
kees@6003
   383
int SDL_iPhoneKeyboardShow(SDL_Window * window)
kees@6003
   384
{
slouken@5131
   385
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   386
    return -1;
slouken@2765
   387
}
slouken@2765
   388
kees@6003
   389
int SDL_iPhoneKeyboardHide(SDL_Window * window)
kees@6003
   390
{
slouken@5131
   391
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   392
    return -1;
slouken@2765
   393
}
slouken@2765
   394
kees@6003
   395
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window)
kees@6003
   396
{
slouken@5131
   397
    return 0;
slouken@2765
   398
}
slouken@2765
   399
kees@6003
   400
int SDL_iPhoneKeyboardToggle(SDL_Window * window)
kees@6003
   401
{
slouken@5131
   402
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   403
    return -1;
slouken@2765
   404
}
slouken@2765
   405
slouken@5132
   406
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   407
slouken@5132
   408
/* vi: set ts=4 sw=4 expandtab: */