cocoaTouch/OverlayViewController.m
changeset 3305 91074496d5c9
parent 3117 f3e363a9b7db
child 3308 b6dcae4b6d2c
equal deleted inserted replaced
3304:8690a3aa93b5 3305:91074496d5c9
     9 #import "OverlayViewController.h"
     9 #import "OverlayViewController.h"
    10 #import "SDL_uikitappdelegate.h"
    10 #import "SDL_uikitappdelegate.h"
    11 #import "PascalImports.h"
    11 #import "PascalImports.h"
    12 #import "CGPointUtils.h"
    12 #import "CGPointUtils.h"
    13 #import "SDL_mouse.h"
    13 #import "SDL_mouse.h"
    14 #import "PopupMenuViewController.h"
    14 #import "PopoverMenuViewController.h"
    15 
    15 
    16 @implementation OverlayViewController
    16 @implementation OverlayViewController
    17 @synthesize dimTimer;
    17 @synthesize dimTimer, menuPopover;
    18 
    18 
    19 
    19 
    20 -(void) didReceiveMemoryWarning {
    20 -(void) didReceiveMemoryWarning {
    21 	// Releases the view if it doesn't have a superview.
    21 	// Releases the view if it doesn't have a superview.
    22     [super didReceiveMemoryWarning];
    22     [super didReceiveMemoryWarning];
    50     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    50     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
    51 }
    51 }
    52 
    52 
    53 -(void) viewDidUnload {
    53 -(void) viewDidUnload {
    54 	[dimTimer invalidate];
    54 	[dimTimer invalidate];
       
    55     self.dimTimer = nil;
       
    56     menuPopover = nil;
       
    57     [super viewDidUnload];
    55 }
    58 }
    56 
    59 
    57 -(void) dealloc {
    60 -(void) dealloc {
       
    61     [menuPopover release];
    58     // dimTimer is autoreleased
    62     // dimTimer is autoreleased
    59     [super dealloc];
    63     [super dealloc];
    60 }
    64 }
    61 
    65 
    62 // draws the controller overlay after the sdl window has taken control
    66 // draws the controller overlay after the sdl window has taken control
    90 }
    94 }
    91 
    95 
    92 // issue certain action based on the tag of the button 
    96 // issue certain action based on the tag of the button 
    93 -(IBAction) buttonPressed:(id) sender {
    97 -(IBAction) buttonPressed:(id) sender {
    94     [self activateOverlay];
    98     [self activateOverlay];
    95     UIActionSheet *actionSheet;
       
    96     UIButton *theButton = (UIButton *)sender;
    99     UIButton *theButton = (UIButton *)sender;
    97     
   100     
    98     switch (theButton.tag) {
   101     switch (theButton.tag) {
    99         case 0:
   102         case 0:
   100             HW_walkLeft();
   103             HW_walkLeft();
   116             break;
   119             break;
   117         case 6:
   120         case 6:
   118             HW_backjump();
   121             HW_backjump();
   119             break;
   122             break;
   120         case 7:
   123         case 7:
   121             HW_pause();
       
   122             break;
       
   123         case 8:
       
   124             HW_chat();
       
   125             break;
       
   126         case 9:
       
   127             actionSheet = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"Are you reeeeeally sure?", @"")
       
   128                                                       delegate:self
       
   129                                              cancelButtonTitle:NSLocalizedString(@"Well, maybe not...", @"")
       
   130                                         destructiveButtonTitle:NSLocalizedString(@"As sure as I can be!", @"")
       
   131                                              otherButtonTitles:nil];
       
   132             [actionSheet showInView:self.view];
       
   133             [actionSheet release];
       
   134 
       
   135             HW_pause();
       
   136 	    break;
       
   137         case 10:
       
   138             HW_tab();
   124             HW_tab();
   139             break;
   125             break;
   140         default:
   126         default:
   141             NSLog(@"Nope");
   127             NSLog(@"Nope");
   142             break;
   128             break;
   143     }
   129     }
   144 }
   130 }
   145 
   131 
       
   132 // present a further check before closing game
       
   133 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
       
   134 	if ([actionSheet cancelButtonIndex] != buttonIndex)
       
   135 	    HW_terminate(NO);
       
   136 	else
       
   137         HW_pause();		
       
   138 }
       
   139 
       
   140 // show up a popover containing a popupMenuViewController; we hook it with setPopoverContentSize
       
   141 -(IBAction) showPopover{
       
   142     PopoverMenuViewController *popupMenu = [[PopoverMenuViewController alloc] init];
       
   143     popoverVisible = YES;
       
   144     Class popoverController = NSClassFromString(@"UIPopoverController");
       
   145     if (popoverController) {
       
   146 #ifdef __IPHONE_3_2
       
   147         menuPopover = [[popoverController alloc] initWithContentViewController:popupMenu];
       
   148         [menuPopover setPopoverContentSize:CGSizeMake(220, 170) animated:YES];
       
   149         
       
   150         [menuPopover presentPopoverFromRect:CGRectMake(960, 0, 220, 32) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
       
   151 #endif
       
   152     } else {
       
   153         //iphone stuff
       
   154     }
       
   155 }
       
   156 
       
   157 // because of the actionSheet, the popover might not get dismissed, so we do it manually (through a NSNotification system, see above)
       
   158 // are we sure about this?
       
   159 -(void) dismissPopover {
       
   160     /*if (popoverVisible) 
       
   161         [menuPopover dismissPopoverAnimated:YES];
       
   162     popoverVisible = NO;*/
       
   163 }
       
   164 
   146 #pragma mark -
   165 #pragma mark -
       
   166 #pragma mark Custom touch event handling
       
   167 
   147 #define kMinimumPinchDelta      50
   168 #define kMinimumPinchDelta      50
   148 #define kMinimumGestureLength	10
   169 #define kMinimumGestureLength	10
   149 #define kMaximumVariance        3
   170 #define kMaximumVariance        3
   150 
   171 
   151 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   172 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   152 	NSArray *twoTouches;
   173 	NSArray *twoTouches;
   153 	UITouch *touch = [touches anyObject];
   174 	UITouch *touch = [touches anyObject];
   154 	
   175 	int width = [[UIScreen mainScreen] bounds].size.width;
       
   176     
   155 	switch ([touches count]) {
   177 	switch ([touches count]) {
   156 		case 1:
   178 		case 1:
   157 			gestureStartPoint = [touch locationInView:self.view];
   179 			gestureStartPoint = [touch locationInView:self.view];
   158 			initialDistanceForPinching = 0;
   180 			initialDistanceForPinching = 0;
   159 			switch ([touch tapCount]) {
   181 			switch ([touch tapCount]) {
   160 				case 1:
   182 				case 1:
   161 					NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y );
   183 					NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y );
   162 					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
   184 					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
   163 							      (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
   185 							      (int)gestureStartPoint.y, width - (int)gestureStartPoint.x);
   164 					HW_click();
   186 					HW_click();
   165 					break;
   187 					break;
   166 				case 2:
   188 				case 2:
   167 					HW_ammoMenu();
   189 					HW_ammoMenu();
   168 					break;
   190 					break;
   201 
   223 
   202 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   224 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   203 	NSArray *twoTouches;
   225 	NSArray *twoTouches;
   204 	CGPoint currentPosition;
   226 	CGPoint currentPosition;
   205 	UITouch *touch = [touches anyObject];
   227 	UITouch *touch = [touches anyObject];
       
   228 	int width = [[UIScreen mainScreen] bounds].size.width;
   206 
   229 
   207 	switch ([touches count]) {
   230 	switch ([touches count]) {
   208 		case 1:
   231 		case 1:
   209 			currentPosition = [touch locationInView:self.view];
   232 			currentPosition = [touch locationInView:self.view];
   210 			// panning
   233 			// panning
   211 			SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
   234 			SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
   212 							(int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
   235 							(int)gestureStartPoint.y, width - (int)gestureStartPoint.x);
   213 			// remember that we have x and y inverted
   236 			// remember that we have x and y inverted
   214 			/* temporarily disabling hog movements for camera panning testing
   237 			/* temporarily disabling hog movements for camera panning testing
   215 			CGFloat vertDiff = gestureStartPoint.x - currentPosition.x;
   238 			CGFloat vertDiff = gestureStartPoint.x - currentPosition.x;
   216 			CGFloat horizDiff = gestureStartPoint.y - currentPosition.y;
   239 			CGFloat horizDiff = gestureStartPoint.y - currentPosition.y;
   217 			CGFloat deltaX = fabsf(vertDiff);
   240 			CGFloat deltaX = fabsf(vertDiff);