src/video/uikit/SDL_uikitview.m
author krogoway
Mon, 24 Jan 2011 13:47:35 -0600
changeset 5081 25d4feb7c127
parent 5051 a69e36365766
child 5131 7b7da52e8775
permissions -rw-r--r--
Renamed SDL_keysym to SDL_KeySym
Renamed SDL_scancode to SDL_ScanCode
Added #defines to SDL_compat.h
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
	[super dealloc];
slouken@2765
    39
}
slouken@2765
    40
slouken@2765
    41
- (id)initWithFrame:(CGRect)frame {
slouken@2765
    42
slouken@2765
    43
	self = [super initWithFrame: frame];
slouken@2765
    44
	
slouken@2765
    45
#if SDL_IPHONE_KEYBOARD
slouken@2765
    46
	[self initializeKeyboard];
slouken@2765
    47
#endif	
slouken@2765
    48
jimtla@4677
    49
#ifdef FIXED_MULTITOUCH
jim@4660
    50
	SDL_Touch touch;
jim@4660
    51
	touch.id = 0; //TODO: Should be -1?
jim@4660
    52
jim@4660
    53
	//touch.driverdata = SDL_malloc(sizeof(EventTouchData));
jim@4660
    54
	//EventTouchData* data = (EventTouchData*)(touch.driverdata);
jim@4660
    55
	
jim@4660
    56
	touch.x_min = 0;
jim@4660
    57
	touch.x_max = frame.size.width;
jimtla@4678
    58
	touch.native_xres = touch.x_max - touch.x_min;
jim@4660
    59
	touch.y_min = 0;
jim@4660
    60
	touch.y_max = frame.size.height;
jimtla@4678
    61
	touch.native_yres = touch.y_max - touch.y_min;
jim@4660
    62
	touch.pressure_min = 0;
jim@4660
    63
	touch.pressure_max = 1;
jimtla@4678
    64
	touch.native_pressureres = touch.pressure_max - touch.pressure_min;
jim@4660
    65
jim@4660
    66
jim@4660
    67
	touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
slouken@4465
    68
#endif
slouken@4661
    69
slouken@2765
    70
	return self;
slouken@2765
    71
slouken@2765
    72
}
slouken@2765
    73
slouken@2765
    74
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
    75
slouken@2765
    76
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@4488
    77
	UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@2765
    78
	
jimtla@4677
    79
	//NSLog("Click");
jimtla@4677
    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
jimtla@4677
    91
#ifdef 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
jimtla@4677
   134
#ifdef 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
jimtla@4677
   181
#ifdef 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@5051
   220
	textField = [[UITextField alloc] initWithFrame: CGRectZero];
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@5051
   238
	[textField release];
slouken@2765
   239
}
slouken@2765
   240
slouken@2765
   241
/* reveal onscreen virtual keyboard */
slouken@2765
   242
- (void)showKeyboard {
slouken@2765
   243
	keyboardVisible = YES;
slouken@2765
   244
	[textField becomeFirstResponder];
slouken@2765
   245
}
slouken@2765
   246
slouken@2765
   247
/* hide onscreen virtual keyboard */
slouken@2765
   248
- (void)hideKeyboard {
slouken@2765
   249
	keyboardVisible = NO;
slouken@2765
   250
	[textField resignFirstResponder];
slouken@2765
   251
}
slouken@2765
   252
slouken@2765
   253
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   254
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
slouken@2765
   255
	
slouken@2765
   256
	if ([string length] == 0) {
slouken@2765
   257
		/* it wants to replace text with nothing, ie a delete */
slouken@4490
   258
		SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@4490
   259
		SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@2765
   260
	}
slouken@2765
   261
	else {
slouken@2765
   262
		/* go through all the characters in the string we've been sent
slouken@2765
   263
		   and convert them to key presses */
slouken@2765
   264
		int i;
slouken@2765
   265
		for (i=0; i<[string length]; i++) {
slouken@2765
   266
			
slouken@2765
   267
			unichar c = [string characterAtIndex: i];
slouken@2765
   268
			
slouken@2765
   269
			Uint16 mod = 0;
krogoway@5081
   270
			SDL_ScanCode code;
slouken@2765
   271
			
slouken@2765
   272
			if (c < 127) {
krogoway@5081
   273
				/* figure out the SDL_ScanCode and SDL_keymod for this unichar */
slouken@2765
   274
				code = unicharToUIKeyInfoTable[c].code;
slouken@2765
   275
				mod  = unicharToUIKeyInfoTable[c].mod;
slouken@2765
   276
			}
slouken@2765
   277
			else {
slouken@2765
   278
				/* we only deal with ASCII right now */
slouken@2765
   279
				code = SDL_SCANCODE_UNKNOWN;
slouken@2765
   280
				mod = 0;
slouken@2765
   281
			}
slouken@2765
   282
			
slouken@2765
   283
			if (mod & KMOD_SHIFT) {
slouken@2765
   284
				/* If character uses shift, press shift down */
slouken@4490
   285
				SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@2765
   286
			}
slouken@2765
   287
			/* send a keydown and keyup even for the character */
slouken@4490
   288
			SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@4490
   289
			SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@2765
   290
			if (mod & KMOD_SHIFT) {
slouken@2765
   291
				/* If character uses shift, press shift back up */
slouken@4490
   292
				SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slouken@2765
   293
			}			
slouken@2765
   294
		}
slouken@2765
   295
	}
slouken@2765
   296
	return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   297
}
slouken@2765
   298
slouken@2765
   299
/* Terminates the editing session */
slouken@2765
   300
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@2765
   301
	[self hideKeyboard];
slouken@2765
   302
	return YES;
slouken@2765
   303
}
slouken@2765
   304
slouken@2765
   305
#endif
slouken@2765
   306
slouken@2765
   307
@end
slouken@2765
   308
slouken@2765
   309
/* iPhone keyboard addition functions */
slouken@2765
   310
#if SDL_IPHONE_KEYBOARD
slouken@2765
   311
slouken@3685
   312
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@2765
   313
	
slouken@2765
   314
	SDL_WindowData *data;
slouken@2765
   315
	SDL_uikitview *view;
slouken@2765
   316
	
slouken@2765
   317
	if (NULL == window) {
slouken@2765
   318
		SDL_SetError("Window does not exist");
slouken@2765
   319
		return -1;
slouken@2765
   320
	}
slouken@2765
   321
	
slouken@2765
   322
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   323
	view = data->view;
slouken@2765
   324
	
slouken@2765
   325
	if (nil == view) {
slouken@2765
   326
		SDL_SetError("Window has no view");
slouken@2765
   327
		return -1;
slouken@2765
   328
	}
slouken@2765
   329
	else {
slouken@2765
   330
		[view showKeyboard];
slouken@2765
   331
		return 0;
slouken@2765
   332
	}
slouken@2765
   333
}
slouken@2765
   334
slouken@3685
   335
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@2765
   336
	
slouken@2765
   337
	SDL_WindowData *data;
slouken@2765
   338
	SDL_uikitview *view;
slouken@2765
   339
	
slouken@2765
   340
	if (NULL == window) {
slouken@2765
   341
		SDL_SetError("Window does not exist");
slouken@2765
   342
		return -1;
slouken@2765
   343
	}	
slouken@2765
   344
	
slouken@2765
   345
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   346
	view = data->view;
slouken@2765
   347
	
slouken@2765
   348
	if (NULL == view) {
slouken@2765
   349
		SDL_SetError("Window has no view");
slouken@2765
   350
		return -1;
slouken@2765
   351
	}
slouken@2765
   352
	else {
slouken@2765
   353
		[view hideKeyboard];
slouken@2765
   354
		return 0;
slouken@2765
   355
	}
slouken@2765
   356
}
slouken@2765
   357
slouken@3685
   358
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@2765
   359
	
slouken@2765
   360
	SDL_WindowData *data;
slouken@2765
   361
	SDL_uikitview *view;
slouken@2765
   362
	
slouken@2765
   363
	if (NULL == window) {
slouken@2765
   364
		SDL_SetError("Window does not exist");
slouken@2765
   365
		return -1;
slouken@2765
   366
	}	
slouken@2765
   367
	
slouken@2765
   368
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   369
	view = data->view;
slouken@2765
   370
	
slouken@2765
   371
	if (NULL == view) {
slouken@2765
   372
		SDL_SetError("Window has no view");
slouken@2765
   373
		return 0;
slouken@2765
   374
	}
slouken@2765
   375
	else {
slouken@2765
   376
		return view.keyboardVisible;
slouken@2765
   377
	}
slouken@2765
   378
}
slouken@2765
   379
slouken@3685
   380
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@2765
   381
	
slouken@2765
   382
	SDL_WindowData *data;
slouken@2765
   383
	SDL_uikitview *view;
slouken@2765
   384
	
slouken@2765
   385
	if (NULL == window) {
slouken@2765
   386
		SDL_SetError("Window does not exist");
slouken@2765
   387
		return -1;
slouken@2765
   388
	}	
slouken@2765
   389
	
slouken@2765
   390
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   391
	view = data->view;
slouken@2765
   392
	
slouken@2765
   393
	if (NULL == view) {
slouken@2765
   394
		SDL_SetError("Window has no view");
slouken@2765
   395
		return -1;
slouken@2765
   396
	}
slouken@2765
   397
	else {
slouken@3685
   398
		if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@3685
   399
			SDL_iPhoneKeyboardHide(window);
slouken@2765
   400
		}
slouken@2765
   401
		else {
slouken@3685
   402
			SDL_iPhoneKeyboardShow(window);
slouken@2765
   403
		}
slouken@2765
   404
		return 0;
slouken@2765
   405
	}
slouken@2765
   406
}
slouken@2765
   407
slouken@2765
   408
#else
slouken@2765
   409
slouken@2765
   410
/* stubs, used if compiled without keyboard support */
slouken@2765
   411
slouken@3685
   412
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@2765
   413
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   414
	return -1;
slouken@2765
   415
}
slouken@2765
   416
slouken@3685
   417
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@2765
   418
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   419
	return -1;
slouken@2765
   420
}
slouken@2765
   421
slouken@3685
   422
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@2765
   423
	return 0;
slouken@2765
   424
}
slouken@2765
   425
slouken@3685
   426
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@2765
   427
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   428
	return -1;
slouken@2765
   429
}
slouken@2765
   430
slouken@2765
   431
slouken@2859
   432
#endif /* SDL_IPHONE_KEYBOARD */