author | koda |
Wed, 07 Jul 2010 03:03:59 +0200 | |
changeset 3627 | f1da1d8fb56c |
parent 3626 | 19f78afa0188 |
child 3628 | 2b4d878ba565 |
permissions | -rw-r--r-- |
3547 | 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 "SDL_config_iphoneos.h" |
|
15 |
#import "PopoverMenuViewController.h" |
|
16 |
#import "CommodityFunctions.h" |
|
17 |
||
18 |
#define HIDING_TIME_DEFAULT [NSDate dateWithTimeIntervalSinceNow:2.7] |
|
19 |
#define HIDING_TIME_NEVER [NSDate dateWithTimeIntervalSinceNow:10000] |
|
20 |
||
21 |
||
22 |
@implementation OverlayViewController |
|
23 |
@synthesize popoverController, popupMenu, writeChatTextField, spinningWheel; |
|
24 |
||
25 |
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
|
26 |
return rotationManager(interfaceOrientation); |
|
27 |
} |
|
28 |
||
29 |
-(void) didRotate:(NSNotification *)notification { |
|
30 |
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; |
|
31 |
CGRect rect = [[UIScreen mainScreen] bounds]; |
|
32 |
CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height); |
|
33 |
UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:12345]; |
|
34 |
||
35 |
[UIView beginAnimations:@"rotation" context:NULL]; |
|
36 |
[UIView setAnimationDuration:0.8f]; |
|
37 |
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; |
|
38 |
switch (orientation) { |
|
39 |
case UIDeviceOrientationLandscapeLeft: |
|
40 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(0)); |
|
41 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); |
|
42 |
[self chatDisappear]; |
|
43 |
HW_setLandscape(YES); |
|
44 |
break; |
|
45 |
case UIDeviceOrientationLandscapeRight: |
|
46 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(180)); |
|
47 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90)); |
|
48 |
[self chatDisappear]; |
|
49 |
HW_setLandscape(YES); |
|
50 |
break; |
|
3551 | 51 |
/* |
3547 | 52 |
case UIDeviceOrientationPortrait: |
53 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
|
54 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(270)); |
|
55 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0)); |
|
56 |
[self chatAppear]; |
|
57 |
HW_setLandscape(NO); |
|
58 |
} |
|
59 |
break; |
|
60 |
case UIDeviceOrientationPortraitUpsideDown: |
|
61 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
|
62 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); |
|
63 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(180)); |
|
64 |
[self chatAppear]; |
|
65 |
HW_setLandscape(NO); |
|
66 |
} |
|
67 |
break; |
|
3551 | 68 |
*/ |
3547 | 69 |
default: |
70 |
break; |
|
71 |
} |
|
72 |
self.view.frame = usefulRect; |
|
73 |
//sdlView.frame = usefulRect; |
|
74 |
[UIView commitAnimations]; |
|
75 |
} |
|
76 |
||
77 |
-(void) chatAppear { |
|
3617 | 78 |
/* |
3547 | 79 |
if (writeChatTextField == nil) { |
80 |
writeChatTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, 768, [UIFont systemFontSize]+8)]; |
|
81 |
writeChatTextField.textColor = [UIColor whiteColor]; |
|
82 |
writeChatTextField.backgroundColor = [UIColor blueColor]; |
|
83 |
writeChatTextField.autocapitalizationType = UITextAutocapitalizationTypeNone; |
|
84 |
writeChatTextField.autocorrectionType = UITextAutocorrectionTypeNo; |
|
85 |
writeChatTextField.enablesReturnKeyAutomatically = NO; |
|
86 |
writeChatTextField.keyboardAppearance = UIKeyboardAppearanceDefault; |
|
87 |
writeChatTextField.keyboardType = UIKeyboardTypeDefault; |
|
88 |
writeChatTextField.returnKeyType = UIReturnKeyDefault; |
|
89 |
writeChatTextField.secureTextEntry = NO; |
|
90 |
[self.view addSubview:writeChatTextField]; |
|
91 |
} |
|
92 |
writeChatTextField.alpha = 1; |
|
93 |
[self activateOverlay]; |
|
94 |
[dimTimer setFireDate:HIDING_TIME_NEVER]; |
|
3617 | 95 |
*/ |
3547 | 96 |
} |
97 |
||
98 |
-(void) chatDisappear { |
|
3617 | 99 |
/* |
3547 | 100 |
writeChatTextField.alpha = 0; |
101 |
[writeChatTextField resignFirstResponder]; |
|
102 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT]; |
|
3617 | 103 |
*/ |
3547 | 104 |
} |
105 |
||
106 |
#pragma mark - |
|
107 |
#pragma mark View Management |
|
108 |
-(void) viewDidLoad { |
|
109 |
isPopoverVisible = NO; |
|
110 |
singleton = self.spinningWheel; |
|
111 |
canDim = NO; |
|
112 |
self.view.alpha = 0; |
|
113 |
self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0); |
|
114 |
||
3627
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
115 |
// set initial orientation |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
116 |
///[self didRotate:[NSNotification notificationWithName:UIDeviceOrientationDidChangeNotification object:nil]]; |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
117 |
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
118 |
UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:12345]; |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
119 |
switch (orientation) { |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
120 |
case UIDeviceOrientationLandscapeLeft: |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
121 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(0)); |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
122 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90)); |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
123 |
break; |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
124 |
case UIDeviceOrientationLandscapeRight: |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
125 |
sdlView.transform = CGAffineTransformMakeRotation(degreesToRadian(180)); |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
126 |
self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(-90)); |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
127 |
break; |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
128 |
} |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
129 |
CGRect rect = [[UIScreen mainScreen] bounds]; |
f1da1d8fb56c
the game now respects the orientation of the frontend
koda
parents:
3626
diff
changeset
|
130 |
self.view.frame = CGRectMake(0, 0, rect.size.width, rect.size.height); |
3547 | 131 |
|
132 |
dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6] |
|
133 |
interval:1000 |
|
134 |
target:self |
|
135 |
selector:@selector(dimOverlay) |
|
136 |
userInfo:nil |
|
137 |
repeats:YES]; |
|
138 |
||
139 |
// add timer too runloop, otherwise it doesn't work |
|
140 |
[[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode]; |
|
141 |
||
142 |
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; |
|
143 |
[[NSNotificationCenter defaultCenter] addObserver:self |
|
144 |
selector:@selector(didRotate:) |
|
3598 | 145 |
name:UIDeviceOrientationDidChangeNotification |
3547 | 146 |
object:nil]; |
147 |
||
148 |
[UIView beginAnimations:@"showing overlay" context:NULL]; |
|
149 |
[UIView setAnimationDuration:1]; |
|
150 |
self.view.alpha = 1; |
|
151 |
[UIView commitAnimations]; |
|
152 |
} |
|
153 |
||
3626 | 154 |
/* these are causing problems at reloading so let's remove 'em |
3547 | 155 |
-(void) viewDidUnload { |
3625 | 156 |
[popoverController dismissPopoverAnimated:NO]; |
3617 | 157 |
[dimTimer invalidate]; |
3547 | 158 |
self.writeChatTextField = nil; |
159 |
self.popoverController = nil; |
|
160 |
self.popupMenu = nil; |
|
161 |
self.spinningWheel = nil; |
|
162 |
[super viewDidUnload]; |
|
163 |
MSG_DIDUNLOAD(); |
|
164 |
} |
|
165 |
||
3626 | 166 |
-(void) didReceiveMemoryWarning { |
167 |
// Releases the view if it doesn't have a superview. |
|
168 |
[super didReceiveMemoryWarning]; |
|
169 |
// Release any cached data, images, etc that aren't in use. |
|
170 |
if (popupMenu.view.superview == nil) |
|
171 |
popupMenu = nil; |
|
172 |
MSG_MEMCLEAN(); |
|
173 |
} |
|
174 |
*/ |
|
175 |
||
3547 | 176 |
-(void) dealloc { |
177 |
[writeChatTextField release]; |
|
178 |
[popupMenu release]; |
|
179 |
[popoverController release]; |
|
180 |
// dimTimer is autoreleased |
|
181 |
[spinningWheel release]; |
|
182 |
[super dealloc]; |
|
183 |
} |
|
184 |
||
185 |
#pragma mark - |
|
186 |
#pragma mark Overlay actions and members |
|
187 |
// nice transition for dimming, should be called only by the timer himself |
|
188 |
-(void) dimOverlay { |
|
189 |
if (canDim) { |
|
190 |
[UIView beginAnimations:@"overlay dim" context:NULL]; |
|
191 |
[UIView setAnimationDuration:0.6]; |
|
192 |
self.view.alpha = 0.2; |
|
193 |
[UIView commitAnimations]; |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
// set the overlay visible and put off the timer for enough time |
|
198 |
-(void) activateOverlay { |
|
199 |
self.view.alpha = 1; |
|
200 |
[dimTimer setFireDate:HIDING_TIME_NEVER]; |
|
201 |
} |
|
202 |
||
3626 | 203 |
// dim the overlay when there's no more input for a certain amount of time |
204 |
-(IBAction) buttonReleased:(id) sender { |
|
205 |
UIButton *theButton = (UIButton *)sender; |
|
206 |
||
207 |
switch (theButton.tag) { |
|
208 |
case 0: |
|
209 |
case 1: |
|
210 |
case 2: |
|
211 |
case 3: |
|
212 |
HW_walkingKeysUp(); |
|
213 |
break; |
|
214 |
case 4: |
|
215 |
case 5: |
|
216 |
case 6: |
|
217 |
HW_otherKeysUp(); |
|
218 |
break; |
|
219 |
default: |
|
220 |
NSLog(@"Nope"); |
|
221 |
break; |
|
222 |
} |
|
223 |
||
224 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT]; |
|
225 |
} |
|
226 |
||
3547 | 227 |
// issue certain action based on the tag of the button |
228 |
-(IBAction) buttonPressed:(id) sender { |
|
229 |
[self activateOverlay]; |
|
230 |
if (isPopoverVisible) { |
|
231 |
[self dismissPopover]; |
|
232 |
} |
|
233 |
UIButton *theButton = (UIButton *)sender; |
|
234 |
||
235 |
switch (theButton.tag) { |
|
236 |
case 0: |
|
237 |
HW_walkLeft(); |
|
238 |
break; |
|
239 |
case 1: |
|
240 |
HW_walkRight(); |
|
241 |
break; |
|
242 |
case 2: |
|
243 |
HW_aimUp(); |
|
244 |
break; |
|
245 |
case 3: |
|
246 |
HW_aimDown(); |
|
247 |
break; |
|
248 |
case 4: |
|
249 |
HW_shoot(); |
|
250 |
break; |
|
251 |
case 5: |
|
252 |
HW_jump(); |
|
253 |
break; |
|
254 |
case 6: |
|
255 |
HW_backjump(); |
|
256 |
break; |
|
257 |
case 7: |
|
258 |
HW_tab(); |
|
259 |
break; |
|
260 |
case 10: |
|
261 |
[self showPopover]; |
|
262 |
break; |
|
3624 | 263 |
case 11: |
264 |
HW_ammoMenu(); |
|
265 |
break; |
|
3547 | 266 |
default: |
3626 | 267 |
DLog(@"Nope"); |
3547 | 268 |
break; |
269 |
} |
|
270 |
} |
|
271 |
||
272 |
// present a further check before closing game |
|
273 |
-(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
|
274 |
if ([actionSheet cancelButtonIndex] != buttonIndex) |
|
275 |
HW_terminate(NO); |
|
276 |
else |
|
277 |
HW_pause(); |
|
278 |
} |
|
279 |
||
280 |
// show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize |
|
281 |
// on iphone instead just use the tableViewController directly (and implement manually all animations) |
|
282 |
-(IBAction) showPopover{ |
|
283 |
isPopoverVisible = YES; |
|
284 |
||
285 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
|
286 |
if (popupMenu == nil) |
|
287 |
popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStylePlain]; |
|
288 |
if (popoverController == nil) { |
|
289 |
popoverController = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
|
290 |
[popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES]; |
|
291 |
[popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]]; |
|
292 |
} |
|
3551 | 293 |
|
294 |
[popoverController presentPopoverFromRect:CGRectMake(1000, 0, 220, 32) |
|
3547 | 295 |
inView:self.view |
296 |
permittedArrowDirections:UIPopoverArrowDirectionUp |
|
297 |
animated:YES]; |
|
298 |
} else { |
|
299 |
if (popupMenu == nil) { |
|
300 |
popupMenu = [[PopoverMenuViewController alloc] initWithStyle:UITableViewStyleGrouped]; |
|
301 |
popupMenu.view.backgroundColor = [UIColor clearColor]; |
|
302 |
popupMenu.view.frame = CGRectMake(480, 0, 200, 170); |
|
303 |
} |
|
304 |
[self.view addSubview:popupMenu.view]; |
|
305 |
||
306 |
[UIView beginAnimations:@"showing popover" context:NULL]; |
|
307 |
[UIView setAnimationDuration:0.35]; |
|
308 |
popupMenu.view.frame = CGRectMake(280, 0, 200, 170); |
|
309 |
[UIView commitAnimations]; |
|
310 |
} |
|
311 |
popupMenu.tableView.scrollEnabled = NO; |
|
312 |
} |
|
313 |
||
314 |
// on ipad just dismiss it, on iphone transtion to the right |
|
315 |
-(void) dismissPopover { |
|
316 |
if (YES == isPopoverVisible) { |
|
317 |
isPopoverVisible = NO; |
|
318 |
||
319 |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
|
320 |
[popoverController dismissPopoverAnimated:YES]; |
|
321 |
} else { |
|
322 |
[UIView beginAnimations:@"hiding popover" context:NULL]; |
|
323 |
[UIView setAnimationDuration:0.35]; |
|
324 |
popupMenu.view.frame = CGRectMake(480, 0, 200, 170); |
|
325 |
[UIView commitAnimations]; |
|
326 |
||
327 |
[popupMenu.view performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.35]; |
|
328 |
} |
|
329 |
[self buttonReleased:nil]; |
|
330 |
} |
|
331 |
} |
|
332 |
||
333 |
-(void) textFieldDoneEditing:(id) sender{ |
|
334 |
[sender resignFirstResponder]; |
|
335 |
} |
|
336 |
||
337 |
// this function is called by pascal FinishProgress and removes the spinning wheel when loading is done |
|
338 |
void spinningWheelDone (void) { |
|
3551 | 339 |
[singleton stopAnimating]; |
340 |
singleton = nil; |
|
3547 | 341 |
canDim = YES; |
342 |
} |
|
343 |
||
344 |
#pragma mark - |
|
345 |
#pragma mark Custom touch event handling |
|
346 |
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
|
347 |
NSArray *twoTouches; |
|
348 |
UITouch *touch = [touches anyObject]; |
|
3551 | 349 |
CGRect screen = [[UIScreen mainScreen] bounds]; |
350 |
CGPoint currentPosition = [touch locationInView:self.view]; |
|
3547 | 351 |
|
352 |
if (isPopoverVisible) { |
|
353 |
[self dismissPopover]; |
|
354 |
} |
|
355 |
if (writeChatTextField) { |
|
356 |
[self.writeChatTextField resignFirstResponder]; |
|
357 |
[dimTimer setFireDate:HIDING_TIME_DEFAULT]; |
|
358 |
} |
|
3598 | 359 |
|
360 |
if (currentPosition.y < screen.size.width - 120) { |
|
361 |
switch ([touches count]) { |
|
362 |
case 1: |
|
363 |
DLog(@"X:%d Y:%d", HWX(currentPosition.x), HWY(currentPosition.y)); |
|
364 |
HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y)); |
|
3624 | 365 |
if (2 == [touch tapCount]) |
366 |
HW_zoomReset(); |
|
3598 | 367 |
break; |
3624 | 368 |
case 2: |
3598 | 369 |
// pinching |
370 |
twoTouches = [touches allObjects]; |
|
371 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
372 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
373 |
initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
374 |
break; |
|
375 |
default: |
|
376 |
break; |
|
377 |
} |
|
3547 | 378 |
} |
379 |
} |
|
380 |
||
381 |
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
|
382 |
initialDistanceForPinching = 0; |
|
383 |
//HW_allKeysUp(); |
|
3551 | 384 |
HW_click(); |
3547 | 385 |
} |
386 |
||
387 |
-(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { |
|
388 |
// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances |
|
389 |
[self touchesEnded:touches withEvent:event]; |
|
390 |
} |
|
391 |
||
392 |
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
|
3551 | 393 |
CGRect screen = [[UIScreen mainScreen] bounds]; |
3547 | 394 |
|
395 |
NSArray *twoTouches; |
|
396 |
CGPoint currentPosition; |
|
397 |
UITouch *touch = [touches anyObject]; |
|
398 |
||
399 |
switch ([touches count]) { |
|
3551 | 400 |
case 1: |
401 |
if (HW_isAmmoOpen()) { |
|
402 |
currentPosition = [touch locationInView:self.view]; |
|
403 |
DLog(@"X:%d Y:%d", HWX(currentPosition.x), HWY(currentPosition.y)); |
|
404 |
HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y)); |
|
3547 | 405 |
} |
3551 | 406 |
break; |
3547 | 407 |
case 2: |
408 |
twoTouches = [touches allObjects]; |
|
409 |
UITouch *first = [twoTouches objectAtIndex:0]; |
|
410 |
UITouch *second = [twoTouches objectAtIndex:1]; |
|
411 |
CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
|
412 |
const int pinchDelta = 40; |
|
413 |
||
414 |
if (0 != initialDistanceForPinching) { |
|
415 |
if (currentDistanceOfPinching - initialDistanceForPinching > pinchDelta) { |
|
416 |
HW_zoomIn(); |
|
417 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
418 |
} |
|
419 |
else if (initialDistanceForPinching - currentDistanceOfPinching > pinchDelta) { |
|
420 |
HW_zoomOut(); |
|
421 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
422 |
} |
|
423 |
} else |
|
424 |
initialDistanceForPinching = currentDistanceOfPinching; |
|
425 |
||
426 |
break; |
|
427 |
default: |
|
428 |
break; |
|
429 |
} |
|
430 |
} |
|
431 |
||
432 |
||
433 |
@end |