author | nemo |
Sun, 14 Mar 2010 20:46:29 +0000 | |
changeset 2994 | 7ae3067546f2 |
parent 2805 | 36a8cebb91e8 |
child 3006 | da6023c2745b |
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 |
||
2772 | 23 |
#import "PascalImports.h" |
24 |
#import "CGPointUtils.h" |
|
2688 | 25 |
#import "SDL_uikitview.h" |
26 |
#import "SDL_uikitappdelegate.h" |
|
27 |
||
28 |
#if SDL_IPHONE_KEYBOARD |
|
29 |
#import "SDL_keyboard_c.h" |
|
30 |
#import "keyinfotable.h" |
|
31 |
#import "SDL_uikitwindow.h" |
|
32 |
#endif |
|
33 |
||
34 |
@implementation SDL_uikitview |
|
35 |
||
2689 | 36 |
// they have to be global variables to allow showControls() to use them |
2805 | 37 |
//UIButton *attackButton, *menuButton; |
38 |
UIView *menuView; |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
39 |
-(void) dealloc { |
2688 | 40 |
#if SDL_IPHONE_KEYBOARD |
41 |
SDL_DelKeyboard(0); |
|
42 |
[textField release]; |
|
43 |
#endif |
|
2805 | 44 |
if (menuView) [menuView release]; |
45 |
//[menuButton release]; |
|
46 |
//[attackButton release]; |
|
2688 | 47 |
[super dealloc]; |
48 |
} |
|
49 |
||
50 |
- (id)initWithFrame:(CGRect)frame { |
|
2689 | 51 |
// the addTarget parameter for the buttons below is set like that because |
52 |
// this object is inherited by SDL_openglesview.m which is the one allocated by SDL. |
|
53 |
// We select this class with [self superclass] and call the selectors with "+" because |
|
54 |
// 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
|
55 |
self = [super initWithFrame:frame]; |
2688 | 56 |
|
57 |
#if SDL_IPHONE_KEYBOARD |
|
58 |
[self initializeKeyboard]; |
|
59 |
#endif |
|
60 |
||
61 |
int i; |
|
62 |
for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) { |
|
63 |
mice[i].id = i; |
|
64 |
mice[i].driverdata = NULL; |
|
65 |
SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1); |
|
66 |
} |
|
67 |
||
2724 | 68 |
self.multipleTouchEnabled = YES; |
2772 | 69 |
self.exclusiveTouch = YES; |
2805 | 70 |
|
71 |
||
72 |
/* |
|
2724 | 73 |
// custom code |
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
74 |
// the coordinate system is still like in Portrait even though everything is rotated |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
75 |
attackButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 480, 260, 50)]; |
2688 | 76 |
[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
|
77 |
[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateHighlighted]; |
2688 | 78 |
[attackButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchDown]; |
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
79 |
[attackButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
80 |
//[self addSubview:attackButton]; |
2689 | 81 |
|
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
82 |
menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 480, 30, 50)]; |
2689 | 83 |
[menuButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal]; |
84 |
[menuButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchUpInside]; |
|
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
85 |
[menuButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
86 |
//[self addSubview:menuButton]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
87 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
88 |
UIButton *walkLeftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
89 |
[walkLeftButton addTarget:[self superclass] action:@selector(walkingLeft) forControlEvents:UIControlEventTouchDown]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
90 |
[walkLeftButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
91 |
[self insertSubview:walkLeftButton atIndex:0]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
92 |
[walkLeftButton release]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
93 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
94 |
UIButton *walkRightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 380, 320, 100)]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
95 |
[walkRightButton addTarget:[self superclass] action:@selector(walkingRight) forControlEvents:UIControlEventTouchDown]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
96 |
[walkRightButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
97 |
[self insertSubview:walkRightButton atIndex:0]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
98 |
[walkRightButton release]; |
2688 | 99 |
|
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
100 |
UIButton *moveDownButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 70, 280)]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
101 |
[moveDownButton addTarget:[self superclass] action:@selector(movingDown) forControlEvents:UIControlEventTouchDown]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
102 |
[moveDownButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
103 |
[self insertSubview:moveDownButton atIndex:0]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
104 |
[moveDownButton release]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
105 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
106 |
UIButton *moveUpButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 100, 70, 280)]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
107 |
[moveUpButton addTarget:[self superclass] action:@selector(movingUp) forControlEvents:UIControlEventTouchDown]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
108 |
[moveUpButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
109 |
[self insertSubview:moveUpButton atIndex:0]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
110 |
[moveUpButton release]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
111 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
112 |
//dummy button used to test chat |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
113 |
UIButton *chatButton = [[UIButton alloc] initWithFrame:CGRectMake(70, 100, 250, 280)]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
114 |
[chatButton addTarget:[self superclass] action:@selector(chatBegin) forControlEvents:UIControlEventTouchDown]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
115 |
[self insertSubview:chatButton atIndex:0]; |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
116 |
[chatButton release]; |
2772 | 117 |
*/ |
2805 | 118 |
|
119 |
//(0,0) is the lower left corner |
|
120 |
//x:[0-320] |
|
121 |
//y:[0-480] |
|
122 |
UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(256, 416, 64, 64)]; |
|
123 |
[menuButton addTarget:[self superclass] action:@selector(showMenu) forControlEvents:UIControlEventTouchDown]; |
|
124 |
[menuButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal]; |
|
125 |
[self insertSubview:menuButton atIndex:0]; |
|
126 |
[menuButton release]; |
|
127 |
||
128 |
menuView = [[UIView alloc] initWithFrame:CGRectMake(320, 480, 150, 100)]; |
|
129 |
menuView.backgroundColor = [UIColor lightGrayColor]; |
|
130 |
[self insertSubview:menuView atIndex:1]; |
|
131 |
||
2688 | 132 |
return self; |
133 |
} |
|
134 |
||
2805 | 135 |
+(void) showMenu { |
136 |
HW_pause(); |
|
137 |
||
138 |
[UIView beginAnimations:@"show menu" context:NULL]; |
|
139 |
[UIView setAnimationDuration:1]; |
|
140 |
||
141 |
menuView.frame = CGRectMake(170, 380, 150, 100); |
|
142 |
||
143 |
[UIView commitAnimations]; |
|
144 |
} |
|
145 |
||
146 |
+(void) hideMenu { |
|
147 |
[UIView beginAnimations:@"hide menu" context:NULL]; |
|
148 |
[UIView setAnimationDuration:1]; |
|
149 |
||
150 |
menuView.frame = CGRectMake(480, -70, 150, 100); |
|
151 |
||
152 |
[UIView commitAnimations]; |
|
153 |
||
154 |
HW_pause(); |
|
155 |
} |
|
156 |
||
157 |
||
158 |
||
2688 | 159 |
#pragma mark - |
2690 | 160 |
#pragma mark Exported functions for FreePascal |
161 |
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
|
162 |
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
|
163 |
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
|
164 |
return [documentsDirectory UTF8String]; |
2690 | 165 |
} |
166 |
||
167 |
void IPH_showControls (void) { |
|
2689 | 168 |
NSLog(@"Showing controls"); |
2772 | 169 |
/* |
2734 | 170 |
[UIView beginAnimations:nil context:NULL]; |
2689 | 171 |
[UIView setAnimationDuration:0.5]; |
172 |
attackButton.frame = CGRectMake(30, 430, 260, 50); |
|
173 |
menuButton.frame = CGRectMake(0, 430, 30, 50); |
|
174 |
[UIView commitAnimations]; |
|
2772 | 175 |
*/ |
2734 | 176 |
} |
2689 | 177 |
|
178 |
#pragma mark - |
|
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
179 |
#pragma mark Superclass methods to call Pascal code |
2688 | 180 |
+(void) attackButtonPressed { |
181 |
HW_shoot(); |
|
182 |
} |
|
183 |
||
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
184 |
+(void) buttonsReleased { |
2688 | 185 |
HW_allKeysUp(); |
186 |
} |
|
187 |
||
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
188 |
+(void) walkingLeft { |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
189 |
HW_walkLeft(); |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
190 |
} |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
191 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
192 |
+(void) walkingRight { |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
193 |
HW_walkRight(); |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
194 |
} |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
195 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
196 |
+(void) movingUp { |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
197 |
HW_aimUp(); |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
198 |
} |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
199 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
200 |
+(void) movingDown { |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
201 |
HW_aimDown(); |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
202 |
} |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
203 |
|
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
204 |
+(void) chatBegin { |
2772 | 205 |
//TODO: implement a UITextScroll and put received chat lines in there |
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
206 |
} |
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
207 |
|
2805 | 208 |
+(void) pauseGame { |
209 |
HW_pause(); |
|
210 |
} |
|
211 |
||
2688 | 212 |
#pragma mark - |
213 |
#pragma mark Custom SDL_UIView input handling |
|
2754
ad4f81fbfb76
touchinput works, invisible buttons added and initial support for chat
koda
parents:
2740
diff
changeset
|
214 |
#define kMinimumPinchDelta 50 |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
215 |
#define kMinimumGestureLength 10 |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
216 |
#define kMaximumVariance 3 |
2688 | 217 |
|
218 |
// we override default touch input to implement our own gestures |
|
219 |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
2772 | 220 |
NSArray *twoTouches; |
2688 | 221 |
UITouch *touch = [touches anyObject]; |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
222 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
223 |
switch ([touches count]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
224 |
case 1: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
225 |
gestureStartPoint = [touch locationInView:self]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
226 |
initialDistanceForPinching = 0; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
227 |
switch ([touch tapCount]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
228 |
case 1: |
2772 | 229 |
|
230 |
NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
|
2734 | 231 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
232 |
(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
|
233 |
HW_click(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
234 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
235 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
236 |
HW_ammoMenu(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
237 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
238 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
239 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
240 |
} |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
241 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
242 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
243 |
if (2 == [touch tapCount]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
244 |
HW_zoomReset(); |
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 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
247 |
// pinching |
2772 | 248 |
twoTouches = [touches allObjects]; |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
249 |
UITouch *first = [twoTouches objectAtIndex:0]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
250 |
UITouch *second = [twoTouches objectAtIndex:1]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
251 |
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
|
252 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
253 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
254 |
break; |
2688 | 255 |
} |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
256 |
|
2688 | 257 |
} |
258 |
||
259 |
- (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
|
260 |
initialDistanceForPinching = 0; |
2772 | 261 |
gestureStartPoint.x = 0; |
262 |
gestureStartPoint.y = 0; |
|
2688 | 263 |
HW_allKeysUp(); |
264 |
} |
|
265 |
||
266 |
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
2772 | 267 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances. |
268 |
// Usually (it seems) all active touches are canceled. |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
269 |
[self touchesEnded:touches withEvent:event]; |
2688 | 270 |
} |
271 |
||
272 |
- (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
|
273 |
NSArray *twoTouches; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
274 |
CGPoint currentPosition; |
2688 | 275 |
UITouch *touch = [touches anyObject]; |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
276 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
277 |
switch ([touches count]) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
278 |
case 1: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
279 |
currentPosition = [touch locationInView:self]; |
2734 | 280 |
// panning |
281 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
282 |
(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
|
283 |
// remember that we have x and y inverted |
2734 | 284 |
/* temporarily disabling hog movements for camera panning testing |
2772 | 285 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
286 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
287 |
CGFloat deltaX = fabsf(vertDiff); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
288 |
CGFloat deltaY = fabsf(horizDiff); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
289 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
290 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
291 |
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
|
292 |
if (horizDiff > 0) HW_walkLeft(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
293 |
else HW_walkRight(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
294 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
295 |
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
|
296 |
if (vertDiff < 0) HW_aimUp(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
297 |
else HW_aimDown(); |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
298 |
} |
2734 | 299 |
*/ |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
300 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
301 |
case 2: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
302 |
twoTouches = [touches allObjects]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
303 |
UITouch *first = [twoTouches objectAtIndex:0]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
304 |
UITouch *second = [twoTouches objectAtIndex:1]; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
305 |
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
|
306 |
|
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
307 |
if (0 == initialDistanceForPinching) |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
308 |
initialDistanceForPinching = currentDistanceOfPinching; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
309 |
|
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2734
diff
changeset
|
310 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
311 |
HW_zoomOut(); |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2734
diff
changeset
|
312 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
313 |
HW_zoomIn(); |
2738
bfccb2ec4334
new graphics from Tiy and frontend is loaded in landscape mode
koda
parents:
2734
diff
changeset
|
314 |
|
2725
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
315 |
currentDistanceOfPinching = initialDistanceForPinching; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
316 |
break; |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
317 |
default: |
89908847b155
input handling reworked, still a lot of bugs in mouse movement
koda
parents:
2724
diff
changeset
|
318 |
break; |
2688 | 319 |
} |
320 |
} |
|
321 |
||
2772 | 322 |
/* |
2688 | 323 |
#pragma mark - |
2772 | 324 |
#pragma mark Keyboard related functionality |
2688 | 325 |
#if SDL_IPHONE_KEYBOARD |
2772 | 326 |
// Is the iPhone virtual keyboard visible onscreen? |
2688 | 327 |
- (BOOL)keyboardVisible { |
328 |
return keyboardVisible; |
|
329 |
} |
|
330 |
||
2772 | 331 |
// Set ourselves up as a UITextFieldDelegate |
2688 | 332 |
- (void)initializeKeyboard { |
333 |
||
334 |
textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease]; |
|
335 |
textField.delegate = self; |
|
2772 | 336 |
// placeholder so there is something to delete! |
2688 | 337 |
textField.text = @" "; |
338 |
||
2772 | 339 |
// set UITextInputTrait properties, mostly to defaults |
2688 | 340 |
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; |
341 |
textField.autocorrectionType = UITextAutocorrectionTypeNo; |
|
342 |
textField.enablesReturnKeyAutomatically = NO; |
|
343 |
textField.keyboardAppearance = UIKeyboardAppearanceDefault; |
|
344 |
textField.keyboardType = UIKeyboardTypeDefault; |
|
345 |
textField.returnKeyType = UIReturnKeyDefault; |
|
346 |
textField.secureTextEntry = NO; |
|
347 |
||
348 |
textField.hidden = YES; |
|
349 |
keyboardVisible = NO; |
|
2772 | 350 |
// add the UITextField (hidden) to our view |
2688 | 351 |
[self addSubview: textField]; |
352 |
||
2772 | 353 |
// create our SDL_Keyboard |
2688 | 354 |
SDL_Keyboard keyboard; |
355 |
SDL_zero(keyboard); |
|
356 |
SDL_AddKeyboard(&keyboard, 0); |
|
357 |
SDLKey keymap[SDL_NUM_SCANCODES]; |
|
358 |
SDL_GetDefaultKeymap(keymap); |
|
359 |
SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES); |
|
360 |
||
361 |
} |
|
362 |
||
2772 | 363 |
// reveal onscreen virtual keyboard |
2688 | 364 |
- (void)showKeyboard { |
365 |
keyboardVisible = YES; |
|
366 |
[textField becomeFirstResponder]; |
|
367 |
} |
|
368 |
||
2772 | 369 |
// hide onscreen virtual keyboard |
2688 | 370 |
- (void)hideKeyboard { |
371 |
keyboardVisible = NO; |
|
372 |
[textField resignFirstResponder]; |
|
373 |
} |
|
374 |
||
2772 | 375 |
// UITextFieldDelegate method. Invoked when user types something. |
2688 | 376 |
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { |
377 |
||
378 |
if ([string length] == 0) { |
|
2772 | 379 |
// it wants to replace text with nothing, ie a delete |
2688 | 380 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE); |
381 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE); |
|
382 |
} |
|
383 |
else { |
|
2772 | 384 |
// go through all the characters in the string we've been sent and convert them to key presses |
2688 | 385 |
int i; |
386 |
for (i=0; i<[string length]; i++) { |
|
387 |
||
388 |
unichar c = [string characterAtIndex: i]; |
|
389 |
||
390 |
Uint16 mod = 0; |
|
391 |
SDL_scancode code; |
|
392 |
||
393 |
if (c < 127) { |
|
2772 | 394 |
// figure out the SDL_scancode and SDL_keymod for this unichar |
2688 | 395 |
code = unicharToUIKeyInfoTable[c].code; |
396 |
mod = unicharToUIKeyInfoTable[c].mod; |
|
397 |
} |
|
398 |
else { |
|
2772 | 399 |
// we only deal with ASCII right now |
2688 | 400 |
code = SDL_SCANCODE_UNKNOWN; |
401 |
mod = 0; |
|
402 |
} |
|
403 |
||
404 |
if (mod & KMOD_SHIFT) { |
|
2772 | 405 |
// If character uses shift, press shift down |
2688 | 406 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT); |
407 |
} |
|
2772 | 408 |
// send a keydown and keyup even for the character |
2688 | 409 |
SDL_SendKeyboardKey( 0, SDL_PRESSED, code); |
410 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, code); |
|
411 |
if (mod & KMOD_SHIFT) { |
|
2772 | 412 |
// If character uses shift, press shift back up |
2688 | 413 |
SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT); |
414 |
} |
|
415 |
} |
|
416 |
} |
|
2772 | 417 |
return NO; // don't allow the edit! (keep placeholder text there) |
2688 | 418 |
} |
419 |
||
2772 | 420 |
// Terminates the editing session |
2688 | 421 |
- (BOOL)textFieldShouldReturn:(UITextField*)_textField { |
422 |
[self hideKeyboard]; |
|
423 |
return YES; |
|
424 |
} |
|
425 |
||
426 |
#endif |
|
2772 | 427 |
*/ |
2688 | 428 |
@end |
2772 | 429 |
/* |
430 |
// iPhone keyboard addition functions |
|
2688 | 431 |
#if SDL_IPHONE_KEYBOARD |
432 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
433 |
int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
2688 | 434 |
SDL_WindowData *data; |
435 |
SDL_uikitview *view; |
|
436 |
||
437 |
if (NULL == window) { |
|
438 |
SDL_SetError("Window does not exist"); |
|
439 |
return -1; |
|
440 |
} |
|
441 |
||
442 |
data = (SDL_WindowData *)window->driverdata; |
|
443 |
view = data->view; |
|
444 |
||
445 |
if (nil == view) { |
|
446 |
SDL_SetError("Window has no view"); |
|
447 |
return -1; |
|
448 |
} |
|
449 |
else { |
|
450 |
[view showKeyboard]; |
|
451 |
return 0; |
|
452 |
} |
|
453 |
} |
|
454 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
455 |
int SDL_iPhoneKeyboardHide(SDL_Window *window) { |
2688 | 456 |
SDL_WindowData *data; |
457 |
SDL_uikitview *view; |
|
458 |
||
459 |
if (NULL == window) { |
|
460 |
SDL_SetError("Window does not exist"); |
|
461 |
return -1; |
|
462 |
} |
|
463 |
||
464 |
data = (SDL_WindowData *)window->driverdata; |
|
465 |
view = data->view; |
|
466 |
||
467 |
if (NULL == view) { |
|
468 |
SDL_SetError("Window has no view"); |
|
469 |
return -1; |
|
470 |
} |
|
471 |
else { |
|
472 |
[view hideKeyboard]; |
|
473 |
return 0; |
|
474 |
} |
|
475 |
} |
|
476 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
477 |
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) { |
2688 | 478 |
SDL_WindowData *data; |
479 |
SDL_uikitview *view; |
|
480 |
||
481 |
if (NULL == window) { |
|
482 |
SDL_SetError("Window does not exist"); |
|
483 |
return -1; |
|
484 |
} |
|
485 |
||
486 |
data = (SDL_WindowData *)window->driverdata; |
|
487 |
view = data->view; |
|
488 |
||
489 |
if (NULL == view) { |
|
490 |
SDL_SetError("Window has no view"); |
|
491 |
return 0; |
|
492 |
} |
|
493 |
else { |
|
494 |
return view.keyboardVisible; |
|
495 |
} |
|
496 |
} |
|
497 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
498 |
int SDL_iPhoneKeyboardToggle(SDL_Window *window) { |
2688 | 499 |
SDL_WindowData *data; |
500 |
SDL_uikitview *view; |
|
501 |
||
502 |
if (NULL == window) { |
|
503 |
SDL_SetError("Window does not exist"); |
|
504 |
return -1; |
|
505 |
} |
|
506 |
||
507 |
data = (SDL_WindowData *)window->driverdata; |
|
508 |
view = data->view; |
|
509 |
||
510 |
if (NULL == view) { |
|
511 |
SDL_SetError("Window has no view"); |
|
512 |
return -1; |
|
513 |
} |
|
514 |
else { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
515 |
if (SDL_iPhoneKeyboardIsShown(window)) { |
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
516 |
SDL_iPhoneKeyboardHide(window); |
2688 | 517 |
} |
518 |
else { |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
519 |
SDL_iPhoneKeyboardShow(window); |
2688 | 520 |
} |
521 |
return 0; |
|
522 |
} |
|
523 |
} |
|
524 |
||
525 |
#else |
|
526 |
||
2772 | 527 |
// stubs, used if compiled without keyboard support |
2688 | 528 |
|
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
529 |
int SDL_iPhoneKeyboardShow(SDL_Window *window) { |
2688 | 530 |
SDL_SetError("Not compiled with keyboard support"); |
531 |
return -1; |
|
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_iPhoneKeyboardHide(SDL_Window *window) { |
2688 | 535 |
SDL_SetError("Not compiled with keyboard support"); |
536 |
return -1; |
|
537 |
} |
|
538 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
539 |
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window *window) { |
2688 | 540 |
return 0; |
541 |
} |
|
542 |
||
2714
c85ffe57d971
update iphone frontend to the new (silly) sdl api, code cleanups for other sections
koda
parents:
2705
diff
changeset
|
543 |
int SDL_iPhoneKeyboardToggle(SDL_Window *window) { |
2688 | 544 |
SDL_SetError("Not compiled with keyboard support"); |
545 |
return -1; |
|
546 |
} |
|
547 |
||
548 |
#endif /* SDL_IPHONE_KEYBOARD */ |