src/video/uikit/SDL_uikitview.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Oct 2008 06:46:59 +0000
changeset 2765 f55c87ae336b
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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