cocoaTouch/SDLOverrides/SDL_uikitview.m
changeset 2725 89908847b155
parent 2724 601158aaa201
child 2734 fb9ad1587054
equal deleted inserted replaced
2724:601158aaa201 2725:89908847b155
    30 #import "SDL_uikitwindow.h"
    30 #import "SDL_uikitwindow.h"
    31 #endif
    31 #endif
    32 
    32 
    33 @implementation SDL_uikitview
    33 @implementation SDL_uikitview
    34 
    34 
    35 @synthesize initialDistance, gestureStartPoint;
       
    36 
       
    37 // they have to be global variables to allow showControls() to use them
    35 // they have to be global variables to allow showControls() to use them
    38 UIButton *attackButton, *menuButton;
    36 UIButton *attackButton, *menuButton;
    39 
    37 
    40 -(void) dealloc {
    38 -(void) dealloc {
    41 #if SDL_IPHONE_KEYBOARD
    39 #if SDL_IPHONE_KEYBOARD
    84 	return self;
    82 	return self;
    85 }
    83 }
    86 
    84 
    87 #pragma mark -
    85 #pragma mark -
    88 #pragma mark Exported functions for FreePascal
    86 #pragma mark Exported functions for FreePascal
    89 
       
    90 const char* IPH_getDocumentsPath() {
    87 const char* IPH_getDocumentsPath() {
    91 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    88 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    92 	NSString *documentsDirectory = [paths objectAtIndex: 0];
    89 	NSString *documentsDirectory = [paths objectAtIndex: 0];
    93 	return [documentsDirectory UTF8String];
    90 	return [documentsDirectory UTF8String];
    94 }
    91 }
    95 
    92 
    96 void IPH_showControls (void) {
    93 void IPH_showControls (void) {
    97 	NSLog(@"Showing controls");
    94 	NSLog(@"Showing controls");
    98 	[UIView beginAnimations:nil context:NULL];
    95 	/*[UIView beginAnimations:nil context:NULL];
    99 	[UIView setAnimationDuration:0.5];
    96 	[UIView setAnimationDuration:0.5];
   100 	attackButton.frame = CGRectMake(30, 430, 260, 50);
    97 	attackButton.frame = CGRectMake(30, 430, 260, 50);
   101 	menuButton.frame = CGRectMake(0, 430, 30, 50);
    98 	menuButton.frame = CGRectMake(0, 430, 30, 50);
   102 	[UIView commitAnimations];
    99 	[UIView commitAnimations];
   103 }
   100 */}
   104 
   101 
   105 #pragma mark -
   102 #pragma mark -
   106 #pragma mark Superclass methods
   103 #pragma mark Superclass methods
   107 
       
   108 +(void) attackButtonPressed {
   104 +(void) attackButtonPressed {
   109 	HW_shoot();
   105 	HW_shoot();
   110 }
   106 }
   111 
   107 
   112 +(void) attackButtonReleased {
   108 +(void) attackButtonReleased {
   113 	HW_allKeysUp();
   109 	HW_allKeysUp();
   114 }
   110 }
   115 
   111 
   116 #pragma mark -
   112 #pragma mark -
   117 #pragma mark Custom SDL_UIView input handling
   113 #pragma mark Custom SDL_UIView input handling
       
   114 #define kMinimumPinchDelta	30
       
   115 #define kMinimumGestureLength	10
       
   116 #define kMaximumVariance	3
   118 
   117 
   119 // we override default touch input to implement our own gestures
   118 // we override default touch input to implement our own gestures
   120 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   119 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   121 	/*NSEnumerator *enumerator = [touches objectEnumerator];
   120 	/*
       
   121 	NSEnumerator *enumerator = [touches objectEnumerator];
   122 	UITouch *touch =(UITouch*)[enumerator nextObject];
   122 	UITouch *touch =(UITouch*)[enumerator nextObject];
   123 	
   123 	
   124 	/* associate touches with mice, so long as we have slots 
   124 	// associate touches with mice, so long as we have slots 
   125 	int i;
   125 	int i;
   126 	int found = 0;
   126 	int found = 0;
   127 	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
   127 	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
   128 	
   128 	
   129 		/* check if this mouse is already tracking a touch 
   129 		// check if this mouse is already tracking a touch 
   130 		if (mice[i].driverdata != NULL) {
   130 		if (mice[i].driverdata != NULL) {
   131 			continue;
   131 			continue;
   132 		}
   132 		}
   133 		/*	
   133 			
   134 			mouse not associated with anything right now,
   134 		// mouse not associated with anything right now, associate the touch with this mouse
   135 			associate the touch with this mouse
       
   136 		
       
   137 		found = 1;
   135 		found = 1;
   138 		
   136 		
   139 		/* save old mouse so we can switch back 
   137 		// save old mouse so we can switch back 
   140 		int oldMouse = SDL_SelectMouse(-1);
   138 		int oldMouse = SDL_SelectMouse(-1);
   141 		
   139 		
   142 		/* select this slot's mouse 
   140 		// select this slot's mouse 
   143 		SDL_SelectMouse(i);
   141 		SDL_SelectMouse(i);
   144 		CGPoint locationInView = [touch locationInView: self];
   142 		CGPoint locationInView = [touch locationInView: self];
   145 		
   143 		
   146 		/* set driver data to touch object, we'll use touch object later 
   144 		CGFloat oldX = locationInView.x;
       
   145 		locationInView.x = locationInView.y;
       
   146 		locationInView.y = 320 - oldX;
       
   147 		
       
   148 		// set driver data to touch object, we'll use touch object later 
   147 		mice[i].driverdata = [touch retain];
   149 		mice[i].driverdata = [touch retain];
   148 		
   150 		
   149 		/* send moved event 
   151 		// send moved event 
   150 		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
   152 		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
   151 		
   153 		
   152 		/* send mouse down event 
   154 		// send mouse down event 
   153 		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
   155 		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
   154 		
   156 		
   155 		/* re-calibrate relative mouse motion 
   157 		// re-calibrate relative mouse motion 
   156 		SDL_GetRelativeMouseState(i, NULL, NULL);
   158 		SDL_GetRelativeMouseState(i, NULL, NULL);
   157 		
   159 		
   158 		/* grab next touch 
   160 		// grab next touch 
   159 		touch = (UITouch*)[enumerator nextObject]; 
   161 		touch = (UITouch*)[enumerator nextObject]; 
   160 		
   162 		
   161 		/* switch back to our old mouse 
   163 		// switch back to our old mouse 
   162 		SDL_SelectMouse(oldMouse);
   164 		SDL_SelectMouse(oldMouse);
   163 		
   165 		
   164 	}	*/
   166 	}
       
   167 	*/
   165 	
   168 	
   166 	UITouch *touch = [touches anyObject];
   169 	UITouch *touch = [touches anyObject];
   167 	gestureStartPoint = [touch locationInView:self];
   170 	
   168 
   171 	switch ([touches count]) {
   169 	// one tap - single click
   172 		case 1:
   170 	if (1 == [touch tapCount] ) {
   173 			gestureStartPoint = [touch locationInView:self];
   171 		CGFloat oldX = gestureStartPoint.x;
   174 			initialDistanceForPinching = 0;
   172 		gestureStartPoint.x = gestureStartPoint.y;
   175 			switch ([touch tapCount]) {
   173 		gestureStartPoint.y = 320 - oldX;
   176 				case 1:
   174 
   177 					NSLog(@"x:%d y:%d",(int)gestureStartPoint.x,(int)gestureStartPoint.y);
   175 		SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, gestureStartPoint.x, gestureStartPoint.y);
   178 					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, (int)gestureStartPoint.y - 250, (int)gestureStartPoint.x);
   176 		HW_click();
   179 					HW_click();
   177 	}
   180 					break;
   178 	
   181 				case 2:
   179 	// two taps - right click
   182 					HW_ammoMenu();
   180 	if (2 == [touch tapCount] ) {
   183 					break;
   181 		HW_ammoMenu();
   184 				default:
   182 	}
   185 					break;
   183 	
   186 			}
   184 	// two taps with two fingers - middle click
   187 			break;
   185 	if (2 == [touch tapCount] && 2 == [touches count]) {
   188 		case 2:
   186 		HW_zoomReset();
   189 			if (2 == [touch tapCount]) {
   187 	}
   190 				HW_zoomReset();
   188 	
   191 			}
   189 	// two fingers - begin pinching
   192 			
   190 	if (2 == [touches count]) {
   193 			// pinching
   191 		NSArray *twoTouches = [touches allObjects];
   194 			NSArray *twoTouches = [touches allObjects];
   192 		UITouch *first = [twoTouches objectAtIndex:0];
   195 			UITouch *first = [twoTouches objectAtIndex:0];
   193 		UITouch *second = [twoTouches objectAtIndex:1];
   196 			UITouch *second = [twoTouches objectAtIndex:1];
   194 		initialDistance = distanceBetweenPoints([first locationInView:self], [second locationInView:self]);
   197 			initialDistanceForPinching = distanceBetweenPoints([first locationInView:self], [second locationInView:self]);
   195 	}
   198 			break;
       
   199 		default:
       
   200 			break;
       
   201 	}
       
   202 
   196 }
   203 }
   197 
   204 
   198 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   205 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   199 	initialDistance = 0;
   206 	initialDistanceForPinching = 0;
   200 	
   207 	
   201 	HW_allKeysUp();
   208 	HW_allKeysUp();
   202 	/*NSEnumerator *enumerator = [touches objectEnumerator];
   209 	/*
       
   210 	NSEnumerator *enumerator = [touches objectEnumerator];
   203 	UITouch *touch=nil;
   211 	UITouch *touch=nil;
   204 	
   212 	
   205 	while(touch = (UITouch *)[enumerator nextObject]) {
   213 	while(touch = (UITouch *)[enumerator nextObject]) {
   206 		/* search for the mouse slot associated with this touch 
   214 		// search for the mouse slot associated with this touch 
   207 		int i, found = NO;
   215 		int i, found = NO;
   208 		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
   216 		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
   209 			if (mice[i].driverdata == touch) {
   217 			if (mice[i].driverdata == touch) {
   210 				/* found the mouse associate with the touch 
   218 				// found the mouse associate with the touch 
   211 				[(UITouch*)(mice[i].driverdata) release];
   219 				[(UITouch*)(mice[i].driverdata) release];
   212 				mice[i].driverdata = NULL;
   220 				mice[i].driverdata = NULL;
   213 				/* send mouse up 
   221 				// send mouse up 
   214 				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
   222 				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
   215 				/* discontinue search for this touch 
   223 				// discontinue search for this touch 
   216 				found = YES;
   224 				found = YES;
   217 			}
   225 			}
   218 		}
   226 		}
   219 	}*/
   227 	}
       
   228 	*/
   220 }
   229 }
   221 
   230 
   222 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
   231 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
       
   232 	// this can happen if the user puts more than 5 touches on the screen
       
   233 	// at once, or perhaps in other circumstances.  Usually (it seems)
       
   234 	// all active touches are canceled.
       
   235 	[self touchesEnded:touches withEvent:event];
       
   236 }
       
   237 
       
   238 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       
   239 	NSArray *twoTouches;
       
   240 	CGPoint currentPosition;
       
   241 	UITouch *touch = [touches anyObject];
       
   242 
       
   243 	switch ([touches count]) {
       
   244 		case 1:
       
   245 			currentPosition = [touch locationInView:self];
       
   246 			
       
   247 			// remember that we have x and y inverted
       
   248 			CGFloat vertDiff = gestureStartPoint.x - currentPosition.x;
       
   249 			CGFloat horizDiff = gestureStartPoint.y - currentPosition.y;
       
   250 			CGFloat deltaX = fabsf(vertDiff);
       
   251 			CGFloat deltaY = fabsf(horizDiff);
       
   252 			
       
   253 			if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
       
   254 				NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x);
       
   255 				if (horizDiff > 0) HW_walkLeft();
       
   256 				else HW_walkRight();
       
   257 			} else if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance){
       
   258 				NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y);
       
   259 				if (vertDiff < 0) HW_aimUp();
       
   260 				else HW_aimDown();
       
   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], [second locationInView:self]);
       
   268 			
       
   269 			if (0 == initialDistanceForPinching) 
       
   270 				initialDistanceForPinching = currentDistanceOfPinching;
       
   271 
       
   272 			if (currentDistanceOfPinching < initialDistanceForPinching + kMinimumPinchDelta) {
       
   273 				NSLog(@"Outward pinch detected");
       
   274 				HW_zoomOut();
       
   275 			}
       
   276 			if (currentDistanceOfPinching > initialDistanceForPinching + kMinimumPinchDelta){
       
   277 				NSLog(@"Inward pinch detected");
       
   278 				HW_zoomIn();
       
   279 			}
       
   280 			
       
   281 			currentDistanceOfPinching = initialDistanceForPinching;
       
   282 			break;
       
   283 		default:
       
   284 			break;
       
   285 	}
       
   286 	
   223 	/*
   287 	/*
   224 		this can happen if the user puts more than 5 touches on the screen
   288 	NSEnumerator *enumerator = [touches objectEnumerator];
   225 		at once, or perhaps in other circumstances.  Usually (it seems)
       
   226 		all active touches are canceled.
       
   227 	*/
       
   228 	[self touchesEnded: touches withEvent: event];
       
   229 }
       
   230 
       
   231 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
       
   232 	UITouch *touch = [touches anyObject];
       
   233 	CGPoint currentPosition = [touch locationInView:self];
       
   234 	
       
   235 	// remember that we have x and y inverted
       
   236 	CGFloat Xdiff = gestureStartPoint.y - currentPosition.y;
       
   237 	CGFloat Ydiff = gestureStartPoint.x - currentPosition.x;
       
   238 	CGFloat deltaX = fabsf(Xdiff);
       
   239 	CGFloat deltaY = fabsf(Ydiff);
       
   240     
       
   241 	if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
       
   242 		NSLog(@"Horizontal swipe detected, begX:%f curX:%f", gestureStartPoint.x, currentPosition.x);
       
   243 		if (Xdiff > 0) HW_walkLeft();
       
   244 		else HW_walkRight();
       
   245 	} else if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){
       
   246 		NSLog(@"Vertical swipe detected, begY:%f curY:%f", gestureStartPoint.y, currentPosition.y);
       
   247 		if (Ydiff > 0) HW_aimUp();
       
   248 		else HW_aimDown();
       
   249 	}
       
   250 	
       
   251 	// end pinch detection
       
   252 	if (2 == [touches count]) {
       
   253 		NSArray *twoTouches = [touches allObjects];
       
   254 		UITouch *first = [twoTouches objectAtIndex:0];
       
   255 		UITouch *second = [twoTouches objectAtIndex:1];
       
   256 		CGFloat currentDistance = distanceBetweenPoints([first locationInView:self], [second locationInView:self]);
       
   257 		
       
   258 		if (0 == initialDistance) 
       
   259 			initialDistance = currentDistance;
       
   260 		else if (currentDistance - initialDistance > kMinimumPinchDelta) {
       
   261 			NSLog(@"Outward pinch detected");
       
   262 			HW_zoomOut();
       
   263 		}
       
   264 		else if (initialDistance - currentDistance > kMinimumPinchDelta) {
       
   265 			NSLog(@"Inward pinch detected");
       
   266 			HW_zoomIn();
       
   267 		}
       
   268 	}
       
   269 	
       
   270 	/*NSEnumerator *enumerator = [touches objectEnumerator];
       
   271 	 UITouch *touch=nil;while(touch = (UITouch *)[enumerator nextObject]) {
   289 	 UITouch *touch=nil;while(touch = (UITouch *)[enumerator nextObject]) {
   272 		// try to find the mouse associated with this touch 
   290 		// try to find the mouse associated with this touch 
   273 		int i, found = NO;
   291 		int i, found = NO;
   274 		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
   292 		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
   275 			if (mice[i].driverdata == touch) {
   293 			if (mice[i].driverdata == touch) {
   279 				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
   297 				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
   280 				// discontinue search 
   298 				// discontinue search 
   281 				found = YES;
   299 				found = YES;
   282 			}
   300 			}
   283 		}
   301 		}
   284 	}*/
   302 	}
       
   303 	*/
   285 }
   304 }
   286 
   305 
   287 #pragma mark -
   306 #pragma mark -
   288 #pragma mark default routines
   307 #pragma mark default routines
   289 /*
   308 /*