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