summary refs log tree commit diff stats
path: root/libs/cocos2d/CCMenuItem.m
diff options
context:
space:
mode:
Diffstat (limited to 'libs/cocos2d/CCMenuItem.m')
-rwxr-xr-xlibs/cocos2d/CCMenuItem.m795
1 files changed, 795 insertions, 0 deletions
diff --git a/libs/cocos2d/CCMenuItem.m b/libs/cocos2d/CCMenuItem.m new file mode 100755 index 0000000..f88c0e0 --- /dev/null +++ b/libs/cocos2d/CCMenuItem.m
@@ -0,0 +1,795 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2008-2011 Ricardo Quesada
5 * Copyright (c) 2011 Zynga Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 */
26
27#import "CCMenuItem.h"
28#import "CCLabelTTF.h"
29#import "CCLabelAtlas.h"
30#import "CCActionInterval.h"
31#import "CCSprite.h"
32#import "Support/CGPointExtension.h"
33#import "CCBlockSupport.h"
34
35 static NSUInteger _fontSize = kCCItemSize;
36static NSString *_fontName = @"Marker Felt";
37static BOOL _fontNameRelease = NO;
38
39
40const uint32_t kCurrentItem = 0xc0c05001;
41const uint32_t kZoomActionTag = 0xc0c05002;
42
43
44#pragma mark -
45#pragma mark CCMenuItem
46
47@implementation CCMenuItem
48
49@synthesize isSelected=isSelected_;
50-(id) init
51{
52 NSAssert(NO, @"MenuItemInit: Init not supported.");
53 [self release];
54 return nil;
55}
56
57+(id) itemWithTarget:(id) r selector:(SEL) s
58{
59 return [[[self alloc] initWithTarget:r selector:s] autorelease];
60}
61
62-(id) initWithTarget:(id) rec selector:(SEL) cb
63{
64 if((self=[super init]) ) {
65
66 anchorPoint_ = ccp(0.5f, 0.5f);
67 NSMethodSignature * sig = nil;
68
69 if( rec && cb ) {
70 sig = [rec methodSignatureForSelector:cb];
71
72 invocation_ = nil;
73 invocation_ = [NSInvocation invocationWithMethodSignature:sig];
74 [invocation_ setTarget:rec];
75 [invocation_ setSelector:cb];
76#if NS_BLOCKS_AVAILABLE
77 if ([sig numberOfArguments] == 3)
78#endif
79 [invocation_ setArgument:&self atIndex:2];
80
81 [invocation_ retain];
82 }
83
84 isEnabled_ = YES;
85 isSelected_ = NO;
86 }
87
88 return self;
89}
90
91#if NS_BLOCKS_AVAILABLE
92
93+(id) itemWithBlock:(void(^)(id sender))block {
94 return [[[self alloc] initWithBlock:block] autorelease];
95}
96
97-(id) initWithBlock:(void(^)(id sender))block {
98 block_ = [block copy];
99 return [self initWithTarget:block_ selector:@selector(ccCallbackBlockWithSender:)];
100}
101
102#endif // NS_BLOCKS_AVAILABLE
103
104-(void) dealloc
105{
106 [invocation_ release];
107
108#if NS_BLOCKS_AVAILABLE
109 [block_ release];
110#endif
111
112 [super dealloc];
113}
114
115-(void) selected
116{
117 isSelected_ = YES;
118}
119
120-(void) unselected
121{
122 isSelected_ = NO;
123}
124
125-(void) activate
126{
127 if(isEnabled_)
128 [invocation_ invoke];
129}
130
131-(void) setIsEnabled: (BOOL)enabled
132{
133 isEnabled_ = enabled;
134}
135
136-(BOOL) isEnabled
137{
138 return isEnabled_;
139}
140
141-(CGRect) rect
142{
143 return CGRectMake( position_.x - contentSize_.width*anchorPoint_.x,
144 position_.y - contentSize_.height*anchorPoint_.y,
145 contentSize_.width, contentSize_.height);
146}
147
148@end
149
150
151#pragma mark -
152#pragma mark CCMenuItemLabel
153
154@implementation CCMenuItemLabel
155
156@synthesize disabledColor = disabledColor_;
157
158+(id) itemWithLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label target:(id)target selector:(SEL)selector
159{
160 return [[[self alloc] initWithLabel:label target:target selector:selector] autorelease];
161}
162
163+(id) itemWithLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label
164{
165 return [[[self alloc] initWithLabel:label target:nil selector:NULL] autorelease];
166}
167
168-(id) initWithLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label target:(id)target selector:(SEL)selector
169{
170 if( (self=[super initWithTarget:target selector:selector]) ) {
171 originalScale_ = 1;
172 colorBackup = ccWHITE;
173 disabledColor_ = ccc3( 126,126,126);
174 self.label = label;
175
176 }
177 return self;
178}
179
180#if NS_BLOCKS_AVAILABLE
181
182+(id) itemWithLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label block:(void(^)(id sender))block {
183 return [[[self alloc] initWithLabel:label block:block] autorelease];
184}
185
186-(id) initWithLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label block:(void(^)(id sender))block {
187 block_ = [block copy];
188 return [self initWithLabel:label target:block_ selector:@selector(ccCallbackBlockWithSender:)];
189}
190
191#endif // NS_BLOCKS_AVAILABLE
192
193-(CCNode<CCLabelProtocol, CCRGBAProtocol>*) label
194{
195 return label_;
196}
197-(void) setLabel:(CCNode<CCLabelProtocol, CCRGBAProtocol>*) label
198{
199 if( label != label_ ) {
200 [self removeChild:label_ cleanup:YES];
201 [self addChild:label];
202
203 label_ = label;
204 label_.anchorPoint = ccp(0,0);
205
206 [self setContentSize:[label_ contentSize]];
207 }
208}
209
210-(void) setString:(NSString *)string
211{
212 [label_ setString:string];
213 [self setContentSize: [label_ contentSize]];
214}
215
216-(void) activate {
217 if(isEnabled_) {
218 [self stopAllActions];
219
220 self.scale = originalScale_;
221
222 [super activate];
223 }
224}
225
226-(void) selected
227{
228 // subclass to change the default action
229 if(isEnabled_) {
230 [super selected];
231
232 CCAction *action = [self getActionByTag:kZoomActionTag];
233 if( action )
234 [self stopAction:action];
235 else
236 originalScale_ = self.scale;
237
238 CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_ * 1.2f];
239 zoomAction.tag = kZoomActionTag;
240 [self runAction:zoomAction];
241 }
242}
243
244-(void) unselected
245{
246 // subclass to change the default action
247 if(isEnabled_) {
248 [super unselected];
249 [self stopActionByTag:kZoomActionTag];
250 CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_];
251 zoomAction.tag = kZoomActionTag;
252 [self runAction:zoomAction];
253 }
254}
255
256-(void) setIsEnabled: (BOOL)enabled
257{
258 if( isEnabled_ != enabled ) {
259 if(enabled == NO) {
260 colorBackup = [label_ color];
261 [label_ setColor: disabledColor_];
262 }
263 else
264 [label_ setColor:colorBackup];
265 }
266
267 [super setIsEnabled:enabled];
268}
269
270- (void) setOpacity: (GLubyte)opacity
271{
272 [label_ setOpacity:opacity];
273}
274-(GLubyte) opacity
275{
276 return [label_ opacity];
277}
278-(void) setColor:(ccColor3B)color
279{
280 [label_ setColor:color];
281}
282-(ccColor3B) color
283{
284 return [label_ color];
285}
286@end
287
288#pragma mark -
289#pragma mark CCMenuItemAtlasFont
290
291@implementation CCMenuItemAtlasFont
292
293+(id) itemFromString: (NSString*) value charMapFile:(NSString*) charMapFile itemWidth:(int)itemWidth itemHeight:(int)itemHeight startCharMap:(char)startCharMap
294{
295 return [CCMenuItemAtlasFont itemFromString:value charMapFile:charMapFile itemWidth:itemWidth itemHeight:itemHeight startCharMap:startCharMap target:nil selector:nil];
296}
297
298+(id) itemFromString: (NSString*) value charMapFile:(NSString*) charMapFile itemWidth:(int)itemWidth itemHeight:(int)itemHeight startCharMap:(char)startCharMap target:(id) rec selector:(SEL) cb
299{
300 return [[[self alloc] initFromString:value charMapFile:charMapFile itemWidth:itemWidth itemHeight:itemHeight startCharMap:startCharMap target:rec selector:cb] autorelease];
301}
302
303-(id) initFromString: (NSString*) value charMapFile:(NSString*) charMapFile itemWidth:(int)itemWidth itemHeight:(int)itemHeight startCharMap:(char)startCharMap target:(id) rec selector:(SEL) cb
304{
305 NSAssert( [value length] != 0, @"value length must be greater than 0");
306
307 CCLabelAtlas *label = [[CCLabelAtlas alloc] initWithString:value charMapFile:charMapFile itemWidth:itemWidth itemHeight:itemHeight startCharMap:startCharMap];
308 [label autorelease];
309
310 if((self=[super initWithLabel:label target:rec selector:cb]) ) {
311 // do something ?
312 }
313
314 return self;
315}
316
317#if NS_BLOCKS_AVAILABLE
318+(id) itemFromString:(NSString*)value charMapFile:(NSString*)charMapFile itemWidth:(int)itemWidth itemHeight:(int)itemHeight startCharMap:(char)startCharMap block:(void(^)(id sender))block {
319 return [[[self alloc] initFromString:value charMapFile:charMapFile itemWidth:itemWidth itemHeight:itemHeight startCharMap:startCharMap block:block] autorelease];
320}
321
322-(id) initFromString:(NSString*)value charMapFile:(NSString*)charMapFile itemWidth:(int)itemWidth itemHeight:(int)itemHeight startCharMap:(char)startCharMap block:(void(^)(id sender))block {
323 block_ = [block copy];
324 return [self initFromString:value charMapFile:charMapFile itemWidth:itemWidth itemHeight:itemHeight startCharMap:startCharMap target:block_ selector:@selector(ccCallbackBlockWithSender:)];
325}
326#endif // NS_BLOCKS_AVAILABLE
327
328-(void) dealloc
329{
330 [super dealloc];
331}
332@end
333
334
335#pragma mark -
336#pragma mark CCMenuItemFont
337
338@implementation CCMenuItemFont
339
340+(void) setFontSize: (NSUInteger) s
341{
342 _fontSize = s;
343}
344
345+(NSUInteger) fontSize
346{
347 return _fontSize;
348}
349
350+(void) setFontName: (NSString*) n
351{
352 if( _fontNameRelease )
353 [_fontName release];
354
355 _fontName = [n retain];
356 _fontNameRelease = YES;
357}
358
359+(NSString*) fontName
360{
361 return _fontName;
362}
363
364+(id) itemFromString: (NSString*) value target:(id) r selector:(SEL) s
365{
366 return [[[self alloc] initFromString: value target:r selector:s] autorelease];
367}
368
369+(id) itemFromString: (NSString*) value
370{
371 return [[[self alloc] initFromString: value target:nil selector:nil] autorelease];
372}
373
374-(id) initFromString: (NSString*) value target:(id) rec selector:(SEL) cb
375{
376 NSAssert( [value length] != 0, @"Value length must be greater than 0");
377
378 fontName_ = [_fontName copy];
379 fontSize_ = _fontSize;
380
381 CCLabelTTF *label = [CCLabelTTF labelWithString:value fontName:fontName_ fontSize:fontSize_];
382
383 if((self=[super initWithLabel:label target:rec selector:cb]) ) {
384 // do something ?
385 }
386
387 return self;
388}
389
390-(void) recreateLabel
391{
392 CCLabelTTF *label = [CCLabelTTF labelWithString:[label_ string] fontName:fontName_ fontSize:fontSize_];
393 self.label = label;
394}
395
396-(void) setFontSize: (NSUInteger) size
397{
398 fontSize_ = size;
399 [self recreateLabel];
400}
401
402-(NSUInteger) fontSize
403{
404 return fontSize_;
405}
406
407-(void) setFontName: (NSString*) fontName
408{
409 if (fontName_)
410 [fontName_ release];
411
412 fontName_ = [fontName copy];
413 [self recreateLabel];
414}
415
416-(NSString*) fontName
417{
418 return fontName_;
419}
420
421#if NS_BLOCKS_AVAILABLE
422+(id) itemFromString: (NSString*) value block:(void(^)(id sender))block
423{
424 return [[[self alloc] initFromString:value block:block] autorelease];
425}
426
427-(id) initFromString: (NSString*) value block:(void(^)(id sender))block
428{
429 block_ = [block copy];
430 return [self initFromString:value target:block_ selector:@selector(ccCallbackBlockWithSender:)];
431}
432#endif // NS_BLOCKS_AVAILABLE
433
434@end
435
436#pragma mark -
437#pragma mark CCMenuItemSprite
438@implementation CCMenuItemSprite
439
440@synthesize normalImage=normalImage_, selectedImage=selectedImage_, disabledImage=disabledImage_;
441
442+(id) itemFromNormalSprite:(CCNode<CCRGBAProtocol>*)normalSprite selectedSprite:(CCNode<CCRGBAProtocol>*)selectedSprite
443{
444 return [self itemFromNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:nil target:nil selector:nil];
445}
446+(id) itemFromNormalSprite:(CCNode<CCRGBAProtocol>*)normalSprite selectedSprite:(CCNode<CCRGBAProtocol>*)selectedSprite target:(id)target selector:(SEL)selector
447{
448 return [self itemFromNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:nil target:target selector:selector];
449}
450+(id) itemFromNormalSprite:(CCNode<CCRGBAProtocol>*)normalSprite selectedSprite:(CCNode<CCRGBAProtocol>*)selectedSprite disabledSprite:(CCNode<CCRGBAProtocol>*)disabledSprite target:(id)target selector:(SEL)selector
451{
452 return [[[self alloc] initFromNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:disabledSprite target:target selector:selector] autorelease];
453}
454-(id) initFromNormalSprite:(CCNode<CCRGBAProtocol>*)normalSprite selectedSprite:(CCNode<CCRGBAProtocol>*)selectedSprite disabledSprite:(CCNode<CCRGBAProtocol>*)disabledSprite target:(id)target selector:(SEL)selector
455{
456 if( (self=[super initWithTarget:target selector:selector]) ) {
457
458 self.normalImage = normalSprite;
459 self.selectedImage = selectedSprite;
460 self.disabledImage = disabledSprite;
461
462 [self setContentSize: [normalImage_ contentSize]];
463 }
464 return self;
465}
466
467#if NS_BLOCKS_AVAILABLE
468+(id) itemFromNormalSprite:(CCNode<CCRGBAProtocol>*)normalSprite selectedSprite:(CCNode<CCRGBAProtocol>*)selectedSprite block:(void(^)(id sender))block {
469 return [self itemFromNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:nil block:block];
470}
471
472+(id) itemFromNormalSprite:(CCNode<CCRGBAProtocol>*)normalSprite selectedSprite:(CCNode<CCRGBAProtocol>*)selectedSprite disabledSprite:(CCNode<CCRGBAProtocol>*)disabledSprite block:(void(^)(id sender))block {
473 return [[[self alloc] initFromNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:disabledSprite block:block] autorelease];
474}
475
476-(id) initFromNormalSprite:(CCNode<CCRGBAProtocol>*)normalSprite selectedSprite:(CCNode<CCRGBAProtocol>*)selectedSprite disabledSprite:(CCNode<CCRGBAProtocol>*)disabledSprite block:(void(^)(id sender))block {
477 block_ = [block copy];
478 return [self initFromNormalSprite:normalSprite selectedSprite:selectedSprite disabledSprite:disabledSprite target:block_ selector:@selector(ccCallbackBlockWithSender:)];
479}
480#endif // NS_BLOCKS_AVAILABLE
481
482
483-(void) setNormalImage:(CCNode <CCRGBAProtocol>*)image
484{
485 if( image != normalImage_ ) {
486 image.anchorPoint = ccp(0,0);
487 image.visible = YES;
488
489 [self removeChild:normalImage_ cleanup:YES];
490 [self addChild:image];
491
492 normalImage_ = image;
493 }
494}
495
496-(void) setSelectedImage:(CCNode <CCRGBAProtocol>*)image
497{
498 if( image != selectedImage_ ) {
499 image.anchorPoint = ccp(0,0);
500 image.visible = NO;
501
502 [self removeChild:selectedImage_ cleanup:YES];
503 [self addChild:image];
504
505 selectedImage_ = image;
506 }
507}
508
509-(void) setDisabledImage:(CCNode <CCRGBAProtocol>*)image
510{
511 if( image != disabledImage_ ) {
512 image.anchorPoint = ccp(0,0);
513 image.visible = NO;
514
515 [self removeChild:disabledImage_ cleanup:YES];
516 [self addChild:image];
517
518 disabledImage_ = image;
519 }
520}
521
522#pragma mark CCMenuItemImage - CCRGBAProtocol protocol
523- (void) setOpacity: (GLubyte)opacity
524{
525 [normalImage_ setOpacity:opacity];
526 [selectedImage_ setOpacity:opacity];
527 [disabledImage_ setOpacity:opacity];
528}
529
530-(void) setColor:(ccColor3B)color
531{
532 [normalImage_ setColor:color];
533 [selectedImage_ setColor:color];
534 [disabledImage_ setColor:color];
535}
536
537-(GLubyte) opacity
538{
539 return [normalImage_ opacity];
540}
541
542-(ccColor3B) color
543{
544 return [normalImage_ color];
545}
546
547-(void) selected
548{
549 [super selected];
550
551 if( selectedImage_ ) {
552 [normalImage_ setVisible:NO];
553 [selectedImage_ setVisible:YES];
554 [disabledImage_ setVisible:NO];
555
556 } else { // there is not selected image
557
558 [normalImage_ setVisible:YES];
559 [selectedImage_ setVisible:NO];
560 [disabledImage_ setVisible:NO];
561 }
562}
563
564-(void) unselected
565{
566 [super unselected];
567 [normalImage_ setVisible:YES];
568 [selectedImage_ setVisible:NO];
569 [disabledImage_ setVisible:NO];
570}
571
572-(void) setIsEnabled:(BOOL)enabled
573{
574 [super setIsEnabled:enabled];
575
576 if( enabled ) {
577 [normalImage_ setVisible:YES];
578 [selectedImage_ setVisible:NO];
579 [disabledImage_ setVisible:NO];
580
581 } else {
582 if( disabledImage_ ) {
583 [normalImage_ setVisible:NO];
584 [selectedImage_ setVisible:NO];
585 [disabledImage_ setVisible:YES];
586 } else {
587 [normalImage_ setVisible:YES];
588 [selectedImage_ setVisible:NO];
589 [disabledImage_ setVisible:NO];
590 }
591 }
592}
593
594@end
595
596#pragma mark -
597#pragma mark CCMenuItemImage
598
599@implementation CCMenuItemImage
600
601+(id) itemFromNormalImage: (NSString*)value selectedImage:(NSString*) value2
602{
603 return [self itemFromNormalImage:value selectedImage:value2 disabledImage: nil target:nil selector:nil];
604}
605
606+(id) itemFromNormalImage: (NSString*)value selectedImage:(NSString*) value2 target:(id) t selector:(SEL) s
607{
608 return [self itemFromNormalImage:value selectedImage:value2 disabledImage: nil target:t selector:s];
609}
610
611+(id) itemFromNormalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage: (NSString*) value3
612{
613 return [[[self alloc] initFromNormalImage:value selectedImage:value2 disabledImage:value3 target:nil selector:nil] autorelease];
614}
615
616+(id) itemFromNormalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage: (NSString*) value3 target:(id) t selector:(SEL) s
617{
618 return [[[self alloc] initFromNormalImage:value selectedImage:value2 disabledImage:value3 target:t selector:s] autorelease];
619}
620
621-(id) initFromNormalImage: (NSString*) normalI selectedImage:(NSString*)selectedI disabledImage: (NSString*) disabledI target:(id)t selector:(SEL)sel
622{
623 CCNode<CCRGBAProtocol> *normalImage = [CCSprite spriteWithFile:normalI];
624 CCNode<CCRGBAProtocol> *selectedImage = nil;
625 CCNode<CCRGBAProtocol> *disabledImage = nil;
626
627 if( selectedI )
628 selectedImage = [CCSprite spriteWithFile:selectedI];
629 if(disabledI)
630 disabledImage = [CCSprite spriteWithFile:disabledI];
631
632 return [self initFromNormalSprite:normalImage selectedSprite:selectedImage disabledSprite:disabledImage target:t selector:sel];
633}
634
635#if NS_BLOCKS_AVAILABLE
636
637+(id) itemFromNormalImage: (NSString*)value selectedImage:(NSString*) value2 block:(void(^)(id sender))block {
638 return [self itemFromNormalImage:value selectedImage:value2 disabledImage:nil block:block];
639}
640
641+(id) itemFromNormalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage:(NSString*) value3 block:(void(^)(id sender))block {
642 return [[[self alloc] initFromNormalImage:value selectedImage:value2 disabledImage:value3 block:block] autorelease];
643}
644
645-(id) initFromNormalImage: (NSString*) value selectedImage:(NSString*)value2 disabledImage:(NSString*) value3 block:(void(^)(id sender))block {
646 block_ = [block copy];
647 return [self initFromNormalImage:value selectedImage:value2 disabledImage:value3 target:block_ selector:@selector(ccCallbackBlockWithSender:)];
648}
649
650#endif // NS_BLOCKS_AVAILABLE
651
652@end
653
654#pragma mark -
655#pragma mark CCMenuItemToggle
656
657//
658// MenuItemToggle
659//
660@implementation CCMenuItemToggle
661
662@synthesize subItems = subItems_;
663@synthesize opacity = opacity_, color = color_;
664
665+(id) itemWithTarget: (id)t selector: (SEL)sel items: (CCMenuItem*) item, ...
666{
667 va_list args;
668 va_start(args, item);
669
670 id s = [[[self alloc] initWithTarget: t selector:sel items: item vaList:args] autorelease];
671
672 va_end(args);
673 return s;
674}
675
676-(id) initWithTarget: (id)t selector: (SEL)sel items:(CCMenuItem*) item vaList: (va_list) args
677{
678 if( (self=[super initWithTarget:t selector:sel]) ) {
679
680 self.subItems = [NSMutableArray arrayWithCapacity:2];
681
682 int z = 0;
683 CCMenuItem *i = item;
684 while(i) {
685 z++;
686 [subItems_ addObject:i];
687 i = va_arg(args, CCMenuItem*);
688 }
689
690 selectedIndex_ = NSUIntegerMax;
691 [self setSelectedIndex:0];
692 }
693
694 return self;
695}
696
697#if NS_BLOCKS_AVAILABLE
698
699+(id) itemWithBlock:(void(^)(id sender))block items:(CCMenuItem*)item, ... {
700 va_list args;
701 va_start(args, item);
702
703 id s = [[[self alloc] initWithBlock:block items:item vaList:args] autorelease];
704
705 va_end(args);
706 return s;
707}
708
709-(id) initWithBlock:(void (^)(id))block items:(CCMenuItem*)item vaList:(va_list)args {
710 block_ = [block copy];
711 return [self initWithTarget:block_ selector:@selector(ccCallbackBlockWithSender:) items:item vaList:args];
712}
713
714#endif // NS_BLOCKS_AVAILABLE
715
716-(void) dealloc
717{
718 [subItems_ release];
719 [super dealloc];
720}
721
722-(void)setSelectedIndex:(NSUInteger)index
723{
724 if( index != selectedIndex_ ) {
725 selectedIndex_=index;
726 [self removeChildByTag:kCurrentItem cleanup:NO];
727
728 CCMenuItem *item = [subItems_ objectAtIndex:selectedIndex_];
729 [self addChild:item z:0 tag:kCurrentItem];
730
731 CGSize s = [item contentSize];
732 [self setContentSize: s];
733 item.position = ccp( s.width/2, s.height/2 );
734 }
735}
736
737-(NSUInteger) selectedIndex
738{
739 return selectedIndex_;
740}
741
742
743-(void) selected
744{
745 [super selected];
746 [[subItems_ objectAtIndex:selectedIndex_] selected];
747}
748
749-(void) unselected
750{
751 [super unselected];
752 [[subItems_ objectAtIndex:selectedIndex_] unselected];
753}
754
755-(void) activate
756{
757 // update index
758 if( isEnabled_ ) {
759 NSUInteger newIndex = (selectedIndex_ + 1) % [subItems_ count];
760 [self setSelectedIndex:newIndex];
761
762 }
763
764 [super activate];
765}
766
767-(void) setIsEnabled: (BOOL)enabled
768{
769 [super setIsEnabled:enabled];
770 for(CCMenuItem* item in subItems_)
771 [item setIsEnabled:enabled];
772}
773
774-(CCMenuItem*) selectedItem
775{
776 return [subItems_ objectAtIndex:selectedIndex_];
777}
778
779#pragma mark CCMenuItemToggle - CCRGBAProtocol protocol
780
781- (void) setOpacity: (GLubyte)opacity
782{
783 opacity_ = opacity;
784 for(CCMenuItem<CCRGBAProtocol>* item in subItems_)
785 [item setOpacity:opacity];
786}
787
788- (void) setColor:(ccColor3B)color
789{
790 color_ = color;
791 for(CCMenuItem<CCRGBAProtocol>* item in subItems_)
792 [item setColor:color];
793}
794
795@end