2010-09-30 19:03:48 +00:00
|
|
|
//
|
|
|
|
// TouchPanel.m
|
|
|
|
// XNI
|
|
|
|
//
|
|
|
|
// Created by Matej Jan on 29.9.10.
|
|
|
|
// Copyright 2010 Retronator. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "TouchPanel.h"
|
2010-10-01 02:36:21 +00:00
|
|
|
#import "TouchPanel+Internal.h"
|
2010-09-30 19:03:48 +00:00
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
#import "Retronator.Xni.Framework.h"
|
2010-09-30 19:03:48 +00:00
|
|
|
#import "Retronator.Xni.Framework.Input.Touch.h"
|
|
|
|
|
2010-12-08 04:57:47 +00:00
|
|
|
@interface XniTouchLocation : NSObject {
|
2010-10-01 02:36:21 +00:00
|
|
|
int identifier;
|
|
|
|
Vector2 *position;
|
|
|
|
Vector2 *previousPosition;
|
|
|
|
TouchLocationState state;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithPosition:(Vector2*)thePosition;
|
|
|
|
|
|
|
|
@property (nonatomic) TouchLocationState state;
|
|
|
|
|
2010-10-19 13:31:39 +00:00
|
|
|
- (void) moveIfPressed;
|
2010-10-01 02:36:21 +00:00
|
|
|
- (void) moveToPosition:(Vector2*)newPosition;
|
|
|
|
|
|
|
|
- (TouchLocation*) createTouchLocation;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2010-12-08 04:57:47 +00:00
|
|
|
@implementation XniTouchLocation
|
2010-10-01 02:36:21 +00:00
|
|
|
|
|
|
|
static int nextID = 0;
|
|
|
|
|
|
|
|
- (id) initWithPosition:(Vector2*)thePosition
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil) {
|
|
|
|
identifier = nextID++;
|
|
|
|
position = [thePosition retain];
|
|
|
|
previousPosition = nil;
|
|
|
|
state = TouchLocationStatePressed;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize state;
|
|
|
|
|
2010-10-19 13:31:39 +00:00
|
|
|
- (void) moveIfPressed {
|
|
|
|
if (state == TouchLocationStatePressed) {
|
|
|
|
state = TouchLocationStateMoved;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (void) moveToPosition:(Vector2*)newPosition {
|
|
|
|
[previousPosition release];
|
|
|
|
previousPosition = position;
|
|
|
|
position = [newPosition retain];
|
|
|
|
state = TouchLocationStateMoved;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (TouchLocation *) createTouchLocation {
|
|
|
|
return [[[TouchLocation alloc] initWithIdentifier:identifier
|
|
|
|
position:[Vector2 vectorWithVector:position]
|
|
|
|
previousPosition:previousPosition ? [Vector2 vectorWithVector:previousPosition] : nil
|
|
|
|
state:state] autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[position release];
|
|
|
|
[previousPosition release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2010-09-30 19:03:48 +00:00
|
|
|
@implementation TouchPanel
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
static TouchPanel *instance;
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self != nil) {
|
|
|
|
addTouches = [[NSMutableSet alloc] init];
|
|
|
|
removeTouches = [[NSMutableSet alloc] init];
|
|
|
|
releaseTouches = [[NSMutableSet alloc] init];
|
|
|
|
lateReleaseTouches = [[NSMutableSet alloc] init];
|
|
|
|
|
|
|
|
touchLocations = (NSMutableDictionary*)CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
|
|
|
}
|
|
|
|
return self;
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
+ (void) initialize {
|
|
|
|
instance = [[TouchPanel alloc] init];
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
@synthesize displayWidth;
|
|
|
|
@synthesize displayHeight;
|
|
|
|
@synthesize displayOrientation;
|
|
|
|
@synthesize enabledGestures;
|
|
|
|
|
2010-11-15 17:23:13 +00:00
|
|
|
+ (TouchCollection*) getState {
|
|
|
|
return [instance getState];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (GestureSample*) readGesture {
|
|
|
|
return [instance readGesture];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (TouchPanel*) getInstance {
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2010-12-14 23:28:16 +00:00
|
|
|
- (void) setView:(UIView *)theView {
|
|
|
|
view = theView;
|
|
|
|
}
|
2010-11-15 17:23:13 +00:00
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (BOOL) isGestureAvailable{
|
|
|
|
return NO;
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (TouchCollection*) getState{
|
|
|
|
TouchCollection *collection = [[[TouchCollection alloc] init] autorelease];
|
2010-12-08 04:57:47 +00:00
|
|
|
for (XniTouchLocation *touch in [touchLocations allValues]) {
|
2010-10-01 02:36:21 +00:00
|
|
|
[collection addObject:[touch createTouchLocation]];
|
2010-10-19 13:31:39 +00:00
|
|
|
|
|
|
|
// After get state is done, all pressed touches should be moved.
|
|
|
|
[touch moveIfPressed];
|
2010-10-01 02:36:21 +00:00
|
|
|
}
|
|
|
|
return collection;
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (GestureSample*) readGesture{
|
|
|
|
return nil;
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
// Internal methods
|
|
|
|
|
|
|
|
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
[addTouches unionSet:touches];
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
|
2011-02-04 22:19:35 +00:00
|
|
|
float scale = view.contentScaleFactor;
|
2010-10-01 02:36:21 +00:00
|
|
|
for (UITouch *touch in touches) {
|
2010-12-08 04:57:47 +00:00
|
|
|
XniTouchLocation *location = [touchLocations objectForKey:touch];
|
2010-12-14 23:28:16 +00:00
|
|
|
if (location) {
|
|
|
|
CGPoint position = [touch locationInView:view];
|
|
|
|
[location moveToPosition:[Vector2 vectorWithX:position.x * scale y:position.y * scale]];
|
2010-10-01 02:36:21 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
if ([addTouches containsObject:touch]) {
|
|
|
|
[lateReleaseTouches addObject:touch];
|
|
|
|
} else {
|
|
|
|
[releaseTouches addObject:touch];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-30 19:03:48 +00:00
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
// Just remove the touch locations so they dissapear from the state (without released happening).
|
|
|
|
for (UITouch *touch in touches) {
|
|
|
|
[touchLocations removeObjectForKey:touch];
|
|
|
|
}
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (void) update {
|
|
|
|
// Remove all previously released touches.
|
|
|
|
for (UITouch *touch in removeTouches) {
|
|
|
|
[touchLocations removeObjectForKey:touch];
|
|
|
|
}
|
|
|
|
[removeTouches removeAllObjects];
|
|
|
|
|
|
|
|
// Set released touches.
|
|
|
|
for (UITouch *touch in releaseTouches) {
|
2010-12-08 04:57:47 +00:00
|
|
|
XniTouchLocation *location = [touchLocations objectForKey:touch];
|
2010-10-01 02:36:21 +00:00
|
|
|
location.state = TouchLocationStateReleased;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift the pools.
|
|
|
|
NSMutableSet *temp = removeTouches;
|
|
|
|
removeTouches = releaseTouches;
|
|
|
|
releaseTouches = lateReleaseTouches;
|
|
|
|
lateReleaseTouches = temp;
|
|
|
|
|
|
|
|
// Add new touches
|
2011-02-04 22:19:35 +00:00
|
|
|
float scale = view.contentScaleFactor;
|
2010-10-01 02:36:21 +00:00
|
|
|
for (UITouch *touch in addTouches) {
|
2010-12-14 23:28:16 +00:00
|
|
|
CGPoint position = [touch locationInView:view];
|
2010-12-08 04:57:47 +00:00
|
|
|
XniTouchLocation *location = [[[XniTouchLocation alloc]
|
2010-11-02 09:29:19 +00:00
|
|
|
initWithPosition:[Vector2 vectorWithX:position.x * scale y:position.y * scale]] autorelease];
|
2010-12-14 23:28:16 +00:00
|
|
|
if (position.x == 0 && position.y == 0) {
|
|
|
|
NSLog(@"BOOMSDFS");
|
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
CFDictionaryAddValue((CFMutableDictionaryRef)touchLocations, touch, location);
|
|
|
|
}
|
|
|
|
[addTouches removeAllObjects];
|
2010-09-30 19:03:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[addTouches release];
|
|
|
|
[removeTouches release];
|
|
|
|
[releaseTouches release];
|
|
|
|
[lateReleaseTouches release];
|
|
|
|
[touchLocations release];
|
|
|
|
[super dealloc];
|
2010-09-30 19:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-01 02:36:21 +00:00
|
|
|
|
2010-09-30 19:03:48 +00:00
|
|
|
@end
|