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.
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.
26 #import "QuartzUtils.h"
29 #ifdef TARGET_OS_IPHONE
30 #define kPickedUpScale 2.0 // more magnification, so piece shows up underneath fingertip
31 #define kPickedUpOpacity 0.6
33 #define kPickedUpScale 1.2
34 #define kPickedUpOpacity 0.9
41 - (id) copyWithZone: (NSZone*)zone
43 Bit *clone = [super copyWithZone: zone];
44 clone->_owner = _owner;
55 @synthesize owner=_owner, tag=_tag;
57 - (BOOL) isFriendly {return _owner.friendly;}
58 - (BOOL) isUnfriendly {return _owner.unfriendly;}
61 - (NSString*) identifier
65 // Defaults to just identifying the owner:
66 return [NSString stringWithFormat: @"p%i", _owner.index+1];
72 NSNumber *scale = [self valueForKeyPath: @"transform.scale"];
73 return scale.floatValue;
76 - (void) setScale: (CGFloat)scale
78 [self setValue: [NSNumber numberWithFloat: scale]
79 forKeyPath: @"transform.scale"];
85 NSNumber *rot = [self valueForKeyPath: @"transform.rotation"];
86 return round( rot.doubleValue * 180.0 / M_PI );
89 - (void) setRotation: (int)rotation
91 [self setValue: [NSNumber numberWithDouble: rotation*M_PI/180.0]
92 forKeyPath: @"transform.rotation"];
101 - (void) setPickedUp: (BOOL)up
103 if( up != _pickedUp ) {
104 CGFloat shadow, radius, opacity, z, scale;
108 offset = CGSizeMake(2,2);
110 opacity = kPickedUpOpacity;
111 scale = kPickedUpScale;
113 _restingZ = self.zPosition;
114 _restingShadowOpacity = self.shadowOpacity;
115 _restingShadowOffset = self.shadowOffset;
116 _restingShadowRadius = self.shadowRadius;
118 shadow = _restingShadowOpacity;
119 offset = _restingShadowOffset;
120 radius = _restingShadowRadius;
122 scale = 1.0/kPickedUpScale;
126 #if !TARGET_OS_IPHONE
128 self.shadowOpacity = shadow;
129 self.shadowOffset = offset;
130 self.shadowRadius = radius;
132 self.opacity = opacity;
139 - (BOOL)containsPoint:(CGPoint)p
141 // Make picked-up pieces invisible to hit-testing.
142 // Otherwise, while dragging a Bit, hit-testing the cursor position would always return
143 // that Bit, since it's directly under the cursor...
147 return [super containsPoint: p];
151 -(id<BitHolder>) holder
153 // Look for my nearest ancestor that's a BitHolder:
154 for( CALayer *layer=self.superlayer; layer; layer=layer.superlayer ) {
155 if( [layer conformsToProtocol: @protocol(BitHolder)] )
156 return (id<BitHolder>)layer;
157 else if( [layer isKindOfClass: [Bit class]] )
166 // "Pop" the Bit by expanding it 5x as it fades away:
169 // Removing the view from its superlayer right now would cancel the animations.
170 // Instead, defer the removal until sometime shortly after the animations finish:
171 [self performSelector: @selector(removeFromSuperlayer) withObject: nil afterDelay: 1.0];