src/video/uikit/SDL_uikitview.m
changeset 2765 f55c87ae336b
child 2859 99210400e8b9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/uikit/SDL_uikitview.m	Sat Oct 04 06:46:59 2008 +0000
     1.3 @@ -0,0 +1,398 @@
     1.4 +/*
     1.5 + SDL - Simple DirectMedia Layer
     1.6 + Copyright (C) 1997-2006 Sam Lantinga
     1.7 + 
     1.8 + This library is free software; you can redistribute it and/or
     1.9 + modify it under the terms of the GNU Lesser General Public
    1.10 + License as published by the Free Software Foundation; either
    1.11 + version 2.1 of the License, or (at your option) any later version.
    1.12 + 
    1.13 + This library is distributed in the hope that it will be useful,
    1.14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 + Lesser General Public License for more details.
    1.17 + 
    1.18 + You should have received a copy of the GNU Lesser General Public
    1.19 + License along with this library; if not, write to the Free Software
    1.20 + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 + 
    1.22 + Sam Lantinga
    1.23 + slouken@libsdl.org
    1.24 + */
    1.25 +
    1.26 +#import "SDL_uikitview.h"
    1.27 +
    1.28 +#if SDL_IPHONE_KEYBOARD
    1.29 +#import "SDL_keyboard_c.h"
    1.30 +#import "keyinfotable.h"
    1.31 +#import "SDL_uikitappdelegate.h"
    1.32 +#import "SDL_uikitwindow.h"
    1.33 +#endif
    1.34 +
    1.35 +@implementation SDL_uikitview
    1.36 +
    1.37 +- (void)dealloc {
    1.38 +#if SDL_IPHONE_KEYBOARD
    1.39 +	SDL_DelKeyboard(0);
    1.40 +	[textField release];
    1.41 +#endif
    1.42 +	[super dealloc];
    1.43 +}
    1.44 +
    1.45 +- (id)initWithFrame:(CGRect)frame {
    1.46 +
    1.47 +	self = [super initWithFrame: frame];
    1.48 +	
    1.49 +#if SDL_IPHONE_KEYBOARD
    1.50 +	[self initializeKeyboard];
    1.51 +#endif	
    1.52 +
    1.53 +	int i;
    1.54 +	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
    1.55 +		mice[i].driverdata = NULL;
    1.56 +		SDL_AddMouse(&mice[i], i, "Mouse", 0, 0, 1);
    1.57 +	}
    1.58 +	self.multipleTouchEnabled = YES;
    1.59 +			
    1.60 +	return self;
    1.61 +
    1.62 +}
    1.63 +
    1.64 +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    1.65 +
    1.66 +	NSEnumerator *enumerator = [touches objectEnumerator];
    1.67 +	UITouch *touch =(UITouch*)[enumerator nextObject];
    1.68 +	
    1.69 +	/* associate touches with mice, so long as we have slots */
    1.70 +	int i;
    1.71 +	int found = 0;
    1.72 +	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
    1.73 +	
    1.74 +		/* check if this mouse is already tracking a touch */
    1.75 +		if (mice[i].driverdata != NULL) {
    1.76 +			continue;
    1.77 +		}
    1.78 +		/*	
    1.79 +			mouse not associated with anything right now,
    1.80 +			associate the touch with this mouse
    1.81 +		*/
    1.82 +		found = 1;
    1.83 +		
    1.84 +		/* save old mouse so we can switch back */
    1.85 +		int oldMouse = SDL_SelectMouse(-1);
    1.86 +		
    1.87 +		/* select this slot's mouse */
    1.88 +		SDL_SelectMouse(i);
    1.89 +		CGPoint locationInView = [touch locationInView: self];
    1.90 +		
    1.91 +		/* set driver data to touch object, we'll use touch object later */
    1.92 +		mice[i].driverdata = [touch retain];
    1.93 +		
    1.94 +		/* send moved event */
    1.95 +		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
    1.96 +		
    1.97 +		/* send mouse down event */
    1.98 +		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
    1.99 +		
   1.100 +		/* re-calibrate relative mouse motion */
   1.101 +		SDL_GetRelativeMouseState(i, NULL, NULL);
   1.102 +		
   1.103 +		/* grab next touch */
   1.104 +		touch = (UITouch*)[enumerator nextObject]; 
   1.105 +		
   1.106 +		/* switch back to our old mouse */
   1.107 +		SDL_SelectMouse(oldMouse);
   1.108 +		
   1.109 +	}	
   1.110 +}
   1.111 +
   1.112 +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   1.113 +	
   1.114 +	NSEnumerator *enumerator = [touches objectEnumerator];
   1.115 +	UITouch *touch=nil;
   1.116 +	
   1.117 +	while(touch = (UITouch *)[enumerator nextObject]) {
   1.118 +		/* search for the mouse slot associated with this touch */
   1.119 +		int i, found = NO;
   1.120 +		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
   1.121 +			if (mice[i].driverdata == touch) {
   1.122 +				/* found the mouse associate with the touch */
   1.123 +				[(UITouch*)(mice[i].driverdata) release];
   1.124 +				mice[i].driverdata = NULL;
   1.125 +				/* send mouse up */
   1.126 +				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
   1.127 +				/* discontinue search for this touch */
   1.128 +				found = YES;
   1.129 +			}
   1.130 +		}
   1.131 +	}
   1.132 +}
   1.133 +
   1.134 +- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
   1.135 +	/*
   1.136 +		this can happen if the user puts more than 5 touches on the screen
   1.137 +		at once, or perhaps in other circumstances.  Usually (it seems)
   1.138 +		all active touches are canceled.
   1.139 +	*/
   1.140 +	[self touchesEnded: touches withEvent: event];
   1.141 +}
   1.142 +
   1.143 +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   1.144 +	
   1.145 +	NSEnumerator *enumerator = [touches objectEnumerator];
   1.146 +	UITouch *touch=nil;
   1.147 +	
   1.148 +	while(touch = (UITouch *)[enumerator nextObject]) {
   1.149 +		/* try to find the mouse associated with this touch */
   1.150 +		int i, found = NO;
   1.151 +		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
   1.152 +			if (mice[i].driverdata == touch) {
   1.153 +				/* found proper mouse */
   1.154 +				CGPoint locationInView = [touch locationInView: self];
   1.155 +				/* send moved event */
   1.156 +				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
   1.157 +				/* discontinue search */
   1.158 +				found = YES;
   1.159 +			}
   1.160 +		}
   1.161 +	}
   1.162 +}
   1.163 +
   1.164 +/*
   1.165 +	---- Keyboard related functionality below this line ----
   1.166 +*/
   1.167 +#if SDL_IPHONE_KEYBOARD
   1.168 +
   1.169 +/* Is the iPhone virtual keyboard visible onscreen? */
   1.170 +- (BOOL)keyboardVisible {
   1.171 +	return keyboardVisible;
   1.172 +}
   1.173 +
   1.174 +/* Set ourselves up as a UITextFieldDelegate */
   1.175 +- (void)initializeKeyboard {
   1.176 +		
   1.177 +	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
   1.178 +	textField.delegate = self;
   1.179 +	/* placeholder so there is something to delete! */
   1.180 +	textField.text = @" ";	
   1.181 +	
   1.182 +	/* set UITextInputTrait properties, mostly to defaults */
   1.183 +	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
   1.184 +	textField.autocorrectionType = UITextAutocorrectionTypeNo;
   1.185 +	textField.enablesReturnKeyAutomatically = NO;
   1.186 +	textField.keyboardAppearance = UIKeyboardAppearanceDefault;
   1.187 +	textField.keyboardType = UIKeyboardTypeDefault;
   1.188 +	textField.returnKeyType = UIReturnKeyDefault;
   1.189 +	textField.secureTextEntry = NO;	
   1.190 +	
   1.191 +	textField.hidden = YES;
   1.192 +	keyboardVisible = NO;
   1.193 +	/* add the UITextField (hidden) to our view */
   1.194 +	[self addSubview: textField];
   1.195 +	
   1.196 +	/* create our SDL_Keyboard */
   1.197 +	SDL_Keyboard keyboard;
   1.198 +	SDL_zero(keyboard);
   1.199 +	SDL_AddKeyboard(&keyboard, 0);
   1.200 +	SDLKey keymap[SDL_NUM_SCANCODES];
   1.201 +	SDL_GetDefaultKeymap(keymap);
   1.202 +	SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
   1.203 +	
   1.204 +}
   1.205 +
   1.206 +/* reveal onscreen virtual keyboard */
   1.207 +- (void)showKeyboard {
   1.208 +	keyboardVisible = YES;
   1.209 +	[textField becomeFirstResponder];
   1.210 +}
   1.211 +
   1.212 +/* hide onscreen virtual keyboard */
   1.213 +- (void)hideKeyboard {
   1.214 +	keyboardVisible = NO;
   1.215 +	[textField resignFirstResponder];
   1.216 +}
   1.217 +
   1.218 +/* UITextFieldDelegate method.  Invoked when user types something. */
   1.219 +- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   1.220 +	
   1.221 +	if ([string length] == 0) {
   1.222 +		/* it wants to replace text with nothing, ie a delete */
   1.223 +		SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
   1.224 +		SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
   1.225 +	}
   1.226 +	else {
   1.227 +		/* go through all the characters in the string we've been sent
   1.228 +		   and convert them to key presses */
   1.229 +		int i;
   1.230 +		for (i=0; i<[string length]; i++) {
   1.231 +			
   1.232 +			unichar c = [string characterAtIndex: i];
   1.233 +			
   1.234 +			Uint16 mod = 0;
   1.235 +			SDL_scancode code;
   1.236 +			
   1.237 +			if (c < 127) {
   1.238 +				/* figure out the SDL_scancode and SDL_keymod for this unichar */
   1.239 +				code = unicharToUIKeyInfoTable[c].code;
   1.240 +				mod  = unicharToUIKeyInfoTable[c].mod;
   1.241 +			}
   1.242 +			else {
   1.243 +				/* we only deal with ASCII right now */
   1.244 +				code = SDL_SCANCODE_UNKNOWN;
   1.245 +				mod = 0;
   1.246 +			}
   1.247 +			
   1.248 +			if (mod & KMOD_SHIFT) {
   1.249 +				/* If character uses shift, press shift down */
   1.250 +				SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
   1.251 +			}
   1.252 +			/* send a keydown and keyup even for the character */
   1.253 +			SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
   1.254 +			SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
   1.255 +			if (mod & KMOD_SHIFT) {
   1.256 +				/* If character uses shift, press shift back up */
   1.257 +				SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
   1.258 +			}			
   1.259 +		}
   1.260 +	}
   1.261 +	return NO; /* don't allow the edit! (keep placeholder text there) */
   1.262 +}
   1.263 +
   1.264 +/* Terminates the editing session */
   1.265 +- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
   1.266 +	[self hideKeyboard];
   1.267 +	return YES;
   1.268 +}
   1.269 +
   1.270 +#endif
   1.271 +
   1.272 +@end
   1.273 +
   1.274 +/* iPhone keyboard addition functions */
   1.275 +#if SDL_IPHONE_KEYBOARD
   1.276 +
   1.277 +int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
   1.278 +	
   1.279 +	SDL_Window *window = SDL_GetWindowFromID(windowID);
   1.280 +	SDL_WindowData *data;
   1.281 +	SDL_uikitview *view;
   1.282 +	
   1.283 +	if (NULL == window) {
   1.284 +		SDL_SetError("Window does not exist");
   1.285 +		return -1;
   1.286 +	}
   1.287 +	
   1.288 +	data = (SDL_WindowData *)window->driverdata;
   1.289 +	view = data->view;
   1.290 +	
   1.291 +	if (nil == view) {
   1.292 +		SDL_SetError("Window has no view");
   1.293 +		return -1;
   1.294 +	}
   1.295 +	else {
   1.296 +		[view showKeyboard];
   1.297 +		return 0;
   1.298 +	}
   1.299 +}
   1.300 +
   1.301 +int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
   1.302 +	
   1.303 +	SDL_Window *window = SDL_GetWindowFromID(windowID);
   1.304 +	SDL_WindowData *data;
   1.305 +	SDL_uikitview *view;
   1.306 +	
   1.307 +	if (NULL == window) {
   1.308 +		SDL_SetError("Window does not exist");
   1.309 +		return -1;
   1.310 +	}	
   1.311 +	
   1.312 +	data = (SDL_WindowData *)window->driverdata;
   1.313 +	view = data->view;
   1.314 +	
   1.315 +	if (NULL == view) {
   1.316 +		SDL_SetError("Window has no view");
   1.317 +		return -1;
   1.318 +	}
   1.319 +	else {
   1.320 +		[view hideKeyboard];
   1.321 +		return 0;
   1.322 +	}
   1.323 +}
   1.324 +
   1.325 +SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
   1.326 +	
   1.327 +	SDL_Window *window = SDL_GetWindowFromID(windowID);
   1.328 +	SDL_WindowData *data;
   1.329 +	SDL_uikitview *view;
   1.330 +	
   1.331 +	if (NULL == window) {
   1.332 +		SDL_SetError("Window does not exist");
   1.333 +		return -1;
   1.334 +	}	
   1.335 +	
   1.336 +	data = (SDL_WindowData *)window->driverdata;
   1.337 +	view = data->view;
   1.338 +	
   1.339 +	if (NULL == view) {
   1.340 +		SDL_SetError("Window has no view");
   1.341 +		return 0;
   1.342 +	}
   1.343 +	else {
   1.344 +		return view.keyboardVisible;
   1.345 +	}
   1.346 +}
   1.347 +
   1.348 +int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
   1.349 +	
   1.350 +	SDL_Window *window = SDL_GetWindowFromID(windowID);
   1.351 +	SDL_WindowData *data;
   1.352 +	SDL_uikitview *view;
   1.353 +	
   1.354 +	if (NULL == window) {
   1.355 +		SDL_SetError("Window does not exist");
   1.356 +		return -1;
   1.357 +	}	
   1.358 +	
   1.359 +	data = (SDL_WindowData *)window->driverdata;
   1.360 +	view = data->view;
   1.361 +	
   1.362 +	if (NULL == view) {
   1.363 +		SDL_SetError("Window has no view");
   1.364 +		return -1;
   1.365 +	}
   1.366 +	else {
   1.367 +		if (SDL_iPhoneKeyboardIsShown(windowID)) {
   1.368 +			SDL_iPhoneKeyboardHide(windowID);
   1.369 +		}
   1.370 +		else {
   1.371 +			SDL_iPhoneKeyboardShow(windowID);
   1.372 +		}
   1.373 +		return 0;
   1.374 +	}
   1.375 +}
   1.376 +
   1.377 +#else
   1.378 +
   1.379 +/* stubs, used if compiled without keyboard support */
   1.380 +
   1.381 +int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
   1.382 +	SDL_SetError("Not compiled with keyboard support");
   1.383 +	return -1;
   1.384 +}
   1.385 +
   1.386 +int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
   1.387 +	SDL_SetError("Not compiled with keyboard support");
   1.388 +	return -1;
   1.389 +}
   1.390 +
   1.391 +SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
   1.392 +	return 0;
   1.393 +}
   1.394 +
   1.395 +int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
   1.396 +	SDL_SetError("Not compiled with keyboard support");
   1.397 +	return -1;
   1.398 +}
   1.399 +
   1.400 +
   1.401 +#endif /* SDL_IPHONE_KEYBOARD */
   1.402 \ No newline at end of file