Source/Piece.m
author Jens Alfke <jens@mooseyard.com>
Tue Jul 08 20:32:52 2008 -0700 (2008-07-08)
changeset 14 4585c74d809c
parent 9 a59acc683080
permissions -rw-r--r--
Minor fixes.
     1 /*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     2     http://developer.apple.com/samplecode/GeekGameBoard/
     3     Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     8     * Redistributions of source code must retain the above copyright notice, this list of conditions
     9       and the following disclaimer.
    10     * Redistributions in binary form must reproduce the above copyright notice, this list of
    11       conditions and the following disclaimer in the documentation and/or other materials provided
    12       with the distribution.
    13 
    14     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    15     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    16     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    17     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    18     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    19     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    20     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    21     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    22 */
    23 #import "Piece.h"
    24 #import "QuartzUtils.h"
    25 
    26 
    27 @implementation Piece
    28 
    29 
    30 - (id) init
    31 {
    32     self = [super init];
    33     if (self != nil) {
    34         self.zPosition = kPieceZ;
    35     }
    36     return self;
    37 }
    38 
    39 
    40 
    41 - (id) initWithImageNamed: (NSString*)imageName
    42                     scale: (CGFloat)scale
    43 {
    44     self = [self init];
    45     if (self != nil) {
    46         [self setImageNamed: imageName scale: scale];
    47     }
    48     return self;
    49 }
    50 
    51 
    52 - (id) copyWithZone: (NSZone*)zone
    53 {
    54     Piece *clone = [super copyWithZone: zone];
    55     clone.imageName = self.imageName;
    56     return clone;
    57 }
    58 
    59 
    60 - (void) dealloc
    61 {
    62     [_imageName release];
    63     [super dealloc];
    64 }
    65 
    66 
    67 - (NSString*) description
    68 {
    69     return [NSString stringWithFormat: @"%@[%@]", 
    70             [self class],
    71             _imageName.lastPathComponent.stringByDeletingPathExtension];
    72 }
    73 
    74 
    75 @synthesize imageName=_imageName;
    76 
    77 
    78 - (void) _setImage: (CGImageRef)image
    79 {
    80     self.contents = (id) image;
    81     self.bounds = CGRectMake(0,0,CGImageGetWidth(image),CGImageGetHeight(image));
    82     self.contentsGravity = kCAGravityResizeAspect;
    83     self.minificationFilter = kCAFilterLinear;
    84     self.imageName = nil;
    85 }
    86 
    87 
    88 - (void) setImage: (CGImageRef)image scale: (CGFloat)scale
    89 {
    90     [self _setImage: CreateScaledImage(image,scale)];
    91 }
    92 
    93 - (void) setImageNamed: (NSString*)imageName scale: (CGFloat)scale
    94 {
    95     [self _setImage: GetScaledImageNamed(imageName,scale)];
    96     self.imageName = imageName;
    97 }
    98 
    99 - (void) setImage: (CGImageRef)image
   100 {
   101     CGSize size = self.bounds.size;
   102     [self setImage: image scale: MAX(size.width,size.height)];
   103 }
   104 
   105 - (void) setImageNamed: (NSString*)name
   106 {
   107     [self setImage: GetCGImageNamed(name)];
   108     self.imageName = name;
   109 }
   110 
   111 
   112 - (BOOL)containsPoint:(CGPoint)p
   113 {
   114     // Overrides CGLayer's implementation,
   115     // returning YES only for pixels at which this layer's alpha is at least 0.5.
   116     // This takes into account the opacity, bg color, and background image's alpha channel.
   117     if( ! [super containsPoint: p] )
   118         return NO;
   119     float opacity = self.opacity;
   120     if( opacity < 0.5 )
   121         return NO;
   122     float thresholdAlpha = 0.5 / self.opacity;
   123     
   124     CGColorRef bg = self.backgroundColor;
   125     float alpha = bg ?CGColorGetAlpha(bg) :0.0;
   126     if( alpha < thresholdAlpha ) {
   127         CGImageRef image = (CGImageRef)self.contents;
   128         if( image ) {
   129             // Note: This makes the convenient assumption that the image entirely fills the bounds.
   130             alpha = MAX(alpha, GetPixelAlpha(image, self.bounds.size, p));
   131         }
   132     }
   133     return alpha >= thresholdAlpha;
   134 }
   135 
   136 
   137 #if ! TARGET_OS_IPHONE
   138 
   139 // An image from another app can be dragged onto a Piece to change its background pattern.
   140 
   141 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
   142 {
   143     if( CanGetCGImageFromPasteboard([sender draggingPasteboard]) )
   144         return NSDragOperationCopy;
   145     else
   146         return NSDragOperationNone;
   147 }
   148 
   149 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
   150 {
   151     CGImageRef image = GetCGImageFromPasteboard([sender draggingPasteboard],sender);
   152     if( image ) {
   153         [self setValue: (id)image ofStyleProperty: @"contents"];
   154         return YES;
   155     } else
   156         return NO;
   157 }
   158 
   159 #endif
   160 
   161 
   162 @end