src/video/uikit/SDL_uikitview.m
author David Ludwig <dludwig@pobox.com>
Sun, 27 Oct 2013 21:26:46 -0400
changeset 8535 e8ee0708ef5c
parent 8477 ad08f0d710f3
parent 7774 c5d6c8c001be
child 8543 b9dd3cf38585
permissions -rw-r--r--
WinRT: merged with SDL 2.0.1 codebase
slouken@6079
     1
 /*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@6518
    25
#include "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@6518
    32
#include "keyinfotable.h"
urkle@7111
    33
#endif
slouken@6518
    34
#include "SDL_uikitappdelegate.h"
slouken@6518
    35
#include "SDL_uikitmodes.h"
slouken@6518
    36
#include "SDL_uikitwindow.h"
slouken@2765
    37
slouken@2765
    38
@implementation SDL_uikitview
slouken@2765
    39
kees@6003
    40
- (void)dealloc
kees@6003
    41
{
slouken@5131
    42
    [super dealloc];
slouken@2765
    43
}
slouken@2765
    44
kees@6003
    45
- (id)initWithFrame:(CGRect)frame
kees@6003
    46
{
slouken@5131
    47
    self = [super initWithFrame: frame];
kees@6001
    48
slouken@2765
    49
#if SDL_IPHONE_KEYBOARD
slouken@5131
    50
    [self initializeKeyboard];
kees@6001
    51
#endif
slouken@2765
    52
slouken@5445
    53
    self.multipleTouchEnabled = YES;
slouken@5445
    54
slouken@6951
    55
    touchId = 1;
slouken@6951
    56
    SDL_AddTouch(touchId, "");
slouken@4661
    57
slouken@5131
    58
    return self;
slouken@2765
    59
slouken@2765
    60
}
slouken@2765
    61
slouken@6436
    62
- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
slouken@6079
    63
{
slouken@6079
    64
    CGPoint point = [touch locationInView: self];
slouken@6436
    65
slouken@7191
    66
    /* Get the display scale and apply that to the input coordinates */
slouken@6436
    67
    SDL_Window *window = self->viewcontroller.window;
slouken@6436
    68
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
slouken@6436
    69
    SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
slouken@7191
    70
slouken@6436
    71
    if (normalize) {
slouken@6439
    72
        CGRect bounds = [self bounds];
slouken@6439
    73
        point.x /= bounds.size.width;
slouken@6439
    74
        point.y /= bounds.size.height;
slouken@6519
    75
    } else {
slouken@6519
    76
        point.x *= displaymodedata->scale;
slouken@6519
    77
        point.y *= displaymodedata->scale;
slouken@6436
    78
    }
slouken@6079
    79
    return point;
slouken@6079
    80
}
slouken@6079
    81
kees@6003
    82
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
    83
{
slouken@5131
    84
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
    85
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5459
    86
slouken@6597
    87
    while (touch) {
slouken@6597
    88
        if (!leftFingerDown) {
slouken@6597
    89
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
kees@6001
    90
slouken@6597
    91
            /* send moved event */
slouken@6950
    92
            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
slouken@4488
    93
slouken@6597
    94
            /* send mouse down event */
slouken@6950
    95
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
jim@4660
    96
icculus@7050
    97
            leftFingerDown = touch;
slouken@6597
    98
        }
slouken@6597
    99
slouken@6436
   100
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
jim@4662
   101
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@7191
   102
        /* FIXME: TODO: Using touch as the fingerId is potentially dangerous
slouken@7191
   103
         * It is also much more efficient than storing the UITouch pointer
slouken@7191
   104
         * and comparing it to the incoming event.
slouken@7191
   105
         */
icculus@7050
   106
        SDL_SendTouch(touchId, (SDL_FingerID)((size_t)touch),
slouken@6951
   107
                      SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
jim@4662
   108
#else
kees@6003
   109
        int i;
kees@6003
   110
        for(i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   111
            if (finger[i] == NULL) {
kees@6003
   112
                finger[i] = touch;
slouken@6951
   113
                SDL_SendTouch(touchId, i,
slouken@6951
   114
                              SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
kees@6003
   115
                break;
kees@6003
   116
            }
slouken@5131
   117
        }
jim@4662
   118
#endif
kees@6003
   119
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   120
    }
slouken@2765
   121
}
slouken@2765
   122
kees@6003
   123
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   124
{
slouken@5131
   125
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   126
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   127
slouken@6597
   128
    while(touch) {
icculus@7050
   129
        if (touch == leftFingerDown) {
slouken@6597
   130
            /* send mouse up */
slouken@6950
   131
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
icculus@7050
   132
            leftFingerDown = nil;
slouken@6597
   133
        }
slouken@4661
   134
slouken@6436
   135
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
jim@4662
   136
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@6951
   137
        SDL_SendTouch(touchId, (long)touch,
slouken@6951
   138
                      SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
jim@4662
   139
#else
kees@6003
   140
        int i;
kees@6003
   141
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   142
            if (finger[i] == touch) {
slouken@6951
   143
                SDL_SendTouch(touchId, i,
slouken@6951
   144
                              SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
kees@6003
   145
                finger[i] = NULL;
kees@6003
   146
                break;
kees@6003
   147
            }
slouken@5131
   148
        }
jim@4662
   149
#endif
kees@6003
   150
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   151
    }
slouken@2765
   152
}
slouken@2765
   153
kees@6003
   154
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   155
{
slouken@5131
   156
    /*
slouken@5131
   157
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   158
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   159
        all active touches are canceled.
slouken@5131
   160
    */
slouken@5131
   161
    [self touchesEnded: touches withEvent: event];
slouken@2765
   162
}
slouken@2765
   163
kees@6003
   164
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   165
{
slouken@5131
   166
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   167
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   168
slouken@6597
   169
    while (touch) {
icculus@7050
   170
        if (touch == leftFingerDown) {
slouken@6597
   171
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
slouken@4488
   172
slouken@6597
   173
            /* send moved event */
slouken@6950
   174
            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
slouken@6597
   175
        }
jim@4660
   176
slouken@6436
   177
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
jim@4662
   178
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
   179
        SDL_SendTouchMotion(touchId, (long)touch,
slouken@6951
   180
                            locationInView.x, locationInView.y, 1.0f);
jim@4662
   181
#else
kees@6003
   182
        int i;
kees@6003
   183
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   184
            if (finger[i] == touch) {
kees@6003
   185
                SDL_SendTouchMotion(touchId, i,
slouken@6951
   186
                                    locationInView.x, locationInView.y, 1.0f);
kees@6003
   187
                break;
kees@6003
   188
            }
slouken@5131
   189
        }
jim@4662
   190
#endif
kees@6003
   191
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   192
    }
slouken@2765
   193
}
slouken@2765
   194
slouken@2765
   195
/*
slouken@5131
   196
    ---- Keyboard related functionality below this line ----
slouken@2765
   197
*/
slouken@2765
   198
#if SDL_IPHONE_KEYBOARD
slouken@2765
   199
slouken@2765
   200
/* Is the iPhone virtual keyboard visible onscreen? */
kees@6003
   201
- (BOOL)keyboardVisible
kees@6003
   202
{
slouken@5131
   203
    return keyboardVisible;
slouken@2765
   204
}
slouken@2765
   205
slouken@2765
   206
/* Set ourselves up as a UITextFieldDelegate */
kees@6003
   207
- (void)initializeKeyboard
kees@6003
   208
{
slouken@5131
   209
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
slouken@5131
   210
    textField.delegate = self;
slouken@5131
   211
    /* placeholder so there is something to delete! */
kees@6001
   212
    textField.text = @" ";
kees@6001
   213
slouken@5131
   214
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   215
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   216
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   217
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   218
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   219
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   220
    textField.returnKeyType = UIReturnKeyDefault;
kees@6001
   221
    textField.secureTextEntry = NO;
kees@6001
   222
slouken@5131
   223
    textField.hidden = YES;
slouken@5131
   224
    keyboardVisible = NO;
slouken@5131
   225
    /* add the UITextField (hidden) to our view */
slouken@5131
   226
    [self addSubview: textField];
slouken@5131
   227
    [textField release];
slouken@2765
   228
}
slouken@2765
   229
slouken@2765
   230
/* reveal onscreen virtual keyboard */
kees@6003
   231
- (void)showKeyboard
kees@6003
   232
{
slouken@5131
   233
    keyboardVisible = YES;
slouken@5131
   234
    [textField becomeFirstResponder];
slouken@2765
   235
}
slouken@2765
   236
slouken@2765
   237
/* hide onscreen virtual keyboard */
kees@6003
   238
- (void)hideKeyboard
kees@6003
   239
{
slouken@5131
   240
    keyboardVisible = NO;
slouken@5131
   241
    [textField resignFirstResponder];
slouken@2765
   242
}
slouken@2765
   243
slouken@2765
   244
/* UITextFieldDelegate method.  Invoked when user types something. */
kees@6003
   245
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
kees@6003
   246
{
slouken@5131
   247
    if ([string length] == 0) {
slouken@5131
   248
        /* it wants to replace text with nothing, ie a delete */
icculus@7774
   249
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
icculus@7774
   250
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
slouken@5131
   251
    }
slouken@5131
   252
    else {
slouken@5131
   253
        /* go through all the characters in the string we've been sent
slouken@5131
   254
           and convert them to key presses */
slouken@5131
   255
        int i;
kees@6003
   256
        for (i = 0; i < [string length]; i++) {
kees@6001
   257
slouken@5131
   258
            unichar c = [string characterAtIndex: i];
kees@6001
   259
slouken@5131
   260
            Uint16 mod = 0;
slouken@5218
   261
            SDL_Scancode code;
kees@6001
   262
slouken@5131
   263
            if (c < 127) {
slouken@5218
   264
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   265
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   266
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   267
            }
slouken@5131
   268
            else {
slouken@5131
   269
                /* we only deal with ASCII right now */
slouken@5131
   270
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   271
                mod = 0;
slouken@5131
   272
            }
kees@6001
   273
slouken@5131
   274
            if (mod & KMOD_SHIFT) {
slouken@5131
   275
                /* If character uses shift, press shift down */
slouken@5131
   276
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   277
            }
slouken@5131
   278
            /* send a keydown and keyup even for the character */
slouken@5131
   279
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   280
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   281
            if (mod & KMOD_SHIFT) {
slouken@5131
   282
                /* If character uses shift, press shift back up */
slouken@5131
   283
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
kees@6001
   284
            }
slouken@5131
   285
        }
slouken@5461
   286
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   287
    }
slouken@5131
   288
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   289
}
slouken@2765
   290
slouken@2765
   291
/* Terminates the editing session */
kees@6003
   292
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
kees@6003
   293
{
slouken@5134
   294
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@6054
   295
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
slouken@6654
   296
    SDL_StopTextInput();
slouken@5131
   297
    return YES;
slouken@2765
   298
}
slouken@2765
   299
slouken@2765
   300
#endif
slouken@2765
   301
slouken@2765
   302
@end
slouken@2765
   303
slouken@2765
   304
/* iPhone keyboard addition functions */
slouken@2765
   305
#if SDL_IPHONE_KEYBOARD
slouken@2765
   306
slouken@6044
   307
static SDL_uikitview * getWindowView(SDL_Window * window)
kees@6010
   308
{
kees@6010
   309
    if (window == NULL) {
kees@6010
   310
        SDL_SetError("Window does not exist");
kees@6010
   311
        return nil;
kees@6010
   312
    }
kees@6010
   313
kees@6010
   314
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
kees@6010
   315
    SDL_uikitview *view = data != NULL ? data->view : nil;
kees@6010
   316
kees@6010
   317
    if (view == nil) {
kees@6010
   318
        SDL_SetError("Window has no view");
kees@6010
   319
    }
kees@6010
   320
kees@6010
   321
    return view;
kees@6010
   322
}
kees@6010
   323
slouken@6654
   324
SDL_bool UIKit_HasScreenKeyboardSupport(_THIS)
slouken@6392
   325
{
slouken@6392
   326
    return SDL_TRUE;
slouken@6392
   327
}
slouken@6392
   328
slouken@6654
   329
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   330
{
kees@6010
   331
    SDL_uikitview *view = getWindowView(window);
slouken@6654
   332
    if (view != nil) {
slouken@6654
   333
        [view showKeyboard];
slouken@5131
   334
    }
slouken@2765
   335
}
slouken@2765
   336
slouken@6654
   337
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   338
{
kees@6010
   339
    SDL_uikitview *view = getWindowView(window);
slouken@6654
   340
    if (view != nil) {
slouken@6654
   341
        [view hideKeyboard];
kees@6001
   342
    }
slouken@2765
   343
}
slouken@2765
   344
slouken@6392
   345
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
kees@6003
   346
{
kees@6010
   347
    SDL_uikitview *view = getWindowView(window);
kees@6010
   348
    if (view == nil) {
kees@6010
   349
        return 0;
kees@6001
   350
    }
kees@6001
   351
kees@6010
   352
    return view.keyboardVisible;
slouken@2765
   353
}
slouken@2765
   354
slouken@5132
   355
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   356
slouken@6044
   357
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   358
slouken@5132
   359
/* vi: set ts=4 sw=4 expandtab: */