cocoaTouch/overlayViewController.m
changeset 3116 97dc65a47b15
parent 3115 831bd0f7050d
child 3117 f3e363a9b7db
equal deleted inserted replaced
3115:831bd0f7050d 3116:97dc65a47b15
     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 "popupMenuViewController.h"
       
    15 
       
    16 @implementation overlayViewController
       
    17 @synthesize dimTimer, menuPopover;
       
    18 
       
    19 
       
    20 -(void) didReceiveMemoryWarning {
       
    21 	// Releases the view if it doesn't have a superview.
       
    22     [super didReceiveMemoryWarning];
       
    23 	
       
    24 	// Release any cached data, images, etc that aren't in use.
       
    25 }
       
    26 
       
    27 -(void) viewDidLoad {
       
    28     self.view.alpha = 0;
       
    29     
       
    30     // needed for rotation to work on os < 3.2
       
    31     self.view.center = CGPointMake(self.view.frame.size.height/2.0, self.view.frame.size.width/2.0);
       
    32     self.view.transform = CGAffineTransformRotate(self.view.transform, (M_PI/2.0));
       
    33 
       
    34     dimTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:6]
       
    35                                         interval:1000
       
    36                                           target:self
       
    37                                         selector:@selector(dimOverlay)
       
    38                                         userInfo:nil
       
    39                                          repeats:YES];
       
    40     
       
    41     // add timer too runloop, otherwise it doesn't work
       
    42     [[NSRunLoop currentRunLoop] addTimer:dimTimer forMode:NSDefaultRunLoopMode];
       
    43     // listen for dismissal of the popover (see below)x
       
    44     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissPopover) name:@"dismissPopover" object:nil];
       
    45     // present the overlay after 2 seconds
       
    46     [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showMenuAfterwards) userInfo:nil repeats:NO];
       
    47 }
       
    48 
       
    49 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
       
    50     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
       
    51 }
       
    52 
       
    53 -(void) viewDidUnload {
       
    54 	[dimTimer invalidate];
       
    55 }
       
    56 
       
    57 -(void) dealloc {
       
    58     [menuPopover release];
       
    59     // dimTimer is autoreleased
       
    60     [super dealloc];
       
    61 }
       
    62 
       
    63 // draws the controller overlay after the sdl window has taken control
       
    64 -(void) showMenuAfterwards {
       
    65     [[SDLUIKitDelegate sharedAppDelegate].uiwindow bringSubviewToFront:self.view];
       
    66 
       
    67 	[UIView beginAnimations:@"showing overlay" context:NULL];
       
    68 	[UIView setAnimationDuration:1];
       
    69 	self.view.alpha = 1;
       
    70 	[UIView commitAnimations];
       
    71 }
       
    72 
       
    73 // dim the overlay when there's no more input for a certain amount of time
       
    74 -(IBAction) buttonReleased:(id) sender {
       
    75 	HW_allKeysUp();
       
    76     [dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.7]];
       
    77 }
       
    78 
       
    79 // nice transition for dimming
       
    80 -(void) dimOverlay {
       
    81     [UIView beginAnimations:@"overlay dim" context:NULL];
       
    82    	[UIView setAnimationDuration:0.6];
       
    83     self.view.alpha = 0.2;
       
    84 	[UIView commitAnimations];
       
    85 }
       
    86 
       
    87 // set the overlay visible and put off the timer for enough time
       
    88 -(void) activateOverlay {
       
    89     self.view.alpha = 1;
       
    90     [dimTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1000]];
       
    91 }
       
    92 
       
    93 // issue certain action based on the tag of the button 
       
    94 -(IBAction) buttonPressed:(id) sender {
       
    95     [self activateOverlay];
       
    96     UIActionSheet *actionSheet;
       
    97     UIButton *theButton = (UIButton *)sender;
       
    98     
       
    99     switch (theButton.tag) {
       
   100         case 0:
       
   101             HW_walkLeft();
       
   102             break;
       
   103         case 1:
       
   104             HW_walkRight();
       
   105             break;
       
   106         case 2:
       
   107             HW_aimUp();
       
   108             break;
       
   109         case 3:
       
   110             HW_aimDown();
       
   111             break;
       
   112         case 4:
       
   113             HW_shoot();
       
   114             break;
       
   115         case 5:
       
   116             HW_jump();
       
   117             break;
       
   118         case 6:
       
   119             HW_backjump();
       
   120             break;
       
   121         case 7:
       
   122             HW_pause();
       
   123             break;
       
   124         case 8:
       
   125             HW_chat();
       
   126             break;
       
   127         case 9:
       
   128             actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"")
       
   129                                                       delegate:self
       
   130                                              cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"")
       
   131                                         destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"")
       
   132                                              otherButtonTitles:nil];
       
   133             [actionSheet showInView:self.view];
       
   134             [actionSheet release];
       
   135 
       
   136             HW_pause();
       
   137 	    break;
       
   138         case 10:
       
   139             HW_tab();
       
   140             break;
       
   141         default:
       
   142             NSLog(@"Nope");
       
   143             break;
       
   144     }
       
   145 }
       
   146 
       
   147 // present a further check before closing game
       
   148 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
       
   149 	if ([actionSheet cancelButtonIndex] != buttonIndex)
       
   150 	    HW_terminate(NO);
       
   151 	else
       
   152             HW_pause();		
       
   153 }
       
   154 
       
   155 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
       
   156 -(IBAction) showPopover{
       
   157     popupMenuViewController *popupMenu = [[popupMenuViewController alloc] initWithNibName:@"popupMenuViewController" bundle:nil];
       
   158     
       
   159     menuPopover = [[UIPopoverController alloc] initWithContentViewController:popupMenu];
       
   160     [menuPopover setPopoverContentSize:CGSizeMake(220, 170) animated:YES];
       
   161 
       
   162     /*UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       
   163     button.frame= CGRectMake(960, 0, 64, 64);
       
   164     button.titleLabel.text=@"UUUUUUUF";
       
   165     [self.view addSubview:button];*/
       
   166     
       
   167     [menuPopover presentPopoverFromRect:CGRectMake(960, 0, 220, 32) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
       
   168     //UIBarButtonItem *sender = [[useless items] objectAtIndex:1];
       
   169     //[self.popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
       
   170 }
       
   171 
       
   172 // because of the actionSheet, the popOver might not get dismissed, so we do it manually (through a NSNotification system, see above)
       
   173 -(void) dismissPopover {
       
   174     if (menuPopover.popoverVisible) 
       
   175         [menuPopover dismissPopoverAnimated:YES];
       
   176 }
       
   177 
       
   178 #pragma mark -
       
   179 #define kMinimumPinchDelta      50
       
   180 #define kMinimumGestureLength	10
       
   181 #define kMaximumVariance        3
       
   182 
       
   183 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
       
   184 	NSArray *twoTouches;
       
   185 	UITouch *touch = [touches anyObject];
       
   186 	
       
   187 	switch ([touches count]) {
       
   188 		case 1:
       
   189 			gestureStartPoint = [touch locationInView:self.view];
       
   190 			initialDistanceForPinching = 0;
       
   191 			switch ([touch tapCount]) {
       
   192 				case 1:
       
   193 					NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y );
       
   194 					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
       
   195 							      (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
       
   196 					HW_click();
       
   197 					break;
       
   198 				case 2:
       
   199 					HW_ammoMenu();
       
   200 					break;
       
   201 				default:
       
   202 					break;
       
   203 			}
       
   204 			break;
       
   205 		case 2:
       
   206 			if (2 == [touch tapCount]) {
       
   207 				HW_zoomReset();
       
   208 			}
       
   209 			
       
   210 			// pinching
       
   211 			twoTouches = [touches allObjects];
       
   212 			UITouch *first = [twoTouches objectAtIndex:0];
       
   213 			UITouch *second = [twoTouches objectAtIndex:1];
       
   214 			initialDistanceForPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
       
   215 			break;
       
   216 		default:
       
   217 			break;
       
   218 	}
       
   219 
       
   220 }
       
   221 
       
   222 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
       
   223 	initialDistanceForPinching = 0;
       
   224 	gestureStartPoint.x = 0;
       
   225 	gestureStartPoint.y = 0;
       
   226 	HW_allKeysUp();
       
   227 }
       
   228 
       
   229 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
       
   230 	// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances.
       
   231 	[self touchesEnded:touches withEvent:event];
       
   232 }
       
   233 
       
   234 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       
   235 	NSArray *twoTouches;
       
   236 	CGPoint currentPosition;
       
   237 	UITouch *touch = [touches anyObject];
       
   238 
       
   239 	switch ([touches count]) {
       
   240 		case 1:
       
   241 			currentPosition = [touch locationInView:self.view];
       
   242 			// panning
       
   243 			SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
       
   244 							(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
       
   245 			// remember that we have x and y inverted
       
   246 			/* temporarily disabling hog movements for camera panning testing
       
   247 			CGFloat vertDiff = gestureStartPoint.x - currentPosition.x;
       
   248 			CGFloat horizDiff = gestureStartPoint.y - currentPosition.y;
       
   249 			CGFloat deltaX = fabsf(vertDiff);
       
   250 			CGFloat deltaY = fabsf(horizDiff);
       
   251 			
       
   252 			if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
       
   253 				NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x);
       
   254 				if (horizDiff > 0) HW_walkLeft();
       
   255 				else HW_walkRight();
       
   256 			} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
       
   257 				NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y);
       
   258 				if (vertDiff < 0) HW_aimUp();
       
   259 				else HW_aimDown();
       
   260 			}
       
   261 			*/
       
   262 			break;
       
   263 		case 2:
       
   264 			twoTouches = [touches allObjects];
       
   265 			UITouch *first = [twoTouches objectAtIndex:0];
       
   266 			UITouch *second = [twoTouches objectAtIndex:1];
       
   267 			CGFloat currentDistanceOfPinching = distanceBetweenPoints([first locationInView:self.view], [second locationInView:self.view]);
       
   268 			
       
   269 			if (0 == initialDistanceForPinching) 
       
   270 				initialDistanceForPinching = currentDistanceOfPinching;
       
   271 
       
   272 			if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta)
       
   273 				HW_zoomOut();
       
   274 			else if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta)
       
   275 				HW_zoomIn();
       
   276 
       
   277 			currentDistanceOfPinching = initialDistanceForPinching;
       
   278 			break;
       
   279 		default:
       
   280 			break;
       
   281 	}
       
   282 }
       
   283 
       
   284 
       
   285 @end