155 +(void) movingDown { |
159 +(void) movingDown { |
156 HW_aimDown(); |
160 HW_aimDown(); |
157 } |
161 } |
158 |
162 |
159 +(void) chatBegin { |
163 +(void) chatBegin { |
160 HW_chat(); |
164 //TODO: implement a UITextScroll and put received chat lines in there |
161 SDL_iPhoneKeyboardShow([SDLUIKitDelegate sharedAppDelegate].uiwindow); |
|
162 } |
165 } |
163 |
166 |
164 #pragma mark - |
167 #pragma mark - |
165 #pragma mark Custom SDL_UIView input handling |
168 #pragma mark Custom SDL_UIView input handling |
166 #define kMinimumPinchDelta 50 |
169 #define kMinimumPinchDelta 50 |
167 #define kMinimumGestureLength 10 |
170 #define kMinimumGestureLength 10 |
168 #define kMaximumVariance 3 |
171 #define kMaximumVariance 3 |
169 |
172 |
170 // we override default touch input to implement our own gestures |
173 // we override default touch input to implement our own gestures |
171 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
174 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
172 /* |
175 NSArray *twoTouches; |
173 NSEnumerator *enumerator = [touches objectEnumerator]; |
|
174 UITouch *touch =(UITouch*)[enumerator nextObject]; |
|
175 |
|
176 // associate touches with mice, so long as we have slots |
|
177 int i; |
|
178 int found = 0; |
|
179 for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) { |
|
180 |
|
181 // check if this mouse is already tracking a touch |
|
182 if (mice[i].driverdata != NULL) { |
|
183 continue; |
|
184 } |
|
185 |
|
186 // mouse not associated with anything right now, associate the touch with this mouse |
|
187 found = 1; |
|
188 |
|
189 // save old mouse so we can switch back |
|
190 int oldMouse = SDL_SelectMouse(-1); |
|
191 |
|
192 // select this slot's mouse |
|
193 SDL_SelectMouse(i); |
|
194 CGPoint locationInView = [touch locationInView: self]; |
|
195 |
|
196 CGFloat oldX = locationInView.x; |
|
197 locationInView.x = locationInView.y; |
|
198 locationInView.y = 320 - oldX; |
|
199 |
|
200 // set driver data to touch object, we'll use touch object later |
|
201 mice[i].driverdata = [touch retain]; |
|
202 |
|
203 // send moved event |
|
204 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); |
|
205 |
|
206 // send mouse down event |
|
207 SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT); |
|
208 |
|
209 // re-calibrate relative mouse motion |
|
210 SDL_GetRelativeMouseState(i, NULL, NULL); |
|
211 |
|
212 // grab next touch |
|
213 touch = (UITouch*)[enumerator nextObject]; |
|
214 |
|
215 // switch back to our old mouse |
|
216 SDL_SelectMouse(oldMouse); |
|
217 |
|
218 } |
|
219 */ |
|
220 |
|
221 UITouch *touch = [touches anyObject]; |
176 UITouch *touch = [touches anyObject]; |
222 |
177 |
223 switch ([touches count]) { |
178 switch ([touches count]) { |
224 case 1: |
179 case 1: |
225 gestureStartPoint = [touch locationInView:self]; |
180 gestureStartPoint = [touch locationInView:self]; |
226 initialDistanceForPinching = 0; |
181 initialDistanceForPinching = 0; |
227 switch ([touch tapCount]) { |
182 switch ([touch tapCount]) { |
228 case 1: |
183 case 1: |
229 NSLog(@"X:%d Y:%d", (int) gestureStartPoint.x, gestureStartPoint.y ); |
184 |
|
185 NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
230 SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
186 SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
231 (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
187 (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
232 HW_click(); |
188 HW_click(); |
233 break; |
189 break; |
234 case 2: |
190 case 2: |
334 currentDistanceOfPinching = initialDistanceForPinching; |
270 currentDistanceOfPinching = initialDistanceForPinching; |
335 break; |
271 break; |
336 default: |
272 default: |
337 break; |
273 break; |
338 } |
274 } |
339 |
275 } |
340 /* |
276 |
341 NSEnumerator *enumerator = [touches objectEnumerator]; |
277 /* |
342 UITouch *touch=nil;while(touch = (UITouch *)[enumerator nextObject]) { |
|
343 // try to find the mouse associated with this touch |
|
344 int i, found = NO; |
|
345 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { |
|
346 if (mice[i].driverdata == touch) { |
|
347 // found proper mouse |
|
348 CGPoint locationInView = [touch locationInView: self]; |
|
349 // send moved event |
|
350 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); |
|
351 // discontinue search |
|
352 found = YES; |
|
353 } |
|
354 } |
|
355 } |
|
356 */ |
|
357 } |
|
358 |
|
359 #pragma mark - |
278 #pragma mark - |
360 #pragma mark default routines |
279 #pragma mark Keyboard related functionality |
361 /* |
|
362 ---- Keyboard related functionality below this line ---- |
|
363 */ |
|
364 #if SDL_IPHONE_KEYBOARD |
280 #if SDL_IPHONE_KEYBOARD |
365 |
281 // Is the iPhone virtual keyboard visible onscreen? |
366 /* Is the iPhone virtual keyboard visible onscreen? */ |
|
367 - (BOOL)keyboardVisible { |
282 - (BOOL)keyboardVisible { |
368 return keyboardVisible; |
283 return keyboardVisible; |
369 } |
284 } |
370 |
285 |
371 /* Set ourselves up as a UITextFieldDelegate */ |
286 // Set ourselves up as a UITextFieldDelegate |
372 - (void)initializeKeyboard { |
287 - (void)initializeKeyboard { |
373 |
288 |
374 textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease]; |
289 textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease]; |
375 textField.delegate = self; |
290 textField.delegate = self; |
376 /* placeholder so there is something to delete! */ |
291 // placeholder so there is something to delete! |
377 textField.text = @" "; |
292 textField.text = @" "; |
378 |
293 |
379 /* set UITextInputTrait properties, mostly to defaults */ |
294 // set UITextInputTrait properties, mostly to defaults |
380 textField.autocapitalizationType = UITextAutocapitalizationTypeNone; |
295 textField.autocapitalizationType = UITextAutocapitalizationTypeNone; |
381 textField.autocorrectionType = UITextAutocorrectionTypeNo; |
296 textField.autocorrectionType = UITextAutocorrectionTypeNo; |
382 textField.enablesReturnKeyAutomatically = NO; |
297 textField.enablesReturnKeyAutomatically = NO; |
383 textField.keyboardAppearance = UIKeyboardAppearanceDefault; |
298 textField.keyboardAppearance = UIKeyboardAppearanceDefault; |
384 textField.keyboardType = UIKeyboardTypeDefault; |
299 textField.keyboardType = UIKeyboardTypeDefault; |
385 textField.returnKeyType = UIReturnKeyDefault; |
300 textField.returnKeyType = UIReturnKeyDefault; |
386 textField.secureTextEntry = NO; |
301 textField.secureTextEntry = NO; |
387 |
302 |
388 textField.hidden = YES; |
303 textField.hidden = YES; |
389 keyboardVisible = NO; |
304 keyboardVisible = NO; |
390 /* add the UITextField (hidden) to our view */ |
305 // add the UITextField (hidden) to our view |
391 [self addSubview: textField]; |
306 [self addSubview: textField]; |
392 |
307 |
393 /* create our SDL_Keyboard */ |
308 // create our SDL_Keyboard |
394 SDL_Keyboard keyboard; |
309 SDL_Keyboard keyboard; |
395 SDL_zero(keyboard); |
310 SDL_zero(keyboard); |
396 SDL_AddKeyboard(&keyboard, 0); |
311 SDL_AddKeyboard(&keyboard, 0); |
397 SDLKey keymap[SDL_NUM_SCANCODES]; |
312 SDLKey keymap[SDL_NUM_SCANCODES]; |
398 SDL_GetDefaultKeymap(keymap); |
313 SDL_GetDefaultKeymap(keymap); |
399 SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES); |
314 SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES); |
400 |
315 |
401 } |
316 } |
402 |
317 |
403 /* reveal onscreen virtual keyboard */ |
318 // reveal onscreen virtual keyboard |
404 - (void)showKeyboard { |
319 - (void)showKeyboard { |
405 keyboardVisible = YES; |
320 keyboardVisible = YES; |
406 [textField becomeFirstResponder]; |
321 [textField becomeFirstResponder]; |
407 } |
322 } |
408 |
323 |
409 /* hide onscreen virtual keyboard */ |
324 // hide onscreen virtual keyboard |
410 - (void)hideKeyboard { |
325 - (void)hideKeyboard { |
411 keyboardVisible = NO; |
326 keyboardVisible = NO; |
412 [textField resignFirstResponder]; |
327 [textField resignFirstResponder]; |
413 } |
328 } |
414 |
329 |
415 /* UITextFieldDelegate method. Invoked when user types something. */ |
330 // UITextFieldDelegate method. Invoked when user types something. |
416 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
331 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
417 |
332 |
418 if ([string length] == 0) { |
333 if ([string length] == 0) { |
419 /* it wants to replace text with nothing, ie a delete */ |
334 // it wants to replace text with nothing, ie a delete |
420 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE); |
335 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE); |
421 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); |
336 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); |
422 } |
337 } |
423 else { |
338 else { |
424 /* go through all the characters in the string we've been sent |
339 // go through all the characters in the string we've been sent and convert them to key presses |
425 and convert them to key presses */ |
|
426 int i; |
340 int i; |
427 for (i=0; i<[string length]; i++) { |
341 for (i=0; i<[string length]; i++) { |
428 |
342 |
429 unichar c = [string characterAtIndex: i]; |
343 unichar c = [string characterAtIndex: i]; |
430 |
344 |
431 Uint16 mod = 0; |
345 Uint16 mod = 0; |
432 SDL_scancode code; |
346 SDL_scancode code; |
433 |
347 |
434 if (c < 127) { |
348 if (c < 127) { |
435 /* figure out the SDL_scancode and SDL_keymod for this unichar */ |
349 // figure out the SDL_scancode and SDL_keymod for this unichar |
436 code = unicharToUIKeyInfoTable[c].code; |
350 code = unicharToUIKeyInfoTable[c].code; |
437 mod = unicharToUIKeyInfoTable[c].mod; |
351 mod = unicharToUIKeyInfoTable[c].mod; |
438 } |
352 } |
439 else { |
353 else { |
440 /* we only deal with ASCII right now */ |
354 // we only deal with ASCII right now |
441 code = SDL_SCANCODE_UNKNOWN; |
355 code = SDL_SCANCODE_UNKNOWN; |
442 mod = 0; |
356 mod = 0; |
443 } |
357 } |
444 |
358 |
445 if (mod & KMOD_SHIFT) { |
359 if (mod & KMOD_SHIFT) { |
446 /* If character uses shift, press shift down */ |
360 // If character uses shift, press shift down |
447 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); |
361 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); |
448 } |
362 } |
449 /* send a keydown and keyup even for the character */ |
363 // send a keydown and keyup even for the character |
450 SDL_SendKeyboardKey( 0, SDL_PRESSED, code); |
364 SDL_SendKeyboardKey( 0, SDL_PRESSED, code); |
451 SDL_SendKeyboardKey( 0, SDL_RELEASED, code); |
365 SDL_SendKeyboardKey( 0, SDL_RELEASED, code); |
452 if (mod & KMOD_SHIFT) { |
366 if (mod & KMOD_SHIFT) { |
453 /* If character uses shift, press shift back up */ |
367 // If character uses shift, press shift back up |
454 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); |
368 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); |
455 } |
369 } |
456 } |
370 } |
457 } |
371 } |
458 return NO; /* don't allow the edit! (keep placeholder text there) */ |
372 return NO; // don't allow the edit! (keep placeholder text there) |
459 } |
373 } |
460 |
374 |
461 /* Terminates the editing session */ |
375 // Terminates the editing session |
462 - (BOOL)textFieldShouldReturn:(UITextField*)_textField { |
376 - (BOOL)textFieldShouldReturn:(UITextField*)_textField { |
463 [self hideKeyboard]; |
377 [self hideKeyboard]; |
464 return YES; |
378 return YES; |
465 } |
379 } |
466 |
380 |
467 #endif |
381 #endif |
468 |
382 */ |
469 @end |
383 @end |
470 |
384 /* |
471 /* iPhone keyboard addition functions */ |
385 // iPhone keyboard addition functions |
472 #if SDL_IPHONE_KEYBOARD |
386 #if SDL_IPHONE_KEYBOARD |
473 |
387 |
474 int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
388 int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
475 SDL_WindowData *data; |
389 SDL_WindowData *data; |
476 SDL_uikitview *view; |
390 SDL_uikitview *view; |