author | koda |
Mon, 01 Feb 2010 21:26:15 +0000 | |
changeset 2738 | bfccb2ec4334 |
parent 2734 | fb9ad1587054 |
child 2740 | 03df0573a9fd |
permissions | -rw-r--r-- |
2688 | 1 |
/* |
2 |
SDL - Simple DirectMedia Layer |
|
3 |
Copyright (C) 1997-2009 Sam Lantinga |
|
4 |
||
5 |
This library is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU Lesser General Public |
|
7 |
License as published by the Free Software Foundation; either |
|
8 |
version 2.1 of the License, or (at your option) any later version. |
|
9 |
||
10 |
This library is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
Lesser General Public License for more details. |
|
14 |
||
15 |
You should have received a copy of the GNU Lesser General Public |
|
16 |
License along with this library; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
||
19 |
Sam Lantinga, mods for Hedgewars by Vittorio Giovara |
|
20 |
slouken@libsdl.org, vittorio.giovara@gmail.com |
|
21 |
*/ |
|
22 |
||
23 |
#include "PascalImports.h" |
|
24 |
#import "SDL_uikitview.h" |
|
25 |
#import "SDL_uikitappdelegate.h" |
|
26 |
||
27 |
#if SDL_IPHONE_KEYBOARD |
|
28 |
#import "SDL_keyboard_c.h" |
|
29 |
#import "keyinfotable.h" |
|
30 |
#import "SDL_uikitwindow.h" |
|
31 |
#endif |
|
32 |
||
33 |
@implementation SDL_uikitview |
|
34 |
||
2689 | 35 |
// they have to be global variables to allow showControls() to use them |
36 |
UIButton *attackButton, *menuButton; |
|
37 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
38 |
-(void) dealloc { |
2688 | 39 |
#if SDL_IPHONE_KEYBOARD |
40 |
SDL_DelKeyboard(0); |
|
41 |
[textField release]; |
|
42 |
#endif |
|
2689 | 43 |
[menuButton release]; |
44 |
[attackButton release]; |
|
2688 | 45 |
[super dealloc]; |
46 |
} |
|
47 |
||
48 |
- (id)initWithFrame:(CGRect)frame { |
|
2689 | 49 |
// the addTarget parameter for the buttons below is set like that because |
50 |
// this object is inherited by SDL_openglesview.m which is the one allocated by SDL. |
|
51 |
// We select this class with [self superclass] and call the selectors with "+" because |
|
52 |
// they are superclass methods |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
53 |
self = [super initWithFrame:frame]; |
2688 | 54 |
|
55 |
#if SDL_IPHONE_KEYBOARD |
|
56 |
[self initializeKeyboard]; |
|
57 |
#endif |
|
58 |
||
59 |
int i; |
|
60 |
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) { |
|
61 |
mice[i].id = i; |
|
62 |
mice[i].driverdata = NULL; |
|
63 |
SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1); |
|
64 |
} |
|
65 |
||
2724 | 66 |
self.multipleTouchEnabled = YES; |
67 |
||
68 |
// custom code |
|
2689 | 69 |
attackButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 480, 260,50)]; |
2688 | 70 |
[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal]; |
2705
2b5625c4ec16
fix a nasty 196 bytes memory leak in engine, plus other stuff for iphone frontend
koda
parents:
2701
diff
changeset
|
71 |
[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateHighlighted]; |
2688 | 72 |
[attackButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchDown]; |
73 |
[attackButton addTarget:[self superclass] action:@selector(attackButtonReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
|
2689 | 74 |
[self addSubview:attackButton]; |
75 |
||
76 |
menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 480, 30,50)]; |
|
77 |
[menuButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal]; |
|
78 |
[menuButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchUpInside]; |
|
2734 | 79 |
[menuButton addTarget:[self superclass] action:@selector(attackButtonReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
2689 | 80 |
[self addSubview:menuButton]; |
2688 | 81 |
|
2689 | 82 |
|
2688 | 83 |
return self; |
84 |
} |
|
85 |
||
86 |
#pragma mark - |
|
2690 | 87 |
#pragma mark Exported functions for FreePascal |
88 |
const char* IPH_getDocumentsPath() { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
89 |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
90 |
NSString *documentsDirectory = [paths objectAtIndex: 0]; |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
91 |
return [documentsDirectory UTF8String]; |
2690 | 92 |
} |
93 |
||
94 |
void IPH_showControls (void) { |
|
2689 | 95 |
NSLog(@"Showing controls"); |
2734 | 96 |
/* |
97 |
[UIView beginAnimations:nil context:NULL]; |
|
2689 | 98 |
[UIView setAnimationDuration:0.5]; |
99 |
attackButton.frame = CGRectMake(30, 430, 260, 50); |
|
100 |
menuButton.frame = CGRectMake(0, 430, 30, 50); |
|
101 |
[UIView commitAnimations]; |
|
2734 | 102 |
*/ |
103 |
} |
|
2689 | 104 |
|
105 |
#pragma mark - |
|
2688 | 106 |
#pragma mark Superclass methods |
107 |
+(void) attackButtonPressed { |
|
108 |
HW_shoot(); |
|
109 |
} |
|
110 |
||
111 |
+(void) attackButtonReleased { |
|
112 |
HW_allKeysUp(); |
|
113 |
} |
|
114 |
||
115 |
#pragma mark - |
|
116 |
#pragma mark Custom SDL_UIView input handling |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
117 |
#define kMinimumPinchDelta 30 |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
118 |
#define kMinimumGestureLength 10 |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
119 |
#define kMaximumVariance 3 |
2688 | 120 |
|
121 |
// we override default touch input to implement our own gestures |
|
122 |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
123 |
/* |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
124 |
NSEnumerator *enumerator = [touches objectEnumerator]; |
2688 | 125 |
UITouch *touch =(UITouch*)[enumerator nextObject]; |
126 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
127 |
// associate touches with mice, so long as we have slots |
2688 | 128 |
int i; |
129 |
int found = 0; |
|
130 |
for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) { |
|
131 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
132 |
// check if this mouse is already tracking a touch |
2688 | 133 |
if (mice[i].driverdata != NULL) { |
134 |
continue; |
|
135 |
} |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
136 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
137 |
// mouse not associated with anything right now, associate the touch with this mouse |
2688 | 138 |
found = 1; |
139 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
140 |
// save old mouse so we can switch back |
2688 | 141 |
int oldMouse = SDL_SelectMouse(-1); |
142 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
143 |
// select this slot's mouse |
2688 | 144 |
SDL_SelectMouse(i); |
145 |
CGPoint locationInView = [touch locationInView: self]; |
|
146 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
147 |
CGFloat oldX = locationInView.x; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
148 |
locationInView.x = locationInView.y; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
149 |
locationInView.y = 320 - oldX; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
150 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
151 |
// set driver data to touch object, we'll use touch object later |
2688 | 152 |
mice[i].driverdata = [touch retain]; |
153 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
154 |
// send moved event |
2688 | 155 |
SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); |
156 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
157 |
// send mouse down event |
2688 | 158 |
SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT); |
159 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
160 |
// re-calibrate relative mouse motion |
2688 | 161 |
SDL_GetRelativeMouseState(i, NULL, NULL); |
162 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
163 |
// grab next touch |
2688 | 164 |
touch = (UITouch*)[enumerator nextObject]; |
165 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
166 |
// switch back to our old mouse |
2688 | 167 |
SDL_SelectMouse(oldMouse); |
168 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
169 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
170 |
*/ |
2688 | 171 |
|
172 |
UITouch *touch = [touches anyObject]; |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
173 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
174 |
switch ([touches count]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
175 |
case 1: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
176 |
gestureStartPoint = [touch locationInView:self]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
177 |
initialDistanceForPinching = 0; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
178 |
switch ([touch tapCount]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
179 |
case 1: |
2734 | 180 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
181 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
182 |
HW_click(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
183 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
184 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
185 |
HW_ammoMenu(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
186 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
187 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
188 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
189 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
190 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
191 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
192 |
if (2 == [touch tapCount]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
193 |
HW_zoomReset(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
194 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
195 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
196 |
// pinching |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
197 |
NSArray *twoTouches = [touches allObjects]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
198 |
UITouch *first = [twoTouches objectAtIndex:0]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
199 |
UITouch *second = [twoTouches objectAtIndex:1]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
200 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self], [second locationInView:self]); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
201 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
202 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
203 |
break; |
2688 | 204 |
} |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
205 |
|
2688 | 206 |
} |
207 |
||
208 |
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
209 |
initialDistanceForPinching = 0; |
2688 | 210 |
|
211 |
HW_allKeysUp(); |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
212 |
/* |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
213 |
NSEnumerator *enumerator = [touches objectEnumerator]; |
2688 | 214 |
UITouch *touch=nil; |
215 |
||
216 |
while(touch = (UITouch *)[enumerator nextObject]) { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
217 |
// search for the mouse slot associated with this touch |
2688 | 218 |
int i, found = NO; |
219 |
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { |
|
220 |
if (mice[i].driverdata == touch) { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
221 |
// found the mouse associate with the touch |
2688 | 222 |
[(UITouch*)(mice[i].driverdata) release]; |
223 |
mice[i].driverdata = NULL; |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
224 |
// send mouse up |
2688 | 225 |
SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT); |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
226 |
// discontinue search for this touch |
2688 | 227 |
found = YES; |
228 |
} |
|
229 |
} |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
230 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
231 |
*/ |
2688 | 232 |
} |
233 |
||
234 |
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
235 |
// this can happen if the user puts more than 5 touches on the screen |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
236 |
// at once, or perhaps in other circumstances. Usually (it seems) |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
237 |
// all active touches are canceled. |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
238 |
[self touchesEnded:touches withEvent:event]; |
2688 | 239 |
} |
240 |
||
241 |
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
242 |
NSArray *twoTouches; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
243 |
CGPoint currentPosition; |
2688 | 244 |
UITouch *touch = [touches anyObject]; |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
245 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
246 |
switch ([touches count]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
247 |
case 1: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
248 |
currentPosition = [touch locationInView:self]; |
2734 | 249 |
// panning |
250 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
251 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
252 |
// remember that we have x and y inverted |
2734 | 253 |
/* temporarily disabling hog movements for camera panning testing |
254 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
255 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
256 |
CGFloat deltaX = fabsf(vertDiff); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
257 |
CGFloat deltaY = fabsf(horizDiff); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
258 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
259 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
260 |
NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
261 |
if (horizDiff > 0) HW_walkLeft(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
262 |
else HW_walkRight(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
263 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
264 |
NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
265 |
if (vertDiff < 0) HW_aimUp(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
266 |
else HW_aimDown(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
267 |
} |
2734 | 268 |
*/ |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
269 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
270 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
271 |
twoTouches = [touches allObjects]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
272 |
UITouch *first = [twoTouches objectAtIndex:0]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
273 |
UITouch *second = [twoTouches objectAtIndex:1]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
274 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self], [second locationInView:self]); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
275 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
276 |
if (0 == initialDistanceForPinching) |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
277 |
initialDistanceForPinching = currentDistanceOfPinching; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
278 |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2734
diff
changeset
|
279 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
280 |
HW_zoomOut(); |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2734
diff
changeset
|
281 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
282 |
HW_zoomIn(); |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2734
diff
changeset
|
283 |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
284 |
currentDistanceOfPinching = initialDistanceForPinching; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
285 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
286 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
287 |
break; |
2688 | 288 |
} |
289 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
290 |
/* |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
291 |
NSEnumerator *enumerator = [touches objectEnumerator]; |
2688 | 292 |
UITouch *touch=nil;while(touch = (UITouch *)[enumerator nextObject]) { |
293 |
// try to find the mouse associated with this touch |
|
294 |
int i, found = NO; |
|
295 |
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { |
|
296 |
if (mice[i].driverdata == touch) { |
|
297 |
// found proper mouse |
|
298 |
CGPoint locationInView = [touch locationInView: self]; |
|
299 |
// send moved event |
|
300 |
SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); |
|
301 |
// discontinue search |
|
302 |
found = YES; |
|
303 |
} |
|
304 |
} |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
305 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
306 |
*/ |
2688 | 307 |
} |
308 |
||
309 |
#pragma mark - |
|
310 |
#pragma mark default routines |
|
311 |
/* |
|
312 |
---- Keyboard related functionality below this line ---- |
|
313 |
*/ |
|
314 |
#if SDL_IPHONE_KEYBOARD |
|
315 |
||
316 |
/* Is the iPhone virtual keyboard visible onscreen? */ |
|
317 |
- (BOOL)keyboardVisible { |
|
318 |
return keyboardVisible; |
|
319 |
} |
|
320 |
||
321 |
/* Set ourselves up as a UITextFieldDelegate */ |
|
322 |
- (void)initializeKeyboard { |
|
323 |
||
324 |
textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease]; |
|
325 |
textField.delegate = self; |
|
326 |
/* placeholder so there is something to delete! */ |
|
327 |
textField.text = @" "; |
|
328 |
||
329 |
/* set UITextInputTrait properties, mostly to defaults */ |
|
330 |
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; |
|
331 |
textField.autocorrectionType = UITextAutocorrectionTypeNo; |
|
332 |
textField.enablesReturnKeyAutomatically = NO; |
|
333 |
textField.keyboardAppearance = UIKeyboardAppearanceDefault; |
|
334 |
textField.keyboardType = UIKeyboardTypeDefault; |
|
335 |
textField.returnKeyType = UIReturnKeyDefault; |
|
336 |
textField.secureTextEntry = NO; |
|
337 |
||
338 |
textField.hidden = YES; |
|
339 |
keyboardVisible = NO; |
|
340 |
/* add the UITextField (hidden) to our view */ |
|
341 |
[self addSubview: textField]; |
|
342 |
||
343 |
/* create our SDL_Keyboard */ |
|
344 |
SDL_Keyboard keyboard; |
|
345 |
SDL_zero(keyboard); |
|
346 |
SDL_AddKeyboard(&keyboard, 0); |
|
347 |
SDLKey keymap[SDL_NUM_SCANCODES]; |
|
348 |
SDL_GetDefaultKeymap(keymap); |
|
349 |
SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES); |
|
350 |
||
351 |
} |
|
352 |
||
353 |
/* reveal onscreen virtual keyboard */ |
|
354 |
- (void)showKeyboard { |
|
355 |
keyboardVisible = YES; |
|
356 |
[textField becomeFirstResponder]; |
|
357 |
} |
|
358 |
||
359 |
/* hide onscreen virtual keyboard */ |
|
360 |
- (void)hideKeyboard { |
|
361 |
keyboardVisible = NO; |
|
362 |
[textField resignFirstResponder]; |
|
363 |
} |
|
364 |
||
365 |
/* UITextFieldDelegate method. Invoked when user types something. */ |
|
366 |
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
367 |
||
368 |
if ([string length] == 0) { |
|
369 |
/* it wants to replace text with nothing, ie a delete */ |
|
370 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE); |
|
371 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); |
|
372 |
} |
|
373 |
else { |
|
374 |
/* go through all the characters in the string we've been sent |
|
375 |
and convert them to key presses */ |
|
376 |
int i; |
|
377 |
for (i=0; i<[string length]; i++) { |
|
378 |
||
379 |
unichar c = [string characterAtIndex: i]; |
|
380 |
||
381 |
Uint16 mod = 0; |
|
382 |
SDL_scancode code; |
|
383 |
||
384 |
if (c < 127) { |
|
385 |
/* figure out the SDL_scancode and SDL_keymod for this unichar */ |
|
386 |
code = unicharToUIKeyInfoTable[c].code; |
|
387 |
mod = unicharToUIKeyInfoTable[c].mod; |
|
388 |
} |
|
389 |
else { |
|
390 |
/* we only deal with ASCII right now */ |
|
391 |
code = SDL_SCANCODE_UNKNOWN; |
|
392 |
mod = 0; |
|
393 |
} |
|
394 |
||
395 |
if (mod & KMOD_SHIFT) { |
|
396 |
/* If character uses shift, press shift down */ |
|
397 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); |
|
398 |
} |
|
399 |
/* send a keydown and keyup even for the character */ |
|
400 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, code); |
|
401 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, code); |
|
402 |
if (mod & KMOD_SHIFT) { |
|
403 |
/* If character uses shift, press shift back up */ |
|
404 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); |
|
405 |
} |
|
406 |
} |
|
407 |
} |
|
408 |
return NO; /* don't allow the edit! (keep placeholder text there) */ |
|
409 |
} |
|
410 |
||
411 |
/* Terminates the editing session */ |
|
412 |
- (BOOL)textFieldShouldReturn:(UITextField*)_textField { |
|
413 |
[self hideKeyboard]; |
|
414 |
return YES; |
|
415 |
} |
|
416 |
||
417 |
#endif |
|
418 |
||
419 |
@end |
|
420 |
||
421 |
/* iPhone keyboard addition functions */ |
|
422 |
#if SDL_IPHONE_KEYBOARD |
|
423 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
424 |
int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
2688 | 425 |
SDL_WindowData *data; |
426 |
SDL_uikitview *view; |
|
427 |
||
428 |
if (NULL == window) { |
|
429 |
SDL_SetError("Window does not exist"); |
|
430 |
return -1; |
|
431 |
} |
|
432 |
||
433 |
data = (SDL_WindowData *)window->driverdata; |
|
434 |
view = data->view; |
|
435 |
||
436 |
if (nil == view) { |
|
437 |
SDL_SetError("Window has no view"); |
|
438 |
return -1; |
|
439 |
} |
|
440 |
else { |
|
441 |
[view showKeyboard]; |
|
442 |
return 0; |
|
443 |
} |
|
444 |
} |
|
445 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
446 |
int SDL_iPhoneKeyboardHide(SDL_Window *window) { |
2688 | 447 |
SDL_WindowData *data; |
448 |
SDL_uikitview *view; |
|
449 |
||
450 |
if (NULL == window) { |
|
451 |
SDL_SetError("Window does not exist"); |
|
452 |
return -1; |
|
453 |
} |
|
454 |
||
455 |
data = (SDL_WindowData *)window->driverdata; |
|
456 |
view = data->view; |
|
457 |
||
458 |
if (NULL == view) { |
|
459 |
SDL_SetError("Window has no view"); |
|
460 |
return -1; |
|
461 |
} |
|
462 |
else { |
|
463 |
[view hideKeyboard]; |
|
464 |
return 0; |
|
465 |
} |
|
466 |
} |
|
467 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
468 |
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) { |
2688 | 469 |
SDL_WindowData *data; |
470 |
SDL_uikitview *view; |
|
471 |
||
472 |
if (NULL == window) { |
|
473 |
SDL_SetError("Window does not exist"); |
|
474 |
return -1; |
|
475 |
} |
|
476 |
||
477 |
data = (SDL_WindowData *)window->driverdata; |
|
478 |
view = data->view; |
|
479 |
||
480 |
if (NULL == view) { |
|
481 |
SDL_SetError("Window has no view"); |
|
482 |
return 0; |
|
483 |
} |
|
484 |
else { |
|
485 |
return view.keyboardVisible; |
|
486 |
} |
|
487 |
} |
|
488 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
489 |
int SDL_iPhoneKeyboardToggle(SDL_Window *window) { |
2688 | 490 |
SDL_WindowData *data; |
491 |
SDL_uikitview *view; |
|
492 |
||
493 |
if (NULL == window) { |
|
494 |
SDL_SetError("Window does not exist"); |
|
495 |
return -1; |
|
496 |
} |
|
497 |
||
498 |
data = (SDL_WindowData *)window->driverdata; |
|
499 |
view = data->view; |
|
500 |
||
501 |
if (NULL == view) { |
|
502 |
SDL_SetError("Window has no view"); |
|
503 |
return -1; |
|
504 |
} |
|
505 |
else { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
506 |
if (SDL_iPhoneKeyboardIsShown(window)) { |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
507 |
SDL_iPhoneKeyboardHide(window); |
2688 | 508 |
} |
509 |
else { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
510 |
SDL_iPhoneKeyboardShow(window); |
2688 | 511 |
} |
512 |
return 0; |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
#else |
|
517 |
||
518 |
/* stubs, used if compiled without keyboard support */ |
|
519 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
520 |
int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
2688 | 521 |
SDL_SetError("Not compiled with keyboard support"); |
522 |
return -1; |
|
523 |
} |
|
524 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
525 |
int SDL_iPhoneKeyboardHide(SDL_Window *window) { |
2688 | 526 |
SDL_SetError("Not compiled with keyboard support"); |
527 |
return -1; |
|
528 |
} |
|
529 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
530 |
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) { |
2688 | 531 |
return 0; |
532 |
} |
|
533 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
534 |
int SDL_iPhoneKeyboardToggle(SDL_Window *window) { |
2688 | 535 |
SDL_SetError("Not compiled with keyboard support"); |
536 |
return -1; |
|
537 |
} |
|
538 |
||
539 |
||
540 |
#endif /* SDL_IPHONE_KEYBOARD */ |