author | koda |
Sat, 30 Jan 2010 18:23:42 +0000 | |
changeset 2725 | 89908847b155 |
parent 2724 | 601158aaa201 |
child 2734 | fb9ad1587054 |
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]; |
|
79 |
[self addSubview:menuButton]; |
|
2688 | 80 |
|
2689 | 81 |
|
2688 | 82 |
return self; |
83 |
} |
|
84 |
||
85 |
#pragma mark - |
|
2690 | 86 |
#pragma mark Exported functions for FreePascal |
87 |
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
|
88 |
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
|
89 |
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
|
90 |
return [documentsDirectory UTF8String]; |
2690 | 91 |
} |
92 |
||
93 |
void IPH_showControls (void) { |
|
2689 | 94 |
NSLog(@"Showing controls"); |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
95 |
/*[UIView beginAnimations:nil context:NULL]; |
2689 | 96 |
[UIView setAnimationDuration:0.5]; |
97 |
attackButton.frame = CGRectMake(30, 430, 260, 50); |
|
98 |
menuButton.frame = CGRectMake(0, 430, 30, 50); |
|
99 |
[UIView commitAnimations]; |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
100 |
*/} |
2689 | 101 |
|
102 |
#pragma mark - |
|
2688 | 103 |
#pragma mark Superclass methods |
104 |
+(void) attackButtonPressed { |
|
105 |
HW_shoot(); |
|
106 |
} |
|
107 |
||
108 |
+(void) attackButtonReleased { |
|
109 |
HW_allKeysUp(); |
|
110 |
} |
|
111 |
||
112 |
#pragma mark - |
|
113 |
#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
|
114 |
#define kMinimumPinchDelta 30 |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
115 |
#define kMinimumGestureLength 10 |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
116 |
#define kMaximumVariance 3 |
2688 | 117 |
|
118 |
// we override default touch input to implement our own gestures |
|
119 |
- (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
|
120 |
/* |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
121 |
NSEnumerator *enumerator = [touches objectEnumerator]; |
2688 | 122 |
UITouch *touch =(UITouch*)[enumerator nextObject]; |
123 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
124 |
// associate touches with mice, so long as we have slots |
2688 | 125 |
int i; |
126 |
int found = 0; |
|
127 |
for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) { |
|
128 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
129 |
// check if this mouse is already tracking a touch |
2688 | 130 |
if (mice[i].driverdata != NULL) { |
131 |
continue; |
|
132 |
} |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
133 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
134 |
// mouse not associated with anything right now, associate the touch with this mouse |
2688 | 135 |
found = 1; |
136 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
137 |
// save old mouse so we can switch back |
2688 | 138 |
int oldMouse = SDL_SelectMouse(-1); |
139 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
140 |
// select this slot's mouse |
2688 | 141 |
SDL_SelectMouse(i); |
142 |
CGPoint locationInView = [touch locationInView: self]; |
|
143 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
144 |
CGFloat oldX = locationInView.x; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
145 |
locationInView.x = locationInView.y; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
146 |
locationInView.y = 320 - oldX; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
147 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
148 |
// set driver data to touch object, we'll use touch object later |
2688 | 149 |
mice[i].driverdata = [touch retain]; |
150 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
151 |
// send moved event |
2688 | 152 |
SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); |
153 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
154 |
// send mouse down event |
2688 | 155 |
SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT); |
156 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
157 |
// re-calibrate relative mouse motion |
2688 | 158 |
SDL_GetRelativeMouseState(i, NULL, NULL); |
159 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
160 |
// grab next touch |
2688 | 161 |
touch = (UITouch*)[enumerator nextObject]; |
162 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
163 |
// switch back to our old mouse |
2688 | 164 |
SDL_SelectMouse(oldMouse); |
165 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
166 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
167 |
*/ |
2688 | 168 |
|
169 |
UITouch *touch = [touches anyObject]; |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
170 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
171 |
switch ([touches count]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
172 |
case 1: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
173 |
gestureStartPoint = [touch locationInView:self]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
174 |
initialDistanceForPinching = 0; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
175 |
switch ([touch tapCount]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
176 |
case 1: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
177 |
NSLog(@"x:%d y:%d",(int)gestureStartPoint.x,(int)gestureStartPoint.y); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
178 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, (int)gestureStartPoint.y - 250, (int)gestureStartPoint.x); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
179 |
HW_click(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
180 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
181 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
182 |
HW_ammoMenu(); |
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 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
185 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
186 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
187 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
188 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
189 |
if (2 == [touch tapCount]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
190 |
HW_zoomReset(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
191 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
192 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
193 |
// pinching |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
194 |
NSArray *twoTouches = [touches allObjects]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
195 |
UITouch *first = [twoTouches objectAtIndex:0]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
196 |
UITouch *second = [twoTouches objectAtIndex:1]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
197 |
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
|
198 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
199 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
200 |
break; |
2688 | 201 |
} |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
202 |
|
2688 | 203 |
} |
204 |
||
205 |
- (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
|
206 |
initialDistanceForPinching = 0; |
2688 | 207 |
|
208 |
HW_allKeysUp(); |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
209 |
/* |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
210 |
NSEnumerator *enumerator = [touches objectEnumerator]; |
2688 | 211 |
UITouch *touch=nil; |
212 |
||
213 |
while(touch = (UITouch *)[enumerator nextObject]) { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
214 |
// search for the mouse slot associated with this touch |
2688 | 215 |
int i, found = NO; |
216 |
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { |
|
217 |
if (mice[i].driverdata == touch) { |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
218 |
// found the mouse associate with the touch |
2688 | 219 |
[(UITouch*)(mice[i].driverdata) release]; |
220 |
mice[i].driverdata = NULL; |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
221 |
// send mouse up |
2688 | 222 |
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
|
223 |
// discontinue search for this touch |
2688 | 224 |
found = YES; |
225 |
} |
|
226 |
} |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
227 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
228 |
*/ |
2688 | 229 |
} |
230 |
||
231 |
- (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
|
232 |
// 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
|
233 |
// 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
|
234 |
// all active touches are canceled. |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
235 |
[self touchesEnded:touches withEvent:event]; |
2688 | 236 |
} |
237 |
||
238 |
- (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
|
239 |
NSArray *twoTouches; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
240 |
CGPoint currentPosition; |
2688 | 241 |
UITouch *touch = [touches anyObject]; |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
242 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
243 |
switch ([touches count]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
244 |
case 1: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
245 |
currentPosition = [touch locationInView:self]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
246 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
247 |
// remember that we have x and y inverted |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
248 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
249 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
250 |
CGFloat deltaX = fabsf(vertDiff); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
251 |
CGFloat deltaY = fabsf(horizDiff); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
252 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
253 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
254 |
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
|
255 |
if (horizDiff > 0) HW_walkLeft(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
256 |
else HW_walkRight(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
257 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
258 |
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
|
259 |
if (vertDiff < 0) HW_aimUp(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
260 |
else HW_aimDown(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
261 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
262 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
263 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
264 |
twoTouches = [touches allObjects]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
265 |
UITouch *first = [twoTouches objectAtIndex:0]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
266 |
UITouch *second = [twoTouches objectAtIndex:1]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
267 |
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
|
268 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
269 |
if (0 == initialDistanceForPinching) |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
270 |
initialDistanceForPinching = currentDistanceOfPinching; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
271 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
272 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
273 |
NSLog(@"Outward pinch detected"); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
274 |
HW_zoomOut(); |
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 (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta){ |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
277 |
NSLog(@"Inward pinch detected"); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
278 |
HW_zoomIn(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
279 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
280 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
281 |
currentDistanceOfPinching = initialDistanceForPinching; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
282 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
283 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
284 |
break; |
2688 | 285 |
} |
286 |
||
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
287 |
/* |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
288 |
NSEnumerator *enumerator = [touches objectEnumerator]; |
2688 | 289 |
UITouch *touch=nil;while(touch = (UITouch *)[enumerator nextObject]) { |
290 |
// try to find the mouse associated with this touch |
|
291 |
int i, found = NO; |
|
292 |
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) { |
|
293 |
if (mice[i].driverdata == touch) { |
|
294 |
// found proper mouse |
|
295 |
CGPoint locationInView = [touch locationInView: self]; |
|
296 |
// send moved event |
|
297 |
SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0); |
|
298 |
// discontinue search |
|
299 |
found = YES; |
|
300 |
} |
|
301 |
} |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
302 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
303 |
*/ |
2688 | 304 |
} |
305 |
||
306 |
#pragma mark - |
|
307 |
#pragma mark default routines |
|
308 |
/* |
|
309 |
---- Keyboard related functionality below this line ---- |
|
310 |
*/ |
|
311 |
#if SDL_IPHONE_KEYBOARD |
|
312 |
||
313 |
/* Is the iPhone virtual keyboard visible onscreen? */ |
|
314 |
- (BOOL)keyboardVisible { |
|
315 |
return keyboardVisible; |
|
316 |
} |
|
317 |
||
318 |
/* Set ourselves up as a UITextFieldDelegate */ |
|
319 |
- (void)initializeKeyboard { |
|
320 |
||
321 |
textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease]; |
|
322 |
textField.delegate = self; |
|
323 |
/* placeholder so there is something to delete! */ |
|
324 |
textField.text = @" "; |
|
325 |
||
326 |
/* set UITextInputTrait properties, mostly to defaults */ |
|
327 |
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; |
|
328 |
textField.autocorrectionType = UITextAutocorrectionTypeNo; |
|
329 |
textField.enablesReturnKeyAutomatically = NO; |
|
330 |
textField.keyboardAppearance = UIKeyboardAppearanceDefault; |
|
331 |
textField.keyboardType = UIKeyboardTypeDefault; |
|
332 |
textField.returnKeyType = UIReturnKeyDefault; |
|
333 |
textField.secureTextEntry = NO; |
|
334 |
||
335 |
textField.hidden = YES; |
|
336 |
keyboardVisible = NO; |
|
337 |
/* add the UITextField (hidden) to our view */ |
|
338 |
[self addSubview: textField]; |
|
339 |
||
340 |
/* create our SDL_Keyboard */ |
|
341 |
SDL_Keyboard keyboard; |
|
342 |
SDL_zero(keyboard); |
|
343 |
SDL_AddKeyboard(&keyboard, 0); |
|
344 |
SDLKey keymap[SDL_NUM_SCANCODES]; |
|
345 |
SDL_GetDefaultKeymap(keymap); |
|
346 |
SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES); |
|
347 |
||
348 |
} |
|
349 |
||
350 |
/* reveal onscreen virtual keyboard */ |
|
351 |
- (void)showKeyboard { |
|
352 |
keyboardVisible = YES; |
|
353 |
[textField becomeFirstResponder]; |
|
354 |
} |
|
355 |
||
356 |
/* hide onscreen virtual keyboard */ |
|
357 |
- (void)hideKeyboard { |
|
358 |
keyboardVisible = NO; |
|
359 |
[textField resignFirstResponder]; |
|
360 |
} |
|
361 |
||
362 |
/* UITextFieldDelegate method. Invoked when user types something. */ |
|
363 |
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
|
364 |
||
365 |
if ([string length] == 0) { |
|
366 |
/* it wants to replace text with nothing, ie a delete */ |
|
367 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE); |
|
368 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); |
|
369 |
} |
|
370 |
else { |
|
371 |
/* go through all the characters in the string we've been sent |
|
372 |
and convert them to key presses */ |
|
373 |
int i; |
|
374 |
for (i=0; i<[string length]; i++) { |
|
375 |
||
376 |
unichar c = [string characterAtIndex: i]; |
|
377 |
||
378 |
Uint16 mod = 0; |
|
379 |
SDL_scancode code; |
|
380 |
||
381 |
if (c < 127) { |
|
382 |
/* figure out the SDL_scancode and SDL_keymod for this unichar */ |
|
383 |
code = unicharToUIKeyInfoTable[c].code; |
|
384 |
mod = unicharToUIKeyInfoTable[c].mod; |
|
385 |
} |
|
386 |
else { |
|
387 |
/* we only deal with ASCII right now */ |
|
388 |
code = SDL_SCANCODE_UNKNOWN; |
|
389 |
mod = 0; |
|
390 |
} |
|
391 |
||
392 |
if (mod & KMOD_SHIFT) { |
|
393 |
/* If character uses shift, press shift down */ |
|
394 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); |
|
395 |
} |
|
396 |
/* send a keydown and keyup even for the character */ |
|
397 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, code); |
|
398 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, code); |
|
399 |
if (mod & KMOD_SHIFT) { |
|
400 |
/* If character uses shift, press shift back up */ |
|
401 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); |
|
402 |
} |
|
403 |
} |
|
404 |
} |
|
405 |
return NO; /* don't allow the edit! (keep placeholder text there) */ |
|
406 |
} |
|
407 |
||
408 |
/* Terminates the editing session */ |
|
409 |
- (BOOL)textFieldShouldReturn:(UITextField*)_textField { |
|
410 |
[self hideKeyboard]; |
|
411 |
return YES; |
|
412 |
} |
|
413 |
||
414 |
#endif |
|
415 |
||
416 |
@end |
|
417 |
||
418 |
/* iPhone keyboard addition functions */ |
|
419 |
#if SDL_IPHONE_KEYBOARD |
|
420 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
421 |
int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
2688 | 422 |
SDL_WindowData *data; |
423 |
SDL_uikitview *view; |
|
424 |
||
425 |
if (NULL == window) { |
|
426 |
SDL_SetError("Window does not exist"); |
|
427 |
return -1; |
|
428 |
} |
|
429 |
||
430 |
data = (SDL_WindowData *)window->driverdata; |
|
431 |
view = data->view; |
|
432 |
||
433 |
if (nil == view) { |
|
434 |
SDL_SetError("Window has no view"); |
|
435 |
return -1; |
|
436 |
} |
|
437 |
else { |
|
438 |
[view showKeyboard]; |
|
439 |
return 0; |
|
440 |
} |
|
441 |
} |
|
442 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
443 |
int SDL_iPhoneKeyboardHide(SDL_Window *window) { |
2688 | 444 |
SDL_WindowData *data; |
445 |
SDL_uikitview *view; |
|
446 |
||
447 |
if (NULL == window) { |
|
448 |
SDL_SetError("Window does not exist"); |
|
449 |
return -1; |
|
450 |
} |
|
451 |
||
452 |
data = (SDL_WindowData *)window->driverdata; |
|
453 |
view = data->view; |
|
454 |
||
455 |
if (NULL == view) { |
|
456 |
SDL_SetError("Window has no view"); |
|
457 |
return -1; |
|
458 |
} |
|
459 |
else { |
|
460 |
[view hideKeyboard]; |
|
461 |
return 0; |
|
462 |
} |
|
463 |
} |
|
464 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
465 |
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) { |
2688 | 466 |
SDL_WindowData *data; |
467 |
SDL_uikitview *view; |
|
468 |
||
469 |
if (NULL == window) { |
|
470 |
SDL_SetError("Window does not exist"); |
|
471 |
return -1; |
|
472 |
} |
|
473 |
||
474 |
data = (SDL_WindowData *)window->driverdata; |
|
475 |
view = data->view; |
|
476 |
||
477 |
if (NULL == view) { |
|
478 |
SDL_SetError("Window has no view"); |
|
479 |
return 0; |
|
480 |
} |
|
481 |
else { |
|
482 |
return view.keyboardVisible; |
|
483 |
} |
|
484 |
} |
|
485 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
486 |
int SDL_iPhoneKeyboardToggle(SDL_Window *window) { |
2688 | 487 |
SDL_WindowData *data; |
488 |
SDL_uikitview *view; |
|
489 |
||
490 |
if (NULL == window) { |
|
491 |
SDL_SetError("Window does not exist"); |
|
492 |
return -1; |
|
493 |
} |
|
494 |
||
495 |
data = (SDL_WindowData *)window->driverdata; |
|
496 |
view = data->view; |
|
497 |
||
498 |
if (NULL == view) { |
|
499 |
SDL_SetError("Window has no view"); |
|
500 |
return -1; |
|
501 |
} |
|
502 |
else { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
503 |
if (SDL_iPhoneKeyboardIsShown(window)) { |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
504 |
SDL_iPhoneKeyboardHide(window); |
2688 | 505 |
} |
506 |
else { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
507 |
SDL_iPhoneKeyboardShow(window); |
2688 | 508 |
} |
509 |
return 0; |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
#else |
|
514 |
||
515 |
/* stubs, used if compiled without keyboard support */ |
|
516 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
517 |
int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
2688 | 518 |
SDL_SetError("Not compiled with keyboard support"); |
519 |
return -1; |
|
520 |
} |
|
521 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
522 |
int SDL_iPhoneKeyboardHide(SDL_Window *window) { |
2688 | 523 |
SDL_SetError("Not compiled with keyboard support"); |
524 |
return -1; |
|
525 |
} |
|
526 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
527 |
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) { |
2688 | 528 |
return 0; |
529 |
} |
|
530 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
531 |
int SDL_iPhoneKeyboardToggle(SDL_Window *window) { |
2688 | 532 |
SDL_SetError("Not compiled with keyboard support"); |
533 |
return -1; |
|
534 |
} |
|
535 |
||
536 |
||
537 |
#endif /* SDL_IPHONE_KEYBOARD */ |