src/video/uikit/SDL_uikitview.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 12 Jul 2010 21:09:23 -0700
changeset 4661 03dcb795c583
parent 4660 b15e7017409b
parent 4490 06c7423f8c60
child 4662 3c4e0130c9b1
permissions -rw-r--r--
Merged changes from the main SDL codebase
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@4660
    94
	  SDL_SendFingerDown(touchId,(int)touch,
jim@4660
    95
			     SDL_TRUE,locationInView.x,locationInView.y,
jim@4660
    96
			     1);
jim@4660
    97
jim@4660
    98
	  touch = (UITouch*)[enumerator nextObject]; 
jim@4660
    99
	}
slouken@4465
   100
#endif
slouken@2765
   101
}
slouken@2765
   102
slouken@2765
   103
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   104
	
slouken@2765
   105
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@4488
   106
	UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@2765
   107
	
slouken@4488
   108
	if (touch) {
slouken@4488
   109
		/* send mouse up */
slouken@4488
   110
		SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@2765
   111
	}
slouken@4661
   112
jim@4660
   113
#if FIXED_MULTITOUCH
jim@4660
   114
	while(touch) {
jim@4660
   115
	  CGPoint locationInView = [touch locationInView: self];
jim@4660
   116
	  SDL_SendFingerDown(touchId,(int)touch,
jim@4660
   117
			     SDL_FALSE,locationInView.x,locationInView.y,
jim@4660
   118
			     1);
jim@4660
   119
jim@4660
   120
	  touch = (UITouch*)[enumerator nextObject]; 
jim@4660
   121
	}
slouken@4465
   122
#endif
slouken@2765
   123
}
slouken@2765
   124
slouken@2765
   125
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   126
	/*
slouken@2765
   127
		this can happen if the user puts more than 5 touches on the screen
slouken@2765
   128
		at once, or perhaps in other circumstances.  Usually (it seems)
slouken@2765
   129
		all active touches are canceled.
slouken@2765
   130
	*/
slouken@2765
   131
	[self touchesEnded: touches withEvent: event];
slouken@2765
   132
}
slouken@2765
   133
slouken@2765
   134
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   135
	
slouken@2765
   136
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@4488
   137
	UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@2765
   138
	
slouken@4488
   139
	if (touch) {
slouken@4488
   140
		CGPoint locationInView = [touch locationInView: self];
slouken@4488
   141
slouken@4488
   142
		/* send moved event */
slouken@4488
   143
		SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@2765
   144
	}
jim@4660
   145
jim@4660
   146
#if FIXED_MULTITOUCH
jim@4660
   147
	while(touch) {
jim@4660
   148
	  CGPoint locationInView = [touch locationInView: self];
jim@4660
   149
	  SDL_SendTouchMotion(touchId,(int)touch,
jim@4660
   150
			     SDL_FALSE,locationInView.x,locationInView.y,
jim@4660
   151
			     1);
jim@4660
   152
jim@4660
   153
	  touch = (UITouch*)[enumerator nextObject]; 
jim@4660
   154
	}
slouken@4465
   155
#endif
slouken@2765
   156
}
slouken@2765
   157
slouken@2765
   158
/*
slouken@2765
   159
	---- Keyboard related functionality below this line ----
slouken@2765
   160
*/
slouken@2765
   161
#if SDL_IPHONE_KEYBOARD
slouken@2765
   162
slouken@2765
   163
/* Is the iPhone virtual keyboard visible onscreen? */
slouken@2765
   164
- (BOOL)keyboardVisible {
slouken@2765
   165
	return keyboardVisible;
slouken@2765
   166
}
slouken@2765
   167
slouken@2765
   168
/* Set ourselves up as a UITextFieldDelegate */
slouken@2765
   169
- (void)initializeKeyboard {
slouken@2765
   170
		
slouken@2765
   171
	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
slouken@2765
   172
	textField.delegate = self;
slouken@2765
   173
	/* placeholder so there is something to delete! */
slouken@2765
   174
	textField.text = @" ";	
slouken@2765
   175
	
slouken@2765
   176
	/* set UITextInputTrait properties, mostly to defaults */
slouken@2765
   177
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@2765
   178
	textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@2765
   179
	textField.enablesReturnKeyAutomatically = NO;
slouken@2765
   180
	textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@2765
   181
	textField.keyboardType = UIKeyboardTypeDefault;
slouken@2765
   182
	textField.returnKeyType = UIReturnKeyDefault;
slouken@2765
   183
	textField.secureTextEntry = NO;	
slouken@2765
   184
	
slouken@2765
   185
	textField.hidden = YES;
slouken@2765
   186
	keyboardVisible = NO;
slouken@2765
   187
	/* add the UITextField (hidden) to our view */
slouken@2765
   188
	[self addSubview: textField];
slouken@2765
   189
}
slouken@2765
   190
slouken@2765
   191
/* reveal onscreen virtual keyboard */
slouken@2765
   192
- (void)showKeyboard {
slouken@2765
   193
	keyboardVisible = YES;
slouken@2765
   194
	[textField becomeFirstResponder];
slouken@2765
   195
}
slouken@2765
   196
slouken@2765
   197
/* hide onscreen virtual keyboard */
slouken@2765
   198
- (void)hideKeyboard {
slouken@2765
   199
	keyboardVisible = NO;
slouken@2765
   200
	[textField resignFirstResponder];
slouken@2765
   201
}
slouken@2765
   202
slouken@2765
   203
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   204
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
slouken@2765
   205
	
slouken@2765
   206
	if ([string length] == 0) {
slouken@2765
   207
		/* it wants to replace text with nothing, ie a delete */
slouken@4490
   208
		SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@4490
   209
		SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@2765
   210
	}
slouken@2765
   211
	else {
slouken@2765
   212
		/* go through all the characters in the string we've been sent
slouken@2765
   213
		   and convert them to key presses */
slouken@2765
   214
		int i;
slouken@2765
   215
		for (i=0; i<[string length]; i++) {
slouken@2765
   216
			
slouken@2765
   217
			unichar c = [string characterAtIndex: i];
slouken@2765
   218
			
slouken@2765
   219
			Uint16 mod = 0;
slouken@2765
   220
			SDL_scancode code;
slouken@2765
   221
			
slouken@2765
   222
			if (c < 127) {
slouken@2765
   223
				/* figure out the SDL_scancode and SDL_keymod for this unichar */
slouken@2765
   224
				code = unicharToUIKeyInfoTable[c].code;
slouken@2765
   225
				mod  = unicharToUIKeyInfoTable[c].mod;
slouken@2765
   226
			}
slouken@2765
   227
			else {
slouken@2765
   228
				/* we only deal with ASCII right now */
slouken@2765
   229
				code = SDL_SCANCODE_UNKNOWN;
slouken@2765
   230
				mod = 0;
slouken@2765
   231
			}
slouken@2765
   232
			
slouken@2765
   233
			if (mod & KMOD_SHIFT) {
slouken@2765
   234
				/* If character uses shift, press shift down */
slouken@4490
   235
				SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@2765
   236
			}
slouken@2765
   237
			/* send a keydown and keyup even for the character */
slouken@4490
   238
			SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@4490
   239
			SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@2765
   240
			if (mod & KMOD_SHIFT) {
slouken@2765
   241
				/* If character uses shift, press shift back up */
slouken@4490
   242
				SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slouken@2765
   243
			}			
slouken@2765
   244
		}
slouken@2765
   245
	}
slouken@2765
   246
	return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   247
}
slouken@2765
   248
slouken@2765
   249
/* Terminates the editing session */
slouken@2765
   250
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@2765
   251
	[self hideKeyboard];
slouken@2765
   252
	return YES;
slouken@2765
   253
}
slouken@2765
   254
slouken@2765
   255
#endif
slouken@2765
   256
slouken@2765
   257
@end
slouken@2765
   258
slouken@2765
   259
/* iPhone keyboard addition functions */
slouken@2765
   260
#if SDL_IPHONE_KEYBOARD
slouken@2765
   261
slouken@3685
   262
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@2765
   263
	
slouken@2765
   264
	SDL_WindowData *data;
slouken@2765
   265
	SDL_uikitview *view;
slouken@2765
   266
	
slouken@2765
   267
	if (NULL == window) {
slouken@2765
   268
		SDL_SetError("Window does not exist");
slouken@2765
   269
		return -1;
slouken@2765
   270
	}
slouken@2765
   271
	
slouken@2765
   272
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   273
	view = data->view;
slouken@2765
   274
	
slouken@2765
   275
	if (nil == view) {
slouken@2765
   276
		SDL_SetError("Window has no view");
slouken@2765
   277
		return -1;
slouken@2765
   278
	}
slouken@2765
   279
	else {
slouken@2765
   280
		[view showKeyboard];
slouken@2765
   281
		return 0;
slouken@2765
   282
	}
slouken@2765
   283
}
slouken@2765
   284
slouken@3685
   285
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@2765
   286
	
slouken@2765
   287
	SDL_WindowData *data;
slouken@2765
   288
	SDL_uikitview *view;
slouken@2765
   289
	
slouken@2765
   290
	if (NULL == window) {
slouken@2765
   291
		SDL_SetError("Window does not exist");
slouken@2765
   292
		return -1;
slouken@2765
   293
	}	
slouken@2765
   294
	
slouken@2765
   295
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   296
	view = data->view;
slouken@2765
   297
	
slouken@2765
   298
	if (NULL == view) {
slouken@2765
   299
		SDL_SetError("Window has no view");
slouken@2765
   300
		return -1;
slouken@2765
   301
	}
slouken@2765
   302
	else {
slouken@2765
   303
		[view hideKeyboard];
slouken@2765
   304
		return 0;
slouken@2765
   305
	}
slouken@2765
   306
}
slouken@2765
   307
slouken@3685
   308
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@2765
   309
	
slouken@2765
   310
	SDL_WindowData *data;
slouken@2765
   311
	SDL_uikitview *view;
slouken@2765
   312
	
slouken@2765
   313
	if (NULL == window) {
slouken@2765
   314
		SDL_SetError("Window does not exist");
slouken@2765
   315
		return -1;
slouken@2765
   316
	}	
slouken@2765
   317
	
slouken@2765
   318
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   319
	view = data->view;
slouken@2765
   320
	
slouken@2765
   321
	if (NULL == view) {
slouken@2765
   322
		SDL_SetError("Window has no view");
slouken@2765
   323
		return 0;
slouken@2765
   324
	}
slouken@2765
   325
	else {
slouken@2765
   326
		return view.keyboardVisible;
slouken@2765
   327
	}
slouken@2765
   328
}
slouken@2765
   329
slouken@3685
   330
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@2765
   331
	
slouken@2765
   332
	SDL_WindowData *data;
slouken@2765
   333
	SDL_uikitview *view;
slouken@2765
   334
	
slouken@2765
   335
	if (NULL == window) {
slouken@2765
   336
		SDL_SetError("Window does not exist");
slouken@2765
   337
		return -1;
slouken@2765
   338
	}	
slouken@2765
   339
	
slouken@2765
   340
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   341
	view = data->view;
slouken@2765
   342
	
slouken@2765
   343
	if (NULL == view) {
slouken@2765
   344
		SDL_SetError("Window has no view");
slouken@2765
   345
		return -1;
slouken@2765
   346
	}
slouken@2765
   347
	else {
slouken@3685
   348
		if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@3685
   349
			SDL_iPhoneKeyboardHide(window);
slouken@2765
   350
		}
slouken@2765
   351
		else {
slouken@3685
   352
			SDL_iPhoneKeyboardShow(window);
slouken@2765
   353
		}
slouken@2765
   354
		return 0;
slouken@2765
   355
	}
slouken@2765
   356
}
slouken@2765
   357
slouken@2765
   358
#else
slouken@2765
   359
slouken@2765
   360
/* stubs, used if compiled without keyboard support */
slouken@2765
   361
slouken@3685
   362
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@2765
   363
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   364
	return -1;
slouken@2765
   365
}
slouken@2765
   366
slouken@3685
   367
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@2765
   368
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   369
	return -1;
slouken@2765
   370
}
slouken@2765
   371
slouken@3685
   372
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@2765
   373
	return 0;
slouken@2765
   374
}
slouken@2765
   375
slouken@3685
   376
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@2765
   377
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   378
	return -1;
slouken@2765
   379
}
slouken@2765
   380
slouken@2765
   381
slouken@2859
   382
#endif /* SDL_IPHONE_KEYBOARD */