src/video/uikit/SDL_uikitview.m
author Alex Szpakowski <slime73@gmail.com>
Fri, 21 Nov 2014 03:29:57 -0400
branchiOS-improvements
changeset 9525 64e3f446d6d7
parent 9524 9de159f9cefd
child 9527 bbd9326ecacf
permissions -rw-r--r--
Added debug labels for the OpenGL ES objects created with SDL_GL_CreateContext on iOS.
More misc. code cleanup.
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
{
slime73@9525
   196
    NSUInteger len = string.length;
slime73@9525
   197
slime73@9525
   198
    if (len == 0) {
slouken@5131
   199
        /* it wants to replace text with nothing, ie a delete */
icculus@7774
   200
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
icculus@7774
   201
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
slime73@9525
   202
    } else {
slouken@5131
   203
        /* go through all the characters in the string we've been sent
slouken@5131
   204
           and convert them to key presses */
slime73@9525
   205
        for (int i = 0; i < len; i++) {
slime73@9524
   206
            unichar c = [string characterAtIndex:i];
slouken@5131
   207
            Uint16 mod = 0;
slouken@5218
   208
            SDL_Scancode code;
kees@6001
   209
slouken@5131
   210
            if (c < 127) {
slouken@5218
   211
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   212
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   213
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   214
            }
slouken@5131
   215
            else {
slouken@5131
   216
                /* we only deal with ASCII right now */
slouken@5131
   217
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   218
                mod = 0;
slouken@5131
   219
            }
kees@6001
   220
slouken@5131
   221
            if (mod & KMOD_SHIFT) {
slouken@5131
   222
                /* If character uses shift, press shift down */
slouken@5131
   223
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   224
            }
slime73@9525
   225
slouken@5131
   226
            /* send a keydown and keyup even for the character */
slouken@5131
   227
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   228
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slime73@9525
   229
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
        }
slime73@9525
   235
slouken@5461
   236
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   237
    }
slime73@9525
   238
slouken@5131
   239
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   240
}
slouken@2765
   241
slouken@2765
   242
/* Terminates the editing session */
kees@6003
   243
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
kees@6003
   244
{
slouken@5134
   245
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@6054
   246
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
slouken@6654
   247
    SDL_StopTextInput();
slouken@5131
   248
    return YES;
slouken@2765
   249
}
slouken@2765
   250
slouken@2765
   251
#endif
slouken@2765
   252
slouken@2765
   253
@end
slouken@2765
   254
slouken@2765
   255
/* iPhone keyboard addition functions */
slouken@2765
   256
#if SDL_IPHONE_KEYBOARD
slouken@2765
   257
slouken@6044
   258
static SDL_uikitview * getWindowView(SDL_Window * window)
kees@6010
   259
{
kees@6010
   260
    if (window == NULL) {
kees@6010
   261
        SDL_SetError("Window does not exist");
kees@6010
   262
        return nil;
kees@6010
   263
    }
kees@6010
   264
slime73@9510
   265
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
slime73@9510
   266
    SDL_uikitview *view = data != nil ? data.view : nil;
kees@6010
   267
kees@6010
   268
    if (view == nil) {
kees@6010
   269
        SDL_SetError("Window has no view");
kees@6010
   270
    }
kees@6010
   271
kees@6010
   272
    return view;
kees@6010
   273
}
kees@6010
   274
slouken@6654
   275
SDL_bool UIKit_HasScreenKeyboardSupport(_THIS)
slouken@6392
   276
{
slouken@6392
   277
    return SDL_TRUE;
slouken@6392
   278
}
slouken@6392
   279
slouken@6654
   280
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   281
{
slime73@9506
   282
    @autoreleasepool {
slime73@9506
   283
        SDL_uikitview *view = getWindowView(window);
slime73@9506
   284
        if (view != nil) {
slime73@9506
   285
            [view showKeyboard];
slime73@9506
   286
        }
slouken@5131
   287
    }
slouken@2765
   288
}
slouken@2765
   289
slouken@6654
   290
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   291
{
slime73@9506
   292
    @autoreleasepool {
slime73@9506
   293
        SDL_uikitview *view = getWindowView(window);
slime73@9506
   294
        if (view != nil) {
slime73@9506
   295
            [view hideKeyboard];
slime73@9506
   296
        }
kees@6001
   297
    }
slouken@2765
   298
}
slouken@2765
   299
slouken@6392
   300
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
kees@6003
   301
{
slime73@9506
   302
    @autoreleasepool {
slime73@9506
   303
        SDL_uikitview *view = getWindowView(window);
slime73@9506
   304
        if (view == nil) {
slime73@9506
   305
            return 0;
slime73@9506
   306
        }
slime73@9506
   307
slime73@9506
   308
        return view.isKeyboardVisible;
kees@6001
   309
    }
slouken@2765
   310
}
slouken@2765
   311
slouken@7962
   312
slouken@7962
   313
void _uikit_keyboard_update() {
slouken@7962
   314
    SDL_Window *window = SDL_GetFocusWindow();
slouken@7962
   315
    if (!window) { return; }
slime73@9510
   316
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
slouken@7962
   317
    if (!data) { return; }
slime73@9510
   318
    SDL_uikitview *view = data.view;
slouken@7962
   319
    if (!view) { return; }
slime73@9510
   320
slouken@7962
   321
    SDL_Rect r = view.textInputRect;
slouken@7962
   322
    int height = view.keyboardHeight;
slouken@7962
   323
    int offsetx = 0;
slouken@7962
   324
    int offsety = 0;
slouken@7962
   325
    if (height) {
slouken@7962
   326
        int sw,sh;
slouken@7962
   327
        SDL_GetWindowSize(window,&sw,&sh);
slouken@7962
   328
        int bottom = (r.y + r.h);
slouken@7962
   329
        int kbottom = sh - height;
slouken@7962
   330
        if (kbottom < bottom) {
slouken@7962
   331
            offsety = kbottom-bottom;
slouken@7962
   332
        }
slouken@7962
   333
    }
slouken@7962
   334
    UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
slouken@7962
   335
    if (ui_orient == UIInterfaceOrientationLandscapeLeft) {
slouken@7962
   336
        int tmp = offsetx; offsetx = offsety; offsety = tmp;
slouken@7962
   337
    }
slouken@7962
   338
    if (ui_orient == UIInterfaceOrientationLandscapeRight) {
slouken@7962
   339
        offsety = -offsety;
slouken@7962
   340
        int tmp = offsetx; offsetx = offsety; offsety = tmp;
slouken@7962
   341
    }
slouken@7962
   342
    if (ui_orient == UIInterfaceOrientationPortraitUpsideDown) {
slouken@7962
   343
        offsety = -offsety;
slouken@7962
   344
    }
slouken@8891
   345
slouken@7962
   346
    view.frame = CGRectMake(offsetx,offsety,view.frame.size.width,view.frame.size.height);
slouken@7962
   347
}
slouken@7962
   348
slouken@7962
   349
void _uikit_keyboard_set_height(int height) {
slouken@7962
   350
    SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
slouken@7962
   351
    if (view == nil) {
slime73@9503
   352
        return;
slouken@7962
   353
    }
slime73@9503
   354
slime73@9503
   355
    view.keyboardVisible = height > 0;
slouken@7962
   356
    view.keyboardHeight = height;
slouken@7962
   357
    _uikit_keyboard_update();
slouken@7962
   358
}
slouken@7962
   359
slouken@7962
   360
void _uikit_keyboard_init() {
slouken@7962
   361
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slouken@7962
   362
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
slouken@7962
   363
    [center addObserverForName:UIKeyboardWillShowNotification
slouken@7962
   364
                        object:nil
slouken@7962
   365
                         queue:queue
slouken@7962
   366
                    usingBlock:^(NSNotification *notification) {
slouken@7962
   367
                        int height = 0;
slime73@9499
   368
                        CGSize keyboardSize = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
slouken@7962
   369
                        height = keyboardSize.height;
slouken@7962
   370
                        UIInterfaceOrientation ui_orient = [[UIApplication sharedApplication] statusBarOrientation];
slouken@7962
   371
                        if (ui_orient == UIInterfaceOrientationLandscapeRight || ui_orient == UIInterfaceOrientationLandscapeLeft) {
slouken@7962
   372
                            height = keyboardSize.width;
slouken@7962
   373
                        }
slouken@7962
   374
                        _uikit_keyboard_set_height(height);
slouken@7962
   375
                    }
slouken@7962
   376
     ];
slouken@7962
   377
    [center addObserverForName:UIKeyboardDidHideNotification
slouken@7962
   378
                        object:nil
slouken@7962
   379
                         queue:queue
slouken@7962
   380
                    usingBlock:^(NSNotification *notification) {
slouken@7962
   381
                        _uikit_keyboard_set_height(0);
slouken@7962
   382
                    }
slouken@7962
   383
     ];
slouken@7962
   384
}
slouken@7962
   385
slouken@7962
   386
void
slouken@7962
   387
UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
slouken@7962
   388
{
slouken@7962
   389
    if (!rect) {
slouken@7962
   390
        SDL_InvalidParamError("rect");
slouken@7962
   391
        return;
slouken@7962
   392
    }
slime73@9506
   393
slime73@9506
   394
    @autoreleasepool {
slime73@9506
   395
        SDL_uikitview *view = getWindowView(SDL_GetFocusWindow());
slime73@9506
   396
        if (view == nil) {
slime73@9506
   397
            return;
slime73@9506
   398
        }
slime73@9506
   399
slime73@9506
   400
        view.textInputRect = *rect;
slouken@7962
   401
    }
slouken@7962
   402
}
slouken@7962
   403
slouken@7962
   404
slouken@5132
   405
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   406
slouken@6044
   407
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   408
slouken@5132
   409
/* vi: set ts=4 sw=4 expandtab: */