void ui_MouseUp(object sender, MouseEventArgs e) { if (null == BeforeMove) return; Selected = false; if (ev == null) return; foreach (var item in ev.Options) { item.Hide = false; } bool found = false; foreach (var item in ev.Options) { var x = xt.ResolveX(item.X, inverted); var y = xt.ResolveY(item.Y, inverted); if (ui.Left > x && ui.Left - x < Square.Size - Square.Padding && ui.Top > y && ui.Top - y < Square.Size - Square.Padding) { if (null != Move) Move(this, new PieceEventArgs(item)); found = true; break; } } if (!found) Square = Square; //fall back ev = null; moving = false; }
private void piece_Move(object sender, PieceEventArgs e) { if (e.NewSquarePiece.HasValue) { xt.Item(AllPieces, e.NewSquarePiece.Value).Kill(board, bKilled, wKilled); } Piece p = xt.Item(AllPieces, (sender as Piece).Id); bool castling = p.Id.Who == Who.King && Math.Abs(e.NewSquare.X - p.Square.X) > 1; bool originatesHere = AllPieces.Contains(sender as Piece); moved.Add(p.Id); p.Square.Piece = null; p.Square = AllSquares[e.NewSquare.X, e.NewSquare.Y]; if (!castling) { blacksTurn = !blacksTurn; } else if (originatesHere) { Piece rook = xt.Item(AllPieces, AllSquares[e.NewSquare.X >= 4 ? 8 : 1, p.Square.Y].Piece.Value); Square rookTo = AllSquares[e.NewSquare.X + (e.NewSquare.X >= 4 ? -1 : 1), p.Square.Y]; piece_Move(rook, new PieceEventArgs(rookTo)); } if (originatesHere && null != PieceMove) PieceMove(sender, e); }
void ui_MouseDown(object sender, MouseEventArgs e) { if (null == BeforeMove) return; ev = new PieceEventArgs(); BeforeMove(this, ev); if (!ev.Movable) return; moving = true; ui.BringToFront(); starting = xt.Subtract(Control.MousePosition, ui.Location); Selected = true; foreach (var item in ev.Options) { item.Hide = true; } }
private void piece_BeforeMove(object sender, PieceEventArgs e) { Piece piece = sender as Piece; board.FindForm().Text = string.Format("Moving {0} from {1}", piece.Name, piece.Square.Name); if (piece.Id.Black != blacksTurn || (null != otherBoard && blacksTurn != inverted)) return; new MoveResolver(e.Options, piece, AllSquares, moved).Resolve(); }