src/video/uikit/SDL_uikitview.m
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Sat, 16 Aug 2008 00:16:32 +0000
branchgsoc2008_iphone
changeset 2450 96124abbcede
parent 2425 491958a6c881
permissions -rw-r--r--
Added comments, view now deletes keyboard upon dealloc, function declarations for iPhone keyboard additions now moved to SDL_uikitkeyboard.h.
hfutrell@2403
     1
/*
hfutrell@2403
     2
 SDL - Simple DirectMedia Layer
hfutrell@2403
     3
 Copyright (C) 1997-2006 Sam Lantinga
hfutrell@2403
     4
 
hfutrell@2403
     5
 This library is free software; you can redistribute it and/or
hfutrell@2403
     6
 modify it under the terms of the GNU Lesser General Public
hfutrell@2403
     7
 License as published by the Free Software Foundation; either
hfutrell@2403
     8
 version 2.1 of the License, or (at your option) any later version.
hfutrell@2403
     9
 
hfutrell@2403
    10
 This library is distributed in the hope that it will be useful,
hfutrell@2403
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
hfutrell@2403
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
hfutrell@2403
    13
 Lesser General Public License for more details.
hfutrell@2403
    14
 
hfutrell@2403
    15
 You should have received a copy of the GNU Lesser General Public
hfutrell@2403
    16
 License along with this library; if not, write to the Free Software
hfutrell@2403
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
hfutrell@2403
    18
 
hfutrell@2403
    19
 Sam Lantinga
hfutrell@2403
    20
 slouken@libsdl.org
hfutrell@2403
    21
 */
hfutrell@2405
    22
hfutrell@2353
    23
#import "SDL_uikitview.h"
hfutrell@2419
    24
hfutrell@2419
    25
#if SDL_IPHONE_KEYBOARD
hfutrell@2419
    26
#import "SDL_keyboard_c.h"
hfutrell@2419
    27
#import "keyinfotable.h"
hfutrell@2419
    28
#import "SDL_uikitappdelegate.h"
hfutrell@2419
    29
#import "SDL_uikitwindow.h"
hfutrell@2419
    30
#endif
hfutrell@2353
    31
hfutrell@2353
    32
@implementation SDL_uikitview
hfutrell@2353
    33
hfutrell@2353
    34
- (void)dealloc {
hfutrell@2405
    35
#if SDL_IPHONE_KEYBOARD
hfutrell@2450
    36
	SDL_DelKeyboard(0);
hfutrell@2405
    37
	[textField release];
hfutrell@2405
    38
#endif
hfutrell@2353
    39
	[super dealloc];
hfutrell@2353
    40
}
hfutrell@2353
    41
hfutrell@2353
    42
- (id)initWithFrame:(CGRect)frame {
hfutrell@2353
    43
hfutrell@2353
    44
	self = [super initWithFrame: frame];
hfutrell@2353
    45
	
hfutrell@2405
    46
#if SDL_IPHONE_KEYBOARD
hfutrell@2405
    47
	[self initializeKeyboard];
hfutrell@2405
    48
#endif	
hfutrell@2405
    49
hfutrell@2353
    50
	int i;
hfutrell@2353
    51
	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
hfutrell@2353
    52
		mice[i].driverdata = NULL;
hfutrell@2353
    53
		SDL_AddMouse(&(mice[i]), i);
hfutrell@2353
    54
	}
hfutrell@2353
    55
	self.multipleTouchEnabled = YES;
hfutrell@2405
    56
			
hfutrell@2353
    57
	return self;
hfutrell@2353
    58
hfutrell@2353
    59
}
hfutrell@2353
    60
hfutrell@2353
    61
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
    62
hfutrell@2353
    63
	NSEnumerator *enumerator = [touches objectEnumerator];
hfutrell@2450
    64
	UITouch *touch =(UITouch*)[enumerator nextObject];
hfutrell@2353
    65
	
hfutrell@2450
    66
	/* associate touches with mice, so long as we have slots */
hfutrell@2353
    67
	int i;
hfutrell@2353
    68
	int found = 0;
hfutrell@2353
    69
	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
hfutrell@2353
    70
	
hfutrell@2450
    71
		/* check if this mouse is already tracking a touch */
hfutrell@2353
    72
		if (mice[i].driverdata != NULL) {
hfutrell@2353
    73
			continue;
hfutrell@2353
    74
		}
hfutrell@2450
    75
		/*	
hfutrell@2450
    76
			mouse not associated with anything right now,
hfutrell@2450
    77
			associate the touch with this mouse
hfutrell@2450
    78
		*/
hfutrell@2353
    79
		found = 1;
hfutrell@2353
    80
		
hfutrell@2450
    81
		/* save old mouse so we can switch back */
hfutrell@2353
    82
		int oldMouse = SDL_SelectMouse(-1);
hfutrell@2450
    83
		
hfutrell@2450
    84
		/* select this slot's mouse */
hfutrell@2353
    85
		SDL_SelectMouse(i);
hfutrell@2353
    86
		CGPoint locationInView = [touch locationInView: self];
hfutrell@2450
    87
		
hfutrell@2450
    88
		/* set driver data to touch object, we'll use touch object later */
hfutrell@2353
    89
		mice[i].driverdata = [touch retain];
hfutrell@2450
    90
		
hfutrell@2450
    91
		/* send moved event */
hfutrell@2353
    92
		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
hfutrell@2450
    93
		
hfutrell@2450
    94
		/* send mouse down event */
hfutrell@2353
    95
		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
hfutrell@2450
    96
		
hfutrell@2450
    97
		/* re-calibrate relative mouse motion */
hfutrell@2353
    98
		SDL_GetRelativeMouseState(NULL, NULL);
hfutrell@2450
    99
		
hfutrell@2450
   100
		/* grab next touch */
hfutrell@2353
   101
		touch = (UITouch*)[enumerator nextObject]; 
hfutrell@2353
   102
		
hfutrell@2450
   103
		/* switch back to our old mouse */
hfutrell@2353
   104
		SDL_SelectMouse(oldMouse);
hfutrell@2353
   105
		
hfutrell@2353
   106
	}	
hfutrell@2353
   107
}
hfutrell@2353
   108
hfutrell@2353
   109
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
   110
	
hfutrell@2353
   111
	NSEnumerator *enumerator = [touches objectEnumerator];
hfutrell@2353
   112
	UITouch *touch=nil;
hfutrell@2353
   113
	
hfutrell@2353
   114
	while(touch = (UITouch *)[enumerator nextObject]) {
hfutrell@2450
   115
		/* search for the mouse slot associated with this touch */
hfutrell@2353
   116
		int i, found = NO;
hfutrell@2353
   117
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
hfutrell@2353
   118
			if (mice[i].driverdata == touch) {
hfutrell@2450
   119
				/* found the mouse associate with the touch */
hfutrell@2353
   120
				[(UITouch*)(mice[i].driverdata) release];
hfutrell@2353
   121
				mice[i].driverdata = NULL;
hfutrell@2450
   122
				/* send mouse up */
hfutrell@2353
   123
				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
hfutrell@2450
   124
				/* discontinue search for this touch */
hfutrell@2353
   125
				found = YES;
hfutrell@2353
   126
			}
hfutrell@2353
   127
		}
hfutrell@2353
   128
	}
hfutrell@2353
   129
}
hfutrell@2353
   130
hfutrell@2353
   131
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
   132
	/*
hfutrell@2353
   133
		this can happen if the user puts more than 5 touches on the screen
hfutrell@2405
   134
		at once, or perhaps in other circumstances.  Usually (it seems)
hfutrell@2405
   135
		all active touches are canceled.
hfutrell@2353
   136
	*/
hfutrell@2353
   137
	[self touchesEnded: touches withEvent: event];
hfutrell@2353
   138
}
hfutrell@2353
   139
hfutrell@2353
   140
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
   141
	
hfutrell@2353
   142
	NSEnumerator *enumerator = [touches objectEnumerator];
hfutrell@2353
   143
	UITouch *touch=nil;
hfutrell@2353
   144
	
hfutrell@2353
   145
	while(touch = (UITouch *)[enumerator nextObject]) {
hfutrell@2450
   146
		/* try to find the mouse associated with this touch */
hfutrell@2353
   147
		int i, found = NO;
hfutrell@2353
   148
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
hfutrell@2353
   149
			if (mice[i].driverdata == touch) {
hfutrell@2450
   150
				/* found proper mouse */
hfutrell@2353
   151
				CGPoint locationInView = [touch locationInView: self];
hfutrell@2450
   152
				/* send moved event */
hfutrell@2353
   153
				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
hfutrell@2450
   154
				/* discontinue search */
hfutrell@2353
   155
				found = YES;
hfutrell@2353
   156
			}
hfutrell@2353
   157
		}
hfutrell@2353
   158
	}
hfutrell@2353
   159
}
hfutrell@2353
   160
hfutrell@2419
   161
/*
hfutrell@2419
   162
	---- Keyboard related functionality below this line ----
hfutrell@2419
   163
*/
hfutrell@2419
   164
#if SDL_IPHONE_KEYBOARD
hfutrell@2405
   165
hfutrell@2450
   166
/* Is the iPhone virtual keyboard visible onscreen? */
hfutrell@2419
   167
- (BOOL)keyboardVisible {
hfutrell@2419
   168
	return keyboardVisible;
hfutrell@2419
   169
}
hfutrell@2419
   170
hfutrell@2450
   171
/* Set ourselves up as a UITextFieldDelegate */
hfutrell@2419
   172
- (void)initializeKeyboard {
hfutrell@2425
   173
		
hfutrell@2450
   174
	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
hfutrell@2419
   175
	textField.delegate = self;
hfutrell@2419
   176
	/* placeholder so there is something to delete! */
hfutrell@2419
   177
	textField.text = @" ";	
hfutrell@2419
   178
	
hfutrell@2419
   179
	/* set UITextInputTrait properties, mostly to defaults */
hfutrell@2419
   180
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
hfutrell@2419
   181
	textField.autocorrectionType = UITextAutocorrectionTypeNo;
hfutrell@2419
   182
	textField.enablesReturnKeyAutomatically = NO;
hfutrell@2419
   183
	textField.keyboardAppearance = UIKeyboardAppearanceDefault;
hfutrell@2419
   184
	textField.keyboardType = UIKeyboardTypeDefault;
hfutrell@2419
   185
	textField.returnKeyType = UIReturnKeyDefault;
hfutrell@2419
   186
	textField.secureTextEntry = NO;	
hfutrell@2419
   187
	
hfutrell@2419
   188
	textField.hidden = YES;
hfutrell@2419
   189
	keyboardVisible = NO;
hfutrell@2450
   190
	/* add the UITextField (hidden) to our view */
hfutrell@2419
   191
	[self addSubview: textField];
hfutrell@2419
   192
	
hfutrell@2450
   193
	/* create our SDL_Keyboard */
hfutrell@2419
   194
	SDL_Keyboard keyboard;
hfutrell@2419
   195
	SDL_zero(keyboard);
hfutrell@2419
   196
	SDL_AddKeyboard(&keyboard, 0);
hfutrell@2419
   197
	SDLKey keymap[SDL_NUM_SCANCODES];
hfutrell@2419
   198
	SDL_GetDefaultKeymap(keymap);
hfutrell@2419
   199
	SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
hfutrell@2419
   200
	
hfutrell@2419
   201
}
hfutrell@2419
   202
hfutrell@2450
   203
/* reveal onscreen virtual keyboard */
hfutrell@2419
   204
- (void)showKeyboard {
hfutrell@2419
   205
	keyboardVisible = YES;
hfutrell@2419
   206
	[textField becomeFirstResponder];
hfutrell@2419
   207
}
hfutrell@2419
   208
hfutrell@2450
   209
/* hide onscreen virtual keyboard */
hfutrell@2419
   210
- (void)hideKeyboard {
hfutrell@2419
   211
	keyboardVisible = NO;
hfutrell@2419
   212
	[textField resignFirstResponder];
hfutrell@2419
   213
}
hfutrell@2419
   214
hfutrell@2450
   215
/* UITextFieldDelegate method.  Invoked when user types something. */
hfutrell@2419
   216
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
hfutrell@2419
   217
	
hfutrell@2419
   218
	if ([string length] == 0) {
hfutrell@2419
   219
		/* it wants to replace text with nothing, ie a delete */
hfutrell@2419
   220
		SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
hfutrell@2419
   221
		SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
hfutrell@2419
   222
	}
hfutrell@2419
   223
	else {
hfutrell@2450
   224
		/* go through all the characters in the string we've been sent
hfutrell@2450
   225
		   and convert them to key presses */
hfutrell@2419
   226
		int i;
hfutrell@2419
   227
		for (i=0; i<[string length]; i++) {
hfutrell@2419
   228
			
hfutrell@2419
   229
			unichar c = [string characterAtIndex: i];
hfutrell@2419
   230
			
hfutrell@2419
   231
			Uint16 mod = 0;
hfutrell@2419
   232
			SDL_scancode code;
hfutrell@2419
   233
			
hfutrell@2419
   234
			if (c < 127) {
hfutrell@2450
   235
				/* figure out the SDL_scancode and SDL_keymod for this unichar */
hfutrell@2419
   236
				code = unicharToUIKeyInfoTable[c].code;
hfutrell@2419
   237
				mod  = unicharToUIKeyInfoTable[c].mod;
hfutrell@2419
   238
			}
hfutrell@2419
   239
			else {
hfutrell@2450
   240
				/* we only deal with ASCII right now */
hfutrell@2419
   241
				code = SDL_SCANCODE_UNKNOWN;
hfutrell@2419
   242
				mod = 0;
hfutrell@2419
   243
			}
hfutrell@2419
   244
			
hfutrell@2419
   245
			if (mod & KMOD_SHIFT) {
hfutrell@2450
   246
				/* If character uses shift, press shift down */
hfutrell@2419
   247
				SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
hfutrell@2419
   248
			}
hfutrell@2450
   249
			/* send a keydown and keyup even for the character */
hfutrell@2419
   250
			SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
hfutrell@2419
   251
			SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
hfutrell@2419
   252
			if (mod & KMOD_SHIFT) {
hfutrell@2450
   253
				/* If character uses shift, press shift back up */
hfutrell@2419
   254
				SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
hfutrell@2419
   255
			}			
hfutrell@2419
   256
		}
hfutrell@2419
   257
	}
hfutrell@2450
   258
	return NO; /* don't allow the edit! (keep placeholder text there) */
hfutrell@2419
   259
}
hfutrell@2419
   260
hfutrell@2419
   261
/* Terminates the editing session */
hfutrell@2419
   262
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
hfutrell@2419
   263
	[self hideKeyboard];
hfutrell@2419
   264
	return YES;
hfutrell@2419
   265
}
hfutrell@2419
   266
hfutrell@2419
   267
#endif
hfutrell@2405
   268
hfutrell@2353
   269
@end
hfutrell@2419
   270
hfutrell@2419
   271
/* iPhone keyboard addition functions */
hfutrell@2419
   272
#if SDL_IPHONE_KEYBOARD
hfutrell@2419
   273
hfutrell@2419
   274
int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
hfutrell@2419
   275
	
hfutrell@2419
   276
	SDL_Window *window = SDL_GetWindowFromID(windowID);
hfutrell@2419
   277
	SDL_WindowData *data;
hfutrell@2419
   278
	SDL_uikitview *view;
hfutrell@2419
   279
	
hfutrell@2419
   280
	if (NULL == window) {
hfutrell@2419
   281
		SDL_SetError("Window does not exist");
hfutrell@2419
   282
		return -1;
hfutrell@2419
   283
	}
hfutrell@2419
   284
	
hfutrell@2419
   285
	data = (SDL_WindowData *)window->driverdata;
hfutrell@2419
   286
	view = data->view;
hfutrell@2419
   287
	
hfutrell@2419
   288
	if (nil == view) {
hfutrell@2419
   289
		SDL_SetError("Window has no view");
hfutrell@2419
   290
		return -1;
hfutrell@2419
   291
	}
hfutrell@2419
   292
	else {
hfutrell@2419
   293
		[view showKeyboard];
hfutrell@2419
   294
		return 0;
hfutrell@2419
   295
	}
hfutrell@2419
   296
}
hfutrell@2419
   297
hfutrell@2419
   298
int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
hfutrell@2419
   299
	
hfutrell@2419
   300
	SDL_Window *window = SDL_GetWindowFromID(windowID);
hfutrell@2419
   301
	SDL_WindowData *data;
hfutrell@2419
   302
	SDL_uikitview *view;
hfutrell@2419
   303
	
hfutrell@2419
   304
	if (NULL == window) {
hfutrell@2419
   305
		SDL_SetError("Window does not exist");
hfutrell@2419
   306
		return -1;
hfutrell@2419
   307
	}	
hfutrell@2419
   308
	
hfutrell@2419
   309
	data = (SDL_WindowData *)window->driverdata;
hfutrell@2419
   310
	view = data->view;
hfutrell@2419
   311
	
hfutrell@2419
   312
	if (NULL == view) {
hfutrell@2419
   313
		SDL_SetError("Window has no view");
hfutrell@2419
   314
		return -1;
hfutrell@2419
   315
	}
hfutrell@2419
   316
	else {
hfutrell@2419
   317
		[view hideKeyboard];
hfutrell@2419
   318
		return 0;
hfutrell@2419
   319
	}
hfutrell@2419
   320
}
hfutrell@2419
   321
hfutrell@2419
   322
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
hfutrell@2419
   323
	
hfutrell@2419
   324
	SDL_Window *window = SDL_GetWindowFromID(windowID);
hfutrell@2419
   325
	SDL_WindowData *data;
hfutrell@2419
   326
	SDL_uikitview *view;
hfutrell@2419
   327
	
hfutrell@2419
   328
	if (NULL == window) {
hfutrell@2419
   329
		SDL_SetError("Window does not exist");
hfutrell@2419
   330
		return -1;
hfutrell@2419
   331
	}	
hfutrell@2419
   332
	
hfutrell@2419
   333
	data = (SDL_WindowData *)window->driverdata;
hfutrell@2419
   334
	view = data->view;
hfutrell@2419
   335
	
hfutrell@2419
   336
	if (NULL == view) {
hfutrell@2419
   337
		SDL_SetError("Window has no view");
hfutrell@2419
   338
		return 0;
hfutrell@2419
   339
	}
hfutrell@2419
   340
	else {
hfutrell@2419
   341
		return view.keyboardVisible;
hfutrell@2419
   342
	}
hfutrell@2419
   343
}
hfutrell@2419
   344
hfutrell@2419
   345
int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
hfutrell@2419
   346
	
hfutrell@2419
   347
	SDL_Window *window = SDL_GetWindowFromID(windowID);
hfutrell@2419
   348
	SDL_WindowData *data;
hfutrell@2419
   349
	SDL_uikitview *view;
hfutrell@2419
   350
	
hfutrell@2419
   351
	if (NULL == window) {
hfutrell@2419
   352
		SDL_SetError("Window does not exist");
hfutrell@2419
   353
		return -1;
hfutrell@2419
   354
	}	
hfutrell@2419
   355
	
hfutrell@2419
   356
	data = (SDL_WindowData *)window->driverdata;
hfutrell@2419
   357
	view = data->view;
hfutrell@2419
   358
	
hfutrell@2419
   359
	if (NULL == view) {
hfutrell@2419
   360
		SDL_SetError("Window has no view");
hfutrell@2419
   361
		return -1;
hfutrell@2419
   362
	}
hfutrell@2419
   363
	else {
hfutrell@2419
   364
		if (SDL_iPhoneKeyboardIsShown(windowID)) {
hfutrell@2419
   365
			SDL_iPhoneKeyboardHide(windowID);
hfutrell@2419
   366
		}
hfutrell@2419
   367
		else {
hfutrell@2419
   368
			SDL_iPhoneKeyboardShow(windowID);
hfutrell@2419
   369
		}
hfutrell@2419
   370
		return 0;
hfutrell@2419
   371
	}
hfutrell@2419
   372
}
hfutrell@2419
   373
hfutrell@2419
   374
#else
hfutrell@2419
   375
hfutrell@2450
   376
/* stubs, used if compiled without keyboard support */
hfutrell@2450
   377
hfutrell@2419
   378
int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
hfutrell@2419
   379
	SDL_SetError("Not compiled with keyboard support");
hfutrell@2419
   380
	return -1;
hfutrell@2419
   381
}
hfutrell@2419
   382
hfutrell@2419
   383
int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
hfutrell@2419
   384
	SDL_SetError("Not compiled with keyboard support");
hfutrell@2419
   385
	return -1;
hfutrell@2419
   386
}
hfutrell@2419
   387
hfutrell@2419
   388
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
hfutrell@2419
   389
	return 0;
hfutrell@2419
   390
}
hfutrell@2419
   391
hfutrell@2419
   392
int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
hfutrell@2419
   393
	SDL_SetError("Not compiled with keyboard support");
hfutrell@2419
   394
	return -1;
hfutrell@2419
   395
}
hfutrell@2419
   396
hfutrell@2419
   397
hfutrell@2419
   398
#endif /* SDL_IPHONE_KEYBOARD */