Source/BoardView.m
changeset 22 4cb50131788f
parent 18 ed057f4a72ca
child 23 efe5d4523a23
     1.1 --- a/Source/BoardView.m	Wed Jul 16 10:49:04 2008 -0700
     1.2 +++ b/Source/BoardView.m	Thu Jul 31 11:18:13 2008 -0700
     1.3 @@ -23,13 +23,16 @@
     1.4  #import "BoardView.h"
     1.5  #import "Bit.h"
     1.6  #import "BitHolder.h"
     1.7 -#import "Game.h"
     1.8 +#import "Game+Protected.h"
     1.9  #import "Turn.h"
    1.10  #import "Player.h"
    1.11  #import "QuartzUtils.h"
    1.12  #import "GGBUtils.h"
    1.13  
    1.14  
    1.15 +#define kMaxPerspective 0.965   // 55 degrees
    1.16 +
    1.17 +
    1.18  @interface BoardView ()
    1.19  - (void) _findDropTarget: (NSPoint)pos;
    1.20  @end
    1.21 @@ -38,7 +41,7 @@
    1.22  @implementation BoardView
    1.23  
    1.24  
    1.25 -@synthesize table=_table;
    1.26 +@synthesize table=_table, gameBoardInset=_gameBoardInset;
    1.27  
    1.28  
    1.29  - (void) dealloc
    1.30 @@ -48,6 +51,41 @@
    1.31  }
    1.32  
    1.33  
    1.34 +- (void) _applyPerspective
    1.35 +{
    1.36 +    CATransform3D t;
    1.37 +    if( fabs(_perspective) >= M_PI/180 ) {
    1.38 +        CGSize size = self.layer.bounds.size;
    1.39 +        t = CATransform3DMakeTranslation(-size.width/2, -size.height/4, 0);
    1.40 +        t = CATransform3DConcat(t, CATransform3DMakeRotation(-_perspective, 1,0,0));
    1.41 +        
    1.42 +        CATransform3D pers = CATransform3DIdentity;
    1.43 +        pers.m34 = 1.0/-800;
    1.44 +        t = CATransform3DConcat(t, pers);
    1.45 +        t = CATransform3DConcat(t, CATransform3DMakeTranslation(size.width/2, 
    1.46 +                                                                size.height*(0.25 + 0.05*sin(2*_perspective)),
    1.47 +                                                                0));
    1.48 +        self.layer.borderWidth = 3;
    1.49 +    } else {
    1.50 +        t = CATransform3DIdentity;
    1.51 +        self.layer.borderWidth = 0;
    1.52 +    }
    1.53 +    self.layer.transform = t;
    1.54 +}    
    1.55 +
    1.56 +- (CGFloat) perspective {return _perspective;}
    1.57 +
    1.58 +- (void) setPerspective: (CGFloat)p
    1.59 +{
    1.60 +    p = MAX(0.0, MIN(kMaxPerspective, p));
    1.61 +    if( p != _perspective ) {
    1.62 +        _perspective = p;
    1.63 +        [self _applyPerspective];
    1.64 +        _game.tablePerspectiveAngle = p;
    1.65 +    }
    1.66 +}
    1.67 +
    1.68 +
    1.69  - (void) _removeGameBoard
    1.70  {
    1.71      if( _table ) {
    1.72 @@ -62,7 +100,7 @@
    1.73      _table = [[CALayer alloc] init];
    1.74      _table.frame = [self gameBoardFrame];
    1.75      _table.autoresizingMask = kCALayerMinXMargin | kCALayerMaxXMargin | kCALayerMinYMargin | kCALayerMaxYMargin;
    1.76 -
    1.77 +    
    1.78      // Tell the game to set up the board:
    1.79      _game.table = _table;
    1.80  
    1.81 @@ -98,7 +136,7 @@
    1.82  
    1.83  - (CGRect) gameBoardFrame
    1.84  {
    1.85 -    return self.layer.bounds;
    1.86 +    return CGRectInset(self.layer.bounds, _gameBoardInset.width,_gameBoardInset.height);
    1.87  }
    1.88  
    1.89  
    1.90 @@ -115,7 +153,6 @@
    1.91      return _fullScreenView ?: self;
    1.92  }
    1.93  
    1.94 -
    1.95  - (IBAction) enterFullScreen: (id)sender
    1.96  {
    1.97      //[self _removeGameBoard];
    1.98 @@ -142,6 +179,7 @@
    1.99          CGAffineTransform xform = _table.affineTransform;
   1.100          xform.a = xform.d = MIN(newSize.width,newSize.height)/MIN(_oldSize.width,_oldSize.height);
   1.101          BeginDisableAnimations();
   1.102 +        [self _applyPerspective];
   1.103          _table.affineTransform = xform;
   1.104          EndDisableAnimations();
   1.105      } else
   1.106 @@ -181,7 +219,9 @@
   1.107  - (CGPoint) _convertPointFromWindowToLayer: (NSPoint)locationInWindow
   1.108  {
   1.109      NSPoint where = [self convertPoint: locationInWindow fromView: nil];    // convert to view coords
   1.110 -    return NSPointToCGPoint( [self convertPointToBase: where] );            // then to layer coords
   1.111 +    where = [self convertPointToBase: where];                               // then to layer base coords
   1.112 +    return [self.layer convertPoint: NSPointToCGPoint(where)                // then to transformed layer coords
   1.113 +                          fromLayer: self.layer.superlayer];
   1.114  }
   1.115  
   1.116  
   1.117 @@ -387,6 +427,13 @@
   1.118  }
   1.119  
   1.120  
   1.121 +- (void)scrollWheel:(NSEvent *)e
   1.122 +{
   1.123 +    self.perspective += e.deltaY * M_PI/180;
   1.124 +    //Log(@"Perspective = %2.0f degrees (%5.3f radians)", self.perspective*180/M_PI, self.perspective);
   1.125 +}
   1.126 +
   1.127 +
   1.128  #pragma mark -
   1.129  #pragma mark INCOMING DRAGS:
   1.130