Created
April 11, 2011 19:05
-
-
Save pixelrevision/914079 to your computer and use it in GitHub Desktop.
SXJoypad.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import "Sparrow.h" | |
#define SX_JOYPAD_EVENT_STARTED_INPUT @"startedInput" | |
#define SX_JOYPAD_EVENT_STOPPED_INPUT @"stoppedInput" | |
#define SX_JOYPAD_EVENT_UPDATED_INPUT @"updatedInput" | |
typedef enum{ | |
kSXJoypadVerticleDirectionNone, | |
kSXJoypadVerticleDirectionTop, | |
kSXJoypadVerticleDirectionBottom | |
}SXJoypadVerticleDirection; | |
typedef enum{ | |
kSXJoypadHorizontalDirectionNone, | |
kSXJoypadHorizontalDirectionLeft, | |
kSXJoypadHorizontalDirectionRight | |
}SXJoypadHorizontalDirection; | |
@interface SXJoypad : SPImage { | |
SPPoint *centerPoint; | |
BOOL watchingTouches; | |
SXJoypadVerticleDirection verticalDirection; | |
SXJoypadHorizontalDirection horizontalDirection; | |
double angle; | |
SPPoint *projection; | |
float projectionModifier; | |
BOOL recievingInput; | |
} | |
@property (nonatomic, retain) SPPoint *centerPoint; | |
@property (nonatomic, readonly) SXJoypadVerticleDirection verticalDirection; | |
@property (nonatomic, readonly) SXJoypadHorizontalDirection horizontalDirection; | |
@property (nonatomic, retain) SPPoint *projection; | |
@property (nonatomic, readonly) double angle; | |
@property (nonatomic, readonly) float projectionModifier; | |
@property (nonatomic, readonly) BOOL recievingInput; | |
- (id)initWithTexture:(SPTexture*)texture; | |
- (id)initWithContentsOfFile:(NSString*)path; | |
+ (SXJoypad*)joypadWithTexture:(SPTexture*)texture; | |
+ (SXJoypad*)joypadWithContentsOfFile:(NSString*)path; | |
- (void)reset; | |
- (void)touched:(SPTouchEvent*)touch; | |
- (void)calculateValuesWithPosition:(SPPoint*)pos; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SXJoypad.h" | |
@implementation SXJoypad | |
@synthesize centerPoint; | |
@synthesize verticalDirection; | |
@synthesize horizontalDirection; | |
@synthesize projection; | |
@synthesize angle; | |
@synthesize projectionModifier; | |
@synthesize recievingInput; | |
- (id)initWithTexture:(SPTexture*)texture{ | |
self = [super initWithTexture:texture]; | |
[self reset]; | |
return self; | |
} | |
- (id)initWithContentsOfFile:(NSString*)path{ | |
self = [super initWithContentsOfFile:path]; | |
[self reset]; | |
return self; | |
} | |
+ (SXJoypad*)joypadWithTexture:(SPTexture*)texture{ | |
SXJoypad *jp = [[[SXJoypad alloc] initWithTexture:texture] autorelease]; | |
return jp; | |
} | |
+ (SXJoypad*)joypadWithContentsOfFile:(NSString*)path{ | |
SXJoypad *jp = [[[SXJoypad alloc] initWithContentsOfFile:path] autorelease]; | |
return jp; | |
} | |
- (void)reset{ | |
self.centerPoint = [SPPoint pointWithX:self.width/2 y:self.height/2]; | |
self.projection = [SPPoint pointWithX:0 y:0]; | |
projectionModifier = 1; | |
angle = 0; | |
if(!watchingTouches){ | |
watchingTouches = YES; | |
[self addEventListener:@selector(touched:) atObject:self forType:SP_EVENT_TYPE_TOUCH]; | |
} | |
centerPoint.x = self.width/2; | |
centerPoint.y = self.height/2; | |
} | |
- (void)touched:(SPTouchEvent*)event{ | |
SPTouch *touchBegan = [[event touchesWithTarget:self andPhase:SPTouchPhaseBegan] anyObject]; | |
if(touchBegan){ | |
SPPoint *touchPosition = [touchBegan locationInSpace:self]; | |
[self calculateValuesWithPosition:touchPosition]; | |
recievingInput = YES; | |
[self dispatchEvent:[SPEvent eventWithType:SX_JOYPAD_EVENT_STARTED_INPUT]]; | |
} | |
SPTouch *touchMoved = [[event touchesWithTarget:self andPhase:SPTouchPhaseMoved] anyObject]; | |
if(touchMoved){ | |
SPPoint *touchPosition = [touchMoved locationInSpace:self]; | |
[self calculateValuesWithPosition:touchPosition]; | |
recievingInput = YES; | |
[self dispatchEvent:[SPEvent eventWithType:SX_JOYPAD_EVENT_UPDATED_INPUT]]; | |
} | |
SPTouch *touchEnded = [[event touchesWithTarget:self andPhase:SPTouchPhaseEnded] anyObject]; | |
if(touchEnded){ | |
horizontalDirection = kSXJoypadVerticleDirectionNone; | |
verticalDirection = kSXJoypadHorizontalDirectionNone; | |
projection.x = 0; | |
projection.y = 0; | |
recievingInput = NO; | |
[self dispatchEvent:[SPEvent eventWithType:SX_JOYPAD_EVENT_STOPPED_INPUT]]; | |
} | |
} | |
- (void)calculateValuesWithPosition:(SPPoint*)pos{ | |
// grab the angle | |
angle = atan2(pos.y - centerPoint.y, pos.x - centerPoint.x); | |
double angCheck = SP_R2D(angle); | |
while (angCheck > 360) { | |
angCheck -= 360; | |
} | |
while(angCheck < 0){ | |
angCheck += 360; | |
} | |
// get some more static input | |
horizontalDirection = kSXJoypadVerticleDirectionNone; | |
verticalDirection = kSXJoypadHorizontalDirectionNone; | |
if(angCheck < 45 || angCheck > 315){ | |
horizontalDirection = kSXJoypadHorizontalDirectionRight; | |
}else if(angCheck >= 45 && angCheck < 135){ | |
verticalDirection = kSXJoypadVerticleDirectionBottom; | |
}else if (angCheck >= 135 && angCheck < 225){ | |
horizontalDirection = kSXJoypadHorizontalDirectionLeft; | |
}else if(angCheck >= 225 && angCheck < 315){ | |
verticalDirection = kSXJoypadVerticleDirectionTop; | |
} | |
// create a vector for projection | |
float dx = pos.x - centerPoint.x; | |
float dy = pos.y - centerPoint.y; | |
float distance = sqrt(dx*dx + dy*dy) * projectionModifier; | |
projection.x = cos(angle) * distance; | |
projection.y = sin(angle) * distance; | |
} | |
- (void)dealloc{ | |
self.centerPoint = nil; | |
self.projection = nil; | |
if(watchingTouches){ | |
watchingTouches = NO; | |
[self removeEventListener:@selector(touched:) atObject:self forType:SP_EVENT_TYPE_TOUCH]; | |
} | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment