#include <sys/types.h>
#include <sys/time.h>
#import <appkit/appkit.h>

#import "Square3D.h"
#import "gnuchess.h"

#define PIECE_WIDTH_3D 54.0
#define PIECE_HEIGHT_3D 95.0

@implementation Square3D : Cell
/*
  This class represents one square on a chess board.  It has a color
  and may contain a piece.
*/

- setMoving: (BOOL)flag { moving = flag; }
- (BOOL) moving { return moving; }
- setType: (int)t color: (int) c 
{
  type = t;
  color = c;
}

- (NXRect *)location { return &location; }
- setLocation: (NXRect *)r { location = *r; }
- (int)type { return type; }
- (int)colorVal { return color; }

- setRow: (int)r { row = r; }
- setBackground: (float) b { background = b; }

- drawSelf:(const NXRect *)f inView: v
{
  [self drawInside: f inView: v];
}
- drawBackground:(const NXRect *)f inView: v
{
}

- highlight: (const NXRect *)f inView: v
{
}
  


/*
Pieces in the big bitmap each are 55 wide 95 heigh and seperated
by a 1 pixel wide line, with no line at the bottom.  They are ordered
king,queen,bishop,rook,knight,pawn -- white then black.
*/

extern id pieces3D;
- drawInside:(const NXRect *)r inView:v
/*
  Draw the chess piece.
*/
{
  NXPoint p;
  NXRect f;
  id bitmap;
  int col;
  NXCoord shrinkFactor;
  
  /* Composite the piece icon in the center of the rect. */
  bitmap = pieces3D;
  switch( type ){
  case pawn:	col = 5;	break;
  case rook:	col = 3;	break;
  case knight:	col = 4;	break;
  case bishop:	col = 2;	break;
  case queen:	col = 1;	break;
  case king:	col = 0;	break;
  default:  return self;
  }
  shrinkFactor = (.65 + ((8-row)*0.03125));
  f.origin.x = (col+(color == black ? 6 : 0))*56+1;
  f.origin.y = row*96;
  f.size.width = PIECE_WIDTH_3D;
  f.size.height = PIECE_HEIGHT_3D;
  p.x = location.origin.x + 
      (location.size.width - (PIECE_WIDTH_3D * shrinkFactor)) / 2 +
      (5 * shrinkFactor);
  p.y = location.origin.y + (8 * shrinkFactor);
  
  p.x = floor(p.x); p.y = floor(p.y);
  
  [v lockFocus];
  [bitmap composite: NX_SOVER fromRect: &f toPoint: &p];
  [v unlockFocus];
  return self;
}


@end
