src/video/uikit/SDL_uikitview.m
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Tue, 22 Jul 2008 23:05:40 +0000
branchgsoc2008_iphone
changeset 2403 e9a1eed243c9
parent 2353 07acabba25d9
child 2405 3d0b3a7d3bea
permissions -rw-r--r--
Added standard SDL header comments
     1 /*
     2  SDL - Simple DirectMedia Layer
     3  Copyright (C) 1997-2006 Sam Lantinga
     4  
     5  This library is free software; you can redistribute it and/or
     6  modify it under the terms of the GNU Lesser General Public
     7  License as published by the Free Software Foundation; either
     8  version 2.1 of the License, or (at your option) any later version.
     9  
    10  This library is distributed in the hope that it will be useful,
    11  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  Lesser General Public License for more details.
    14  
    15  You should have received a copy of the GNU Lesser General Public
    16  License along with this library; if not, write to the Free Software
    17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18  
    19  Sam Lantinga
    20  slouken@libsdl.org
    21  */
    22 #import "SDL_uikitview.h"
    23 
    24 @implementation SDL_uikitview
    25 
    26 - (void)dealloc {
    27 	[super dealloc];
    28 }
    29 
    30 - (id)initWithFrame:(CGRect)frame {
    31 
    32 	self = [super initWithFrame: frame];
    33 	
    34 	int i;
    35 	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
    36 		mice[i].driverdata = NULL;
    37 		SDL_AddMouse(&(mice[i]), i);
    38 	}
    39 	self.multipleTouchEnabled = YES;
    40 		
    41 	return self;
    42 
    43 }
    44 
    45 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    46 
    47 	NSEnumerator *enumerator = [touches objectEnumerator];
    48 	UITouch *touch=(UITouch*)[enumerator nextObject];
    49 	
    50 	// associate touches with mice, so long as we have slots
    51 	int i;
    52 	int found = 0;
    53 	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
    54 	
    55 		// check if this mouse is already tracking a touch
    56 		if (mice[i].driverdata != NULL) {
    57 			continue;
    58 		}
    59 		
    60 		found = 1;
    61 		
    62 		int oldMouse = SDL_SelectMouse(-1);
    63 		SDL_SelectMouse(i);
    64 		CGPoint locationInView = [touch locationInView: self];
    65 		mice[i].driverdata = [touch retain];
    66 		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
    67 		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
    68 		SDL_GetRelativeMouseState(NULL, NULL);
    69 		touch = (UITouch*)[enumerator nextObject]; 
    70 		
    71 		SDL_SelectMouse(oldMouse);
    72 		
    73 	}	
    74 }
    75 
    76 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    77 	
    78 	NSEnumerator *enumerator = [touches objectEnumerator];
    79 	UITouch *touch=nil;
    80 	
    81 	while(touch = (UITouch *)[enumerator nextObject]) {
    82 		int i, found = NO;
    83 		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
    84 			if (mice[i].driverdata == touch) {
    85 				[(UITouch*)(mice[i].driverdata) release];
    86 				mice[i].driverdata = NULL;
    87 				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
    88 
    89 				found = YES;
    90 			}
    91 		}
    92 	}
    93 }
    94 
    95 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    96 	/*
    97 		this can happen if the user puts more than 5 touches on the screen
    98 		at once, or perhaps in other circumstances.  Usually all active
    99 		touches are canceled.
   100 	*/
   101 	[self touchesEnded: touches withEvent: event];
   102 }
   103 
   104 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
   105 	
   106 	NSEnumerator *enumerator = [touches objectEnumerator];
   107 	UITouch *touch=nil;
   108 	
   109 	while(touch = (UITouch *)[enumerator nextObject]) {
   110 		int i, found = NO;
   111 		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
   112 			if (mice[i].driverdata == touch) {
   113 				CGPoint locationInView = [touch locationInView: self];
   114 				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
   115 				found = YES;
   116 			}
   117 		}
   118 	}
   119 }
   120 
   121 @end