/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. #import "LampButton.h"
  8. #import "CCNode+ContentSize.h"
  9. @implementation LampButton
  10. + (id) buttonWithText:(NSString*) text
  11. font:(NSString*) font
  12. size:(float) fontSize
  13. lampOnLeft:(bool) lampOnLeft
  14. target:(id) target
  15. selector:(SEL) selector
  16. {
  17. return [[[self alloc] initWithText:text
  18. font:font
  19. size:fontSize
  20. lampOnLeft:lampOnLeft
  21. target:target
  22. selector:selector] autorelease];
  23. }
  24. - (id) initWithText:(NSString*) text
  25. font:(NSString*) font
  26. size:(float) fontSize
  27. lampOnLeft:(bool) lampOnLeft
  28. target:(id) targetIn
  29. selector:(SEL) selectorIn
  30. {
  31. label = [CCLabel labelWithString:text fontName:font fontSize:fontSize];
  32. label.anchorPoint = ccp(0.5f, 0.5f);
  33. lampOn = [CCSprite spriteWithFile:@"panel-lamp-on.png"];
  34. lampOn.anchorPoint = ccp(0.5f, 0.5f);
  35. lampOff = [CCSprite spriteWithFile:@"panel-lamp-off.png"];
  36. lampOff.anchorPoint = ccp(0.5f, 0.5f);
  37. float maxHeight = lampOn.contentSize.height;
  38. if(lampOff.contentSize.height > maxHeight)
  39. {
  40. maxHeight = lampOff.contentSize.height;
  41. }
  42. if(label.contentSize.height > maxHeight)
  43. {
  44. maxHeight = label.contentSize.height;
  45. }
  46. float halfHeight = maxHeight * 0.5f;
  47. if(lampOnLeft)
  48. {
  49. label.position = ccp(lampOn.contentSize.width + 4 + label.contentSize.width*0.5f, halfHeight);
  50. lampOn.position = lampOff.position = ccp(lampOn.contentSize.width*0.5f, halfHeight);
  51. }
  52. else
  53. {
  54. label.position = ccp(label.contentSize.width*0.5f, halfHeight);
  55. lampOn.position = lampOff.position = ccp(label.contentSize.width + 4 + lampOn.contentSize.width*0.5f, halfHeight);
  56. }
  57. lampOn.visible = NO;
  58. CCNode* node = [CCNode node];
  59. [node addChild:lampOn];
  60. [node addChild:lampOff];
  61. [node addChild:label];
  62. [node setContentSizeFromChildren];
  63. if(nil != (self = [super initWithTouchablePortion:node target:targetIn selector:selectorIn]))
  64. {
  65. scaleOnPush = NO;
  66. }
  67. return self;
  68. }
  69. - (void) onButtonPressed
  70. {
  71. self.isOn = !self.isOn;
  72. [super onButtonPressed];
  73. }
  74. @synthesize label;
  75. - (bool) isOn
  76. {
  77. return lampOn.visible;
  78. }
  79. - (void) setIsOn:(bool) value
  80. {
  81. lampOn.visible = value;
  82. lampOff.visible = !value;
  83. }
  84. @end