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