src/video/uikit/SDL_uikitview.m
author Alex Szpakowski <slime73@gmail.com>
Thu, 20 Nov 2014 18:45:04 -0400
branchiOS-improvements
changeset 9524 9de159f9cefd
parent 9519 37ade10d4570
child 9525 64e3f446d6d7
permissions -rw-r--r--
Minor objective-c code style improvements
slouken@6079
     1
 /*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.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
slime73@9487
    38
void _uikit_keyboard_init();
slouken@7962
    39
slime73@9501
    40
@implementation SDL_uikitview {
slime73@9501
    41
slime73@9501
    42
    SDL_TouchID touchId;
slime73@9501
    43
    UITouch *leftFingerDown;
slime73@9501
    44
slime73@9501
    45
#if SDL_IPHONE_KEYBOARD
slime73@9501
    46
    UITextField *textField;
slime73@9501
    47
#endif
slime73@9501
    48
slime73@9501
    49
}
slouken@2765
    50
slime73@9510
    51
@synthesize viewcontroller;
slime73@9510
    52
kees@6003
    53
- (id)initWithFrame:(CGRect)frame
kees@6003
    54
{
slime73@9524
    55
    if (self = [super initWithFrame:frame]) {
slouken@2765
    56
#if SDL_IPHONE_KEYBOARD
slime73@9505
    57
        [self initializeKeyboard];
kees@6001
    58
#endif
slouken@2765
    59
slime73@9505
    60
        self.multipleTouchEnabled = YES;
slouken@5445
    61
slime73@9505
    62
        touchId = 1;
slime73@9505
    63
        SDL_AddTouch(touchId, "");
slime73@9505
    64
    }
slouken@4661
    65
slouken@5131
    66
    return self;
slouken@2765
    67
slouken@2765
    68
}
slouken@2765
    69
slouken@6436
    70
- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
slouken@6079
    71
{
slime73@9524
    72
    CGPoint point = [touch locationInView:self];
slouken@6436
    73
slouken@6436
    74
    if (normalize) {
slime73@9501
    75
        CGRect bounds = self.bounds;
slouken@6439
    76
        point.x /= bounds.size.width;
slouken@6439
    77
        point.y /= bounds.size.height;
slouken@6436
    78
    }
slime73@9488
    79
slouken@6079
    80
    return point;
slouken@6079
    81
}
slouken@6079
    82
kees@6003
    83
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
    84
{
slime73@9487
    85
    for (UITouch *touch in touches) {
slouken@6597
    86
        if (!leftFingerDown) {
slouken@6597
    87
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
kees@6001
    88
slouken@6597
    89
            /* send moved event */
slime73@9510
    90
            SDL_SendMouseMotion(self.viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
slouken@4488
    91
slouken@6597
    92
            /* send mouse down event */
slime73@9510
    93
            SDL_SendMouseButton(self.viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
jim@4660
    94
icculus@7050
    95
            leftFingerDown = touch;
slouken@6597
    96
        }
slouken@6597
    97
slouken@6436
    98
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
icculus@7050
    99
        SDL_SendTouch(touchId, (SDL_FingerID)((size_t)touch),
slouken@6951
   100
                      SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
slouken@5131
   101
    }
slouken@2765
   102
}
slouken@2765
   103
kees@6003
   104
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   105
{
slime73@9487
   106
    for (UITouch *touch in touches) {
icculus@7050
   107
        if (touch == leftFingerDown) {
slouken@6597
   108
            /* send mouse up */
slime73@9510
   109
            SDL_SendMouseButton(self.viewcontroller.window, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
icculus@7050
   110
            leftFingerDown = nil;
slouken@6597
   111
        }
slouken@4661
   112
slouken@6436
   113
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
slime73@9501
   114
        SDL_SendTouch(touchId, (SDL_FingerID)((size_t)touch),
slouken@6951
   115
                      SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
slouken@5131
   116
    }
slouken@2765
   117
}
slouken@2765
   118
kees@6003
   119
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   120
{
slouken@5131
   121
    /*
slouken@5131
   122
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   123
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   124
        all active touches are canceled.
slouken@5131
   125
    */
slime73@9524
   126
    [self touchesEnded:touches withEvent:event];
slouken@2765
   127
}
slouken@2765
   128
kees@6003
   129
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   130
{
slime73@9487
   131
    for (UITouch *touch in touches) {
icculus@7050
   132
        if (touch == leftFingerDown) {
slouken@6597
   133
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
slouken@4488
   134
slouken@6597
   135
            /* send moved event */
slime73@9510
   136
            SDL_SendMouseMotion(self.viewcontroller.window, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
slouken@6597
   137
        }
jim@4660
   138
slouken@6436
   139
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
slime73@9501
   140
        SDL_SendTouchMotion(touchId, (SDL_FingerID)((size_t)touch),
slouken@6951
   141
                            locationInView.x, locationInView.y, 1.0f);
slouken@5131
   142
    }
slouken@2765
   143
}
slouken@2765
   144
slouken@2765
   145
/*
slouken@5131
   146
    ---- Keyboard related functionality below this line ----
slouken@2765
   147
*/
slouken@2765
   148
#if SDL_IPHONE_KEYBOARD
slouken@2765
   149
slime73@9501
   150
@synthesize textInputRect;
slime73@9501
   151
@synthesize keyboardHeight;
slime73@9501
   152
@synthesize keyboardVisible;
slouken@2765
   153
slouken@2765
   154
/* Set ourselves up as a UITextFieldDelegate */
kees@6003
   155
- (void)initializeKeyboard
kees@6003
   156
{
slime73@9524
   157
    textField = [[UITextField alloc] initWithFrame:CGRectZero];
slouken@5131
   158
    textField.delegate = self;
slouken@5131
   159
    /* placeholder so there is something to delete! */
kees@6001
   160
    textField.text = @" ";
kees@6001
   161
slouken@5131
   162
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   163
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   164
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   165
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   166
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   167
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   168
    textField.returnKeyType = UIReturnKeyDefault;
kees@6001
   169
    textField.secureTextEntry = NO;
kees@6001
   170
slouken@5131
   171
    textField.hidden = YES;
slouken@5131
   172
    keyboardVisible = NO;
slouken@5131
   173
    /* add the UITextField (hidden) to our view */
slime73@9524
   174
    [self addSubview:textField];
slouken@7962
   175
    
slouken@7962
   176
    _uikit_keyboard_init();
slouken@2765
   177
}
slouken@2765
   178
slouken@2765
   179
/* reveal onscreen virtual keyboard */
kees@6003
   180
- (void)showKeyboard
kees@6003
   181
{
slouken@5131
   182
    keyboardVisible = YES;
slouken@5131
   183
    [textField becomeFirstResponder];
slouken@2765
   184
}
slouken@2765
   185
slouken@2765
   186
/* hide onscreen virtual keyboard */
kees@6003
   187
- (void)hideKeyboard
kees@6003
   188
{
slouken@5131
   189
    keyboardVisible = NO;
slouken@5131
   190
    [textField resignFirstResponder];
slouken@2765
   191
}
slouken@2765
   192
slouken@2765
   193
/* UITextFieldDelegate method.  Invoked when user types something. */
kees@6003
   194
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
kees@6003
   195
{
slouken@5131
   196
    if ([string length] == 0) {
slouken@5131
   197
        /* it wants to replace text with nothing, ie a delete */
icculus@7774
   198
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
icculus@7774
   199
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
slouken@5131
   200
    }
slouken@5131
   201
    else {
slouken@5131
   202
        /* go through all the characters in the string we've been sent
slouken@5131
   203
           and convert them to key presses */
slouken@5131
   204
        int i;
kees@6003
   205
        for (i = 0; i < [string length]; i++) {
kees@6001
   206
slime73@9524
   207
            unichar c = [string characterAtIndex:i];
kees@6001
   208
slouken@5131
   209
            Uint16 mod = 0;
slouken@5218
   210
            SDL_Scancode code;
kees@6001
   211
slouken@5131
   212
            if (c < 127) {
slouken@5218
   213
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   214
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   215
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   216
            }
slouken@5131
   217
            else {
slouken@5131
   218
                /* we only deal with ASCII right now */
slouken@5131
   219
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   220
                mod = 0;
slouken@5131
   221
            }
kees@6001
   222
slouken@5131
   223
            if (mod & KMOD_SHIFT) {
slouken@5131
   224
                /* If character uses shift, press shift down */
slouken@5131
   225
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   226
            }
slouken@5131
   227
            /* send a keydown and keyup even for the character */
slouken@5131
   228
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   229
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   230
            if (mod & KMOD_SHIFT) {
slouken@5131
   231
                /* If character uses shift, press shift back up */
slouken@5131
   232
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
kees@6001
   233
            }
slouken@5131
   234
        }
slouken@5461
   235
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   236
    }
slouken@5131
   237
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   238
}
slouken@2765
   239
slouken@2765
   240
/* Terminates the editing session */
kees@6003
   241
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
kees@6003
   242
{
slouken@5134
   243
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@6054
   244
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
slouken@6654
   245
    SDL_StopTextInput();
slouken@5131
   246
    return YES;
slouken@2765
   247
}
slouken@2765
   248
slouken@2765
   249
#endif
slouken@2765
   250
slouken@2765
   251
@end
slouken@2765
   252
slouken@2765
   253
/* iPhone keyboard addition functions */
slouken@2765
   254
#if SDL_IPHONE_KEYBOARD
slouken@2765
   255
slouken@6044
   256
static SDL_uikitview * getWindowView(SDL_Window * window)
kees@6010
   257
{
kees@6010
   258
    if (window == NULL) {
kees@6010
   259
        SDL_SetError("Window does not exist");
kees@6010
   260
        return nil;
kees@6010
   261
    }
kees@6010
   262
slime73@9510
   263
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
slime73@9510
   264
    SDL_uikitview *view = data != nil ? data.view : nil;
kees@6010
   265
kees@6010
   266
    if (view == nil) {
kees@6010
   267
        SDL_SetError("Window has no view");
kees@6010
   268
    }
kees@6010
   269
kees@6010
   270
    return view;
kees@6010
   271
}
kees@6010
   272
slouken@6654
   273
SDL_bool UIKit_HasScreenKeyboardSupport(_THIS)
slouken@6392
   274
{
slouken@6392
   275
    return SDL_TRUE;
slouken@6392
   276
}
slouken@6392
   277
slouken@6654
   278
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   279
{
slime73@9506
   280
    @autoreleasepool {
slime73@9506
   281
        SDL_uikitview *view = getWindowView(window);
slime73@9506
   282
        if (view != nil) {
slime73@9506
   283
            [view showKeyboard];
slime73@9506
   284
        }
slouken@5131
   285
    }
slouken@2765
   286
}
slouken@2765
   287
slouken@6654
   288
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   289
{
slime73@9506
   290
    @autoreleasepool {
slime73@9506
   291
        SDL_uikitview *view = getWindowView(window);
slime73@9506
   292
        if (view != nil) {
slime73@9506
   293
            [view hideKeyboard];
slime73@9506
   294
        }
kees@6001
   295
    }
slouken@2765
   296
}
slouken@2765
   297
slouken@6392
   298
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
kees@6003
   299
{
slime73@9506
   300
    @autoreleasepool {
slime73@9506
   301
        SDL_uikitview *view = getWindowView(window);
slime73@9506
   302
        if (view == nil) {
slime73@9506
   303
            return 0;
slime73@9506
   304
        }
slime73@9506
   305
slime73@9506
   306
        return view.isKeyboardVisible;
kees@6001
   307
    }
slouken@2765
   308
}
slouken@2765
   309
slouken@7962
   310
slouken@7962
   311
void _uikit_keyboard_update() {
slouken@7962
   312
    SDL_Window *window = SDL_GetFocusWindow();
slouken@7962
   313
    if (!window) { return; }
slime73@9510
   314
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
slouken@7962
   315
    if (!data) { return; }
slime73@9510
   316
    SDL_uikitview *view = data.view;
slouken@7962
   317
    if (!view) { return; }
slime73@9510
   318
slouken@7962
   319
    SDL_Rect r = view.textInputRect;
slouken@7962
   320
    int height = view.keyboardHeight;
slouken@7962
   321
    int offsetx = 0;
slouken@7962
   322
    int offsety = 0;
slouken@7962
   323
    if (height) {
slouken@7962
   324
        int sw,sh;
slouken@7962
   325
        SDL_GetWindowSize(window,&sw,&sh);
slouken@7962
   326
        int bottom = (r.y + r.h);
slouken@7962
   327
        int kbottom = sh - height;
slouken@7962
   328
        if (kbottom < bottom) {
slouken@7962
   329
            offsety = kbottom-bottom;
slouken@7962
   330
        }
slouken@7962
   331
    }
slouken@7962
   332
    UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
slouken@7962
   333
    if (ui_orient == UIInterfaceOrientationLandscapeLeft) {
slouken@7962
   334
        int tmp = offsetx; offsetx = offsety; offsety = tmp;
slouken@7962
   335
    }
slouken@7962
   336
    if (ui_orient == UIInterfaceOrientationLandscapeRight) {
slouken@7962
   337
        offsety = -offsety;
slouken@7962
   338
        int tmp = offsetx; offsetx = offsety; offsety = tmp;
slouken@7962
   339
    }
slouken@7962
   340
    if (ui_orient == UIInterfaceOrientationPortraitUpsideDown) {
slouken@7962
   341
        offsety = -offsety;
slouken@7962
   342
    }
slouken@8891
   343
slouken@7962
   344
    view.frame = CGRectMake(offsetx,offsety,view.frame.size.width,view.frame.size.height);
slouken@7962
   345
}
slouken@7962
   346
slouken@7962
   347
void _uikit_keyboard_set_height(int height) {
slouken@7962
   348
    SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
slouken@7962
   349
    if (view == nil) {
slime73@9503
   350
        return;
slouken@7962
   351
    }
slime73@9503
   352
slime73@9503
   353
    view.keyboardVisible = height > 0;
slouken@7962
   354
    view.keyboardHeight = height;
slouken@7962
   355
    _uikit_keyboard_update();
slouken@7962
   356
}
slouken@7962
   357
slouken@7962
   358
void _uikit_keyboard_init() {
slouken@7962
   359
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slouken@7962
   360
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
slouken@7962
   361
    [center addObserverForName:UIKeyboardWillShowNotification
slouken@7962
   362
                        object:nil
slouken@7962
   363
                         queue:queue
slouken@7962
   364
                    usingBlock:^(NSNotification *notification) {
slouken@7962
   365
                        int height = 0;
slime73@9499
   366
                        CGSize keyboardSize = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
slouken@7962
   367
                        height = keyboardSize.height;
slouken@7962
   368
                        UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
slouken@7962
   369
                        if (ui_orient == UIInterfaceOrientationLandscapeRight || ui_orient == UIInterfaceOrientationLandscapeLeft) {
slouken@7962
   370
                            height = keyboardSize.width;
slouken@7962
   371
                        }
slouken@7962
   372
                        _uikit_keyboard_set_height(height);
slouken@7962
   373
                    }
slouken@7962
   374
     ];
slouken@7962
   375
    [center addObserverForName:UIKeyboardDidHideNotification
slouken@7962
   376
                        object:nil
slouken@7962
   377
                         queue:queue
slouken@7962
   378
                    usingBlock:^(NSNotification *notification) {
slouken@7962
   379
                        _uikit_keyboard_set_height(0);
slouken@7962
   380
                    }
slouken@7962
   381
     ];
slouken@7962
   382
}
slouken@7962
   383
slouken@7962
   384
void
slouken@7962
   385
UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
slouken@7962
   386
{
slouken@7962
   387
    if (!rect) {
slouken@7962
   388
        SDL_InvalidParamError("rect");
slouken@7962
   389
        return;
slouken@7962
   390
    }
slime73@9506
   391
slime73@9506
   392
    @autoreleasepool {
slime73@9506
   393
        SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
slime73@9506
   394
        if (view == nil) {
slime73@9506
   395
            return;
slime73@9506
   396
        }
slime73@9506
   397
slime73@9506
   398
        view.textInputRect = *rect;
slouken@7962
   399
    }
slouken@7962
   400
}
slouken@7962
   401
slouken@7962
   402
slouken@5132
   403
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   404
slouken@6044
   405
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   406
slouken@5132
   407
/* vi: set ts=4 sw=4 expandtab: */