|
| 1 | +/* |
| 2 | + * cocos2d for iPhone: http://www.cocos2d-iphone.org |
| 3 | + * |
| 4 | + * Copyright (c) 2008-2010 Ricardo Quesada |
| 5 | + * Copyright (c) 2011 Zynga Inc. |
| 6 | + * Copyright (c) 2013-2014 Cocos2D Authors |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#import "CCNodeTag.h" |
| 28 | +#import <objc/runtime.h> |
| 29 | + |
| 30 | +//---------------------------------------------------------------------- |
| 31 | + |
| 32 | +static void *nodeTagKey = &nodeTagKey; |
| 33 | + |
| 34 | +//---------------------------------------------------------------------- |
| 35 | + |
| 36 | +@implementation CCNode (CCNodeTag) |
| 37 | + |
| 38 | +//---------------------------------------------------------------------- |
| 39 | + |
| 40 | +- (void)addChild:(CCNode *)node z:(NSInteger)z tag:(NSInteger)tag |
| 41 | +{ |
| 42 | + node.tag = tag; |
| 43 | + [self addChild:node z:z]; |
| 44 | +} |
| 45 | + |
| 46 | +//---------------------------------------------------------------------- |
| 47 | + |
| 48 | +- (void)removeChildByTag:(NSInteger)tag |
| 49 | +{ |
| 50 | + CCNode *node = [self getChildByTag:tag]; |
| 51 | + if (!node) |
| 52 | + ; |
| 53 | + else |
| 54 | + [self removeChild:node]; |
| 55 | +} |
| 56 | + |
| 57 | +//---------------------------------------------------------------------- |
| 58 | + |
| 59 | +- (void)removeChildByTag:(NSInteger)tag cleanup:(BOOL)cleanup |
| 60 | +{ |
| 61 | + CCNode *node = [self getChildByTag:tag]; |
| 62 | + if (!node) |
| 63 | + ; |
| 64 | + else |
| 65 | + [self removeChild:node cleanup:cleanup]; |
| 66 | +} |
| 67 | + |
| 68 | +//---------------------------------------------------------------------- |
| 69 | + |
| 70 | +- (CCNode *)getChildByTag:(NSInteger)tag |
| 71 | +{ |
| 72 | + /* |
| 73 | + for (CCNode *node in self.children) |
| 74 | + { |
| 75 | + if (node.tag == tag) return(node); |
| 76 | + } |
| 77 | + return(nil); |
| 78 | + */ |
| 79 | + return [self getChildByTag:tag recursively:NO]; |
| 80 | +} |
| 81 | + |
| 82 | +// Recursively get a child by tag, but don't return the root of the search. |
| 83 | +-(CCNode*) getChildByTagRecursive:(NSInteger)tag root:(CCNode *)root |
| 84 | +{ |
| 85 | + if(self != root && self.tag == tag) return self; |
| 86 | + |
| 87 | + for (CCNode* node in _children) { |
| 88 | + CCNode *n = [node getChildByTagRecursive:tag root:root]; |
| 89 | + if(n) return n; |
| 90 | + } |
| 91 | + // not found |
| 92 | + return nil; |
| 93 | +} |
| 94 | + |
| 95 | +- (CCNode *)getChildByTag:(NSInteger)tag recursively:(bool)isRecursive { |
| 96 | + NSAssert(tag, @"tag is nil."); |
| 97 | + |
| 98 | + if(isRecursive){ |
| 99 | + return [self getChildByTagRecursive:tag root:self]; |
| 100 | + } else { |
| 101 | + for (CCNode* node in _children) { |
| 102 | + if(node.tag == tag){ |
| 103 | + return node; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + // not found |
| 108 | + return nil; |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +//---------------------------------------------------------------------- |
| 113 | +// tag property implementation |
| 114 | + |
| 115 | +// OBS! |
| 116 | +// As long as tag hasn't been set, the associated object will be nil, and intergetValue will return 0 (zero), which is well defined behaviour |
| 117 | + |
| 118 | +- (NSInteger)tag |
| 119 | +{ |
| 120 | + NSNumber *number = objc_getAssociatedObject(self, nodeTagKey); |
| 121 | + return([number integerValue]); |
| 122 | +} |
| 123 | + |
| 124 | +- (void)setTag:(NSInteger)tag |
| 125 | +{ |
| 126 | + objc_setAssociatedObject(self, nodeTagKey, [NSNumber numberWithInteger:tag], OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 127 | +} |
| 128 | + |
| 129 | +//---------------------------------------------------------------------- |
| 130 | + |
| 131 | +@end |
0 commit comments