src/video/uikit/SDL_uikitview.m
author Kees Bakker <kees@ijzerbout.nl>
Tue, 27 Sep 2011 21:02:26 +0200
changeset 6001 3ae3d1e58d59
parent 5535 96594ac5fd1a
child 6003 fddf81967e2d
permissions -rw-r--r--
Cleanup out trailing whitespace in src/video/uikit
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
slouken@2765
    36
- (void)dealloc {
slouken@5131
    37
    [super dealloc];
slouken@2765
    38
}
slouken@2765
    39
slouken@2765
    40
- (id)initWithFrame:(CGRect)frame {
slouken@2765
    41
slouken@5131
    42
    self = [super initWithFrame: frame];
kees@6001
    43
slouken@2765
    44
#if SDL_IPHONE_KEYBOARD
slouken@5131
    45
    [self initializeKeyboard];
kees@6001
    46
#endif
slouken@2765
    47
jimtla@4677
    48
#ifdef FIXED_MULTITOUCH
slouken@5445
    49
    self.multipleTouchEnabled = YES;
slouken@5445
    50
slouken@5131
    51
    SDL_Touch touch;
slouken@5131
    52
    touch.id = 0; //TODO: Should be -1?
jim@4660
    53
slouken@5131
    54
    //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
slouken@5131
    55
    //EventTouchData* data = (EventTouchData*)(touch.driverdata);
kees@6001
    56
slouken@5131
    57
    touch.x_min = 0;
slouken@5131
    58
    touch.x_max = frame.size.width;
slouken@5131
    59
    touch.native_xres = touch.x_max - touch.x_min;
slouken@5131
    60
    touch.y_min = 0;
slouken@5131
    61
    touch.y_max = frame.size.height;
slouken@5131
    62
    touch.native_yres = touch.y_max - touch.y_min;
slouken@5131
    63
    touch.pressure_min = 0;
slouken@5131
    64
    touch.pressure_max = 1;
slouken@5131
    65
    touch.native_pressureres = touch.pressure_max - touch.pressure_min;
jim@4660
    66
jim@4660
    67
slouken@5131
    68
    touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
slouken@4465
    69
#endif
slouken@4661
    70
slouken@5131
    71
    return self;
slouken@2765
    72
slouken@2765
    73
}
slouken@2765
    74
slouken@2765
    75
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
    76
slouken@5131
    77
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
    78
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5459
    79
slouken@5131
    80
    if (touch) {
slouken@5131
    81
        CGPoint locationInView = [touch locationInView: self];
kees@6001
    82
slouken@5131
    83
        /* send moved event */
slouken@5131
    84
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@4488
    85
slouken@5131
    86
        /* send mouse down event */
slouken@5131
    87
        SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@5131
    88
    }
jim@4660
    89
jimtla@4677
    90
#ifdef FIXED_MULTITOUCH
slouken@5131
    91
    while(touch) {
slouken@5131
    92
      CGPoint locationInView = [touch locationInView: self];
jim@4662
    93
jim@4662
    94
jim@4662
    95
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
    96
      //FIXME: TODO: Using touch as the fingerId is potentially dangerous
slouken@5131
    97
      //It is also much more efficient than storing the UITouch pointer
slouken@5131
    98
      //and comparing it to the incoming event.
slouken@5131
    99
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   100
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   101
                 1);
jim@4662
   102
#else
slouken@5131
   103
      int i;
slouken@5131
   104
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   105
        if(finger[i] == NULL) {
slouken@5131
   106
          finger[i] = touch;
slouken@5131
   107
          SDL_SendFingerDown(touchId,i,
slouken@5131
   108
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   109
                 1);
slouken@5131
   110
          break;
slouken@5131
   111
        }
slouken@5131
   112
      }
jim@4662
   113
#endif
jim@4660
   114
kees@6001
   115
      touch = (UITouch*)[enumerator nextObject];
slouken@5131
   116
    }
slouken@4465
   117
#endif
slouken@2765
   118
}
slouken@2765
   119
slouken@2765
   120
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
kees@6001
   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) {
slouken@5131
   132
      CGPoint locationInView = [touch locationInView: self];
kees@6001
   133
jim@4662
   134
jim@4662
   135
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   136
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   137
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   138
                 1);
jim@4662
   139
#else
slouken@5131
   140
      int i;
slouken@5131
   141
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   142
        if(finger[i] == touch) {
slouken@5131
   143
          SDL_SendFingerDown(touchId,i,
slouken@5131
   144
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   145
                 1);
slouken@5459
   146
          finger[i] = NULL;
slouken@5131
   147
          break;
slouken@5131
   148
        }
slouken@5131
   149
      }
jim@4662
   150
#endif
jim@4660
   151
kees@6001
   152
      touch = (UITouch*)[enumerator nextObject];
slouken@5131
   153
    }
slouken@4465
   154
#endif
slouken@2765
   155
}
slouken@2765
   156
slouken@2765
   157
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
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
slouken@2765
   166
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
kees@6001
   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) {
slouken@5131
   180
      CGPoint locationInView = [touch locationInView: self];
kees@6001
   181
jim@4662
   182
jim@4662
   183
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   184
      SDL_SendTouchMotion(touchId,(long)touch,
slouken@5131
   185
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   186
                  1);
jim@4662
   187
#else
slouken@5131
   188
      int i;
slouken@5131
   189
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   190
        if(finger[i] == touch) {
slouken@5131
   191
          SDL_SendTouchMotion(touchId,i,
slouken@5131
   192
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   193
                  1);
slouken@5131
   194
          break;
slouken@5131
   195
        }
slouken@5131
   196
      }
jim@4662
   197
#endif
jim@4660
   198
kees@6001
   199
      touch = (UITouch*)[enumerator nextObject];
slouken@5131
   200
    }
slouken@4465
   201
#endif
slouken@2765
   202
}
slouken@2765
   203
slouken@2765
   204
/*
slouken@5131
   205
    ---- Keyboard related functionality below this line ----
slouken@2765
   206
*/
slouken@2765
   207
#if SDL_IPHONE_KEYBOARD
slouken@2765
   208
slouken@2765
   209
/* Is the iPhone virtual keyboard visible onscreen? */
slouken@2765
   210
- (BOOL)keyboardVisible {
slouken@5131
   211
    return keyboardVisible;
slouken@2765
   212
}
slouken@2765
   213
slouken@2765
   214
/* Set ourselves up as a UITextFieldDelegate */
slouken@2765
   215
- (void)initializeKeyboard {
kees@6001
   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 */
slouken@2765
   239
- (void)showKeyboard {
slouken@5131
   240
    keyboardVisible = YES;
slouken@5131
   241
    [textField becomeFirstResponder];
slouken@2765
   242
}
slouken@2765
   243
slouken@2765
   244
/* hide onscreen virtual keyboard */
slouken@2765
   245
- (void)hideKeyboard {
slouken@5131
   246
    keyboardVisible = NO;
slouken@5131
   247
    [textField resignFirstResponder];
slouken@2765
   248
}
slouken@2765
   249
slouken@2765
   250
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   251
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
kees@6001
   252
slouken@5131
   253
    if ([string length] == 0) {
slouken@5131
   254
        /* it wants to replace text with nothing, ie a delete */
slouken@5131
   255
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@5131
   256
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@5131
   257
    }
slouken@5131
   258
    else {
slouken@5131
   259
        /* go through all the characters in the string we've been sent
slouken@5131
   260
           and convert them to key presses */
slouken@5131
   261
        int i;
slouken@5131
   262
        for (i=0; i<[string length]; i++) {
kees@6001
   263
slouken@5131
   264
            unichar c = [string characterAtIndex: i];
kees@6001
   265
slouken@5131
   266
            Uint16 mod = 0;
slouken@5218
   267
            SDL_Scancode code;
kees@6001
   268
slouken@5131
   269
            if (c < 127) {
slouken@5218
   270
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   271
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   272
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   273
            }
slouken@5131
   274
            else {
slouken@5131
   275
                /* we only deal with ASCII right now */
slouken@5131
   276
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   277
                mod = 0;
slouken@5131
   278
            }
kees@6001
   279
slouken@5131
   280
            if (mod & KMOD_SHIFT) {
slouken@5131
   281
                /* If character uses shift, press shift down */
slouken@5131
   282
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   283
            }
slouken@5131
   284
            /* send a keydown and keyup even for the character */
slouken@5131
   285
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   286
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   287
            if (mod & KMOD_SHIFT) {
slouken@5131
   288
                /* If character uses shift, press shift back up */
slouken@5131
   289
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
kees@6001
   290
            }
slouken@5131
   291
        }
slouken@5461
   292
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   293
    }
slouken@5131
   294
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   295
}
slouken@2765
   296
slouken@2765
   297
/* Terminates the editing session */
slouken@2765
   298
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@5134
   299
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@5131
   300
    [self hideKeyboard];
slouken@5131
   301
    return YES;
slouken@2765
   302
}
slouken@2765
   303
slouken@2765
   304
#endif
slouken@2765
   305
slouken@2765
   306
@end
slouken@2765
   307
slouken@2765
   308
/* iPhone keyboard addition functions */
slouken@2765
   309
#if SDL_IPHONE_KEYBOARD
slouken@2765
   310
slouken@3685
   311
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
kees@6001
   312
slouken@5131
   313
    SDL_WindowData *data;
slouken@5131
   314
    SDL_uikitview *view;
kees@6001
   315
slouken@5131
   316
    if (NULL == window) {
slouken@5131
   317
        SDL_SetError("Window does not exist");
slouken@5131
   318
        return -1;
slouken@5131
   319
    }
kees@6001
   320
slouken@5131
   321
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   322
    view = data->view;
kees@6001
   323
slouken@5131
   324
    if (nil == view) {
slouken@5131
   325
        SDL_SetError("Window has no view");
slouken@5131
   326
        return -1;
slouken@5131
   327
    }
slouken@5131
   328
    else {
slouken@5131
   329
        [view showKeyboard];
slouken@5131
   330
        return 0;
slouken@5131
   331
    }
slouken@2765
   332
}
slouken@2765
   333
slouken@3685
   334
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
kees@6001
   335
slouken@5131
   336
    SDL_WindowData *data;
slouken@5131
   337
    SDL_uikitview *view;
kees@6001
   338
slouken@5131
   339
    if (NULL == window) {
slouken@5131
   340
        SDL_SetError("Window does not exist");
slouken@5131
   341
        return -1;
kees@6001
   342
    }
kees@6001
   343
slouken@5131
   344
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   345
    view = data->view;
kees@6001
   346
slouken@5131
   347
    if (NULL == view) {
slouken@5131
   348
        SDL_SetError("Window has no view");
slouken@5131
   349
        return -1;
slouken@5131
   350
    }
slouken@5131
   351
    else {
slouken@5131
   352
        [view hideKeyboard];
slouken@5131
   353
        return 0;
slouken@5131
   354
    }
slouken@2765
   355
}
slouken@2765
   356
slouken@3685
   357
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
kees@6001
   358
slouken@5131
   359
    SDL_WindowData *data;
slouken@5131
   360
    SDL_uikitview *view;
kees@6001
   361
slouken@5131
   362
    if (NULL == window) {
slouken@5131
   363
        SDL_SetError("Window does not exist");
slouken@5131
   364
        return -1;
kees@6001
   365
    }
kees@6001
   366
slouken@5131
   367
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   368
    view = data->view;
kees@6001
   369
slouken@5131
   370
    if (NULL == view) {
slouken@5131
   371
        SDL_SetError("Window has no view");
slouken@5131
   372
        return 0;
slouken@5131
   373
    }
slouken@5131
   374
    else {
slouken@5131
   375
        return view.keyboardVisible;
slouken@5131
   376
    }
slouken@2765
   377
}
slouken@2765
   378
slouken@3685
   379
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
kees@6001
   380
slouken@5131
   381
    SDL_WindowData *data;
slouken@5131
   382
    SDL_uikitview *view;
kees@6001
   383
slouken@5131
   384
    if (NULL == window) {
slouken@5131
   385
        SDL_SetError("Window does not exist");
slouken@5131
   386
        return -1;
kees@6001
   387
    }
kees@6001
   388
slouken@5131
   389
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   390
    view = data->view;
kees@6001
   391
slouken@5131
   392
    if (NULL == view) {
slouken@5131
   393
        SDL_SetError("Window has no view");
slouken@5131
   394
        return -1;
slouken@5131
   395
    }
slouken@5131
   396
    else {
slouken@5131
   397
        if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@5131
   398
            SDL_iPhoneKeyboardHide(window);
slouken@5131
   399
        }
slouken@5131
   400
        else {
slouken@5131
   401
            SDL_iPhoneKeyboardShow(window);
slouken@5131
   402
        }
slouken@5131
   403
        return 0;
slouken@5131
   404
    }
slouken@2765
   405
}
slouken@2765
   406
slouken@2765
   407
#else
slouken@2765
   408
slouken@2765
   409
/* stubs, used if compiled without keyboard support */
slouken@2765
   410
slouken@3685
   411
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@5131
   412
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   413
    return -1;
slouken@2765
   414
}
slouken@2765
   415
slouken@3685
   416
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@5131
   417
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   418
    return -1;
slouken@2765
   419
}
slouken@2765
   420
slouken@3685
   421
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@5131
   422
    return 0;
slouken@2765
   423
}
slouken@2765
   424
slouken@3685
   425
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@5131
   426
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   427
    return -1;
slouken@2765
   428
}
slouken@2765
   429
slouken@5132
   430
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   431
slouken@5132
   432
/* vi: set ts=4 sw=4 expandtab: */