/* * cocos2d for iPhone: http://www.cocos2d-iphone.org * * Copyright (c) 2008-2010 Ricardo Quesada * Copyright (c) 2011 Zynga Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. * */ #import #import "Platforms/CCGL.h" #import "CCLayer.h" #import "CCDirector.h" #import "ccMacros.h" #import "Support/CGPointExtension.h" #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED #import "Platforms/iOS/CCTouchDispatcher.h" #import "Platforms/iOS/CCDirectorIOS.h" #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) #import "Platforms/Mac/CCEventDispatcher.h" #endif #pragma mark - #pragma mark Layer @implementation CCLayer #pragma mark Layer - Init -(id) init { if( (self=[super init]) ) { CGSize s = [[CCDirector sharedDirector] winSize]; anchorPoint_ = ccp(0.5f, 0.5f); [self setContentSize:s]; self.isRelativeAnchorPoint = NO; isTouchEnabled_ = NO; #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED isAccelerometerEnabled_ = NO; #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) isMouseEnabled_ = NO; isKeyboardEnabled_ = NO; #endif } return self; } #pragma mark Layer - Touch and Accelerometer related #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED -(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:0]; } -(BOOL) isAccelerometerEnabled { return isAccelerometerEnabled_; } -(void) setIsAccelerometerEnabled:(BOOL)enabled { if( enabled != isAccelerometerEnabled_ ) { isAccelerometerEnabled_ = enabled; if( isRunning_ ) { if( enabled ) [[UIAccelerometer sharedAccelerometer] setDelegate:self]; else [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; } } } -(BOOL) isTouchEnabled { return isTouchEnabled_; } -(void) setIsTouchEnabled:(BOOL)enabled { if( isTouchEnabled_ != enabled ) { isTouchEnabled_ = enabled; if( isRunning_ ) { if( enabled ) [self registerWithTouchDispatcher]; else [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; } } } #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) #pragma mark CCLayer - Mouse, Keyboard & Touch events -(NSInteger) mouseDelegatePriority { return 0; } -(BOOL) isMouseEnabled { return isMouseEnabled_; } -(void) setIsMouseEnabled:(BOOL)enabled { if( isMouseEnabled_ != enabled ) { isMouseEnabled_ = enabled; if( isRunning_ ) { if( enabled ) [[CCEventDispatcher sharedDispatcher] addMouseDelegate:self priority:[self mouseDelegatePriority]]; else [[CCEventDispatcher sharedDispatcher] removeMouseDelegate:self]; } } } -(NSInteger) keyboardDelegatePriority { return 0; } -(BOOL) isKeyboardEnabled { return isKeyboardEnabled_; } -(void) setIsKeyboardEnabled:(BOOL)enabled { if( isKeyboardEnabled_ != enabled ) { isKeyboardEnabled_ = enabled; if( isRunning_ ) { if( enabled ) [[CCEventDispatcher sharedDispatcher] addKeyboardDelegate:self priority:[self keyboardDelegatePriority] ]; else [[CCEventDispatcher sharedDispatcher] removeKeyboardDelegate:self]; } } } -(NSInteger) touchDelegatePriority { return 0; } -(BOOL) isTouchEnabled { return isTouchEnabled_; } -(void) setIsTouchEnabled:(BOOL)enabled { if( isTouchEnabled_ != enabled ) { isTouchEnabled_ = enabled; if( isRunning_ ) { if( enabled ) [[CCEventDispatcher sharedDispatcher] addTouchDelegate:self priority:[self touchDelegatePriority]]; else [[CCEventDispatcher sharedDispatcher] removeTouchDelegate:self]; } } } #endif // Mac #pragma mark Layer - Callbacks -(void) onEnter { #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED // register 'parent' nodes first // since events are propagated in reverse order if (isTouchEnabled_) [self registerWithTouchDispatcher]; #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) if( isMouseEnabled_ ) [[CCEventDispatcher sharedDispatcher] addMouseDelegate:self priority:[self mouseDelegatePriority]]; if( isKeyboardEnabled_) [[CCEventDispatcher sharedDispatcher] addKeyboardDelegate:self priority:[self keyboardDelegatePriority]]; if( isTouchEnabled_) [[CCEventDispatcher sharedDispatcher] addTouchDelegate:self priority:[self touchDelegatePriority]]; #endif // then iterate over all the children [super onEnter]; } // issue #624. // Can't register mouse, touches here because of #issue #1018, and #1021 -(void) onEnterTransitionDidFinish { #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED if( isAccelerometerEnabled_ ) [[UIAccelerometer sharedAccelerometer] setDelegate:self]; #endif [super onEnterTransitionDidFinish]; } -(void) onExit { #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED if( isTouchEnabled_ ) [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; if( isAccelerometerEnabled_ ) [[UIAccelerometer sharedAccelerometer] setDelegate:nil]; #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED) if( isMouseEnabled_ ) [[CCEventDispatcher sharedDispatcher] removeMouseDelegate:self]; if( isKeyboardEnabled_ ) [[CCEventDispatcher sharedDispatcher] removeKeyboardDelegate:self]; if( isTouchEnabled_ ) [[CCEventDispatcher sharedDispatcher] removeTouchDelegate:self]; #endif [super onExit]; } #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { NSAssert(NO, @"Layer#ccTouchBegan override me"); return YES; } #endif @end #pragma mark - #pragma mark LayerColor @interface CCLayerColor (Private) -(void) updateColor; @end @implementation CCLayerColor // Opacity and RGB color protocol @synthesize opacity = opacity_, color = color_; @synthesize blendFunc = blendFunc_; + (id) layerWithColor:(ccColor4B)color width:(GLfloat)w height:(GLfloat) h { return [[[self alloc] initWithColor:color width:w height:h] autorelease]; } + (id) layerWithColor:(ccColor4B)color { return [[(CCLayerColor*)[self alloc] initWithColor:color] autorelease]; } - (id) initWithColor:(ccColor4B)color width:(GLfloat)w height:(GLfloat) h { if( (self=[super init]) ) { // default blend function blendFunc_ = (ccBlendFunc) { CC_BLEND_SRC, CC_BLEND_DST }; color_.r = color.r; color_.g = color.g; color_.b = color.b; opacity_ = color.a; for (NSUInteger i = 0; i