Source/PlayingCard.m
author Jens Alfke <jens@mooseyard.com>
Wed Mar 12 15:51:32 2008 -0700 (2008-03-12)
changeset 6 af9b2b929b03
parent 1 3eb7be1dd7b6
child 8 45c82a071aca
permissions -rw-r--r--
Fixed: An exception in the Go game if you mouse down on the board but then drag to the captured-pieces area and release the mouse.
     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 "PlayingCard.h"
    24 #import "GGBTextLayer.h"
    25 #import "QuartzUtils.h"
    26 
    27 
    28 @implementation PlayingCard
    29 
    30 
    31 + (NSRange) serialNumberRange;
    32 {
    33     return NSMakeRange(1,52);
    34 }
    35 
    36 
    37 - (GGBLayer*) createFront
    38 {
    39     GGBLayer *front = [super createFront];
    40     NSString *name = [NSString stringWithFormat: @"%@%@",
    41                       self.rankString, self.suitString];
    42     
    43     CGColorRef suitColor = self.suitColor;
    44     float scale = [Card cardSize].height/150;
    45     float cornerFontSize = MAX(18*scale, 14);
    46     float centerFontSize = 80*scale;
    47     
    48 #if TARGET_OS_ASPEN
    49     UIFont *cornerFont = [UIFont boldSystemFontOfSize: cornerFontSize];
    50 #else
    51     NSFont *cornerFont = [NSFont boldSystemFontOfSize: cornerFontSize];
    52 #endif
    53     GGBTextLayer *label;
    54     label = [GGBTextLayer textLayerInSuperlayer: front
    55                                        withText: name
    56                                            font: cornerFont
    57                                       alignment: kCALayerMaxXMargin | kCALayerBottomMargin];
    58     label.foregroundColor = suitColor;
    59     label = [GGBTextLayer textLayerInSuperlayer: front
    60                                        withText: name
    61                                            font: cornerFont
    62                                       alignment: kCALayerMaxXMargin | kCALayerTopMargin];
    63     label.foregroundColor = suitColor;
    64     label.anchorPoint = CGPointMake(1,1);
    65     [label setValue: [NSNumber numberWithFloat: M_PI] forKeyPath: @"transform.rotation"];
    66     
    67     label = [GGBTextLayer textLayerInSuperlayer: front
    68                                        withText: self.faceSymbol
    69                                        fontSize: centerFontSize
    70                                       alignment: kCALayerWidthSizable | kCALayerHeightSizable];
    71     label.foregroundColor = suitColor;
    72     //label.borderWidth = 1;
    73     //label.borderColor = kBlackColor;
    74     
    75     return front;
    76 }
    77 
    78 
    79 - (CardRank) rank       {return (self.serialNumber-1)%13 + 1;}
    80 - (CardSuit) suit       {return (self.serialNumber-1)/13;}
    81 
    82 - (CardColor) color
    83 {
    84     CardSuit suit = self.suit;
    85     return suit==kSuitDiamonds || suit==kSuitHearts ?kColorRed :kColorBlack;
    86 }
    87 
    88 
    89 - (NSString*) suitString
    90 {
    91     return [@"\u2663\u2666\u2665\u2660" substringWithRange: NSMakeRange(self.suit,1)];
    92 }
    93 
    94 - (NSString*) rankString
    95 {
    96     CardRank rank = self.rank;
    97     if( rank == 10 )
    98         return @"10";
    99     else
   100         return [@"A234567890JQK" substringWithRange: NSMakeRange(rank-1,1)];
   101 }
   102 
   103 - (CGColorRef) suitColor
   104 {
   105     static CGColorRef kSuitColor[4];
   106     if( ! kSuitColor[0] ) {
   107         kSuitColor[0] = kSuitColor[3] = kBlackColor;
   108         kSuitColor[1] = kSuitColor[2] = CreateRGB(1, 0, 0, 1);
   109     }
   110     return kSuitColor[self.suit];
   111 }
   112 
   113 
   114 - (NSString*) faceSymbol
   115 {
   116     int rank = self.rank;
   117     if( rank < kRankJack )
   118         return self.suitString;
   119     else
   120         return [@"\u265E\u265B\u265A" substringWithRange: NSMakeRange(rank-kRankJack,1)]; // actually chess symbols
   121 }
   122 
   123 
   124 - (NSString*) description
   125 {
   126     return [NSString stringWithFormat: @"%@[%@%@]",self.class,self.rankString,self.suitString];
   127 }
   128 
   129 
   130 @end