Source/Card.m
author Jens Alfke <jens@mooseyard.com>
Fri Mar 07 11:43:02 2008 -0800 (2008-03-07)
changeset 0 e9f7ba4718e1
child 1 3eb7be1dd7b6
permissions -rw-r--r--
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.
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@0
    24
#import "QuartzUtils.h"
jens@0
    25
jens@0
    26
jens@0
    27
@implementation Card
jens@0
    28
jens@0
    29
jens@0
    30
static CATransform3D kFaceUpTransform, kFaceDownTransform;
jens@0
    31
jens@0
    32
+ (void) initialize
jens@0
    33
{
jens@0
    34
    if( self==[Card class] ) {
jens@0
    35
        kFaceUpTransform = kFaceDownTransform = CATransform3DIdentity;
jens@0
    36
        // Construct a 180-degree rotation matrix:
jens@0
    37
        kFaceDownTransform.m11 = kFaceDownTransform.m33 = -1;
jens@0
    38
        // The more obvious way to create kFaceDownTransform would be to call
jens@0
    39
        // CATransform3DMakeRotation(pi,0,1,0), but due to round-off errors, that transform
jens@0
    40
        // will have non-zero values in some other places, making it appear to CA as a true
jens@0
    41
        // 3D transform; this will then cause unexpected clipping behaviors when used.
jens@0
    42
    }
jens@0
    43
}
jens@0
    44
jens@0
    45
jens@0
    46
+ (NSRange) serialNumberRange;
jens@0
    47
{
jens@0
    48
    NSAssert1(NO,@"%@ forgot to override +serialNumberRange",self);
jens@0
    49
    return NSMakeRange(0,0);
jens@0
    50
}
jens@0
    51
jens@0
    52
jens@0
    53
- (id) initWithSerialNumber: (int)serial position: (CGPoint)pos
jens@0
    54
{
jens@0
    55
    self = [super init];
jens@0
    56
    if (self != nil) {
jens@0
    57
        _serialNumber = serial;
jens@0
    58
        self.bounds = CGRectMake(0,0,kCardWidth,kCardHeight);
jens@0
    59
        self.position = pos;
jens@0
    60
        self.edgeAntialiasingMask = 0;
jens@0
    61
        _back = [self createBack];
jens@0
    62
        [self addSublayer: _back];
jens@0
    63
        _front = [self createFront];
jens@0
    64
        _front.transform = kFaceDownTransform;
jens@0
    65
        [self addSublayer: _front];
jens@0
    66
    }
jens@0
    67
    return self;
jens@0
    68
}
jens@0
    69
jens@0
    70
jens@0
    71
- (void)encodeWithCoder:(NSCoder *)aCoder
jens@0
    72
{
jens@0
    73
    [super encodeWithCoder: aCoder];
jens@0
    74
    [aCoder encodeInt: _serialNumber forKey: @"serialNumber"];
jens@0
    75
}
jens@0
    76
jens@0
    77
- (id)initWithCoder:(NSCoder *)aDecoder
jens@0
    78
{
jens@0
    79
    self = [super initWithCoder: aDecoder];
jens@0
    80
    if( self ) {
jens@0
    81
        _serialNumber = [aDecoder decodeIntForKey: @"serialNumber"];
jens@0
    82
    }
jens@0
    83
    return self;
jens@0
    84
}
jens@0
    85
jens@0
    86
jens@0
    87
- (NSString*) description
jens@0
    88
{
jens@0
    89
    return [NSString stringWithFormat: @"%@[#%i]",self.class,_serialNumber];
jens@0
    90
}
jens@0
    91
jens@0
    92
jens@0
    93
@synthesize serialNumber=_serialNumber;
jens@0
    94
jens@0
    95
jens@0
    96
- (BOOL) faceUp
jens@0
    97
{
jens@0
    98
    return _faceUp;
jens@0
    99
}
jens@0
   100
jens@0
   101
- (void) setFaceUp: (BOOL)up
jens@0
   102
{
jens@0
   103
    if( up != _faceUp ) {
jens@0
   104
        // The Card has separate sub-layers for its front and back. At any time, one of them
jens@0
   105
        // is hidden, by having a 180 degree rotation about the Y axis.
jens@0
   106
        // To flip the card, both front and back layers are flipped over.
jens@0
   107
        CATransform3D xform;
jens@0
   108
        xform = up ?kFaceUpTransform :kFaceDownTransform;
jens@0
   109
        _front.transform = xform;
jens@0
   110
        
jens@0
   111
        xform = up ?kFaceDownTransform :kFaceUpTransform;
jens@0
   112
        _back.transform = xform;
jens@0
   113
        _faceUp = up;
jens@0
   114
    }
jens@0
   115
}
jens@0
   116
jens@0
   117
jens@0
   118
- (CALayer*) createFront
jens@0
   119
{
jens@0
   120
    CALayer *front = [[CALayer alloc] init];
jens@0
   121
    front.bounds = CGRectMake(0,0,kCardWidth,kCardHeight);
jens@0
   122
    front.position = CGPointMake(kCardWidth/2,kCardHeight/2);
jens@0
   123
    front.edgeAntialiasingMask = 0;
jens@0
   124
    front.backgroundColor = kWhiteColor;
jens@0
   125
    front.cornerRadius = 8;
jens@0
   126
    front.borderWidth = 1;
jens@0
   127
    front.borderColor = CGColorCreateGenericGray(0.7, 1.0);
jens@0
   128
    front.doubleSided = NO;         // this makes the layer invisible when it's flipped
jens@0
   129
    return [front autorelease];
jens@0
   130
}
jens@0
   131
jens@0
   132
jens@0
   133
- (CALayer*) createBack
jens@0
   134
{
jens@0
   135
    CGSize size = self.bounds.size;
jens@0
   136
    CALayer *back = [[CALayer alloc] init];
jens@0
   137
    back.bounds = CGRectMake(0,0,size.width,size.height);
jens@0
   138
    back.position = CGPointMake(kCardWidth/2,kCardHeight/2);
jens@0
   139
    back.contents = (id) GetCGImageNamed(@"/Library/Desktop Pictures/Classic Aqua Blue.jpg");
jens@0
   140
    back.contentsGravity = kCAGravityResize;
jens@0
   141
    back.masksToBounds = YES;
jens@0
   142
    back.borderWidth = 4;
jens@0
   143
    back.borderColor = kWhiteColor;
jens@0
   144
    back.cornerRadius = 8;
jens@0
   145
    back.edgeAntialiasingMask = 0;
jens@0
   146
    back.doubleSided = NO;          // this makes the layer invisible when it's flipped
jens@0
   147
    
jens@0
   148
    CATextLayer *label = AddTextLayer(back, @"\u2603",          // Unicode snowman character
jens@0
   149
                                      [NSFont systemFontOfSize: 0.9*size.width],
jens@0
   150
                                      kCALayerWidthSizable|kCALayerHeightSizable);
jens@0
   151
    label.foregroundColor = CGColorCreateGenericGray(1.0,0.5);
jens@0
   152
    return [back autorelease];
jens@0
   153
}    
jens@0
   154
jens@0
   155
jens@0
   156
#pragma mark -
jens@0
   157
#pragma mark DRAG-AND-DROP:
jens@0
   158
jens@0
   159
jens@0
   160
// An image from another app can be dragged onto a Card to change its background. */
jens@0
   161
jens@0
   162
jens@0
   163
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
jens@0
   164
{
jens@0
   165
    NSPasteboard *pb = [sender draggingPasteboard];
jens@0
   166
    if( [NSImage canInitWithPasteboard: pb] )
jens@0
   167
        return NSDragOperationCopy;
jens@0
   168
    else
jens@0
   169
        return NSDragOperationNone;
jens@0
   170
}
jens@0
   171
jens@0
   172
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
jens@0
   173
{
jens@0
   174
    CGImageRef image = GetCGImageFromPasteboard([sender draggingPasteboard]);
jens@0
   175
    if( image ) {
jens@0
   176
        CALayer *face = _faceUp ?_front :_back;
jens@0
   177
        face.contents = (id) image;
jens@0
   178
        face.contentsGravity = kCAGravityResizeAspectFill;
jens@0
   179
        face.masksToBounds = YES;
jens@0
   180
        return YES;
jens@0
   181
    } else
jens@0
   182
        return NO;
jens@0
   183
}
jens@0
   184
jens@0
   185
jens@0
   186
@end