jens@0: /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0. jens@0: http://developer.apple.com/samplecode/GeekGameBoard/ jens@0: Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved. jens@0: jens@0: Redistribution and use in source and binary forms, with or without modification, are permitted jens@0: provided that the following conditions are met: jens@0: jens@0: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@0: and the following disclaimer. jens@0: * Redistributions in binary form must reproduce the above copyright notice, this list of jens@0: conditions and the following disclaimer in the documentation and/or other materials provided jens@0: with the distribution. jens@0: jens@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@0: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@0: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@0: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@0: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@0: */ jens@0: #import "Piece.h" jens@0: #import "QuartzUtils.h" jens@0: jens@0: jens@0: @implementation Piece jens@0: jens@0: jens@0: - (id) initWithImageNamed: (NSString*)imageName jens@0: scale: (CGFloat)scale jens@0: { jens@0: self = [super init]; jens@0: if (self != nil) { jens@0: self.imageName = imageName; jens@0: [self setImage: GetCGImageNamed(imageName) scale: scale]; jens@0: self.zPosition = kPieceZ; jens@0: } jens@0: return self; jens@0: } jens@0: jens@0: jens@1: - (id) copyWithZone: (NSZone*)zone jens@0: { jens@1: Piece *clone = [super copyWithZone: zone]; jens@1: clone.imageName = self.imageName; jens@1: return clone; jens@0: } jens@0: jens@0: jens@0: - (void) dealloc jens@0: { jens@0: [_imageName release]; jens@0: [super dealloc]; jens@0: } jens@0: jens@0: jens@0: - (NSString*) description jens@0: { jens@0: return [NSString stringWithFormat: @"%@[%@]", jens@0: [self class], jens@0: _imageName.lastPathComponent.stringByDeletingPathExtension]; jens@0: } jens@0: jens@0: jens@0: @synthesize imageName=_imageName; jens@0: jens@0: jens@0: - (void) setImage: (CGImageRef)image scale: (CGFloat)scale jens@0: { jens@0: self.contents = (id) image; jens@0: self.contentsGravity = @"resize"; jens@0: self.minificationFilter = kCAFilterLinear; jens@0: int width = CGImageGetWidth(image), height = CGImageGetHeight(image); jens@0: if( scale > 0 ) { jens@0: if( scale >= 4.0 ) jens@0: scale /= MAX(width,height); // interpret scale as target dimensions jens@0: width = ceil( width * scale); jens@0: height= ceil( height* scale); jens@0: } jens@0: self.bounds = CGRectMake(0,0,width,height); jens@0: self.imageName = nil; jens@0: } jens@0: jens@0: - (void) setImage: (CGImageRef)image jens@0: { jens@0: CGSize size = self.bounds.size; jens@0: [self setImage: image scale: MAX(size.width,size.height)]; jens@0: } jens@0: jens@0: - (void) setImageNamed: (NSString*)name jens@0: { jens@0: [self setImage: GetCGImageNamed(name)]; jens@0: self.imageName = name; jens@0: } jens@0: jens@0: jens@0: - (BOOL)containsPoint:(CGPoint)p jens@0: { jens@0: // Overrides CGLayer's implementation, jens@0: // returning YES only for pixels at which this layer's alpha is at least 0.5. jens@0: // This takes into account the opacity, bg color, and background image's alpha channel. jens@0: if( ! [super containsPoint: p] ) jens@0: return NO; jens@0: float opacity = self.opacity; jens@0: if( opacity < 0.5 ) jens@0: return NO; jens@0: float thresholdAlpha = 0.5 / self.opacity; jens@0: jens@0: CGColorRef bg = self.backgroundColor; jens@0: float alpha = bg ?CGColorGetAlpha(bg) :0.0; jens@0: if( alpha < thresholdAlpha ) { jens@0: CGImageRef image = (CGImageRef)self.contents; jens@0: if( image ) { jens@0: // Note: This makes the convenient assumption that the image entirely fills the bounds. jens@0: alpha = MAX(alpha, GetPixelAlpha(image, self.bounds.size, p)); jens@0: } jens@0: } jens@0: return alpha >= thresholdAlpha; jens@0: } jens@0: jens@0: jens@0: @end