Mercurial > sdl-ios-xcode
comparison src/video/uikit/SDL_uikitview.m @ 4660:b15e7017409b
Added Iphone support (untested)
author | Jim Grandpre <jim.tla@gmail.com> |
---|---|
date | Mon, 12 Jul 2010 22:03:25 -0400 |
parents | 64ce267332c6 |
children | 03dcb795c583 |
comparison
equal
deleted
inserted
replaced
4659:063b9455bd1a | 4660:b15e7017409b |
---|---|
20 slouken@libsdl.org | 20 slouken@libsdl.org |
21 */ | 21 */ |
22 | 22 |
23 #import "SDL_uikitview.h" | 23 #import "SDL_uikitview.h" |
24 | 24 |
25 #include "../../events/SDL_keyboard_c.h" | |
26 #include "../../events/SDL_mouse_c.h" | |
27 | |
25 #if SDL_IPHONE_KEYBOARD | 28 #if SDL_IPHONE_KEYBOARD |
26 #import "SDL_keyboard_c.h" | |
27 #import "keyinfotable.h" | 29 #import "keyinfotable.h" |
28 #import "SDL_uikitappdelegate.h" | 30 #import "SDL_uikitappdelegate.h" |
29 #import "SDL_uikitwindow.h" | 31 #import "SDL_uikitwindow.h" |
30 #endif | 32 #endif |
31 | 33 |
32 @implementation SDL_uikitview | 34 @implementation SDL_uikitview |
33 | 35 |
34 - (void)dealloc { | 36 - (void)dealloc { |
35 #if SDL_IPHONE_KEYBOARD | 37 #if SDL_IPHONE_KEYBOARD |
36 SDL_DelKeyboard(0); | |
37 [textField release]; | 38 [textField release]; |
38 #endif | 39 #endif |
39 [super dealloc]; | 40 [super dealloc]; |
40 } | 41 } |
41 | 42 |
45 | 46 |
46 #if SDL_IPHONE_KEYBOARD | 47 #if SDL_IPHONE_KEYBOARD |
47 [self initializeKeyboard]; | 48 [self initializeKeyboard]; |
48 #endif | 49 #endif |
49 | 50 |
51 #if FIXME_MULTITOUCH | |
50 int i; | 52 int i; |
51 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) { | 53 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) { |
52 mice[i].id = i; | 54 mice[i].id = i; |
53 mice[i].driverdata = NULL; | 55 mice[i].driverdata = NULL; |
54 SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1); | 56 SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1); |
55 } | 57 } |
56 self.multipleTouchEnabled = YES; | 58 self.multipleTouchEnabled = YES; |
59 #endif | |
60 #if FIXED_MULTITOUCH | |
61 SDL_Touch touch; | |
62 touch.id = 0; //TODO: Should be -1? | |
63 | |
64 //touch.driverdata = SDL_malloc(sizeof(EventTouchData)); | |
65 //EventTouchData* data = (EventTouchData*)(touch.driverdata); | |
66 | |
67 touch.x_min = 0; | |
68 touch.x_max = frame.size.width; | |
69 touch.xres = touch.x_max - touch.x_min; | |
70 touch.y_min = 0; | |
71 touch.y_max = frame.size.height; | |
72 touch.yres = touch.y_max - touch.y_min; | |
73 touch.pressure_min = 0; | |
74 touch.pressure_max = 1; | |
75 touch.pressureres = touch.pressure_max - touch.pressure_min; | |
76 | |
77 | |
78 touchId = SDL_AddTouch(&touch, "IPHONE SCREEN"); | |
79 #endif | |
57 | 80 |
58 return self; | 81 return self; |
59 | 82 |
60 } | 83 } |
61 | 84 |
62 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | 85 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
63 | 86 |
64 NSEnumerator *enumerator = [touches objectEnumerator]; | 87 NSEnumerator *enumerator = [touches objectEnumerator]; |
65 UITouch *touch =(UITouch*)[enumerator nextObject]; | 88 UITouch *touch = (UITouch*)[enumerator nextObject]; |
66 | 89 |
90 #if FIXME_MULTITOUCH | |
67 /* associate touches with mice, so long as we have slots */ | 91 /* associate touches with mice, so long as we have slots */ |
68 int i; | 92 int i; |
69 int found = 0; | 93 int found = 0; |
70 for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) { | 94 for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) { |
71 | 95 |
96 SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT); | 120 SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT); |
97 | 121 |
98 /* re-calibrate relative mouse motion */ | 122 /* re-calibrate relative mouse motion */ |
99 SDL_GetRelativeMouseState(i, NULL, NULL); | 123 SDL_GetRelativeMouseState(i, NULL, NULL); |
100 | 124 |
125 /* switch back to our old mouse */ | |
126 SDL_SelectMouse(oldMouse); | |
127 | |
101 /* grab next touch */ | 128 /* grab next touch */ |
102 touch = (UITouch*)[enumerator nextObject]; | 129 touch = (UITouch*)[enumerator nextObject]; |
103 | 130 } |
104 /* switch back to our old mouse */ | 131 #else |
105 SDL_SelectMouse(oldMouse); | 132 if (touch) { |
106 | 133 CGPoint locationInView = [touch locationInView: self]; |
107 } | 134 |
135 /* send moved event */ | |
136 SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y); | |
137 | |
138 /* send mouse down event */ | |
139 SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT); | |
140 } | |
141 #endif | |
142 | |
143 #if FIXED_MULTITOUCH | |
144 while(touch) { | |
145 CGPoint locationInView = [touch locationInView: self]; | |
146 SDL_SendFingerDown(touchId,(int)touch, | |
147 SDL_TRUE,locationInView.x,locationInView.y, | |
148 1); | |
149 | |
150 touch = (UITouch*)[enumerator nextObject]; | |
151 } | |
152 #endif | |
108 } | 153 } |
109 | 154 |
110 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { | 155 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
111 | 156 |
112 NSEnumerator *enumerator = [touches objectEnumerator]; | 157 NSEnumerator *enumerator = [touches objectEnumerator]; |
113 UITouch *touch=nil; | 158 UITouch *touch = (UITouch*)[enumerator nextObject]; |
114 | 159 |
115 while(touch = (UITouch *)[enumerator nextObject]) { | 160 #if FIXME_MULTITOUCH |
161 while(touch) { | |
116 /* search for the mouse slot associated with this touch */ | 162 /* search for the mouse slot associated with this touch */ |
117 int i, found = NO; | 163 int i, found = NO; |
118 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { | 164 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { |
119 if (mice[i].driverdata == touch) { | 165 if (mice[i].driverdata == touch) { |
120 /* found the mouse associate with the touch */ | 166 /* found the mouse associate with the touch */ |
124 SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT); | 170 SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT); |
125 /* discontinue search for this touch */ | 171 /* discontinue search for this touch */ |
126 found = YES; | 172 found = YES; |
127 } | 173 } |
128 } | 174 } |
129 } | 175 |
176 /* grab next touch */ | |
177 touch = (UITouch*)[enumerator nextObject]; | |
178 } | |
179 #else | |
180 if (touch) { | |
181 /* send mouse up */ | |
182 SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT); | |
183 } | |
184 #endif | |
185 #if FIXED_MULTITOUCH | |
186 while(touch) { | |
187 CGPoint locationInView = [touch locationInView: self]; | |
188 SDL_SendFingerDown(touchId,(int)touch, | |
189 SDL_FALSE,locationInView.x,locationInView.y, | |
190 1); | |
191 | |
192 touch = (UITouch*)[enumerator nextObject]; | |
193 } | |
194 #endif | |
130 } | 195 } |
131 | 196 |
132 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { | 197 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
133 /* | 198 /* |
134 this can happen if the user puts more than 5 touches on the screen | 199 this can happen if the user puts more than 5 touches on the screen |
139 } | 204 } |
140 | 205 |
141 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { | 206 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
142 | 207 |
143 NSEnumerator *enumerator = [touches objectEnumerator]; | 208 NSEnumerator *enumerator = [touches objectEnumerator]; |
144 UITouch *touch=nil; | 209 UITouch *touch = (UITouch*)[enumerator nextObject]; |
145 | 210 |
146 while(touch = (UITouch *)[enumerator nextObject]) { | 211 #if FIXME_MULTITOUCH |
212 while(touch) { | |
147 /* try to find the mouse associated with this touch */ | 213 /* try to find the mouse associated with this touch */ |
148 int i, found = NO; | 214 int i, found = NO; |
149 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { | 215 for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { |
150 if (mice[i].driverdata == touch) { | 216 if (mice[i].driverdata == touch) { |
151 /* found proper mouse */ | 217 /* found proper mouse */ |
154 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); | 220 SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); |
155 /* discontinue search */ | 221 /* discontinue search */ |
156 found = YES; | 222 found = YES; |
157 } | 223 } |
158 } | 224 } |
159 } | 225 |
226 /* grab next touch */ | |
227 touch = (UITouch*)[enumerator nextObject]; | |
228 } | |
229 #else | |
230 if (touch) { | |
231 CGPoint locationInView = [touch locationInView: self]; | |
232 | |
233 /* send moved event */ | |
234 SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y); | |
235 } | |
236 #endif | |
237 | |
238 #if FIXED_MULTITOUCH | |
239 while(touch) { | |
240 CGPoint locationInView = [touch locationInView: self]; | |
241 SDL_SendTouchMotion(touchId,(int)touch, | |
242 SDL_FALSE,locationInView.x,locationInView.y, | |
243 1); | |
244 | |
245 touch = (UITouch*)[enumerator nextObject]; | |
246 } | |
247 #endif | |
160 } | 248 } |
161 | 249 |
162 /* | 250 /* |
163 ---- Keyboard related functionality below this line ---- | 251 ---- Keyboard related functionality below this line ---- |
164 */ | 252 */ |
188 | 276 |
189 textField.hidden = YES; | 277 textField.hidden = YES; |
190 keyboardVisible = NO; | 278 keyboardVisible = NO; |
191 /* add the UITextField (hidden) to our view */ | 279 /* add the UITextField (hidden) to our view */ |
192 [self addSubview: textField]; | 280 [self addSubview: textField]; |
193 | |
194 /* create our SDL_Keyboard */ | |
195 SDL_Keyboard keyboard; | |
196 SDL_zero(keyboard); | |
197 SDL_AddKeyboard(&keyboard, 0); | |
198 SDLKey keymap[SDL_NUM_SCANCODES]; | |
199 SDL_GetDefaultKeymap(keymap); | |
200 SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES); | |
201 | |
202 } | 281 } |
203 | 282 |
204 /* reveal onscreen virtual keyboard */ | 283 /* reveal onscreen virtual keyboard */ |
205 - (void)showKeyboard { | 284 - (void)showKeyboard { |
206 keyboardVisible = YES; | 285 keyboardVisible = YES; |
216 /* UITextFieldDelegate method. Invoked when user types something. */ | 295 /* UITextFieldDelegate method. Invoked when user types something. */ |
217 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { | 296 - (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
218 | 297 |
219 if ([string length] == 0) { | 298 if ([string length] == 0) { |
220 /* it wants to replace text with nothing, ie a delete */ | 299 /* it wants to replace text with nothing, ie a delete */ |
221 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE); | 300 SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE); |
222 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); | 301 SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE); |
223 } | 302 } |
224 else { | 303 else { |
225 /* go through all the characters in the string we've been sent | 304 /* go through all the characters in the string we've been sent |
226 and convert them to key presses */ | 305 and convert them to key presses */ |
227 int i; | 306 int i; |
243 mod = 0; | 322 mod = 0; |
244 } | 323 } |
245 | 324 |
246 if (mod & KMOD_SHIFT) { | 325 if (mod & KMOD_SHIFT) { |
247 /* If character uses shift, press shift down */ | 326 /* If character uses shift, press shift down */ |
248 SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); | 327 SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT); |
249 } | 328 } |
250 /* send a keydown and keyup even for the character */ | 329 /* send a keydown and keyup even for the character */ |
251 SDL_SendKeyboardKey( 0, SDL_PRESSED, code); | 330 SDL_SendKeyboardKey(SDL_PRESSED, code); |
252 SDL_SendKeyboardKey( 0, SDL_RELEASED, code); | 331 SDL_SendKeyboardKey(SDL_RELEASED, code); |
253 if (mod & KMOD_SHIFT) { | 332 if (mod & KMOD_SHIFT) { |
254 /* If character uses shift, press shift back up */ | 333 /* If character uses shift, press shift back up */ |
255 SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); | 334 SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT); |
256 } | 335 } |
257 } | 336 } |
258 } | 337 } |
259 return NO; /* don't allow the edit! (keep placeholder text there) */ | 338 return NO; /* don't allow the edit! (keep placeholder text there) */ |
260 } | 339 } |