src/video/uikit/SDL_uikitview.m
author Ryan C. Gordon <icculus@icculus.org>
Wed, 03 Apr 2013 11:34:21 -0400
changeset 7050 aa1f44908402
parent 6951 7833f01322b3
child 7111 f4ae9f1233df
child 8460 f483e8bdae3c
permissions -rw-r--r--
Fixed compiler warnings. "leftFingerDown" doesn't need to be an SDL_FingerID.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6079
d7135957b216 Fixed issues with the touch coordinates in landscape mode.
Sam Lantinga <slouken@libsdl.org>
parents: 6054
diff changeset
     1
 /*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6654
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     7
  arising from the use of this software.
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    11
  freely, subject to the following restrictions:
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
5262
b530ef003506 Happy 2011! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    20
*/
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6010
diff changeset
    21
#include "SDL_config.h"
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6010
diff changeset
    22
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6010
diff changeset
    23
#if SDL_VIDEO_DRIVER_UIKIT
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
6518
42948831d16e Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
Sam Lantinga <slouken@libsdl.org>
parents: 6439
diff changeset
    25
#include "SDL_uikitview.h"
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
4490
06c7423f8c60 Updated iPhone keyboard code (which builds and runs on the iPad and iPhone simulator now)
Sam Lantinga <slouken@libsdl.org>
parents: 4488
diff changeset
    27
#include "../../events/SDL_keyboard_c.h"
06c7423f8c60 Updated iPhone keyboard code (which builds and runs on the iPad and iPhone simulator now)
Sam Lantinga <slouken@libsdl.org>
parents: 4488
diff changeset
    28
#include "../../events/SDL_mouse_c.h"
4677
31607094315c Added Iphone project. Iphone multi-touch is now functional.
jimtla
parents: 4662
diff changeset
    29
#include "../../events/SDL_touch_c.h"
4490
06c7423f8c60 Updated iPhone keyboard code (which builds and runs on the iPad and iPhone simulator now)
Sam Lantinga <slouken@libsdl.org>
parents: 4488
diff changeset
    30
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#if SDL_IPHONE_KEYBOARD
6518
42948831d16e Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
Sam Lantinga <slouken@libsdl.org>
parents: 6439
diff changeset
    32
#include "keyinfotable.h"
42948831d16e Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
Sam Lantinga <slouken@libsdl.org>
parents: 6439
diff changeset
    33
#include "SDL_uikitappdelegate.h"
42948831d16e Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
Sam Lantinga <slouken@libsdl.org>
parents: 6439
diff changeset
    34
#include "SDL_uikitmodes.h"
42948831d16e Refactored the UIKit mode code into a separate file so it's cleaner and more consistent with other backends
Sam Lantinga <slouken@libsdl.org>
parents: 6439
diff changeset
    35
#include "SDL_uikitwindow.h"
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
#endif
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
@implementation SDL_uikitview
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    40
- (void)dealloc
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    41
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
    42
    [super dealloc];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    45
- (id)initWithFrame:(CGRect)frame
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    46
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
    47
    self = [super initWithFrame: frame];
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
    48
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
#if SDL_IPHONE_KEYBOARD
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
    50
    [self initializeKeyboard];
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
    51
#endif
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
5445
b2bcc3c36fa3 Enabled multi-touch
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    53
    self.multipleTouchEnabled = YES;
b2bcc3c36fa3 Enabled multi-touch
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    54
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
    55
    touchId = 1;
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
    56
    SDL_AddTouch(touchId, "");
4661
03dcb795c583 Merged changes from the main SDL codebase
Sam Lantinga <slouken@libsdl.org>
parents: 4660 4490
diff changeset
    57
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
    58
    return self;
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
6436
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    62
- (CGPoint)touchLocation:(UITouch *)touch shouldNormalize:(BOOL)normalize
6079
d7135957b216 Fixed issues with the touch coordinates in landscape mode.
Sam Lantinga <slouken@libsdl.org>
parents: 6054
diff changeset
    63
{
d7135957b216 Fixed issues with the touch coordinates in landscape mode.
Sam Lantinga <slouken@libsdl.org>
parents: 6054
diff changeset
    64
    CGPoint point = [touch locationInView: self];
6436
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    65
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    66
    // Get the display scale and apply that to the input coordinates
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    67
    SDL_Window *window = self->viewcontroller.window;
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    68
    SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    69
    SDL_DisplayModeData *displaymodedata = (SDL_DisplayModeData *) display->current_mode.driverdata;
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    70
    
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    71
    if (normalize) {
6439
fbbd640d7867 Fixed touch coordinates with the new UI bounds calculation
Sam Lantinga <slouken@libsdl.org>
parents: 6436
diff changeset
    72
        CGRect bounds = [self bounds];
fbbd640d7867 Fixed touch coordinates with the new UI bounds calculation
Sam Lantinga <slouken@libsdl.org>
parents: 6436
diff changeset
    73
        point.x /= bounds.size.width;
fbbd640d7867 Fixed touch coordinates with the new UI bounds calculation
Sam Lantinga <slouken@libsdl.org>
parents: 6436
diff changeset
    74
        point.y /= bounds.size.height;
6519
fc926eedd78a Fixed issue with touch input on retina displays
Sam Lantinga <slouken@libsdl.org>
parents: 6518
diff changeset
    75
    } else {
fc926eedd78a Fixed issue with touch input on retina displays
Sam Lantinga <slouken@libsdl.org>
parents: 6518
diff changeset
    76
        point.x *= displaymodedata->scale;
fc926eedd78a Fixed issue with touch input on retina displays
Sam Lantinga <slouken@libsdl.org>
parents: 6518
diff changeset
    77
        point.y *= displaymodedata->scale;
6436
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
    78
    }
6079
d7135957b216 Fixed issues with the touch coordinates in landscape mode.
Sam Lantinga <slouken@libsdl.org>
parents: 6054
diff changeset
    79
    return point;
d7135957b216 Fixed issues with the touch coordinates in landscape mode.
Sam Lantinga <slouken@libsdl.org>
parents: 6054
diff changeset
    80
}
d7135957b216 Fixed issues with the touch coordinates in landscape mode.
Sam Lantinga <slouken@libsdl.org>
parents: 6054
diff changeset
    81
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    82
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
    83
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
    84
    NSEnumerator *enumerator = [touches objectEnumerator];
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
    85
    UITouch *touch = (UITouch*)[enumerator nextObject];
5459
881d1e03e381 Fixed touches for the !IPHONE_TOUCH_EFFICIENT_DANGEROUS case
Sam Lantinga <slouken@libsdl.org>
parents: 5445
diff changeset
    86
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
    87
    while (touch) {
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
    88
        if (!leftFingerDown) {
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
    89
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
    90
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
    91
            /* send moved event */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    92
            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
4488
6dc6a2bdd55e Re-implemented single mouse touches on the iPhone/iPad
Sam Lantinga <slouken@libsdl.org>
parents: 4465
diff changeset
    93
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
    94
            /* send mouse down event */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
    95
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
4660
b15e7017409b Added Iphone support (untested)
Jim Grandpre <jim.tla@gmail.com>
parents: 3685
diff changeset
    96
7050
aa1f44908402 Fixed compiler warnings. "leftFingerDown" doesn't need to be an SDL_FingerID.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
    97
            leftFingerDown = touch;
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
    98
        }
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
    99
6436
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
   100
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   101
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   102
        // FIXME: TODO: Using touch as the fingerId is potentially dangerous
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   103
        // It is also much more efficient than storing the UITouch pointer
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   104
        // and comparing it to the incoming event.
7050
aa1f44908402 Fixed compiler warnings. "leftFingerDown" doesn't need to be an SDL_FingerID.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   105
        SDL_SendTouch(touchId, (SDL_FingerID)((size_t)touch),
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   106
                      SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   107
#else
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   108
        int i;
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   109
        for(i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   110
            if (finger[i] == NULL) {
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   111
                finger[i] = touch;
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   112
                SDL_SendTouch(touchId, i,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   113
                              SDL_TRUE, locationInView.x, locationInView.y, 1.0f);
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   114
                break;
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   115
            }
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   116
        }
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   117
#endif
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   118
        touch = (UITouch*)[enumerator nextObject];
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   119
    }
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   121
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   122
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   123
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   124
    NSEnumerator *enumerator = [touches objectEnumerator];
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   125
    UITouch *touch = (UITouch*)[enumerator nextObject];
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   126
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   127
    while(touch) {
7050
aa1f44908402 Fixed compiler warnings. "leftFingerDown" doesn't need to be an SDL_FingerID.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   128
        if (touch == leftFingerDown) {
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   129
            /* send mouse up */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   130
            SDL_SendMouseButton(NULL, SDL_TOUCH_MOUSEID, SDL_RELEASED, SDL_BUTTON_LEFT);
7050
aa1f44908402 Fixed compiler warnings. "leftFingerDown" doesn't need to be an SDL_FingerID.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   131
            leftFingerDown = nil;
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   132
        }
4661
03dcb795c583 Merged changes from the main SDL codebase
Sam Lantinga <slouken@libsdl.org>
parents: 4660 4490
diff changeset
   133
6436
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
   134
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   135
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   136
        SDL_SendTouch(touchId, (long)touch,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   137
                      SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   138
#else
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   139
        int i;
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   140
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   141
            if (finger[i] == touch) {
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   142
                SDL_SendTouch(touchId, i,
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   143
                              SDL_FALSE, locationInView.x, locationInView.y, 1.0f);
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   144
                finger[i] = NULL;
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   145
                break;
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   146
            }
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   147
        }
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   148
#endif
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   149
        touch = (UITouch*)[enumerator nextObject];
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   150
    }
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   153
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   154
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   155
    /*
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   156
        this can happen if the user puts more than 5 touches on the screen
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   157
        at once, or perhaps in other circumstances.  Usually (it seems)
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   158
        all active touches are canceled.
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   159
    */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   160
    [self touchesEnded: touches withEvent: event];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   163
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   164
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   165
    NSEnumerator *enumerator = [touches objectEnumerator];
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   166
    UITouch *touch = (UITouch*)[enumerator nextObject];
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   167
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   168
    while (touch) {
7050
aa1f44908402 Fixed compiler warnings. "leftFingerDown" doesn't need to be an SDL_FingerID.
Ryan C. Gordon <icculus@icculus.org>
parents: 6951
diff changeset
   169
        if (touch == leftFingerDown) {
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   170
            CGPoint locationInView = [self touchLocation:touch shouldNormalize:NO];
4488
6dc6a2bdd55e Re-implemented single mouse touches on the iPhone/iPad
Sam Lantinga <slouken@libsdl.org>
parents: 4465
diff changeset
   171
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   172
            /* send moved event */
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   173
            SDL_SendMouseMotion(NULL, SDL_TOUCH_MOUSEID, 0, locationInView.x, locationInView.y);
6597
d359909f8121 Remember what finger was used for left button down and only send mouse events for that finger.
Sam Lantinga <slouken@libsdl.org>
parents: 6519
diff changeset
   174
        }
4660
b15e7017409b Added Iphone support (untested)
Jim Grandpre <jim.tla@gmail.com>
parents: 3685
diff changeset
   175
6436
29a35c72905a Fixed retina display input scaling
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
   176
        CGPoint locationInView = [self touchLocation:touch shouldNormalize:YES];
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   177
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   178
        SDL_SendTouchMotion(touchId, (long)touch,
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   179
                            locationInView.x, locationInView.y, 1.0f);
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   180
#else
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   181
        int i;
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   182
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   183
            if (finger[i] == touch) {
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   184
                SDL_SendTouchMotion(touchId, i,
6951
7833f01322b3 Updated touch API
Sam Lantinga <slouken@libsdl.org>
parents: 6950
diff changeset
   185
                                    locationInView.x, locationInView.y, 1.0f);
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   186
                break;
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   187
            }
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   188
        }
4662
3c4e0130c9b1 Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
Jim Grandpre <jim.tla@gmail.com>
parents: 4661
diff changeset
   189
#endif
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   190
        touch = (UITouch*)[enumerator nextObject];
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   191
    }
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
/*
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   195
    ---- Keyboard related functionality below this line ----
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   196
*/
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   197
#if SDL_IPHONE_KEYBOARD
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   198
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
/* Is the iPhone virtual keyboard visible onscreen? */
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   200
- (BOOL)keyboardVisible
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   201
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   202
    return keyboardVisible;
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   205
/* Set ourselves up as a UITextFieldDelegate */
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   206
- (void)initializeKeyboard
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   207
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   208
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   209
    textField.delegate = self;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   210
    /* placeholder so there is something to delete! */
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   211
    textField.text = @" ";
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   212
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   213
    /* set UITextInputTrait properties, mostly to defaults */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   214
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   215
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   216
    textField.enablesReturnKeyAutomatically = NO;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   217
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   218
    textField.keyboardType = UIKeyboardTypeDefault;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   219
    textField.returnKeyType = UIReturnKeyDefault;
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   220
    textField.secureTextEntry = NO;
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   221
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   222
    textField.hidden = YES;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   223
    keyboardVisible = NO;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   224
    /* add the UITextField (hidden) to our view */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   225
    [self addSubview: textField];
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   226
    [textField release];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   228
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
/* reveal onscreen virtual keyboard */
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   230
- (void)showKeyboard
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   231
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   232
    keyboardVisible = YES;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   233
    [textField becomeFirstResponder];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   234
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   235
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
/* hide onscreen virtual keyboard */
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   237
- (void)hideKeyboard
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   238
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   239
    keyboardVisible = NO;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   240
    [textField resignFirstResponder];
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   243
/* UITextFieldDelegate method.  Invoked when user types something. */
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   244
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   245
{
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   246
    if ([string length] == 0) {
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   247
        /* it wants to replace text with nothing, ie a delete */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   248
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   249
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   250
    }
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   251
    else {
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   252
        /* go through all the characters in the string we've been sent
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   253
           and convert them to key presses */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   254
        int i;
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   255
        for (i = 0; i < [string length]; i++) {
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   256
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   257
            unichar c = [string characterAtIndex: i];
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   258
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   259
            Uint16 mod = 0;
5218
572a73d71b5f Sheena pointed out that "scancode" and "keysym" are single words and shouldn't be camel-cased.
Sam Lantinga <slouken@libsdl.org>
parents: 5134
diff changeset
   260
            SDL_Scancode code;
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   261
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   262
            if (c < 127) {
5218
572a73d71b5f Sheena pointed out that "scancode" and "keysym" are single words and shouldn't be camel-cased.
Sam Lantinga <slouken@libsdl.org>
parents: 5134
diff changeset
   263
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   264
                code = unicharToUIKeyInfoTable[c].code;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   265
                mod  = unicharToUIKeyInfoTable[c].mod;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   266
            }
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   267
            else {
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   268
                /* we only deal with ASCII right now */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   269
                code = SDL_SCANCODE_UNKNOWN;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   270
                mod = 0;
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   271
            }
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   272
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   273
            if (mod & KMOD_SHIFT) {
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   274
                /* If character uses shift, press shift down */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   275
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   276
            }
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   277
            /* send a keydown and keyup even for the character */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   278
            SDL_SendKeyboardKey(SDL_PRESSED, code);
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   279
            SDL_SendKeyboardKey(SDL_RELEASED, code);
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   280
            if (mod & KMOD_SHIFT) {
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   281
                /* If character uses shift, press shift back up */
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   282
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   283
            }
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   284
        }
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5459
diff changeset
   285
        SDL_SendKeyboardText([string UTF8String]);
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   286
    }
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   287
    return NO; /* don't allow the edit! (keep placeholder text there) */
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   288
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   289
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
/* Terminates the editing session */
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   291
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   292
{
5134
c63b901d97ab Fixed bug #1025 (iphone keyboard doesn't send 'return' and 'backspace' events)
Sam Lantinga <slouken@libsdl.org>
parents: 5132
diff changeset
   293
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
6054
effedf4d675c Need to send a key up, silly.
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
   294
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
6654
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   295
    SDL_StopTextInput();
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   296
    return YES;
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   297
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
#endif
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   300
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
@end
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
/* iPhone keyboard addition functions */
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
#if SDL_IPHONE_KEYBOARD
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6010
diff changeset
   306
static SDL_uikitview * getWindowView(SDL_Window * window)
6010
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   307
{
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   308
    if (window == NULL) {
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   309
        SDL_SetError("Window does not exist");
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   310
        return nil;
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   311
    }
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   312
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   313
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   314
    SDL_uikitview *view = data != NULL ? data->view : nil;
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   315
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   316
    if (view == nil) {
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   317
        SDL_SetError("Window has no view");
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   318
    }
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   319
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   320
    return view;
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   321
}
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   322
6654
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   323
SDL_bool UIKit_HasScreenKeyboardSupport(_THIS)
6392
fa7eb111f994 Fixed bug 1564 - SDL has no function to open a screen keyboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   324
{
fa7eb111f994 Fixed bug 1564 - SDL has no function to open a screen keyboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   325
    return SDL_TRUE;
fa7eb111f994 Fixed bug 1564 - SDL has no function to open a screen keyboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   326
}
fa7eb111f994 Fixed bug 1564 - SDL has no function to open a screen keyboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   327
6654
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   328
void UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   329
{
6010
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   330
    SDL_uikitview *view = getWindowView(window);
6654
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   331
    if (view != nil) {
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   332
        [view showKeyboard];
5131
7b7da52e8775 Fixed spacing
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
   333
    }
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
6654
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   336
void UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   337
{
6010
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   338
    SDL_uikitview *view = getWindowView(window);
6654
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   339
    if (view != nil) {
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6597
diff changeset
   340
        [view hideKeyboard];
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   341
    }
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   342
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   343
6392
fa7eb111f994 Fixed bug 1564 - SDL has no function to open a screen keyboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 6342
diff changeset
   344
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
6003
fddf81967e2d Use a consistent source code format in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 6001
diff changeset
   345
{
6010
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   346
    SDL_uikitview *view = getWindowView(window);
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   347
    if (view == nil) {
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   348
        return 0;
6001
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   349
    }
3ae3d1e58d59 Cleanup out trailing whitespace in src/video/uikit
Kees Bakker <kees@ijzerbout.nl>
parents: 5535
diff changeset
   350
6010
508aa078acc0 Minor code cleanup in uikit/SDL_uikitview.m (no functional changes)
Kees Bakker <kees@ijzerbout.nl>
parents: 6003
diff changeset
   351
    return view.keyboardVisible;
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   352
}
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
5132
5f09cb749d75 Added vim spacing hints
Sam Lantinga <slouken@libsdl.org>
parents: 5131
diff changeset
   354
#endif /* SDL_IPHONE_KEYBOARD */
2765
f55c87ae336b Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   355
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6010
diff changeset
   356
#endif /* SDL_VIDEO_DRIVER_UIKIT */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 6010
diff changeset
   357
5132
5f09cb749d75 Added vim spacing hints
Sam Lantinga <slouken@libsdl.org>
parents: 5131
diff changeset
   358
/* vi: set ts=4 sw=4 expandtab: */