Initial check-in into Mercurial. Branched from 1.0 release of Apple's sample code. No longer requires garbage collection. Fixed some memory leaks of CG objects. Fixed a bug when advancing to the 8th row in the Checkers game.
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.
5 Redistribution and use in source and binary forms, with or without modification, are permitted
6 provided that the following conditions are met:
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.
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.
25 #import "QuartzUtils.h"
31 - (id) copyWithZone: (NSZone*)zone
33 // NSLayer isn't copyable, but it is archivable. So create a copy by archiving to
34 // a temporary data block, then unarchiving a new layer from that block.
36 // One complication is that, due to a bug in Core Animation, CALayer can't archive
37 // a pattern-based CGColor. So as a workaround, clear the background before archiving,
38 // then restore it afterwards.
40 // Also, archiving a CALayer with an image in it leaks memory. (Filed as rdar://5786865 )
41 // As a workaround, clear the contents before archiving, then restore.
43 CGColorRef bg = CGColorRetain(self.backgroundColor);
44 self.backgroundColor = NULL;
45 id contents = [self.contents retain];
48 NSData *data = [NSKeyedArchiver archivedDataWithRootObject: self];
50 self.backgroundColor = bg;
51 self.contents = contents;
53 Bit *clone = [NSKeyedUnarchiver unarchiveObjectWithData: data];
54 clone.backgroundColor = bg;
55 clone.contents = contents;
59 clone->_owner = _owner; // _owner is not archived
60 return [clone retain];
64 - (NSString*) description
66 return [NSString stringWithFormat: @"%@[(%g,%g)]", self.class,self.position.x,self.position.y];
70 @synthesize owner=_owner;
72 - (BOOL) isFriendly {return _owner.friendly;}
73 - (BOOL) isUnfriendly {return _owner.unfriendly;}
78 NSNumber *scale = [self valueForKeyPath: @"transform.scale"];
79 return scale.floatValue;
82 - (void) setScale: (CGFloat)scale
84 [self setValue: [NSNumber numberWithFloat: scale]
85 forKeyPath: @"transform.scale"];
91 NSNumber *rot = [self valueForKeyPath: @"transform.rotation"];
92 return round( rot.doubleValue * 180.0 / M_PI );
95 - (void) setRotation: (int)rotation
97 [self setValue: [NSNumber numberWithDouble: rotation*M_PI/180.0]
98 forKeyPath: @"transform.rotation"];
104 return self.zPosition >= kPickedUpZ;
107 - (void) setPickedUp: (BOOL)up
109 if( up != self.pickedUp ) {
110 CGFloat shadow, offset, radius, opacity, z, scale;
118 _restingZ = self.zPosition;
120 shadow = offset = radius = 0.0;
127 self.shadowOpacity = shadow;
128 self.shadowOffset = CGSizeMake(offset,-offset);
129 self.shadowRadius = radius;
130 self.opacity = opacity;
136 - (BOOL)containsPoint:(CGPoint)p
138 // Make picked-up pieces invisible to hit-testing.
139 // Otherwise, while dragging a Bit, hit-testing the cursor position would always return
140 // that Bit, since it's directly under the cursor...
144 return [super containsPoint: p];
148 -(id<BitHolder>) holder
150 // Look for my nearest ancestor that's a BitHolder:
151 for( CALayer *layer=self.superlayer; layer; layer=layer.superlayer ) {
152 if( [layer conformsToProtocol: @protocol(BitHolder)] )
153 return (id<BitHolder>)layer;
154 else if( [layer isKindOfClass: [Bit class]] )
163 // "Pop" the Bit by expanding it 5x as it fades away:
166 // Removing the view from its superlayer right now would cancel the animations.
167 // Instead, defer the removal until sometime shortly after the animations finish:
168 [self performSelector: @selector(removeFromSuperlayer) withObject: nil afterDelay: 1.0];