cocoaTouch/SDLOverrides/SDL_uikitview.m
changeset 3006 da6023c2745b
parent 2805 36a8cebb91e8
equal deleted inserted replaced
3005:1ff835e9ce32 3006:da6023c2745b
    39 -(void) dealloc {
    39 -(void) dealloc {
    40 #if SDL_IPHONE_KEYBOARD
    40 #if SDL_IPHONE_KEYBOARD
    41 	SDL_DelKeyboard(0);
    41 	SDL_DelKeyboard(0);
    42 	[textField release];
    42 	[textField release];
    43 #endif
    43 #endif
    44 	if (menuView) [menuView release];
       
    45 	//[menuButton release];
       
    46 	//[attackButton release];
       
    47 	[super dealloc];
    44 	[super dealloc];
    48 }
    45 }
    49 
    46 
    50 - (id)initWithFrame:(CGRect)frame {
    47 -(id) initWithFrame:(CGRect)frame {
    51 	// the addTarget parameter for the buttons below is set like that because
       
    52 	// this object is inherited by SDL_openglesview.m which is the one allocated by SDL.
       
    53 	// We select this class with [self superclass] and call the selectors with "+" because
       
    54 	// they are superclass methods 
       
    55 	self = [super initWithFrame:frame];
    48 	self = [super initWithFrame:frame];
    56 	
    49 	
    57 #if SDL_IPHONE_KEYBOARD
    50 #if SDL_IPHONE_KEYBOARD
    58 	[self initializeKeyboard];
    51 	[self initializeKeyboard];
    59 #endif	
    52 #endif	
    66 	}
    59 	}
    67 
    60 
    68 	self.multipleTouchEnabled = YES;
    61 	self.multipleTouchEnabled = YES;
    69 	self.exclusiveTouch = YES;
    62 	self.exclusiveTouch = YES;
    70 
    63 
    71 	
       
    72 	/*
       
    73 	// custom code
       
    74 	// the coordinate system is still like in Portrait even though everything is rotated
       
    75 	attackButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 480, 260, 50)];
       
    76 	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
       
    77 	[attackButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateHighlighted];
       
    78 	[attackButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchDown];
       
    79 	[attackButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
       
    80 	//[self addSubview:attackButton];
       
    81 
       
    82 	menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 480, 30, 50)];
       
    83 	[menuButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
       
    84 	[menuButton addTarget:[self superclass] action:@selector(attackButtonPressed) forControlEvents:UIControlEventTouchUpInside];
       
    85 	[menuButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
       
    86 	//[self addSubview:menuButton];
       
    87 
       
    88 	UIButton *walkLeftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
       
    89 	[walkLeftButton addTarget:[self superclass] action:@selector(walkingLeft) forControlEvents:UIControlEventTouchDown];
       
    90 	[walkLeftButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
       
    91 	[self insertSubview:walkLeftButton atIndex:0];
       
    92 	[walkLeftButton release];
       
    93 	
       
    94 	UIButton *walkRightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 380, 320, 100)];
       
    95 	[walkRightButton addTarget:[self superclass] action:@selector(walkingRight) forControlEvents:UIControlEventTouchDown];
       
    96 	[walkRightButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
       
    97 	[self insertSubview:walkRightButton atIndex:0];
       
    98 	[walkRightButton release];
       
    99 
       
   100 	UIButton *moveDownButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 70, 280)];
       
   101 	[moveDownButton addTarget:[self superclass] action:@selector(movingDown) forControlEvents:UIControlEventTouchDown];
       
   102 	[moveDownButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
       
   103 	[self insertSubview:moveDownButton atIndex:0];
       
   104 	[moveDownButton release];
       
   105 	
       
   106 	UIButton *moveUpButton = [[UIButton alloc] initWithFrame:CGRectMake(250, 100, 70, 280)];
       
   107 	[moveUpButton addTarget:[self superclass] action:@selector(movingUp) forControlEvents:UIControlEventTouchDown];
       
   108 	[moveUpButton addTarget:[self superclass] action:@selector(buttonsReleased) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
       
   109 	[self insertSubview:moveUpButton atIndex:0];
       
   110 	[moveUpButton release];
       
   111 	
       
   112 	//dummy button used to test chat
       
   113 	UIButton *chatButton = [[UIButton alloc] initWithFrame:CGRectMake(70, 100, 250, 280)];
       
   114 	[chatButton addTarget:[self superclass] action:@selector(chatBegin) forControlEvents:UIControlEventTouchDown];
       
   115 	[self insertSubview:chatButton atIndex:0];
       
   116 	[chatButton release];
       
   117 	*/
       
   118 
       
   119 //(0,0) is the lower left corner
    64 //(0,0) is the lower left corner
   120 //x:[0-320]
    65 //x:[0-320]
   121 //y:[0-480]
    66 //y:[0-480]
   122 	UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(256, 416, 64, 64)];
    67     
   123 	[menuButton addTarget:[self superclass] action:@selector(showMenu) forControlEvents:UIControlEventTouchDown];
    68     // TO BE MOVED somewhere
   124 	[menuButton setBackgroundImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
    69     /*
   125 	[self insertSubview:menuButton atIndex:0];
    70 	UIButton *menuCorner = [[UIButton alloc] initWithFrame:CGRectMake(256, 416, 64, 64)];
   126 	[menuButton release];
    71 	[menuCorner addTarget:[self superclass] action:@selector(showMenu) forControlEvents:UIControlEventTouchDown];
       
    72 	[menuCorner setBackgroundImage:[UIImage imageNamed:@"menuCorner.png"] forState:UIControlStateNormal];
       
    73 	[self insertSubview:menuCorner atIndex:0];
       
    74 	[menuCorner release];
   127 	
    75 	
   128 	menuView = [[UIView alloc] initWithFrame:CGRectMake(320, 480, 150, 100)];
    76 	menuView = [[UIView alloc] initWithFrame:CGRectMake(320, 480, 150, 100)];
   129 	menuView.backgroundColor = [UIColor lightGrayColor];
    77 	menuView.backgroundColor = [UIColor lightGrayColor];
   130 	[self insertSubview:menuView atIndex:1];
    78 	[self insertSubview:menuView atIndex:1];
   131 
    79     */
   132 	return self;
    80 	return self;
   133 }
    81 }
   134 
    82 
       
    83 /*
       
    84  TO BE MOVED IN overlayViewController ASAP
   135 +(void) showMenu {
    85 +(void) showMenu {
   136 	HW_pause();
    86 	HW_pause();
   137 	
    87 	
   138 	[UIView beginAnimations:@"show menu" context:NULL];
    88 	[UIView beginAnimations:@"show menu" context:NULL];
   139 	[UIView setAnimationDuration:1];
    89 	[UIView setAnimationDuration:1];
   152 	[UIView commitAnimations];
   102 	[UIView commitAnimations];
   153 	
   103 	
   154 	HW_pause();
   104 	HW_pause();
   155 }
   105 }
   156 
   106 
   157 
   107 */
   158 
   108 
   159 #pragma mark -
   109 #pragma mark -
   160 #pragma mark Exported functions for FreePascal
   110 #pragma mark Exported functions for FreePascal
   161 const char* IPH_getDocumentsPath() {
   111 const char* IPH_getDocumentsPath() {
   162 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   112 	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   164 	return [documentsDirectory UTF8String];
   114 	return [documentsDirectory UTF8String];
   165 }
   115 }
   166 
   116 
   167 void IPH_showControls (void) {
   117 void IPH_showControls (void) {
   168 	NSLog(@"Showing controls");
   118 	NSLog(@"Showing controls");
   169 	/*
       
   170 	[UIView beginAnimations:nil context:NULL];
       
   171 	[UIView setAnimationDuration:0.5];
       
   172 	attackButton.frame = CGRectMake(30, 430, 260, 50);
       
   173 	menuButton.frame = CGRectMake(0, 430, 30, 50);
       
   174 	[UIView commitAnimations];
       
   175 	 */
       
   176 }
       
   177 
       
   178 #pragma mark -
       
   179 #pragma mark Superclass methods to call Pascal code
       
   180 +(void) attackButtonPressed {
       
   181 	HW_shoot();
       
   182 }
       
   183 
       
   184 +(void) buttonsReleased {
       
   185 	HW_allKeysUp();
       
   186 }
       
   187 
       
   188 +(void) walkingLeft {
       
   189 	HW_walkLeft();
       
   190 }
       
   191 
       
   192 +(void) walkingRight {
       
   193 	HW_walkRight();
       
   194 }
       
   195 
       
   196 +(void) movingUp {
       
   197 	HW_aimUp();
       
   198 }
       
   199 
       
   200 +(void) movingDown {
       
   201 	HW_aimDown();
       
   202 }
       
   203 
       
   204 +(void) chatBegin {
       
   205 	//TODO: implement a UITextScroll and put received chat lines in there
       
   206 }
       
   207 
       
   208 +(void) pauseGame {
       
   209 	HW_pause();
       
   210 }
   119 }
   211 
   120 
   212 #pragma mark -
   121 #pragma mark -
   213 #pragma mark Custom SDL_UIView input handling
   122 #pragma mark Custom SDL_UIView input handling
   214 #define kMinimumPinchDelta	50
   123 #define kMinimumPinchDelta      50
   215 #define kMinimumGestureLength	10
   124 #define kMinimumGestureLength	10
   216 #define kMaximumVariance	3
   125 #define kMaximumVariance        3
   217 
   126 
   218 // we override default touch input to implement our own gestures
   127 // we override default touch input to implement our own gestures
   219 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   128 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   220 	NSArray *twoTouches;
   129 	NSArray *twoTouches;
   221 	UITouch *touch = [touches anyObject];
   130 	UITouch *touch = [touches anyObject];
   222 	
   131 	
   223 	switch ([touches count]) {
   132 	switch ([touches count]) {
   224 		case 1:
   133 		case 1:
   225 			gestureStartPoint = [touch locationInView:self];
   134 			gestureStartPoint = [touch locationInView:self];
   226 			initialDistanceForPinching = 0;
   135 			initialDistanceForPinching = 0;
   227 			switch ([touch tapCount]) {
   136 			switch ([touch tapCount]) {
   228 				case 1:
   137 				case 1:
   229 					
       
   230 					NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y );
   138 					NSLog(@"X:%d Y:%d", (int)gestureStartPoint.x, (int)gestureStartPoint.y );
   231 					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
   139 					SDL_WarpMouseInWindow([SDLUIKitDelegate sharedAppDelegate].window, 
   232 							      (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
   140 							      (int)gestureStartPoint.y, 320 - (int)gestureStartPoint.x);
   233 					HW_click();
   141 					HW_click();
   234 					break;
   142 					break;
   254 			break;
   162 			break;
   255 	}
   163 	}
   256 
   164 
   257 }
   165 }
   258 
   166 
   259 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   167 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
   260 	initialDistanceForPinching = 0;
   168 	initialDistanceForPinching = 0;
   261 	gestureStartPoint.x = 0;
   169 	gestureStartPoint.x = 0;
   262 	gestureStartPoint.y = 0;
   170 	gestureStartPoint.y = 0;
   263 	HW_allKeysUp();
   171 	HW_allKeysUp();
   264 }
   172 }
   265 
   173 
   266 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
   174 -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
   267 	// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances.
   175 	// this can happen if the user puts more than 5 touches on the screen at once, or perhaps in other circumstances.
   268 	// Usually (it seems) all active touches are canceled.
   176 	// Usually (it seems) all active touches are canceled.
   269 	[self touchesEnded:touches withEvent:event];
   177 	[self touchesEnded:touches withEvent:event];
   270 }
   178 }
   271 
   179 
   272 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   180 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   273 	NSArray *twoTouches;
   181 	NSArray *twoTouches;
   274 	CGPoint currentPosition;
   182 	CGPoint currentPosition;
   275 	UITouch *touch = [touches anyObject];
   183 	UITouch *touch = [touches anyObject];
   276 
   184 
   277 	switch ([touches count]) {
   185 	switch ([touches count]) {