src/video/uikit/SDL_uikitview.m
author Jim Grandpre <jim.tla@gmail.com>
Tue, 13 Jul 2010 18:31:09 -0400
changeset 4662 3c4e0130c9b1
parent 4661 03dcb795c583
child 4677 31607094315c
permissions -rw-r--r--
Added alternative finger tracking method. Still prefer IPHONE_TOUCH_EFFICIENT_DANGEROUS.
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
jim@4660
    51
#if FIXED_MULTITOUCH
jim@4660
    52
	SDL_Touch touch;
jim@4660
    53
	touch.id = 0; //TODO: Should be -1?
jim@4660
    54
jim@4660
    55
	//touch.driverdata = SDL_malloc(sizeof(EventTouchData));
jim@4660
    56
	//EventTouchData* data = (EventTouchData*)(touch.driverdata);
jim@4660
    57
	
jim@4660
    58
	touch.x_min = 0;
jim@4660
    59
	touch.x_max = frame.size.width;
jim@4660
    60
	touch.xres = touch.x_max - touch.x_min;
jim@4660
    61
	touch.y_min = 0;
jim@4660
    62
	touch.y_max = frame.size.height;
jim@4660
    63
	touch.yres = touch.y_max - touch.y_min;
jim@4660
    64
	touch.pressure_min = 0;
jim@4660
    65
	touch.pressure_max = 1;
jim@4660
    66
	touch.pressureres = touch.pressure_max - touch.pressure_min;
jim@4660
    67
jim@4660
    68
jim@4660
    69
	touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
slouken@4465
    70
#endif
slouken@4661
    71
slouken@2765
    72
	return self;
slouken@2765
    73
slouken@2765
    74
}
slouken@2765
    75
slouken@2765
    76
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
    77
slouken@2765
    78
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@4488
    79
	UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@2765
    80
	
slouken@4488
    81
	if (touch) {
slouken@4488
    82
		CGPoint locationInView = [touch locationInView: self];
slouken@4488
    83
			
slouken@4488
    84
		/* send moved event */
slouken@4488
    85
		SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@4488
    86
slouken@4488
    87
		/* send mouse down event */
slouken@4488
    88
		SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@4465
    89
	}
jim@4660
    90
jim@4660
    91
#if FIXED_MULTITOUCH
jim@4660
    92
	while(touch) {
jim@4660
    93
	  CGPoint locationInView = [touch locationInView: self];
jim@4662
    94
jim@4662
    95
jim@4662
    96
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
jim@4662
    97
	  //FIXME: TODO: Using touch as the fingerId is potentially dangerous
jim@4662
    98
	  //It is also much more efficient than storing the UITouch pointer
jim@4662
    99
	  //and comparing it to the incoming event.
jim@4662
   100
	  SDL_SendFingerDown(touchId,(long)touch,
jim@4660
   101
			     SDL_TRUE,locationInView.x,locationInView.y,
jim@4660
   102
			     1);
jim@4662
   103
#else
jim@4662
   104
	  int i;
jim@4662
   105
	  for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
jim@4662
   106
	    if(finger[i] == NULL) {
jim@4662
   107
	      finger[i] = touch;
jim@4662
   108
	      SDL_SendFingerDown(touchId,i,
jim@4662
   109
				 SDL_TRUE,locationInView.x,locationInView.y,
jim@4662
   110
				 1);
jim@4662
   111
	      break;
jim@4662
   112
	    }
jim@4662
   113
	  }
jim@4662
   114
#endif
jim@4662
   115
	  
jim@4662
   116
jim@4662
   117
	  
jim@4660
   118
jim@4660
   119
	  touch = (UITouch*)[enumerator nextObject]; 
jim@4660
   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@4488
   129
	if (touch) {
slouken@4488
   130
		/* send mouse up */
slouken@4488
   131
		SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@2765
   132
	}
slouken@4661
   133
jim@4660
   134
#if FIXED_MULTITOUCH
jim@4660
   135
	while(touch) {
jim@4660
   136
	  CGPoint locationInView = [touch locationInView: self];
jim@4662
   137
	  
jim@4662
   138
jim@4662
   139
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
jim@4662
   140
	  SDL_SendFingerDown(touchId,(long)touch,
jim@4660
   141
			     SDL_FALSE,locationInView.x,locationInView.y,
jim@4660
   142
			     1);
jim@4662
   143
#else
jim@4662
   144
	  int i;
jim@4662
   145
	  for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
jim@4662
   146
	    if(finger[i] == touch) {
jim@4662
   147
	      SDL_SendFingerDown(touchId,i,
jim@4662
   148
				 SDL_FALSE,locationInView.x,locationInView.y,
jim@4662
   149
				 1);
jim@4662
   150
	      break;
jim@4662
   151
	    }
jim@4662
   152
	  }
jim@4662
   153
#endif
jim@4660
   154
jim@4660
   155
	  touch = (UITouch*)[enumerator nextObject]; 
jim@4660
   156
	}
slouken@4465
   157
#endif
slouken@2765
   158
}
slouken@2765
   159
slouken@2765
   160
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   161
	/*
slouken@2765
   162
		this can happen if the user puts more than 5 touches on the screen
slouken@2765
   163
		at once, or perhaps in other circumstances.  Usually (it seems)
slouken@2765
   164
		all active touches are canceled.
slouken@2765
   165
	*/
slouken@2765
   166
	[self touchesEnded: touches withEvent: event];
slouken@2765
   167
}
slouken@2765
   168
slouken@2765
   169
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   170
	
slouken@2765
   171
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@4488
   172
	UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@2765
   173
	
slouken@4488
   174
	if (touch) {
slouken@4488
   175
		CGPoint locationInView = [touch locationInView: self];
slouken@4488
   176
slouken@4488
   177
		/* send moved event */
slouken@4488
   178
		SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@2765
   179
	}
jim@4660
   180
jim@4660
   181
#if FIXED_MULTITOUCH
jim@4660
   182
	while(touch) {
jim@4660
   183
	  CGPoint locationInView = [touch locationInView: self];
jim@4662
   184
	  
jim@4662
   185
jim@4662
   186
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
jim@4662
   187
	  SDL_SendTouchMotion(touchId,(long)touch,
jim@4662
   188
			      SDL_FALSE,locationInView.x,locationInView.y,
jim@4662
   189
			      1);
jim@4662
   190
#else
jim@4662
   191
	  int i;
jim@4662
   192
	  for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
jim@4662
   193
	    if(finger[i] == touch) {
jim@4662
   194
	      SDL_SendTouchMotion(touchId,i,
jim@4662
   195
				  SDL_FALSE,locationInView.x,locationInView.y,
jim@4662
   196
				  1);
jim@4662
   197
	      break;
jim@4662
   198
	    }
jim@4662
   199
	  }
jim@4662
   200
#endif
jim@4660
   201
jim@4660
   202
	  touch = (UITouch*)[enumerator nextObject]; 
jim@4660
   203
	}
slouken@4465
   204
#endif
slouken@2765
   205
}
slouken@2765
   206
slouken@2765
   207
/*
slouken@2765
   208
	---- Keyboard related functionality below this line ----
slouken@2765
   209
*/
slouken@2765
   210
#if SDL_IPHONE_KEYBOARD
slouken@2765
   211
slouken@2765
   212
/* Is the iPhone virtual keyboard visible onscreen? */
slouken@2765
   213
- (BOOL)keyboardVisible {
slouken@2765
   214
	return keyboardVisible;
slouken@2765
   215
}
slouken@2765
   216
slouken@2765
   217
/* Set ourselves up as a UITextFieldDelegate */
slouken@2765
   218
- (void)initializeKeyboard {
slouken@2765
   219
		
slouken@2765
   220
	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
slouken@2765
   221
	textField.delegate = self;
slouken@2765
   222
	/* placeholder so there is something to delete! */
slouken@2765
   223
	textField.text = @" ";	
slouken@2765
   224
	
slouken@2765
   225
	/* set UITextInputTrait properties, mostly to defaults */
slouken@2765
   226
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@2765
   227
	textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@2765
   228
	textField.enablesReturnKeyAutomatically = NO;
slouken@2765
   229
	textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@2765
   230
	textField.keyboardType = UIKeyboardTypeDefault;
slouken@2765
   231
	textField.returnKeyType = UIReturnKeyDefault;
slouken@2765
   232
	textField.secureTextEntry = NO;	
slouken@2765
   233
	
slouken@2765
   234
	textField.hidden = YES;
slouken@2765
   235
	keyboardVisible = NO;
slouken@2765
   236
	/* add the UITextField (hidden) to our view */
slouken@2765
   237
	[self addSubview: textField];
slouken@2765
   238
}
slouken@2765
   239
slouken@2765
   240
/* reveal onscreen virtual keyboard */
slouken@2765
   241
- (void)showKeyboard {
slouken@2765
   242
	keyboardVisible = YES;
slouken@2765
   243
	[textField becomeFirstResponder];
slouken@2765
   244
}
slouken@2765
   245
slouken@2765
   246
/* hide onscreen virtual keyboard */
slouken@2765
   247
- (void)hideKeyboard {
slouken@2765
   248
	keyboardVisible = NO;
slouken@2765
   249
	[textField resignFirstResponder];
slouken@2765
   250
}
slouken@2765
   251
slouken@2765
   252
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   253
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
slouken@2765
   254
	
slouken@2765
   255
	if ([string length] == 0) {
slouken@2765
   256
		/* it wants to replace text with nothing, ie a delete */
slouken@4490
   257
		SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@4490
   258
		SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@2765
   259
	}
slouken@2765
   260
	else {
slouken@2765
   261
		/* go through all the characters in the string we've been sent
slouken@2765
   262
		   and convert them to key presses */
slouken@2765
   263
		int i;
slouken@2765
   264
		for (i=0; i<[string length]; i++) {
slouken@2765
   265
			
slouken@2765
   266
			unichar c = [string characterAtIndex: i];
slouken@2765
   267
			
slouken@2765
   268
			Uint16 mod = 0;
slouken@2765
   269
			SDL_scancode code;
slouken@2765
   270
			
slouken@2765
   271
			if (c < 127) {
slouken@2765
   272
				/* figure out the SDL_scancode and SDL_keymod for this unichar */
slouken@2765
   273
				code = unicharToUIKeyInfoTable[c].code;
slouken@2765
   274
				mod  = unicharToUIKeyInfoTable[c].mod;
slouken@2765
   275
			}
slouken@2765
   276
			else {
slouken@2765
   277
				/* we only deal with ASCII right now */
slouken@2765
   278
				code = SDL_SCANCODE_UNKNOWN;
slouken@2765
   279
				mod = 0;
slouken@2765
   280
			}
slouken@2765
   281
			
slouken@2765
   282
			if (mod & KMOD_SHIFT) {
slouken@2765
   283
				/* If character uses shift, press shift down */
slouken@4490
   284
				SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@2765
   285
			}
slouken@2765
   286
			/* send a keydown and keyup even for the character */
slouken@4490
   287
			SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@4490
   288
			SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@2765
   289
			if (mod & KMOD_SHIFT) {
slouken@2765
   290
				/* If character uses shift, press shift back up */
slouken@4490
   291
				SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slouken@2765
   292
			}			
slouken@2765
   293
		}
slouken@2765
   294
	}
slouken@2765
   295
	return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   296
}
slouken@2765
   297
slouken@2765
   298
/* Terminates the editing session */
slouken@2765
   299
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@2765
   300
	[self hideKeyboard];
slouken@2765
   301
	return YES;
slouken@2765
   302
}
slouken@2765
   303
slouken@2765
   304
#endif
slouken@2765
   305
slouken@2765
   306
@end
slouken@2765
   307
slouken@2765
   308
/* iPhone keyboard addition functions */
slouken@2765
   309
#if SDL_IPHONE_KEYBOARD
slouken@2765
   310
slouken@3685
   311
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@2765
   312
	
slouken@2765
   313
	SDL_WindowData *data;
slouken@2765
   314
	SDL_uikitview *view;
slouken@2765
   315
	
slouken@2765
   316
	if (NULL == window) {
slouken@2765
   317
		SDL_SetError("Window does not exist");
slouken@2765
   318
		return -1;
slouken@2765
   319
	}
slouken@2765
   320
	
slouken@2765
   321
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   322
	view = data->view;
slouken@2765
   323
	
slouken@2765
   324
	if (nil == view) {
slouken@2765
   325
		SDL_SetError("Window has no view");
slouken@2765
   326
		return -1;
slouken@2765
   327
	}
slouken@2765
   328
	else {
slouken@2765
   329
		[view showKeyboard];
slouken@2765
   330
		return 0;
slouken@2765
   331
	}
slouken@2765
   332
}
slouken@2765
   333
slouken@3685
   334
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@2765
   335
	
slouken@2765
   336
	SDL_WindowData *data;
slouken@2765
   337
	SDL_uikitview *view;
slouken@2765
   338
	
slouken@2765
   339
	if (NULL == window) {
slouken@2765
   340
		SDL_SetError("Window does not exist");
slouken@2765
   341
		return -1;
slouken@2765
   342
	}	
slouken@2765
   343
	
slouken@2765
   344
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   345
	view = data->view;
slouken@2765
   346
	
slouken@2765
   347
	if (NULL == view) {
slouken@2765
   348
		SDL_SetError("Window has no view");
slouken@2765
   349
		return -1;
slouken@2765
   350
	}
slouken@2765
   351
	else {
slouken@2765
   352
		[view hideKeyboard];
slouken@2765
   353
		return 0;
slouken@2765
   354
	}
slouken@2765
   355
}
slouken@2765
   356
slouken@3685
   357
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@2765
   358
	
slouken@2765
   359
	SDL_WindowData *data;
slouken@2765
   360
	SDL_uikitview *view;
slouken@2765
   361
	
slouken@2765
   362
	if (NULL == window) {
slouken@2765
   363
		SDL_SetError("Window does not exist");
slouken@2765
   364
		return -1;
slouken@2765
   365
	}	
slouken@2765
   366
	
slouken@2765
   367
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   368
	view = data->view;
slouken@2765
   369
	
slouken@2765
   370
	if (NULL == view) {
slouken@2765
   371
		SDL_SetError("Window has no view");
slouken@2765
   372
		return 0;
slouken@2765
   373
	}
slouken@2765
   374
	else {
slouken@2765
   375
		return view.keyboardVisible;
slouken@2765
   376
	}
slouken@2765
   377
}
slouken@2765
   378
slouken@3685
   379
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@2765
   380
	
slouken@2765
   381
	SDL_WindowData *data;
slouken@2765
   382
	SDL_uikitview *view;
slouken@2765
   383
	
slouken@2765
   384
	if (NULL == window) {
slouken@2765
   385
		SDL_SetError("Window does not exist");
slouken@2765
   386
		return -1;
slouken@2765
   387
	}	
slouken@2765
   388
	
slouken@2765
   389
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   390
	view = data->view;
slouken@2765
   391
	
slouken@2765
   392
	if (NULL == view) {
slouken@2765
   393
		SDL_SetError("Window has no view");
slouken@2765
   394
		return -1;
slouken@2765
   395
	}
slouken@2765
   396
	else {
slouken@3685
   397
		if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@3685
   398
			SDL_iPhoneKeyboardHide(window);
slouken@2765
   399
		}
slouken@2765
   400
		else {
slouken@3685
   401
			SDL_iPhoneKeyboardShow(window);
slouken@2765
   402
		}
slouken@2765
   403
		return 0;
slouken@2765
   404
	}
slouken@2765
   405
}
slouken@2765
   406
slouken@2765
   407
#else
slouken@2765
   408
slouken@2765
   409
/* stubs, used if compiled without keyboard support */
slouken@2765
   410
slouken@3685
   411
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@2765
   412
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   413
	return -1;
slouken@2765
   414
}
slouken@2765
   415
slouken@3685
   416
int SDL_iPhoneKeyboardHide(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@3685
   421
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@2765
   422
	return 0;
slouken@2765
   423
}
slouken@2765
   424
slouken@3685
   425
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@2765
   426
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   427
	return -1;
slouken@2765
   428
}
slouken@2765
   429
slouken@2765
   430
slouken@2859
   431
#endif /* SDL_IPHONE_KEYBOARD */