|
1 // |
|
2 // overlayViewController.m |
|
3 // HedgewarsMobile |
|
4 // |
|
5 // Created by Vittorio on 16/03/10. |
|
6 // Copyright 2010 __MyCompanyName__. All rights reserved. |
|
7 // |
|
8 |
|
9 #import "OverlayViewController.h" |
|
10 #import "SDL_uikitappdelegate.h" |
|
11 #import "PascalImports.h" |
|
12 #import "CGPointUtils.h" |
|
13 #import "SDL_mouse.h" |
|
14 #import "PopupMenuViewController.h" |
|
15 |
|
16 @implementation OverlayViewController |
|
17 @synthesize dimTimer; |
|
18 |
|
19 |
|
20 -(void) didReceiveMemoryWarning { |
|
21 // Releases the view if it doesn't have a superview. |
|
22 [super didReceiveMemoryWarning]; |
|
23 |
|
24 // Release any cached data, images, etc that aren't in use. |
|
25 } |
|
26 |
|
27 -(void) viewDidLoad { |
|
28 self.view.alpha = 0; |
|
29 |
|
30 // needed for rotation to work on os < 3.2 |
|
31 self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0); |
|
32 self.view.transform = CGAffineTransformRotate(self.view.transform, (M_PI/2.0)); |
|
33 |
|
34 dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6] |
|
35 interval:1000 |
|
36 target:self |
|
37 selector:@selector(dimOverlay) |
|
38 userInfo:nil |
|
39 repeats:YES]; |
|
40 |
|
41 // add timer too runloop, otherwise it doesn't work |
|
42 [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode]; |
|
43 // listen for dismissal of the popover (see below)x |
|
44 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissPopover) name:@"dismissPopover" object:nil]; |
|
45 // present the overlay after 2 seconds |
|
46 [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showMenuAfterwards) userInfo:nil repeats:NO]; |
|
47 } |
|
48 |
|
49 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
50 return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
|
51 } |
|
52 |
|
53 -(void) viewDidUnload { |
|
54 [dimTimer invalidate]; |
|
55 } |
|
56 |
|
57 -(void) dealloc { |
|
58 // dimTimer is autoreleased |
|
59 [super dealloc]; |
|
60 } |
|
61 |
|
62 // draws the controller overlay after the sdl window has taken control |
|
63 -(void) showMenuAfterwards { |
|
64 [[SDLUIKitDelegate sharedAppDelegate].uiwindow bringSubviewToFront:self.view]; |
|
65 |
|
66 [UIView beginAnimations:@"showing overlay" context:NULL]; |
|
67 [UIView setAnimationDuration:1]; |
|
68 self.view.alpha = 1; |
|
69 [UIView commitAnimations]; |
|
70 } |
|
71 |
|
72 // dim the overlay when there's no more input for a certain amount of time |
|
73 -(IBAction) buttonReleased:(id) sender { |
|
74 HW_allKeysUp(); |
|
75 [dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]]; |
|
76 } |
|
77 |
|
78 // nice transition for dimming |
|
79 -(void) dimOverlay { |
|
80 [UIView beginAnimations:@"overlay dim" context:NULL]; |
|
81 [UIView setAnimationDuration:0.6]; |
|
82 self.view.alpha = 0.2; |
|
83 [UIView commitAnimations]; |
|
84 } |
|
85 |
|
86 // set the overlay visible and put off the timer for enough time |
|
87 -(void) activateOverlay { |
|
88 self.view.alpha = 1; |
|
89 [dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1000]]; |
|
90 } |
|
91 |
|
92 // issue certain action based on the tag of the button |
|
93 -(IBAction) buttonPressed:(id) sender { |
|
94 [self activateOverlay]; |
|
95 UIActionSheet *actionSheet; |
|
96 UIButton *theButton = (UIButton *)sender; |
|
97 |
|
98 switch (theButton.tag) { |
|
99 case 0: |
|
100 HW_walkLeft(); |
|
101 break; |
|
102 case 1: |
|
103 HW_walkRight(); |
|
104 break; |
|
105 case 2: |
|
106 HW_aimUp(); |
|
107 break; |
|
108 case 3: |
|
109 HW_aimDown(); |
|
110 break; |
|
111 case 4: |
|
112 HW_shoot(); |
|
113 break; |
|
114 case 5: |
|
115 HW_jump(); |
|
116 break; |
|
117 case 6: |
|
118 HW_backjump(); |
|
119 break; |
|
120 case 7: |
|
121 HW_pause(); |
|
122 break; |
|
123 case 8: |
|
124 HW_chat(); |
|
125 break; |
|
126 case 9: |
|
127 actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"") |
|
128 delegate:self |
|
129 cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"") |
|
130 destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"") |
|
131 otherButtonTitles:nil]; |
|
132 [actionSheet showInView:self.view]; |
|
133 [actionSheet release]; |
|
134 |
|
135 HW_pause(); |
|
136 break; |
|
137 case 10: |
|
138 HW_tab(); |
|
139 break; |
|
140 default: |
|
141 NSLog(@"Nope"); |
|
142 break; |
|
143 } |
|
144 } |
|
145 |
|
146 #pragma mark - |
|
147 #define kMinimumPinchDelta 50 |
|
148 #define kMinimumGestureLength 10 |
|
149 #define kMaximumVariance 3 |
|
150 |
|
151 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
152 NSArray *twoTouches; |
|
153 UITouch *touch = [touches anyObject]; |
|
154 |
|
155 switch ([touches count]) { |
|
156 case 1: |
|
157 gestureStartPoint = [touch locationInView:self.view]; |
|
158 initialDistanceForPinching = 0; |
|
159 switch ([touch tapCount]) { |
|
160 case 1: |
|
161 NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
|
162 SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
163 (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
164 HW_click(); |
|
165 break; |
|
166 case 2: |
|
167 HW_ammoMenu(); |
|
168 break; |
|
169 default: |
|
170 break; |
|
171 } |
|
172 break; |
|
173 case 2: |
|
174 if (2 == [touch tapCount]) { |
|
175 HW_zoomReset(); |
|
176 } |
|
177 |
|
178 // pinching |
|
179 twoTouches = [touches allObjects]; |
|
180 UITouch *first = [twoTouches objectAtIndex:0]; |
|
181 UITouch *second = [twoTouches objectAtIndex:1]; |
|
182 initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
183 break; |
|
184 default: |
|
185 break; |
|
186 } |
|
187 |
|
188 } |
|
189 |
|
190 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
191 initialDistanceForPinching = 0; |
|
192 gestureStartPoint.x = 0; |
|
193 gestureStartPoint.y = 0; |
|
194 HW_allKeysUp(); |
|
195 } |
|
196 |
|
197 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
198 // this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances. |
|
199 [self touchesEnded:touches withEvent:event]; |
|
200 } |
|
201 |
|
202 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
203 NSArray *twoTouches; |
|
204 CGPoint currentPosition; |
|
205 UITouch *touch = [touches anyObject]; |
|
206 |
|
207 switch ([touches count]) { |
|
208 case 1: |
|
209 currentPosition = [touch locationInView:self.view]; |
|
210 // panning |
|
211 SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
212 (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
213 // remember that we have x and y inverted |
|
214 /* temporarily disabling hog movements for camera panning testing |
|
215 CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
|
216 CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
|
217 CGFloat deltaX = fabsf(vertDiff); |
|
218 CGFloat deltaY = fabsf(horizDiff); |
|
219 |
|
220 if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
|
221 NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x); |
|
222 if (horizDiff > 0) HW_walkLeft(); |
|
223 else HW_walkRight(); |
|
224 } else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
|
225 NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y); |
|
226 if (vertDiff < 0) HW_aimUp(); |
|
227 else HW_aimDown(); |
|
228 } |
|
229 */ |
|
230 break; |
|
231 case 2: |
|
232 twoTouches = [touches allObjects]; |
|
233 UITouch *first = [twoTouches objectAtIndex:0]; |
|
234 UITouch *second = [twoTouches objectAtIndex:1]; |
|
235 CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
236 |
|
237 if (0 == initialDistanceForPinching) |
|
238 initialDistanceForPinching = currentDistanceOfPinching; |
|
239 |
|
240 if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
|
241 HW_zoomOut(); |
|
242 else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
|
243 HW_zoomIn(); |
|
244 |
|
245 currentDistanceOfPinching = initialDistanceForPinching; |
|
246 break; |
|
247 default: |
|
248 break; |
|
249 } |
|
250 } |
|
251 |
|
252 |
|
253 @end |