author | nemo |
Fri, 26 Mar 2010 17:18:44 +0000 | |
changeset 3082 | 36586b751d64 |
parent 3073 | c22b395b6a2e |
child 3090 | 51629e69da51 |
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]; |
|
3015 | 93 |
|
3063 | 94 |
UIButton *theButton = (UIButton *)sender; |
3006 | 95 |
switch (theButton.tag) { |
96 |
case 0: |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
97 |
HW_walkLeft(); |
3006 | 98 |
break; |
99 |
case 1: |
|
100 |
HW_walkRight(); |
|
101 |
break; |
|
102 |
case 2: |
|
103 |
HW_aimUp(); |
|
104 |
break; |
|
105 |
case 3: |
|
106 |
HW_aimDown(); |
|
107 |
break; |
|
108 |
case 4: |
|
109 |
HW_shoot(); |
|
110 |
break; |
|
111 |
case 5: |
|
3015 | 112 |
HW_jump(); |
3006 | 113 |
break; |
114 |
case 6: |
|
3015 | 115 |
HW_backjump(); |
3006 | 116 |
break; |
3063 | 117 |
case 7: |
118 |
HW_pause(); |
|
119 |
break; |
|
120 |
case 8: |
|
121 |
HW_chat(); |
|
122 |
break; |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
123 |
case 9: |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
124 |
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"") |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
125 |
delegate:self |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
126 |
cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"") |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
127 |
destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"") |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
128 |
otherButtonTitles:nil]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
129 |
[actionSheet showInView:self.view]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
130 |
[actionSheet release]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
131 |
|
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
132 |
HW_pause(); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
133 |
break; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
134 |
case 10: |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
135 |
HW_tab(); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
136 |
break; |
3006 | 137 |
default: |
3063 | 138 |
NSLog(@"Nope"); |
3006 | 139 |
break; |
140 |
} |
|
141 |
} |
|
142 |
||
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
143 |
-(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
|
144 |
if ([actionSheet cancelButtonIndex] != buttonIndex) |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
145 |
HW_terminate(NO); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
146 |
else |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
147 |
HW_pause(); |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
148 |
} |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
149 |
|
3063 | 150 |
-(IBAction) showPopover{ |
151 |
//UIViewController *content = [[UIViewController alloc] initWithNibName: nil bundle:nil]; |
|
152 |
//CGRect rectArea = CGRectMake(0, 0, 320, 480); |
|
153 |
//content.view.frame = rectArea; |
|
154 |
//settings.view.frame = rectArea; |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
155 |
//popupMenuViewController *popupMenu = [[UIViewController alloc] initWithNibName:@"popupMenuViewController" bundle:nil]; |
3063 | 156 |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
157 |
UIViewController *popupMenu = [[UIViewController alloc] init]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
158 |
|
3063 | 159 |
UIButton *buttonPause = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
160 |
buttonPause.tag = 7; |
|
161 |
buttonPause.frame = CGRectMake(100, 170, 170, 30); |
|
162 |
[buttonPause setTitle:@"Pause Game" forState:UIControlStateNormal]; |
|
163 |
[buttonPause addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; |
|
164 |
[popupMenu.view addSubview:buttonPause]; |
|
165 |
||
166 |
UIButton *buttonChat = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
|
167 |
buttonChat.tag = 8; |
|
168 |
buttonChat.frame = CGRectMake(100, 220, 170, 30); |
|
169 |
[buttonChat setTitle:@"Chat" forState:UIControlStateNormal]; |
|
170 |
[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
|
171 |
[popupMenu.view addSubview:buttonChat]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
172 |
|
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
173 |
UIButton *buttonEnd = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
174 |
buttonEnd.tag = 9; |
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
175 |
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
|
176 |
[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
|
177 |
[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
|
178 |
[popupMenu.view addSubview:buttonEnd]; |
3063 | 179 |
|
180 |
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
181 |
[aPopover setPopoverContentSize:CGSizeMake(220, 480) animated:YES]; |
3063 | 182 |
|
3073
c22b395b6a2e
add another button to close the game and return to the iFrontend (untested)
koda
parents:
3063
diff
changeset
|
183 |
[aPopover presentPopoverFromRect:CGRectMake(1024, 0, 320, 480) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
3063 | 184 |
//UIBarButtonItem *sender = [[useless items] objectAtIndex:1]; |
185 |
//[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
|
186 |
} |
|
187 |
||
188 |
||
3025 | 189 |
#pragma mark - |
190 |
#pragma mark Custom SDL_UIView input handling |
|
191 |
#define kMinimumPinchDelta 50 |
|
192 |
#define kMinimumGestureLength 10 |
|
193 |
#define kMaximumVariance 3 |
|
194 |
||
195 |
// we override default touch input to implement our own gestures |
|
196 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
197 |
NSArray *twoTouches; |
|
198 |
UITouch *touch = [touches anyObject]; |
|
199 |
||
200 |
switch ([touches count]) { |
|
201 |
case 1: |
|
202 |
gestureStartPoint = [touch locationInView:self.view]; |
|
203 |
initialDistanceForPinching = 0; |
|
204 |
switch ([touch tapCount]) { |
|
205 |
case 1: |
|
206 |
NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
|
207 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
208 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
209 |
HW_click(); |
|
210 |
break; |
|
211 |
case 2: |
|
212 |
HW_ammoMenu(); |
|
213 |
break; |
|
214 |
default: |
|
215 |
break; |
|
216 |
} |
|
217 |
break; |
|
218 |
case 2: |
|
219 |
if (2 == [touch tapCount]) { |
|
220 |
HW_zoomReset(); |
|
221 |
} |
|
222 |
||
223 |
// pinching |
|
224 |
twoTouches = [touches allObjects]; |
|
225 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
226 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
227 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
228 |
break; |
|
229 |
default: |
|
230 |
break; |
|
231 |
} |
|
232 |
||
233 |
} |
|
234 |
||
235 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
236 |
initialDistanceForPinching = 0; |
|
237 |
gestureStartPoint.x = 0; |
|
238 |
gestureStartPoint.y = 0; |
|
239 |
HW_allKeysUp(); |
|
240 |
} |
|
241 |
||
242 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
243 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances. |
|
244 |
[self touchesEnded:touches withEvent:event]; |
|
245 |
} |
|
246 |
||
247 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
248 |
NSArray *twoTouches; |
|
249 |
CGPoint currentPosition; |
|
250 |
UITouch *touch = [touches anyObject]; |
|
251 |
||
252 |
switch ([touches count]) { |
|
253 |
case 1: |
|
254 |
currentPosition = [touch locationInView:self.view]; |
|
255 |
// panning |
|
256 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
257 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
258 |
// remember that we have x and y inverted |
|
259 |
/* temporarily disabling hog movements for camera panning testing |
|
260 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
|
261 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
|
262 |
CGFloat deltaX = fabsf(vertDiff); |
|
263 |
CGFloat deltaY = fabsf(horizDiff); |
|
264 |
||
265 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
|
266 |
NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x); |
|
267 |
if (horizDiff > 0) HW_walkLeft(); |
|
268 |
else HW_walkRight(); |
|
269 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
|
270 |
NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y); |
|
271 |
if (vertDiff < 0) HW_aimUp(); |
|
272 |
else HW_aimDown(); |
|
273 |
} |
|
274 |
*/ |
|
275 |
break; |
|
276 |
case 2: |
|
277 |
twoTouches = [touches allObjects]; |
|
278 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
279 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
280 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
281 |
||
282 |
if (0 == initialDistanceForPinching) |
|
283 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
284 |
||
285 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
|
286 |
HW_zoomOut(); |
|
287 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
|
288 |
HW_zoomIn(); |
|
289 |
||
290 |
currentDistanceOfPinching = initialDistanceForPinching; |
|
291 |
break; |
|
292 |
default: |
|
293 |
break; |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
||
3006 | 298 |
|
299 |
@end |