cocoaTouch/SDLOverrides/SDL_uikitview.m
author koda
Sun, 31 Jan 2010 22:43:09 +0000
changeset 2734 fb9ad1587054
parent 2725 89908847b155
child 2738 bfccb2ec4334
permissions -rw-r--r--
smaller improvements in mouse handling
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     1
/*
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     2
 SDL - Simple DirectMedia Layer
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     3
 Copyright (C) 1997-2009 Sam Lantinga
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     4
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     5
 This library is free software; you can redistribute it and/or
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     6
 modify it under the terms of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     7
 License as published by the Free Software Foundation; either
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     8
 version 2.1 of the License, or (at your option) any later version.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
     9
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    10
 This library is distributed in the hope that it will be useful,
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    13
 Lesser General Public License for more details.
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    14
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    15
 You should have received a copy of the GNU Lesser General Public
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    16
 License along with this library; if not, write to the Free Software
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    18
 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    19
 Sam Lantinga, mods for Hedgewars by Vittorio Giovara
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    20
 slouken@libsdl.org, vittorio.giovara@gmail.com
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    21
 */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    22
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    23
#include "PascalImports.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    24
#import "SDL_uikitview.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    25
#import "SDL_uikitappdelegate.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    26
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    27
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    28
#import "SDL_keyboard_c.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    29
#import "keyinfotable.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    30
#import "SDL_uikitwindow.h"
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    31
#endif
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    32
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    33
@implementation SDL_uikitview
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    34
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    35
// they have to be global variables to allow showControls() to use them
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    36
UIButton *attackButton, *menuButton;
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    37
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
    38
-(void) dealloc {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    39
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    40
	SDL_DelKeyboard(0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    41
	[textField release];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    42
#endif
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    43
	[menuButton release];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    44
	[attackButton release];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    45
	[super dealloc];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    46
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    47
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    48
- (id)initWithFrame:(CGRect)frame {
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    49
	// the addTarget parameter for the buttons below is set like that because
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    50
	// this object is inherited by SDL_openglesview.m which is the one allocated by SDL.
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    51
	// We select this class with [self superclass] and call the selectors with "+" because
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    52
	// they are superclass methods 
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
    53
	self = [super initWithFrame:frame];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    54
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    55
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    56
	[self initializeKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    57
#endif	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    58
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    59
	int i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    60
	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    61
        mice[i].id = i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    62
		mice[i].driverdata = NULL;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    63
		SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    64
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    65
2724
601158aaa201 redo LANDSCAPE MODE in a saner way with lots of fps
koda
parents: 2714
diff changeset
    66
	self.multipleTouchEnabled = YES;
601158aaa201 redo LANDSCAPE MODE in a saner way with lots of fps
koda
parents: 2714
diff changeset
    67
601158aaa201 redo LANDSCAPE MODE in a saner way with lots of fps
koda
parents: 2714
diff changeset
    68
	// custom code
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    69
	attackButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 480, 260,50)];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    70
	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
2705
2b5625c4ec16 fix a nasty 196 bytes memory leak in engine, plus other stuff for iphone frontend
koda
parents: 2701
diff changeset
    71
	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateHighlighted];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    72
	[attackButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchDown];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    73
	[attackButton addTarget:[self superclass] action:@selector(attackButtonReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    74
	[self addSubview:attackButton];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    75
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    76
	menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 480, 30,50)];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    77
	[menuButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    78
	[menuButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchUpInside];
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
    79
	[menuButton addTarget:[self superclass] action:@selector(attackButtonReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    80
	[self addSubview:menuButton];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    81
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    82
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    83
	return self;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    84
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    85
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
    86
#pragma mark -
2690
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
    87
#pragma mark Exported functions for FreePascal
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
    88
const char* IPH_getDocumentsPath() {
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
    89
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
    90
	NSString *documentsDirectory = [paths objectAtIndex: 0];
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
    91
	return [documentsDirectory UTF8String];
2690
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
    92
}
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
    93
8e83c7e31720 move mixed functions in PascalExports
koda
parents: 2689
diff changeset
    94
void IPH_showControls (void) {
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    95
	NSLog(@"Showing controls");
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
    96
	/*
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
    97
	[UIView beginAnimations:nil context:NULL];
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    98
	[UIView setAnimationDuration:0.5];
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
    99
	attackButton.frame = CGRectMake(30, 430, 260, 50);
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   100
	menuButton.frame = CGRectMake(0, 430, 30, 50);
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   101
	[UIView commitAnimations];
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   102
	*/
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   103
}
2689
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   104
dfda97c153a4 iphone overlay button animation
koda
parents: 2688
diff changeset
   105
#pragma mark -
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   106
#pragma mark Superclass methods
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   107
+(void) attackButtonPressed {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   108
	HW_shoot();
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   109
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   110
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   111
+(void) attackButtonReleased {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   112
	HW_allKeysUp();
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   113
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   114
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   115
#pragma mark -
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   116
#pragma mark Custom SDL_UIView input handling
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   117
#define kMinimumPinchDelta	30
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   118
#define kMinimumGestureLength	10
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   119
#define kMaximumVariance	3
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   120
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   121
// we override default touch input to implement our own gestures
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   122
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   123
	/*
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   124
	NSEnumerator *enumerator = [touches objectEnumerator];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   125
	UITouch *touch =(UITouch*)[enumerator nextObject];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   126
	
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   127
	// associate touches with mice, so long as we have slots 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   128
	int i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   129
	int found = 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   130
	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   131
	
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   132
		// check if this mouse is already tracking a touch 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   133
		if (mice[i].driverdata != NULL) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   134
			continue;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   135
		}
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   136
			
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   137
		// mouse not associated with anything right now, associate the touch with this mouse
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   138
		found = 1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   139
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   140
		// save old mouse so we can switch back 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   141
		int oldMouse = SDL_SelectMouse(-1);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   142
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   143
		// select this slot's mouse 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   144
		SDL_SelectMouse(i);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   145
		CGPoint locationInView = [touch locationInView: self];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   146
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   147
		CGFloat oldX = locationInView.x;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   148
		locationInView.x = locationInView.y;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   149
		locationInView.y = 320 - oldX;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   150
		
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   151
		// set driver data to touch object, we'll use touch object later 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   152
		mice[i].driverdata = [touch retain];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   153
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   154
		// send moved event 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   155
		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   156
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   157
		// send mouse down event 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   158
		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   159
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   160
		// re-calibrate relative mouse motion 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   161
		SDL_GetRelativeMouseState(i, NULL, NULL);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   162
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   163
		// grab next touch 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   164
		touch = (UITouch*)[enumerator nextObject]; 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   165
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   166
		// switch back to our old mouse 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   167
		SDL_SelectMouse(oldMouse);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   168
		
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   169
	}
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   170
	*/
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   171
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   172
	UITouch *touch = [touches anyObject];
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   173
	
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   174
	switch ([touches count]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   175
		case 1:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   176
			gestureStartPoint = [touch locationInView:self];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   177
			initialDistanceForPinching = 0;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   178
			switch ([touch tapCount]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   179
				case 1:
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   180
					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   181
							      (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   182
					HW_click();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   183
					break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   184
				case 2:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   185
					HW_ammoMenu();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   186
					break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   187
				default:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   188
					break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   189
			}
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   190
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   191
		case 2:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   192
			if (2 == [touch tapCount]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   193
				HW_zoomReset();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   194
			}
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   195
			
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   196
			// pinching
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   197
			NSArray *twoTouches = [touches allObjects];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   198
			UITouch *first = [twoTouches objectAtIndex:0];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   199
			UITouch *second = [twoTouches objectAtIndex:1];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   200
			initialDistanceForPinching = distanceBetweenPoints([first locationInView:self], [second locationInView:self]);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   201
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   202
		default:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   203
			break;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   204
	}
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   205
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   206
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   207
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   208
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   209
	initialDistanceForPinching = 0;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   210
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   211
	HW_allKeysUp();
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   212
	/*
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   213
	NSEnumerator *enumerator = [touches objectEnumerator];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   214
	UITouch *touch=nil;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   215
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   216
	while(touch = (UITouch *)[enumerator nextObject]) {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   217
		// search for the mouse slot associated with this touch 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   218
		int i, found = NO;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   219
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   220
			if (mice[i].driverdata == touch) {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   221
				// found the mouse associate with the touch 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   222
				[(UITouch*)(mice[i].driverdata) release];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   223
				mice[i].driverdata = NULL;
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   224
				// send mouse up 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   225
				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   226
				// discontinue search for this touch 
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   227
				found = YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   228
			}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   229
		}
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   230
	}
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   231
	*/
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   232
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   233
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   234
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   235
	// this can happen if the user puts more than 5 touches on the screen
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   236
	// at once, or perhaps in other circumstances.  Usually (it seems)
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   237
	// all active touches are canceled.
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   238
	[self touchesEnded:touches withEvent:event];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   239
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   240
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   241
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   242
	NSArray *twoTouches;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   243
	CGPoint currentPosition;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   244
	UITouch *touch = [touches anyObject];
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   245
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   246
	switch ([touches count]) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   247
		case 1:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   248
			currentPosition = [touch locationInView:self];
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   249
			// panning
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   250
			SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   251
							(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   252
			// remember that we have x and y inverted
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   253
			/* temporarily disabling hog movements for camera panning testing
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   254
			 CGFloat vertDiff = gestureStartPoint.x - currentPosition.x;
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   255
			CGFloat horizDiff = gestureStartPoint.y - currentPosition.y;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   256
			CGFloat deltaX = fabsf(vertDiff);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   257
			CGFloat deltaY = fabsf(horizDiff);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   258
			
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   259
			if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   260
				NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   261
				if (horizDiff > 0) HW_walkLeft();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   262
				else HW_walkRight();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   263
			} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   264
				NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   265
				if (vertDiff < 0) HW_aimUp();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   266
				else HW_aimDown();
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   267
			}
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   268
			*/
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   269
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   270
		case 2:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   271
			twoTouches = [touches allObjects];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   272
			UITouch *first = [twoTouches objectAtIndex:0];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   273
			UITouch *second = [twoTouches objectAtIndex:1];
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   274
			CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self], [second locationInView:self]);
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   275
			
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   276
			if (0 == initialDistanceForPinching) 
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   277
				initialDistanceForPinching = currentDistanceOfPinching;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   278
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   279
			if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) {
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   280
				NSLog(@"Outward pinch detected");
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   281
				HW_zoomOut();
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   282
			} else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta){
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   283
				NSLog(@"Inward pinch detected");
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   284
				HW_zoomIn();
2734
fb9ad1587054 smaller improvements in mouse handling
koda
parents: 2725
diff changeset
   285
			} 			
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   286
			currentDistanceOfPinching = initialDistanceForPinching;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   287
			break;
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   288
		default:
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   289
			break;
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   290
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   291
	
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   292
	/*
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   293
	NSEnumerator *enumerator = [touches objectEnumerator];
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   294
	 UITouch *touch=nil;while(touch = (UITouch *)[enumerator nextObject]) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   295
		// try to find the mouse associated with this touch 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   296
		int i, found = NO;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   297
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   298
			if (mice[i].driverdata == touch) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   299
				// found proper mouse 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   300
				CGPoint locationInView = [touch locationInView: self];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   301
				// send moved event 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   302
				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   303
				// discontinue search 
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   304
				found = YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   305
			}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   306
		}
2725
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   307
	}
89908847b155 input handling reworked, still a lot of bugs in mouse movement
koda
parents: 2724
diff changeset
   308
	*/
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   309
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   310
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   311
#pragma mark -
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   312
#pragma mark default routines
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   313
/*
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   314
	---- Keyboard related functionality below this line ----
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   315
*/
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   316
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   317
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   318
/* Is the iPhone virtual keyboard visible onscreen? */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   319
- (BOOL)keyboardVisible {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   320
	return keyboardVisible;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   321
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   322
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   323
/* Set ourselves up as a UITextFieldDelegate */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   324
- (void)initializeKeyboard {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   325
		
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   326
	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   327
	textField.delegate = self;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   328
	/* placeholder so there is something to delete! */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   329
	textField.text = @" ";	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   330
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   331
	/* set UITextInputTrait properties, mostly to defaults */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   332
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   333
	textField.autocorrectionType = UITextAutocorrectionTypeNo;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   334
	textField.enablesReturnKeyAutomatically = NO;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   335
	textField.keyboardAppearance = UIKeyboardAppearanceDefault;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   336
	textField.keyboardType = UIKeyboardTypeDefault;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   337
	textField.returnKeyType = UIReturnKeyDefault;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   338
	textField.secureTextEntry = NO;	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   339
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   340
	textField.hidden = YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   341
	keyboardVisible = NO;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   342
	/* add the UITextField (hidden) to our view */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   343
	[self addSubview: textField];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   344
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   345
	/* create our SDL_Keyboard */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   346
	SDL_Keyboard keyboard;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   347
	SDL_zero(keyboard);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   348
	SDL_AddKeyboard(&keyboard, 0);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   349
	SDLKey keymap[SDL_NUM_SCANCODES];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   350
	SDL_GetDefaultKeymap(keymap);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   351
	SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   352
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   353
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   354
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   355
/* reveal onscreen virtual keyboard */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   356
- (void)showKeyboard {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   357
	keyboardVisible = YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   358
	[textField becomeFirstResponder];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   359
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   360
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   361
/* hide onscreen virtual keyboard */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   362
- (void)hideKeyboard {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   363
	keyboardVisible = NO;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   364
	[textField resignFirstResponder];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   365
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   366
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   367
/* UITextFieldDelegate method.  Invoked when user types something. */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   368
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   369
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   370
	if ([string length] == 0) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   371
		/* it wants to replace text with nothing, ie a delete */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   372
		SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   373
		SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   374
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   375
	else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   376
		/* go through all the characters in the string we've been sent
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   377
		   and convert them to key presses */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   378
		int i;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   379
		for (i=0; i<[string length]; i++) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   380
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   381
			unichar c = [string characterAtIndex: i];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   382
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   383
			Uint16 mod = 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   384
			SDL_scancode code;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   385
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   386
			if (c < 127) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   387
				/* figure out the SDL_scancode and SDL_keymod for this unichar */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   388
				code = unicharToUIKeyInfoTable[c].code;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   389
				mod  = unicharToUIKeyInfoTable[c].mod;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   390
			}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   391
			else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   392
				/* we only deal with ASCII right now */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   393
				code = SDL_SCANCODE_UNKNOWN;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   394
				mod = 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   395
			}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   396
			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   397
			if (mod & KMOD_SHIFT) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   398
				/* If character uses shift, press shift down */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   399
				SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   400
			}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   401
			/* send a keydown and keyup even for the character */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   402
			SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   403
			SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   404
			if (mod & KMOD_SHIFT) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   405
				/* If character uses shift, press shift back up */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   406
				SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   407
			}			
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   408
		}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   409
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   410
	return NO; /* don't allow the edit! (keep placeholder text there) */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   411
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   412
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   413
/* Terminates the editing session */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   414
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   415
	[self hideKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   416
	return YES;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   417
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   418
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   419
#endif
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   420
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   421
@end
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   422
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   423
/* iPhone keyboard addition functions */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   424
#if SDL_IPHONE_KEYBOARD
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   425
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   426
int SDL_iPhoneKeyboardShow(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   427
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   428
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   429
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   430
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   431
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   432
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   433
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   434
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   435
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   436
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   437
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   438
	if (nil == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   439
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   440
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   441
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   442
	else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   443
		[view showKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   444
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   445
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   446
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   447
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   448
int SDL_iPhoneKeyboardHide(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   449
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   450
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   451
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   452
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   453
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   454
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   455
	}	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   456
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   457
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   458
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   459
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   460
	if (NULL == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   461
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   462
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   463
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   464
	else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   465
		[view hideKeyboard];
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   466
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   467
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   468
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   469
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   470
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   471
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   472
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   473
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   474
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   475
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   476
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   477
	}	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   478
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   479
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   480
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   481
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   482
	if (NULL == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   483
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   484
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   485
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   486
	else {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   487
		return view.keyboardVisible;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   488
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   489
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   490
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   491
int SDL_iPhoneKeyboardToggle(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   492
	SDL_WindowData *data;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   493
	SDL_uikitview *view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   494
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   495
	if (NULL == window) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   496
		SDL_SetError("Window does not exist");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   497
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   498
	}	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   499
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   500
	data = (SDL_WindowData *)window->driverdata;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   501
	view = data->view;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   502
	
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   503
	if (NULL == view) {
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   504
		SDL_SetError("Window has no view");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   505
		return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   506
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   507
	else {
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   508
		if (SDL_iPhoneKeyboardIsShown(window)) {
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   509
			SDL_iPhoneKeyboardHide(window);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   510
		}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   511
		else {
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   512
			SDL_iPhoneKeyboardShow(window);
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   513
		}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   514
		return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   515
	}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   516
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   517
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   518
#else
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   519
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   520
/* stubs, used if compiled without keyboard support */
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   521
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   522
int SDL_iPhoneKeyboardShow(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   523
	SDL_SetError("Not compiled with keyboard support");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   524
	return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   525
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   526
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   527
int SDL_iPhoneKeyboardHide(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   528
	SDL_SetError("Not compiled with keyboard support");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   529
	return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   530
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   531
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   532
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   533
	return 0;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   534
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   535
2714
c85ffe57d971 update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents: 2705
diff changeset
   536
int SDL_iPhoneKeyboardToggle(SDL_Window *window) {
2688
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   537
	SDL_SetError("Not compiled with keyboard support");
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   538
	return -1;
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   539
}
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   540
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   541
174c94b8ea72 move all sdl source files in a proper directory
koda
parents:
diff changeset
   542
#endif /* SDL_IPHONE_KEYBOARD */