author | koda |
Sat, 27 Mar 2010 16:57:18 +0000 | |
changeset 3113 | 2829ea0dd47c |
parent 3091 | 9d05c8000ed4 |
permissions | -rw-r--r-- |
3006 | 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" |
|
3025 | 10 |
#import "SDL_uikitappdelegate.h" |
3006 | 11 |
#import "PascalImports.h" |
3026
1a44c0f2b83b
move interface files around to use standard names in different versions
koda
parents:
3025
diff
changeset
|
12 |
#import "CGPointUtils.h" |
3025 | 13 |
#import "SDL_mouse.h" |
3063 | 14 |
#import "popupMenuViewController.h" |
3006 | 15 |
|
16 |
@implementation overlayViewController |
|
3113 | 17 |
@synthesize dimTimer, menuPopover; |
3015 | 18 |
|
3006 | 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 |
||
3015 | 27 |
-(void) viewDidLoad { |
3027 | 28 |
self.view.alpha = 0; |
3034 | 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)); |
|
3027 | 33 |
|
3015 | 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 |
||
3113 | 41 |
// add timer too runloop, otherwise it doesn't work |
3015 | 42 |
[[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode]; |
3113 | 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 |
|
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
46 |
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showMenuAfterwards) userInfo:nil repeats:NO]; |
3015 | 47 |
} |
48 |
||
3034 | 49 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
3027 | 50 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
51 |
} |
|
52 |
||
3006 | 53 |
-(void) viewDidUnload { |
3015 | 54 |
[dimTimer invalidate]; |
3006 | 55 |
} |
56 |
||
57 |
-(void) dealloc { |
|
3113 | 58 |
[menuPopover release]; |
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
59 |
// dimTimer is autoreleased |
3006 | 60 |
[super dealloc]; |
61 |
} |
|
62 |
||
3027 | 63 |
// draws the controller overlay after the sdl window has taken control |
64 |
-(void) showMenuAfterwards { |
|
65 |
[[SDLUIKitDelegate sharedAppDelegate].uiwindow bringSubviewToFront:self.view]; |
|
66 |
||
67 |
[UIView beginAnimations:@"showing overlay" context:NULL]; |
|
68 |
[UIView setAnimationDuration:1]; |
|
69 |
self.view.alpha = 1; |
|
70 |
[UIView commitAnimations]; |
|
71 |
} |
|
3006 | 72 |
|
73 |
// dim the overlay when there's no more input for a certain amount of time |
|
74 |
-(IBAction) buttonReleased:(id) sender { |
|
75 |
HW_allKeysUp(); |
|
3015 | 76 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]]; |
3006 | 77 |
} |
78 |
||
3015 | 79 |
// nice transition for dimming |
3006 | 80 |
-(void) dimOverlay { |
81 |
[UIView beginAnimations:@"overlay dim" context:NULL]; |
|
82 |
[UIView setAnimationDuration:0.6]; |
|
83 |
self.view.alpha = 0.2; |
|
84 |
[UIView commitAnimations]; |
|
85 |
} |
|
86 |
||
3015 | 87 |
// set the overlay visible and put off the timer for enough time |
3006 | 88 |
-(void) activateOverlay { |
89 |
self.view.alpha = 1; |
|
3015 | 90 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1000]]; |
3006 | 91 |
} |
92 |
||
93 |
// issue certain action based on the tag of the button |
|
94 |
-(IBAction) buttonPressed:(id) sender { |
|
95 |
[self activateOverlay]; |
|
3090 | 96 |
UIActionSheet *actionSheet; |
3063 | 97 |
UIButton *theButton = (UIButton *)sender; |
3090 | 98 |
|
3006 | 99 |
switch (theButton.tag) { |
100 |
case 0: |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
101 |
HW_walkLeft(); |
3006 | 102 |
break; |
103 |
case 1: |
|
104 |
HW_walkRight(); |
|
105 |
break; |
|
106 |
case 2: |
|
107 |
HW_aimUp(); |
|
108 |
break; |
|
109 |
case 3: |
|
110 |
HW_aimDown(); |
|
111 |
break; |
|
112 |
case 4: |
|
113 |
HW_shoot(); |
|
114 |
break; |
|
115 |
case 5: |
|
3015 | 116 |
HW_jump(); |
3006 | 117 |
break; |
118 |
case 6: |
|
3015 | 119 |
HW_backjump(); |
3006 | 120 |
break; |
3063 | 121 |
case 7: |
122 |
HW_pause(); |
|
123 |
break; |
|
124 |
case 8: |
|
125 |
HW_chat(); |
|
126 |
break; |
|
3090 | 127 |
case 9: |
128 |
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"") |
|
129 |
delegate:self |
|
130 |
cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"") |
|
131 |
destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"") |
|
132 |
otherButtonTitles:nil]; |
|
133 |
[actionSheet showInView:self.view]; |
|
134 |
[actionSheet release]; |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
135 |
|
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
136 |
HW_pause(); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
137 |
break; |
3090 | 138 |
case 10: |
139 |
HW_tab(); |
|
140 |
break; |
|
3006 | 141 |
default: |
3063 | 142 |
NSLog(@"Nope"); |
3006 | 143 |
break; |
144 |
} |
|
145 |
} |
|
146 |
||
3113 | 147 |
// present a further check before closing game |
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
148 |
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
149 |
if ([actionSheet cancelButtonIndex] != buttonIndex) |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
150 |
HW_terminate(NO); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
151 |
else |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
152 |
HW_pause(); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
153 |
} |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
154 |
|
3113 | 155 |
// show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize |
3063 | 156 |
-(IBAction) showPopover{ |
3090 | 157 |
popupMenuViewController *popupMenu = [[popupMenuViewController alloc] initWithNibName:@"popupMenuViewController" bundle:nil]; |
3113 | 158 |
|
159 |
menuPopover = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
|
160 |
[menuPopover setPopoverContentSize:CGSizeMake(220, 170) animated:YES]; |
|
3063 | 161 |
|
3091 | 162 |
/*UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
163 |
button.frame= CGRectMake(960, 0, 64, 64); |
|
164 |
button.titleLabel.text=@"UUUUUUUF"; |
|
165 |
[self.view addSubview:button];*/ |
|
166 |
||
3113 | 167 |
[menuPopover presentPopoverFromRect:CGRectMake(960, 0, 220, 32) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; |
3063 | 168 |
//UIBarButtonItem *sender = [[useless items] objectAtIndex:1]; |
169 |
//[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
|
170 |
} |
|
171 |
||
3113 | 172 |
// because of the actionSheet, the popOver might not get dismissed, so we do it manually (through a NSNotification system, see above) |
173 |
-(void) dismissPopover { |
|
174 |
if (menuPopover.popoverVisible) |
|
175 |
[menuPopover dismissPopoverAnimated:YES]; |
|
176 |
} |
|
3063 | 177 |
|
3025 | 178 |
#pragma mark - |
179 |
#define kMinimumPinchDelta 50 |
|
180 |
#define kMinimumGestureLength 10 |
|
181 |
#define kMaximumVariance 3 |
|
182 |
||
183 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
184 |
NSArray *twoTouches; |
|
185 |
UITouch *touch = [touches anyObject]; |
|
186 |
||
187 |
switch ([touches count]) { |
|
188 |
case 1: |
|
189 |
gestureStartPoint = [touch locationInView:self.view]; |
|
190 |
initialDistanceForPinching = 0; |
|
191 |
switch ([touch tapCount]) { |
|
192 |
case 1: |
|
193 |
NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
|
194 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
195 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
196 |
HW_click(); |
|
197 |
break; |
|
198 |
case 2: |
|
199 |
HW_ammoMenu(); |
|
200 |
break; |
|
201 |
default: |
|
202 |
break; |
|
203 |
} |
|
204 |
break; |
|
205 |
case 2: |
|
206 |
if (2 == [touch tapCount]) { |
|
207 |
HW_zoomReset(); |
|
208 |
} |
|
209 |
||
210 |
// pinching |
|
211 |
twoTouches = [touches allObjects]; |
|
212 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
213 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
214 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
215 |
break; |
|
216 |
default: |
|
217 |
break; |
|
218 |
} |
|
219 |
||
220 |
} |
|
221 |
||
222 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
223 |
initialDistanceForPinching = 0; |
|
224 |
gestureStartPoint.x = 0; |
|
225 |
gestureStartPoint.y = 0; |
|
226 |
HW_allKeysUp(); |
|
227 |
} |
|
228 |
||
229 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
230 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances. |
|
231 |
[self touchesEnded:touches withEvent:event]; |
|
232 |
} |
|
233 |
||
234 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
235 |
NSArray *twoTouches; |
|
236 |
CGPoint currentPosition; |
|
237 |
UITouch *touch = [touches anyObject]; |
|
238 |
||
239 |
switch ([touches count]) { |
|
240 |
case 1: |
|
241 |
currentPosition = [touch locationInView:self.view]; |
|
242 |
// panning |
|
243 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
244 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
245 |
// remember that we have x and y inverted |
|
246 |
/* temporarily disabling hog movements for camera panning testing |
|
247 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
|
248 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
|
249 |
CGFloat deltaX = fabsf(vertDiff); |
|
250 |
CGFloat deltaY = fabsf(horizDiff); |
|
251 |
||
252 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
|
253 |
NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x); |
|
254 |
if (horizDiff > 0) HW_walkLeft(); |
|
255 |
else HW_walkRight(); |
|
256 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
|
257 |
NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y); |
|
258 |
if (vertDiff < 0) HW_aimUp(); |
|
259 |
else HW_aimDown(); |
|
260 |
} |
|
261 |
*/ |
|
262 |
break; |
|
263 |
case 2: |
|
264 |
twoTouches = [touches allObjects]; |
|
265 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
266 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
267 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
268 |
||
269 |
if (0 == initialDistanceForPinching) |
|
270 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
271 |
||
272 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
|
273 |
HW_zoomOut(); |
|
274 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
|
275 |
HW_zoomIn(); |
|
276 |
||
277 |
currentDistanceOfPinching = initialDistanceForPinching; |
|
278 |
break; |
|
279 |
default: |
|
280 |
break; |
|
281 |
} |
|
282 |
} |
|
283 |
||
284 |
||
3006 | 285 |
@end |