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