From 26466a37f2aaff86b44d610e55a30bc23078efb9 Mon Sep 17 00:00:00 2001 From: Holmes Futrell Date: Sat, 16 Aug 2008 00:16:32 +0000 Subject: [PATCH] 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 | 9 +---- src/video/uikit/SDL_uikitview.m | 69 +++++++++++++++++++++++---------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/src/video/uikit/SDL_uikitview.h b/src/video/uikit/SDL_uikitview.h index e41df76cb..bea0f1e9a 100644 --- a/src/video/uikit/SDL_uikitview.h +++ b/src/video/uikit/SDL_uikitview.h @@ -53,11 +53,4 @@ @property (readonly) BOOL keyboardVisible; #endif -@end - -#if SDL_IPHONE_KEYBOARD -extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_WindowID windowID); -extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_WindowID windowID); -extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID); -extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_WindowID windowID); -#endif +@end \ No newline at end of file diff --git a/src/video/uikit/SDL_uikitview.m b/src/video/uikit/SDL_uikitview.m index 341cdccec..49e6c3d32 100644 --- a/src/video/uikit/SDL_uikitview.m +++ b/src/video/uikit/SDL_uikitview.m @@ -33,6 +33,7 @@ @implementation SDL_uikitview - (void)dealloc { #if SDL_IPHONE_KEYBOARD + SDL_DelKeyboard(0); [textField release]; #endif [super dealloc]; @@ -60,29 +61,46 @@ - (id)initWithFrame:(CGRect)frame { - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSEnumerator *enumerator = [touches objectEnumerator]; - UITouch *touch=(UITouch*)[enumerator nextObject]; + UITouch *touch =(UITouch*)[enumerator nextObject]; - // associate touches with mice, so long as we have slots + /* associate touches with mice, so long as we have slots */ int i; int found = 0; for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) { - // check if this mouse is already tracking a touch + /* check if this mouse is already tracking a touch */ if (mice[i].driverdata != NULL) { continue; } - + /* + mouse not associated with anything right now, + associate the touch with this mouse + */ found = 1; + /* save old mouse so we can switch back */ int oldMouse = SDL_SelectMouse(-1); + + /* select this slot's mouse */ SDL_SelectMouse(i); CGPoint locationInView = [touch locationInView: self]; + + /* set driver data to touch object, we'll use touch object later */ mice[i].driverdata = [touch retain]; + + /* send moved event */ SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y); + + /* send mouse down event */ SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT); + + /* re-calibrate relative mouse motion */ SDL_GetRelativeMouseState(NULL, NULL); + + /* grab next touch */ touch = (UITouch*)[enumerator nextObject]; + /* switch back to our old mouse */ SDL_SelectMouse(oldMouse); } @@ -94,13 +112,16 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch=nil; while(touch = (UITouch *)[enumerator nextObject]) { + /* search for the mouse slot associated with this touch */ int i, found = NO; for (i=0; ikeyboard = SDL_AddKeyboard(&keyboard, -1); - /* - We'll need to delete this keyboard ... - */ SDL_AddKeyboard(&keyboard, 0); SDLKey keymap[SDL_NUM_SCANCODES]; SDL_GetDefaultKeymap(keymap); @@ -180,16 +200,19 @@ - (void)initializeKeyboard { } +/* reveal onscreen virtual keyboard */ - (void)showKeyboard { keyboardVisible = YES; [textField becomeFirstResponder]; } +/* hide onscreen virtual keyboard */ - (void)hideKeyboard { keyboardVisible = NO; [textField resignFirstResponder]; } +/* UITextFieldDelegate method. Invoked when user types something. */ - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([string length] == 0) { @@ -198,7 +221,8 @@ - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRan SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); } else { - + /* go through all the characters in the string we've been sent + and convert them to key presses */ int i; for (i=0; i<[string length]; i++) { @@ -208,27 +232,30 @@ - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRan SDL_scancode code; if (c < 127) { + /* figure out the SDL_scancode and SDL_keymod for this unichar */ code = unicharToUIKeyInfoTable[c].code; mod = unicharToUIKeyInfoTable[c].mod; } else { + /* we only deal with ASCII right now */ code = SDL_SCANCODE_UNKNOWN; mod = 0; } if (mod & KMOD_SHIFT) { + /* If character uses shift, press shift down */ SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); } + /* send a keydown and keyup even for the character */ SDL_SendKeyboardKey( 0, SDL_PRESSED, code); SDL_SendKeyboardKey( 0, SDL_RELEASED, code); if (mod & KMOD_SHIFT) { + /* If character uses shift, press shift back up */ SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); } - } - } - return NO; /* don't allow the edit(!) */ + return NO; /* don't allow the edit! (keep placeholder text there) */ } /* Terminates the editing session */ @@ -241,8 +268,6 @@ - (BOOL)textFieldShouldReturn:(UITextField*)_textField { @end - - /* iPhone keyboard addition functions */ #if SDL_IPHONE_KEYBOARD @@ -348,6 +373,8 @@ int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) { #else +/* stubs, used if compiled without keyboard support */ + int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) { SDL_SetError("Not compiled with keyboard support"); return -1;