PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/LampButton.m

http://github.com/kstenerud/ObjectAL-for-iPhone
Objective C | 101 lines | 78 code | 17 blank | 6 comment | 5 complexity | 828a9e4c26b543b6324f89c57ca1ae25 MD5 | raw file
  1//
  2//  LampButton.m
  3//  ObjectAL
  4//
  5//  Created by Karl Stenerud.
  6//
  7
  8#import "LampButton.h"
  9#import "CCNode+ContentSize.h"
 10
 11
 12@implementation LampButton
 13
 14+ (id) buttonWithText:(NSString*) text
 15				 font:(NSString*) font
 16				 size:(float) fontSize
 17		   lampOnLeft:(bool) lampOnLeft
 18			   target:(id) target
 19			 selector:(SEL) selector
 20{
 21	return [[[self alloc] initWithText:text
 22								  font:font
 23								  size:fontSize
 24							lampOnLeft:lampOnLeft
 25								target:target
 26							  selector:selector] autorelease];
 27}
 28
 29- (id) initWithText:(NSString*) text
 30			   font:(NSString*) font
 31			   size:(float) fontSize
 32		 lampOnLeft:(bool) lampOnLeft
 33			 target:(id) targetIn
 34		   selector:(SEL) selectorIn
 35{
 36	label = [CCLabel labelWithString:text fontName:font fontSize:fontSize];
 37	label.anchorPoint = ccp(0.5f, 0.5f);
 38
 39	lampOn = [CCSprite spriteWithFile:@"panel-lamp-on.png"];
 40	lampOn.anchorPoint = ccp(0.5f, 0.5f);
 41	lampOff = [CCSprite spriteWithFile:@"panel-lamp-off.png"];
 42	lampOff.anchorPoint = ccp(0.5f, 0.5f);
 43	
 44	float maxHeight = lampOn.contentSize.height;
 45	if(lampOff.contentSize.height > maxHeight)
 46	{
 47		maxHeight = lampOff.contentSize.height;
 48	}
 49	if(label.contentSize.height > maxHeight)
 50	{
 51		maxHeight = label.contentSize.height;
 52	}
 53	
 54	float halfHeight = maxHeight * 0.5f;
 55
 56	if(lampOnLeft)
 57	{
 58		label.position = ccp(lampOn.contentSize.width + 4 + label.contentSize.width*0.5f, halfHeight);
 59		lampOn.position = lampOff.position = ccp(lampOn.contentSize.width*0.5f, halfHeight);
 60	}
 61	else
 62	{
 63		label.position = ccp(label.contentSize.width*0.5f, halfHeight);
 64		lampOn.position = lampOff.position = ccp(label.contentSize.width + 4 + lampOn.contentSize.width*0.5f, halfHeight);
 65	}
 66	
 67	lampOn.visible = NO;
 68
 69	CCNode* node = [CCNode node];
 70	[node addChild:lampOn];
 71	[node addChild:lampOff];
 72	[node addChild:label];
 73	[node setContentSizeFromChildren];
 74	
 75	if(nil != (self = [super initWithTouchablePortion:node target:targetIn selector:selectorIn]))
 76	{
 77		scaleOnPush = NO;
 78	}
 79	return self;
 80}
 81
 82- (void) onButtonPressed
 83{
 84	self.isOn = !self.isOn;
 85	[super onButtonPressed];
 86}
 87
 88@synthesize label;
 89
 90- (bool) isOn
 91{
 92	return lampOn.visible;
 93}
 94
 95- (void) setIsOn:(bool) value
 96{
 97	lampOn.visible = value;
 98	lampOff.visible = !value;
 99}
100
101@end