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@11: - (id) init jens@11: { jens@11: self = [super init]; jens@11: if (self != nil) { jens@11: self.zPosition = kPieceZ; jens@11: } jens@11: return self; jens@11: } jens@11: jens@11: jens@11: jens@0: - (id) initWithImageNamed: (NSString*)imageName jens@0: scale: (CGFloat)scale jens@0: { jens@11: self = [self init]; jens@0: if (self != nil) { jens@9: [self setImageNamed: imageName scale: scale]; 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@9: - (void) _setImage: (CGImageRef)image jens@9: { jens@9: self.contents = (id) image; jens@9: self.bounds = CGRectMake(0,0,CGImageGetWidth(image),CGImageGetHeight(image)); jens@9: self.contentsGravity = kCAGravityResizeAspect; jens@9: self.minificationFilter = kCAFilterLinear; jens@9: self.imageName = nil; jens@9: } jens@9: jens@9: jens@0: - (void) setImage: (CGImageRef)image scale: (CGFloat)scale jens@0: { jens@9: [self _setImage: CreateScaledImage(image,scale)]; jens@9: } jens@9: jens@9: - (void) setImageNamed: (NSString*)imageName scale: (CGFloat)scale jens@9: { jens@9: [self _setImage: GetScaledImageNamed(imageName,scale)]; jens@9: self.imageName = imageName; 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@11: #if ! TARGET_OS_IPHONE jens@11: jens@11: // An image from another app can be dragged onto a Piece to change its background pattern. jens@11: jens@11: - (NSDragOperation)draggingEntered:(id )sender jens@11: { jens@11: if( CanGetCGImageFromPasteboard([sender draggingPasteboard]) ) jens@11: return NSDragOperationCopy; jens@11: else jens@11: return NSDragOperationNone; jens@11: } jens@11: jens@11: - (BOOL)performDragOperation:(id )sender jens@11: { jens@11: CGImageRef image = GetCGImageFromPasteboard([sender draggingPasteboard],sender); jens@11: if( image ) { jens@11: [self setValue: (id)image ofStyleProperty: @"contents"]; jens@11: return YES; jens@11: } else jens@11: return NO; jens@11: } jens@11: jens@11: #endif jens@11: jens@11: jens@0: @end