author | nemo |
Sat, 27 Mar 2010 14:49:04 +0000 | |
changeset 3108 | 2d666993854a |
parent 3091 | 9d05c8000ed4 |
child 3113 | 2829ea0dd47c |
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{ |
3090 | 152 |
popupMenuViewController *popupMenu = [[popupMenuViewController alloc] initWithNibName:@"popupMenuViewController" bundle:nil]; |
3063 | 153 |
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
3090 | 154 |
[aPopover setPopoverContentSize:CGSizeMake(220, 170) animated:YES]; |
3063 | 155 |
|
3091 | 156 |
/*UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
157 |
button.frame= CGRectMake(960, 0, 64, 64); |
|
158 |
button.titleLabel.text=@"UUUUUUUF"; |
|
159 |
[self.view addSubview:button];*/ |
|
160 |
||
161 |
[aPopover presentPopoverFromRect:CGRectMake(960, 0, 220, 32) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; |
|
3063 | 162 |
//UIBarButtonItem *sender = [[useless items] objectAtIndex:1]; |
163 |
//[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES]; |
|
164 |
} |
|
165 |
||
166 |
||
3025 | 167 |
#pragma mark - |
168 |
#pragma mark Custom SDL_UIView input handling |
|
169 |
#define kMinimumPinchDelta 50 |
|
170 |
#define kMinimumGestureLength 10 |
|
171 |
#define kMaximumVariance 3 |
|
172 |
||
173 |
// we override default touch input to implement our own gestures |
|
174 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
175 |
NSArray *twoTouches; |
|
176 |
UITouch *touch = [touches anyObject]; |
|
177 |
||
178 |
switch ([touches count]) { |
|
179 |
case 1: |
|
180 |
gestureStartPoint = [touch locationInView:self.view]; |
|
181 |
initialDistanceForPinching = 0; |
|
182 |
switch ([touch tapCount]) { |
|
183 |
case 1: |
|
184 |
NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y ); |
|
185 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
186 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
187 |
HW_click(); |
|
188 |
break; |
|
189 |
case 2: |
|
190 |
HW_ammoMenu(); |
|
191 |
break; |
|
192 |
default: |
|
193 |
break; |
|
194 |
} |
|
195 |
break; |
|
196 |
case 2: |
|
197 |
if (2 == [touch tapCount]) { |
|
198 |
HW_zoomReset(); |
|
199 |
} |
|
200 |
||
201 |
// pinching |
|
202 |
twoTouches = [touches allObjects]; |
|
203 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
204 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
205 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
206 |
break; |
|
207 |
default: |
|
208 |
break; |
|
209 |
} |
|
210 |
||
211 |
} |
|
212 |
||
213 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
214 |
initialDistanceForPinching = 0; |
|
215 |
gestureStartPoint.x = 0; |
|
216 |
gestureStartPoint.y = 0; |
|
217 |
HW_allKeysUp(); |
|
218 |
} |
|
219 |
||
220 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
221 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances. |
|
222 |
[self touchesEnded:touches withEvent:event]; |
|
223 |
} |
|
224 |
||
225 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
226 |
NSArray *twoTouches; |
|
227 |
CGPoint currentPosition; |
|
228 |
UITouch *touch = [touches anyObject]; |
|
229 |
||
230 |
switch ([touches count]) { |
|
231 |
case 1: |
|
232 |
currentPosition = [touch locationInView:self.view]; |
|
233 |
// panning |
|
234 |
SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, |
|
235 |
(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x); |
|
236 |
// remember that we have x and y inverted |
|
237 |
/* temporarily disabling hog movements for camera panning testing |
|
238 |
CGFloat vertDiff = gestureStartPoint.x - currentPosition.x; |
|
239 |
CGFloat horizDiff = gestureStartPoint.y - currentPosition.y; |
|
240 |
CGFloat deltaX = fabsf(vertDiff); |
|
241 |
CGFloat deltaY = fabsf(horizDiff); |
|
242 |
||
243 |
if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) { |
|
244 |
NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x); |
|
245 |
if (horizDiff > 0) HW_walkLeft(); |
|
246 |
else HW_walkRight(); |
|
247 |
} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){ |
|
248 |
NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y); |
|
249 |
if (vertDiff < 0) HW_aimUp(); |
|
250 |
else HW_aimDown(); |
|
251 |
} |
|
252 |
*/ |
|
253 |
break; |
|
254 |
case 2: |
|
255 |
twoTouches = [touches allObjects]; |
|
256 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
257 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
258 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
259 |
||
260 |
if (0 == initialDistanceForPinching) |
|
261 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
262 |
||
263 |
if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) |
|
264 |
HW_zoomOut(); |
|
265 |
else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta) |
|
266 |
HW_zoomIn(); |
|
267 |
||
268 |
currentDistanceOfPinching = initialDistanceForPinching; |
|
269 |
break; |
|
270 |
default: |
|
271 |
break; |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
||
3006 | 276 |
|
277 |
@end |