comparison src/video/uikit/SDL_uikitview.m @ 2450:96124abbcede gsoc2008_iphone

Added comments, view now deletes keyboard upon dealloc, function declarations for iPhone keyboard additions now moved to SDL_uikitkeyboard.h.
author Holmes Futrell <hfutrell@umail.ucsb.edu>
date Sat, 16 Aug 2008 00:16:32 +0000
parents 491958a6c881
children
comparison
equal deleted inserted replaced
2449:4a07dc228e93 2450:96124abbcede
31 31
32 @implementation SDL_uikitview 32 @implementation SDL_uikitview
33 33
34 - (void)dealloc { 34 - (void)dealloc {
35 #if SDL_IPHONE_KEYBOARD 35 #if SDL_IPHONE_KEYBOARD
36 SDL_DelKeyboard(0);
36 [textField release]; 37 [textField release];
37 #endif 38 #endif
38 [super dealloc]; 39 [super dealloc];
39 } 40 }
40 41
58 } 59 }
59 60
60 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 61 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
61 62
62 NSEnumerator *enumerator = [touches objectEnumerator]; 63 NSEnumerator *enumerator = [touches objectEnumerator];
63 UITouch *touch=(UITouch*)[enumerator nextObject]; 64 UITouch *touch =(UITouch*)[enumerator nextObject];
64 65
65 // associate touches with mice, so long as we have slots 66 /* associate touches with mice, so long as we have slots */
66 int i; 67 int i;
67 int found = 0; 68 int found = 0;
68 for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) { 69 for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
69 70
70 // check if this mouse is already tracking a touch 71 /* check if this mouse is already tracking a touch */
71 if (mice[i].driverdata != NULL) { 72 if (mice[i].driverdata != NULL) {
72 continue; 73 continue;
73 } 74 }
74 75 /*
76 mouse not associated with anything right now,
77 associate the touch with this mouse
78 */
75 found = 1; 79 found = 1;
76 80
81 /* save old mouse so we can switch back */
77 int oldMouse = SDL_SelectMouse(-1); 82 int oldMouse = SDL_SelectMouse(-1);
83
84 /* select this slot's mouse */
78 SDL_SelectMouse(i); 85 SDL_SelectMouse(i);
79 CGPoint locationInView = [touch locationInView: self]; 86 CGPoint locationInView = [touch locationInView: self];
87
88 /* set driver data to touch object, we'll use touch object later */
80 mice[i].driverdata = [touch retain]; 89 mice[i].driverdata = [touch retain];
90
91 /* send moved event */
81 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y); 92 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
93
94 /* send mouse down event */
82 SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT); 95 SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
96
97 /* re-calibrate relative mouse motion */
83 SDL_GetRelativeMouseState(NULL, NULL); 98 SDL_GetRelativeMouseState(NULL, NULL);
99
100 /* grab next touch */
84 touch = (UITouch*)[enumerator nextObject]; 101 touch = (UITouch*)[enumerator nextObject];
85 102
103 /* switch back to our old mouse */
86 SDL_SelectMouse(oldMouse); 104 SDL_SelectMouse(oldMouse);
87 105
88 } 106 }
89 } 107 }
90 108
92 110
93 NSEnumerator *enumerator = [touches objectEnumerator]; 111 NSEnumerator *enumerator = [touches objectEnumerator];
94 UITouch *touch=nil; 112 UITouch *touch=nil;
95 113
96 while(touch = (UITouch *)[enumerator nextObject]) { 114 while(touch = (UITouch *)[enumerator nextObject]) {
115 /* search for the mouse slot associated with this touch */
97 int i, found = NO; 116 int i, found = NO;
98 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { 117 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
99 if (mice[i].driverdata == touch) { 118 if (mice[i].driverdata == touch) {
119 /* found the mouse associate with the touch */
100 [(UITouch*)(mice[i].driverdata) release]; 120 [(UITouch*)(mice[i].driverdata) release];
101 mice[i].driverdata = NULL; 121 mice[i].driverdata = NULL;
122 /* send mouse up */
102 SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT); 123 SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
103 124 /* discontinue search for this touch */
104 found = YES; 125 found = YES;
105 } 126 }
106 } 127 }
107 } 128 }
108 } 129 }
120 141
121 NSEnumerator *enumerator = [touches objectEnumerator]; 142 NSEnumerator *enumerator = [touches objectEnumerator];
122 UITouch *touch=nil; 143 UITouch *touch=nil;
123 144
124 while(touch = (UITouch *)[enumerator nextObject]) { 145 while(touch = (UITouch *)[enumerator nextObject]) {
146 /* try to find the mouse associated with this touch */
125 int i, found = NO; 147 int i, found = NO;
126 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { 148 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
127 if (mice[i].driverdata == touch) { 149 if (mice[i].driverdata == touch) {
150 /* found proper mouse */
128 CGPoint locationInView = [touch locationInView: self]; 151 CGPoint locationInView = [touch locationInView: self];
152 /* send moved event */
129 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y); 153 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
154 /* discontinue search */
130 found = YES; 155 found = YES;
131 } 156 }
132 } 157 }
133 } 158 }
134 } 159 }
136 /* 161 /*
137 ---- Keyboard related functionality below this line ---- 162 ---- Keyboard related functionality below this line ----
138 */ 163 */
139 #if SDL_IPHONE_KEYBOARD 164 #if SDL_IPHONE_KEYBOARD
140 165
166 /* Is the iPhone virtual keyboard visible onscreen? */
141 - (BOOL)keyboardVisible { 167 - (BOOL)keyboardVisible {
142 return keyboardVisible; 168 return keyboardVisible;
143 } 169 }
144 170
145 /* UITextFieldDelegate related methods */ 171 /* Set ourselves up as a UITextFieldDelegate */
146 - (void)initializeKeyboard { 172 - (void)initializeKeyboard {
147 173
148 textField = [[UITextField alloc] initWithFrame: CGRectZero]; 174 textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
149 textField.delegate = self; 175 textField.delegate = self;
150 /* placeholder so there is something to delete! */ 176 /* placeholder so there is something to delete! */
151 textField.text = @" "; 177 textField.text = @" ";
152 178
153 /* set UITextInputTrait properties, mostly to defaults */ 179 /* set UITextInputTrait properties, mostly to defaults */
159 textField.returnKeyType = UIReturnKeyDefault; 185 textField.returnKeyType = UIReturnKeyDefault;
160 textField.secureTextEntry = NO; 186 textField.secureTextEntry = NO;
161 187
162 textField.hidden = YES; 188 textField.hidden = YES;
163 keyboardVisible = NO; 189 keyboardVisible = NO;
190 /* add the UITextField (hidden) to our view */
164 [self addSubview: textField]; 191 [self addSubview: textField];
165 192
166 /* 193 /* create our SDL_Keyboard */
167 SDL makes a copy of our keyboard.
168 */
169
170 SDL_Keyboard keyboard; 194 SDL_Keyboard keyboard;
171 SDL_zero(keyboard); 195 SDL_zero(keyboard);
172 //data->keyboard = SDL_AddKeyboard(&keyboard, -1);
173 /*
174 We'll need to delete this keyboard ...
175 */
176 SDL_AddKeyboard(&keyboard, 0); 196 SDL_AddKeyboard(&keyboard, 0);
177 SDLKey keymap[SDL_NUM_SCANCODES]; 197 SDLKey keymap[SDL_NUM_SCANCODES];
178 SDL_GetDefaultKeymap(keymap); 198 SDL_GetDefaultKeymap(keymap);
179 SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES); 199 SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
180 200
181 } 201 }
182 202
203 /* reveal onscreen virtual keyboard */
183 - (void)showKeyboard { 204 - (void)showKeyboard {
184 keyboardVisible = YES; 205 keyboardVisible = YES;
185 [textField becomeFirstResponder]; 206 [textField becomeFirstResponder];
186 } 207 }
187 208
209 /* hide onscreen virtual keyboard */
188 - (void)hideKeyboard { 210 - (void)hideKeyboard {
189 keyboardVisible = NO; 211 keyboardVisible = NO;
190 [textField resignFirstResponder]; 212 [textField resignFirstResponder];
191 } 213 }
192 214
215 /* UITextFieldDelegate method. Invoked when user types something. */
193 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { 216 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
194 217
195 if ([string length] == 0) { 218 if ([string length] == 0) {
196 /* it wants to replace text with nothing, ie a delete */ 219 /* it wants to replace text with nothing, ie a delete */
197 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE); 220 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
198 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); 221 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
199 } 222 }
200 else { 223 else {
201 224 /* go through all the characters in the string we've been sent
225 and convert them to key presses */
202 int i; 226 int i;
203 for (i=0; i<[string length]; i++) { 227 for (i=0; i<[string length]; i++) {
204 228
205 unichar c = [string characterAtIndex: i]; 229 unichar c = [string characterAtIndex: i];
206 230
207 Uint16 mod = 0; 231 Uint16 mod = 0;
208 SDL_scancode code; 232 SDL_scancode code;
209 233
210 if (c < 127) { 234 if (c < 127) {
235 /* figure out the SDL_scancode and SDL_keymod for this unichar */
211 code = unicharToUIKeyInfoTable[c].code; 236 code = unicharToUIKeyInfoTable[c].code;
212 mod = unicharToUIKeyInfoTable[c].mod; 237 mod = unicharToUIKeyInfoTable[c].mod;
213 } 238 }
214 else { 239 else {
240 /* we only deal with ASCII right now */
215 code = SDL_SCANCODE_UNKNOWN; 241 code = SDL_SCANCODE_UNKNOWN;
216 mod = 0; 242 mod = 0;
217 } 243 }
218 244
219 if (mod & KMOD_SHIFT) { 245 if (mod & KMOD_SHIFT) {
246 /* If character uses shift, press shift down */
220 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); 247 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
221 } 248 }
249 /* send a keydown and keyup even for the character */
222 SDL_SendKeyboardKey( 0, SDL_PRESSED, code); 250 SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
223 SDL_SendKeyboardKey( 0, SDL_RELEASED, code); 251 SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
224 if (mod & KMOD_SHIFT) { 252 if (mod & KMOD_SHIFT) {
253 /* If character uses shift, press shift back up */
225 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); 254 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
226 } 255 }
227 256 }
228 } 257 }
229 258 return NO; /* don't allow the edit! (keep placeholder text there) */
230 }
231 return NO; /* don't allow the edit(!) */
232 } 259 }
233 260
234 /* Terminates the editing session */ 261 /* Terminates the editing session */
235 - (BOOL)textFieldShouldReturn:(UITextField*)_textField { 262 - (BOOL)textFieldShouldReturn:(UITextField*)_textField {
236 [self hideKeyboard]; 263 [self hideKeyboard];
239 266
240 #endif 267 #endif
241 268
242 @end 269 @end
243 270
244
245
246 /* iPhone keyboard addition functions */ 271 /* iPhone keyboard addition functions */
247 #if SDL_IPHONE_KEYBOARD 272 #if SDL_IPHONE_KEYBOARD
248 273
249 int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) { 274 int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
250 275
346 } 371 }
347 } 372 }
348 373
349 #else 374 #else
350 375
376 /* stubs, used if compiled without keyboard support */
377
351 int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) { 378 int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
352 SDL_SetError("Not compiled with keyboard support"); 379 SDL_SetError("Not compiled with keyboard support");
353 return -1; 380 return -1;
354 } 381 }
355 382