static void draw(Cairo.Context gr, int width, int height) { int w, h; ImageSurface image; gr.Scale(width, height); gr.LineWidth = 0.04; gr.Arc(0.5, 0.5, 0.3, 0, 2 * M_PI); gr.Clip(); gr.NewPath(); image = new ImageSurface("data/e.png"); w = image.Width; h = image.Height; gr.Scale(1.0 / w, 1.0 / h); image.Show(gr, 0, 0); image.Destroy(); gr.Arc(0.5, 0.5, 0.3, 0, 2 * M_PI); gr.Clip(); gr.NewPath(); gr.Rectangle(new PointD(0, 0), 1, 1); gr.Fill(); gr.Color = new Color(0, 1, 0, 1); gr.MoveTo(new PointD(0, 0)); gr.LineTo(new PointD(1, 1)); gr.MoveTo(new PointD(1, 0)); gr.LineTo(new PointD(0, 1)); gr.Stroke(); }
void RedrawBoard() { boardContext = Gdk.CairoHelper.Create(BoardArea.GdkWindow); double transx = Math.Abs((BoardArea.Allocation.Width - (boardBackground.Width * 0.75))) / 2; boardContext.Translate(transx, 0); boardContext.Scale(0.75, 0.75); boardBackground.Show(boardContext, 0, 0); PieceDisplay.DrawPieces(boardContext); boardContext.Dispose(); }
static void draw(Cairo.Context gr, int width, int height) { int w, h; ImageSurface image; gr.Scale(width, height); gr.LineWidth = 0.04; image = new ImageSurface("data/e.png"); w = image.Width; h = image.Height; gr.Translate(0.5, 0.5); gr.Rotate(45 * M_PI / 180); gr.Scale(1.0 / w, 1.0 / h); gr.Translate(-0.5 * w, -0.5 * h); image.Show(gr, 0, 0); image.Destroy(); }
public static void DrawPieces(Cairo.Context cr) { for (int i = 0; i < 64; i++) { Piece currentPiece = MainClass.CurrentBoard.Squares [i].Piece; PointD currentPoint = MainClass.BoardOrientation == PieceColour.White ? pieceCoordinates [i] : pieceCoordinates [Math.Abs(i - 63)]; if (currentPiece == null) { continue; } if (currentPiece.Colour == PieceColour.White) { switch (currentPiece.Type) { case PieceType.Pawn: whitePawn.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Rook: whiteRook.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Knight: whiteKnight.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Bishop: whiteBishop.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Queen: whiteQueen.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.King: whiteKing.Show(cr, currentPoint.X, currentPoint.Y); break; default: break; } } else { switch (currentPiece.Type) { case PieceType.Pawn: blackPawn.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Rook: blackRook.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Knight: blackKnight.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Bishop: blackBishop.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.Queen: blackQueen.Show(cr, currentPoint.X, currentPoint.Y); break; case PieceType.King: blackKing.Show(cr, currentPoint.X, currentPoint.Y); break; default: break; } } } }
protected override void DrawOff(Cairo.Context context, IDrawingView view) { RectangleD r = ViewDisplayBox(view); m_imageOff.Show(context, Math.Round(r.X), Math.Round(r.Y)); }
protected void OnPieceClick(object o, ButtonPressEventArgs args) { double transx = Math.Abs((BoardArea.Allocation.Width - (boardBackground.Width * 0.75))) / 2; PointD clickLocation = new PointD(args.Event.X - transx, args.Event.Y - transx); if (clickLocation.X < 30 || clickLocation.Y < 30 || clickLocation.X > 522 || clickLocation.Y > 522) { return; } PointD pieceLocation = PieceDisplay.pieceCoordinates [0]; int pieceIndex = 0; for (int i = 0; i < PieceDisplay.pieceCoordinates.Length; i++) { PointD p = PieceDisplay.pieceCoordinates[i]; double x1 = p.X * 0.75; double y1 = p.Y * 0.75; double x2 = x1 + 61.5; double y2 = y1 + 61.5; if (x1 <= clickLocation.X && clickLocation.X <= x2) { if (y1 <= clickLocation.Y && clickLocation.Y <= y2) { pieceLocation = p; pieceIndex = MainClass.BoardOrientation == PieceColour.White ? i : Math.Abs(i - 63); break; } } } if (currentSelectionState == PieceSelectionState.None) { if (MainClass.CurrentBoard.Squares [pieceIndex].Piece == null) { return; } selectedPiece = (byte)pieceIndex; boardContext = Gdk.CairoHelper.Create(BoardArea.GdkWindow); boardContext.Translate(transx, 0); boardContext.Scale(0.75, 0.75); selectionBorder.Show(boardContext, pieceLocation.X + 1, pieceLocation.Y + 1); boardContext.Dispose(); currentSelectionState = PieceSelectionState.Selected; } else { if (!MainClass.CurrentBoard.IsMoveValid(selectedPiece, (byte)pieceIndex)) { currentSelectionState = PieceSelectionState.None; Gtk.Application.Invoke(delegate { RedrawBoard(); }); return; } if (MainClass.CurrentGameStatus != GameStatus.Active && MainClass.CurrentGameStatus != GameStatus.Inactive) { Console.Error.WriteLine("(EE) Attempted move during finished game."); MessageDialog errorDialog = new MessageDialog( this, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, "The game is over!"); errorDialog.Run(); errorDialog.Destroy(); return; } // Handle pawn promotion PieceType?promoteTo = null; if (MainClass.CurrentBoard.Squares [selectedPiece].Piece.Type == PieceType.Pawn && MainClass.CurrentBoard.IsMoveValid(selectedPiece, (byte)pieceIndex) && Array.IndexOf(MainClass.CurrentBoard.pawnPromotionDestinations, (byte)pieceIndex) != -1) { PawnPromotionDialog dialog = new PawnPromotionDialog(); if (dialog.Run() == (int)Gtk.ResponseType.Ok) { promoteTo = dialog.PromoteTo; } else { dialog.Destroy(); return; } dialog.Destroy(); } try { SpecifierType specifierRequired = GameHistory.checkDisabiguationNeeded(MainClass.CurrentBoard, selectedPiece, (byte)pieceIndex); MoveResult result = MainClass.CurrentBoard.MakeMove(selectedPiece, (byte)pieceIndex, promoteTo); Piece movingPiece = null; if (promoteTo == null) { movingPiece = MainClass.CurrentBoard.Squares[(byte)pieceIndex].Piece; } else { movingPiece = new Piece(MainClass.CurrentBoard.Squares [(byte)pieceIndex].Piece.Colour, PieceType.Pawn); } if (result == MoveResult.Capture && movingPiece.Type == PieceType.Pawn) { specifierRequired = SpecifierType.File; } int checkOrCheckmate = 0; GameStatus mateState = MainClass.CurrentBoard.CheckForMate(); if (mateState == GameStatus.WhiteCheckmate || mateState == GameStatus.BlackCheckmate) { checkOrCheckmate = 2; } else if (MainClass.CurrentBoard.WhiteCheck || MainClass.CurrentBoard.BlackCheck) { checkOrCheckmate = 1; } string fenPosition = MainClass.CurrentBoard.ToFEN().Split(' ')[0]; MainClass.CurrentGameHistory.AddMove(new Move(selectedPiece, (byte)pieceIndex, MainClass.CurrentBoard.Squares [(byte)pieceIndex].Piece.Colour, movingPiece, result, MainClass.CurrentBoard.ToFEN(), checkOrCheckmate, specifierRequired, promoteTo), fenPosition); UpdateGameHistoryView(); if (MainClass.CurrentGameHistory.UpdateFiftyMoveCount(result) == GameStatus.DrawFifty) { MainClass.CurrentGameStatus = GameStatus.DrawFifty; } else if (MainClass.CurrentGameHistory.CheckThreefoldRepetition() == GameStatus.DrawRepetition) { MainClass.CurrentGameStatus = GameStatus.DrawRepetition; } } catch (InvalidOperationException) { } Gtk.Application.Invoke(delegate { RedrawBoard(); }); GameStatus isMate = MainClass.CurrentBoard.CheckForMate(); if (isMate != GameStatus.Active) { MainClass.CurrentGameStatus = isMate; } if (MainClass.CurrentGameStatus != GameStatus.Active && MainClass.CurrentGameStatus != GameStatus.Inactive) { ShowGameOverDialog(MainClass.CurrentGameStatus); } if (MainClass.CurrentGameStatus == GameStatus.Inactive) { MainClass.CurrentGameStatus = GameStatus.Active; } Gtk.Application.Invoke(delegate { MainClass.UpdateClock(); UpdatePlayerToMove(); }); currentSelectionState = PieceSelectionState.None; } }