author | koda |
Fri, 26 Mar 2010 23:47:12 +0000 | |
changeset 3090 | 51629e69da51 |
parent 3073 | c22b395b6a2e |
child 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 "SettingsViewController.h" |
15 |
#import "popupMenuViewController.h" |
|
3006 | 16 |
|
17 |
@implementation overlayViewController |
|
3015 | 18 |
@synthesize dimTimer; |
19 |
||
3006 | 20 |
|
21 |
-(void) didReceiveMemoryWarning { |
|
22 |
// Releases the view if it doesn't have a superview. |
|
23 |
[super didReceiveMemoryWarning]; |
|
24 |
||
25 |
// Release any cached data, images, etc that aren't in use. |
|
26 |
} |
|
27 |
||
3015 | 28 |
-(void) viewDidLoad { |
3027 | 29 |
self.view.alpha = 0; |
3034 | 30 |
|
31 |
// needed for rotation to work on os < 3.2 |
|
32 |
self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0); |
|
33 |
self.view.transform = CGAffineTransformRotate(self.view.transform, (M_PI/2.0)); |
|
3027 | 34 |
|
3015 | 35 |
dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6] |
36 |
interval:1000 |
|
37 |
target:self |
|
38 |
selector:@selector(dimOverlay) |
|
39 |
userInfo:nil |
|
40 |
repeats:YES]; |
|
41 |
||
42 |
[[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode]; |
|
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
43 |
|
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
44 |
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showMenuAfterwards) userInfo:nil repeats:NO]; |
3015 | 45 |
} |
46 |
||
3034 | 47 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
3027 | 48 |
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); |
49 |
} |
|
50 |
||
3006 | 51 |
-(void) viewDidUnload { |
3015 | 52 |
[dimTimer invalidate]; |
3006 | 53 |
} |
54 |
||
55 |
-(void) dealloc { |
|
3029
67483e87590c
a couple of smaller cleanups that didn't get in previous commit
koda
parents:
3027
diff
changeset
|
56 |
// dimTimer is autoreleased |
3006 | 57 |
[super dealloc]; |
58 |
} |
|
59 |
||
3027 | 60 |
// draws the controller overlay after the sdl window has taken control |
61 |
-(void) showMenuAfterwards { |
|
62 |
[[SDLUIKitDelegate sharedAppDelegate].uiwindow bringSubviewToFront:self.view]; |
|
63 |
||
64 |
[UIView beginAnimations:@"showing overlay" context:NULL]; |
|
65 |
[UIView setAnimationDuration:1]; |
|
66 |
self.view.alpha = 1; |
|
67 |
[UIView commitAnimations]; |
|
68 |
} |
|
3006 | 69 |
|
70 |
// dim the overlay when there's no more input for a certain amount of time |
|
71 |
-(IBAction) buttonReleased:(id) sender { |
|
72 |
HW_allKeysUp(); |
|
3015 | 73 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]]; |
3006 | 74 |
} |
75 |
||
3015 | 76 |
// nice transition for dimming |
3006 | 77 |
-(void) dimOverlay { |
78 |
[UIView beginAnimations:@"overlay dim" context:NULL]; |
|
79 |
[UIView setAnimationDuration:0.6]; |
|
80 |
self.view.alpha = 0.2; |
|
81 |
[UIView commitAnimations]; |
|
82 |
} |
|
83 |
||
3015 | 84 |
// set the overlay visible and put off the timer for enough time |
3006 | 85 |
-(void) activateOverlay { |
86 |
self.view.alpha = 1; |
|
3015 | 87 |
[dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1000]]; |
3006 | 88 |
} |
89 |
||
90 |
// issue certain action based on the tag of the button |
|
91 |
-(IBAction) buttonPressed:(id) sender { |
|
92 |
[self activateOverlay]; |
|
3090 | 93 |
UIActionSheet *actionSheet; |
3063 | 94 |
UIButton *theButton = (UIButton *)sender; |
3090 | 95 |
|
3006 | 96 |
switch (theButton.tag) { |
97 |
case 0: |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
98 |
HW_walkLeft(); |
3006 | 99 |
break; |
100 |
case 1: |
|
101 |
HW_walkRight(); |
|
102 |
break; |
|
103 |
case 2: |
|
104 |
HW_aimUp(); |
|
105 |
break; |
|
106 |
case 3: |
|
107 |
HW_aimDown(); |
|
108 |
break; |
|
109 |
case 4: |
|
110 |
HW_shoot(); |
|
111 |
break; |
|
112 |
case 5: |
|
3015 | 113 |
HW_jump(); |
3006 | 114 |
break; |
115 |
case 6: |
|
3015 | 116 |
HW_backjump(); |
3006 | 117 |
break; |
3063 | 118 |
case 7: |
119 |
HW_pause(); |
|
120 |
break; |
|
121 |
case 8: |
|
122 |
HW_chat(); |
|
123 |
break; |
|
3090 | 124 |
case 9: |
125 |
actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"") |
|
126 |
delegate:self |
|
127 |
cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"") |
|
128 |
destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"") |
|
129 |
otherButtonTitles:nil]; |
|
130 |
[actionSheet showInView:self.view]; |
|
131 |
[actionSheet release]; |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
132 |
|
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
133 |
HW_pause(); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
134 |
break; |
3090 | 135 |
case 10: |
136 |
HW_tab(); |
|
137 |
break; |
|
3006 | 138 |
default: |
3063 | 139 |
NSLog(@"Nope"); |
3006 | 140 |
break; |
141 |
} |
|
142 |
} |
|
143 |
||
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
144 |
-(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
|
145 |
if ([actionSheet cancelButtonIndex] != buttonIndex) |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
146 |
HW_terminate(NO); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
147 |
else |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
148 |
HW_pause(); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
149 |
} |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
150 |
|
3063 | 151 |
-(IBAction) showPopover{ |
152 |
//UIViewController *content = [[UIViewController alloc] initWithNibName: nil bundle:nil]; |
|
153 |
//CGRect rectArea = CGRectMake(0, 0, 320, 480); |
|
154 |
//content.view.frame = rectArea; |
|
155 |
//settings.view.frame = rectArea; |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
156 |
//popupMenuViewController *popupMenu = [[UIViewController alloc] initWithNibName:@"popupMenuViewController" bundle:nil]; |
3063 | 157 |
|
3090 | 158 |
popupMenuViewController *popupMenu = [[popupMenuViewController alloc] initWithNibName:@"popupMenuViewController" bundle:nil]; |
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
159 |
|
3090 | 160 |
/*UIButton *buttonPause = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
3063 | 161 |
buttonPause.tag = 7; |
162 |
buttonPause.frame = CGRectMake(100, 170, 170, 30); |
|
163 |
[buttonPause setTitle:@"Pause Game" forState:UIControlStateNormal]; |
|
164 |
[buttonPause addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; |
|
165 |
[popupMenu.view addSubview:buttonPause]; |
|
166 |
||
167 |
UIButton *buttonChat = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
|
168 |
buttonChat.tag = 8; |
|
169 |
buttonChat.frame = CGRectMake(100, 220, 170, 30); |
|
170 |
[buttonChat setTitle:@"Chat" forState:UIControlStateNormal]; |
|
171 |
[buttonChat addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
172 |
[popupMenu.view addSubview:buttonChat]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
173 |
|
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
174 |
UIButton *buttonEnd = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
175 |
buttonEnd.tag = 9; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
176 |
buttonEnd.frame = CGRectMake(100, 270, 170, 30); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
177 |
[buttonEnd setTitle:@"End Game" forState:UIControlStateNormal]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
178 |
[buttonEnd addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
179 |
[popupMenu.view addSubview:buttonEnd]; |
3090 | 180 |
*/ |
3063 | 181 |
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
3090 | 182 |
[aPopover setPopoverContentSize:CGSizeMake(220, 170) animated:YES]; |
3063 | 183 |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
184 |
[aPopover presentPopoverFromRect:CGRectMake(1024, 0, 320, 480) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
3063 | 185 |
//UIBarButtonItem *sender = [[useless items] objectAtIndex:1]; |
186 |
//[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
|
187 |
} |
|
188 |
||
189 |
||
3025 | 190 |
#pragma mark - |
191 |
#pragma mark Custom SDL_UIView input handling |
|
192 |
#define kMinimumPinchDelta 50 |
|
193 |
#define kMinimumGestureLength 10 |
|
194 |
#define kMaximumVariance 3 |
|
195 |
||
196 |
// we override default touch input to implement our own gestures |
|
197 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
198 |
NSArray *twoTouches; |
|
199 |
UITouch *touch = [touches anyObject]; |
|
200 |
||
201 |
switch ([touches count]) { |
|
202 |
case 1: |
|
203 |
gestureStartPoint = [touch locationInView:self.view]; |
|
204 |
initialDistanceForPinching = 0; |
|
205 |
switch ([touch tapCount]) { |
|
206 |
case 1: |
|
207 |
NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
|
208 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
209 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
210 |
HW_click(); |
|
211 |
break; |
|
212 |
case 2: |
|
213 |
HW_ammoMenu(); |
|
214 |
break; |
|
215 |
default: |
|
216 |
break; |
|
217 |
} |
|
218 |
break; |
|
219 |
case 2: |
|
220 |
if (2 == [touch tapCount]) { |
|
221 |
HW_zoomReset(); |
|
222 |
} |
|
223 |
||
224 |
// pinching |
|
225 |
twoTouches = [touches allObjects]; |
|
226 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
227 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
228 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
229 |
break; |
|
230 |
default: |
|
231 |
break; |
|
232 |
} |
|
233 |
||
234 |
} |
|
235 |
||
236 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
237 |
initialDistanceForPinching = 0; |
|
238 |
gestureStartPoint.x = 0; |
|
239 |
gestureStartPoint.y = 0; |
|
240 |
HW_allKeysUp(); |
|
241 |
} |
|
242 |
||
243 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
244 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances. |
|
245 |
[self touchesEnded:touches withEvent:event]; |
|
246 |
} |
|
247 |
||
248 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
249 |
NSArray *twoTouches; |
|
250 |
CGPoint currentPosition; |
|
251 |
UITouch *touch = [touches anyObject]; |
|
252 |
||
253 |
switch ([touches count]) { |
|
254 |
case 1: |
|
255 |
currentPosition = [touch locationInView:self.view]; |
|
256 |
// panning |
|
257 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
258 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
259 |
// remember that we have x and y inverted |
|
260 |
/* temporarily disabling hog movements for camera panning testing |
|
261 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
|
262 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
|
263 |
CGFloat deltaX = fabsf(vertDiff); |
|
264 |
CGFloat deltaY = fabsf(horizDiff); |
|
265 |
||
266 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
|
267 |
NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x); |
|
268 |
if (horizDiff > 0) HW_walkLeft(); |
|
269 |
else HW_walkRight(); |
|
270 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
|
271 |
NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y); |
|
272 |
if (vertDiff < 0) HW_aimUp(); |
|
273 |
else HW_aimDown(); |
|
274 |
} |
|
275 |
*/ |
|
276 |
break; |
|
277 |
case 2: |
|
278 |
twoTouches = [touches allObjects]; |
|
279 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
280 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
281 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
282 |
||
283 |
if (0 == initialDistanceForPinching) |
|
284 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
285 |
||
286 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
|
287 |
HW_zoomOut(); |
|
288 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
|
289 |
HW_zoomIn(); |
|
290 |
||
291 |
currentDistanceOfPinching = initialDistanceForPinching; |
|
292 |
break; |
|
293 |
default: |
|
294 |
break; |
|
295 |
} |
|
296 |
} |
|
297 |
||
298 |
||
3006 | 299 |
|
300 |
@end |