21 #define doNotDim() [dimTimer setFireDate:HIDING_TIME_NEVER] |
21 #define doNotDim() [dimTimer setFireDate:HIDING_TIME_NEVER] |
22 |
22 |
23 #define CONFIRMATION_TAG 5959 |
23 #define CONFIRMATION_TAG 5959 |
24 #define GRENADE_TAG 9595 |
24 #define GRENADE_TAG 9595 |
25 #define ANIMATION_DURATION 0.25 |
25 #define ANIMATION_DURATION 0.25 |
26 #define removeConfirmationInput() [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview]; |
26 #define removeConfirmationInput() [[self.view viewWithTag:CONFIRMATION_TAG] removeFromSuperview]; |
27 |
27 |
28 @implementation OverlayViewController |
28 @implementation OverlayViewController |
29 @synthesize popoverController, popupMenu; |
29 @synthesize popoverController, popupMenu; |
30 |
30 |
31 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
31 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation { |
32 return rotationManager(interfaceOrientation); |
32 return rotationManager(interfaceOrientation); |
33 } |
33 } |
34 |
34 |
35 -(void) didRotate:(NSNotification *)notification { |
35 -(void) didRotate:(NSNotification *)notification { |
36 UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; |
36 UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; |
37 CGRect rect = [[UIScreen mainScreen] bounds]; |
37 CGRect rect = [[UIScreen mainScreen] bounds]; |
38 CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height); |
38 CGRect usefulRect = CGRectMake(0, 0, rect.size.width, rect.size.height); |
39 UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG]; |
39 UIView *sdlView = [[[UIApplication sharedApplication] keyWindow] viewWithTag:SDL_VIEW_TAG]; |
40 |
40 |
41 [UIView beginAnimations:@"rotation" context:NULL]; |
41 [UIView beginAnimations:@"rotation" context:NULL]; |
42 [UIView setAnimationDuration:0.8f]; |
42 [UIView setAnimationDuration:0.8f]; |
43 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; |
43 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; |
44 switch (orientation) { |
44 switch (orientation) { |
45 case UIDeviceOrientationLandscapeLeft: |
45 case UIDeviceOrientationLandscapeLeft: |
100 default: |
100 default: |
101 break; |
101 break; |
102 } |
102 } |
103 CGRect rect = [[UIScreen mainScreen] bounds]; |
103 CGRect rect = [[UIScreen mainScreen] bounds]; |
104 self.view.frame = CGRectMake(0, 0, rect.size.width, rect.size.height); |
104 self.view.frame = CGRectMake(0, 0, rect.size.width, rect.size.height); |
105 |
105 |
106 dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6] |
106 dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6] |
107 interval:1000 |
107 interval:1000 |
108 target:self |
108 target:self |
109 selector:@selector(dimOverlay) |
109 selector:@selector(dimOverlay) |
110 userInfo:nil |
110 userInfo:nil |
111 repeats:YES]; |
111 repeats:YES]; |
112 |
112 |
113 // add timer too runloop, otherwise it doesn't work |
113 // add timer too runloop, otherwise it doesn't work |
114 [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode]; |
114 [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode]; |
115 |
115 |
116 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; |
116 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; |
117 [[NSNotificationCenter defaultCenter] addObserver:self |
117 [[NSNotificationCenter defaultCenter] addObserver:self |
118 selector:@selector(didRotate:) |
118 selector:@selector(didRotate:) |
119 name:UIDeviceOrientationDidChangeNotification |
119 name:UIDeviceOrientationDidChangeNotification |
120 object:nil]; |
120 object:nil]; |
121 |
121 |
122 [UIView beginAnimations:@"showing overlay" context:NULL]; |
122 [UIView beginAnimations:@"showing overlay" context:NULL]; |
123 [UIView setAnimationDuration:1]; |
123 [UIView setAnimationDuration:1]; |
124 self.view.alpha = 1; |
124 self.view.alpha = 1; |
125 [UIView commitAnimations]; |
125 [UIView commitAnimations]; |
126 |
126 |
127 // find the sdl window we're on |
127 // find the sdl window we're on |
128 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
128 SDL_VideoDevice *_this = SDL_GetVideoDevice(); |
129 SDL_VideoDisplay *display = &_this->displays[0]; |
129 SDL_VideoDisplay *display = &_this->displays[0]; |
130 sdlwindow = display->windows; |
130 sdlwindow = display->windows; |
131 } |
131 } |
265 // present a further check before closing game |
265 // present a further check before closing game |
266 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
266 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex { |
267 if ([actionSheet cancelButtonIndex] != buttonIndex) |
267 if ([actionSheet cancelButtonIndex] != buttonIndex) |
268 HW_terminate(NO); |
268 HW_terminate(NO); |
269 else |
269 else |
270 HW_pause(); |
270 HW_pause(); |
271 } |
271 } |
272 |
272 |
273 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize |
273 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize |
274 // on iphone instead just use the tableViewController directly (and implement manually all animations) |
274 // on iphone instead just use the tableViewController directly (and implement manually all animations) |
275 -(IBAction) showPopover{ |
275 -(IBAction) showPopover{ |
276 CGRect screen = [[UIScreen mainScreen] bounds]; |
276 CGRect screen = [[UIScreen mainScreen] bounds]; |
277 isPopoverVisible = YES; |
277 isPopoverVisible = YES; |
278 |
278 |
279 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
279 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
280 if (popupMenu == nil) |
280 if (popupMenu == nil) |
281 popupMenu = [[InGameMenuViewController alloc] initWithStyle:UITableViewStylePlain]; |
281 popupMenu = [[InGameMenuViewController alloc] initWithStyle:UITableViewStylePlain]; |
282 if (popoverController == nil) { |
282 if (popoverController == nil) { |
283 popoverController = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
283 popoverController = [[UIPopoverController alloc] initWithContentViewController:popupMenu]; |
284 [popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES]; |
284 [popoverController setPopoverContentSize:CGSizeMake(220, 170) animated:YES]; |
285 [popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]]; |
285 [popoverController setPassthroughViews:[NSArray arrayWithObject:self.view]]; |
319 #pragma mark - |
319 #pragma mark - |
320 #pragma mark Custom touch event handling |
320 #pragma mark Custom touch event handling |
321 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
321 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
322 NSSet *allTouches = [event allTouches]; |
322 NSSet *allTouches = [event allTouches]; |
323 UITouch *first, *second; |
323 UITouch *first, *second; |
324 |
324 |
325 // hide in-game menu |
325 // hide in-game menu |
326 if (isPopoverVisible) |
326 if (isPopoverVisible) |
327 [self dismissPopover]; |
327 [self dismissPopover]; |
328 |
328 |
329 // reset default dimming |
329 // reset default dimming |
330 doDim(); |
330 doDim(); |
331 |
331 |
332 HW_setPianoSound([allTouches count]); |
332 HW_setPianoSound([allTouches count]); |
333 |
333 |
334 switch ([allTouches count]) { |
334 switch ([allTouches count]) { |
335 case 1: |
335 case 1: |
336 removeConfirmationInput(); |
336 removeConfirmationInput(); |
337 startingPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view]; |
337 startingPoint = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view]; |
338 if (2 == [[[allTouches allObjects] objectAtIndex:0] tapCount]) |
338 if (2 == [[[allTouches allObjects] objectAtIndex:0] tapCount]) |
339 HW_zoomReset(); |
339 HW_zoomReset(); |
340 break; |
340 break; |
341 case 2: |
341 case 2: |
342 // pinching |
342 // pinching |
343 first = [[allTouches allObjects] objectAtIndex:0]; |
343 first = [[allTouches allObjects] objectAtIndex:0]; |
344 second = [[allTouches allObjects] objectAtIndex:1]; |
344 second = [[allTouches allObjects] objectAtIndex:1]; |
345 initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
345 initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]); |
346 break; |
346 break; |
353 |
353 |
354 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
354 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
355 CGRect screen = [[UIScreen mainScreen] bounds]; |
355 CGRect screen = [[UIScreen mainScreen] bounds]; |
356 NSSet *allTouches = [event allTouches]; |
356 NSSet *allTouches = [event allTouches]; |
357 CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view]; |
357 CGPoint currentPosition = [[[allTouches allObjects] objectAtIndex:0] locationInView:self.view]; |
358 |
358 |
359 switch ([allTouches count]) { |
359 switch ([allTouches count]) { |
360 case 1: |
360 case 1: |
361 // if we're in the menu we just click in the point |
361 // if we're in the menu we just click in the point |
362 if (HW_isAmmoOpen()) { |
362 if (HW_isAmmoOpen()) { |
363 HW_setCursor(HWXZ(currentPosition.x), HWYZ(currentPosition.y)); |
363 HW_setCursor(HWXZ(currentPosition.x), HWYZ(currentPosition.y)); |
364 // this click doesn't need any wrapping because the ammoMenu already limits the cursor |
364 // this click doesn't need any wrapping because the ammoMenu already limits the cursor |
365 HW_click(); |
365 HW_click(); |
366 } else |
366 } else |
367 // if weapon requires a further click, ask for tapping again |
367 // if weapon requires a further click, ask for tapping again |
368 if (HW_isWeaponRequiringClick()) { |
368 if (HW_isWeaponRequiringClick()) { |
369 // here don't have to wrap thanks to isCursorVisible magic |
369 // here don't have to wrap thanks to isCursorVisible magic |
370 HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y)); |
370 HW_setCursor(HWX(currentPosition.x), HWY(currentPosition.y)); |
371 |
371 |
372 // draw the button at the last touched point (which is the current position) |
372 // draw the button at the last touched point (which is the current position) |
373 UIButton *tapAgain = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
373 UIButton *tapAgain = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
374 tapAgain.frame = CGRectMake(currentPosition.x - 75, currentPosition.y + 25, 150, 40); |
374 tapAgain.frame = CGRectMake(currentPosition.x - 75, currentPosition.y + 25, 150, 40); |
375 tapAgain.tag = CONFIRMATION_TAG; |
375 tapAgain.tag = CONFIRMATION_TAG; |
376 tapAgain.alpha = 0; |
376 tapAgain.alpha = 0; |
377 [tapAgain addTarget:self action:@selector(sendHWClick) forControlEvents:UIControlEventTouchUpInside]; |
377 [tapAgain addTarget:self action:@selector(sendHWClick) forControlEvents:UIControlEventTouchUpInside]; |
378 [tapAgain setTitle:NSLocalizedString(@"Tap to set!",@"from the overlay") forState:UIControlStateNormal]; |
378 [tapAgain setTitle:NSLocalizedString(@"Tap to set!",@"from the overlay") forState:UIControlStateNormal]; |
379 [self.view addSubview:tapAgain]; |
379 [self.view addSubview:tapAgain]; |
380 |
380 |
381 // animation ftw! |
381 // animation ftw! |
382 [UIView beginAnimations:@"inserting button" context:NULL]; |
382 [UIView beginAnimations:@"inserting button" context:NULL]; |
383 [UIView setAnimationDuration:ANIMATION_DURATION]; |
383 [UIView setAnimationDuration:ANIMATION_DURATION]; |
384 [self.view viewWithTag:CONFIRMATION_TAG].alpha = 1; |
384 [self.view viewWithTag:CONFIRMATION_TAG].alpha = 1; |
385 [UIView commitAnimations]; |
385 [UIView commitAnimations]; |
386 |
386 |
387 // keep the overlay active, or the button will fade |
387 // keep the overlay active, or the button will fade |
388 [self activateOverlay]; |
388 [self activateOverlay]; |
389 doNotDim(); |
389 doNotDim(); |
390 } else |
390 } else |
391 if (HW_isWeaponTimerable()) { |
391 if (HW_isWeaponTimerable()) { |
392 if (isSegmentVisible) { |
392 if (isSegmentVisible) { |
393 UISegmentedControl *grenadeTime = (UISegmentedControl *)[self.view viewWithTag:GRENADE_TAG]; |
393 UISegmentedControl *grenadeTime = (UISegmentedControl *)[self.view viewWithTag:GRENADE_TAG]; |
394 |
394 |
395 [UIView beginAnimations:@"removing segmented control" context:NULL]; |
395 [UIView beginAnimations:@"removing segmented control" context:NULL]; |
396 [UIView setAnimationDuration:ANIMATION_DURATION]; |
396 [UIView setAnimationDuration:ANIMATION_DURATION]; |
397 [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; |
397 [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; |
398 grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width, 250, 50); |
398 grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width, 250, 50); |
399 [UIView commitAnimations]; |
399 [UIView commitAnimations]; |
400 |
400 |
401 [grenadeTime performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:ANIMATION_DURATION]; |
401 [grenadeTime performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:ANIMATION_DURATION]; |
402 } else { |
402 } else { |
403 NSArray *items = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil]; |
403 NSArray *items = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil]; |
404 UISegmentedControl *grenadeTime = [[UISegmentedControl alloc] initWithItems:items]; |
404 UISegmentedControl *grenadeTime = [[UISegmentedControl alloc] initWithItems:items]; |
405 [items release]; |
405 [items release]; |
406 |
406 |
407 [grenadeTime addTarget:self action:@selector(setGrenadeTime:) forControlEvents:UIControlEventValueChanged]; |
407 [grenadeTime addTarget:self action:@selector(setGrenadeTime:) forControlEvents:UIControlEventValueChanged]; |
408 grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width, 250, 50); |
408 grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width, 250, 50); |
409 grenadeTime.selectedSegmentIndex = 2; |
409 grenadeTime.selectedSegmentIndex = 2; |
410 grenadeTime.tag = GRENADE_TAG; |
410 grenadeTime.tag = GRENADE_TAG; |
411 [self.view addSubview:grenadeTime]; |
411 [self.view addSubview:grenadeTime]; |
412 [grenadeTime release]; |
412 [grenadeTime release]; |
413 |
413 |
414 [UIView beginAnimations:@"inserting segmented control" context:NULL]; |
414 [UIView beginAnimations:@"inserting segmented control" context:NULL]; |
415 [UIView setAnimationDuration:ANIMATION_DURATION]; |
415 [UIView setAnimationDuration:ANIMATION_DURATION]; |
416 [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; |
416 [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; |
417 grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width - 100, 250, 50); |
417 grenadeTime.frame = CGRectMake(screen.size.height / 2 - 125, screen.size.width - 100, 250, 50); |
418 [UIView commitAnimations]; |
418 [UIView commitAnimations]; |
419 |
419 |
420 [self activateOverlay]; |
420 [self activateOverlay]; |
421 doNotDim(); |
421 doNotDim(); |
422 } |
422 } |
423 isSegmentVisible = !isSegmentVisible; |
423 isSegmentVisible = !isSegmentVisible; |
424 } else |
424 } else |
425 if (HW_isWeaponSwitch()) |
425 if (HW_isWeaponSwitch()) |
426 HW_tab(); |
426 HW_tab(); |
427 break; |
427 break; |