Added comments, view now deletes keyboard upon dealloc, function declarations for iPhone keyboard additions now moved to SDL_uikitkeyboard.h. gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Sat, 16 Aug 2008 00:16:32 +0000
branchgsoc2008_iphone
changeset 245096124abbcede
parent 2449 4a07dc228e93
child 2451 28286ecb3f15
Added comments, view now deletes keyboard upon dealloc, function declarations for iPhone keyboard additions now moved to SDL_uikitkeyboard.h.
src/video/uikit/SDL_uikitview.h
src/video/uikit/SDL_uikitview.m
     1.1 --- a/src/video/uikit/SDL_uikitview.h	Sat Aug 16 00:14:35 2008 +0000
     1.2 +++ b/src/video/uikit/SDL_uikitview.h	Sat Aug 16 00:16:32 2008 +0000
     1.3 @@ -53,11 +53,4 @@
     1.4  @property (readonly) BOOL keyboardVisible;
     1.5  #endif 
     1.6  
     1.7 -@end
     1.8 -
     1.9 -#if SDL_IPHONE_KEYBOARD
    1.10 -extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_WindowID windowID);
    1.11 -extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_WindowID windowID);
    1.12 -extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID);
    1.13 -extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_WindowID windowID);
    1.14 -#endif
    1.15 +@end
    1.16 \ No newline at end of file
     2.1 --- a/src/video/uikit/SDL_uikitview.m	Sat Aug 16 00:14:35 2008 +0000
     2.2 +++ b/src/video/uikit/SDL_uikitview.m	Sat Aug 16 00:16:32 2008 +0000
     2.3 @@ -33,6 +33,7 @@
     2.4  
     2.5  - (void)dealloc {
     2.6  #if SDL_IPHONE_KEYBOARD
     2.7 +	SDL_DelKeyboard(0);
     2.8  	[textField release];
     2.9  #endif
    2.10  	[super dealloc];
    2.11 @@ -60,29 +61,46 @@
    2.12  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    2.13  
    2.14  	NSEnumerator *enumerator = [touches objectEnumerator];
    2.15 -	UITouch *touch=(UITouch*)[enumerator nextObject];
    2.16 +	UITouch *touch =(UITouch*)[enumerator nextObject];
    2.17  	
    2.18 -	// associate touches with mice, so long as we have slots
    2.19 +	/* associate touches with mice, so long as we have slots */
    2.20  	int i;
    2.21  	int found = 0;
    2.22  	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
    2.23  	
    2.24 -		// check if this mouse is already tracking a touch
    2.25 +		/* check if this mouse is already tracking a touch */
    2.26  		if (mice[i].driverdata != NULL) {
    2.27  			continue;
    2.28  		}
    2.29 -		
    2.30 +		/*	
    2.31 +			mouse not associated with anything right now,
    2.32 +			associate the touch with this mouse
    2.33 +		*/
    2.34  		found = 1;
    2.35  		
    2.36 +		/* save old mouse so we can switch back */
    2.37  		int oldMouse = SDL_SelectMouse(-1);
    2.38 +		
    2.39 +		/* select this slot's mouse */
    2.40  		SDL_SelectMouse(i);
    2.41  		CGPoint locationInView = [touch locationInView: self];
    2.42 +		
    2.43 +		/* set driver data to touch object, we'll use touch object later */
    2.44  		mice[i].driverdata = [touch retain];
    2.45 +		
    2.46 +		/* send moved event */
    2.47  		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
    2.48 +		
    2.49 +		/* send mouse down event */
    2.50  		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
    2.51 +		
    2.52 +		/* re-calibrate relative mouse motion */
    2.53  		SDL_GetRelativeMouseState(NULL, NULL);
    2.54 +		
    2.55 +		/* grab next touch */
    2.56  		touch = (UITouch*)[enumerator nextObject]; 
    2.57  		
    2.58 +		/* switch back to our old mouse */
    2.59  		SDL_SelectMouse(oldMouse);
    2.60  		
    2.61  	}	
    2.62 @@ -94,13 +112,16 @@
    2.63  	UITouch *touch=nil;
    2.64  	
    2.65  	while(touch = (UITouch *)[enumerator nextObject]) {
    2.66 +		/* search for the mouse slot associated with this touch */
    2.67  		int i, found = NO;
    2.68  		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
    2.69  			if (mice[i].driverdata == touch) {
    2.70 +				/* found the mouse associate with the touch */
    2.71  				[(UITouch*)(mice[i].driverdata) release];
    2.72  				mice[i].driverdata = NULL;
    2.73 +				/* send mouse up */
    2.74  				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
    2.75 -
    2.76 +				/* discontinue search for this touch */
    2.77  				found = YES;
    2.78  			}
    2.79  		}
    2.80 @@ -122,11 +143,15 @@
    2.81  	UITouch *touch=nil;
    2.82  	
    2.83  	while(touch = (UITouch *)[enumerator nextObject]) {
    2.84 +		/* try to find the mouse associated with this touch */
    2.85  		int i, found = NO;
    2.86  		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
    2.87  			if (mice[i].driverdata == touch) {
    2.88 +				/* found proper mouse */
    2.89  				CGPoint locationInView = [touch locationInView: self];
    2.90 +				/* send moved event */
    2.91  				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
    2.92 +				/* discontinue search */
    2.93  				found = YES;
    2.94  			}
    2.95  		}
    2.96 @@ -138,14 +163,15 @@
    2.97  */
    2.98  #if SDL_IPHONE_KEYBOARD
    2.99  
   2.100 +/* Is the iPhone virtual keyboard visible onscreen? */
   2.101  - (BOOL)keyboardVisible {
   2.102  	return keyboardVisible;
   2.103  }
   2.104  
   2.105 -/* UITextFieldDelegate related methods */
   2.106 +/* Set ourselves up as a UITextFieldDelegate */
   2.107  - (void)initializeKeyboard {
   2.108  		
   2.109 -	textField = [[UITextField alloc] initWithFrame: CGRectZero];
   2.110 +	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
   2.111  	textField.delegate = self;
   2.112  	/* placeholder so there is something to delete! */
   2.113  	textField.text = @" ";	
   2.114 @@ -161,18 +187,12 @@
   2.115  	
   2.116  	textField.hidden = YES;
   2.117  	keyboardVisible = NO;
   2.118 +	/* add the UITextField (hidden) to our view */
   2.119  	[self addSubview: textField];
   2.120  	
   2.121 -	/*
   2.122 -	 SDL makes a copy of our keyboard.
   2.123 -	 */
   2.124 -	
   2.125 +	/* create our SDL_Keyboard */
   2.126  	SDL_Keyboard keyboard;
   2.127  	SDL_zero(keyboard);
   2.128 -	//data->keyboard = SDL_AddKeyboard(&keyboard, -1);
   2.129 -	/* 
   2.130 -	 We'll need to delete this keyboard ...
   2.131 -	 */
   2.132  	SDL_AddKeyboard(&keyboard, 0);
   2.133  	SDLKey keymap[SDL_NUM_SCANCODES];
   2.134  	SDL_GetDefaultKeymap(keymap);
   2.135 @@ -180,16 +200,19 @@
   2.136  	
   2.137  }
   2.138  
   2.139 +/* reveal onscreen virtual keyboard */
   2.140  - (void)showKeyboard {
   2.141  	keyboardVisible = YES;
   2.142  	[textField becomeFirstResponder];
   2.143  }
   2.144  
   2.145 +/* hide onscreen virtual keyboard */
   2.146  - (void)hideKeyboard {
   2.147  	keyboardVisible = NO;
   2.148  	[textField resignFirstResponder];
   2.149  }
   2.150  
   2.151 +/* UITextFieldDelegate method.  Invoked when user types something. */
   2.152  - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   2.153  	
   2.154  	if ([string length] == 0) {
   2.155 @@ -198,7 +221,8 @@
   2.156  		SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
   2.157  	}
   2.158  	else {
   2.159 -		
   2.160 +		/* go through all the characters in the string we've been sent
   2.161 +		   and convert them to key presses */
   2.162  		int i;
   2.163  		for (i=0; i<[string length]; i++) {
   2.164  			
   2.165 @@ -208,27 +232,30 @@
   2.166  			SDL_scancode code;
   2.167  			
   2.168  			if (c < 127) {
   2.169 +				/* figure out the SDL_scancode and SDL_keymod for this unichar */
   2.170  				code = unicharToUIKeyInfoTable[c].code;
   2.171  				mod  = unicharToUIKeyInfoTable[c].mod;
   2.172  			}
   2.173  			else {
   2.174 +				/* we only deal with ASCII right now */
   2.175  				code = SDL_SCANCODE_UNKNOWN;
   2.176  				mod = 0;
   2.177  			}
   2.178  			
   2.179  			if (mod & KMOD_SHIFT) {
   2.180 +				/* If character uses shift, press shift down */
   2.181  				SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
   2.182  			}
   2.183 +			/* send a keydown and keyup even for the character */
   2.184  			SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
   2.185  			SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
   2.186  			if (mod & KMOD_SHIFT) {
   2.187 +				/* If character uses shift, press shift back up */
   2.188  				SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
   2.189  			}			
   2.190 -			
   2.191  		}
   2.192 -		
   2.193  	}
   2.194 -	return NO; /* don't allow the edit(!) */
   2.195 +	return NO; /* don't allow the edit! (keep placeholder text there) */
   2.196  }
   2.197  
   2.198  /* Terminates the editing session */
   2.199 @@ -241,8 +268,6 @@
   2.200  
   2.201  @end
   2.202  
   2.203 -
   2.204 -
   2.205  /* iPhone keyboard addition functions */
   2.206  #if SDL_IPHONE_KEYBOARD
   2.207  
   2.208 @@ -348,6 +373,8 @@
   2.209  
   2.210  #else
   2.211  
   2.212 +/* stubs, used if compiled without keyboard support */
   2.213 +
   2.214  int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
   2.215  	SDL_SetError("Not compiled with keyboard support");
   2.216  	return -1;