Created
April 12, 2011 06:24
-
-
Save pixelrevision/915041 to your computer and use it in GitHub Desktop.
SXSwitch.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_SWITCH_EVENT_CHANGED @"switchChanged" | |
@interface SXSwitch : SPSprite { | |
BOOL on; | |
SPDisplayObject *onGraphic; | |
SPDisplayObject *offGraphic; | |
} | |
@property (nonatomic, assign) BOOL on; | |
@property (nonatomic, retain) SPDisplayObject *onGraphic; | |
@property (nonatomic, retain) SPDisplayObject *offGraphic; | |
- (id)initWithOnImagePath:(NSString*)onG andOffImagePath:(NSString*)offG; | |
- (void)update; | |
@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 "SXSwitch.h" | |
@implementation SXSwitch | |
@synthesize on; | |
@synthesize onGraphic; | |
@synthesize offGraphic; | |
- (id)init{ | |
self = [super init]; | |
on = YES; | |
self.onGraphic = [SPQuad quadWithWidth:20 height:20 color:0xc0c0c0]; | |
self.offGraphic = [SPQuad quadWithWidth:20 height:20 color:0x3d3d3d]; | |
[self addEventListener:@selector(touched:) atObject:self forType:SP_EVENT_TYPE_TOUCH]; | |
[self update]; | |
return self; | |
} | |
- (id)initWithOnImagePath:(NSString*)onG andOffImagePath:(NSString*)offG{ | |
self = [super init]; | |
on = YES; | |
self.onGraphic = [SPImage imageWithContentsOfFile:onG]; | |
self.offGraphic = [SPImage imageWithContentsOfFile:offG]; | |
[self addEventListener:@selector(touched:) atObject:self forType:SP_EVENT_TYPE_TOUCH]; | |
[self update]; | |
return self; | |
} | |
- (void)touched:(SPTouchEvent*)event{ | |
SPTouch *touchEnded = [[event touchesWithTarget:self andPhase:SPTouchPhaseEnded] anyObject]; | |
if(touchEnded){ | |
SPPoint *touchPosition = [touchEnded locationInSpace:self]; | |
if ([self hitTestPoint:touchPosition forTouch:YES]) { | |
self.on = !self.on; | |
} | |
} | |
} | |
- (void)setOn:(BOOL)val{ | |
on = val; | |
[self update]; | |
[self dispatchEvent:[SPEvent eventWithType:SX_SWITCH_EVENT_CHANGED]]; | |
} | |
- (void)setOnGraphic :(SPDisplayObject *)val{ | |
if(onGraphic){ | |
if([self containsChild:onGraphic]){ | |
[self removeChild:onGraphic]; | |
} | |
[onGraphic release]; | |
onGraphic = nil; | |
} | |
if(val != nil){ | |
onGraphic = [val retain]; | |
[self addChild:onGraphic]; | |
} | |
[self update]; | |
} | |
- (void)setOffGraphic :(SPDisplayObject *)val{ | |
if(offGraphic){ | |
if([self containsChild:offGraphic]){ | |
[self removeChild:offGraphic]; | |
} | |
[offGraphic release]; | |
offGraphic = nil; | |
} | |
if(val != nil){ | |
offGraphic = [val retain]; | |
[self addChild:offGraphic]; | |
} | |
[self update]; | |
} | |
- (void)update{ | |
onGraphic.visible = on; | |
offGraphic.visible = !on; | |
} | |
- (void)dealloc{ | |
self.onGraphic = nil; | |
self.offGraphic = nil; | |
[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