Source/Grid.m
changeset 23 efe5d4523a23
parent 15 73f8c889f053
child 24 db8640a38faf
     1.1 --- a/Source/Grid.m	Wed Jul 09 17:07:45 2008 -0700
     1.2 +++ b/Source/Grid.m	Thu Jul 31 13:23:44 2008 -0700
     1.3 @@ -28,6 +28,11 @@
     1.4  #import "QuartzUtils.h"
     1.5  
     1.6  
     1.7 +@interface GridCell ()
     1.8 +- (void) setBitTransform: (CATransform3D)bitTransform;
     1.9 +@end
    1.10 +
    1.11 +
    1.12  @implementation Grid
    1.13  
    1.14  
    1.15 @@ -45,6 +50,7 @@
    1.16          self.lineColor = kBlackColor;
    1.17          _allowsMoves = YES;
    1.18          _usesDiagonals = YES;
    1.19 +        _bitTransform = CATransform3DIdentity;
    1.20  
    1.21          self.bounds = CGRectMake(-1, -1, nColumns*spacing.width+2, nRows*spacing.height+2);
    1.22          self.position = pos;
    1.23 @@ -113,7 +119,8 @@
    1.24  }
    1.25  
    1.26  @synthesize cellClass=_cellClass, rows=_nRows, columns=_nColumns, spacing=_spacing, reversed=_reversed,
    1.27 -            usesDiagonals=_usesDiagonals, allowsMoves=_allowsMoves, allowsCaptures=_allowsCaptures;
    1.28 +            usesDiagonals=_usesDiagonals, allowsMoves=_allowsMoves, allowsCaptures=_allowsCaptures,
    1.29 +            bitTransform=_bitTransform;
    1.30  
    1.31  
    1.32  #pragma mark -
    1.33 @@ -160,7 +167,8 @@
    1.34          cell = [self createCellAtRow: row column: col suggestedFrame: frame];
    1.35          if( cell ) {
    1.36              [_cells replaceObjectAtIndex: index withObject: cell];
    1.37 -            [self addSublayer: cell];
    1.38 +            //[self addSublayer: cell];
    1.39 +            [self insertSublayer: cell atIndex: 0];
    1.40              [self setNeedsDisplay];
    1.41          }
    1.42      }
    1.43 @@ -221,6 +229,26 @@
    1.44  }
    1.45  
    1.46  
    1.47 +- (CATransform3D) bitTransform
    1.48 +{
    1.49 +    return _bitTransform;
    1.50 +}
    1.51 +
    1.52 +- (void) setBitTransform: (CATransform3D)t
    1.53 +{
    1.54 +    _bitTransform = t;
    1.55 +    for( GridCell *cell in self.cells )
    1.56 +        [cell setBitTransform: t];
    1.57 +}
    1.58 +
    1.59 +- (void) updateCellTransform
    1.60 +{
    1.61 +    CATransform3D t = self.aggregateTransform;
    1.62 +    t.m41 = t.m42 = t.m43 = 0.0f;           // remove translation component
    1.63 +    t = CATransform3DInvert(t);
    1.64 +    self.bitTransform = t;
    1.65 +}
    1.66 +
    1.67  
    1.68  #pragma mark -
    1.69  #pragma mark GAME STATE:
    1.70 @@ -332,13 +360,14 @@
    1.71          _grid = grid;
    1.72          _row = row;
    1.73          _column = col;
    1.74 +        self.anchorPoint = CGPointMake(0,0);
    1.75          self.position = frame.origin;
    1.76          CGRect bounds = frame;
    1.77          bounds.origin.x -= floor(bounds.origin.x);  // make sure my coords fall on pixel boundaries
    1.78          bounds.origin.y -= floor(bounds.origin.y);
    1.79          self.bounds = bounds;
    1.80 -        self.anchorPoint = CGPointMake(0,0);
    1.81          self.borderColor = kHighlightColor;         // Used when highlighting (see -setHighlighted:)
    1.82 +        [self setBitTransform: grid.bitTransform];
    1.83      }
    1.84      return self;
    1.85  }
    1.86 @@ -351,6 +380,18 @@
    1.87  @synthesize grid=_grid, row=_row, column=_column;
    1.88  
    1.89  
    1.90 +- (void) setBitTransform: (CATransform3D)bitTransform
    1.91 +{
    1.92 +    // To make the bitTransform relative to my center, I need to offset the center to the origin
    1.93 +    // first, and then back afterwards.
    1.94 +    CGSize size = self.bounds.size;
    1.95 +    CATransform3D x = CATransform3DMakeTranslation(-size.width/2, -size.height/2,0);
    1.96 +    x = CATransform3DConcat(x, bitTransform);
    1.97 +    x = CATransform3DConcat(x, CATransform3DMakeTranslation(size.width/2, size.height/2,0));
    1.98 +    self.sublayerTransform = x;    
    1.99 +}
   1.100 +
   1.101 +
   1.102  - (void) drawInParentContext: (CGContextRef)ctx fill: (BOOL)fill
   1.103  {
   1.104      // Default implementation just fills or outlines the cell.
   1.105 @@ -366,12 +407,8 @@
   1.106  {
   1.107      if( bit != self.bit ) {
   1.108          [super setBit: bit];
   1.109 -        if( bit ) {
   1.110 -            // Center it:
   1.111 -            CGSize size = self.bounds.size;
   1.112 -            bit.position = CGPointMake(floor(size.width/2.0),
   1.113 -                                       floor(size.height/2.0));
   1.114 -        }
   1.115 +        if( bit )
   1.116 +            bit.position = GetCGRectCenter(self.bounds);
   1.117      }
   1.118  }
   1.119