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