src/video/uikit/SDL_uikitview.m
author Holmes Futrell <hfutrell@umail.ucsb.edu>
Thu, 17 Jul 2008 22:43:09 +0000
branchgsoc2008_iphone
changeset 2353 07acabba25d9
child 2403 e9a1eed243c9
permissions -rw-r--r--
SDL_uikitview is just a generic view class which SDL_uikitopenglview inherits from. The functionality found in this class relates to (right now) mouse/touch input support. The reason for putting it here is that if someone wanted to write a render driver for iPhone based around CoreGraphics rather than OpenGL ES, they could make their Core Graphics view inherit from this class as well.
hfutrell@2353
     1
//
hfutrell@2353
     2
//  SDL_uikitview.m
hfutrell@2353
     3
//  iPodSDL
hfutrell@2353
     4
//
hfutrell@2353
     5
//  Created by Holmes Futrell on 6/23/08.
hfutrell@2353
     6
//  Copyright 2008 __MyCompanyName__. All rights reserved.
hfutrell@2353
     7
//
hfutrell@2353
     8
hfutrell@2353
     9
#import "SDL_uikitview.h"
hfutrell@2353
    10
hfutrell@2353
    11
@implementation SDL_uikitview
hfutrell@2353
    12
hfutrell@2353
    13
- (void)dealloc {
hfutrell@2353
    14
	[super dealloc];
hfutrell@2353
    15
}
hfutrell@2353
    16
hfutrell@2353
    17
- (id)initWithFrame:(CGRect)frame {
hfutrell@2353
    18
hfutrell@2353
    19
	self = [super initWithFrame: frame];
hfutrell@2353
    20
	
hfutrell@2353
    21
	int i;
hfutrell@2353
    22
	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
hfutrell@2353
    23
		mice[i].driverdata = NULL;
hfutrell@2353
    24
		SDL_AddMouse(&(mice[i]), i);
hfutrell@2353
    25
	}
hfutrell@2353
    26
	self.multipleTouchEnabled = YES;
hfutrell@2353
    27
		
hfutrell@2353
    28
	return self;
hfutrell@2353
    29
hfutrell@2353
    30
}
hfutrell@2353
    31
hfutrell@2353
    32
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
    33
hfutrell@2353
    34
	NSEnumerator *enumerator = [touches objectEnumerator];
hfutrell@2353
    35
	UITouch *touch=(UITouch*)[enumerator nextObject];
hfutrell@2353
    36
	
hfutrell@2353
    37
	// associate touches with mice, so long as we have slots
hfutrell@2353
    38
	int i;
hfutrell@2353
    39
	int found = 0;
hfutrell@2353
    40
	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
hfutrell@2353
    41
	
hfutrell@2353
    42
		// check if this mouse is already tracking a touch
hfutrell@2353
    43
		if (mice[i].driverdata != NULL) {
hfutrell@2353
    44
			continue;
hfutrell@2353
    45
		}
hfutrell@2353
    46
		
hfutrell@2353
    47
		found = 1;
hfutrell@2353
    48
		
hfutrell@2353
    49
		int oldMouse = SDL_SelectMouse(-1);
hfutrell@2353
    50
		SDL_SelectMouse(i);
hfutrell@2353
    51
		CGPoint locationInView = [touch locationInView: self];
hfutrell@2353
    52
		mice[i].driverdata = [touch retain];
hfutrell@2353
    53
		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
hfutrell@2353
    54
		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
hfutrell@2353
    55
		SDL_GetRelativeMouseState(NULL, NULL);
hfutrell@2353
    56
		touch = (UITouch*)[enumerator nextObject]; 
hfutrell@2353
    57
		
hfutrell@2353
    58
		SDL_SelectMouse(oldMouse);
hfutrell@2353
    59
		
hfutrell@2353
    60
	}	
hfutrell@2353
    61
}
hfutrell@2353
    62
hfutrell@2353
    63
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
    64
	
hfutrell@2353
    65
	NSEnumerator *enumerator = [touches objectEnumerator];
hfutrell@2353
    66
	UITouch *touch=nil;
hfutrell@2353
    67
	
hfutrell@2353
    68
	while(touch = (UITouch *)[enumerator nextObject]) {
hfutrell@2353
    69
		int i, found = NO;
hfutrell@2353
    70
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
hfutrell@2353
    71
			if (mice[i].driverdata == touch) {
hfutrell@2353
    72
				[(UITouch*)(mice[i].driverdata) release];
hfutrell@2353
    73
				mice[i].driverdata = NULL;
hfutrell@2353
    74
				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
hfutrell@2353
    75
hfutrell@2353
    76
				found = YES;
hfutrell@2353
    77
			}
hfutrell@2353
    78
		}
hfutrell@2353
    79
	}
hfutrell@2353
    80
}
hfutrell@2353
    81
hfutrell@2353
    82
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
    83
	/*
hfutrell@2353
    84
		this can happen if the user puts more than 5 touches on the screen
hfutrell@2353
    85
		at once, or perhaps in other circumstances.  Usually all active
hfutrell@2353
    86
		touches are canceled.
hfutrell@2353
    87
	*/
hfutrell@2353
    88
	[self touchesEnded: touches withEvent: event];
hfutrell@2353
    89
}
hfutrell@2353
    90
hfutrell@2353
    91
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
hfutrell@2353
    92
	
hfutrell@2353
    93
	NSEnumerator *enumerator = [touches objectEnumerator];
hfutrell@2353
    94
	UITouch *touch=nil;
hfutrell@2353
    95
	
hfutrell@2353
    96
	while(touch = (UITouch *)[enumerator nextObject]) {
hfutrell@2353
    97
		int i, found = NO;
hfutrell@2353
    98
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
hfutrell@2353
    99
			if (mice[i].driverdata == touch) {
hfutrell@2353
   100
				CGPoint locationInView = [touch locationInView: self];
hfutrell@2353
   101
				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y);
hfutrell@2353
   102
				found = YES;
hfutrell@2353
   103
			}
hfutrell@2353
   104
		}
hfutrell@2353
   105
	}
hfutrell@2353
   106
}
hfutrell@2353
   107
hfutrell@2353
   108
@end