jens@0
|
1 |
/* This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
|
jens@0
|
2 |
http://developer.apple.com/samplecode/GeekGameBoard/
|
jens@0
|
3 |
Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
|
jens@0
|
4 |
|
jens@0
|
5 |
Redistribution and use in source and binary forms, with or without modification, are permitted
|
jens@0
|
6 |
provided that the following conditions are met:
|
jens@0
|
7 |
|
jens@0
|
8 |
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
jens@0
|
9 |
and the following disclaimer.
|
jens@0
|
10 |
* Redistributions in binary form must reproduce the above copyright notice, this list of
|
jens@0
|
11 |
conditions and the following disclaimer in the documentation and/or other materials provided
|
jens@0
|
12 |
with the distribution.
|
jens@0
|
13 |
|
jens@0
|
14 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
jens@0
|
15 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
jens@0
|
16 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
|
jens@0
|
17 |
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
jens@0
|
18 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
jens@0
|
19 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
jens@0
|
20 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
jens@0
|
21 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
jens@0
|
22 |
*/
|
jens@0
|
23 |
#import "Card.h"
|
jens@1
|
24 |
#import "GGBTextLayer.h"
|
jens@0
|
25 |
#import "QuartzUtils.h"
|
jens@0
|
26 |
|
jens@0
|
27 |
|
jens@0
|
28 |
@implementation Card
|
jens@0
|
29 |
|
jens@0
|
30 |
|
jens@0
|
31 |
static CATransform3D kFaceUpTransform, kFaceDownTransform;
|
jens@0
|
32 |
|
jens@0
|
33 |
+ (void) initialize
|
jens@0
|
34 |
{
|
jens@0
|
35 |
if( self==[Card class] ) {
|
jens@0
|
36 |
kFaceUpTransform = kFaceDownTransform = CATransform3DIdentity;
|
jens@0
|
37 |
// Construct a 180-degree rotation matrix:
|
jens@0
|
38 |
kFaceDownTransform.m11 = kFaceDownTransform.m33 = -1;
|
jens@0
|
39 |
// The more obvious way to create kFaceDownTransform would be to call
|
jens@0
|
40 |
// CATransform3DMakeRotation(pi,0,1,0), but due to round-off errors, that transform
|
jens@0
|
41 |
// will have non-zero values in some other places, making it appear to CA as a true
|
jens@0
|
42 |
// 3D transform; this will then cause unexpected clipping behaviors when used.
|
jens@0
|
43 |
}
|
jens@0
|
44 |
}
|
jens@0
|
45 |
|
jens@0
|
46 |
|
jens@0
|
47 |
+ (NSRange) serialNumberRange;
|
jens@0
|
48 |
{
|
jens@0
|
49 |
NSAssert1(NO,@"%@ forgot to override +serialNumberRange",self);
|
jens@0
|
50 |
return NSMakeRange(0,0);
|
jens@0
|
51 |
}
|
jens@0
|
52 |
|
jens@0
|
53 |
|
jens@0
|
54 |
- (id) initWithSerialNumber: (int)serial position: (CGPoint)pos
|
jens@0
|
55 |
{
|
jens@0
|
56 |
self = [super init];
|
jens@0
|
57 |
if (self != nil) {
|
jens@0
|
58 |
_serialNumber = serial;
|
jens@0
|
59 |
self.bounds = CGRectMake(0,0,kCardWidth,kCardHeight);
|
jens@0
|
60 |
self.position = pos;
|
jens@0
|
61 |
self.edgeAntialiasingMask = 0;
|
jens@0
|
62 |
_back = [self createBack];
|
jens@0
|
63 |
[self addSublayer: _back];
|
jens@0
|
64 |
_front = [self createFront];
|
jens@0
|
65 |
_front.transform = kFaceDownTransform;
|
jens@0
|
66 |
[self addSublayer: _front];
|
jens@0
|
67 |
}
|
jens@0
|
68 |
return self;
|
jens@0
|
69 |
}
|
jens@0
|
70 |
|
jens@0
|
71 |
|
jens@1
|
72 |
- (id) copyWithZone: (NSZone*)zone
|
jens@0
|
73 |
{
|
jens@1
|
74 |
Card *clone = [super copyWithZone: zone];
|
jens@1
|
75 |
clone->_serialNumber = _serialNumber;
|
jens@1
|
76 |
return clone;
|
jens@0
|
77 |
}
|
jens@0
|
78 |
|
jens@0
|
79 |
|
jens@0
|
80 |
- (NSString*) description
|
jens@0
|
81 |
{
|
jens@0
|
82 |
return [NSString stringWithFormat: @"%@[#%i]",self.class,_serialNumber];
|
jens@0
|
83 |
}
|
jens@0
|
84 |
|
jens@0
|
85 |
|
jens@0
|
86 |
@synthesize serialNumber=_serialNumber;
|
jens@0
|
87 |
|
jens@0
|
88 |
|
jens@0
|
89 |
- (BOOL) faceUp
|
jens@0
|
90 |
{
|
jens@0
|
91 |
return _faceUp;
|
jens@0
|
92 |
}
|
jens@0
|
93 |
|
jens@0
|
94 |
- (void) setFaceUp: (BOOL)up
|
jens@0
|
95 |
{
|
jens@0
|
96 |
if( up != _faceUp ) {
|
jens@0
|
97 |
// The Card has separate sub-layers for its front and back. At any time, one of them
|
jens@0
|
98 |
// is hidden, by having a 180 degree rotation about the Y axis.
|
jens@0
|
99 |
// To flip the card, both front and back layers are flipped over.
|
jens@0
|
100 |
CATransform3D xform;
|
jens@0
|
101 |
xform = up ?kFaceUpTransform :kFaceDownTransform;
|
jens@0
|
102 |
_front.transform = xform;
|
jens@0
|
103 |
|
jens@0
|
104 |
xform = up ?kFaceDownTransform :kFaceUpTransform;
|
jens@0
|
105 |
_back.transform = xform;
|
jens@0
|
106 |
_faceUp = up;
|
jens@0
|
107 |
}
|
jens@0
|
108 |
}
|
jens@0
|
109 |
|
jens@0
|
110 |
|
jens@1
|
111 |
- (GGBLayer*) createFront
|
jens@0
|
112 |
{
|
jens@1
|
113 |
GGBLayer *front = [[GGBLayer alloc] init];
|
jens@0
|
114 |
front.bounds = CGRectMake(0,0,kCardWidth,kCardHeight);
|
jens@0
|
115 |
front.position = CGPointMake(kCardWidth/2,kCardHeight/2);
|
jens@0
|
116 |
front.edgeAntialiasingMask = 0;
|
jens@0
|
117 |
front.backgroundColor = kWhiteColor;
|
jens@0
|
118 |
front.cornerRadius = 8;
|
jens@0
|
119 |
front.borderWidth = 1;
|
jens@1
|
120 |
front.borderColor = CreateGray(0.7, 1.0);
|
jens@0
|
121 |
front.doubleSided = NO; // this makes the layer invisible when it's flipped
|
jens@0
|
122 |
return [front autorelease];
|
jens@0
|
123 |
}
|
jens@0
|
124 |
|
jens@0
|
125 |
|
jens@1
|
126 |
- (GGBLayer*) createBack
|
jens@0
|
127 |
{
|
jens@0
|
128 |
CGSize size = self.bounds.size;
|
jens@1
|
129 |
GGBLayer *back = [[GGBLayer alloc] init];
|
jens@0
|
130 |
back.bounds = CGRectMake(0,0,size.width,size.height);
|
jens@0
|
131 |
back.position = CGPointMake(kCardWidth/2,kCardHeight/2);
|
jens@0
|
132 |
back.contents = (id) GetCGImageNamed(@"/Library/Desktop Pictures/Classic Aqua Blue.jpg");
|
jens@0
|
133 |
back.contentsGravity = kCAGravityResize;
|
jens@0
|
134 |
back.masksToBounds = YES;
|
jens@0
|
135 |
back.borderWidth = 4;
|
jens@0
|
136 |
back.borderColor = kWhiteColor;
|
jens@0
|
137 |
back.cornerRadius = 8;
|
jens@0
|
138 |
back.edgeAntialiasingMask = 0;
|
jens@0
|
139 |
back.doubleSided = NO; // this makes the layer invisible when it's flipped
|
jens@0
|
140 |
|
jens@1
|
141 |
GGBTextLayer *label = [GGBTextLayer textLayerInSuperlayer: back
|
jens@1
|
142 |
withText: @"\u2603" // Unicode snowman character
|
jens@1
|
143 |
fontSize: 0.9*size.width
|
jens@1
|
144 |
alignment: kCALayerWidthSizable|kCALayerHeightSizable];
|
jens@1
|
145 |
label.foregroundColor = CreateGray(1.0,0.5);
|
jens@0
|
146 |
return [back autorelease];
|
jens@0
|
147 |
}
|
jens@0
|
148 |
|
jens@0
|
149 |
|
jens@0
|
150 |
#pragma mark -
|
jens@0
|
151 |
#pragma mark DRAG-AND-DROP:
|
jens@0
|
152 |
|
jens@0
|
153 |
|
jens@1
|
154 |
#if ! TARGET_OS_ASPEN
|
jens@1
|
155 |
|
jens@0
|
156 |
// An image from another app can be dragged onto a Card to change its background. */
|
jens@0
|
157 |
|
jens@0
|
158 |
|
jens@0
|
159 |
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
|
jens@0
|
160 |
{
|
jens@0
|
161 |
NSPasteboard *pb = [sender draggingPasteboard];
|
jens@0
|
162 |
if( [NSImage canInitWithPasteboard: pb] )
|
jens@0
|
163 |
return NSDragOperationCopy;
|
jens@0
|
164 |
else
|
jens@0
|
165 |
return NSDragOperationNone;
|
jens@0
|
166 |
}
|
jens@0
|
167 |
|
jens@0
|
168 |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
|
jens@0
|
169 |
{
|
jens@0
|
170 |
CGImageRef image = GetCGImageFromPasteboard([sender draggingPasteboard]);
|
jens@0
|
171 |
if( image ) {
|
jens@1
|
172 |
GGBLayer *face = _faceUp ?_front :_back;
|
jens@0
|
173 |
face.contents = (id) image;
|
jens@0
|
174 |
face.contentsGravity = kCAGravityResizeAspectFill;
|
jens@0
|
175 |
face.masksToBounds = YES;
|
jens@0
|
176 |
return YES;
|
jens@0
|
177 |
} else
|
jens@0
|
178 |
return NO;
|
jens@0
|
179 |
}
|
jens@0
|
180 |
|
jens@1
|
181 |
#endif
|
jens@0
|
182 |
|
jens@0
|
183 |
@end
|