Source/BoardView.m
changeset 11 436cbdf56810
parent 9 a59acc683080
child 12 4e567e11f45f
     1.1 --- a/Source/BoardView.m	Thu May 29 15:04:06 2008 -0700
     1.2 +++ b/Source/BoardView.m	Sat Jul 05 17:46:43 2008 -0700
     1.3 @@ -24,6 +24,7 @@
     1.4  #import "Bit.h"
     1.5  #import "BitHolder.h"
     1.6  #import "Game.h"
     1.7 +#import "Player.h"
     1.8  #import "QuartzUtils.h"
     1.9  #import "GGBUtils.h"
    1.10  
    1.11 @@ -36,7 +37,7 @@
    1.12  @implementation BoardView
    1.13  
    1.14  
    1.15 -@synthesize game=_game, gameboard=_gameboard;
    1.16 +@synthesize gameboard=_gameboard;
    1.17  
    1.18  
    1.19  - (void) dealloc
    1.20 @@ -46,20 +47,63 @@
    1.21  }
    1.22  
    1.23  
    1.24 +- (void) _removeGameBoard
    1.25 +{
    1.26 +    if( _gameboard ) {
    1.27 +        RemoveImmediately(_gameboard);
    1.28 +        _gameboard = nil;
    1.29 +    }
    1.30 +}
    1.31 +
    1.32 +- (void) createGameBoard
    1.33 +{
    1.34 +    [self _removeGameBoard];
    1.35 +    _gameboard = [[CALayer alloc] init];
    1.36 +    _gameboard.frame = [self gameBoardFrame];
    1.37 +    _gameboard.autoresizingMask = kCALayerMinXMargin | kCALayerMaxXMargin | kCALayerMinYMargin | kCALayerMaxYMargin;
    1.38 +
    1.39 +    // Tell the game to set up the board:
    1.40 +    _game.board = _gameboard;
    1.41 +
    1.42 +    [self.layer addSublayer: _gameboard];
    1.43 +    [_gameboard release];
    1.44 +}
    1.45 +
    1.46 +
    1.47 +- (void) reverseBoard
    1.48 +{
    1.49 +    [_gameboard setValue: [NSNumber numberWithDouble: M_PI]
    1.50 +              forKeyPath: @"transform.rotation"];
    1.51 +}
    1.52 +
    1.53 +
    1.54 +- (Game*) game
    1.55 +{
    1.56 +    return _game;
    1.57 +}
    1.58 +
    1.59 +- (void) setGame: (Game*)game
    1.60 +{
    1.61 +    if( game!=_game ) {
    1.62 +        setObj(&_game,game);
    1.63 +        [self createGameBoard];
    1.64 +    }
    1.65 +}
    1.66 +
    1.67  - (void) startGameNamed: (NSString*)gameClassName
    1.68  {
    1.69 -    if( _gameboard ) {
    1.70 -        [_gameboard removeFromSuperlayer];
    1.71 -        _gameboard = nil;
    1.72 +    Class gameClass = NSClassFromString(gameClassName);
    1.73 +    Game *game = [[gameClass alloc] init];
    1.74 +    if( game ) {
    1.75 +        self.game = game;
    1.76 +        [game release];
    1.77      }
    1.78 -    _gameboard = [[CALayer alloc] init];
    1.79 -    _gameboard.frame = [self gameBoardFrame];
    1.80 -    _gameboard.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
    1.81 -    [self.layer addSublayer: _gameboard];
    1.82 -    [_gameboard release];
    1.83 -    
    1.84 -    Class gameClass = NSClassFromString(gameClassName);
    1.85 -    setObj(&_game, [[gameClass alloc] initWithBoard: _gameboard]);
    1.86 +}
    1.87 +
    1.88 +
    1.89 +- (BOOL) canMakeMove
    1.90 +{
    1.91 +    return (_game && _game.currentPlayer.local && _game.currentTurnNo==_game.maxTurnNo);
    1.92  }
    1.93  
    1.94  
    1.95 @@ -72,18 +116,46 @@
    1.96  - (void)resetCursorRects
    1.97  {
    1.98      [super resetCursorRects];
    1.99 -    [self addCursorRect: self.bounds cursor: [NSCursor openHandCursor]];
   1.100 +    if( self.canMakeMove )
   1.101 +        [self addCursorRect: self.bounds cursor: [NSCursor openHandCursor]];
   1.102  }
   1.103  
   1.104  
   1.105  - (IBAction) enterFullScreen: (id)sender
   1.106  {
   1.107 +    [self _removeGameBoard];
   1.108      if( self.isInFullScreenMode ) {
   1.109          [self exitFullScreenModeWithOptions: nil];
   1.110      } else {
   1.111          [self enterFullScreenMode: self.window.screen 
   1.112                        withOptions: nil];
   1.113      }
   1.114 +    [self createGameBoard];
   1.115 +}
   1.116 +
   1.117 +
   1.118 +- (void)viewWillStartLiveResize
   1.119 +{
   1.120 +    [super viewWillStartLiveResize];
   1.121 +    _oldSize = self.frame.size;
   1.122 +}
   1.123 +
   1.124 +- (void)setFrameSize:(NSSize)newSize
   1.125 +{
   1.126 +    [super setFrameSize: newSize];
   1.127 +    if( _oldSize.width > 0.0f ) {
   1.128 +        CGAffineTransform xform = _gameboard.affineTransform;
   1.129 +        xform.a = xform.d = MIN(newSize.width,newSize.height)/MIN(_oldSize.width,_oldSize.height);
   1.130 +        _gameboard.affineTransform = xform;
   1.131 +    } else
   1.132 +        [self createGameBoard];
   1.133 +}
   1.134 +
   1.135 +- (void)viewDidEndLiveResize
   1.136 +{
   1.137 +    [super viewDidEndLiveResize];
   1.138 +    _oldSize.width = _oldSize.height = 0.0f;
   1.139 +    [self createGameBoard];
   1.140  }
   1.141  
   1.142  
   1.143 @@ -149,6 +221,11 @@
   1.144  
   1.145  - (void) mouseDown: (NSEvent*)ev
   1.146  {
   1.147 +    if( ! self.canMakeMove ) {
   1.148 +        NSBeep();
   1.149 +        return;
   1.150 +    }
   1.151 +    
   1.152      BOOL placing = NO;
   1.153      _dragStartPos = ev.locationInWindow;
   1.154      _dragBit = (Bit*) [self hitTestPoint: _dragStartPos